У меня есть назначение программирования, в котором необходимо сделать 2D настольную игру. Игра, которую я пытаюсь сделать, - это игра с подключением 4. Проблема в том, что я не могу заставить условия выигрыша работать. Есть ли у кого-нибудь рекомендации. Я все еще относительно новичок в программировании, поэтому мне жаль, если это простое исправление. Вот мой код:Connect 4 Проверка состояния Java Win
import java.io.*;
import java.net.*;
class C4GameSession implements C4Constants {
private Socket player1;
private Socket player2;
// Create and initialize cells
private char[][] cell = new char[6][7];
private DataInputStream fromPlayer1;
private DataOutputStream toPlayer1;
private DataInputStream fromPlayer2;
private DataOutputStream toPlayer2;
// Continue to play
private boolean continueToPlay = true;
/** Construct a thread */
public C4GameSession(Socket player1, Socket player2) {
this.player1 = player1;
this.player2 = player2;
// Initialize cells with a blank character
for (int i = 0; i < 42; i++)
for (int j = 0; j < 42; j++)
cell[i][j] = ' ';
}
public void runGame() {
try {
// Create data input and output streams
DataInputStream fromPlayer1 = new DataInputStream(player1.getInputStream());
DataOutputStream toPlayer1 = new DataOutputStream(player1.getOutputStream());
DataInputStream fromPlayer2 = new DataInputStream(player2.getInputStream());
DataOutputStream toPlayer2 = new DataOutputStream(player2.getOutputStream());
// Write anything to notify player 1 to start
// This is just to let player 1 know to start
// in other words, don't let the client start until the server is ready
toPlayer1.writeInt(CONTINUE);
// Continuously serve the players and determine and report
// the game status to the players
while (true) {
// Receive a move from player 1
int row = fromPlayer1.readInt();
int column = fromPlayer1.readInt();
cell[row][column] = 'X';
// Check if Player 1 wins
if (isWon('X')) {
toPlayer1.writeInt(PLAYER1_WON);
toPlayer2.writeInt(PLAYER1_WON);
sendMove(toPlayer2, row, column);
break; // Break the loop
}
else if (isFull()) { // Check if all cells are filled
toPlayer1.writeInt(DRAW);
toPlayer2.writeInt(DRAW);
sendMove(toPlayer2, row, column);
break;
}
else {
// Notify player 2 to take the turn - as this message is not '1' then
// this will swicth to the relevant player at the client side
toPlayer2.writeInt(CONTINUE);
// Send player 1's selected row and column to player 2
sendMove(toPlayer2, row, column);
}
// Receive a move from Player 2
row = fromPlayer2.readInt();
column = fromPlayer2.readInt();
cell[row][column] = 'O';
// Check if Player 2 wins
if (isWon('O')) {
toPlayer1.writeInt(PLAYER2_WON);
toPlayer2.writeInt(PLAYER2_WON);
sendMove(toPlayer1, row, column);
break;
}
else {
// Notify player 1 to take the turn
toPlayer1.writeInt(CONTINUE);
// Send player 2's selected row and column to player 1
sendMove(toPlayer1, row, column);
}
}
}
catch(IOException ex) {
System.err.println(ex);
}
}
/** Send the move to other player */
private void sendMove(DataOutputStream out, int row, int column) throws IOException {
out.writeInt(row); // Send row index
out.writeInt(column); // Send column index
}
/** Determine if the cells are all occupied */
private boolean isFull() {
for (int i = 0; i < 43; i++)
for (int j = 0; j < 43; j++)
if (cell[i][j] == ' ')
return false; // At least one cell is not filled
// All cells are filled
return true;
}
/** Determine if the player with the specified token wins */
private boolean isWon(char token) {
/*
int count = 0;
for (int i = 0; i < 6; ++i)
for (int j = 0; j < 7; ++j)
if (cell[i][j] == token)
++count;
if (count == 4)
return true; // found
/* else
count = 0; // reset and count again if not consecutive
*/
int count_piece = 0;
//Checking Horizontal Win
for (int i = 0; i < 6; i++) {
count_piece = 0;
for (int j = 0; j < 7; j++) {
if (cell[i][j] == 'X') {
count_piece++;
if (count_piece == 4) {
System.out.println("you win");
return true;
}
} else {
count_piece = 0;
}
}
}
return false; // no 4-in-a-line found
}
}
Уточнение «не работает». Вы получаете ошибку компиляции, исключение во время выполнения, ложные срабатывания, ложные негативы, что? – cHao
Проблема даже в том, что я соединяю четыре подряд, игра не распознает ее и не заявляет победителя. – user3199712
Основываясь на быстром взгляде на внешний вид логики в вашем методе 'isWon', похоже, что единственный способ выиграть - это то, что 4 части находятся в одной строке, и идут влево-вправо. В connect 4 (если используется память) вы можете выиграть по диагонали, по вертикали или по горизонтали. Проведите тест и посмотрите, победит ли ваша игра, когда у вас есть фигуры, выстроенные горизонтально? –