mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Made the GameState enum it's own file and fixed imports
This commit is contained in:
@@ -13,6 +13,7 @@ import org.toop.framework.networking.events.NetworkEvents;
|
|||||||
import org.toop.game.Connect4.Connect4;
|
import org.toop.game.Connect4.Connect4;
|
||||||
import org.toop.game.Connect4.Connect4AI;
|
import org.toop.game.Connect4.Connect4AI;
|
||||||
import org.toop.game.Game;
|
import org.toop.game.Game;
|
||||||
|
import org.toop.game.enumerators.GameState;
|
||||||
|
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
@@ -147,7 +148,7 @@ public class Connect4Game {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Game.State state = game.play(move);
|
final GameState state = game.play(move);
|
||||||
updateCanvas();
|
updateCanvas();
|
||||||
/*
|
/*
|
||||||
if (move.value() == 'X') {
|
if (move.value() == 'X') {
|
||||||
@@ -156,10 +157,10 @@ public class Connect4Game {
|
|||||||
canvas.drawO(Color.ROYALBLUE, move.position());
|
canvas.drawO(Color.ROYALBLUE, move.position());
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
if (state != Game.State.NORMAL) {
|
if (state != GameState.NORMAL) {
|
||||||
if (state == Game.State.WIN) {
|
if (state == GameState.WIN) {
|
||||||
view.gameOver(true, information.players[currentTurn].name);
|
view.gameOver(true, information.players[currentTurn].name);
|
||||||
} else if (state == Game.State.DRAW) {
|
} else if (state == GameState.DRAW) {
|
||||||
view.gameOver(false, "");
|
view.gameOver(false, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,10 +183,10 @@ public class Connect4Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final Game.Move move = new Game.Move(Integer.parseInt(response.move()), playerChar);
|
final Game.Move move = new Game.Move(Integer.parseInt(response.move()), playerChar);
|
||||||
final Game.State state = game.play(move);
|
final GameState state = game.play(move);
|
||||||
|
|
||||||
if (state != Game.State.NORMAL) {
|
if (state != GameState.NORMAL) {
|
||||||
if (state == Game.State.WIN) {
|
if (state == GameState.WIN) {
|
||||||
if (response.player().equalsIgnoreCase(information.players[0].name)) {
|
if (response.player().equalsIgnoreCase(information.players[0].name)) {
|
||||||
view.gameOver(true, information.players[0].name);
|
view.gameOver(true, information.players[0].name);
|
||||||
gameOver();
|
gameOver();
|
||||||
@@ -193,7 +194,7 @@ public class Connect4Game {
|
|||||||
view.gameOver(false, information.players[1].name);
|
view.gameOver(false, information.players[1].name);
|
||||||
gameOver();
|
gameOver();
|
||||||
}
|
}
|
||||||
} else if (state == Game.State.DRAW) {
|
} else if (state == GameState.DRAW) {
|
||||||
view.gameOver(false, "");
|
view.gameOver(false, "");
|
||||||
gameOver();
|
gameOver();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import org.toop.app.view.views.LocalMultiplayerView;
|
|||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
import org.toop.framework.networking.events.NetworkEvents;
|
import org.toop.framework.networking.events.NetworkEvents;
|
||||||
import org.toop.game.Game;
|
import org.toop.game.Game;
|
||||||
|
import org.toop.game.enumerators.GameState;
|
||||||
import org.toop.game.reversi.Reversi;
|
import org.toop.game.reversi.Reversi;
|
||||||
import org.toop.game.reversi.ReversiAI;
|
import org.toop.game.reversi.ReversiAI;
|
||||||
|
|
||||||
@@ -163,13 +164,13 @@ public final class ReversiGame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
canvas.setCurrentlyHighlightedMovesNull();
|
canvas.setCurrentlyHighlightedMovesNull();
|
||||||
final Game.State state = game.play(move);
|
final GameState state = game.play(move);
|
||||||
updateCanvas(true);
|
updateCanvas(true);
|
||||||
|
|
||||||
if (state != Game.State.NORMAL) {
|
if (state != GameState.NORMAL) {
|
||||||
if (state == Game.State.WIN) {
|
if (state == GameState.WIN) {
|
||||||
view.gameOver(true, information.players[currentTurn].name);
|
view.gameOver(true, information.players[currentTurn].name);
|
||||||
} else if (state == Game.State.DRAW) {
|
} else if (state == GameState.DRAW) {
|
||||||
view.gameOver(false, "");
|
view.gameOver(false, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,10 +193,10 @@ public final class ReversiGame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final Game.Move move = new Game.Move(Integer.parseInt(response.move()), playerChar);
|
final Game.Move move = new Game.Move(Integer.parseInt(response.move()), playerChar);
|
||||||
final Game.State state = game.play(move);
|
final GameState state = game.play(move);
|
||||||
|
|
||||||
if (state != Game.State.NORMAL) {
|
if (state != GameState.NORMAL) {
|
||||||
if (state == Game.State.WIN) {
|
if (state == GameState.WIN) {
|
||||||
if (response.player().equalsIgnoreCase(information.players[0].name)) {
|
if (response.player().equalsIgnoreCase(information.players[0].name)) {
|
||||||
view.gameOver(true, information.players[0].name);
|
view.gameOver(true, information.players[0].name);
|
||||||
gameOver();
|
gameOver();
|
||||||
@@ -203,7 +204,7 @@ public final class ReversiGame {
|
|||||||
view.gameOver(false, information.players[1].name);
|
view.gameOver(false, information.players[1].name);
|
||||||
gameOver();
|
gameOver();
|
||||||
}
|
}
|
||||||
} else if (state == Game.State.DRAW) {
|
} else if (state == GameState.DRAW) {
|
||||||
view.gameOver(false, "");
|
view.gameOver(false, "");
|
||||||
game.play(move);
|
game.play(move);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import org.toop.app.view.views.LocalMultiplayerView;
|
|||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
import org.toop.framework.networking.events.NetworkEvents;
|
import org.toop.framework.networking.events.NetworkEvents;
|
||||||
import org.toop.game.Game;
|
import org.toop.game.Game;
|
||||||
|
import org.toop.game.enumerators.GameState;
|
||||||
import org.toop.game.tictactoe.TicTacToe;
|
import org.toop.game.tictactoe.TicTacToe;
|
||||||
import org.toop.game.tictactoe.TicTacToeAI;
|
import org.toop.game.tictactoe.TicTacToeAI;
|
||||||
|
|
||||||
@@ -145,7 +146,7 @@ public final class TicTacToeGame {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Game.State state = game.play(move);
|
final GameState state = game.play(move);
|
||||||
|
|
||||||
if (move.value() == 'X') {
|
if (move.value() == 'X') {
|
||||||
canvas.drawX(Color.INDIANRED, move.position());
|
canvas.drawX(Color.INDIANRED, move.position());
|
||||||
@@ -153,10 +154,10 @@ public final class TicTacToeGame {
|
|||||||
canvas.drawO(Color.ROYALBLUE, move.position());
|
canvas.drawO(Color.ROYALBLUE, move.position());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state != Game.State.NORMAL) {
|
if (state != GameState.NORMAL) {
|
||||||
if (state == Game.State.WIN) {
|
if (state == GameState.WIN) {
|
||||||
view.gameOver(true, information.players[currentTurn].name);
|
view.gameOver(true, information.players[currentTurn].name);
|
||||||
} else if (state == Game.State.DRAW) {
|
} else if (state == GameState.DRAW) {
|
||||||
view.gameOver(false, "");
|
view.gameOver(false, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,10 +180,10 @@ public final class TicTacToeGame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final Game.Move move = new Game.Move(Integer.parseInt(response.move()), playerChar);
|
final Game.Move move = new Game.Move(Integer.parseInt(response.move()), playerChar);
|
||||||
final Game.State state = game.play(move);
|
final GameState state = game.play(move);
|
||||||
|
|
||||||
if (state != Game.State.NORMAL) {
|
if (state != GameState.NORMAL) {
|
||||||
if (state == Game.State.WIN) {
|
if (state == GameState.WIN) {
|
||||||
if (response.player().equalsIgnoreCase(information.players[0].name)) {
|
if (response.player().equalsIgnoreCase(information.players[0].name)) {
|
||||||
view.gameOver(true, information.players[0].name);
|
view.gameOver(true, information.players[0].name);
|
||||||
gameOver();
|
gameOver();
|
||||||
@@ -190,7 +191,7 @@ public final class TicTacToeGame {
|
|||||||
view.gameOver(false, information.players[1].name);
|
view.gameOver(false, information.players[1].name);
|
||||||
gameOver();
|
gameOver();
|
||||||
}
|
}
|
||||||
} else if (state == Game.State.DRAW) {
|
} else if (state == GameState.DRAW) {
|
||||||
if(game.getLegalMoves().length == 0) { //only return draw in online multiplayer if the game is actually over.
|
if(game.getLegalMoves().length == 0) { //only return draw in online multiplayer if the game is actually over.
|
||||||
view.gameOver(false, "");
|
view.gameOver(false, "");
|
||||||
gameOver();
|
gameOver();
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package org.toop.game.Connect4;
|
package org.toop.game.Connect4;
|
||||||
|
|
||||||
import org.toop.game.TurnBasedGame;
|
import org.toop.game.TurnBasedGame;
|
||||||
|
import org.toop.game.enumerators.GameState;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ public class Connect4 extends TurnBasedGame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public State play(Move move) {
|
public GameState play(Move move) {
|
||||||
assert move != null;
|
assert move != null;
|
||||||
assert move.position() >= 0 && move.position() < board.length;
|
assert move.position() >= 0 && move.position() < board.length;
|
||||||
assert move.value() == getCurrentValue();
|
assert move.value() == getCurrentValue();
|
||||||
@@ -49,13 +50,13 @@ public class Connect4 extends TurnBasedGame {
|
|||||||
movesLeft--;
|
movesLeft--;
|
||||||
|
|
||||||
if (checkForWin()) {
|
if (checkForWin()) {
|
||||||
return State.WIN;
|
return GameState.WIN;
|
||||||
}
|
}
|
||||||
|
|
||||||
nextTurn();
|
nextTurn();
|
||||||
|
|
||||||
|
|
||||||
return State.NORMAL;
|
return GameState.NORMAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkForWin() {
|
private boolean checkForWin() {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package org.toop.game.Connect4;
|
|||||||
|
|
||||||
import org.toop.game.AI;
|
import org.toop.game.AI;
|
||||||
import org.toop.game.Game;
|
import org.toop.game.Game;
|
||||||
|
import org.toop.game.enumerators.GameState;
|
||||||
import org.toop.game.tictactoe.TicTacToe;
|
import org.toop.game.tictactoe.TicTacToe;
|
||||||
|
|
||||||
public class Connect4AI extends AI<Connect4> {
|
public class Connect4AI extends AI<Connect4> {
|
||||||
@@ -34,11 +35,11 @@ public class Connect4AI extends AI<Connect4> {
|
|||||||
|
|
||||||
private int getMoveScore(Connect4 game, int depth, Game.Move move, boolean maximizing) {
|
private int getMoveScore(Connect4 game, int depth, Game.Move move, boolean maximizing) {
|
||||||
final Connect4 copy = new Connect4(game);
|
final Connect4 copy = new Connect4(game);
|
||||||
final Game.State state = copy.play(move);
|
final GameState state = copy.play(move);
|
||||||
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case Game.State.DRAW: return 0;
|
case GameState.DRAW: return 0;
|
||||||
case Game.State.WIN: return maximizing? depth + 1 : -depth - 1;
|
case GameState.WIN: return maximizing? depth + 1 : -depth - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (depth <= 0) {
|
if (depth <= 0) {
|
||||||
|
|||||||
@@ -1,16 +1,10 @@
|
|||||||
package org.toop.game;
|
package org.toop.game;
|
||||||
|
|
||||||
|
import org.toop.game.enumerators.GameState;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public abstract class Game {
|
public abstract class Game {
|
||||||
public enum State {
|
|
||||||
NORMAL,
|
|
||||||
DRAW,
|
|
||||||
WIN,
|
|
||||||
|
|
||||||
TURN_SKIPPED,
|
|
||||||
}
|
|
||||||
|
|
||||||
public record Move(int position, char value) {}
|
public record Move(int position, char value) {}
|
||||||
|
|
||||||
public static final char EMPTY = (char)0;
|
public static final char EMPTY = (char)0;
|
||||||
@@ -37,5 +31,5 @@ public abstract class Game {
|
|||||||
|
|
||||||
public abstract Move[] getLegalMoves();
|
public abstract Move[] getLegalMoves();
|
||||||
|
|
||||||
public abstract State play(Move move);
|
public abstract GameState play(Move move);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package org.toop.game.enumerators;
|
||||||
|
|
||||||
|
public enum GameState {
|
||||||
|
NORMAL,
|
||||||
|
DRAW,
|
||||||
|
WIN,
|
||||||
|
|
||||||
|
TURN_SKIPPED,
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package org.toop.game.reversi;
|
|||||||
|
|
||||||
import org.toop.game.Game;
|
import org.toop.game.Game;
|
||||||
import org.toop.game.TurnBasedGame;
|
import org.toop.game.TurnBasedGame;
|
||||||
|
import org.toop.game.enumerators.GameState;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -130,7 +131,7 @@ public final class Reversi extends TurnBasedGame {
|
|||||||
return boardGrid;
|
return boardGrid;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public State play(Move move) {
|
public GameState play(Move move) {
|
||||||
Move[] legalMoves = getLegalMoves();
|
Move[] legalMoves = getLegalMoves();
|
||||||
boolean moveIsLegal = false;
|
boolean moveIsLegal = false;
|
||||||
for (Move legalMove : legalMoves) {
|
for (Move legalMove : legalMoves) {
|
||||||
@@ -151,19 +152,19 @@ public final class Reversi extends TurnBasedGame {
|
|||||||
if (getLegalMoves().length == 0) {
|
if (getLegalMoves().length == 0) {
|
||||||
skipMyTurn();
|
skipMyTurn();
|
||||||
if (getLegalMoves().length > 0) {
|
if (getLegalMoves().length > 0) {
|
||||||
return State.TURN_SKIPPED;
|
return GameState.TURN_SKIPPED;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Score score = getScore();
|
Score score = getScore();
|
||||||
if (score.player1Score() == score.player2Score()) {
|
if (score.player1Score() == score.player2Score()) {
|
||||||
return State.DRAW;
|
return GameState.DRAW;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return State.WIN;
|
return GameState.WIN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return State.NORMAL;
|
return GameState.NORMAL;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package org.toop.game.tictactoe;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import org.toop.game.TurnBasedGame;
|
import org.toop.game.TurnBasedGame;
|
||||||
|
import org.toop.game.enumerators.GameState;
|
||||||
|
|
||||||
public final class TicTacToe extends TurnBasedGame {
|
public final class TicTacToe extends TurnBasedGame {
|
||||||
private int movesLeft;
|
private int movesLeft;
|
||||||
@@ -31,7 +32,7 @@ public final class TicTacToe extends TurnBasedGame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public State play(Move move) {
|
public GameState play(Move move) {
|
||||||
assert move != null;
|
assert move != null;
|
||||||
assert move.position() >= 0 && move.position() < board.length;
|
assert move.position() >= 0 && move.position() < board.length;
|
||||||
assert move.value() == getCurrentValue();
|
assert move.value() == getCurrentValue();
|
||||||
@@ -40,18 +41,18 @@ public final class TicTacToe extends TurnBasedGame {
|
|||||||
movesLeft--;
|
movesLeft--;
|
||||||
|
|
||||||
if (checkForWin()) {
|
if (checkForWin()) {
|
||||||
return State.WIN;
|
return GameState.WIN;
|
||||||
}
|
}
|
||||||
|
|
||||||
nextTurn();
|
nextTurn();
|
||||||
|
|
||||||
if (movesLeft <= 2) {
|
if (movesLeft <= 2) {
|
||||||
if (movesLeft <= 0 || checkForEarlyDraw(this)) {
|
if (movesLeft <= 0 || checkForEarlyDraw(this)) {
|
||||||
return State.DRAW;
|
return GameState.DRAW;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return State.NORMAL;
|
return GameState.NORMAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkForWin() {
|
private boolean checkForWin() {
|
||||||
@@ -86,7 +87,7 @@ public final class TicTacToe extends TurnBasedGame {
|
|||||||
for (final Move move : game.getLegalMoves()) {
|
for (final Move move : game.getLegalMoves()) {
|
||||||
final TicTacToe copy = new TicTacToe(game);
|
final TicTacToe copy = new TicTacToe(game);
|
||||||
|
|
||||||
if (copy.play(move) == State.WIN || !checkForEarlyDraw(copy)) {
|
if (copy.play(move) == GameState.WIN || !checkForEarlyDraw(copy)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package org.toop.game.tictactoe;
|
|||||||
|
|
||||||
import org.toop.game.AI;
|
import org.toop.game.AI;
|
||||||
import org.toop.game.Game;
|
import org.toop.game.Game;
|
||||||
|
import org.toop.game.enumerators.GameState;
|
||||||
|
|
||||||
public final class TicTacToeAI extends AI<TicTacToe> {
|
public final class TicTacToeAI extends AI<TicTacToe> {
|
||||||
@Override
|
@Override
|
||||||
@@ -59,11 +60,11 @@ public final class TicTacToeAI extends AI<TicTacToe> {
|
|||||||
|
|
||||||
private int getMoveScore(TicTacToe game, int depth, Game.Move move, boolean maximizing) {
|
private int getMoveScore(TicTacToe game, int depth, Game.Move move, boolean maximizing) {
|
||||||
final TicTacToe copy = new TicTacToe(game);
|
final TicTacToe copy = new TicTacToe(game);
|
||||||
final Game.State state = copy.play(move);
|
final GameState state = copy.play(move);
|
||||||
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case Game.State.DRAW: return 0;
|
case GameState.DRAW: return 0;
|
||||||
case Game.State.WIN: return maximizing? depth + 1 : -depth - 1;
|
case GameState.WIN: return maximizing? depth + 1 : -depth - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (depth <= 0) {
|
if (depth <= 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user