-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingGame.java
More file actions
168 lines (147 loc) · 5.18 KB
/
GuessingGame.java
File metadata and controls
168 lines (147 loc) · 5.18 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
163
164
/**
* GuessingGame.java
* The program makes a random and asks the user to correctly guess the number.
* Part of homework 1, part 1
*/
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
static Scanner scan = new Scanner(System.in);
static Scanner scan1 = new Scanner(System.in);
/**
* decides whether the condition is met to end the game
* @param choice variable to determine to end the game or keep playing
* @return true to end the game and false to keep playing
*/
private static boolean gameQuit (String choice)
{
if (choice.equals("q") || choice.equals("n"))
{
return true;
}
return false;
}
/**
* decides if the user input is valid
* @param guess user input
* @return true if the input is valid and false if its not
*/
private static boolean inputValid (String guess){
if (!guess.equals("q")){
for (char nextChar : guess.toCharArray()){
if (!Character.isDigit(nextChar)){
return false;
}
}
}
return true;
}
/**
* decides if the number was correct
* @param ranNum randum number created by the program
* @param guessNum number guessed from the user
* @return true if they match and false if they don't
*/
private static boolean numCorrect (int ranNum, int guessNum)
{
if (guessNum == ranNum)
{
return true;
}
return false;
}
/**
* decides if the guess was greater or less than the random number
* @param guessNum number guessed by user
* @param ranNum number created by the game
* @return true if user guess was greater and false if its not
*/
private static boolean guessGreater (int guessNum, int ranNum){
if (guessNum > ranNum)
{
return true;
} else {
return false;
}
}
/**
* function that runs the game
* @param guess user input that runs the game
* @param ranNum nuber created by the system
* @param cnt variable that counts number of tries
* @return gamefinish to determine whether to end the program or not in main function.
*/
private static boolean gamePlay (String guess, int ranNum, int cnt){
Random rand2 = new Random();
boolean gameFinish = false;
System.out.println("Please guess a number from 1-100 or q to quit");
guess = scan.nextLine();
while (inputValid(guess))
{
if (gameQuit(guess))
{
System.out.println("Thanks for playing");
System.out.println("The mystery number was " + ranNum);
gameFinish = true;
scan1.close();
scan.close();
return gameFinish;
} else
{
int guessNum = Integer.parseInt(guess);
if (numCorrect(ranNum, guessNum))
{
System.out.print("That's correct.");
System.out.println(" It took you " + cnt + " tries");
System.out.println("Would you like to play again?(y/n)");
String gameChoice = scan1.nextLine();
if (gameQuit(gameChoice))
{
gameFinish = true;
System.out.println("Thanks for playing");
scan1.close();
scan.close();
return gameFinish;
} else{
ranNum = rand2.nextInt(100)+1;
cnt = 1;
System.out.println("Please guess a number from 1-100 or q to quit");
guess = scan.nextLine();
}
} else
{
System.out.print("That's incorrect");
if (guessGreater(guessNum,ranNum)){
System.out.println(" guess was too high");
} else{
System.out.println(" guess was too low");
}
System.out.println("Please guess a number from 1-100 or q to quit");
guess = scan.nextLine();
cnt++;
}
}
}
return gameFinish;
}
public static void main (String [] args)
{
String guess = "";
boolean gameFinish;
int cnt = 1;
boolean game = true;
Random rand = new Random();
Scanner scan = new Scanner(System.in);
Scanner scan1 = new Scanner(System.in);
int ranNum = rand.nextInt(100)+1;
while (game)
{
gameFinish = gamePlay(guess, ranNum, cnt);
if (gameFinish){
game = false;
}
}
scan.close();
scan1.close();
}
}