diff --git a/projetechecs-master/.classpath b/projetechecs-master/.classpath new file mode 100644 index 0000000000000000000000000000000000000000..07661e7c4cb5d76454abab503ca40695916191e3 --- /dev/null +++ b/projetechecs-master/.classpath @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="src" path=""/> + <classpathentry kind="output" path=""/> +</classpath> diff --git a/projetechecs-master/.gitignore b/projetechecs-master/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..5241a7220a93c9db3e5d0cb642586fd917393c39 --- /dev/null +++ b/projetechecs-master/.gitignore @@ -0,0 +1 @@ +*.class \ No newline at end of file diff --git a/projetechecs-master/.project b/projetechecs-master/.project new file mode 100644 index 0000000000000000000000000000000000000000..4f8a9ec6cf4e2988f150086ca2d873a72e6f1752 --- /dev/null +++ b/projetechecs-master/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>projetechecs-master</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/projetechecs-master/Bishop.java b/projetechecs-master/Bishop.java new file mode 100644 index 0000000000000000000000000000000000000000..a7e858c4501ff41f855dffeff022a3dd0520f6c3 --- /dev/null +++ b/projetechecs-master/Bishop.java @@ -0,0 +1,34 @@ +public class Bishop extends Piece { + + public Bishop(int x, int y, Player owner){ + super(x, y, owner); + + } + + public boolean isMoveAuthorized(Board board, Coordinates destination){ + int dx = destination.getX(); + int dy = destination.getY(); + int ox = this.getX(); + int oy = this.getY(); + Coordinates origin = new Coordinates(ox,oy); + for(int i=0; i<=7;i++) { + for(int j=0; j<=7;j++) { + if(board.sameDiagonalNothingBetween(origin, destination) &&(dx-ox)%(dy-oy)==0) {return true;} + } + } + + + return false; + } + + @Override + public Type getType() { + return Type.BISHOP; + } + + @Override + public int getValue() { + return 3; + } + +} diff --git a/projetechecs-master/Board.java b/projetechecs-master/Board.java new file mode 100644 index 0000000000000000000000000000000000000000..5995f09e558c4a1b4a67abff74cbbf9394421ee7 --- /dev/null +++ b/projetechecs-master/Board.java @@ -0,0 +1,143 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import java.io.FileNotFoundException; +import java.io.File; + +public class Board{ + private Piece[][] array; + + public Board(String fileName, Player white, Player black){ + int pieceType; + int col; + int row; + String nextWord; + Player owner; + + this.array = new Piece[8][8]; + try { + File file = new File(fileName); + if(file.exists()==false) { + System.err.println("Error: Cannot find file "+ fileName); + System.exit(1); + } + + Scanner in = new Scanner(file); + while(in.hasNext()) { + if ((nextWord = in.nextLine()).length()>2) { + pieceType = nextWord.charAt(0); + col = nextWord.charAt(1)-'0'; + row = nextWord.charAt(2)-'0'; + + owner = black; + if (pieceType >= 'a' && pieceType <= 'z') + owner = white; + switch(pieceType) { + case 'K' : case 'k' : + { this.addPiece(new King(col, row, owner)); break;} + case 'Q' : case 'q' : + { this.addPiece(new Queen(col, row, owner)); break;} + case 'B' : case 'b' : + { this.addPiece(new Bishop(col, row, owner)); break;} + case 'R' : case 'r' : + { this.addPiece(new Rook(col, row, owner)); break;} + case 'N' : case 'n' : + { this.addPiece(new Knight(col, row, owner)); break;} + case 'P' : case 'p' : + { this.addPiece(new Pawn(col, row, owner)); break;} + } + } + } + in.close(); + } + catch(FileNotFoundException e) { + System.err.println("Error file not found : "+e); + System.exit(1); + } + } + + public List<Coordinates> getAllCoordinates(){ + List<Coordinates> allCoordinates = new ArrayList<>(); + for (int i = 0; i < 8; i++) for (int j = 0; j < 8; j++) allCoordinates.add(new Coordinates(j, i)); + return allCoordinates; + } + + public List<Piece> getPieces(Player player) { + List<Piece> pieces = new ArrayList<>(); + List<Piece> all = getPieces(); + for (Piece piece : all) if (player.equals(piece.getOwner())) pieces.add(piece); + return pieces; + } + + public List<Piece> getPieces() { + List<Piece> all = new ArrayList<>(); + for (Piece[] pieces : array) for (Piece piece : pieces) if (piece != null) all.add(piece); + return all; + } + + public void addPiece(Piece piece){ + array[piece.getY()][piece.getX()] = piece; + } + + public Piece getPiece(Coordinates coordinates){ + return array[coordinates.getY()][coordinates.getX()]; + } + + public Piece getPiece(int x, int y){ + return array[y][x]; + } + + public void emptyCell(Coordinates coordinates){ + array[coordinates.getY()][coordinates.getX()] = null; + } + + public boolean isEmptyCell(Coordinates coordinates){ + return array[coordinates.getY()][coordinates.getX()] == null; + } + + public boolean isEmptyCell(int x, int y){ + return array[y][x] == null; + } + + public boolean sameColumnNothingBetween(Coordinates origin, Coordinates destination){ + if (origin.equals(destination)) return false; + if (origin.getX() != destination.getX()) return false; + int min = Math.min(origin.getY(), destination.getY()); + int max = Math.max(origin.getY(), destination.getY()); + for (int i = min ; i <= max; i++) { + if (!isEmptyCell(origin.getX(), i)) return false; + } + return true; + } + + public boolean sameRowNothingBetween(Coordinates origin, Coordinates destination){ + if (origin.equals(destination)) return false; + if (origin.getY() != destination.getY()) return false; + int min = Math.min(origin.getX(), destination.getX()); + int max = Math.max(origin.getX(), destination.getX()); + for (int i = min + 1; i < max; i++) { + if (!isEmptyCell(i, origin.getY())) return false; + } + return true; + } + + public boolean sameDiagonalNothingBetween(Coordinates origin, Coordinates destination){ + if (origin.equals(destination)) return false; + int lenX = Math.abs(origin.getX() - destination.getX()); + int lenY = Math.abs(origin.getY() - destination.getY()); + if (lenX != lenY) return false; + if (origin.getX() - destination.getX() > 1) { + if (origin.getY() - destination.getY() > 1) + for (int i = 1; i < lenX; i++) if (!isEmptyCell(origin.getX() - i, origin.getY() - i)) return false; + if (origin.getY() - destination.getY() < -1) + for (int i = 1; i < lenX; i++) if (!isEmptyCell(origin.getX() - i, origin.getY() + i)) return false; + } + if (origin.getX() - destination.getX() < -1) { + if (origin.getY() - destination.getY() > 1) + for (int i = 1; i < lenX; i++) if (!isEmptyCell(origin.getX() + i, origin.getY() - i)) return false; + if (origin.getY() - destination.getY() < -1) + for (int i = 1; i < lenX; i++) if (!isEmptyCell(origin.getX() + i, origin.getY() + i)) return false; + } + return true; + } +} diff --git a/projetechecs-master/ChessBot.java b/projetechecs-master/ChessBot.java new file mode 100644 index 0000000000000000000000000000000000000000..3abb92bc104ab626a67e96945182627bb0211937 --- /dev/null +++ b/projetechecs-master/ChessBot.java @@ -0,0 +1,41 @@ +import java.util.Random; + +public class ChessBot extends Player { + + Board board; + + public ChessBot(ChessColor color){ + super(color); + board = null; + } + + public void setBoard(Board board){ + this.board = board; + } + + @Override + public FromTo getFromTo() { + Random rd = new Random(); + FromTo chosenMove = new FromTo(0,0,0,0); + int max = -1; + int scorePiece; + + for(Move move : getAllMoves(board)) { + if(move.pieceAtDestination == null) { + scorePiece = 0; + } else { + scorePiece = move.pieceAtDestination.getValue(); + } + + if(this.isCheck && move.pieceAtOrigin.getType() == Piece.Type.KING) + scorePiece += 100; + + if (scorePiece > max || (scorePiece == max && rd.nextBoolean())) { + chosenMove = new FromTo(move.origin.getX(),move.origin.getY(),move.destination.getX(),move.destination.getY()); + max = scorePiece; + } + } + + return chosenMove; + } +} diff --git a/projetechecs-master/ChessColor.java b/projetechecs-master/ChessColor.java new file mode 100644 index 0000000000000000000000000000000000000000..fc2226b75bb3b68f7711ccf0d567288c3324b777 --- /dev/null +++ b/projetechecs-master/ChessColor.java @@ -0,0 +1,4 @@ +public enum ChessColor { + BLACK, + WHITE; +} diff --git a/projetechecs-master/ChessUI.java b/projetechecs-master/ChessUI.java new file mode 100644 index 0000000000000000000000000000000000000000..d7c4917045830c3d9562ca0560a1ad28adc1b17d --- /dev/null +++ b/projetechecs-master/ChessUI.java @@ -0,0 +1,213 @@ +import java.awt.*; +import java.awt.Color; +import java.awt.event.*; +import java.util.*; +import javax.swing.*; + +public class ChessUI extends JFrame implements MouseListener, MouseMotionListener { + JLayeredPane layeredPane; + JPanel chessBoard; + int cellSize; + + private ImageIcon[] chessIcons; + private JLabel[] pieceLabels; + + JLabel movingChessPiece; + int originXMove; + int originYMove; + int destXMove; + int destYMove; + + boolean moveFreshness; + + int xAdjustment; + int yAdjustment; + + + public ChessUI(){ + this.cellSize = 60; + Dimension boardSize = new Dimension(8 * cellSize, 8 * cellSize); + + // Use a Layered Pane for this this application + layeredPane = new JLayeredPane(); + getContentPane().add(layeredPane); + layeredPane.setPreferredSize(boardSize); + layeredPane.addMouseListener(this); + layeredPane.addMouseMotionListener(this); + + //Add a chess board to the Layered Pane + + chessBoard = new JPanel(); + layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER); + chessBoard.setLayout( new GridLayout(8, 8) ); + chessBoard.setPreferredSize( boardSize ); + chessBoard.setBounds(0, 0, boardSize.width, boardSize.height); + + for (int i = 0; i < 64; i++) { + JPanel square = new JPanel( new BorderLayout() ); + chessBoard.add( square ); + + square.setBackground( (i + i / 8) % 2 == 1 ? Color.GRAY : Color.WHITE ); + } + + initIcons(); + initPieceLabels(); + + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + + pack(); + setResizable(true); + setLocationRelativeTo( null ); + setVisible(true); + } + + /** --------------------------------------------------------------------------------- + * Chess piece graphics **/ + + private void initIcons() { + chessIcons = new ImageIcon[] { + new ImageIcon("./img/Chess_kdt60.png"), + new ImageIcon("./img/Chess_qdt60.png"), + new ImageIcon("./img/Chess_rdt60.png"), + new ImageIcon("./img/Chess_bdt60.png"), + new ImageIcon("./img/Chess_ndt60.png"), + new ImageIcon("./img/Chess_pdt60.png"), + new ImageIcon("./img/Chess_klt60.png"), + new ImageIcon("./img/Chess_qlt60.png"), + new ImageIcon("./img/Chess_rlt60.png"), + new ImageIcon("./img/Chess_blt60.png"), + new ImageIcon("./img/Chess_nlt60.png"), + new ImageIcon("./img/Chess_plt60.png") + }; + } + + private void initPieceLabels() { + pieceLabels = new JLabel[64]; + + for(int k = 0; k < 64; k++) { + JLabel label = new JLabel(); + label.setVisible(false); + label.setSize(cellSize, cellSize); + pieceLabels[k] = label; + JPanel panel = (JPanel)chessBoard.getComponent(k); + panel.add(label); + } + } + + private JLabel getPieceLabel(int row, int column) { + return pieceLabels[row * 8 + column]; + } + + private ImageIcon getImageIcon(Piece.Type type, ChessColor color) { + return chessIcons[type.ordinal() + (color == ChessColor.WHITE ? Piece.Type.values().length : 0)]; + } + + /** + * This method takes a piece of a color and places it in the specified coordinates. + * @param type Desired type + * @param color Desired color (should only be either Color.white or Color.black) + * @param coord Coordinates of the piece + */ + public void placePiece(Piece.Type type, ChessColor color, Coordinates coord) { + getPieceLabel(coord.getY(), coord.getX()).setIcon(getImageIcon(type, color)); + getPieceLabel(coord.getY(), coord.getX()).setVisible(true); + } + + /** + * This method removes the piece at the specified position, if there is any. + * @param coord Coordinates of the piece + */ + public void removePiece(Coordinates coord) { + getPieceLabel(coord.getY(), coord.getX()).setVisible(false); + } + + /** --------------------------------------------------------------------------------- + * Events management **/ + + /** + * This method waits for the player to enter a move in the UI. Calling this is blocking. + */ + public FromTo waitForPlayerMove() { + moveFreshness = false; + + while(! moveFreshness) + try { + synchronized (this.getClass()) { + this.getClass().wait(); + } + } + catch (Exception e) { + System.err.println("ERROR : interrupted while waiting for chess move"); + System.err.println(e); + System.exit(-1); + } + + return new FromTo(originXMove, originYMove, destXMove, destYMove); + } + + public void mousePressed(MouseEvent e){ + movingChessPiece = null; + + originXMove = e.getX() / cellSize; + originYMove = e.getY() / cellSize; + + xAdjustment = - e.getX() + originXMove * cellSize; + yAdjustment = - e.getY() + originYMove * cellSize; + + movingChessPiece = new JLabel(getPieceLabel(originYMove, originXMove).getIcon()); + movingChessPiece.setVisible(getPieceLabel(originYMove, originXMove).isVisible()); + + movingChessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment); + if(movingChessPiece.getIcon() != null) + movingChessPiece.setSize(movingChessPiece.getIcon().getIconWidth(), movingChessPiece.getIcon().getIconHeight()); + layeredPane.add(movingChessPiece, JLayeredPane.DRAG_LAYER); + } + + //Move the chess piece around + + public void mouseDragged(MouseEvent me) { + if (movingChessPiece == null) return; + movingChessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment); + } + + //Drop the chess piece back onto the chess board + + public void mouseReleased(MouseEvent e) { + if(movingChessPiece == null) return; + + movingChessPiece.setVisible(false); + + layeredPane.remove(movingChessPiece); + movingChessPiece = null; + + destXMove = e.getX() / cellSize; + destYMove = e.getY() / cellSize; + + moveFreshness = true; + + synchronized (this.getClass()) { + this.getClass().notifyAll(); + } + } + + public void mouseClicked(MouseEvent e) {} + public void mouseMoved(MouseEvent e) {} + public void mouseEntered(MouseEvent e){} + public void mouseExited(MouseEvent e) {} + + public static void main(String[] args) { + ChessUI ui = new ChessUI(); + ui.setDefaultCloseOperation(DISPOSE_ON_CLOSE); + + for(int k = 0; k < Piece.Type.values().length; k++) + ui.placePiece(Piece.Type.values()[k], ChessColor.BLACK, new Coordinates(k, 0)); + + for(int k = 0; k < Piece.Type.values().length; k++) + ui.placePiece(Piece.Type.values()[k], ChessColor.WHITE, new Coordinates(k, 1)); + + ui.pack(); + ui.setResizable(true); + ui.setLocationRelativeTo( null ); + ui.setVisible(true); + } +} diff --git a/projetechecs-master/Coordinates.java b/projetechecs-master/Coordinates.java new file mode 100644 index 0000000000000000000000000000000000000000..68a191d5116c46b736b3027a302fd2f42e60350c --- /dev/null +++ b/projetechecs-master/Coordinates.java @@ -0,0 +1,29 @@ +public class Coordinates{ + private int x; + private int y; + + public Coordinates(int x, int y){ + this.x = x; + this.y = y; + } + + public int getX(){ + return x; + } + public int getY(){ + return y; + } + + @Override + public String toString(){ + return "x= "+x+" y= "+y; + } + + @Override + public boolean equals(Object o) { + if (this.getClass() != o.getClass()) return false; + if(this==o) return true; + Coordinates oCords = (Coordinates)o; + return this.x == oCords.x && this.y == oCords.y; + } +} diff --git a/projetechecs-master/FromTo.java b/projetechecs-master/FromTo.java new file mode 100644 index 0000000000000000000000000000000000000000..36c1c6ef3e97af3b059357b369ed349316174303 --- /dev/null +++ b/projetechecs-master/FromTo.java @@ -0,0 +1,17 @@ +public class FromTo { + private final Coordinates from; + private final Coordinates to; + + public FromTo(int ox, int oy, int dx, int dy) { + from = new Coordinates(ox,oy); + to = new Coordinates(dx,dy); + } + + public Coordinates getFrom() { + return this.from; + } + + public Coordinates getTo() { + return this.to; + } +} diff --git a/projetechecs-master/GameUI.java b/projetechecs-master/GameUI.java new file mode 100644 index 0000000000000000000000000000000000000000..e2cefc26a6a0617203028d5bf1976d2361a7fb15 --- /dev/null +++ b/projetechecs-master/GameUI.java @@ -0,0 +1,150 @@ +import java.util.List; +import java.util.Stack; + + + + +public class GameUI { + public Board board; + private Player white; + private Player black; + private Player currentPlayer; + private ChessUI ui; + private Stack<Move> history; + + public GameUI(ChessUI ui, String boardConfigFileName, Player white, Player black){ + this.board = new Board(boardConfigFileName, white, black); + this.white = white; + this.black = black; + this.currentPlayer = white; + this.ui = ui; + this.history = new Stack<Move>(); + + for(Piece p : board.getPieces()) + this.ui.placePiece(p.getType(), p.getColor(), p.getPosition()); + } + + public Board getBoard(){ + return this.board; + } + + public boolean undo(){ + if(this.history.empty()) return false; + Move move = this.history.pop(); + board.emptyCell(move.destination); + ui.removePiece(move.destination); + if(move.pieceAtDestination != null){ + move.pieceAtDestination.setPosition(move.destination); + board.addPiece(move.pieceAtDestination); + ui.placePiece(move.pieceAtDestination.getType(), move.pieceAtDestination.getColor(), move.pieceAtDestination.getPosition()); + } + board.emptyCell(move.origin); + ui.removePiece(move.origin); + move.pieceAtOrigin.setPosition(move.origin); + board.addPiece(move.pieceAtOrigin); + ui.placePiece(move.pieceAtOrigin.getType(), move.pieceAtOrigin.getColor(), move.pieceAtOrigin.getPosition()); + + currentPlayer = move.pieceAtOrigin.getOwner(); + if(move.pieceAtDestination != null) + currentPlayer.removeFromScore(move.pieceAtDestination.getValue()); + return true; + } + + + + public boolean isMovePlayable(Move gameMove){ + Piece piece = board.getPiece(gameMove.origin); + if(piece == null + ||currentPlayer.getColor() != piece.getColor() + || !gameMove.pieceAtOrigin.isMoveAuthorized(board, gameMove.destination) + + || gameMove.pieceAtOrigin.getOwner() != currentPlayer){ + System.out.println("Le coups ne peut pas etre jouer 1"); + return false;} + + + + + if (gameMove.pieceAtDestination != null + && gameMove.pieceAtDestination.sameColor(gameMove.pieceAtOrigin)) { + System.out.println("Le coups ne peut pas etre jouer 2"); + + + + return false;} + + + applyMove(gameMove); + + /**if (isCheck(getOpponent(currentPlayer))) { + undo(); + return false; + } + + ***/ + undo(); + return true; + + } + + public void applyMove(Move move){ + Piece piece = board.getPiece(move.origin); + history.push(move); + board.emptyCell(piece.getPosition()); + piece.setPosition(move.destination); + board.addPiece(piece); + switchPlayers(); + } + + public void switchPlayers(){ + this.currentPlayer = getOpponent(currentPlayer); + } + + public Player getOpponent(Player player){ + if (player == white) return black; + return player; + } + + public boolean isPrey(Piece prey){ + Player playerOp = getOpponent(prey.owner); + List<Move> opponentMoves = playerOp.getAllMoves(board); + for (Move move : opponentMoves) { + if (prey.equals(move.pieceAtDestination)) return true; + } + return false; + } + + public boolean isCheck(Player player){return isPrey(player.getKing());} + + public boolean isCheckMate(Player player){ + return false; + } + + public void determineWinner(){ + } + + public void play(){ + int numberOfHits = 0; + while(numberOfHits < 50){ + System.out.println("A toi de jouer " + currentPlayer.getColor()); + Move move; + do { + move = new Move(board, currentPlayer.getFromTo()); + } + while(!isMovePlayable(move)); + applyMove(move); + ui.placePiece(move.pieceAtOrigin.getType(), move.pieceAtOrigin.getColor(), move.destination); + ui.removePiece(move.origin); + numberOfHits++; + switchPlayers(); + + + + + + numberOfHits ++; + System.out.println("Nombre de coups: " + numberOfHits+" /50"); + switchPlayers(); + } + } +} diff --git a/projetechecs-master/Human.java b/projetechecs-master/Human.java new file mode 100644 index 0000000000000000000000000000000000000000..b99515747020814ebe9c4576560b71ed9118d510 --- /dev/null +++ b/projetechecs-master/Human.java @@ -0,0 +1,14 @@ +public class Human extends Player{ + + ChessUI ui; + public Human(ChessUI ui, ChessColor color){ + super(color); + this.ui = ui; + } + + @Override + public FromTo getFromTo() { + return ui.waitForPlayerMove(); + } + +} diff --git a/projetechecs-master/King.java b/projetechecs-master/King.java new file mode 100644 index 0000000000000000000000000000000000000000..96ea876c597020c34f56dae126c9c15b5a57eb4e --- /dev/null +++ b/projetechecs-master/King.java @@ -0,0 +1,38 @@ +public class King extends Piece { + + public King(int x, int y, Player owner){ + super(x, y, owner); + owner.setKing(this); + } + + public boolean isMoveAuthorized(Board board, Coordinates destination){ + int dx = destination.getX(); + int dy = destination.getY(); + int ox = this.getX(); + int oy = this.getY(); + + for(int i=-1;i<=1;i++) { + for(int j=-1; j<=1; j++) { + if( (dx-ox)==i && (dy-oy)==j && board.isEmptyCell(dx, dy)) { + return true; + } + } + + } + + + + return false; + } + + @Override + public Type getType() { + return Type.KING; + } + + @Override + public int getValue() { + return 0; + } + +} diff --git a/projetechecs-master/Knight.java b/projetechecs-master/Knight.java new file mode 100644 index 0000000000000000000000000000000000000000..318c6911ea99cf98bec6d8f5f67e9e9c6a989b86 --- /dev/null +++ b/projetechecs-master/Knight.java @@ -0,0 +1,35 @@ +public class Knight extends Piece { + + public Knight(int x, int y, Player owner){ + super(x, y, owner); + + } + + public boolean isMoveAuthorized(Board board, Coordinates destination){ + int dx = destination.getX(); + int dy = destination.getY(); + int ox = this.getX(); + int oy = this.getY(); + for(int i=-2; i <= 2; i++) { + for(int j=-2; j <= 2; j++) { + if(((dx-ox)%2==1 && (dy-oy)%2==0) || ((dx-ox)%2==0 && (dy-oy)%2==1)) { + return true; + } + } + } + + + return false; + } + + @Override + public Type getType() { + return Type.KNIGHT; + } + + @Override + public int getValue() { + return 3; + } + +} diff --git a/projetechecs-master/Main.java b/projetechecs-master/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..708eab91dc389a2e5197e05de6c2a1680c49aea0 --- /dev/null +++ b/projetechecs-master/Main.java @@ -0,0 +1,9 @@ +public class Main{ + public static void main(String[] args) { + ChessUI ui = new ChessUI(); + GameUI g = new GameUI(ui, "boardConfigurationFiles/FullBoard.txt", new Human(ui, ChessColor.WHITE), new Human(ui, ChessColor.BLACK)); + + g.play(); + } + +} diff --git a/projetechecs-master/Move.java b/projetechecs-master/Move.java new file mode 100644 index 0000000000000000000000000000000000000000..3ab7d9b2915737d53d2703af5f736f4f9eb8bba8 --- /dev/null +++ b/projetechecs-master/Move.java @@ -0,0 +1,29 @@ +public class Move{ + + final Coordinates origin; + final Coordinates destination; + final Piece pieceAtOrigin; + final Piece pieceAtDestination; + + public Move(Board board, Coordinates origin, Coordinates destination){ + this.origin = origin; + this.destination = destination; + this.pieceAtOrigin = board.getPiece(origin); + this.pieceAtDestination = board.getPiece(destination); + } + + public Move(Coordinates origin, Coordinates destination, Piece pieceAtOrigin, Piece pieceAtDestination){ + this.origin = origin; + this.destination = destination; + this.pieceAtOrigin = pieceAtOrigin; + this.pieceAtDestination = pieceAtDestination; + } + + public Move(Board board, FromTo ft){ + this.origin = ft.getFrom(); + this.destination = ft.getTo(); + this.pieceAtOrigin = board.getPiece(ft.getFrom()); + this.pieceAtDestination = board.getPiece(ft.getTo()); + } +} + diff --git a/projetechecs-master/Pawn.java b/projetechecs-master/Pawn.java new file mode 100644 index 0000000000000000000000000000000000000000..ef8fb5b15c2ba7ec6773d93490a82f8a1a8f1fbf --- /dev/null +++ b/projetechecs-master/Pawn.java @@ -0,0 +1,37 @@ +public class Pawn extends Piece { + + public Pawn(int x, int y, Player owner){ + super(x, y, owner); + + } + + public boolean isMoveAuthorized(Board board, Coordinates destination){ + int dx = destination.getX(); + int dy = destination.getY(); + int ox = this.getX(); + int oy = this.getY(); + int absx = Math.abs(dx-ox); + int absy = Math.abs(dy-oy); + for(int i=0; i<=7; i++) { + + if((oy==1 || oy == 6) && (absy==1 || absx == 2) && board.isEmptyCell(destination)&&absx==0) {return true;} + + if((oy!=1 || oy!=6) && absy==1 && absx==0){return true;} + return false; + } + + + return true; + } + + @Override + public Type getType() { + return Type.PAWN; + } + + @Override + public int getValue() { + return 1; + } + +} diff --git a/projetechecs-master/Piece.java b/projetechecs-master/Piece.java new file mode 100644 index 0000000000000000000000000000000000000000..efe0429c6239199d1045e7d0d91368f3a493ca32 --- /dev/null +++ b/projetechecs-master/Piece.java @@ -0,0 +1,66 @@ +import java.util.ArrayList; +import java.util.List; + +public abstract class Piece{ + protected Coordinates position; + protected Player owner; + + public Piece(int x, int y, Player owner){ + this.position = new Coordinates(x,y); + this.owner = owner; + } + + public enum Type { + KING, + QUEEN, + ROOK, + BISHOP, + KNIGHT, + PAWN + } + + public void setPosition(Coordinates destination){ + this.position = destination; + } + + public Player getOwner(){ + return this.owner; + } + + public ChessColor getColor(){ + return this.owner.color; + } + + public Coordinates getPosition(){ + return this.position; + } + + public int getX(){ + return this.position.getX(); + } + + public int getY(){ + return this.position.getY(); + } + + public List<Move> getAllMoves(Board board) { + List<Move> allMoves = new ArrayList<>(); + for (Coordinates coordinates : board.getAllCoordinates()){ + if (this.isMoveAuthorized(board,coordinates)){ + allMoves.add(new Move(board, position, coordinates)); + } + } + return allMoves; + } + + public boolean sameColor(Piece piece){ + return this.getColor()== piece.getColor(); + } + + public abstract boolean isMoveAuthorized(Board board, Coordinates destination); + + public abstract Type getType(); + public abstract int getValue(); + + +} diff --git a/projetechecs-master/Player.java b/projetechecs-master/Player.java new file mode 100644 index 0000000000000000000000000000000000000000..1a38ecdf9ed0ead715c05f3c2329425e52570af2 --- /dev/null +++ b/projetechecs-master/Player.java @@ -0,0 +1,61 @@ +import java.util.ArrayList; +import java.util.List; + +public abstract class Player{ + protected ChessColor color; + private int score; + private King king; + public boolean isCheck; + public boolean isCheckMate; + + public Player(ChessColor color){ + this.color = color; + this.score = 0; + this.isCheckMate = false; + } + + public ChessColor getColor(){ + return this.color; + } + + public int getScore(){ + return this.score; + } + + public void addToScore(int value){ + score+=value; + } + + public void removeFromScore(int value){ + score-= value; + } + + public abstract FromTo getFromTo(); + + public Piece getKing(){ + return this.king; + } + + public void setKing(King king){ + this.king=king; + } + + public boolean isCheckMate(Board board){ + return false; + } + + public void setCheck(){ + } + + public void unSetCheck(){ + } + + public List<Move> getAllMoves(Board board) { + return null; + } + + @Override + public String toString(){ + return null; + } +} diff --git a/projetechecs-master/Queen.java b/projetechecs-master/Queen.java new file mode 100644 index 0000000000000000000000000000000000000000..abdbfc69669ca1143648c7b2d5cfb23a7b5c02ba --- /dev/null +++ b/projetechecs-master/Queen.java @@ -0,0 +1,43 @@ +public class Queen extends Piece { + + public Queen(int x, int y, Player owner){ + super(x, y, owner); + + } + + public boolean isMoveAuthorized(Board board, Coordinates destination){ + int dx = destination.getX(); + int dy = destination.getY(); + int ox = this.getX(); + int oy = this.getY(); + Coordinates origin = new Coordinates(ox,oy); + + + + for(int i=0; i<=7;i++) { + for(int j=0; j<=7;j++) { + if(board.sameColumnNothingBetween(origin,destination)&& dy<=7 && dy >=0) {return true;} + else if(board.sameRowNothingBetween(origin,destination)&& dx<=7 && dx>=0) {return true;} + else if(board.sameDiagonalNothingBetween(origin, destination) &&(dx-ox)%(dy-oy)==0) {return true;} + } + } + + + + + + return false; + + } + + @Override + public Type getType() { + return Type.QUEEN; + } + + @Override + public int getValue() { + return 9; + } + +} diff --git a/projetechecs-master/Rook.java b/projetechecs-master/Rook.java new file mode 100644 index 0000000000000000000000000000000000000000..f7abc5dcef5c51365e0a71d58646f2d29ad46bf8 --- /dev/null +++ b/projetechecs-master/Rook.java @@ -0,0 +1,33 @@ +public class Rook extends Piece { + + public Rook(int x, int y, Player owner){ + super(x, y, owner); + + } + + public boolean isMoveAuthorized(Board board, Coordinates destination){ + int dx = destination.getX(); + int dy = destination.getY(); + int ox = this.getX(); + int oy = this.getY(); + Coordinates origin = new Coordinates(ox,oy); + for(int i=0; i<=7;i++) { + for(int j=0; j<=7;j++) { + if(board.sameColumnNothingBetween(origin,destination)&& dy<=7 && dy >=0) {return true;} + else if(board.sameRowNothingBetween(origin,destination)&& dx<=7 && dx>=0) {return true;} + } + } + return false; + } + + @Override + public Type getType() { + return Type.ROOK; + } + + @Override + public int getValue() { + return 5; + } + +} diff --git a/projetechecs-master/TestChess.java b/projetechecs-master/TestChess.java new file mode 100644 index 0000000000000000000000000000000000000000..d4e40c1957506d6e67de062ccac751858ba1d651 --- /dev/null +++ b/projetechecs-master/TestChess.java @@ -0,0 +1,76 @@ +import java.io.*; +import java.util.Scanner; + +public class TestChess{ + + public static void main(String[] args) { + + boolean resultTest; + /* Test de déplacements autorisés selon les regles de pièces */ + System.out.println("authorized moves"); + System.out.print("test 1 : "); + resultTest = testAuthorizedMove("boardConfigurationFiles/FullBoard.txt", new Coordinates(0,1), new Coordinates(0,2)); + if(resultTest == true) System.out.println("pass"); else System.out.println("fail"); + + System.out.print("test 2 : "); + resultTest = testAuthorizedMove("boardConfigurationFiles/FullBoard.txt", new Coordinates(0,1), new Coordinates(0,4)); + if(resultTest == false) System.out.println("pass"); else System.out.println("fail"); + + + /* Test de déplacements jouables sur l'échiquier actuel, selon les regles du jeu */ + System.out.println("playable moves"); + System.out.print("test 1 : "); + resultTest = testPlayableMove("boardConfigurationFiles/FullBoard.txt",new Coordinates(0,1),new Coordinates(0,2)); + if(resultTest == true) System.out.println("pass"); else System.out.println("fail"); + + System.out.print("test 2 : "); + resultTest = testPlayableMove("boardConfigurationFiles/FullBoard.txt",new Coordinates(0,1),new Coordinates(0,3)); + if(resultTest == true) System.out.println("pass"); else System.out.println("fail"); + + /* Tests de la mise en échec */ + + /* Tests de la Echec et mat "isCheckMate()" */ + + /* Tests pours le calcul des points en fin de partie */ + } + + + public static boolean testAuthorizedMove(String filename, Coordinates origin, Coordinates destination) { + ChessUI ui = new ChessUI(); + Board testBoard = new Board(filename, new Human(ui, ChessColor.WHITE), new Human(ui, ChessColor.BLACK)); + Piece testPiece = testBoard.getPiece(origin); + if(testPiece == null) { + System.out.println("No Piece at :"+origin); + return false; + } + return testPiece.isMoveAuthorized(testBoard, destination); + } + + + public static boolean testPlayableMove(String fileName, Coordinates origin, Coordinates destination) { + ChessUI ui = new ChessUI(); + GameUI g = new GameUI(ui, fileName, new Human(ui, ChessColor.WHITE), new Human(ui, ChessColor.BLACK)); + + Piece testPiece = g.getBoard().getPiece(origin); + if(testPiece == null) { + System.out.println("No Piece at :"+origin); + return false; + } + return g.isMovePlayable(new Move(g.getBoard(), origin, destination)); + } + + public static boolean testIsCheck(String fileName, Player p) { + ChessUI ui = new ChessUI(); + GameUI g = new GameUI(ui, fileName, new Human(ui, ChessColor.WHITE), new Human(ui, ChessColor.BLACK)); + return g.isCheck(p); + } + + public static boolean testIsCheckMate(String fileName, Player p) { + ChessUI ui = new ChessUI(); + GameUI g = new GameUI(ui, fileName, new Human(ui, ChessColor.WHITE), new Human(ui, ChessColor.BLACK)); + return g.isCheck(p) && g.isCheckMate(p); + } + + +} + diff --git a/projetechecs-master/boardConfigurationFiles/BackLine.txt b/projetechecs-master/boardConfigurationFiles/BackLine.txt new file mode 100644 index 0000000000000000000000000000000000000000..97b7ec568f0c89889b659a12211874ee97cbf2f0 --- /dev/null +++ b/projetechecs-master/boardConfigurationFiles/BackLine.txt @@ -0,0 +1,16 @@ +r00 +n10 +b20 +q30 +k40 +b50 +n60 +r70 +R07 +N17 +B27 +Q37 +K47 +B57 +N67 +R77 diff --git a/projetechecs-master/boardConfigurationFiles/FullBoard.txt b/projetechecs-master/boardConfigurationFiles/FullBoard.txt new file mode 100644 index 0000000000000000000000000000000000000000..e653b8919306e4564ba50f3929b833f0e7a47ea9 --- /dev/null +++ b/projetechecs-master/boardConfigurationFiles/FullBoard.txt @@ -0,0 +1,32 @@ +r00 +n10 +b20 +k30 +q40 +b50 +n60 +r70 +p01 +p11 +p21 +p31 +p41 +p51 +p61 +p71 +P06 +P16 +P26 +P36 +P46 +P56 +P66 +P76 +R07 +N17 +B27 +K37 +Q47 +B57 +N67 +R77 diff --git a/projetechecs-master/boardConfigurationFiles/KingsOnly.txt b/projetechecs-master/boardConfigurationFiles/KingsOnly.txt new file mode 100644 index 0000000000000000000000000000000000000000..c0392a7b9884e8a0faf8aaca3616df6eb9b563ca --- /dev/null +++ b/projetechecs-master/boardConfigurationFiles/KingsOnly.txt @@ -0,0 +1,3 @@ +K30 +k37 + diff --git a/projetechecs-master/boardConfigurationFiles/check.txt b/projetechecs-master/boardConfigurationFiles/check.txt new file mode 100644 index 0000000000000000000000000000000000000000..0d8d02ebcffbbcd8c73b64d3902e050d825b67ad --- /dev/null +++ b/projetechecs-master/boardConfigurationFiles/check.txt @@ -0,0 +1,3 @@ +r00 +k40 +K07 \ No newline at end of file diff --git a/projetechecs-master/boardConfigurationFiles/check2.txt b/projetechecs-master/boardConfigurationFiles/check2.txt new file mode 100644 index 0000000000000000000000000000000000000000..49ff12e1b31f63f18d6dac4a3b37fd95b0f8b56f --- /dev/null +++ b/projetechecs-master/boardConfigurationFiles/check2.txt @@ -0,0 +1,4 @@ +r00 +k40 +R06 +K07 \ No newline at end of file diff --git a/projetechecs-master/boardConfigurationFiles/checkMate.txt b/projetechecs-master/boardConfigurationFiles/checkMate.txt new file mode 100644 index 0000000000000000000000000000000000000000..5f1fe1077539c17b09b3810435b8d42036c24346 --- /dev/null +++ b/projetechecs-master/boardConfigurationFiles/checkMate.txt @@ -0,0 +1,4 @@ +r00 +r10 +k40 +K07 \ No newline at end of file diff --git a/projetechecs-master/boardConfigurationFiles/checkMate2.txt b/projetechecs-master/boardConfigurationFiles/checkMate2.txt new file mode 100644 index 0000000000000000000000000000000000000000..2e746fdf7720e200f86170a0c73af2792c042d22 --- /dev/null +++ b/projetechecs-master/boardConfigurationFiles/checkMate2.txt @@ -0,0 +1,4 @@ +r00 +r20 +k40 +K07 \ No newline at end of file diff --git a/projetechecs-master/img/Chess_bdt60.png b/projetechecs-master/img/Chess_bdt60.png new file mode 100644 index 0000000000000000000000000000000000000000..453cb32355348ceadcc41e5c371130e7be1dc45e Binary files /dev/null and b/projetechecs-master/img/Chess_bdt60.png differ diff --git a/projetechecs-master/img/Chess_blt60.png b/projetechecs-master/img/Chess_blt60.png new file mode 100644 index 0000000000000000000000000000000000000000..26dae01cf054534b4f5c56ac60f334a7ab575f10 Binary files /dev/null and b/projetechecs-master/img/Chess_blt60.png differ diff --git a/projetechecs-master/img/Chess_kdt60.png b/projetechecs-master/img/Chess_kdt60.png new file mode 100644 index 0000000000000000000000000000000000000000..225f869e9cc481683eae10622c0dad885c3aa3d4 Binary files /dev/null and b/projetechecs-master/img/Chess_kdt60.png differ diff --git a/projetechecs-master/img/Chess_klt60.png b/projetechecs-master/img/Chess_klt60.png new file mode 100644 index 0000000000000000000000000000000000000000..d7341649bbfa2508149d8f57562503e6076aaab6 Binary files /dev/null and b/projetechecs-master/img/Chess_klt60.png differ diff --git a/projetechecs-master/img/Chess_ndt60.png b/projetechecs-master/img/Chess_ndt60.png new file mode 100644 index 0000000000000000000000000000000000000000..8e3d04e6fa29d261b11356ebf3503e09d20f3781 Binary files /dev/null and b/projetechecs-master/img/Chess_ndt60.png differ diff --git a/projetechecs-master/img/Chess_nlt60.png b/projetechecs-master/img/Chess_nlt60.png new file mode 100644 index 0000000000000000000000000000000000000000..2d716b15b180f2a9b194cd78df3a5e7e9a0c22b0 Binary files /dev/null and b/projetechecs-master/img/Chess_nlt60.png differ diff --git a/projetechecs-master/img/Chess_pdt60.png b/projetechecs-master/img/Chess_pdt60.png new file mode 100644 index 0000000000000000000000000000000000000000..c432d38aeda8c8a49c772c9d7295d0359add9c01 Binary files /dev/null and b/projetechecs-master/img/Chess_pdt60.png differ diff --git a/projetechecs-master/img/Chess_plt60.png b/projetechecs-master/img/Chess_plt60.png new file mode 100644 index 0000000000000000000000000000000000000000..e98fae2bd8dd89313781f3107fe1b156e4674907 Binary files /dev/null and b/projetechecs-master/img/Chess_plt60.png differ diff --git a/projetechecs-master/img/Chess_qdt60.png b/projetechecs-master/img/Chess_qdt60.png new file mode 100644 index 0000000000000000000000000000000000000000..0d94a1c29a50896a6fbe017e1eeda9b5a42fe0a4 Binary files /dev/null and b/projetechecs-master/img/Chess_qdt60.png differ diff --git a/projetechecs-master/img/Chess_qlt60.png b/projetechecs-master/img/Chess_qlt60.png new file mode 100644 index 0000000000000000000000000000000000000000..a4fe68c80f79ab7b0b2ed6147383cda435407ea1 Binary files /dev/null and b/projetechecs-master/img/Chess_qlt60.png differ diff --git a/projetechecs-master/img/Chess_rdt60.png b/projetechecs-master/img/Chess_rdt60.png new file mode 100644 index 0000000000000000000000000000000000000000..b9748e87f29861cb4ca00c7ce03df54f3f6b35a0 Binary files /dev/null and b/projetechecs-master/img/Chess_rdt60.png differ diff --git a/projetechecs-master/img/Chess_rlt60.png b/projetechecs-master/img/Chess_rlt60.png new file mode 100644 index 0000000000000000000000000000000000000000..a805de496f4d152dd02026bed09b45548b6e88d8 Binary files /dev/null and b/projetechecs-master/img/Chess_rlt60.png differ diff --git a/projetechecs-master/readme.md b/projetechecs-master/readme.md new file mode 100644 index 0000000000000000000000000000000000000000..57083f1d12f3ce5f2da548a004df0a34243e1fb4 --- /dev/null +++ b/projetechecs-master/readme.md @@ -0,0 +1,78 @@ +# Projet de jeu d'échec en Java + +Projet Final (S3 Programmation 2) + +## Binôme + +Alexandre AGUEDO + +Astrid BEYER + + +Aix Marseille Université + +L2 Informatique + +Groupe 3 + +## Encadrant de TPs et projet + +Pacôme PERROTIN + +## Objectif et tâches + +L’objectif de ce projet est de programmer un jeu d’´echec. Ce jeu permet `a deux joueurs humains de jouer ensemble +ou bien à un joueur humain de jouer contre une version très simple d’un joueur artificiel, ou à deux joueurs artificiels +de jouer ensemble ! + +En savoir plus : http://pageperso.lif.univ-mrs.fr/~alexis.nasr/Ens/PROG2/projet_echecs_an.pdf + +## Les fichiers + +* boardConfigurationFiles/ +> Dossier contenant des placements de pièces pré-remplis, ces placements sont utiles pour tester le programme dans diverses situations. + +* img/ +> Dossier avec les images en format PNG des 12 pièces de l'échiquier (6 noires et 6 blanches). + +* Player.java +> Classe abstraite déterminant un joueur. + +* Board.java +> Classe permettant d'instancier le plateau de jeu d'échec. + +* ChessBot.java +> Classe qui étend Player. ChessBot est la classe d'un joueur robot qui va choisir les coups les plus avantageux (rapportant le plus de score) lorsqu'il jouera. + +* Human.java +> Classe qui étend Player. Cette classe permet à un utilisateur de jouer. + +* ChessColor.java +> Énumération des couleurs possibles des joueurs/pièces. + +* Piece.java +> Classe abstraite déterminant une pièce de l'échiquier. + +* Bishop.java ; King.java ; Knight.java ; Pawn.java ; Queen.java ; Rook.java +> Respectivement les classes du fou, roi, cavalier, pion, dame et tour. Ces classes étendent toutes la classe Piece et déterminent, pour chaque pièce, leurs mouvements possibles et autorisés. + +* TestChess.java +> Fichier de tests. + +* Move.java +> Classe publique qui permet à une pièce de se déplacer d'un point à un autre dans l'échiquier. + +* ChessUI.java +> Classe gérant l'interface du jeu d'échec et la possibilité de déplacer ses pièces avec sa souris. + +* GameUI.java +> Classe gérant le déroulement de la partie. + +* Coordinates.java +> Classe permettant de récupérer des coordonnées. + +* FromTo.java +> Classe permettant de récupérer les coordonnées d'origine d'une pièce et les coordonnées de destination d'une pièce. + +* Main.java +> Classe principale qui lance le jeu. \ No newline at end of file