0% found this document useful (0 votes)
33 views5 pages

Game 21

This document contains the code for a game of 21 between a user and computer. It defines constants like the minimum and maximum card values, draws random cards for both players' turns, checks if either player goes over 21 to end the game, and determines a winner by comparing final scores. The computer will draw cards until it reaches a maximum score of 50, while the user loses if they go over 21. It prompts the user to choose to "hit" or "stand" and continues play accordingly.

Uploaded by

Sophia Rosiee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views5 pages

Game 21

This document contains the code for a game of 21 between a user and computer. It defines constants like the minimum and maximum card values, draws random cards for both players' turns, checks if either player goes over 21 to end the game, and determines a winner by comparing final scores. The computer will draw cards until it reaches a maximum score of 50, while the user loses if they go over 21. It prompts the user to choose to "hit" or "stand" and continues play accordingly.

Uploaded by

Sophia Rosiee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

package Game21;

import java.util.Scanner;

public class GameMain {

static int min=2;

static int max=20;

static int userScore=0;

static int computerScore=0;

final static String


DASH="__________________________________________________________________________
______________________";

final static String INPUT =DASH +"\n"+ "Press 's' for \"Stand\" (not ask for another card) or 'h'
for \"hit\" (ask for another card)";

final static int MAXIMUMCOMPUTERLIMIT =50;

final static String STAR="*********************************************************";

public static void main(String args[])

System.out.println("*********************************************************************");

System.out.println("***************WELCOME TO GAME OF TWENTY ONE********");

System.out.println("*********************************************************************");

System.out.println( " NOTE : computer player will get another " +

"card until it reaches a maximum point of "


+MAXIMUMCOMPUTERLIMIT +".");

System.out.println("***************************************************");

// 1: Generate Random number for user and computer between 2 to 20

drawRandomCard();

System.out.println("\nUser's Points= " + userScore);

if(userScore > 21)

{
PrintOutput();

return;

// 2: Play the game further based on the user on the user decision

System.out.println(INPUT);

pickCards();

/* Main function where the user and the computer pickcards and all the conditions are
checked here

* Also user is asked to select the choice to stand or hit the card.

*/

static void pickCards()

Scanner reader = new Scanner(System.in);

char choice= reader.next().trim().charAt(0);

if( Character.toLowerCase(choice) =='s')

System.out.println("Game Over!!!");

System.out.println("\nUser's Points= " + userScore);

System.out.println("Computer's Points= " +computerScore);

if(userScore<=21)

System.out.println(STAR +"\n"+ "The User Wins!!!" +"\n" + STAR);

return;

else if(userScore > 21)

PrintOutput();

return;

}
if(Character.toLowerCase(choice) =='h')

//Pick card again

drawRandomCard();

System.out.println("\nUser's Points= " + userScore);

if(userScore > 21)

PrintOutput();

return;

else

//if you don't have 21 or more, it continues.

System.out.println(INPUT);

pickCards();//call this method again. This is known as recursion.

/*This function is to generate the minimum and maximum value of both user and computer

*/

static void drawRandomCard()

for(int i=0;i<2;i++)

if(i==0)

computerScore=computerScore+ min + (int)(Math.random() * (max -


min + 1));

else

int CurrentUserCardValue= min + (int)(Math.random() * (max - min


+ 1));

userScore=userScore+CurrentUserCardValue;

System.out.println("Current User Card drawn Value:"


+CurrentUserCardValue );
}

/* This function is to print the final output

*/

static void PrintOutput()

System.out.println("Computer's Score :" + computerScore);

System.out.println("\nGame over!!!\n");

// Here THe Computer Maximum limit is set to 50 and user Limit to 21

if(computerScore <=MAXIMUMCOMPUTERLIMIT)

System.out.println(STAR +"\n"+ "The Computer Wins!!!"+"\n" +


STAR);

return;

if(computerScore > MAXIMUMCOMPUTERLIMIT)

System.out.println(STAR +"\n"+"Computer and User both have


crossed their Maximum limit, Please start a new game if you wish"+"\n" + STAR);

return;

You might also like