-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
80 lines (79 loc) · 2.94 KB
/
TicTacToe.java
File metadata and controls
80 lines (79 loc) · 2.94 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
//Making a Tic Tac Toe game from Scratch
import java.util.Scanner;
class TicTacToe {
static int[][] mat = new int[3][3];//Game Board 3x3
static int turn = 1;//Player 1 for X and 2 for O (Player X starts)
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(true) {
printBoard();
System.out.print("Player " + (turn==1?'X':'O') + "'s turn : ");
inputmove(input);
if(checkWin()){
printBoard();
System.out.println("Player " + (turn==1?'X':'O') + " Win");
break;//End the game if any player win
}
if(isBoardFull()){
//if board full then its a draw
printBoard();
System.out.println("It's a Draw !");
break;//game ends in a draw
}
changeTurn();//change the turn from player 'x' to player 'o'
}
System.out.print("Result : " + checkWin());
}
public static void printBoard() {
System.out.println("Current Board : ");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
char symbol = mat[i][j]==0 ? '_' : (mat[i][j] == 1 ? 'X':'O');
System.out.print(symbol + " ");
}
}
}
public static void changeTurn() {
turn = (turn==1)?2:1;//change turn
}
public static void inputmove(Scanner input) {
int i,j;
while(true) {
System.out.print("Enter Row(0-2) : ");
i = input.nextInt();
System.out.print("Enter Column(0-2)");
j = input.nextInt();
if(i >=0 && i < 3 && j >= 0 && j < 3 && mat[i][j]==0) {
mat[i][j] = turn;
break;//move is valid
}
else {
System.out.println("Invalid move ");
}
}
}
public static boolean checkWin() {
for(int i = 0; i < 3; i++) {
if(mat[i][0] == mat[i][1] && mat[i][1] == mat[i][2] && mat[i][0]!=0)
return true;//row win condition
}
for(int i = 0; i < 3; i++) {
if(mat[0][i] == mat[1][i] && mat[1][i] == mat[2][i] && mat[0][i]!=0)
return true;//Column win condition
}
if(mat[0][0] == mat[1][1] && mat[1][1] == mat[2][2] && mat[0][0]!=0)
return true;//major diagonal win condition
if(mat[0][2] == mat[1][1] && mat[1][1] == mat[2][0] && mat[0][2]!=0)
return true;//minor diagonal win condition
return false;//no win condition matched
}
public static boolean isBoardFull() {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(mat[i][j]==0)
return false;//Board is not full;
}
}
return true;//Board is full
}
}