-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHigherLower.java
More file actions
82 lines (73 loc) · 2.6 KB
/
HigherLower.java
File metadata and controls
82 lines (73 loc) · 2.6 KB
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
import java.util.*;
public class HigherLower {
static Deck deck = new Deck();
static Tableau tableau = new Tableau();
static Scanner scanner = new Scanner(System.in);
static int score = 0;
public static void main(String[] args) {
/**
* So this is higher or lower,
* Cards are drawn from a deck
* The first card is flipped over
* then the user guesses higher or lower
* if the user guesses higher and the next card is higher than the previous, the
* user continues and gets a point
* if the user is wrong, then the game ends and the score is shown
*/
printWelcome();
// Draw the first card!
tableau.addCard(deck.draw());
Card lastCard = tableau.lastCard();
Card newCard;
tableau.printCards();
String userInput = getUserInput();
game: while (!userInput.equalsIgnoreCase("quit")) {
tableau.addCard(deck.draw());
newCard = tableau.lastCard();
switch (lastCard.compareTo(newCard)) {
case -1:
if (userInput.equals("-")) {
correct();
} else {
gameOver();
break game;
}
break;
case 1:
if (userInput.equals("+")) {
gameOver();
break game;
} else {
correct();
}
break;
default:
correct();
}
tableau.printCards();
userInput = getUserInput();
}
tableau.printFinalCards();
System.out.println("Your score was: " + score);
}
public static void resetGame() {
deck = new Deck();
tableau = new Tableau();
}
public static void printWelcome() {
System.out.println("Welcome to Higher Or Lower!");
System.out.println("We'll flip a card for you, and you have to tell us if the next card is higher or lower!");
System.out.println("Let's draw the first card!");
}
public static String getUserInput() {
System.out.println("Type '+' for higher, type '-' for lower, and type 'quit' to end the game!");
return scanner.next();
}
public static void correct() {
System.out.println("You are correct! You get a point!");
score += 1;
}
public static void gameOver() {
System.out.println("Oh.. that's too bad, you were wrong.");
}
}