-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameLogic.java
60 lines (52 loc) · 1.94 KB
/
GameLogic.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
public class GameLogic {
private char[][] board; // 3x3 틱택토 보드
private char currentPlayer; // 현재 플레이어 (X 또는 O)
public GameLogic(char firstPlayer) {
board = new char[3][3];
currentPlayer = firstPlayer;
}
public char getCurrentPlayer() {
return currentPlayer;
}
public char[][] getBoard() {
return board;
}
public void switchPlayer() {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
public void resetBoard() {
board = new char[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '\0'; // 각 셀을 빈 값으로 초기화
}
}
}
public boolean makeMove(int row, int col) {
if (board[row][col] == '\0') {
board[row][col] = currentPlayer;
return true;
}
return false;
}
public boolean checkWin(int row, int col) {
// 현재 플레이어가 승리했는지 확인 (가로, 세로, 대각선)
return (board[row][0] == currentPlayer && board[row][1] == currentPlayer && board[row][2] == currentPlayer) ||
(board[0][col] == currentPlayer && board[1][col] == currentPlayer && board[2][col] == currentPlayer) ||
(board[0][0] == currentPlayer && board[1][1] == currentPlayer && board[2][2] == currentPlayer) ||
(board[0][2] == currentPlayer && board[1][1] == currentPlayer && board[2][0] == currentPlayer);
}
public boolean isBoardFull() {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (board[row][col] == '\0') {
return false;
}
}
}
return true;
}
public void setCurrentPlayer(char player) {
this.currentPlayer = player;
}
}