Commit f7d55334 authored by Anna Maria Eilertsen's avatar Anna Maria Eilertsen
Browse files

renames + docs

parent 6cfa8f26
......@@ -18,9 +18,9 @@ import javax.swing.JOptionPane;
import javax.swing.JPanel;
import inf101.v20.lab4.grid.Grid;
import inf101.v20.sem2.gameplay.TwoPlayerGame;
import inf101.v20.sem2.mnkgames.MNKGame;
import inf101.v20.sem2.mnkgames.MNKGame.Piece;
import inf101.v20.sem2.twoPlayerGame.TwoPlayerGame;
/**
*
......
package inf101.v20.sem2.gameplay;
package inf101.v20.sem2.twoPlayerGame;
import inf101.v20.sem2.mnkgames.MNKGame;
import inf101.v20.sem2.mnkgames.MNKGame.Piece;
import inf101.v20.sem2.mnkgames.player.HumanPlayer;
import inf101.v20.sem2.mnkgames.player.Player;
import inf101.v20.sem2.mnkgames.player.AI.AIPlayer;
import inf101.v20.sem2.mnkgames.player.AI.SimpleAIPlayer;
import inf101.v20.sem2.twoPlayerGame.player.HumanPlayer;
import inf101.v20.sem2.twoPlayerGame.player.Player;
import inf101.v20.sem2.twoPlayerGame.player.AI.AIPlayer;
import inf101.v20.sem2.twoPlayerGame.player.AI.SimpleAIPlayer;
/**
* Class that controls the game play with two players and a MNKGame
*
* Supports autonomous turns for AI player
* Class that controls the game play with two players and a MNKGame
*
* This class supports up to one AI player as player 2, and manages the AI player's turn.
*
* @author Anna Eilertsen - anna.eilertsen@uib.no
*
*/
public class TwoPlayerGame<T extends MNKGame> {
private Player p1, p2;
private T game;
private T currentGame;
private Player currentPlayer;
public TwoPlayerGame() {
}
public TwoPlayerGame(T game) {
this.game = game;
this.currentGame = game;
}
public TwoPlayerGame(T game, Player player1, Player player2) {
this.p1 = player1;
this.p2 = player2;
this.game = game;
this.currentGame = game;
initializeCurrentPlayer();
}
public void setGame(T game) {
if(this.game!=null) {
if(this.currentGame!=null) {
throw new IllegalStateException("Game is already initialized");
}
this.game = game;
this.currentGame = game;
}
public void setHumanPlayer1(String name) {
......@@ -55,6 +55,7 @@ public class TwoPlayerGame<T extends MNKGame> {
this.p2 = new HumanPlayer(name, T.getStartColor().next());
}
// TODO introduce argument 'type' or level of AI to support different types of implementations
public void setAIPlayer2() {
if(p2!=null) {
throw new IllegalStateException("Player 2 is already initialized");
......@@ -70,8 +71,8 @@ public class TwoPlayerGame<T extends MNKGame> {
return currentPlayer == p1 ? p2 : p1;
}
public void checkComplete() {
if(p1 == null || p2 == null || game == null) {
private void checkIsInitialized() {
if(p1 == null || p2 == null || currentGame == null) {
throw new IllegalStateException("Cannot use object before it is completely instanciated");
}
}
......@@ -89,23 +90,23 @@ public class TwoPlayerGame<T extends MNKGame> {
}
public boolean hasWinner() {
return game.hasWinner();
return currentGame.hasWinner();
}
public String winner() {
return playerFromPiece(game.getWinner());
return playerFromPiece(currentGame.getWinner());
}
public boolean hasPossibleMoves() {
return !game.hasWinner() && game.hasPossibleMoves();
return !currentGame.hasWinner() && currentGame.hasPossibleMoves();
}
public Piece pieceAt(int x, int y) {
return game.getPieceAt(x, y);
return currentGame.getPieceAt(x, y);
}
public boolean hasPieceAt(int x, int y) {
return game.getPieceAt(x, y) != Piece.NONE;
return currentGame.getPieceAt(x, y) != Piece.NONE;
}
public String playerFromPiece(Piece piece) {
......@@ -118,37 +119,46 @@ public class TwoPlayerGame<T extends MNKGame> {
}
public int height() {
return game.getHeight();
return currentGame.getHeight();
}
public int width() {
return game.getWidth();
return currentGame.getWidth();
}
/**
* Takes a turn for a human player such that the player places a piece at position (x, y) in the current game
* @param x the x-position of the piece
* @param y the y-position of the piece
*/
public void takeTurnforHumanPlayer(int x, int y) {
checkComplete();
checkIsInitialized();
if(currentPlayer==null) {
initializeCurrentPlayer();
}
if(! (currentPlayer instanceof HumanPlayer))
throw new IllegalStateException("This method is only supported for AI players");
HumanPlayer pl = (HumanPlayer) currentPlayer;
pl.makeMove(game, x, y);
pl.makeMove(currentGame, x, y);
currentPlayer = next();
if(currentPlayer instanceof AIPlayer) {
takeTurnForAI();
}
}
/**
* Lets an AI player make a move in the current game
*/
private void takeTurnForAI() {
checkComplete();
checkIsInitialized();
if(currentPlayer==null) {
initializeCurrentPlayer();
}
if(! (currentPlayer instanceof AIPlayer))
throw new IllegalStateException("This method is only supported for AI players");
AIPlayer ai = (AIPlayer) currentPlayer;
ai.makeMove(game);
ai.makeMove(currentGame);
currentPlayer = next();
}
......
package inf101.v20.sem2.mnkgames.player.AI;
package inf101.v20.sem2.twoPlayerGame.player.AI;
import inf101.v20.sem2.mnkgames.MNKGame;
import inf101.v20.sem2.mnkgames.player.Player;
import inf101.v20.sem2.twoPlayerGame.player.Player;
public interface AIPlayer extends Player{
......
package inf101.v20.sem2.mnkgames.player.AI;
package inf101.v20.sem2.twoPlayerGame.player.AI;
import inf101.v20.sem2.mnkgames.MNKGame;
import inf101.v20.sem2.mnkgames.player.AbstractPlayer;
import inf101.v20.sem2.twoPlayerGame.player.AbstractPlayer;
public class SimpleAIPlayer extends AbstractPlayer implements AIPlayer{
......
package inf101.v20.sem2.mnkgames.player;
package inf101.v20.sem2.twoPlayerGame.player;
import inf101.v20.sem2.mnkgames.MNKGame;
import inf101.v20.sem2.mnkgames.MNKGame.Piece;
......
package inf101.v20.sem2.mnkgames.player;
package inf101.v20.sem2.twoPlayerGame.player;
import inf101.v20.sem2.mnkgames.MNKGame;
......
package inf101.v20.sem2.mnkgames.player;
package inf101.v20.sem2.twoPlayerGame.player;
import inf101.v20.sem2.mnkgames.MNKGame;
......
package inf101.v20.lab4.datastructure;
package inf101.v20.lab4.grid;
import static org.junit.jupiter.api.Assertions.*;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment