Java 5 - Interactive Terminal & Java Poker Game
Interactive Terminal
- The
Scannerclass is used to get user input, and it is found in thejava.utilpackage. - We can use the
Scannerclass to get user input in the following way:
1
2
3
4
5
6
7
8
9
10
11
import java.util.Scanner; // Import the Scanner class
public class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");
String userName = myObj.nextLine(); // Read user input
System.out.println("Username is: " + userName); // Output user input
}
}
The
nextLine()method is used to read Strings, and thenextInt()method is used to read integers.nextDouble()is used to read doubles.In order to avoid memory leaks, we should close the
Scannerobject after use.
1
2
3
4
5
6
7
8
9
10
11
12
import java.util.Scanner; // Import the Scanner class
public class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");
String userName = myObj.nextLine(); // Read user input
System.out.println("Username is: " + userName); // Output user input
myObj.close(); // Close the Scanner object
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.Scanner;
public class Survey {
public static void main(String[] args) {
//*********PART A: PICKING UP THE USER'S ANSWERS*********
System.out.println("Welcome. Thank you for taking the survey");
Scanner scanner = new Scanner(System.in);
int counter = 0;
System.out.println("\nWhat is your name?");
String name = scanner.nextLine();
counter++;
System.out.println("\nHow much money do you spend on coffee?");
double coffeePrice = scanner.nextDouble();
counter++;
System.out.println("\nHow much money do you spend on fast food?");
double foodPrice = scanner.nextDouble();
counter++;
System.out.println("\nHow many times a week do you buy coffee?");
int coffeeAmount = scanner.nextInt();
counter++;
System.out.println("\nHow many times a week do you buy fast food?");
int foodAmount = scanner.nextInt();
counter++;
scanner.close();
//*********PART B: RESPONDING TO THE USER**********
//System.out.println("Thank you <name> for answering all <counter> questions");
System.out.println("\nThank you " + name + " for answering all " + counter + " questions");
//System.out.println("Weekly, you spend $<totalPrice> on coffee");
System.out.println("Weekly, you spend" + " €"+ (coffeePrice * coffeeAmount) + " on coffee");
//System.out.println("Weekly, you spend $<totalPrice> on food");
System.out.println("Weekly, you spend" + " €"+ (foodPrice * foodAmount) + " on food");
}
}
The Java Poker Game
- This game incoporates the use of functions, loops, conditionals, and the previous concepts we have learned.
The Game
- The game is a simple poker game that runs in the terminal.
- The game is played between the user and the computer.
- The computer generates a random number between 1 and 13, picks a random suit, and displays the card for both the user and the computer.
- The dealer asks the user to draw 5 cards.
- Each time a matching card is drawn, the user or the computer gets a point.
- The player with the most points wins the game.
The tasks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/** Task 1
* create a function that returns a random card
* Function name – randomCard
* @return (String)
*
* Inside the function:
* 1. Gets a random number between 1 and 13.
* 2. Returns a card that matches the random number (get the String values from cards.text).
*/
/*Task 2: Explain the rules
>>Let's play Pokerito. Type anything when you're ready.
|
>>It's like Poker, but a lot simpler.
>> (new line)
>> • There are two players, you and the computer.
>> • The dealer will give each player one card.
>> • Then, the dealer will draw five cards (the river)
>> • The player with the most river matches wins!
>> • If the matches are equal, everyone's a winner!
>> (new line)
>> • Ready? Type anything if you are.
|
*/
/*Task 3: Present the user with a card
println 'Here's your card:'
<show card>
<new line>
println 'Here's the computer's card:'
<show computer's card>
*/
/** Task 4 - Draw five cards
*
* • print: Now, the dealer will draw five cards. Press enter to continue.
* • The dealer will draw a card every time the user presses enter.
* • Before you draw a card, print the order it was drawn in:
* Card 1
* <2 new lines>
* <print card>
* Card 2
* <2 new lines>
* <print card>
* ...
*/
/** Task 5 - Get the winner
*
* • Count your number of matches.
* • Count the computer's number of matches.
* • print: Your number of matches: <yourMatches>
* • print: Computer number of matches: <computerMatches>
*
* • If you have more matches, print: You win!.
* • If the computer has more matches, print: The computer wins!
* • If the matches are equal, print: everyone wins!.
*/
The Solution
- Task 1:
- Function name –
randomCardthat returns aString. - Inside the function:
- Gets a random number between 1 and 13.
- Return a card that matches the random number (get the String values from cards.text).
- Function name –
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
public static String randomCard() {
double randomNum = Math.random() * 13 + 1;
int randomInt = (int)randomNum;
switch (randomInt){
case 1:
return " _____\n"+
" |A _ |\n"+
" | ( ) |\n"+
" |(_'_)|\n"+
" | | |\n"+
" |____V|\n";
case 2:
return " _____\n"+
" |2 |\n"+
" | o |\n"+
" | |\n"+
" | o |\n"+
" |____Z|\n";
case 3:
return " _____\n" +
" |3 |\n"+
" | o o |\n"+
" | |\n"+
" | o |\n"+
" |____E|\n";
case 4:
return " _____\n" +
" |4 |\n"+
" | o o |\n"+
" | |\n"+
" | o o |\n"+
" |____h|\n";
case 5:
return " _____\n" +
" |5 |\n"+
" | o o |\n"+
" | o |\n"+
" | o o |\n"+
" |____S|\n";
case 6:
return " _____\n" +
" |6 |\n"+
" | o o |\n"+
" | o o |\n"+
" | o o |\n"+
" |____9|\n";
case 7:
return " _____\n" +
" |7 |\n"+
" | o o |\n"+
" |o o o|\n"+
" | o o |\n"+
" |____L|\n";
case 8:
return " _____\n" +
" |8 |\n"+
" |o o o|\n"+
" | o o |\n"+
" |o o o|\n"+
" |____8|\n";
case 9:
return " _____\n" +
" |9 |\n"+
" |o o o|\n"+
" |o o o|\n"+
" |o o o|\n"+
" |____6|\n";
case 10:
return " _____\n" +
" |10 o|\n"+
" |o o o|\n"+
" |o o o|\n"+
" |o o o|\n"+
" |___J |\n";
case 11:
return " _____\n" +
" |J ww|\n"+
" | o {)|\n"+
" |o o% |\n"+
" | | % |\n"+
" |__%%[|\n";
case 12:
return " _____\n" +
" |Q ww|\n"+
" | o {(|\n"+
" |o o%%|\n"+
" | |%%%|\n"+
" |_%%%O|\n";
case 13:
return " _____\n" +
" |K WW|\n"+
" | o {)|\n"+
" |o o%%|\n"+
" | |%%%|\n"+
" |_%%%>|\n";
default:
return "This should never happen.";
}
}
- Task 2:
- Explain the rules:
- Let’s play Pokerito. Type anything when you’re ready.
- It’s like Poker, but a lot simpler.
- There are two players, you and the computer.
- The dealer will give each player one card.
- Then, the dealer will draw five cards (the river)
- The player with the most river matches wins!
- If the matches are equal, everyone’s a winner!
- Ready? Type anything if you are.
- Explain the rules:
1
2
3
4
5
6
7
8
9
10
11
12
13
System.out.println("Let's play Pokerito. Type anything when you're ready.");
scan.nextLine();
System.out.println("It's like Poker, but a lot simpler.\n" +
"\n" +
"• There are two players, you and the computer.\n" +
"• The dealer will give each player one card.\n" +
"• Then, the dealer will draw five cards (the river)\n" +
"• The player with the most river matches wins!\n" +
"• If the matches are equal, everyone's a winner!\n" +
"\n" +
"• Ready? Type anything if you are.");
scan.nextLine();
- Task 3:
- Present the user with a card
- println ‘Here’s your card:’
-
-
- println ‘Here’s the computer’s card:’
- <show computer’s card>
- Present the user with a card
1
2
3
4
5
6
7
8
9
String yourCard = randomCard();
String computerCard = randomCard();
System.out.println("Here's your card:");
System.out.println(yourCard);
System.out.println();
System.out.println("Here's the computer's card:");
System.out.println(computerCard);
- Task 4:
- Draw five cards
- print: Now, the dealer will draw five cards. Press enter to continue.
- The dealer will draw a card every time the user presses enter.
- Before you draw a card, print the order it was drawn in:
- Card 1
- <2 new lines>
- Card 2
- <2 new lines>
- …
- Draw five cards
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int yourMatches = 0;
int computerMatches =0;
System.out.println("Now, the dealer will draw five cards. Press enter to continue.");
scan.nextLine();
for (int i = 1; i <= 5; i++) {
System.out.println("Card " + i);
System.out.println();
System.out.println(randomCard());
System.out.println();
scan.nextLine();
}
- Task 5:
- Get the winner
- Count your number of matches.
- Count the computer’s number of matches.
- print: Your number of matches:
- print: Computer number of matches:
- If you have more matches, print: You win!.
- If the computer has more matches, print: The computer wins!
- If the matches are equal, print: everyone wins!.
- Get the winner
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for (int i = 1; i <= 5; i++) {
if (yourCard.equals(randomCard())) {
yourMatches++;
}
if (computerCard.equals(randomCard())) {
computerMatches++;
}
}
System.out.println("Your number of matches: " + yourMatches);
System.out.println("Computer number of matches: " + computerMatches);
if (yourMatches > computerMatches) {
System.out.println("You win!");
} else if (computerMatches > yourMatches) {
System.out.println("The computer wins!");
} else {
System.out.println("Everyone wins!");
}
This post is licensed under CC BY 4.0 by the author.