Skip to content
Snippets Groups Projects
Commit 9e7605bd authored by Alexis Nasr's avatar Alexis Nasr
Browse files

initialisation

parents
No related branches found
No related tags found
No related merge requests found
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;}
}
}
}
in.close();
}
catch(FileNotFoundException e) {
System.err.println("Error file not found : "+e);
System.exit(1);
}
}
public List<Coordinates> getAllCoordinates(){
return null;
}
public List<Piece> getPieces(Player player) {
return null;
}
public List<Piece> getPieces() {
return null;
}
public void addPiece(Piece piece){
}
public Piece getPiece(Coordinates coordinates){
return null;
}
public Piece getPiece(int x, int y){
return null;
}
public void emptyCell(Coordinates coordinates){
}
public boolean isEmptyCell(Coordinates coordinates){
return true;
}
public boolean isEmptyCell(int x, int y){
return true;
}
public boolean sameColumnNothingBetween(Coordinates origin, Coordinates destination){
return false;
}
public boolean sameLineNothingBetween(Coordinates origin, Coordinates destination){
return false;
}
public boolean sameDiagonalNothingBetween(Coordinates origin, Coordinates destination){
return false;
}
}
public enum ChessColor {
BLACK,
WHITE;
}
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);
}
}
public class Coordinates{
private int x;
private int y;
public Coordinates(int x, int y){
}
public int getX(){
return 0;
}
public int getY(){
return 0;
}
@Override
public String toString(){
return "";
}
@Override
public boolean equals(Object o) {
return true;
}
}
public class FromTo {
private final Coordinates from;
private final Coordinates to;
public FromTo(int ox, int oy, int dx, int dy) {
from = null;
to = null;
}
public Coordinates getFrom() {
return null;
}
public Coordinates getTo() {
return null;
}
}
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 null;
}
public boolean undo(){
return true;
}
public boolean isMovePlayable(Move gameMove){
return true;
}
public void applyMove(Move move){
}
public void switchPlayers(){
}
public Player getOpponent(Player player){
return null;
}
public boolean isPrey(Piece prey){
return false;
}
public boolean isCheck(Player player){
return false;
}
public boolean isCheckMate(Player player){
return false;
}
public void determineWinner(){
}
public void play(){
}
}
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();
}
}
public class King extends Piece {
public King(int x, int y, Player owner){
super(x, y, owner);
owner.setKing(this);
}
public boolean isMoveLegal(Board board, Coordinates destination){
int dx = destination.getX();
int dy = destination.getY();
int ox = this.getX();
int oy = this.getY();
return true;
}
@Override
public Type getType() {
return Type.KING;
}
@Override
public int getValue() {
return 0;
}
}
public class Main{
public static void main(String[] args) {
ChessUI ui = new ChessUI();
GameUI g = new GameUI(ui, "boardConfigurationFiles/KingsOnly.txt", new Human(ui, ChessColor.WHITE), new Human(ui, ChessColor.BLACK));
g.play();
}
}
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 = null;
this.destination = null;
this.pieceAtOrigin = null;
this.pieceAtDestination = null;
}
public Move(Coordinates origin, Coordinates destination, Piece pieceAtOrigin, Piece pieceAtDestination){
this.origin = null;
this.destination = null;
this.pieceAtOrigin = null;
this.pieceAtDestination = null;
}
public Move(Board board, FromTo ft){
this.origin = null;
this.destination = null;
this.pieceAtOrigin = null;
this.pieceAtDestination = null;
}
}
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){
}
public enum Type {
KING,
QUEEN,
ROOK,
BISHOP,
KNIGHT,
PAWN
}
public void setPosition(Coordinates destination){
}
public Player getOwner(){
return null;
}
public ChessColor getColor(){
return null;
}
public Coordinates getPosition(){
return null;
}
public int getX(){
return 0;
}
public int getY(){
return 0;
}
public List<Move> getAllMoves(Board board) {
return null;
}
public boolean sameColor(Piece piece){
return true;
}
public abstract boolean isMoveLegal(Board board, Coordinates destination);
public abstract Type getType();
public abstract int getValue();
public boolean isMovePlayable(Board board, Coordinates destination){
return true;
}
}
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){
}
public ChessColor getColor(){
return null;
}
public int getScore(){
return 0;
}
public void addToScore(int value){
}
public void removeFromScore(int value){
}
public abstract FromTo getFromTo();
public Piece getKing(){
return null;
}
public void setKing(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;
}
}
import java.io.*;
import java.util.Scanner;
public class TestChess{
public static void main(String[] args) {
boolean result;
/* Test de déplacements autorisés selon les regles de pièces */
System.out.println("legal moves");
System.out.print("test 1 : ");
result = testLegalMove("boardConfigurationFiles/FullBoard.txt", new Coordinates(0,1), new Coordinates(0,2));
if(result == true) System.out.println("pass"); else System.out.println("fail");
System.out.print("test 2 : ");
result = testLegalMove("boardConfigurationFiles/FullBoard.txt", new Coordinates(0,1), new Coordinates(0,4));
if(result == false) System.out.println("pass"); else System.out.println("fail");
/* Test de déplacements legal sur l'échiquier actuel, selon les regles du jeu */
System.out.println("playable moves");
System.out.print("test 1 : ");
result = testPlayableMove("boardConfigurationFiles/FullBoard.txt",new Coordinates(0,1),new Coordinates(0,2));
if(result == true) System.out.println("pass"); else System.out.println("fail");
System.out.print("test 2 : ");
result = testPlayableMove("boardConfigurationFiles/FullBoard.txt",new Coordinates(0,1),new Coordinates(0,3));
if(result == true) System.out.println("pass"); else System.out.println("fail");
/* Tests de la mise en echec */
/* Tests de la Echec et mat "isCheckMate()" */
/* Tests pours le calcul des points en fin de partie */
}
public static boolean testLegalMove(String filename, Coordinates origin, Coordinates destination) {
ChessUI ui = new ChessUI(false);
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.isMoveLegal(testBoard, destination);
}
public static boolean testPlayableMove(String fileName, Coordinates origin, Coordinates destination) {
ChessUI ui = new ChessUI(false);
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(false);
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(false);
GameUI g = new GameUI(ui, fileName, new Human(ui, ChessColor.WHITE), new Human(ui, ChessColor.BLACK));
return g.isCheck(p) && g.isCheckMate(p);
}
}
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
K30
k37
img/Chess_bdt60.png

1.23 KiB

img/Chess_blt60.png

1.9 KiB

img/Chess_kdt60.png

2.43 KiB

img/Chess_klt60.png

2.23 KiB

img/Chess_ndt60.png

1.48 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment