Compare commits
7 Commits
failed
...
decorateur
| Author | SHA1 | Date | |
|---|---|---|---|
| fa3c6672e4 | |||
| b920c4fbb3 | |||
|
|
f4b5e10e5b | ||
|
|
747bc62596 | ||
|
|
d60e66fd09 | ||
|
|
c8c6019b15 | ||
|
|
d720e16de0 |
2
.gitignore
vendored
@@ -3,3 +3,5 @@
|
|||||||
|
|
||||||
# Ignore Gradle build output directory
|
# Ignore Gradle build output directory
|
||||||
build
|
build
|
||||||
|
|
||||||
|
app/bin
|
||||||
@@ -17,7 +17,7 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
def lwjgl_version = "3.3.6"
|
def lwjgl_version = "3.3.6"
|
||||||
def lwjgl_natives = "natives-linux"
|
def lwjgl_natives = "natives-windows"
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
// Use JUnit Jupiter for testing.
|
// Use JUnit Jupiter for testing.
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
import chess.render.*;
|
import chess.view.render2D.Window;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
public String getGreeting() {
|
public String getGreeting() {
|
||||||
@@ -11,6 +11,6 @@ public class App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new Window().run();
|
Window.main(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
29
app/src/main/java/chess/controller/Controller.java
Normal 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
17
app/src/main/java/chess/model/AccessibleCellsDecorator.java
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
81
app/src/main/java/chess/model/Board.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
22
app/src/main/java/chess/model/Coordinates.java
Normal 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 + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
87
app/src/main/java/chess/model/Game.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
app/src/main/java/chess/model/Model2D.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
26
app/src/main/java/chess/model/Move.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
27
app/src/main/java/chess/model/Piece.java
Normal 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);
|
||||||
|
|
||||||
|
}
|
||||||
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);
|
||||||
|
|
||||||
|
}
|
||||||
21
app/src/main/java/chess/model/Player.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
22
app/src/main/java/chess/model/decorators/LineDecorator.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
17
app/src/main/java/chess/model/pieces/Bishop.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
17
app/src/main/java/chess/model/pieces/King.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
17
app/src/main/java/chess/model/pieces/Knight.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
17
app/src/main/java/chess/model/pieces/Pawn.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
16
app/src/main/java/chess/model/pieces/Queen.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
21
app/src/main/java/chess/model/pieces/Rook.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
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/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";
|
||||||
|
}
|
||||||
|
}
|
||||||
23
app/src/main/java/chess/view/render2D/Pieces.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
105
app/src/main/java/chess/view/render2D/Window.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package chess.render;
|
package chess.view.render3D;
|
||||||
|
|
||||||
import org.joml.Matrix4f;
|
import org.joml.Matrix4f;
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package chess.render;
|
package chess.view.render3D;
|
||||||
|
|
||||||
import org.lwjgl.opengl.GL30;
|
import org.lwjgl.opengl.GL30;
|
||||||
|
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
package chess.render;
|
package chess.view.render3D;
|
||||||
|
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
import org.lwjgl.opengl.*;
|
import org.lwjgl.opengl.*;
|
||||||
|
|
||||||
import static org.lwjgl.opengl.GL30.*;
|
import static org.lwjgl.opengl.GL30.*;
|
||||||
import chess.render.shader.BoardShader;
|
import chess.view.render3D.shader.BoardShader;
|
||||||
|
|
||||||
public class Renderer {
|
public class Renderer {
|
||||||
private BoardShader shader;
|
private BoardShader shader;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package chess.render;
|
package chess.view.render3D;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package chess.render;
|
package chess.view.render3D;
|
||||||
|
|
||||||
public class VertexAttribPointer {
|
public class VertexAttribPointer {
|
||||||
public int index;
|
public int index;
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
package chess.render;
|
package chess.view.render3D;
|
||||||
|
|
||||||
import static org.lwjgl.opengl.GL11.GL_FLOAT;
|
import static org.lwjgl.opengl.GL11.GL_FLOAT;
|
||||||
|
|
||||||
import java.nio.FloatBuffer;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -1,13 +1,10 @@
|
|||||||
package chess.render;
|
package chess.view.render3D;
|
||||||
|
|
||||||
import org.lwjgl.*;
|
import org.lwjgl.*;
|
||||||
import org.lwjgl.glfw.*;
|
import org.lwjgl.glfw.*;
|
||||||
import org.lwjgl.opengl.*;
|
import org.lwjgl.opengl.*;
|
||||||
import org.lwjgl.system.*;
|
import org.lwjgl.system.*;
|
||||||
|
|
||||||
import chess.render.Camera;
|
|
||||||
import chess.render.Renderer;
|
|
||||||
|
|
||||||
import java.nio.*;
|
import java.nio.*;
|
||||||
|
|
||||||
import static org.lwjgl.glfw.Callbacks.*;
|
import static org.lwjgl.glfw.Callbacks.*;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package chess.render.shader;
|
package chess.view.render3D.shader;
|
||||||
|
|
||||||
import org.joml.Matrix4f;
|
import org.joml.Matrix4f;
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package chess.render.shader;
|
package chess.view.render3D.shader;
|
||||||
|
|
||||||
import java.nio.FloatBuffer;
|
import java.nio.FloatBuffer;
|
||||||
import java.nio.IntBuffer;
|
import java.nio.IntBuffer;
|
||||||
BIN
app/src/main/resources/pieces2D/black-bishop.png
Normal file
|
After Width: | Height: | Size: 1.2 KiB |
BIN
app/src/main/resources/pieces2D/black-king.png
Normal file
|
After Width: | Height: | Size: 3.3 KiB |
BIN
app/src/main/resources/pieces2D/black-knight.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
BIN
app/src/main/resources/pieces2D/black-pawn.png
Normal file
|
After Width: | Height: | Size: 626 B |
BIN
app/src/main/resources/pieces2D/black-queen.png
Normal file
|
After Width: | Height: | Size: 2.4 KiB |
BIN
app/src/main/resources/pieces2D/black-rook.png
Normal file
|
After Width: | Height: | Size: 589 B |
BIN
app/src/main/resources/pieces2D/white-bishop.png
Normal file
|
After Width: | Height: | Size: 2.7 KiB |
BIN
app/src/main/resources/pieces2D/white-king.png
Normal file
|
After Width: | Height: | Size: 3.1 KiB |
BIN
app/src/main/resources/pieces2D/white-knight.png
Normal file
|
After Width: | Height: | Size: 2.8 KiB |
BIN
app/src/main/resources/pieces2D/white-pawn.png
Normal file
|
After Width: | Height: | Size: 1.9 KiB |
BIN
app/src/main/resources/pieces2D/white-queen.png
Normal file
|
After Width: | Height: | Size: 4.2 KiB |
BIN
app/src/main/resources/pieces2D/white-rook.png
Normal file
|
After Width: | Height: | Size: 1.3 KiB |