Repository navigation
Expand file tree
/
Copy pathTicTacToe.java
More file actions
61 lines (55 loc) · 1.59 KB
/
Copy pathTicTacToe.java
File metadata and controls
61 lines (55 loc) · 1.59 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
import java.util.Scanner;
public class TicTacToe {
public static int row,col;
public static Scanner sc = new Scanner(System.in);
public static char[][] gameBoard = new char[3][3];
public static char val = 'X';
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i = 0 ; i < 3 ; i++) {
for(int j = 0 ; j < 3 ; j++) {
gameBoard[i][j] =' ';
}
}
play();
printBoard();
}
public static void play() {
boolean play = true;
printBoard();
while(play) {
System.out.println("Please enter the row and column");
row = sc.nextInt() - 1;
col = sc.nextInt() - 1;
gameBoard[row][col] = val;
printBoard();
if(val == 'X')
val = 'O';
else
val = 'X';
if(Win(row,col)) {
play = false;
System.out.println("Game Over " + val + " won !!!");
break;
}
}
}
public static void printBoard() {
System.out.println("\n----------");
for(int i = 0 ; i < 3 ; i++) {
for(int j = 0 ; j < 3 ; j++) {
if(j==0)System.out.print("| ");
System.out.print(gameBoard[i][j] + "| ");
}
System.out.println("\n----------");
}
}
public static boolean Win(int rmove,int cmove) {
if((gameBoard[rmove][0] == val && gameBoard[rmove][1] == val && gameBoard[rmove][2] == val) ||
(gameBoard[0][cmove] == val && gameBoard[1][cmove] == val && gameBoard[2][cmove] == val) ||
(gameBoard[0][0] == val && gameBoard[1][1] == val && gameBoard[2][2] == val) ||
(gameBoard[0][2] == val && gameBoard[1][1] == val && gameBoard[2][0] == val))
return true;
return false;
}
}