7 Commits

Author SHA1 Message Date
fa3c6672e4 ignore classes 2025-04-07 10:17:27 +02:00
b920c4fbb3 change board to cells 2025-04-07 10:17:19 +02:00
Janet-Doe
f4b5e10e5b addition of decorator to manage pieces' moves 2025-03-31 12:02:23 +02:00
Janet-Doe
747bc62596 contoller 2025-03-31 11:21:27 +02:00
Janet-Doe
d60e66fd09 working piece display + basic moves 2025-03-25 16:50:31 +01:00
Janet-Doe
c8c6019b15 addo pieces 2025-03-25 10:21:50 +01:00
Janet-Doe
d720e16de0 Implementation of 2D mode 2025-03-24 11:17:32 +01:00
46 changed files with 719 additions and 17 deletions

2
.gitignore vendored
View File

@@ -3,3 +3,5 @@
# Ignore Gradle build output directory
build
app/bin

View File

@@ -17,7 +17,7 @@ repositories {
}
def lwjgl_version = "3.3.6"
def lwjgl_natives = "natives-linux"
def lwjgl_natives = "natives-windows"
dependencies {
// Use JUnit Jupiter for testing.

View File

@@ -3,7 +3,7 @@
*/
package chess;
import chess.render.*;
import chess.view.render2D.Window;
public class App {
public String getGreeting() {
@@ -11,6 +11,6 @@ public class App {
}
public static void main(String[] args) {
new Window().run();
Window.main(args);
}
}

View File

@@ -0,0 +1,29 @@
package chess.controller;
import chess.model.Coordinates;
import chess.model.Game;
import chess.model.Move;
import chess.view.render2D.Window;
public class Controller {
private final Game game;
private final Window view;
public Controller(Game game, Window view) {
this.game = game;
this.view = view;
}
/**
*
* @param from Coordinates: old coordinates
* @param to Coordinates: new coordinates
*/
public void sendMove(Coordinates from, Coordinates to) {
System.out.println("New move: " + from + " to " + to);
game.setMove(new Move(from, to));
}
}

View File

@@ -0,0 +1,17 @@
package chess.model;
import java.util.ArrayList;
public abstract class AccessibleCellsDecorator {
public AccessibleCellsDecorator base;
public ArrayList<Cell> cells;
protected abstract ArrayList<Cell> getAccessibleCells();
public AccessibleCellsDecorator(AccessibleCellsDecorator base) {
this.base = base;
}
public AccessibleCellsDecorator() {
this(null);
}
}

View File

@@ -0,0 +1,81 @@
package chess.model;
import chess.model.pieces.*;
public class Board {
private final Cell[][] cells;
private static final int WHITE = 0;
private static final int BLACK = 1;
public Board(int size) {
cells = new Cell[size][size];
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
cells[y][x] = new Cell();
}
}
setPieces();
}
public Board() {
this(8);
}
public Cell getCell(Coordinates coordinates) {
return cells[coordinates.getX()][coordinates.getY()];
}
public int getSize() {
return cells.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++) {
cells[i][1].setPiece(new Pawn(BLACK, this, cells[i][1]));
cells[i][6].setPiece(new Pawn(WHITE, this, cells[i][6]));
}
for (int i = 0; i <= 7; i += 7) {
cells[i][0].setPiece(new Rook(BLACK, this, cells[i][0]));
cells[i][7].setPiece(new Rook(WHITE, this, cells[i][7]));
}
for (int i = 1; i <= 6; i += 5) {
cells[i][0].setPiece(new Knight(BLACK, this, cells[i][0]));
cells[i][7].setPiece(new Knight(WHITE, this, cells[i][7]));
}
for (int i = 2; i <= 5; i += 3) {
cells[i][0].setPiece(new Bishop(BLACK, this, cells[i][0]));
cells[i][7].setPiece(new Bishop(WHITE, this, cells[i][7]));
}
cells[3][0].setPiece(new Queen(BLACK, this, cells[3][0]));
cells[3][7].setPiece(new Queen(WHITE, this, cells[3][7]));
cells[4][0].setPiece(new King(BLACK, this, cells[4][0]));
cells[4][7].setPiece(new King(WHITE, this, cells[4][7]));
}
public Piece getPiece(Coordinates coordinates) {
return getCell(coordinates).getPiece();
}
public void movePiece(Move move) throws Error {
Piece movingPiece = getPiece(move.getStart());
Cell arrivalCell = getCell(move.getEnd());
getCell(move.getStart()).deletePiece();
arrivalCell.setPiece(movingPiece);
movingPiece.setPosition(arrivalCell);
}
public Cell getRelativePosition(Coordinates coordinates) {
// todo
return null;
}
}

View 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;
}
}

View File

@@ -0,0 +1,22 @@
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;
}
public String toString() {
return "(" + this.x + ", " + this.y + ")";
}
}

View File

