forked from anumarwaha/ProjectEvaluation1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
91 lines (66 loc) · 2.48 KB
/
Copy pathGame.java
File metadata and controls
91 lines (66 loc) · 2.48 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
package tic;
public class Game {
char [][] table = new char[3][3];
public void initializeGame()
{
for (int i = 0; i < 3; i++)
for (int p=0; p < 3; p++)
table [i][p]= ' ';
}
public boolean checkIfLegal(int row, int column)
{
if( (row>2 || column>2) || (row<0 || column <0) )
return true;
else if(table[row][column]=='x' || table[row][column]=='o')
return true;
return false;
}
public void changeBoard(char player, int row, int column)
{
table[row][column]=player;
}
public void displayBoard()
{
System.out.println(" 0 " + table[0][0] + "|" + table[0][1] + "|" + table[0][2]);
System.out.println(" --+-+--");
System.out.println(" 1 " + table[1][0] + "|" + table[1][1] + "|" + table[1][2]);
System.out.println(" --+-+--");
System.out.println(" 2 " + table[2][0] + "|" + table[2][1] + "|" + table[2][2]);
System.out.println(" 0 1 2 ");
}
public char changePlayer(char player) {
char newTurn='e';
if (player == 'o')
newTurn='x';
if (player == 'x')
newTurn='o';
return newTurn;
}
public boolean checkIfWinner() {
if( table [0][0]==table[1][0] && table[1][0] == table[2][0] && (table [0][0]=='x' || table [0][0]=='o'))
return true;
else if( table [0][1]==table[1][1] && table[1][1] == table[2][1] && (table [0][1]=='x' || table [0][1]=='o'))
return true;
else if( table [0][2]==table[1][2] && table[1][2] == table[2][2] && (table [0][2]=='x' || table [0][2]=='o'))
return true;
else if( table [0][0]==table[0][1] && table[0][1] == table[0][2] && (table [0][0]=='x' || table [0][0]=='o'))
return true;
else if( table [1][0]==table[1][1] && table[1][1] == table[1][2] && (table [1][0]=='x' || table [1][0]=='o'))
return true;
else if( table [2][0]==table[2][1] && table[2][1] == table[2][2] && (table [2][0]=='x' || table [2][0]=='o'))
return true;
else if( table [0][0]==table[1][1] && table[1][1] == table[2][2] && (table [0][0]=='x' || table [0][0]=='o'))
return true;
else if( table [2][0]==table[1][1] && table[1][1] == table[0][2] && (table [2][0]=='x' || table [2][0]=='o'))
return true;
else
return false;
}
public boolean checkIfTie() {
for (int i = 0; i < 3; i++)
for (int p=0; p < 3; p++)
if(table [i][p]==' ')
return false;
return true;
}
}