converted AbstractPlayer to interface
This commit is contained in:
parent
a62e3f77ba
commit
ca65348b1f
@ -2,30 +2,30 @@ package othello;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import othello.players.AbstractPlayer;
|
||||
import othello.players.Player;
|
||||
import othello.players.RandomPlayer;
|
||||
|
||||
public class Main {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
AbstractPlayer p1 = new RandomPlayer();
|
||||
AbstractPlayer p2 = new RandomPlayer();
|
||||
AbstractPlayer[][] board = initialize(p1, p2);
|
||||
Player p1 = new RandomPlayer();
|
||||
Player p2 = new RandomPlayer();
|
||||
Player[][] board = initialize(p1, p2);
|
||||
State game = new State(board, p1, p2);
|
||||
System.out.println("joueur 1: " + p1);
|
||||
System.out.println("joueur 2: " + p2);
|
||||
while(!game.isOver()) {
|
||||
AbstractPlayer player = game.getCurrentPlayer();
|
||||
Player player = game.getCurrentPlayer();
|
||||
ArrayList<Pair<Point, Point>> moves = game.getMove(player);
|
||||
System.out.println(game.toString());
|
||||
game = game.play(player.play(moves));
|
||||
game = game.play(player.play(moves, game, player));
|
||||
}
|
||||
System.out.println("C'est " + game.getWinner() + " qui a gagné");
|
||||
}
|
||||
|
||||
public static AbstractPlayer[][] initialize(AbstractPlayer p1, AbstractPlayer p2){
|
||||
AbstractPlayer[][] board = new AbstractPlayer[7][7];
|
||||
public static Player[][] initialize(Player p1, Player p2){
|
||||
Player[][] board = new Player[7][7];
|
||||
board[0][0] = p2;
|
||||
board[0][6] = p1;
|
||||
board[6][0] = p1;
|
||||
|
@ -2,18 +2,18 @@ package othello;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import othello.players.AbstractPlayer;
|
||||
import othello.players.Player;
|
||||
|
||||
public class State {
|
||||
|
||||
private AbstractPlayer[][] board;
|
||||
private AbstractPlayer player1;
|
||||
private AbstractPlayer player2;
|
||||
private AbstractPlayer currentPlayer;
|
||||
private Player[][] board;
|
||||
private Player player1;
|
||||
private Player player2;
|
||||
private Player currentPlayer;
|
||||
private int n1;
|
||||
private int n2;
|
||||
|
||||
public State(AbstractPlayer[][] board, AbstractPlayer p1, AbstractPlayer p2, int n1, int n2) {
|
||||
public State(Player[][] board, Player p1, Player p2, int n1, int n2) {
|
||||
this.board = board;
|
||||
this.player1 = p1;
|
||||
this.player2 = p2;
|
||||
@ -22,7 +22,7 @@ public class State {
|
||||
this.n2 = n2;
|
||||
}
|
||||
|
||||
public State(AbstractPlayer[][] board, AbstractPlayer p1, AbstractPlayer p2) {
|
||||
public State(Player[][] board, Player p1, Player p2) {
|
||||
this(board, p1, p2, 2, 2);
|
||||
}
|
||||
|
||||
@ -32,7 +32,7 @@ public class State {
|
||||
return getMove(player1).isEmpty() && getMove(player2).isEmpty();
|
||||
}
|
||||
|
||||
public ArrayList<Pair<Point, Point>> getMove(AbstractPlayer player) {
|
||||
public ArrayList<Pair<Point, Point>> getMove(Player player) {
|
||||
// Pair<Depart, Arrivee>
|
||||
ArrayList<Pair<Point, Point>> moves = new ArrayList<>();
|
||||
// Parcours du plateau de jeu
|
||||
@ -64,11 +64,11 @@ public class State {
|
||||
return moves;
|
||||
}
|
||||
|
||||
public int getScore(AbstractPlayer player) {
|
||||
public int getScore(Player player) {
|
||||
return player == player1 ? n1/(n1+n2) : n2/(n2+n1);
|
||||
}
|
||||
|
||||
public AbstractPlayer getWinner() {
|
||||
public Player getWinner() {
|
||||
int scoreP1 = getScore(player1), scoreP2 = getScore(player2);
|
||||
if(scoreP1 > scoreP2)
|
||||
return player1;
|
||||
@ -99,11 +99,11 @@ public class State {
|
||||
copy.switchPlayer();
|
||||
return copy;
|
||||
}
|
||||
public AbstractPlayer getCurrentPlayer() {
|
||||
public Player getCurrentPlayer() {
|
||||
return currentPlayer;
|
||||
}
|
||||
|
||||
public void setCurrentPlayer(AbstractPlayer currentPlayer) {
|
||||
public void setCurrentPlayer(Player currentPlayer) {
|
||||
this.currentPlayer = currentPlayer;
|
||||
}
|
||||
|
||||
|
@ -1,12 +0,0 @@
|
||||
package othello.players;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import othello.Pair;
|
||||
import othello.Point;
|
||||
|
||||
public abstract class AbstractPlayer {
|
||||
|
||||
public abstract Pair<Point, Point> play(ArrayList<Pair<Point, Point>> moves);
|
||||
|
||||
}
|
20
src/othello/players/NegamaxPlayer.java
Normal file
20
src/othello/players/NegamaxPlayer.java
Normal file
@ -0,0 +1,20 @@
|
||||
package othello.players;
|
||||
|
||||
import othello.Pair;
|
||||
import othello.Point;
|
||||
import othello.State;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class NegamaxPlayer implements Player {
|
||||
|
||||
@Override
|
||||
public Pair<Point, Point> play(ArrayList<Pair<Point, Point>> moves, State game, Player player) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void negamax(State game, int depth, Player player) {
|
||||
|
||||
}
|
||||
|
||||
}
|
13
src/othello/players/Player.java
Normal file
13
src/othello/players/Player.java
Normal file
@ -0,0 +1,13 @@
|
||||
package othello.players;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import othello.Pair;
|
||||
import othello.Point;
|
||||
import othello.State;
|
||||
|
||||
public interface Player {
|
||||
|
||||
public Pair<Point, Point> play(ArrayList<Pair<Point, Point>> moves, State board, Player player);
|
||||
|
||||
}
|
@ -5,11 +5,12 @@ import java.util.Random;
|
||||
|
||||
import othello.Pair;
|
||||
import othello.Point;
|
||||
import othello.State;
|
||||
|
||||
public class RandomPlayer extends AbstractPlayer {
|
||||
public class RandomPlayer implements Player {
|
||||
|
||||
@Override
|
||||
public Pair<Point, Point> play(ArrayList<Pair<Point, Point>> moves) {
|
||||
public Pair<Point, Point> play(ArrayList<Pair<Point, Point>> moves, State game, Player player) {
|
||||
return moves.get(new Random().nextInt(moves.size()));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user