contoller
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -40,28 +40,29 @@ public class Board {
|
|||||||
|
|
||||||
public void setPiecesSize8(){
|
public void setPiecesSize8(){
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
board[i][1].setPiece(new Pawn(WHITE));
|
board[i][1].setPiece(new Pawn(BLACK));
|
||||||
board[i][6].setPiece(new Pawn(BLACK));
|
board[i][6].setPiece(new Pawn(WHITE));
|
||||||
if (i==0 || i==7) {
|
if (i==0 || i==7) {
|
||||||
board[i][0].setPiece(new Rook(WHITE));
|
board[i][0].setPiece(new Rook(BLACK));
|
||||||
board[i][7].setPiece(new Rook(BLACK));
|
board[i][7].setPiece(new Rook(WHITE));
|
||||||
}
|
}
|
||||||
if (i==1 || i==6) {
|
if (i==1 || i==6) {
|
||||||
board[i][0].setPiece(new Knight(WHITE));
|
board[i][0].setPiece(new Knight(BLACK));
|
||||||
board[i][7].setPiece(new Knight(BLACK));
|
board[i][7].setPiece(new Knight(WHITE));
|
||||||
}
|
}
|
||||||
if (i==2 || i==5) {
|
if (i==2 || i==5) {
|
||||||
board[i][0].setPiece(new Bishop(WHITE));
|
board[i][0].setPiece(new Bishop(BLACK));
|
||||||
board[i][7].setPiece(new Bishop(BLACK));
|
board[i][7].setPiece(new Bishop(WHITE));
|
||||||
}
|
}
|
||||||
if (i==3) {
|
if (i==3) {
|
||||||
board[i][0].setPiece(new King(WHITE));
|
board[i][0].setPiece(new Queen(BLACK));
|
||||||
board[i][7].setPiece(new King(BLACK));
|
board[i][7].setPiece(new Queen(WHITE));
|
||||||
}
|
}
|
||||||
if (i==4) {
|
if (i==4) {
|
||||||
board[i][0].setPiece(new Queen(WHITE));
|
board[i][0].setPiece(new King(BLACK));
|
||||||
board[i][7].setPiece(new Queen(BLACK));
|
board[i][7].setPiece(new King(WHITE));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,12 +70,12 @@ public class Board {
|
|||||||
return getCell(coordinates).getPiece();
|
return getCell(coordinates).getPiece();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void movePiece(Coordinates initialPosition, Coordinates finalPosition) throws Error {
|
public void movePiece(Move move) throws Error {
|
||||||
Piece movingPiece = getPiece(initialPosition);
|
Piece movingPiece = getPiece(move.getStart());
|
||||||
Cell arrivalCell = getCell(finalPosition);
|
Cell arrivalCell = getCell(move.getEnd());
|
||||||
getCell(initialPosition).deletePiece();
|
getCell(move.getStart()).deletePiece();
|
||||||
arrivalCell.setPiece(movingPiece);
|
arrivalCell.setPiece(movingPiece);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// je fais mumuse
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,4 +15,8 @@ public class Coordinates {
|
|||||||
public int getY() {
|
public int getY() {
|
||||||
return this.y;
|
return this.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "(" + this.x + ", " + this.y + ")";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,29 +1,87 @@
|
|||||||
package chess.model;
|
package chess.model;
|
||||||
|
|
||||||
public class Game {
|
import java.util.ArrayList;
|
||||||
private Board board;
|
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) {
|
public Game(int size) {
|
||||||
this.board = new Board(size);
|
this.board = new Board(size);
|
||||||
|
this.setChanged();
|
||||||
|
this.notifyObservers();
|
||||||
|
}
|
||||||
|
public Game() {
|
||||||
|
this(8);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSize(){
|
public int getSize(){
|
||||||
return board.getSize();
|
return board.getSize();
|
||||||
}
|
}
|
||||||
|
public Board getBoard() {
|
||||||
public Game() {
|
|
||||||
this(8);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void jouerPartie(){
|
|
||||||
// todo
|
|
||||||
}
|
|
||||||
|
|
||||||
public Board getBoard(){
|
|
||||||
return board;
|
return board;
|
||||||
}
|
}
|
||||||
|
public Move getMove() {
|
||||||
|
return move;
|
||||||
|
}
|
||||||
|
|
||||||
public void move(Coordinates initialPosition, Coordinates finalPosition){
|
public void setMove(Move m){
|
||||||
board.movePiece(initialPosition, finalPosition);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -15,6 +15,6 @@ public abstract class Piece {
|
|||||||
|
|
||||||
public abstract void accept(PieceVisitor pv);
|
public abstract void accept(PieceVisitor pv);
|
||||||
|
|
||||||
// public abstract void move();
|
// public abstract void allowedMove();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ import chess.model.pieces.*;
|
|||||||
public class PieceFileName implements PieceVisitor {
|
public class PieceFileName implements PieceVisitor {
|
||||||
|
|
||||||
private String pieceName;
|
private String pieceName;
|
||||||
private static final String BASE = "app/src/main/ressources/pieces2D/";
|
private static final String BASE = "app/src/main/resources/pieces2D/";
|
||||||
|
|
||||||
PieceFileName(Piece piece) {
|
PieceFileName(Piece piece) {
|
||||||
visit(piece);
|
visit(piece);
|
||||||
|
|||||||
@@ -18,21 +18,6 @@ public enum Pieces {
|
|||||||
BLACK_PAWN;
|
BLACK_PAWN;
|
||||||
|
|
||||||
public static ImageIcon getIcon(String path) {
|
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));
|
return new ImageIcon(new ImageIcon(path).getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package chess.view.render2D;
|
package chess.view.render2D;
|
||||||
|
|
||||||
|
import chess.controller.Controller;
|
||||||
import chess.model.Coordinates;
|
import chess.model.Coordinates;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
@@ -13,18 +14,27 @@ import java.util.Observer;
|
|||||||
|
|
||||||
public class Window extends JFrame implements Observer {
|
public class Window extends JFrame implements Observer {
|
||||||
private final Game game;
|
private final Game game;
|
||||||
|
private final Controller controller;
|
||||||
private final JLabel[][] tab;
|
private final JLabel[][] tab;
|
||||||
private static final String path = "app/src/main/ressources/pieces2D/";
|
private static final String path = "app/src/main/resources/pieces2D/";
|
||||||
Coordinates mouseClick = null;
|
Coordinates mouseClick = null;
|
||||||
Coordinates mouseRelease = null;
|
Coordinates mouseRelease = null;
|
||||||
|
|
||||||
public Window(Game game) {
|
public Window(Game game) {
|
||||||
this.game = game;
|
this.game = game;
|
||||||
|
game.addObserver(this);
|
||||||
|
this.controller = new Controller(game, this);
|
||||||
tab = new JLabel[game.getSize()][game.getSize()];
|
tab = new JLabel[game.getSize()][game.getSize()];
|
||||||
|
build();
|
||||||
|
showBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
new Window(new Game()).build();
|
Game game = new Game();
|
||||||
|
Thread gameThread = new Thread(game);
|
||||||
|
gameThread.start();
|
||||||
|
Window f = new Window(game);
|
||||||
|
f.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,14 +57,12 @@ public class Window extends JFrame implements Observer {
|
|||||||
jl.addMouseListener(new MouseAdapter() {
|
jl.addMouseListener(new MouseAdapter() {
|
||||||
public void mousePressed(MouseEvent e) {
|
public void mousePressed(MouseEvent e) {
|
||||||
if (mouseClick == null) {
|
if (mouseClick == null) {
|
||||||
System.out.println("Click on (" + xx + ";" + yy + ")");
|
|
||||||
mouseClick = new Coordinates(xx, yy);
|
mouseClick = new Coordinates(xx, yy);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
System.out.println("Release on (" + xx + ";" + yy + ")");
|
|
||||||
mouseRelease = new Coordinates(xx, yy);
|
mouseRelease = new Coordinates(xx, yy);
|
||||||
if (!mouseClick.equals(mouseRelease)) {
|
if (!mouseClick.equals(mouseRelease)) {
|
||||||
game.move(mouseClick, mouseRelease);
|
controller.sendMove(mouseClick, mouseRelease);
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
mouseClick = null;
|
mouseClick = null;
|
||||||
@@ -67,11 +75,8 @@ public class Window extends JFrame implements Observer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
setSize(800,800);
|
setSize(800,800);
|
||||||
setVisible(true);
|
|
||||||
update();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void showBoard(){
|
public void showBoard(){
|
||||||
for (int y = 0; y < game.getSize(); y++){
|
for (int y = 0; y < game.getSize(); y++){
|
||||||
for (int x = 0; x < game.getSize(); x++){
|
for (int x = 0; x < game.getSize(); x++){
|
||||||
@@ -95,7 +100,6 @@ public class Window extends JFrame implements Observer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Observable o, Object arg) {
|
public void update(Observable o, Object arg) {
|
||||||
System.out.println("Update.");
|
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
|
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 2.3 KiB |
|
Before Width: | Height: | Size: 626 B After Width: | Height: | Size: 626 B |
|
Before Width: | Height: | Size: 2.4 KiB After Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 589 B After Width: | Height: | Size: 589 B |
|
Before Width: | Height: | Size: 2.7 KiB After Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 2.8 KiB After Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.9 KiB |
|
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |