-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameBoard.java
85 lines (71 loc) · 2.08 KB
/
GameBoard.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
package week06;
import java.util.Arrays;
public class GameBoard {
String[] board = new String[9];
public void initialize() {
for (int i = 1; i <= 9; i++) {
this.board[i-1] = String.valueOf(i);
}
}
public void display() {
System.out.println(" +---+---+---+");
System.out.println(" | " + this.board[0] + " | " + this.board[1] + " | " + this.board[2] + " |");
System.out.println(" +---+---+---+");
System.out.println(" | " + this.board[3] + " | " + this.board[4] + " | " + this.board[5] + " |");
System.out.println(" +---+---+---+");
System.out.println(" | " + this.board[6] + " | " + this.board[7] + " | " + this.board[8] + " |");
System.out.println(" +---+---+---+");
}
public boolean ifValueSetSquare(int squareNumber, String readInput, String player) {
if (this.board[squareNumber-1].equals(readInput)) {
this.board[squareNumber-1] = player;
return true;
} else {
return false;
}
}
public String checkWinStatus() {
for (int pos = 0; pos < 8; pos++) {
String winningCombo = "";
switch (pos) {
case 0:
winningCombo = this.board[0] + this.board[1] + this.board[2];
break;
case 1:
winningCombo = this.board[3] + this.board[4] + this.board[5];
break;
case 2:
winningCombo = this.board[6] + this.board[7] + this.board[8];
break;
case 3:
winningCombo = this.board[0] + this.board[3] + this.board[6];
break;
case 4:
winningCombo = this.board[1] + this.board[4] + this.board[7];
break;
case 5:
winningCombo = this.board[2] + this.board[5] + this.board[8];
break;
case 6:
winningCombo = this.board[0] + this.board[4] + this.board[8];
break;
case 7:
winningCombo = this.board[2] + this.board[4] + this.board[6];
break;
}
if (winningCombo.equals("xxx")) {
return "x";
} else if (winningCombo.equals("000")) {
return "0";
}
}
for (int pos = 1; pos <= 9; pos++) {
if (Arrays.asList(this.board).contains(String.valueOf(pos))) {
break;
} else if (pos == 9) {
return "draw";
}
}
return "";
}
}