-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
143 lines (143 loc) · 5.85 KB
/
Copy pathMain.java
File metadata and controls
143 lines (143 loc) · 5.85 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
import java.util.*;
public class Main {
static Scanner terminal = new Scanner(System.in);
static int[][] gameTable = {{0,0,0}, {0,0,0}, {0,0,0}};
static int gamesPlayed, p1Wins, p2Wins = 0;
static boolean gameIsRunning = false;
static int player = 1;
static void displayMainMenu() {
System.out.print("Main Menu:\n1. Play\n2. Stats\n3. Exit\nInput: ");
int input = terminal.nextInt(); System.out.print("\n");
switch (input) {
case 1: // start the game
gameIsRunning = true; gameCycle();
break;
case 2: // show stats screen
displayStatsScreen();
case 3: // leave the game
System.exit(0);
default: // invalid input
displayMainMenu();
break;
}
}
static void displayStatsScreen() {
String print = (gamesPlayed > 0) ? "// Games played in a single session of Tic Tac Toe: "+ gamesPlayed +"\n// Player 1 Wins: "+ p1Wins +" (Win Rate: "+ ((double) p1Wins / gamesPlayed) * 100 +"%)\n// Player 2 Wins: "+ p2Wins +" (Win Rate: "+ ((double) p2Wins / gamesPlayed) * 100 +"%)\n" : "// Games played in a single session of Tic Tac Toe: 0\n// Player 1 Wins: 0 (Win Rate: 100%)\n// Player 2 Wins: 0 (Win Rate: 100%)\n";
System.out.println(print);
int input;
System.out.print("Type 1 to go back to Main Menu: ");
input = terminal.nextInt();
if (input == 1) {
System.out.print("\n");
displayMainMenu();
} else {
displayStatsScreen();
}
}
static void displayTable() {
for (int row = 0; row < gameTable.length; ++row) {
for (int col = 0; col < gameTable[row].length; ++col) {
switch (gameTable[row][col]) {
case 0 -> System.out.print(".");
case 1 -> System.out.print("X");
case 2 -> System.out.print("O");
}
}
System.out.print("\n");
}
}
static void setSymbolToTable(int row, int col, int plr) {
if (gameTable[row][col] == 0) {
gameTable[row][col] = plr;
} else {
System.out.println("⚠️ There is already a symbol in this place. Please enter another input.");
inputSymbol(plr);
}
}
static void inputSymbol(int plr) {
int place;
System.out.print("Input: ");
place = terminal.nextInt();
if (place <= 0 || place > 9) {
System.out.println("⚠️ Invalid input. Please enter a number between 1 and 9.");
inputSymbol(plr);
}
place -= 1;
switch (place) {
case 0 -> setSymbolToTable(0,0,plr);
case 1 -> setSymbolToTable(0,1,plr);
case 2 -> setSymbolToTable(0,2,plr);
case 3 -> setSymbolToTable(1,0,plr);
case 4 -> setSymbolToTable(1,1,plr);
case 5 -> setSymbolToTable(1,2,plr);
case 6 -> setSymbolToTable(2,0,plr);
case 7 -> setSymbolToTable(2,1,plr);
case 8 -> setSymbolToTable(2,2,plr);
}
}
static void resetGameVars() {
gamesPlayed++;
gameIsRunning = false;
player = 1;
for (int row = 0; row < gameTable.length; ++row) {
for (int col = 0; col < gameTable.length; ++col) {
gameTable[row][col] = 0;
}
}
}
static void checkGameState(int player) {
if ( // winning patterns (by row, column, and diagonals)
gameTable[0][0] == player && gameTable[1][0] == player && gameTable[2][0] == player ||
gameTable[0][1] == player && gameTable[1][1] == player && gameTable[2][1] == player ||
gameTable[0][2] == player && gameTable[1][2] == player && gameTable[2][2] == player ||
gameTable[0][0] == player && gameTable[0][1] == player && gameTable[0][2] == player ||
gameTable[1][0] == player && gameTable[1][1] == player && gameTable[1][2] == player ||
gameTable[2][0] == player && gameTable[2][1] == player && gameTable[2][2] == player ||
gameTable[0][0] == player && gameTable[1][1] == player && gameTable[2][2] == player ||
gameTable[0][2] == player && gameTable[1][1] == player && gameTable[2][0] == player
)
{
displayTable();
System.out.println("[ANNOUNCER] Player "+ player +" has won this game of Tic Tac Toe!");
switch (player) {
case 1 -> p1Wins++;
case 2 -> p2Wins++;
}
resetGameVars();
gameCycle();
}
int filledUpSlots = 0;
for (int row = 0; row < gameTable.length; ++row) {
for (int col = 0; col < gameTable.length; ++col) {
if (gameTable[row][col] != 0) {
filledUpSlots++;
}
}
}
if (filledUpSlots == 9) {
displayTable();
System.out.println("[ANNOUNCER] This game of Tic Tac Toe is a draw!");
resetGameVars();
gameCycle();
}
}
static void gameCycle() {
if (gameIsRunning) {
System.out.println("[ANNOUNCER] You are Player "+ player +". Please enter an input from 1-9 depending on where you want to place your symbol. (The order goes from left to right, top to bottom.)");
displayTable();
inputSymbol(player);
checkGameState(player);
switch (player) {
case 1 -> player = 2;
case 2 -> player = 1;
}
gameCycle();
} else {
displayMainMenu();
}
}
public static void main(String[] args) {
System.out.println("# ROOK'S TIC TAC TOE #\n Written in Java!\n");
displayMainMenu();
}
}