added reset function

This commit is contained in:
Dorian Zedler 2020-02-25 11:29:23 +01:00
parent 16a6d49c77
commit 5a136f4c9b

View file

@ -21,7 +21,6 @@ package de.itsblue.ConnectFour;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader;
import javax.swing.*; import javax.swing.*;
@ -166,21 +165,36 @@ public class ConnectFour extends JFrame implements ActionListener {
} }
private void gameOver(String gameOverType) { private void gameOver(String gameOverType) {
if (!this.gameState.equals(GameState.Running))
return;
if (gameOverType.equals("draw")) { if (gameOverType.equals("draw")) {
this.winnerPlayer = -1; this.winnerPlayer = -1;
this.statusLabel.setText("Game over. This was a draw!"); this.statusLabel.setText("Game over. This was a draw!");
} else { } else {
PlateType winnerPlateType = PlateType.valueOf(gameOverType); PlateType winnerPlateType = PlateType.valueOf(gameOverType);
this.winnerPlayer = this.players[0].getUsedPlateType().equals(winnerPlateType) ? 0 : 1; this.winnerPlayer = this.players[0].getUsedPlateType().equals(winnerPlateType) ? 0 : 1;
if(!this.gameType.equals(GameType.Local) && this.players[this.winnerPlayer] instanceof LocalPlayer) if (!this.gameType.equals(GameType.Local) && this.players[this.winnerPlayer] instanceof LocalPlayer)
this.statusLabel.setText("Game over. You won the game!"); this.statusLabel.setText("Game over. You won the game!");
else else
this.statusLabel.setText("Game over. " + this.players[this.winnerPlayer].getName() + " won the game!"); this.statusLabel.setText("Game over. " + this.players[this.winnerPlayer].getName() + " won the game!");
} }
this.setGameState(GameState.Over); this.setGameState(GameState.Over);
} }
public void resetGame() {
if (!this.gameState.equals(GameState.Over))
return;
this.players[0] = null;
this.players[1] = null;
this.currentPlayer = -1;
this.winnerPlayer = -1;
this.gameBoard.clearBoard();
this.setGameState(GameState.Idle);
}
/** /**
* Function to switch the current player * Function to switch the current player
*/ */
@ -216,11 +230,11 @@ public class ConnectFour extends JFrame implements ActionListener {
if (this.gameType != GameType.RemoteClient && this.gameState.equals(GameState.Running)) if (this.gameType != GameType.RemoteClient && this.gameState.equals(GameState.Running))
switchPlayer(); switchPlayer();
} else if (e.getActionCommand().startsWith("isMyTurnChanged")) { } else if (e.getActionCommand().startsWith("isMyTurnChanged")) {
if(e.getActionCommand().split(" ")[1].equals("true")) { if (e.getActionCommand().split(" ")[1].equals("true")) {
if(!this.gameType.equals(GameType.Local) && src instanceof LocalPlayer) if (!this.gameType.equals(GameType.Local) && src instanceof LocalPlayer)
this.statusLabel.setText("Running, it's your turn!'"); this.statusLabel.setText("Running, it's your turn!'");
else else
this.statusLabel.setText("Running, it's " + src.getName() + "'s turn!'"); this.statusLabel.setText("Running, it's " + src.getName() + "'s turn!'");
} }
} }
} }