working piece display + basic moves
This commit is contained in:
80
app/src/main/java/chess/model/Board.java
Normal file
80
app/src/main/java/chess/model/Board.java
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package chess.model;
|
||||||
|
|
||||||
|
import chess.model.pieces.*;
|
||||||
|
|
||||||
|
public class Board {
|
||||||
|
private final Cell[][] board;
|
||||||
|
private static final int WHITE = 0;
|
||||||
|
private static final int BLACK = 1;
|
||||||
|
|
||||||
|
public Board(int size){
|
||||||
|
board = new Cell[size][size];
|
||||||
|
for (int y = 0; y < size; y++) {
|
||||||
|
for (int x = 0; x < size; x++) {
|
||||||
|
board[y][x] = new Cell();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setPieces();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Board(){
|
||||||
|
this(8);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Cell getCell(Coordinates coordinates){
|
||||||
|
return board[coordinates.getX()][coordinates.getY()];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSize(){
|
||||||
|
return board.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPieces() throws Error {
|
||||||
|
if (this.getSize() == 8) {
|
||||||
|
setPiecesSize8();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new Error("Oops. This isn't implemented yet.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPiecesSize8(){
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
board[i][1].setPiece(new Pawn(WHITE));
|
||||||
|
board[i][6].setPiece(new Pawn(BLACK));
|
||||||
|
if (i==0 || i==7) {
|
||||||
|
board[i][0].setPiece(new Rook(WHITE));
|
||||||
|
board[i][7].setPiece(new Rook(BLACK));
|
||||||
|
}
|
||||||
|
if (i==1 || i==6) {
|
||||||
|
board[i][0].setPiece(new Knight(WHITE));
|
||||||
|
board[i][7].setPiece(new Knight(BLACK));
|
||||||
|
}
|
||||||
|
if (i==2 || i==5) {
|
||||||
|
board[i][0].setPiece(new Bishop(WHITE));
|
||||||
|
board[i][7].setPiece(new Bishop(BLACK));
|
||||||
|
}
|
||||||
|
if (i==3) {
|
||||||
|
board[i][0].setPiece(new King(WHITE));
|
||||||
|
board[i][7].setPiece(new King(BLACK));
|
||||||
|
}
|
||||||
|
if (i==4) {
|
||||||
|
board[i][0].setPiece(new Queen(WHITE));
|
||||||
|
board[i][7].setPiece(new Queen(BLACK));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Piece getPiece(Coordinates coordinates){
|
||||||
|
return getCell(coordinates).getPiece();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void movePiece(Coordinates initialPosition, Coordinates finalPosition) throws Error {
|
||||||
|
Piece movingPiece = getPiece(initialPosition);
|
||||||
|
Cell arrivalCell = getCell(finalPosition);
|
||||||
|
getCell(initialPosition).deletePiece();
|
||||||
|
arrivalCell.setPiece(movingPiece);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
25
app/src/main/java/chess/model/Cell.java
Normal file
25
app/src/main/java/chess/model/Cell.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package chess.model;
|
||||||
|
|
||||||
|
public class Cell {
|
||||||
|
private Piece piece = null;
|
||||||
|
|
||||||
|
public Cell(Piece piece) {
|
||||||
|
this.piece = piece;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Cell(){
|
||||||
|
this.piece = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPiece(Piece piece) {
|
||||||
|
this.piece = piece;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deletePiece() {
|
||||||
|
this.piece = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Piece getPiece() {
|
||||||
|
return piece;
|
||||||
|
}
|
||||||
|
}
|
||||||
18
app/src/main/java/chess/model/Coordinates.java
Normal file
18
app/src/main/java/chess/model/Coordinates.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package chess.model;
|
||||||
|
|
||||||
|
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 this.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getY() {
|
||||||
|
return this.y;
|
||||||
|
}
|
||||||
|
}
|
||||||
29
app/src/main/java/chess/model/Game.java
Normal file
29
app/src/main/java/chess/model/Game.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package chess.model;
|
||||||
|
|
||||||
|
public class Game {
|
||||||
|
private Board board;
|
||||||
|
|
||||||
|
public Game(int size) {
|
||||||
|
this.board = new Board(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSize(){
|
||||||
|
return board.getSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Game() {
|
||||||
|
this(8);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void jouerPartie(){
|
||||||
|
// todo
|
||||||
|
}
|
||||||
|
|
||||||
|
public Board getBoard(){
|
||||||
|
return board;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void move(Coordinates initialPosition, Coordinates finalPosition){
|
||||||
|
board.movePiece(initialPosition, finalPosition);
|
||||||
|
}
|
||||||
|
}
|
||||||
20
app/src/main/java/chess/model/Piece.java
Normal file
20
app/src/main/java/chess/model/Piece.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package chess.model;
|
||||||
|
|
||||||
|
import chess.view.render2D.Pieces;
|
||||||
|
|
||||||
|
public abstract class Piece {
|
||||||
|
public int color;
|
||||||
|
|
||||||
|
public Piece(int color) {
|
||||||
|
this.color = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getColor() {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void accept(PieceVisitor pv);
|
||||||
|
|
||||||
|
// public abstract void move();
|
||||||
|
|
||||||
|
}
|
||||||
17
app/src/main/java/chess/model/PieceVisitor.java
Normal file
17
app/src/main/java/chess/model/PieceVisitor.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package chess.model;
|
||||||
|
|
||||||
|
import chess.model.pieces.*;
|
||||||
|
|
||||||
|
public interface PieceVisitor{
|
||||||
|
default void visit(Piece p) {
|
||||||
|
p.accept(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void visitPiece(Bishop p);
|
||||||
|
void visitPiece(Knight p);
|
||||||
|
void visitPiece(Pawn p);
|
||||||
|
void visitPiece(Queen p);
|
||||||
|
void visitPiece(King p);
|
||||||
|
void visitPiece(Rook p);
|
||||||
|
|
||||||
|
}
|
||||||
15
app/src/main/java/chess/model/pieces/Bishop.java
Normal file
15
app/src/main/java/chess/model/pieces/Bishop.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package chess.model.pieces;
|
||||||
|
|
||||||
|
import chess.model.Piece;
|
||||||
|
import chess.model.PieceVisitor;
|
||||||
|
|
||||||
|
public class Bishop extends Piece {
|
||||||
|
public Bishop(final int color) {
|
||||||
|
super(color);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(PieceVisitor pv) {
|
||||||
|
pv.visitPiece(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
app/src/main/java/chess/model/pieces/King.java
Normal file
15
app/src/main/java/chess/model/pieces/King.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package chess.model.pieces;
|
||||||
|
|
||||||
|
import chess.model.Piece;
|
||||||
|
import chess.model.PieceVisitor;
|
||||||
|
|
||||||
|
public class King extends Piece {
|
||||||
|
public King(final int color) {
|
||||||
|
super(color);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(PieceVisitor pv) {
|
||||||
|
pv.visitPiece(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
app/src/main/java/chess/model/pieces/Knight.java
Normal file
15
app/src/main/java/chess/model/pieces/Knight.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package chess.model.pieces;
|
||||||
|
|
||||||
|
import chess.model.Piece;
|
||||||
|
import chess.model.PieceVisitor;
|
||||||
|
|
||||||
|
public class Knight extends Piece {
|
||||||
|
public Knight(final int color) {
|
||||||
|
super(color);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(PieceVisitor pv) {
|
||||||
|
pv.visitPiece(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
app/src/main/java/chess/model/pieces/Pawn.java
Normal file
15
app/src/main/java/chess/model/pieces/Pawn.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package chess.model.pieces;
|
||||||
|
|
||||||
|
import chess.model.Piece;
|
||||||
|
import chess.model.PieceVisitor;
|
||||||
|
|
||||||
|
public class Pawn extends Piece {
|
||||||
|
public Pawn(final int color) {
|
||||||
|
super(color);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(PieceVisitor pv) {
|
||||||
|
pv.visitPiece(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
app/src/main/java/chess/model/pieces/Queen.java
Normal file
15
app/src/main/java/chess/model/pieces/Queen.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package chess.model.pieces;
|
||||||
|
|
||||||
|
import chess.model.Piece;
|
||||||
|
import chess.model.PieceVisitor;
|
||||||
|
|
||||||
|
public class Queen extends Piece {
|
||||||
|
public Queen(final int color) {
|
||||||
|
super(color);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(PieceVisitor pv) {
|
||||||
|
pv.visitPiece(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
app/src/main/java/chess/model/pieces/Rook.java
Normal file
15
app/src/main/java/chess/model/pieces/Rook.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package chess.model.pieces;
|
||||||
|
|
||||||
|
import chess.model.Piece;
|
||||||
|
import chess.model.PieceVisitor;
|
||||||
|
|
||||||
|
public class Rook extends Piece {
|
||||||
|
public Rook(final int color) {
|
||||||
|
super(color);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(PieceVisitor pv) {
|
||||||
|
pv.visitPiece(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
61
app/src/main/java/chess/view/render2D/PieceFileName.java
Normal file
61
app/src/main/java/chess/view/render2D/PieceFileName.java
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package chess.view.render2D;
|
||||||
|
|
||||||
|
import chess.model.Piece;
|
||||||
|
import chess.model.PieceVisitor;
|
||||||
|
import chess.model.pieces.*;
|
||||||
|
|
||||||
|
public class PieceFileName implements PieceVisitor {
|
||||||
|
|
||||||
|
private String pieceName;
|
||||||
|
private static final String BASE = "app/src/main/ressources/pieces2D/";
|
||||||
|
|
||||||
|
PieceFileName(Piece piece) {
|
||||||
|
visit(piece);
|
||||||
|
pieceName = colorToString(piece.getColor()) +"-"+ pieceName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String colorToString(int color) {
|
||||||
|
switch (color) {
|
||||||
|
case 0:
|
||||||
|
return "white";
|
||||||
|
case 1:
|
||||||
|
return "black";
|
||||||
|
default:
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitPiece(Bishop p) {
|
||||||
|
this.pieceName = "bishop";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitPiece(Knight p) {
|
||||||
|
this.pieceName = "knight";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitPiece(Pawn p) {
|
||||||
|
this.pieceName = "pawn";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitPiece(Queen p) {
|
||||||
|
this.pieceName = "queen";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitPiece(King p) {
|
||||||
|
this.pieceName = "king";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitPiece(Rook p) {
|
||||||
|
this.pieceName = "rook";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFileName() {
|
||||||
|
return BASE + pieceName + ".png";
|
||||||
|
}
|
||||||
|
}
|
||||||
38
app/src/main/java/chess/view/render2D/Pieces.java
Normal file
38
app/src/main/java/chess/view/render2D/Pieces.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package chess.view.render2D;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public enum Pieces {
|
||||||
|
WHITE_KING,
|
||||||
|
WHITE_QUEEN,
|
||||||
|
WHITE_KNIGHT,
|
||||||
|
WHITE_BISHOP,
|
||||||
|
WHITE_ROOK,
|
||||||
|
WHITE_PAWN,
|
||||||
|
BLACK_KING,
|
||||||
|
BLACK_QUEEN,
|
||||||
|
BLACK_KNIGHT,
|
||||||
|
BLACK_BISHOP,
|
||||||
|
BLACK_ROOK,
|
||||||
|
BLACK_PAWN;
|
||||||
|
|
||||||
|
public static ImageIcon getIcon(String path) {
|
||||||
|
/*return switch (this) {
|
||||||
|
case WHITE_KING -> new ImageIcon(new ImageIcon(path + "white-king.png").getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||||
|
case WHITE_QUEEN -> new ImageIcon(new ImageIcon(path + "white-queen.png").getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||||
|
case WHITE_KNIGHT -> new ImageIcon(new ImageIcon(path + "white-knight.png").getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||||
|
case WHITE_BISHOP -> new ImageIcon(new ImageIcon(path + "white-bishop.png").getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||||
|
case WHITE_ROOK -> new ImageIcon(new ImageIcon(path + "white-rook.png").getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||||
|
case WHITE_PAWN -> new ImageIcon(new ImageIcon(path + "white-pawn.png").getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||||
|
case BLACK_KING -> new ImageIcon(new ImageIcon(path + "black-king.png").getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||||
|
case BLACK_QUEEN -> new ImageIcon(new ImageIcon(path + "black-queen.png").getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||||
|
case BLACK_KNIGHT -> new ImageIcon(new ImageIcon(path + "black-knight.png").getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||||
|
case BLACK_BISHOP -> new ImageIcon(new ImageIcon(path + "black-bishop.png").getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||||
|
case BLACK_ROOK -> new ImageIcon(new ImageIcon(path + "black-rook.png").getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||||
|
case BLACK_PAWN -> new ImageIcon(new ImageIcon(path + "black-pawn.png").getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||||
|
default -> null;
|
||||||
|
};*/
|
||||||
|
return new ImageIcon(new ImageIcon(path).getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package chess.view.render2D;
|
package chess.view.render2D;
|
||||||
|
|
||||||
import chess.model.Model2D;
|
import chess.model.Coordinates;
|
||||||
|
import chess.model.Game;
|
||||||
|
import chess.model.Piece;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@@ -10,27 +12,30 @@ import java.util.Observable;
|
|||||||
import java.util.Observer;
|
import java.util.Observer;
|
||||||
|
|
||||||
public class Window extends JFrame implements Observer {
|
public class Window extends JFrame implements Observer {
|
||||||
private final Model2D model;
|
private final Game game;
|
||||||
private final JLabel[][] tab = new JLabel[8][8];
|
private final JLabel[][] tab;
|
||||||
|
private static final String path = "app/src/main/ressources/pieces2D/";
|
||||||
|
Coordinates mouseClick = null;
|
||||||
|
Coordinates mouseRelease = null;
|
||||||
|
|
||||||
public Window(Model2D model) {
|
public Window(Game game) {
|
||||||
System.out.println("New window has been created.");
|
this.game = game;
|
||||||
this.model = model;
|
tab = new JLabel[game.getSize()][game.getSize()];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
new Window(new Model2D()).build();
|
new Window(new Game()).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a playing grid with alternating black & white grids
|
* Create a playing grid with alternating black & white cells
|
||||||
*/
|
*/
|
||||||
private void build(){
|
private void build(){
|
||||||
JPanel jp = new JPanel(new GridLayout(8,8));
|
JPanel jp = new JPanel(new GridLayout(game.getSize(), game.getSize()));
|
||||||
setContentPane(jp);
|
setContentPane(jp);
|
||||||
setTitle("Let's play chess!");
|
setTitle("Let's play chess!");
|
||||||
for (int y = 0; y < 8; y++){
|
for (int y = 0; y < game.getSize(); y++){
|
||||||
for (int x = 0; x < 8; x++){
|
for (int x = 0; x < game.getSize(); x++){
|
||||||
JLabel jl = new JLabel();
|
JLabel jl = new JLabel();
|
||||||
jl.setOpaque(true);
|
jl.setOpaque(true);
|
||||||
if (((y+x)%2)!=0) {
|
if (((y+x)%2)!=0) {
|
||||||
@@ -40,11 +45,22 @@ public class Window extends JFrame implements Observer {
|
|||||||
final int xx = x;
|
final int xx = x;
|
||||||
|
|
||||||
jl.addMouseListener(new MouseAdapter() {
|
jl.addMouseListener(new MouseAdapter() {
|
||||||
public void mouseClicked(MouseEvent e) {
|
public void mousePressed(MouseEvent e) {
|
||||||
System.out.println("(" + xx + ";" + yy + ")");
|
if (mouseClick == null) {
|
||||||
model.set(xx, yy);
|
System.out.println("Click on (" + xx + ";" + yy + ")");
|
||||||
|
mouseClick = new Coordinates(xx, yy);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.out.println("Release on (" + xx + ";" + yy + ")");
|
||||||
|
mouseRelease = new Coordinates(xx, yy);
|
||||||
|
if (!mouseClick.equals(mouseRelease)) {
|
||||||
|
game.move(mouseClick, mouseRelease);
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
mouseClick = null;
|
||||||
|
mouseRelease = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
this.tab[x][y]=jl;
|
this.tab[x][y]=jl;
|
||||||
jp.add(jl);
|
jp.add(jl);
|
||||||
@@ -52,15 +68,34 @@ public class Window extends JFrame implements Observer {
|
|||||||
}
|
}
|
||||||
setSize(800,800);
|
setSize(800,800);
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void showBoard(){
|
||||||
|
for (int y = 0; y < game.getSize(); y++){
|
||||||
|
for (int x = 0; x < game.getSize(); x++){
|
||||||
|
Piece p = game.getBoard().getCell(new Coordinates(x,y)).getPiece();
|
||||||
|
if (p != null){
|
||||||
|
this.tab[x][y].setIcon(Pieces.getIcon(new PieceFileName(p).getFileName()));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.tab[x][y].setIcon(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
tab[model.x][model.y].setBackground(Color.RED);
|
// todo
|
||||||
|
showBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Observable o, Object arg) {
|
public void update(Observable o, Object arg) {
|
||||||
System.out.println("ok");
|
System.out.println("Update.");
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user