-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinalProject.java
More file actions
283 lines (240 loc) · 8.78 KB
/
Copy pathfinalProject.java
File metadata and controls
283 lines (240 loc) · 8.78 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Max Simmer, Ohene Yeboah, and Chongwoo Chang
// imports
import java.util.Scanner; /* user input, scanners are created for each method to avoid having to pass
scanner through */
import java.util.Random; // random generation for AI
import java.util.*;
public class finalProject {
// initalize variables for GAME TIC TAC TOE
static String board[] = new String[9];
static String turn;
// main method which begins our program
public static void main(String[] args)throws ClassNotFoundException{
selection(); // call game selection first
}
// gameboard for threeInARow / tic tac toe
public static void printBoard(){
System.out.println("|---|---|---|");
System.out.println("| " + board[0] + " | "
+ board[1] + " | " + board[2]
+ " |");
System.out.println("|-----------|");
System.out.println("| " + board[3] + " | "
+ board[4] + " | " + board[5]
+ " |");
System.out.println("|-----------|");
System.out.println("| " + board[6] + " | "
+ board[7] + " | " + board[8]
+ " |");
System.out.println("|---|---|---|");
}
public static String checkWinner(){ // method to check winner of tic tac toe
for (int a = 0; a < 8; a++) {
String line = null;
switch (a) {
case 0:
line = board[0] + board[1] + board[2];
break;
case 1:
line = board[3] + board[4] + board[5];
break;
case 2:
line = board[6] + board[7] + board[8];
break;
case 3:
line = board[0] + board[3] + board[6];
break;
case 4:
line = board[1] + board[4] + board[7];
break;
case 5:
line = board[2] + board[5] + board[8];
break;
case 6:
line = board[0] + board[4] + board[8];
break;
case 7:
line = board[2] + board[4] + board[6];
break;
}
//For X winner
if (line.equals("XXX")) {
return "X";
}
// For O winner
else if (line.equals("OOO")) {
return "O";
}
}
for (int a = 0; a < 9; a++) {
if (Arrays.asList(board).contains(String.valueOf(a + 1))) {
break;
} else if (a == 8) {
return "draw";
}
}
// To enter the X Or O at the exact place on board.
System.out.println(
turn + "'s turn; enter a slot number to place "
+ turn + " in:");
return null;
}
// method for game threeInARow
public static void threeInARow() {
Scanner in = new Scanner(System.in);
board = new String[9];
turn = "X";
String winner = null;
for (int a = 0; a < 9; a++) {
board[a] = String.valueOf(a + 1);
}
System.out.println("Welcome to 3x3 Tic Tac Toe.");
printBoard();
System.out.println(
"X will play first. Enter a slot number to place X in:");
while (winner == null) {
int numInput;
// Exception handling.
// numInput will take input from user like from 1 to 9.
// If it is not in range from 1 to 9.
// then it will show you an error "Invalid input."
try {
numInput = in.nextInt();
if (!(numInput > 0 && numInput <= 9)) {
System.out.println(
"Invalid input; re-enter slot number:");
continue;
}
}
catch (InputMismatchException e) {
System.out.println(
"Invalid input; re-enter slot number:");
continue;
}
// This game has two player x and O.
// Here is the logic to decide the turn.
if (board[numInput - 1].equals(
String.valueOf(numInput))) {
board[numInput - 1] = turn;
if (turn.equals("X")) {
turn = "O";
}
else {
turn = "X";
}
printBoard(); // print the gameboard every turn
winner = checkWinner();
}
else {
System.out.println(
"Slot already taken; re-enter slot number:");
continue;
}
}
if (winner.equalsIgnoreCase("draw")) { // if tie scenario, print draw
System.out.println(
"It's a draw! Thanks for playing.");
}
else { // winner is printed
System.out.println(
"Congratulations! " + winner
+ "'s have won! Thanks for playing.");
}
selection();
}
// method for game RPS
public static void RPS() {
System.out.println("The rules are simple. Paper beats rock, rock beats scissors, and scissors beats rock.");
// scans input for Rock, Paper, or Scissors
Scanner rpsInput = new Scanner(System.in);
System.out.print("Please enter a weapon (Rock, Paper, or Scissors), or 'quit' to quit: ");
String rpsChoice = rpsInput.nextLine(); // read users weapon choice
rpsChoice = rpsChoice.toLowerCase();
// rock = 1, paper = 2, scissors = 3
while(!rpsChoice.equals("quit")){ // IF INPUT IS NOT QUIT DO THIS
int userChoiceToNumber = 0;
if (rpsChoice.equals("rock")){
userChoiceToNumber = 1;
} else if (rpsChoice.equals("paper")){
userChoiceToNumber = 2;
} else if (rpsChoice.equals("scissors")){
userChoiceToNumber = 3;
} else { // if it is NOT a valid weapon
while(userChoiceToNumber == 0){
System.out.print("Incorrect Input, Please Try Again: ");
rpsChoice = rpsInput.nextLine();
// try again
if (rpsChoice.equals("rock")){
userChoiceToNumber = 1;
} else if (rpsChoice.equals("paper")){
userChoiceToNumber = 2;
} else if (rpsChoice.equals("scissors")){
userChoiceToNumber = 3;
} else if (rpsChoice.equals("quit")){ // if quit, quit the game
System.exit(0);
}
}
}
// ai random generation
Random cpuGenerator = new Random();
int cpu = cpuGenerator.nextInt(3)+1; // AI choice is randomly chosen
if (cpu == 1){
System.out.print("\nAI Picked Rock");
} else if (cpu == 2) {
System.out.print("\nAI Picked Paper");
} else if (cpu == 3){
System.out.print("\nAI Picked Scissors");
}
// rock > scissors, scissors > paper, paper > rock
if (userChoiceToNumber == cpu){
System.out.println("\nDraw!");
} else if (userChoiceToNumber == 1 && cpu == 2){ // rock v paper, paper wins
System.out.println("\nAI Wins!");
} else if (userChoiceToNumber == 2 && cpu == 3){ // paper vs scissors, scissor wins
System.out.println("\nAI Wins!");
} else if (userChoiceToNumber == 3 && cpu == 1){ // scissors vs rock, rock wins
System.out.println("\nAI Wins!");
} else {
System.out.println("\nYou Win!");
}
// reset the AI selection
selection(); // after the game ends, choose another game
if(rpsChoice.equals("quit")){
System.exit(0);
}
// selection();
}
rpsInput.close();
// selection();
}
// method for game Pong
public static void Pong() {
pongFrame frame = new pongFrame();
}
/* selction will allow the user to input their specific game type and
depending on what they enter will determine the game they are brought to.
if the user enters a game not featured, it will ask for user input again. */
public static void selection() {
// intialize scanner for user input
Scanner input = new Scanner(System.in);
System.out.print("Enter a game type: threeInARow, Pong, or RPS. Type 'quit' to quit: ");
// initalize variable choice to read user input
String choice = input.next();
choice = choice.toLowerCase();
/* if user inputs this game, call the method for that specific game.
else repeat */
if (choice.equals("threeinarow")) { // if threeInARow, call that method
threeInARow();
} else if (choice.equals("rps")) { // if Pong, call that method
RPS();
} else if (choice.equals("pong")) { // if RPS call that method
Pong();
} else if (choice.equals("quit")){ // if quit, then quit
System.exit(0);
} else { // else incorrect input, recall selection to reloop
System.out.println("Incorrect Input, Please Try Again: ");
selection();
}
input.close();
}
}