@@ -0,0 +1,87 @@
package chess.model;
import java.util.ArrayList;
import java.util.Observable;
public class Game extends Observable implements Runnable {
private final Board board;
private Move move;
private final ArrayList<Player> players = new ArrayList<>();
public Game(int size) {
this.board = new Board(size);
this.setChanged();
this.notifyObservers();
}
public Game() {
this(8);
}
public int getSize(){
return board.getSize();
}
public Board getBoard() {
return board;
}
public Move getMove() {
return move;
}
public void setMove(Move m){
this.move = m;
synchronized (this) {
this.notify();
}
}
public boolean gameDone(){
// todo
return false;
}
public Player getNextPlayer(){
return players.getFirst();
}
public void updatePlayersList(){
Player player = players.getFirst();
players.remove(player);
players.add(player);
}
public void playGame() {
players.add(new Player(this));
players.add(new Player(this));
while (!gameDone()){
Move m = getNextPlayer().getMove();
while (!applyMove(m)){
m = getNextPlayer().getMove();
}
updatePlayersList();
setChanged();
notifyObservers();
}
}
@Override
public void run(){
playGame();
}
public boolean applyMove(Move m){
if (board.getCell(m.getStart()).getPiece() == null){
return false;
}
try {
move(m);
}
catch (Exception e) {
return false;
}
return true;
}
public void move(Move move) throws Exception{
board.movePiece(move);
}
}

View File

@@ -0,0 +1,15 @@
package chess.model;
import java.util.Observable;
public class Model2D extends Observable {
public int x;
public int y;
public void set(int i, int j){
this.x = i;
this.y = j;
setChanged();
notifyObservers();
}
}

View File

@@ -0,0 +1,26 @@
package chess.model;
public class Move {
private Coordinates start;
private Coordinates end;
public Move(Coordinates start, Coordinates end) {
this.start = start;
this.end = end;
}
public Coordinates getEnd() {
return end;
}
public Coordinates getStart() {
return start;
}
public void updateMove(Coordinates start, Coordinates end) {
this.start = start;
this.end = end;
}
}

View File

@@ -0,0 +1,27 @@
package chess.model;
import chess.view.render2D.Pieces;
public abstract class Piece {
public final int color;
public AccessibleCellsDecorator decorator;
public final Board board;
public Cell position;
public Piece(int color, Board board, Cell position) {
this.color = color;
this.board = board;
this.position = position;
}
public int getColor() {
return color;
}
public void setPosition(Cell cell) {
this.position = cell;
}
public abstract void accept(PieceVisitor pv);
}

View 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);
}

View File

@@ -0,0 +1,21 @@
package chess.model;
public class Player {
private final Game game;
public Player(Game game) {
this.game = game;
}
public Move getMove(){
synchronized(game){
try {
game.wait();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
return game.getMove();
}
}

View File

@@ -0,0 +1,21 @@
package chess.model.decorators;
import chess.model.AccessibleCellsDecorator;
import chess.model.Cell;
import java.util.ArrayList;
public class DiagonalDecorator extends AccessibleCellsDecorator {
public DiagonalDecorator(AccessibleCellsDecorator base) {
super(base);
}
public DiagonalDecorator(){
super();
}
@Override
protected ArrayList<Cell> getAccessibleCells() {
return null;
}
}

View File

@@ -0,0 +1,22 @@
package chess.model.decorators;
import chess.model.AccessibleCellsDecorator;
import chess.model.Cell;
import java.util.ArrayList;
public class LineDecorator extends AccessibleCellsDecorator {
public LineDecorator(AccessibleCellsDecorator base) {
super(base);
}
public LineDecorator(){
super();
}
@Override
protected ArrayList<Cell> getAccessibleCells() {
return null;
}
}

View File

@@ -0,0 +1,17 @@
package chess.model.pieces;
import chess.model.Board;
import chess.model.Cell;
import chess.model.Piece;
import chess.model.PieceVisitor;
public class Bishop extends Piece {
public Bishop(final int color, Board board, Cell cell) {
super(color, board, cell);
}
@Override
public void accept(PieceVisitor pv) {
pv.visitPiece(this);
}
}

View File

@@ -0,0 +1,17 @@
package chess.model.pieces;
import chess.model.Board;
import chess.model.Cell;
import chess.model.Piece;
import chess.model.PieceVisitor;
public class King extends Piece {
public King(final int color, Board board, Cell cell) {
super(color, board, cell);
}
@Override
public void accept(PieceVisitor pv) {
pv.visitPiece(this);
}
}

View File

@@ -0,0 +1,17 @@
package chess.model.pieces;
import chess.model.Board;
import chess.model.Cell;
import chess.model.Piece;
import chess.model.PieceVisitor;
public class Knight extends Piece {
public Knight(final int color, Board board, Cell cell) {
super(color, board, cell);
}
@Override
public void accept(PieceVisitor pv) {
pv.visitPiece(this);
}
}

View File

@@ -0,0 +1,17 @@
package chess.model.pieces;
import chess.model.Board;
import chess.model.Cell;
import chess.model.Piece;
import chess.model.PieceVisitor;
public class Pawn extends Piece {
public Pawn(final int color, Board board, Cell cell) {
super(color, board, cell);
}
@Override
public void accept(PieceVisitor pv) {
pv.visitPiece(this);
}
}

View File

@@ -0,0 +1,16 @@
package chess.model.pieces;
import chess.model.*;
import chess.model.decorators.DiagonalDecorator;
import chess.model.decorators.LineDecorator;
public class Queen extends Piece {
public Queen(final int color, Board board, Cell cell) {
super(color, board, cell);
}
@Override
public void accept(PieceVisitor pv) {
pv.visitPiece(this);
}
}

View File

@@ -0,0 +1,21 @@
package chess.model.pieces;
import chess.model.Board;
import chess.model.Cell;
import chess.model.Piece;
import chess.model.PieceVisitor;
import chess.model.decorators.DiagonalDecorator;
import chess.model.decorators.LineDecorator;
public class Rook extends Piece {
public Rook(final int color, Board board, Cell cell) {
super(color, board, cell);
this.decorator = new LineDecorator();
// todo
}
@Override
public void accept(PieceVisitor pv) {
pv.visitPiece(this);
}
}

View 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/resources/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";
}
}

