-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavaTicTacToe.java
More file actions
143 lines (115 loc) · 4.41 KB
/
javaTicTacToe.java
File metadata and controls
143 lines (115 loc) · 4.41 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 javaTicTacToe
{
public static int winCheck(String[] board, int turPas) {
int[][] winCndts = {
{1,2,3}, // Top Horiz
{4,5,6}, // Mid Horiz
{7,8,9}, // Bottom Horiz
{1,4,7}, // Left Vert
{2,5,8}, // Mid Vert
{3,6,9}, // Right Vert
{1,5,9}, // Diag1
{3,5,7} // Diag2
};
for (int i = 0; i < 8; i++) {
if (board[winCndts[i][0]-1].equals(board[winCndts[i][1]-1]) && board[winCndts[i][1]-1].equals(board[winCndts[i][2]-1]) && !board[winCndts[i][2]-1].equals(" ")) {
return 1; // Win Detected
}
}
if (turPas == 9) {
return -1; // Draw
}
return 0; // Continue
}
public static void prtBoard(String[] board) {
for (int i = 0; i < 9; i++) {
System.out.print(" " + board[i] + " ");
if ((i + 1) % 3 == 0) {
System.out.println();
if (i < 6) System.out.println("-----------");
} else {
System.out.print("|");
}
}
}
public static void clrConsole() {
System.out.print("\033[2J\033[H");
System.out.flush();
}
public static int promptInt(Scanner input, String prompt, int min, int max) {
System.out.println(prompt);
System.out.print("> ");
boolean gaveValid = false;
int returnValue = 0;
if (max <= min) {
throw new IllegalArgumentException("Maximum parameter (" + max + ") can not be equal to or less than minimum (" + min + ").");
}
while (!gaveValid) {
if (input.hasNextInt()) {
returnValue = input.nextInt();
input.nextLine();
if (returnValue < min) {
System.out.println("[ERROR] The inputted value is less than the minimum. (" + returnValue + "<" + min + ")");
System.out.print("> ");
} else if (returnValue > max) {
System.out.println("[ERROR] The inputted value is greater than the maximum. (" + returnValue + ">" + max + ")");
System.out.print("> ");
} else {
gaveValid = true;
}
} else {
System.out.println("[ERROR] The inputted value is not an integer.");
input.nextLine();
System.out.print("> ");
gaveValid = false;
}
}
return returnValue;
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String[] board = {" ", " ", " ", " ", " ", " ", " ", " ", " "};
int turn = 2; // Supposed to be 1, but the way i coded it, it'll flip at the start.
int turnsPassed = 0;
boolean gameEnded = false;
while (!gameEnded) {
turnsPassed++;
clrConsole();
if (turn == 1) {
turn = 2;
} else {
turn = 1;
}
prtBoard(board);
System.out.println("Player " + turn + "'s turn.");
boolean validMove = false;
while (!validMove) {
int move = promptInt(input,"",1,9);
if (!board[move-1].equals(" ")) {
validMove = false;
System.out.println("[ERROR} This spot is already taken.");
} else {
validMove = true;
if (turn == 1) {
board[move-1] = "X";
} else {
board[move-1] = "O";
}
}
}
if (winCheck(board, turnsPassed) == 1) {
clrConsole();
prtBoard(board);
gameEnded = true;
System.out.println("Player " + turn + " has won the game.");
} else if (winCheck(board, turnsPassed) == -1) {
clrConsole();
prtBoard(board);
gameEnded = true;
System.out.println("The game has ended in a draw.");
}
}
}
}