|
|
|
@ -4,6 +4,7 @@ import fr.uca.iut.clfreville2.morpion.win.AlignmentWin;
|
|
|
|
|
import fr.uca.iut.clfreville2.morpion.win.CantPlace;
|
|
|
|
|
import fr.uca.iut.clfreville2.morpion.win.MoveResult;
|
|
|
|
|
import fr.uca.iut.clfreville2.morpion.win.Result;
|
|
|
|
|
import org.jetbrains.annotations.UnmodifiableView;
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
@ -11,8 +12,6 @@ import java.util.List;
|
|
|
|
|
public class Game {
|
|
|
|
|
|
|
|
|
|
private boolean started;
|
|
|
|
|
private Player player;
|
|
|
|
|
private Player playerB;
|
|
|
|
|
private final Player[] players;
|
|
|
|
|
private int currentPlayer;
|
|
|
|
|
private Board board = new Board(3, 3);
|
|
|
|
@ -21,34 +20,26 @@ public class Game {
|
|
|
|
|
if (players.length == 0) {
|
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
|
}
|
|
|
|
|
player = players[0];
|
|
|
|
|
if (players.length != 1) {
|
|
|
|
|
playerB = players[1];
|
|
|
|
|
}
|
|
|
|
|
this.players = players;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void start() {
|
|
|
|
|
if (started) {
|
|
|
|
|
throw new IllegalStateException();
|
|
|
|
|
if (this.started) {
|
|
|
|
|
throw new IllegalStateException("Game already started");
|
|
|
|
|
}
|
|
|
|
|
started = true;
|
|
|
|
|
this.started = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Player currentPlayer() {
|
|
|
|
|
return players[currentPlayer];
|
|
|
|
|
//return player;
|
|
|
|
|
return this.players[this.currentPlayer];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void nextPlayer() {
|
|
|
|
|
final Player tmp = player;
|
|
|
|
|
player = playerB;
|
|
|
|
|
playerB = tmp;
|
|
|
|
|
currentPlayer = (currentPlayer + 1) % players.length;
|
|
|
|
|
this.currentPlayer = (this.currentPlayer + 1) % this.players.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Board board() {
|
|
|
|
|
return board;
|
|
|
|
|
return this.board;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setBoard(Board board) {
|
|
|
|
@ -56,50 +47,26 @@ public class Game {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MoveResult placeCurrent(Position position) {
|
|
|
|
|
if (!started) {
|
|
|
|
|
throw new IllegalStateException();
|
|
|
|
|
if (!this.started) {
|
|
|
|
|
throw new IllegalStateException("Game not started");
|
|
|
|
|
}
|
|
|
|
|
if (board.isOccupied(position)) {
|
|
|
|
|
if (this.board.isOccupied(position)) {
|
|
|
|
|
return new CantPlace();
|
|
|
|
|
}
|
|
|
|
|
board.place(position, new Placed(switch (currentPlayer().name()) {
|
|
|
|
|
this.board.place(position, new Placed(switch (currentPlayer().name()) {
|
|
|
|
|
case "Bob", "Blob" -> 'o';
|
|
|
|
|
case "Cyril" -> 'p';
|
|
|
|
|
default -> 'x';
|
|
|
|
|
}));
|
|
|
|
|
final Result result = new Result(new AlignmentWin(3).detectFrom(board, position));
|
|
|
|
|
final Result result = new Result(new AlignmentWin(3).detectFrom(this.board, position));
|
|
|
|
|
if (!result.wins().isEmpty()) {
|
|
|
|
|
currentPlayer().incrementScore();
|
|
|
|
|
}
|
|
|
|
|
nextPlayer();
|
|
|
|
|
return result;
|
|
|
|
|
/*if (position.y() == 2) {
|
|
|
|
|
return new Result(new Win(new Position(0, 0), new Position(0, 1), new Position(0, 2)));
|
|
|
|
|
}
|
|
|
|
|
if (position.x() == 0 && position.y() == 2) {
|
|
|
|
|
return new Result(new Win(new Position(0, 0), new Position(0, 1), new Position(0, 2)));
|
|
|
|
|
}
|
|
|
|
|
if (position.x() == 1 && position.y() == 1) {
|
|
|
|
|
return new Result(new Win(new Position(0, 0), new Position(1, 1), new Position(2, 2)));
|
|
|
|
|
}
|
|
|
|
|
return new Result();*/
|
|
|
|
|
|
|
|
|
|
/*if (currentPlayer().name().equals("Cyril")) {
|
|
|
|
|
board.place(position, new Placed('p'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (player.name().equals("Bob") || currentPlayer().name().equals("Blob")) {
|
|
|
|
|
board.place(position, new Placed('o'));
|
|
|
|
|
} else {
|
|
|
|
|
board.place(position, new Placed('x'));
|
|
|
|
|
}
|
|
|
|
|
if (player.name().equals("Blob")) {
|
|
|
|
|
nextPlayer();
|
|
|
|
|
}
|
|
|
|
|
player = playerB;*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Player> players() {
|
|
|
|
|
return Arrays.asList(players);
|
|
|
|
|
public @UnmodifiableView List<Player> players() {
|
|
|
|
|
return Arrays.asList(this.players);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|