View File

@@ -0,0 +1,23 @@
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 new ImageIcon(new ImageIcon(path).getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
}
}

View File

@@ -0,0 +1,105 @@
package chess.view.render2D;
import chess.controller.Controller;
import chess.model.Coordinates;
import chess.model.Game;
import chess.model.Piece;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Observable;
import java.util.Observer;
public class Window extends JFrame implements Observer {
private final Game game;
private final Controller controller;
private final JLabel[][] tab;
private static final String path = "app/src/main/resources/pieces2D/";
Coordinates mouseClick = null;
Coordinates mouseRelease = null;
public Window(Game game) {
this.game = game;
game.addObserver(this);
this.controller = new Controller(game, this);
tab = new JLabel[game.getSize()][game.getSize()];
build();
showBoard();
}
public static void main(String[] args){
Game game = new Game();
Thread gameThread = new Thread(game);
gameThread.start();
Window f = new Window(game);
f.setVisible(true);
}
/**
* Create a playing grid with alternating black & white cells
*/
private void build(){
JPanel jp = new JPanel(new GridLayout(game.getSize(), game.getSize()));
setContentPane(jp);
setTitle("Let's play chess!");
for (int y = 0; y < game.getSize(); y++){
for (int x = 0; x < game.getSize(); x++){
JLabel jl = new JLabel();
jl.setOpaque(true);
if (((y+x)%2)!=0) {
jl.setBackground(Color.BLACK);
}
final int yy = y;
final int xx = x;
jl.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (mouseClick == null) {
mouseClick = new Coordinates(xx, yy);
}
else {
mouseRelease = new Coordinates(xx, yy);
if (!mouseClick.equals(mouseRelease)) {
controller.sendMove(mouseClick, mouseRelease);
update();
}
mouseClick = null;
mouseRelease = null;
}
}
});
this.tab[x][y]=jl;
jp.add(jl);
}
}
setSize(800,800);
}
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() {
// todo
showBoard();
}
@Override
public void update(Observable o, Object arg) {
update();
}
}

View File

@@ -1,4 +1,4 @@
package chess.render;
package chess.view.render3D;
import org.joml.Matrix4f;
import org.joml.Vector3f;

View File

@@ -1,4 +1,4 @@
package chess.render;
package chess.view.render3D;
import org.lwjgl.opengl.GL30;

View File

@@ -1,10 +1,10 @@
package chess.render;
package chess.view.render3D;
import org.joml.Vector3f;
import org.lwjgl.opengl.*;
import static org.lwjgl.opengl.GL30.*;
import chess.render.shader.BoardShader;
import chess.view.render3D.shader.BoardShader;
public class Renderer {
private BoardShader shader;

View File

@@ -1,4 +1,4 @@
package chess.render;
package chess.view.render3D;
import java.util.ArrayList;
import java.util.List;

View File

@@ -1,4 +1,4 @@
package chess.render;
package chess.view.render3D;
public class VertexAttribPointer {
public int index;

View File

@@ -1,8 +1,7 @@
package chess.render;
package chess.view.render3D;
import static org.lwjgl.opengl.GL11.GL_FLOAT;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;

View File

@@ -1,13 +1,10 @@
package chess.render;
package chess.view.render3D;
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;
import chess.render.Camera;
import chess.render.Renderer;
import java.nio.*;
import static org.lwjgl.glfw.Callbacks.*;

View File

@@ -1,4 +1,4 @@
package chess.render.shader;
package chess.view.render3D.shader;
import org.joml.Matrix4f;

View File

@@ -1,4 +1,4 @@
package chess.render.shader;
package chess.view.render3D.shader;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 626 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 589 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB