Add playtest methode and diverses attributes and methods. Refactor isPossible method

This commit is contained in:
Katchan 2021-10-22 13:03:14 +02:00
parent ce39edaead
commit f611509175
10 changed files with 63 additions and 16 deletions

View File

@ -3,35 +3,34 @@ package fr.lnl.game.server.games;
import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import java.util.ArrayList;
import java.util.Arrays;
public class Game { public class Game {
Grid grid; Grid grid;
Player player_One; Player player_One;
Player player_Two; Player player_Two;
Player current_player; Player current_player;
Player[] players; ArrayList<Player> players;
public Game(Grid grid, Player player_One, Player player_Two){ public Game(Grid grid, Player player_One, Player player_Two){
this.player_One = player_One; this.player_One = player_One;
this.player_Two = player_Two; this.player_Two = player_Two;
this.current_player = player_One; this.current_player = player_One;
this.grid = grid; this.grid = grid;
players = new Player[]{player_One, player_Two}; players = new ArrayList<Player>(Arrays.asList(player_One, player_Two));
}
public void play(){
} }
public boolean isOver(){ public boolean isOver(){
return players.length == 1; return players.size() == 1;
} }
public Player getWinner(){ public Player getWinner(){
// Quentin: simple avis: appel de isOver pas forcément nécessaire, puisqu'on appelera surement getWinner après // Quentin: simple avis: appel de isOver pas forcément nécessaire, puisqu'on appelera surement getWinner après
// un appel a isOver retournant true // un appel a isOver retournant true
if(isOver()){ if(isOver()){
return players[0]; return players.get(0);
} }
return null; return null;
} }
@ -40,11 +39,19 @@ public class Game {
return current_player; return current_player;
} }
public void setCurrent_player(Player current_player) {
this.current_player = current_player;
}
public void decrementPlayers(Player player){
players.remove(player);
}
public Grid getGrid() { public Grid getGrid() {
return grid; return grid;
} }
public Player[] getPlayers() { public ArrayList<Player> getPlayers() {
return players; return players;
} }
} }

View File

@ -10,7 +10,7 @@ public class DeployShield extends AbstractAction {
@Override @Override
public void doAction(){ public void doAction(){
getGame().getCurrentPlayer().setShieldDeploy(true); getGame().getCurrentPlayer().setShieldDeploy(true);
//TO-DO retirer les point du player getGame().getCurrentPlayer().decrementEnergy(getGame().getCurrentPlayer().getClassPlayer().getShieldCost());
} }
@Override @Override

View File

@ -18,7 +18,7 @@ public class DropMine extends DropObject {
Point point = points.get(random.nextInt(0,points.size()-1)); Point point = points.get(random.nextInt(0,points.size()-1));
Mine mine = new Mine(); Mine mine = new Mine();
getGame().getGrid().getBoard().get(point).setB(mine); getGame().getGrid().getBoard().get(point).setB(mine);
//TO-DO : retirer les points au player getGame().getCurrentPlayer().decrementEnergy(getGame().getCurrentPlayer().getClassPlayer().getMineCost());
} }
@Override @Override

View File

