-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartOne.java
168 lines (157 loc) · 6.17 KB
/
PartOne.java
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
package tictactoe;
import java.util.InputMismatchException;
import java.util.Scanner;
public class PartOne {
public static void main(String[] args) {
char[][] gameBoard = new char[3][3]; // create a 3 by 3 char array to store the board
inputBoard(gameBoard);
printBoard(gameBoard);
play(gameBoard);
printBoard(gameBoard);
winner(gameBoard);
}
static void inputBoard(char gameBoard[][]) { // this function inputs itesm into the gameboard
System.out.print("Enter cells: ");
Scanner scanner = new Scanner(System.in);
String input = scanner.next(); // allow the user to input a populated board as a string
int count = 0; // create a variable to index the string
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
gameBoard[i][j] = input.charAt(count);// input elements of the string in the gameBoard array
count++;
}
}
}
static void printBoard(char gameBoard[][]) {
System.out.println("----------");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 0) {
System.out.print("| ");
}
System.out.print(gameBoard[i][j] + " ");
if (j == 2) {
System.out.println(" |");
}
}
}
System.out.println("----------");
}
static boolean winner(char gameBoard[][]) {
boolean rowWinner = false;
boolean colWinner = false;
boolean diagWinner = false;
int rowIndex = 0;
int colIndex = 0;
int countWinner = 0;
boolean notBlank = false;
for (int i = 0; i < 3; i++) {// not equal to white space //then check the values of each row
notBlank = gameBoard[i][0] != ' ' && gameBoard[i][0] != '_';
boolean threeInRow = gameBoard[i][0] == gameBoard[i][1] && gameBoard[i][1] == gameBoard[i][2];
if (notBlank && threeInRow) {
rowWinner = true;
countWinner++;
rowIndex = i;
}
}
for (int i = 0; i < 3; i++) { // find the row winner
notBlank = gameBoard[0][i] != ' ' && gameBoard[i][0] != '_';
boolean threeInColumn = gameBoard[0][i] == gameBoard[1][i] && gameBoard[1][i] == gameBoard[2][i];
if (notBlank && threeInColumn) {
colWinner = true;
countWinner++;
colIndex = i;
}
}
notBlank = gameBoard[1][1] != ' ' && gameBoard[1][1] != '_';
if (notBlank) {
if (gameBoard[0][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[2][2]) {
diagWinner = true;
countWinner++;
} else if (gameBoard[2][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[0][2]) {
diagWinner = true;
countWinner++;
}
}
int countO = 0;
int countX = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (gameBoard[i][j] == 'O') {
countO++;
} else if (gameBoard[i][j] == 'X') {
countX++;
}
}
}
if (countWinner > 1) { // if there are multipe winnners print impossible
System.out.println("Impossible");
} else if (Math.abs(countO - countX) > 2) {
System.out.println("Impossible");
} else if (rowWinner) {
System.out.println(gameBoard[rowIndex][0] + " wins");
} else if (colWinner) {
System.out.println(gameBoard[0][colIndex] + " wins");
} else if (diagWinner) {
System.out.println(gameBoard[1][1] + " wins");
return true;
} else if (countX + countO == 9) {
System.out.println("Draw");
return true;
} else {
System.out.println("Game not finished");
return false;
}
return false;
}
static void play(char gameBoard[][]) {
boolean notEntered = true;
Scanner scanner = new Scanner(System.in);
while (notEntered) {
System.out.print("Enter the coordinates:");
boolean equal = countXO(gameBoard);
try {
int x = scanner.nextInt();
int y = scanner.nextInt();
if (x > 0 && x < 4 && y > 0 && y < 4) {
int row = x - 1;
int col = Math.abs(y - 3);
if (gameBoard[col][row] == '_' || gameBoard[col][row] == ' ') {
notEntered = false;
if (equal) {
gameBoard[col][row] = 'X';
} else {
gameBoard[col][row] = 'O';
}
} else {
System.out.println("This cell is occupied! Choose another one!");
}
} else {
System.out.println("Coordinates should be from 1 to 3!");
continue;
}
} catch (InputMismatchException e) {
System.out.println("You should enter numbers!");
scanner.next();
}
}
}
static boolean countXO(char gameBoard[][]) {
int countO = 0;
int countX = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (gameBoard[i][j] == 'O') {
countO++;
} else if (gameBoard[i][j] == 'X') {
countX++;
}
}
}
if (countO == countX) {
return true;
} else {
return false;
}
}
}