Repository navigation
Expand file tree
/
Copy pathTicTacToe.java
More file actions
98 lines (95 loc) · 3.17 KB
/
Copy pathTicTacToe.java
File metadata and controls
98 lines (95 loc) · 3.17 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
92
93
94
95
96
97
98
import java.util.*;
public class TicTacToe {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random rand = new Random();
char [][] board = new char[3][3];
for (int row =0; row < board.length; row++){
for (int col = 0; col < board[row].length; col++){
board[row][col] = ' ';
}
}
//input
char Player = 'X';
boolean gameOver = false;
printboard(board);
while (!gameOver){
int row, col;
if (Player == 'X'){
System.out.print(" Player " + Player + " enter : ");
row = sc.nextInt();
col = sc.nextInt();
}else{
System.out.println(" Computer turns ! : ");
do {
row = rand.nextInt(3);
col = rand.nextInt(3);
System.out.println("Computer choose ..." + row + " " + col );
} while (board[row][col] != ' ');
}
if (board[row][col] == ' '){
board[row][col] = Player;
gameOver = Won(board , Player);
if (gameOver){
System.out.println("Player " + Player + " won the game! ");
}
else if (isDraw(board)){
System.out.println(" Game is Draw! ");
gameOver = true;
}
else{
if (Player == 'X'){
Player = 'O';
}else{
Player = 'X';
}
}
}
else{
System.out.println(" Invalid Player entry!, Try Again : ");
}
printboard(board);
}
}
// who will win the game!
public static boolean Won(char[][] board , char Player){
//row check
for (int i = 0; i < board.length; i++){
if (board[i][0] == Player && board[i][1] == Player && board[i][2] == Player){
return true;
}
}
//column check
for (int i = 0; i < board[0].length; i++){
if (board[0][i] == Player && board[1][i] == Player && board[2][i] == Player){
return true;
}
}
//diagonal check
if (board[0][0] == Player && board[1][1] == Player && board[2][2] == Player){
return true;
}
if (board[0][2] == Player && board[1][1] == Player && board[2][0] == Player){
return true;
}
return false;
}
public static void printboard(char[][] board){
for (int row = 0; row < board.length; row++){
for (int col = 0; col < board[row].length; col++){
System.out.print(board[row][col] + " |");
}
System.out.println();
}
}
public static boolean isDraw(char[][] board){
for (int i = 0; i < board.length; i++){
for (int j =0; j < board[i].length; j++){
if (board[i][j] == ' '){
return false;
}
}
}
return true;
}
}