ADD new rules | ADD method changeRack | Change 'Fl' -> 'f' for class

Shape | ADD getPositionRow, getPositionColumn in Tile | CREATE Constant
'BLUE' in GameBoard
master
Mathis 2022-05-30 19:35:34 +02:00
parent 8060aa88e8
commit 8e2e2a09d3
7 changed files with 274 additions and 22 deletions

View File

@ -1,13 +1,16 @@
package latice.application; package latice.application;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner; import java.util.Scanner;
import java.util.regex.Pattern;
import latice.model.Color; import latice.model.Color;
import latice.model.Deck; import latice.model.Deck;
import latice.model.GameBoard; import latice.model.GameBoard;
import latice.model.Player; import latice.model.Player;
import latice.model.Rack; import latice.model.Rack;
import latice.model.Rules;
import latice.model.Score; import latice.model.Score;
import latice.model.Shape; import latice.model.Shape;
import latice.model.Tile; import latice.model.Tile;
@ -110,14 +113,109 @@ public class LaticeApplicationConsole {
GameBoard board = new GameBoard(); GameBoard board = new GameBoard();
board.displayGameBoard(); board.displayGameBoard();
System.out.println(Objects.equals(board.getGridBoard()[1][0], Tile.class));
Scanner play = new Scanner(System.in); Scanner play = new Scanner(System.in);
Player player;
Boolean round;
Tile tile = null;
Boolean start = true;
Boolean freeTile;
Rules arbitre = new Rules();
for(int i = 0; i < 10; i++) { for(int i = 0; i < 20; i++) {
round = true;
freeTile = true;
player1.Play(play,board); if (i%2 == 0) {
player2.Play(play,board); player = player1;
}else {
player = player2;
}
while (round) {
System.out.println("c'est à votre tour de jouer " + player.getName() +"!");
System.out.println("Vous avez " + player.getScore() +", donc que voulez-vous faire ?\n"
+ " 1. Jouer une Tuile (à partir de la deuxième tuile jouée, cela coûtera 2 points)\n"
+ " 2. Acheter une action supplémentaire\n"
+ " 3. Changer le Rack et passer(coûte 3 points)\n"
+ " 4. Passer\n");
int choiceMenu = Integer.parseInt(play.next());
switch(choiceMenu) {
case 1: //if (arbitre.checkScore(freeTile)){
//System.out.println("Vous n'avez pas assez de points pour jouer un nouvelle tuile");
//}else {
Boolean rulesCheck = false;
while (rulesCheck == false) {
tile = player.Play(play,board,i);
rulesCheck = arbitre.arbitration(player, board, tile, start);
};
if (i == 0) {
start = false;
}
board.setGridBoard(" "+tile.getShapeConsole()+tile.getColorConsole()+" ", tile.getPositionRow(), tile.getPositionColumn());
player.getRack().removeTile(tile);
board.displayGameBoard();
//}
break;
case 2:
case 3: player.getRack().changeRack();
System.out.println("Votre rack à été changé avec succès !");
case 4: System.out.println("Votre tour est terminé " + player.getName() + " !");
round = false;
break;
default: throw new IllegalArgumentException("Veuillez choisir un nombre entre 1 et 4!");
}
}
/*
if (PlayOrPass == 2) {
round = false;
System.out.println("Votre tour est terminé " + player.getName() + " !");
}
while (round) {
Boolean rulesCheck = false;
System.out.println("c'est à votre tour de jouer " + player.getName() +"!");
while (rulesCheck == false) {
tile = player.Play(play,board,i);
rulesCheck = arbitre.arbitration(player, board, tile, START);
};
if (i == 0) {
START = false;
}
board.setGridBoard(" "+tile.getShapeConsole()+tile.getColorConsole()+" ", tile.getPositionRow(), tile.getPositionColumn());
player.getRack().removeTile(tile);
board.displayGameBoard();
System.out.println(player.getName() + " ! Voulez-vous passer votre tour ou continuer à jouer ? 1.continuer ou 2.passer");
int ContinueOrPass = Integer.parseInt(play.next());
if (ContinueOrPass == 2) {
round = false;
System.out.println("Votre tour est terminé " + player.getName() + " !");
}
}
player.getRack().updateRack();*/
} }

View File

@ -4,6 +4,8 @@ public class GameBoard {
private Integer DIMENSION = 9; private Integer DIMENSION = 9;
public static final String SUN = " SU "; public static final String SUN = " SU ";
public static final String MOON = " MO "; public static final String MOON = " MO ";
public static final String BLUE = " ";
private String[][] gridBoard; private String[][] gridBoard;
public GameBoard() { public GameBoard() {
@ -30,7 +32,7 @@ public class GameBoard {
this.gridBoard[i][j] = SUN; this.gridBoard[i][j] = SUN;
}else { }else {
this.gridBoard[i][j] = " "; this.gridBoard[i][j] = BLUE;
} }
if (j == 8) { if (j == 8) {
@ -67,6 +69,7 @@ public class GameBoard {
if (j == 8) { if (j == 8) {
System.out.println("|"); System.out.println("|");
} }
} }
} }

View File

@ -24,12 +24,28 @@ public class Player {
return this.name; return this.name;
} }
public Rack getRack() {
return this.rack;
}
public Integer getScore() { public Integer getScore() {
return this.score.getScore(); return this.score.getScore();
} }
public void Play(Scanner play, GameBoard board) { public Integer addScore(Integer value) {
System.out.println("c'est à votre tour de jouer " + this.name +"!"); int newScore = this.score.getScore()+value;
this.score.setScore(newScore);
return this.score.getScore();
}
public Integer diffScore(Integer value) {
int newScore = this.score.getScore()-value;
this.score.setScore(newScore);
return this.score.getScore();
}
public Tile Play(Scanner play, GameBoard board, Integer start) {
if (this.getScore() == 0) { if (this.getScore() == 0) {
System.out.println("Vous avez " + this.getScore() + " point"); System.out.println("Vous avez " + this.getScore() + " point");
@ -41,17 +57,23 @@ public class Player {
System.out.print("Quel tuile voulez-vous jouez ? "); System.out.print("Quel tuile voulez-vous jouez ? ");
this.rack.displayRack(); this.rack.displayRack();
String tileToPlay = play.next(); System.out.println(1);
Integer idTileToPlay = Integer.parseInt(play.next())-1;
Tile tileToPlay = this.rack.getListRackTile().get(idTileToPlay);
System.out.print("Sur quelle ligne, voulez-vous placer la tuile ?"); System.out.print("Sur quelle ligne, voulez-vous placer la tuile ?");
int row = Integer.parseInt(play.next()); int row = Integer.parseInt(play.next());
System.out.print("Sur quelle colonne, voulez-vous placer la tuile ?"); System.out.print("Sur quelle colonne, voulez-vous placer la tuile ?");
int column = Integer.parseInt(play.next()); int column = Integer.parseInt(play.next());
board.setGridBoard(" "+tileToPlay+" ", row, column);
this.rack.removeTile(tileToPlay);
board.displayGameBoard(); tileToPlay.setPosition(new Position(row, column));
return tileToPlay;
this.rack.updateRack(); //.setGridBoard(" "+tileToPlay.getShapeConsole()+tileToPlay.getColorConsole()+" ", row, column);
//this.rack.removeTile(tileToPlay);
//board.displayGameBoard();
//this.rack.updateRack();
} }

View File

@ -40,7 +40,6 @@ public class Rack {
} }
public ArrayList<Tile> getListRackTile() { public ArrayList<Tile> getListRackTile() {
System.out.println(this.listRackTile);
return this.listRackTile; return this.listRackTile;
} }
@ -64,13 +63,14 @@ public class Rack {
} }
public void removeTile(String stringTile) { public void removeTile(Tile tileToDelete) {
int count = 0; int count = 0;
int index = -1; int index = -1;
System.out.println("taille : " + this.listRackTile.size()); System.out.println("taille : " + this.listRackTile.size());
String strTileToDelete = tileToDelete.getShapeConsole()+tileToDelete.getColorConsole();
for (Tile tile : this.listRackTile) { for (Tile tile : this.listRackTile) {
System.out.println(count++); System.out.println(count++);
if (stringTile.equals(tile.getShapeConsole() + tile.getColorConsole())) { if (strTileToDelete.equals(tile.getShapeConsole()+tile.getColorConsole())) {
index = this.listRackTile.indexOf(tile); index = this.listRackTile.indexOf(tile);
System.out.println(index); System.out.println(index);
System.out.println("tuile supprimé avec succès"); System.out.println("tuile supprimé avec succès");
@ -85,16 +85,41 @@ public class Rack {
} }
public void changeRack() {
Tile tile;
int listRackTileSize = this.listRackTile.size();
for (int i = 0; i < listRackTileSize ; i++) {
tile = this.listRackTile.get(0);
this.deck.getListTile().add(tile);
this.listRackTile.remove(0);
}
for (int i = 0; i < listRackTileSize ; i++) {
int index = (int)(Math.random()*( ((this.deck.getListTile()).size()-1)-0+1)+0); //(int)(Math.random()*(max-min+1)+min);
tile = (this.deck.getListTile()).get(index);
this.listRackTile.add(tile);
this.deck.getListTile().remove(index);
}
}
public void displayRack() { public void displayRack() {
boolean success = false; boolean success = false;
Integer tile_id = 1;
System.out.print("rack : "); System.out.print("rack : ");
for (Tile tile : this.listRackTile) { for (Tile tile : this.listRackTile) {
if (success) { if (success) {
System.out.print(", " + tile.getShapeConsole() + tile.getColorConsole()); System.out.print(", " + tile_id + "." + tile.getShapeConsole() + tile.getColorConsole());
}else { }else {
System.out.print(tile.getShapeConsole() + tile.getColorConsole()); System.out.print(tile_id + "." + tile.getShapeConsole() + tile.getColorConsole());
success = true; success = true;
} }
tile_id = tile_id + 1;
} }
System.out.println(); System.out.println();
} }

View File

@ -1,11 +1,111 @@
package latice.model; package latice.model;
import java.util.Objects;
import java.util.regex.Pattern;
public class Rules { public class Rules {
private static boolean START = true; //private static boolean START = true;
public Rules() { public Rules() {
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
} }
public Boolean moonRule(GameBoard board, Tile tile) {
if (GameBoard.MOON.equals(board.getGridBoard()[tile.getPositionRow()][tile.getPositionColumn()])) {
System.out.println("La première tuile se trouve bien sur la lune !");
return true;
}else {
System.out.println("La première tuile doit être placé sur la lune, recommencez !");
return false;
}
}
public Integer neighborRule(GameBoard board, Tile tile) {
Integer nbrNeighbor = 0;
String checkNeighbor = null;
Boolean checkCase = false;
for(int i = 0; i < 2 ; i++) {
for(int j = -1; j < 2 ; j=j+2) {
if (i == 0) {
if (tile.getPositionColumn()+j >= 0 && tile.getPositionColumn()+j <= 9) {
checkNeighbor = board.getGridBoard()[tile.getPositionRow()][tile.getPositionColumn()+j];
checkCase = true;
}
}else {
if (tile.getPositionRow()+j >= 0 && tile.getPositionRow()+j <= 9) {
checkNeighbor = board.getGridBoard()[tile.getPositionRow()+j][tile.getPositionColumn()];
checkCase = true;
}
}
if (checkCase) {
if (!(GameBoard.SUN.equals(checkNeighbor)) || !(GameBoard.BLUE.equals(checkNeighbor))) {
System.out.println("Il y a une tuile");
if ( tile.getShapeConsole().equals(checkNeighbor.substring(1, 2)) || tile.getColorConsole().equals(checkNeighbor.substring(2, 3)) ) {
System.out.println("Il y a correspondance avec la tuile !");
nbrNeighbor = nbrNeighbor + 1;
}else {
System.out.println("Il n'y a pas correspondance avec la tuile !");
}
}
}
}
}
return nbrNeighbor;
}
public Boolean sunRule(GameBoard board, Tile tile) {
Boolean sun;
if (GameBoard.SUN.equals(board.getGridBoard()[tile.getPositionRow()][tile.getPositionColumn()])) {
sun = true;
}else {
sun = false;
}
return sun;
}
public Boolean arbitration(Player player, GameBoard board, Tile tile, Boolean start) {
if (start == true){
return this.moonRule(board, tile);
}else {
System.out.println("-----------------------------");
if (this.sunRule(board, tile)){
player.addScore(2);
}
int nbr = this.neighborRule(board, tile);
if (nbr == 0) {
System.out.println("l'emplacement où est posé la tuile n'a pas de voisin ou il n'y a pas de correspondance avec les voisins !");
return false;
}else {
if (nbr == 2) {
System.out.println("Vous avez gagné 1 point");
player.addScore(1);
}else if (nbr == 3) {
System.out.println("Vous avez gagné 2 points");
player.addScore(2);
}else if (nbr == 4) {
System.out.println("Vous avez gagné 4 points");
player.addScore(4);
}
return true;
}
}
}
} }

View File

@ -3,7 +3,7 @@ package latice.model;
public enum Shape { public enum Shape {
BIRD("bird", "B"), BIRD("bird", "B"),
DOLPHIN("dolphin", "D"), DOLPHIN("dolphin", "D"),
FLOWER("flower", "Fl"), FLOWER("flower", "f"),
FEATHER("feather", "F"), FEATHER("feather", "F"),
GECKO("gecko", "G"), GECKO("gecko", "G"),
TURTLE("turtle", "T"); TURTLE("turtle", "T");

View File

@ -30,7 +30,11 @@ public class Tile {
return this.shape.getStringShapeConsole(); return this.shape.getStringShapeConsole();
} }
public Position getPosition() { public Integer getPositionRow() {
return this.position; return this.position.getRow();
}
public Integer getPositionColumn() {
return this.position.getColumn();
} }
} }