-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
162 lines (131 loc) · 6.8 KB
/
Game.java
File metadata and controls
162 lines (131 loc) · 6.8 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.util.*;
public class Game {
public int totalGuesses; //variable to store user's total of guesses
private String input; //variable to handle terminal input
private MyRandomNumber rNumber; //variable to instance a new MyRandomNumber object
//Default Constructor
public Game () {
this.totalGuesses = 0; //start at 0 guesses
this.input = ""; //start empty
this.rNumber = new MyRandomNumber(); //new object
}
//Display main menu with game's modes
public short DisplayMenu() {
boolean isDone = false; //options
int selection = -1; //beginning of the game
Scanner inOption = new Scanner(System.in); //initiate input
System.out.println("---------------------------------------------------------");
System.out.println("| ### Choose from these choices ### |");
System.out.println("|-------------------------------------------------------|");
System.out.println("| 1 Easy : Guess a number between 1 and 20 |");
System.out.println("|-------------------------------------------------------|");
System.out.println("| 2 Normal: Guess a number between 1 and 100 |");
System.out.println("|-------------------------------------------------------|");
System.out.println("| 3 Hard : Guess a number between 1 and 1000 |");
System.out.println("|-------------------------------------------------------|");
System.out.println("| 0 Exit |");
System.out.println("---------------------------------------------------------");
System.out.print("> ");
//while not a valid choice, keep looping
while (!isDone) {
try {
//read input
selection = (inOption.nextInt());
//check if it is a valid option
isDone = (selection >= 0 && selection <= 3);
//if not ok, throw controlled exception
if (!isDone) {
throw new Exception("Please enter 0, 1, 2 or 3: ");
}
} catch (InputMismatchException e) {
//in case of not typing a digit, give err message and ask new guess
selection = 0;
System.err.println(inOption.next() + " was not valid input. Try again: ");
} catch (Exception e) {
//handle controlled exception
System.err.println(e.getMessage());
}
}
//when having valid option, call GameSetup method with the chosen option
return this.GameSetup( (short) selection);
}//END DisplayMenu
//Set up min and max values and return the secret number
private short GameSetup(short option) {
switch (option){
case 0:
return option;
case 1:
rNumber.SetMaximum(20); //20 is the max for Game Mode 1
break;
case 2:
rNumber.SetMaximum(100); //100 is the max for Game Mode 1
break;
case 3:
rNumber.SetMaximum(1000); //1000 is the max for Game Mode 1
break;
default:
this.DisplayMenu(); //in case of some different value passes, menu is called again
return -1;
}
rNumber.SetMinimum(1); // min value is always 1 for this game
//return a short Secret Number to be guessed
return (short) rNumber.generateRandomNumber();
}//END GameSetup
//controls all the game interactions.
public void PlayGame(short inNumber) {
boolean isRight = false; //control if the guess is right
Scanner inOption = new Scanner(System.in); //initiate new object to read input values
System.out.println("-------------------------------------");
System.out.println("| To leave the game press X ### |");
System.out.println("-------------------------------------");
//while is not the righe guess, os X is not pressed, continues looping
while (!isRight) {
try {
this.totalGuesses++; //increment number of tried guesses
System.out.println("Type your guess: ");
System.out.print("> ");
short guess = 0; //initiate guess as 0
input = inOption.nextLine();
//if input is x, user wants to exit game. Else, input guess is checked with Secret Number
if (input.equals("x") || input.equals("X")) {
break;
} else {
guess = Short.parseShort(input); //get input guess
isRight = (inNumber == guess); //set isRight true or false according to the guess
}
//if not the right number, clear screen, give a hint, and start the process again
if (!isRight) {
System.out.println("--------------");
System.out.println(this.checkNumber(inNumber, guess));
System.out.println("--------------");
}
} catch (NumberFormatException | InputMismatchException e) {
//in case of guess not a digit, raise a message error
System.out.println(input + " was not valid input. Try again. ");
}
}
//game is over. Stringify Secret Number
String myNumber = Integer.toString(rNumber.getCurrentRandomNumber());
System.out.println();
System.out.println("-------------------------------");
System.out.println("| Thank you for playing! |");
System.out.println("| The Magic number was " + padLeft(myNumber, 4) + " |");
}//END PlayGame
//this method controls how close the guess is to the Secret Number
private String checkNumber(short num, short bet) {
float perc = (float) bet/num;
return (perc > 1)
?
((perc-1 < 0.33f) ? "| So close! A little bit down! |" :
(perc-1 < 0.66f) ? "| Almost there! Guess a lower number. |" :
"| Too high!! Try miles below it!!!! |")
:
(perc < 0.33f) ? "| So low!! Think Bigger!!!!! |" :
(perc < 0.66f) ? "| Half-way there. A larger number would work!! |" :
"| Getting Hot here! Guess a bit up! |";
}//END checkNumber
//this method pad elements with spaces
public static String padLeft(String str, int n) {
return String.format("%1$" + n + "s", str);
}
}//END class