@ -31,7 +31,7 @@ public class Move extends AbstractAction {
@Override @Override
public boolean isPossible() { public boolean isPossible() {
return getValidPoint().isEmpty(); return !getValidPoint().isEmpty();
} }
public List<Point> getValidPoint() { public List<Point> getValidPoint() {
@ -40,7 +40,7 @@ public class Move extends AbstractAction {
Point position = getGame().getCurrentPlayer().getPoint(); Point position = getGame().getCurrentPlayer().getPoint();
for (int row = -1; row <= 1; row++) { for (int row = -1; row <= 1; row++) {
for (int column = -1; column <= 1; column++) { for (int column = -1; column <= 1; column++) {
if(row == getGame().getGrid().getRow() || column == getGame().getGrid().getColumn()){ if(row == position.getA() + row || column == position.getB() + column){
if(Grid.caseisValid(position.getA(),row,position.getB(),column)){ if(Grid.caseisValid(position.getA(),row,position.getB(),column)){
Point neighbour = new Point(position.getA() + row, position.getB() + column); Point neighbour = new Point(position.getA() + row, position.getB() + column);
Pair<Player, Box> state = board.get(neighbour); Pair<Player, Box> state = board.get(neighbour);

View File

@ -9,7 +9,6 @@ public class Nothing extends AbstractAction {
} }
@Override @Override
public void doAction(){ public void doAction(){
} }
@Override @Override

View File

@ -67,7 +67,7 @@ public class Grid {
} }
public static boolean caseisValid(int row, int column, int deltaRow, int deltaColumn){ public static boolean caseisValid(int row, int column, int deltaRow, int deltaColumn){
return row + deltaRow >= 0 && row + deltaRow <= row && column + deltaColumn >= 0 && column + deltaColumn <= column; return row + deltaRow >= 0 && row + deltaRow < row && column + deltaColumn >= 0 && column + deltaColumn < column;
} }
public HashMap<Point, Pair<Player, Box>> getBoard() { public HashMap<Point, Pair<Player, Box>> getBoard() {

View File

@ -59,6 +59,10 @@ public abstract class AbstractPlayer implements Player {
return actions; return actions;
} }
public void setActions(Action[] actions){
this.actions = actions;
}
public ClassPlayer getClassPlayer() { public ClassPlayer getClassPlayer() {
return classPlayer; return classPlayer;
} }

View File

@ -15,6 +15,7 @@ public interface Player {
void setShieldDeploy(boolean shieldDeploy); void setShieldDeploy(boolean shieldDeploy);
void setWeapon(Weapon weapon); void setWeapon(Weapon weapon);
Action[] getActions(); Action[] getActions();
void setActions(Action[] actions);
ClassPlayer getClassPlayer(); ClassPlayer getClassPlayer();
void setPoint(Point point); void setPoint(Point point);
void decrementEnergy(int energy); void decrementEnergy(int energy);

View File

@ -22,7 +22,7 @@ public class ActionPlayerTest {
// en checkant son résultat // en checkant son résultat
@Test @Test
public void moveActionTest() { public void moveActionTest() {
Assertions.assertEquals(game.getPlayers()[0], game.getCurrentPlayer()); Assertions.assertEquals(game.getPlayers().get(0), game.getCurrentPlayer());
} }
} }

View File

@ -1,23 +1,30 @@
package fr.lnl.game.server; package fr.lnl.game.server;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.action.*;
import fr.lnl.game.server.games.grid.EnergyBall; import fr.lnl.game.server.games.grid.EnergyBall;
import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.Wall; import fr.lnl.game.server.games.grid.Wall;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Cardinal; import fr.lnl.game.server.utils.Cardinal;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
public class GridTest { public class GridTest {
private Grid grid; private Grid grid;
private Game game;
@BeforeEach @BeforeEach
public void mock() { public void mock() {
Mock mock = new Mock(); Mock mock = new Mock();
grid = mock.grid; grid = mock.grid;
game = mock.game;
} }
@Test @Test
@ -35,4 +42,33 @@ public class GridTest {
assertEquals(new EnergyBall(), grid.getBoard().get(new Point(7, 10)).getB()); assertEquals(new EnergyBall(), grid.getBoard().get(new Point(7, 10)).getB());
} }
@Test
public void testPlay(){
for (Player player: game.getPlayers()) {
player.setActions(new Action[]{new Move(game)});
}
System.out.println(game.getGrid().toString());
while (!game.isOver()){
Random random = new Random();
Action action = null;
do {
action = game.getCurrentPlayer().getActions()[random.nextInt(0,game.getCurrentPlayer().getActions().length -1)];
}while (!action.isPossible());
action.doAction();
System.out.println(game.getGrid().toString());
if(game.getCurrentPlayer().getEnergy() <= 0){
game.decrementPlayers(game.getCurrentPlayer());
}
else{
if(game.getCurrentPlayer() == game.getPlayers().get(0)){
game.setCurrent_player(game.getPlayers().get(1));
}
else{
game.setCurrent_player(game.getPlayers().get(0));
}
}
}
System.out.println("Le joueur gagnant : " + game.getWinner().getId());
}
} }