From 2263d0614b420497f888985e269e89cd19670bab Mon Sep 17 00:00:00 2001 From: Arthur <78031901+Arthur7770@users.noreply.github.com> Date: Tue, 2 Feb 2021 09:14:10 +0100 Subject: [PATCH] added getScore --- src/othello/Main.java | 2 +- src/othello/State.java | 34 ++++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/othello/Main.java b/src/othello/Main.java index 49b1afe..4cb0a12 100644 --- a/src/othello/Main.java +++ b/src/othello/Main.java @@ -9,7 +9,7 @@ public class Main { public static void main(String[] args) { int p1 = 1, p2 = 2; int[][] board = initialize(p1, p2); - State game = new State(board, p1, p2); + State game = new State(board, p1, p2,0,0); while(!game.isOver()) { int player = game.getCurrentPlayer(); diff --git a/src/othello/State.java b/src/othello/State.java index 13515d2..fa96888 100644 --- a/src/othello/State.java +++ b/src/othello/State.java @@ -9,12 +9,16 @@ public class State { private int player1; private int player2; private int currentPlayer; + private int n1; + private int n2; - public State(int[][] board, int p1, int p2) { + public State(int[][] board, int p1, int p2, int n1,int n2) { this.board = board; this.player1 = p1; this.player2 = p2; currentPlayer = p1; + this.n1 = n1+2; + this.n2 = n2+2; } public boolean isOver() { @@ -33,9 +37,9 @@ public class State { System.out.println("recherche"); for (int k=-1; k<2;k++) { for (int l=-1; l<2; l++) { - // La position du pion trouvé est exclue + // La position du pion trouv� est exclue if (k!=0 || l!=0) { - // Si une place libre est trouvée elle est ajouté à la liste de coups + // Si une place libre est trouv�e elle est ajout� � la liste de coups System.out.println("close"); if ( (this.board[i+k][j+l]==0) && (i+k >= 0) && (i+k < 7 ) && (j+l >= 0) && (j+l < 7 ) ) { System.out.println("jadd"); @@ -55,16 +59,34 @@ public class State { } public int getScore(int player) { - return 0; + if (currentPlayer == 1) + return n1/(n1+n2); + else + return n2/(n2+n1); } public State play(int x, int y) { State copie = this.copie(); copie.board[x][y] = copie.getCurrentPlayer(); + int increment = 0; + for(int i = -1; i<2;i++){ + for(int z = -1;z<2;z++){ + try { + copie.board[x+i][y+z] = copie.getCurrentPlayer(); + increment+=1; + } catch (Exception e) { + + } + } + } + if (currentPlayer == 1){ + copie.n1 += increment; + }else{ + copie.n2 += increment; + } copie.switchPlayer(); return copie; } - public int getCurrentPlayer() { return currentPlayer; } @@ -74,7 +96,7 @@ public class State { } public State copie () { - State copie = new State (this.board, this.player1, this.player2); + State copie = new State (this.board, this.player1, this.player2,this.n1,this.n2); for (int i=0; i