-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
195 lines (163 loc) · 5.81 KB
/
TicTacToe.java
File metadata and controls
195 lines (163 loc) · 5.81 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
import java.util.Scanner;
public class TicTacToe {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("\nLet's play tic tac toe");
//Create an array with three rows of '_' characters.
char[][] board = {
{'_', '_', '_'},
{'_', '_', '_'},
{'_', '_', '_'}
};
//Call the function printArray();
printBoard(board);
/*
{Loop through turns.
if (X) turn {
Task 4: call askUser().
Task 5: populate the board using askUser's return value.
} else {
Task 4: call askUser().
Task 5: populate the board using askUser's return value. Then, print it.
}
Call the function.
if return value == 3 {
print: X wins and break the loop
} else if return value == -3 {
print: O wins and break the loop
}
}
*/
for (int i = 0; i < 9; i++) {
if (i % 2 == 0) {
System.out.println("Turn: X");
int[] spot = askUser(board);
board[spot[0]][spot[1]] = 'X';
printBoard(board);
} else {
System.out.println("Turn: O");
int[] spot = askUser(board);
board[spot[0]][spot[1]] = 'O';
printBoard(board);
}
if (checkWin(board) == 3) {
System.out.println("X wins!!");
break;
} else if (checkWin(board) == -3) {
System.out.println("O wins!!");
break;
} else if (i == 8) {
System.out.println("It's a tie!");
}
}
scan.close();
}
/** Write a function that prints the board.
* Function name - printBoard()
* @param board (char[][])
*
* Inside the function:
* 1. print a new line.
* 2. print the board.
* • separate each row by two lines.
* • each row precedes a tab of space
* • each character in the grid has one space from the other character
*/
public static void printBoard(char[][] board) {
System.out.print("\n");
for (int i = 0; i < board.length; i++) {
System.out.print("\t");
for (int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j]+ " ");
}
System.out.print("\n\n");
}
}
/**Write a function that lets the user choose a spot
* Function name – askUser
* @param board (char[][] board)
* @return spot (int[])
*
* Inside the function
* 1. Asks the user: • pick a row and column number:
* 2. Check if the spot is taken. If so, let the user choose again.
* 3. Return the row and column in an int[] array.
*
*/
public static int[] askUser(char[][] board) {
System.out.print(" - pick a row and column number: ");
int row = scan.nextInt();
int column = scan.nextInt();
while (board[row][column] != ('_')){
System.out.print("Spot taken, try again: ");
row = scan.nextInt();
column = scan.nextInt();
}
int[] spot = {row, column};
return spot;
}
/** Write a function that determines the winner
* Function name - checkWin
* @param board (char[][])
* @return count (int)
*
* Inside the function:
* 1. Make a count variable that starts at 0.
* 2. Check every row for a straight X or straight O (Task 7).
* 3. Check every column for a straight X or straight O (Task 8).
* 4. Check the left diagonal for a straight X or straight O (Task 9).
* 5. Check the right diagonal for a straight X or straight O (Task 10).
*/
public static int checkWin(char[][] board) {
//Check each of 3 rows
int count = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == 'X') {
count++; //count each X as +1
} else if (board[i][j] == 'O') {
count--; //count each O as -1.
}
}
if (count == 3 || count == -3) {
return count;
} else {
count = 0;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[j][i] == 'X') {
count++; //count each X as +1
} else if (board[j][i] == 'O') {
count--; //count each O as -1.
}
}
if (count == 3 || count == -3) {
return count;
} else {
count = 0;
}
}
for (int i = 0; i < 3; i++) {
if (board[i][i] == 'X') {
count++; //count each X as +1
} else if (board[i][i] == 'O') {
count--; //count each O as -1.
}
}
if (count == 3 || count == -3) {
return count;
} else {
count = 0;
}
for (int i = 0; i < 3; i++) {
if (board[i][2-i] == 'X') {
count++; //count each X as +1
} else if (board[i][2-i] == 'O') {
count--; //count each O as -1.
}
}
return count;
}
}