-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameBoard.java
141 lines (122 loc) · 4.11 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
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
package game;
import java.util.Scanner;
public class GameBoard {
private char[][] gameBoard;
private boolean gameOnGoing = true;
//constructor of GameBoard.
public GameBoard() {
gameBoard = new char[3][3];
int Iterator=1;
for (int i = 0; i<3; i++) {
for (int j = 0; j<3; j++){
char val=Integer.toString(Iterator).charAt(0);
gameBoard[i][j]=val;
Iterator++;
}
}
}
//To display gameBoard .
public void displayBoard() {
for (int row = 0; row < gameBoard.length; row++)
{
for (int column = 0; column < 3; column++)
{
switch (column)
{
case 0:
System.out.print(" " + gameBoard[row][column] + " | ");
break;
case 1:
System.out.print(gameBoard[row][column] + " | ");
break;
case 2:
System.out.println(gameBoard[row][column]);
break;
}
}
if (row < 2)
{
System.out.println("-----------");
}
}
}
//to return game status.
public boolean gameStatus() {
return gameOnGoing;
}
//take user input and makeMove
public void askPlayer(char player) {
Scanner keyboard = new Scanner(System.in);
int n;
System.out.printf("Player %s, please enter the position: ",player);
n = keyboard.nextInt();
try{
makeMove(player,n);
}
catch(Exception e){
System.out.println("Unable to make move");
throw e;
}
}
public void checkForWinner() {
//check over row
for (int row =0; row < gameBoard.length; row++) {
if (gameBoard[row][0] == gameBoard[row][1] && gameBoard[row][1] == gameBoard[row][2]
&& gameBoard[row][0] != ' ') {
System.out.print("The winner is " + gameBoard[row][0] + "!");
gameOnGoing = false;
}
}
//check over column
for (int col=0; col < gameBoard[0].length; col++) {
if (gameBoard[0][col] == gameBoard[1][col] && gameBoard[1][col] == gameBoard[2][col]
&& gameBoard[0][col] != ' ') {
System.out.print("The winner is " + gameBoard[0][col] + "!");
gameOnGoing = false;
}
}
//check over diagonals
if (gameBoard[0][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[2][2]
&& gameBoard[0][0] != ' ') {
System.out.print("The winner is " + gameBoard[0][0] + "!");
gameOnGoing = false;
}
if (gameBoard[2][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[0][2]
&& gameBoard[2][0] != ' ') {
System.out.print("The winner is " + gameBoard[2][0] + "!");
gameOnGoing = false;
}
}
public boolean makeMove(char player, int n) {
switch (n){
case 1:
gameBoard[0][0] = player;
break;
case 2:
gameBoard[0][1] = player;
break;
case 3:
gameBoard[0][2] = player;
break;
case 4:
gameBoard[1][0] = player;
break;
case 5:
gameBoard[1][1] = player;
break;
case 6:
gameBoard[1][2] = player;
break;
case 7:
gameBoard[2][0] = player;
break;
case 8:
gameBoard[2][1] = player;
break;
case 9:
gameBoard[2][2] = player;
break;
}
return true;
}
}