-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReversiGame.java
More file actions
196 lines (169 loc) · 7.07 KB
/
ReversiGame.java
File metadata and controls
196 lines (169 loc) · 7.07 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//////////////////////////////////
// Author: George Gherardi III
// Class: Programming II
// Assignment: Implementation Lab
//////////////////////////////////
import java.io.*;
import java.util.*;
// This code is an absolute mess so I tried to add a bunch of comments to make
// it easier to parce -GG
public class ReversiGame {
private char[][] board; // Array for the game board with X and O players
public Player currentPlayer; // Keeps track of the current player.
public Player player1;
public Player player2;
// Sets up board with starting pieces
public ReversiGame(String player1Name, String player2Name) {
player1 = new Player(player1Name, 'X');
player2 = new Player(player2Name, 'O');
board = new char[8][8];
currentPlayer = player1;
// This is where it actually places blanks & the starting pieces
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = '.';
}
}
board[3][3] = board[4][4] = 'X';
board[3][4] = board[4][3] = 'O';
}
// Prints the current state of the game board
public void printBoard() {
System.out.println(" 1 2 3 4 5 6 7 8");
for (int i = 0; i < 8; i++) {
System.out.print((i + 1) + " ");
for (int j = 0; j < 8; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
// Careful, There be Dragons
public boolean isValidMove(int targetRow, int targetCol) {
if (board[targetRow][targetCol] != '.') {
return false; // If the cell is not empty, the move is invalid
}
int[] rowOffsets = {-1, -1, -1, 0, 0, 1, 1, 1}; // Offsets for row
int[] colOffsets = {-1, 0, 1, -1, 1, -1, 0, 1}; // Offsets for column
// to explain this a little these basically define the difference
// between the piece you just placed and its relationship to the
// pieces around it. This was the easiest way i could think of to do
// this from a logic perspective. And was absolutely awful to make
// this work correctly but by the grace of the gods its working
// right now. -GG
// This next part uses these offsets to determine surrounding pieces
// & whether or not the move is valid based on those. Reversi sucks
for (int direction = 0; direction < 8; direction++) {
int currentRow = targetRow + rowOffsets[direction];
int currentCol = targetCol + colOffsets[direction];
while (currentRow >= 0 && currentRow < 8 && currentCol >= 0 &&
currentCol < 8 && board[currentRow][currentCol] == getOpponentSymbol()) {
currentRow = currentRow + rowOffsets[direction];
currentCol = currentCol + colOffsets[direction];
if (currentRow >= 0 && currentRow < 8 && currentCol >= 0 &&
currentCol < 8 && board[currentRow][currentCol]
== currentPlayer.getSymbol()) {
return true;
// If we find our own piece in this direction,
// the move is valid
}
}
}
return false; // If no valid direction was found, the move is invalid
}
// Makes a move at the specified row and column
public void makeMove(int targetRow, int targetCol) {
if (!isValidMove(targetRow, targetCol)) {
System.out.println("Invalid move. Try again.");
return;
}
board[targetRow][targetCol] = currentPlayer.getSymbol();
// same thing I described before, this is the relationship between
// the current piece and pieces around it.
int[] rowOffsets = {-1, -1, -1, 0, 0, 1, 1, 1};
int[] colOffsets = {-1, 0, 1, -1, 1, -1, 0, 1};
// This goes through the surrounding pieces and flips them accordingly
// This was also an absolute pain in the butt to get working correctly
for (int direction = 0; direction < 8; direction++) {
int currentRow = targetRow + rowOffsets[direction];
int currentCol = targetCol + colOffsets[direction];
if (currentRow >= 0 && currentRow < 8 && currentCol >= 0 &&
currentCol < 8 && board[currentRow][currentCol] ==
getOpponentSymbol()) {
while (currentRow >= 0 && currentRow < 8 && currentCol
>= 0 && currentCol < 8) {
if (board[currentRow][currentCol] == '.') {
break;
}
if (board[currentRow][currentCol] == currentPlayer.getSymbol()) {
currentRow = currentRow - rowOffsets[direction];
currentCol = currentCol - colOffsets[direction];
while (currentRow != targetRow || currentCol !=
targetCol) {
board[currentRow][currentCol] = currentPlayer.getSymbol();
currentRow = currentRow - rowOffsets[direction];
currentCol = currentCol - colOffsets[direction];
}
break;
}
currentRow = currentRow + rowOffsets[direction];
currentCol = currentCol + colOffsets[direction];
}
}
}
currentPlayer = getOpponent();
}
// Returns the opponent
public Player getOpponent() {
Player opponent;
if (currentPlayer == player1)
opponent = player2;
else
opponent = player1;
return opponent;
}
// Returns the opponent's symbol
public char getOpponentSymbol() {
Player opponent;
if (currentPlayer == player1)
opponent = player2;
else
opponent = player1;
return opponent.getSymbol();
}
// Checks if the game is over (no valid moves for either player)
public boolean isGameOver() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == '.') {
if (isValidMove(i, j)) {
return false; // If there's a valid move,
//the game is not over
}
}
}
}
return true; // If no valid moves were found, the game is over
}
// Determines the winner based on the number of pieces on the board
public char getWinner() {
int countX = 0, countO = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == 'X') {
countX++;
} else if (board[i][j] == 'O') {
countO++;
}
}
}
char winner;
if (countX > countO)
winner = 'X';
else if (countX < countO)
winner = 'O';
else
winner = ' ';
return winner;
}
}