added generic Pair and Point + fixed getMove

This commit is contained in:
Quentin Legot 2021-02-10 12:01:51 +01:00
parent 780b21bf0e
commit d4437f8b87
5 changed files with 102 additions and 36 deletions

View File

@ -1,6 +1,5 @@
package othello;
import java.awt.Point;
import java.util.ArrayList;
public class Main {
@ -10,11 +9,11 @@ public class Main {
int p1 = 1, p2 = 2;
int[][] board = initialize(p1, p2);
State game = new State(board, p1, p2);
//while(!game.isOver()) {
while(!game.isOver()) {
int player = game.getCurrentPlayer();
//}
ArrayList<Point> a = new ArrayList<Point>();
ArrayList<Pair<Point, Point>> moves = game.getMove(player);
}
ArrayList<Point> a = new ArrayList<>();
a.add(new Point(3,3));
System.out.println(game.getMove(p1).toString());
//System.out.println(a.get(0).toString());

51
src/othello/Pair.java Normal file
View File

@ -0,0 +1,51 @@
package othello;
/**
* tuple containing 2 unknown type elements
*
* @param <U> left
* @param <K> right
*/
public class Pair<U, K> {
private final U left;
private final K right;
public Pair(U left, K right) {
this.left = left;
this.right = right;
}
public U getLeft() {
return left;
}
public K getRight() {
return right;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Pair<?, ?> other = (Pair<?, ?>) obj;
return this.left.equals(other.getLeft()) && this.left.equals(other.getRight());
}
@Override
public int hashCode() {
return 31 + left.hashCode() * right.hashCode();
}
@Override
public String toString() {
return "(" + left + ", " + right + ")";
}
}

17
src/othello/Point.java Normal file
View File

@ -0,0 +1,17 @@
package othello;
public class Point {
public int x;
public int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public boolean isJump(Point other) {
return Math.pow(other.x - this.x, 2) + Math.pow(other.y - this.y, 2) == 4;
}
}

View File

@ -1,6 +1,5 @@
package othello;
import java.awt.Point;
import java.util.ArrayList;
public class State {
@ -26,45 +25,40 @@ public class State {
}
public boolean isOver() {
return false;
if(n1 == 0 || n2 == 0)
return true;
return getMove(player1).isEmpty() && getMove(player2).isEmpty();
}
public ArrayList<Point> getMove(int player) {
ArrayList<Point> moves = new ArrayList<Point>();
// Clonage
public ArrayList<Pair<Point, Point>> getMove(int player) {
// Pair<Depart, Arrivee>
ArrayList<Pair<Point, Point>> moves = new ArrayList<>();
// Parcours du plateau de jeu
for (int i=0; i<this.board.length;i++) {
for (int j=0; j<this.board.length; j++) {
if (this.board[i][j] == this.currentPlayer) {
for (int y = 0; y < this.board.length; y++) {
for (int x = 0; x < this.board[y].length; x++) {
if (this.board[y][x] == player) {
// Recherche autour du pion du joueur courant
for (int k=-1; k<2;k++) {
for (int l=-1; l<2; l++) {
// La position du pion trouvé est exclue
if (k!=0 || l!=0) {
// Si une place libre est trouvée elle est ajoutée à la liste de coups
if ( ((i+k >= 0) && (i+k < 7 )) && ((j+l >= 0) && (j+l < 7 )) && (this.board[i+k][j+l]==0)) {
moves.add(new Point(i+k, j+l));
}
for (int deltaY = -2; deltaY < 3; deltaY++) {
for (int deltaX = -2; deltaX < 3; deltaX++) {
// La position du pion trouv<EFBFBD> est exclue
if (deltaY != 0 && deltaX != 0) {
// Si une place libre est trouv<EFBFBD>e elle est ajout<EFBFBD>e à la liste des coups
try {
if ((this.board[y+deltaY][x+deltaX]==0)) {
moves.add(new Pair<Point, Point>(new Point(y, x), new Point(y+deltaY, x+deltaX)));
}
} catch(ArrayIndexOutOfBoundsException ignored) {}
}
}
}
}
}
}
// Saut
return moves;
}
public int getScore(int player) {
if (currentPlayer == 1)
return n1/(n1+n2);
else
return n2/(n2+n1);
return currentPlayer == player1 ? n1/(n1+n2) : n2/(n2+n1);
}
public State play(int x, int y) {
@ -75,15 +69,15 @@ public class State {
for(int z = -1;z<2;z++){
try {
copy.board[x+i][y+z] = copy.getCurrentPlayer();
increment+=1;
increment++;
} catch (IndexOutOfBoundsException ignored) {}
}
}
if (currentPlayer == 1){
if (currentPlayer == player1)
copy.n1 += increment;
}else{
else
copy.n2 += increment;
}
copy.switchPlayer();
return copy;
}
@ -105,7 +99,7 @@ public class State {
return copy;
}
public void switchPlayer () {
public void switchPlayer() {
setCurrentPlayer(getCurrentPlayer() == this.player1 ? player2 : player1);
}

View File

@ -0,0 +1,5 @@
package othello.players;
public class RandomPlayer {
}