forked from bee-san/AnvilHack_IV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainGame.java
106 lines (76 loc) · 1.79 KB
/
MainGame.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
package game;
public class MainGame {
public char[] [] board;
private char currentPlayer;
public MainGame() {
currentPlayer = 'x';
}
public char getCurrentPlayer() {
return currentPlayer;
}
public void initialiseBoard()
{
for (int i=0;i<3;i++) {
for(int j = 0; j<3; j++) {
board[i][j] = '-';
}
}
}
public boolean checkRows(){
for(int i=0; i<3; i++ ) {
if(checkRowCol(board[i][0], board[i][1], board[i][2])== true)
{
return true;
}
}
return false;
}
private boolean checkColumns() {
for (int i=0; i<3; i++) {
if(checkRowCol(board[0][i], board[1][i],
board[2][i]) == true){
return true;
}
}
return false;
}
private boolean checkDiagonals(){
return((checkRowCol(board[0][0], board[1][1], board[2][2])==true
|| (checkRowCol(board[0][2], board[1][1], board[2][0]) == true)));
}
public void printBoard() {
System.out.println("---------");
for(int i=0; i<3; i++) {
System.out.println("|");{
for(int j=0; j<3; j++) {
System.out.print(board[i][j] + "|");
}
}
}
System.out.println();;
System.out.println("------");
}
public boolean placeMark(int row, int col) {
if ((row >=0)&&(col<3)){
if(board[row][col]=='-');{
board[row][col]= currentPlayer;
return true;
}
}
return false;
}
private boolean checkRowCol(char c1, char c2, char c3) {
return((c1 != '-')&&(c1==c2)&&(c2==3));
}
public void changePlayer() {
if(currentPlayer=='x') {
currentPlayer= 'o';
}
else {
currentPlayer = 'x';
}
}
public boolean isWinner() {
return(checkRows() || checkColumns() || checkDiagonals());
}
}