-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
77 lines (60 loc) · 2.3 KB
/
Main.java
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
import java.util.*;
public class Main {
public static void main(String[] args) {
char[][] board = new char[3][3];
Scanner sc = new Scanner(System.in);
for (int i = 0;i < board.length;i++) {
for (int j = 0;j < board[0].length;j++) {
board[i][j] = ' ';
}
}
char player = 'X';
boolean gameOver = false;
while (!gameOver) {
printBoard(board);
System.out.println("player: " + player + ": Enter the position: like(1,1): ");
int row = sc.nextInt() - 1;
int col = sc.nextInt() - 1;
if (row >= board.length || col >= board[0].length || row < 0 || col < 0) {
System.out.println("Enter the valid position");
continue;
}
if (board[row][col] == ' ') {
board[row][col] = player;
gameOver = hasWon(board, player);
if (gameOver) {
System.out.println(player + " has won.");
} else {
player = (player == 'X') ? 'O': 'X';
}
} else {
System.out.println("invalid position.try again");
}
}
}
private static void printBoard(char[][] board) {
for (int i = 0;i < board.length;i++) {
for (int j = 0;j < board[0].length;j++) {
System.out.print(board[i][j] + " | ");
}
System.out.println();
}
}
private static boolean hasWon(char[][] board, char player) {
for (int i = 0;i < board.length;i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player)
return true;
}
for (int j = 0;j < board[0].length;j++) {
if (board[0][j] == player && board[1][j] == player && board[2][j] == player)
return true;
}
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;
}
}