Je dois réaliser un Jeu d'echec en Java (non graphique).
Pour cela j'ai pour le moment 3 classes :
- Chess : classe contenant une méthode main permettant uniquement, dans un premier temps, de tester les méthodes de la classe Piece
package tp1;
/**
* A chess game. Contains a main methods to play chess.
*
* @author Pascale Launay
*/
public class Chess
{
/**
* Chess playing program.
*
* @param args none
*/
public static void main(String[] args)
{
// check args and print a help message if needed
if (args.length == 1 && args[0].equals("-h")) {
usage();
}
// play chess
Chess chess = new Chess();
chess.playChess();
}
/**
* Play a chess game
*/
private void playChess()
{
// make a piece
Piece piece = makePiece();
// move the piece
movePiece(piece);
//Q3
// print the piece
System.out.println("The piece: " + piece.getName()+" "+ piece.getPieceColor()+
", position => "+"("+piece.getX()+","+piece.getY()+")");
}
/**
* Ask the user for the piece properties and create a piece
*
* @return a new piece
*/
private Piece makePiece()
{
// ask for the piece properties
System.out.print("Give the piece name (PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING): ");
String name = Keyboard.readLine();
System.out.print("Give the piece color (black, white): ");
String color = Keyboard.readLine();
//System.out.print("Give the x position of the piece : ");
//int x = Keyboard.readInt();
//System.out.print("Give the y position of the piece : ");
//int y = Keyboard.readInt();
// create and return the piece
//Q6
return new Piece(name, color, 2, 5);
}
/**
* Ask the user for the movement properties and move a piece
*
* @param piece the piece to be moved
*/
private void movePiece(Piece piece)
{
// ask for the movement properties
System.out.print("Give the piece x movement: ");
int dx = Keyboard.readInt();
System.out.print("Give the piece y movement: ");
int dy = Keyboard.readInt();
// move the piece
piece.move(dx, dy);
}
/**
* Print a help message and exit
*/
private static void usage()
{
System.out.println("Usage: java Chess");
System.out.println(" my first chess game");
System.exit(-1);
}
}
- Keyboard : classe offrant des méthodes pour réaliser des saisies au clavier.
package tp1;
import java.io.*;
/**
* A utility class allowing to read form keyboard.
*
* @author Pascale Launay
*/
public class Keyboard
{
/**
* standard input (keyboard)
*/
private static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
/**
* Reads an line from the keyboard
*
* @return the line entered by the user
*/
public static String readLine()
{
String line = "";
try {
line = Keyboard.stdin.readLine();
} catch (IOException e) {
System.err.println("Keyboard input error.");
e.printStackTrace();
System.exit(-1);
}
return line;
}
/**
* Reads an integer from the keyboard
*
* @return the integer value entered by the user
*/
public static int readInt()
{
int nb = 0;
try {
nb = Integer.parseInt(Keyboard.stdin.readLine());
} catch (IOException e) {
System.err.println("Keyboard input error.");
e.printStackTrace();
System.exit(-1);
} catch (NumberFormatException e) {
System.err.println("Keyboard input : number format exception.");
e.printStackTrace();
System.exit(-2);
}
return nb;
}
}
- Piece : classe représentant une pièce du jeu d’échecs
package tp1;
/**
* A chess piece. Represented by a location in the chessboard (x,y),
* a name and a color.
*
* @author Pascale Launay
*/
public class Piece
{
//********************Attributs*******************//
/**
* The piece name: PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING.
*/
private String name;
/**
* The piece color: black, white.
*/
private String color;
//Q1
/**
* The piece position : x,y
*/
private int x, y;
//********************Constructeur*******************//
/**
* Constructs a piece with given name and color.
*
* @param name the piece name: PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING.
* @param color the piece color: black, white.
*/
public Piece(String name, String color)
{
this.name = name;
this.color = color;
}
/**
* Constructs a piece with given name, color, x and y (position).
*
* @param name the piece name: PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING.
* @param color the piece color: black, white.
* @param x position
* @param y position
*/
//Q4
public Piece(String name, String color, int x, int y)
{
this.name = name;
this.color = color;
this.x = x;
this.y = y;
}
//********************Accesseurs*******************//
//GET
//Q2
/**
* Retrieve name piece
* @return the name of a piece
*/
public String getName(){
return name;
}
/**
* Retrieve piece color
* @return the name of a piece
*/
public String getPieceColor(){
return color;
}
/**
* Retrieve x position
* @return the position x of a piece
*/
public int getX(){
return x;
}
/**
* Retrieve y position
* @return the position y of a piece
*/
public int getY(){
return y;
}
//SET
//********************Méthode*******************//
//Q7
/**
* Moves the piece.
*
* @param dx the horizontal movement.
* @param dy the vertical movement.
*/
public void move(int dx, int dy)
{
// TODO
dx++;
dy++;
}
//Q5
/**
* Gives a string representation of the piece in the form "name color".
*
* @return a string representation of the piece.
*/
public String toString()
{
return this.name + " " + this.color+ " "+ this.x + " "+this.y ;
}
/**
* Gives a boolean to know if a movement is valid or not
*
* @return a boolean, false : no valid movement, true : valid movement
*/
public boolean isValidMove(int dx, int dy){
/**if ((this.x + dx)<0 || (this.x + dx)>7){
return false;
}
if(((this.y + dy)<0 || (this.y + dy)>7)){
return false;
}*/
if (name.equals("QUEEN")){
if (dx - x == dy - y){ //La diagonale
return true;
}
if (dx == x){
return true;
}
if (dy == y){
return true;
}
return false;
}
if (name.equals("ROOK")){
if (dx == x){
return true;
}
if (dy == y){
return true;
}
return false;
}
if (name.equals("BISHOP")){
if (dx - x == dy - y){ //La diagonale
return true;
}
}
if (name.equals("KNIGHT")){
}
return true;
}
}
Je suis actuellement en train de travailler sur la méthode IsValidMove
public boolean isValidMove(int dx, int dy){
/**if ((this.x + dx)<0 || (this.x + dx)>7){
return false;
}
if(((this.y + dy)<0 || (this.y + dy)>7)){
return false;
}*/
if (name.equals("QUEEN")){
if (dx - x == dy - y){ //La diagonale
return true;
}
if (dx == x){
return true;
}
if (dy == y){
return true;
}
return false;
}
if (name.equals("ROOK")){
if (dx == x){
return true;
}
if (dy == y){
return true;
}
return false;
}
if (name.equals("BISHOP")){
if (dx - x == dy - y){ //La diagonale
return true;
}
}
if (name.equals("KNIGHT")){
}
return true;
}
Et je me questionnais par rapport au "Bishop". La définition de son déplacement est la suivante : "Le Fou se déplace sur les diagonales de la couleur où il a été posé. Il peut avancer du nombre de cases souhaité par le joueur dans la limite du plateau, mais ne saute pas au-dessus des pièces. Valeur estimée : 2. "
Je voulais ainsi savoir comment faire pour indiquer qu'il se déplace sur les diagonales de la couleur où il a été posé?
J'imagine quelque chose du genre :
if this.color.equals(white){
(dx - x == dy - y) => true
}
if this.color.equals(black){
(dx - x == dy - y) => true
}
Mais il manque la partie indiquant qu'il n'avance que sur des cases blanche...
Merci d'avance pour votre aide !
Sab
- Edité par darmikouraichia 21 janvier 2019 à 20:39:48
Jeu d'échec méthode IsValidMove
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.