Skip to content

Commit 1d918f6

Browse files
authored
Merge pull request #445 from EshraqIbrahim/master
added Tic-tac-toe in java
2 parents 057da43 + 1da45ff commit 1d918f6

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

tic-tac-toe/TicTacToe.java

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
2+
public class TicTacToe {
3+
4+
public boolean hasWinner(char[][] board, char player) {
5+
// TODO Auto-generated method stub
6+
for (int i = 0; i < board.length; i++) {
7+
for (int j = 0; j < board[i].length; j++) {
8+
boolean check = true;
9+
if (board[i][j] == player) {
10+
for (int row = 0; row < board.length; row++) {
11+
if (board[i][row] != player) {
12+
13+
check = false;
14+
}
15+
}
16+
if (check == true) {
17+
return check;
18+
}
19+
check = true;
20+
for (int column = 0; column < board.length; column++) {
21+
22+
if (board[column][j] != player) {
23+
check = false;
24+
}
25+
}
26+
if (check == true) {
27+
return check;
28+
}
29+
30+
if (i == j) {
31+
check = true;
32+
for (int dia = 0; dia < board.length; dia++) {
33+
if (board[dia][dia] != player) {
34+
check = false;
35+
}
36+
}
37+
if (check == true) {
38+
return check;
39+
}
40+
}
41+
if ((i == 1 && j == 1) || (i == 2 && j == 0) || (i == 0 && j == 2)) {
42+
check = true;
43+
for (int dia = 0; dia < board.length; dia++) {
44+
if (board[dia][2 - dia] != player) {
45+
check = false;
46+
}
47+
}
48+
49+
if (check == true) {
50+
return check;
51+
}
52+
}
53+
54+
}
55+
56+
}
57+
}
58+
return false;
59+
}
60+
61+
public void printBoard(char[][] board) {
62+
// TODO Auto-generated method stub
63+
for (int i = 0; i < board.length; i++) {
64+
for (int j = 0; j < board.length; j++) {
65+
System.out.print(board[i][j]);
66+
}
67+
System.out.println("");
68+
}
69+
70+
}
71+
72+
public void intializeBoard(char[][] board) {
73+
// TODO Auto-generated method stub
74+
for (int i = 0; i < board.length; i++) {
75+
for (int j = 0; j < board[i].length; j++) {
76+
board[i][j] = '0';
77+
}
78+
}
79+
80+
}
81+
82+
public boolean validMove(char[][] board, int[] playerMove, char player) {
83+
// TODO Auto-generated method stub
84+
if (board[playerMove[0]][playerMove[1]] == '0') {
85+
return true;
86+
}
87+
return false;
88+
}
89+
90+
public char changePlayer(char player) {
91+
// TODO Auto-generated method stub
92+
if (player == 'X') {
93+
player = 'O';
94+
} else {
95+
player = 'X';
96+
}
97+
return player;
98+
}
99+
100+
public static void main(String[] args) {
101+
// TODO Auto-generated method stub
102+
TicTacToe game = new TicTacToe();
103+
char[][] board = new char[3][3];
104+
int moves = 9;
105+
game.intializeBoard(board);
106+
char player;
107+
System.out.println("Welcome to XO game");
108+
System.out.println("the first player is X");
109+
player = 'X';
110+
Scanner scan = new Scanner(System.in);
111+
while (moves != 0) {
112+
System.out.println("enter the coordinates of your move");
113+
int x = scan.nextInt();
114+
int y = scan.nextInt();
115+
if (x < 0 || x > board.length - 1 || y < 0 || y > board.length - 1) {
116+
System.out.println("invalid Move");
117+
continue;
118+
}
119+
int[] playerMove = new int[2];
120+
playerMove[0] = x;
121+
playerMove[1] = y;
122+
if (game.validMove(board, playerMove, player)) {
123+
board[x][y] = player;
124+
moves--;
125+
if (game.hasWinner(board, player)) {
126+
game.printBoard(board);
127+
System.out.printf("Congratulations player %c has Won", player);
128+
break;
129+
} else {
130+
player = game.changePlayer(player);
131+
}
132+
} else {
133+
System.out.println("invalid Move");
134+
continue;
135+
136+
}
137+
game.printBoard(board);
138+
}
139+
if (moves == 0) {
140+
System.out.println("No one won");
141+
}
142+
scan.close();
143+
144+
}
145+
}

0 commit comments

Comments
 (0)