Compare commits
4 Commits
e5e91351dd
...
af871e6b72
| Author | SHA1 | Date |
|---|---|---|
|
|
af871e6b72 | |
|
|
f89edacf08 | |
|
|
1ed4de95f5 | |
|
|
fe0bcf52c6 |
|
|
@ -1,4 +1,3 @@
|
||||||
<<<<<<< HEAD
|
|
||||||
import backend.Board;
|
import backend.Board;
|
||||||
import backend.Move;
|
import backend.Move;
|
||||||
import backend.Piece;
|
import backend.Piece;
|
||||||
|
|
@ -21,27 +20,3 @@ public class Main {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
import backend.Board;
|
|
||||||
import backend.Move;
|
|
||||||
import backend.Piece;
|
|
||||||
import backend.PieceType;
|
|
||||||
import windowInterface.MyInterface;
|
|
||||||
|
|
||||||
|
|
||||||
public class Main {
|
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
// testing :
|
|
||||||
Board testBoard = new Board(8, 8);
|
|
||||||
testBoard.populateBoard();
|
|
||||||
System.out.println(testBoard.toString());
|
|
||||||
|
|
||||||
// launches graphical interface :
|
|
||||||
MyInterface mjf = new MyInterface();
|
|
||||||
mjf.setVisible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
>>>>>>> branch 'master' of https://gitarero.ecam.fr/piranut.phlang/OOP_2B5_Project.git
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
<<<<<<< HEAD
|
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
public class AutoPlayer {
|
public class AutoPlayer {
|
||||||
|
|
@ -16,22 +15,3 @@ public class AutoPlayer {
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
package backend;
|
|
||||||
|
|
||||||
public class AutoPlayer {
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the best Move to try on provided board for active player
|
|
||||||
* @param board
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public Move computeBestMove(Board board) {
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
>>>>>>> branch 'master' of https://gitarero.ecam.fr/piranut.phlang/OOP_2B5_Project.git
|
|
||||||
|
|
|
||||||
|
|
@ -1,147 +1,4 @@
|
||||||
<<<<<<< HEAD
|
|
||||||
package backend;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class Board {
|
|
||||||
private int width;
|
|
||||||
private int height;
|
|
||||||
private Piece[][] boardMatrix; // 2D board
|
|
||||||
private ArrayList<Piece> pieceList;
|
|
||||||
private boolean isWhiteTurn;
|
|
||||||
private int TurnNumber;
|
|
||||||
|
|
||||||
|
|
||||||
public Board(int colNum, int lineNum) {
|
|
||||||
this.width = colNum;
|
|
||||||
this.height = lineNum;
|
|
||||||
this.boardMatrix = new Piece[width][height];
|
|
||||||
this.pieceList = new ArrayList<>();
|
|
||||||
this.isWhiteTurn = true;
|
|
||||||
this.TurnNumber = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getWidth() {
|
|
||||||
return width;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getHeight() {
|
|
||||||
return height;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getTurnNumber() {
|
|
||||||
return TurnNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isTurnWhite() {
|
|
||||||
return isWhiteTurn;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
|
||||||
Piece piece = new Piece(isWhite, type, x, y);
|
|
||||||
boardMatrix[x][y] = piece;
|
|
||||||
pieceList.add(piece);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void populateBoard() {
|
|
||||||
// Pawns
|
|
||||||
for (int i = 0; i < width; i++) {
|
|
||||||
setPiece(true, PieceType.Pawn, i, 1);
|
|
||||||
setPiece(false, PieceType.Pawn, i, 6);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rooks
|
|
||||||
setPiece(true, PieceType.Rook, 0, 0);
|
|
||||||
setPiece(true, PieceType.Rook, 7, 0);
|
|
||||||
setPiece(false, PieceType.Rook, 0, 7);
|
|
||||||
setPiece(false, PieceType.Rook, 7, 7);
|
|
||||||
|
|
||||||
// Knights
|
|
||||||
setPiece(true, PieceType.Knight, 1, 0);
|
|
||||||
setPiece(true, PieceType.Knight, 6, 0);
|
|
||||||
setPiece(false, PieceType.Knight, 1, 7);
|
|
||||||
setPiece(false, PieceType.Knight, 6, 7);
|
|
||||||
|
|
||||||
// Bishops
|
|
||||||
setPiece(true, PieceType.Bishop, 2, 0);
|
|
||||||
setPiece(true, PieceType.Bishop, 5, 0);
|
|
||||||
setPiece(false, PieceType.Bishop, 2, 7);
|
|
||||||
setPiece(false, PieceType.Bishop, 5, 7);
|
|
||||||
|
|
||||||
// Queens
|
|
||||||
setPiece(true, PieceType.Queen, 3, 0);
|
|
||||||
setPiece(false, PieceType.Queen, 3, 7);
|
|
||||||
|
|
||||||
// Kings
|
|
||||||
setPiece(true, PieceType.King, 4, 0);
|
|
||||||
setPiece(false, PieceType.King, 4, 7);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void cleanBoard() {
|
|
||||||
pieceList.clear();
|
|
||||||
for (int x = 0; x < width; x++) {
|
|
||||||
for (int y = 0; y < height; y++) {
|
|
||||||
boardMatrix[x][y] = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
|
||||||
//TODO
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<Piece> getPieces() {
|
|
||||||
return pieceList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void userTouch(int x, int y) {
|
|
||||||
//TODO
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isSelected(int x, int y) {
|
|
||||||
//TODO
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* saving-loading feature :*/
|
|
||||||
|
|
||||||
public String[] toFileRep() {
|
|
||||||
//TODO
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Board(String[] array) {
|
|
||||||
//TODO
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The following methods require more work ! */
|
|
||||||
|
|
||||||
public boolean isHighlighted(int x, int y) {
|
|
||||||
//TODO
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void undoLastMove() {
|
|
||||||
//TODO
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public Board(Board board) {
|
|
||||||
//TODO
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void playMove(Move move) {
|
|
||||||
//TODO
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
=======
|
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
@ -149,10 +6,12 @@ import java.util.ArrayList;
|
||||||
public class Board {
|
public class Board {
|
||||||
private int width;
|
private int width;
|
||||||
private int height;
|
private int height;
|
||||||
|
private ArrayList<Piece> pieces;
|
||||||
|
|
||||||
public Board(int colNum, int lineNum) {
|
public Board(int colNum, int lineNum) {
|
||||||
this.width = colNum;
|
this.width = colNum;
|
||||||
this.height = lineNum;
|
this.height = lineNum;
|
||||||
|
this.pieces = new ArrayList<>();
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -177,20 +36,77 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||||
|
Piece piece = new Piece(isWhite, type, x, y);
|
||||||
|
pieces.add(piece);
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
public void populateBoard() {
|
public void populateBoard() {
|
||||||
|
cleanBoard(); // clear any existing pieces
|
||||||
|
|
||||||
|
// White pieces
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
setPiece(true, PieceType.Pawn, i, 1);
|
||||||
|
}
|
||||||
|
setPiece(true, PieceType.Rook, 0, 0);
|
||||||
|
setPiece(true, PieceType.Knight, 1, 0);
|
||||||
|
setPiece(true, PieceType.Bishop, 2, 0);
|
||||||
|
setPiece(true, PieceType.Queen, 3, 0);
|
||||||
|
setPiece(true, PieceType.King, 4, 0);
|
||||||
|
setPiece(true, PieceType.Bishop, 5, 0);
|
||||||
|
setPiece(true, PieceType.Knight, 6, 0);
|
||||||
|
setPiece(true, PieceType.Rook, 7, 0);
|
||||||
|
|
||||||
|
// Black pieces
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
setPiece(false, PieceType.Pawn, i, 6);
|
||||||
|
}
|
||||||
|
setPiece(false, PieceType.Rook, 0, 7);
|
||||||
|
setPiece(false, PieceType.Knight, 1, 7);
|
||||||
|
setPiece(false, PieceType.Bishop, 2, 7);
|
||||||
|
setPiece(false, PieceType.Queen, 3, 7);
|
||||||
|
setPiece(false, PieceType.King, 4, 7);
|
||||||
|
setPiece(false, PieceType.Bishop, 5, 7);
|
||||||
|
setPiece(false, PieceType.Knight, 6, 7);
|
||||||
|
setPiece(false, PieceType.Rook, 7, 7);
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cleanBoard() {
|
public void cleanBoard() {
|
||||||
|
pieces.clear();
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
String[][] grid = new String[height][width];
|
||||||
|
|
||||||
|
// Fill grid with dots (empty)
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
grid[y][x] = ".";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Place each piece in its position
|
||||||
|
for (Piece p : pieces) {
|
||||||
|
String symbol = p.getType().toString().substring(0, 1); // e.g., "P" for Pawn
|
||||||
|
if (!p.isWhite()) {
|
||||||
|
symbol = symbol.toLowerCase(); // lowercase for black
|
||||||
|
}
|
||||||
|
grid[p.getY()][p.getX()] = symbol;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build the string representation
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
sb.append(grid[y][x]).append(" ");
|
||||||
|
}
|
||||||
|
sb.append("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
//TODO
|
//TODO
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Piece> getPieces() {
|
public ArrayList<Piece> getPieces() {
|
||||||
|
|
@ -245,4 +161,3 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
>>>>>>> branch 'master' of https://gitarero.ecam.fr/piranut.phlang/OOP_2B5_Project.git
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
<<<<<<< HEAD
|
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
import windowInterface.MyInterface;
|
import windowInterface.MyInterface;
|
||||||
|
|
@ -109,115 +108,3 @@ public class Game extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
package backend;
|
|
||||||
|
|
||||||
import windowInterface.MyInterface;
|
|
||||||
|
|
||||||
public class Game extends Thread {
|
|
||||||
|
|
||||||
private AutoPlayer aiPlayer;
|
|
||||||
private Board board;
|
|
||||||
|
|
||||||
private MyInterface mjf;
|
|
||||||
private int COL_NUM = 8;
|
|
||||||
private int LINE_NUM = 8;
|
|
||||||
private int loopDelay = 250;
|
|
||||||
boolean[] activationAIFlags;
|
|
||||||
|
|
||||||
public Game(MyInterface mjfParam) {
|
|
||||||
mjf = mjfParam;
|
|
||||||
board = new Board(COL_NUM, LINE_NUM);
|
|
||||||
loopDelay = 250;
|
|
||||||
LINE_NUM = 8;
|
|
||||||
COL_NUM = 8;
|
|
||||||
activationAIFlags = new boolean[2];
|
|
||||||
aiPlayer = new AutoPlayer();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getWidth() {
|
|
||||||
return board.getWidth();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getHeight() {
|
|
||||||
return board.getHeight();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void run() {
|
|
||||||
while(true) {
|
|
||||||
aiPlayerTurn();
|
|
||||||
mjf.update(board.getTurnNumber(), board.isTurnWhite());
|
|
||||||
try {
|
|
||||||
Thread.sleep(loopDelay);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isAITurn() {
|
|
||||||
return activationAIFlags[board.isTurnWhite()?1:0];
|
|
||||||
}
|
|
||||||
|
|
||||||
private void aiPlayerTurn() {
|
|
||||||
if(isAITurn()) {
|
|
||||||
board.playMove(aiPlayer.computeBestMove(new Board(board)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickCoords(int x, int y) {
|
|
||||||
int width = this.getWidth();
|
|
||||||
int height = this.getHeight();
|
|
||||||
if(0>x || 0>y || x>width || y>height) {
|
|
||||||
System.out.println("Click out of bounds");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(!isAITurn()) {
|
|
||||||
board.userTouch(x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
|
||||||
board.setPiece(isWhite, type, x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String[] getFileRepresentation() {
|
|
||||||
return board.toFileRep();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLoopDelay(int delay) {
|
|
||||||
this.loopDelay = delay;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDefaultSetup() {
|
|
||||||
board.cleanBoard();
|
|
||||||
board.populateBoard();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBoard(String[] array) {
|
|
||||||
board = new Board(array);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Iterable<Piece> getPieces() {
|
|
||||||
return board.getPieces();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isSelected(int x, int y) {
|
|
||||||
return board.isSelected(x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isHighlighted(int x, int y) {
|
|
||||||
return board.isHighlighted(x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void undoLastMove() {
|
|
||||||
board.undoLastMove();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void toggleAI(boolean isWhite) {
|
|
||||||
this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0];
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
>>>>>>> branch 'master' of https://gitarero.ecam.fr/piranut.phlang/OOP_2B5_Project.git
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,25 @@
|
||||||
<<<<<<< HEAD
|
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
public class Move {
|
public class Move {
|
||||||
|
private Piece pieceMoved;
|
||||||
|
private int fromX, fromY;
|
||||||
|
private int toX, toY;
|
||||||
|
private Piece pieceCaptured; // can be null
|
||||||
|
|
||||||
|
public Move(Piece pieceMoved, int fromX, int fromY, int toX, int toY, Piece pieceCaptured) {
|
||||||
|
this.pieceMoved = pieceMoved;
|
||||||
|
this.fromX = fromX;
|
||||||
|
this.fromY = fromY;
|
||||||
|
this.toX = toX;
|
||||||
|
this.toY = toY;
|
||||||
|
this.pieceCaptured = pieceCaptured;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Piece getPieceMoved() { return pieceMoved; }
|
||||||
|
public int getFromX() { return fromX; }
|
||||||
|
public int getFromY() { return fromY; }
|
||||||
|
public int getToX() { return toX; }
|
||||||
|
public int getToY() { return toY; }
|
||||||
|
public Piece getPieceCaptured() { return pieceCaptured; }
|
||||||
|
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
package backend;
|
|
||||||
|
|
||||||
public class Move {
|
|
||||||
|
|
||||||
}
|
|
||||||
>>>>>>> branch 'master' of https://gitarero.ecam.fr/piranut.phlang/OOP_2B5_Project.git
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
<<<<<<< HEAD
|
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
public class Piece {
|
public class Piece {
|
||||||
|
|
@ -33,26 +32,3 @@ public class Piece {
|
||||||
this.y=y;
|
this.y=y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
package backend;
|
|
||||||
|
|
||||||
public class Piece {
|
|
||||||
|
|
||||||
public int getX() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getY() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PieceType getType() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isWhite() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
>>>>>>> branch 'master' of https://gitarero.ecam.fr/piranut.phlang/OOP_2B5_Project.git
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
<<<<<<< HEAD
|
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
public enum PieceType {
|
public enum PieceType {
|
||||||
|
|
@ -27,33 +27,3 @@ public enum PieceType {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
package backend;
|
|
||||||
|
|
||||||
public enum PieceType {
|
|
||||||
Pawn, Rook, Knight, Bishop, Queen, King;
|
|
||||||
|
|
||||||
public String getSummary() {
|
|
||||||
if(this == PieceType.Knight) {
|
|
||||||
return "N";
|
|
||||||
}
|
|
||||||
return this.name().substring(0, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static PieceType fromSummary(char c) {
|
|
||||||
if(c=='P') {
|
|
||||||
return PieceType.Pawn;
|
|
||||||
}else if(c=='N') {
|
|
||||||
return PieceType.Knight;
|
|
||||||
}else if(c=='B') {
|
|
||||||
return PieceType.Bishop;
|
|
||||||
}else if(c=='R') {
|
|
||||||
return PieceType.Rook;
|
|
||||||
}else if(c=='K') {
|
|
||||||
return PieceType.King;
|
|
||||||
}
|
|
||||||
return PieceType.Queen;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
>>>>>>> branch 'master' of https://gitarero.ecam.fr/piranut.phlang/OOP_2B5_Project.git
|
|
||||||
|
|
|
||||||
|
|
@ -1,201 +1,3 @@
|
||||||
<<<<<<< HEAD
|
|
||||||
package windowInterface;
|
|
||||||
|
|
||||||
import java.awt.Color;
|
|
||||||
import java.awt.Graphics;
|
|
||||||
import java.awt.Image;
|
|
||||||
import java.awt.event.MouseAdapter;
|
|
||||||
import java.awt.event.MouseEvent;
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import javax.swing.JPanel;
|
|
||||||
|
|
||||||
import backend.Game;
|
|
||||||
import backend.Piece;
|
|
||||||
import backend.PieceType;
|
|
||||||
|
|
||||||
public class JPanelChessBoard extends JPanel {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
private Game myGame;
|
|
||||||
private MyInterface interfaceGlobal;
|
|
||||||
private BufferedImage spriteSheet;
|
|
||||||
private int PIECE_WIDTH = 16; //in spritesheet
|
|
||||||
private int PIECE_HEIGHT = 16; //in spritesheet
|
|
||||||
private int MARGIN = 0;
|
|
||||||
|
|
||||||
private boolean pieceSelectorMode;
|
|
||||||
private boolean selectedPieceIsWhite;
|
|
||||||
private PieceType selectedPieceType;
|
|
||||||
private boolean pieceAdderMode;
|
|
||||||
|
|
||||||
public JPanelChessBoard(MyInterface itf) {
|
|
||||||
super();
|
|
||||||
myGame = null;
|
|
||||||
interfaceGlobal = itf;
|
|
||||||
selectedPieceIsWhite = true;
|
|
||||||
selectedPieceType = PieceType.Pawn;
|
|
||||||
pieceSelectorMode = false;
|
|
||||||
try {
|
|
||||||
spriteSheet = ImageIO.read(getClass().getResourceAsStream("/pieces.png"));
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
pieceSelectorMode = false;
|
|
||||||
pieceAdderMode = false;
|
|
||||||
addMouseListener(new MouseAdapter() {
|
|
||||||
public void mousePressed(MouseEvent me) {
|
|
||||||
// System.out.println(me);
|
|
||||||
if(pieceSelectorMode) {
|
|
||||||
int x = Math.round(me.getX()/cellWidth());
|
|
||||||
selectedPieceType = PieceType.values()[5-x];
|
|
||||||
selectedPieceIsWhite = (me.getY() > cellHeight());
|
|
||||||
pieceSelectorMode = false;
|
|
||||||
} else {
|
|
||||||
if(myGame == null) {
|
|
||||||
interfaceGlobal.instantiateSimu();
|
|
||||||
}
|
|
||||||
int x = (me.getX()*myGame.getWidth())/getWidth();
|
|
||||||
int y = (me.getY()*myGame.getHeight())/getHeight();
|
|
||||||
if(pieceAdderMode) {
|
|
||||||
//TODO
|
|
||||||
myGame.setPiece(selectedPieceIsWhite,selectedPieceType, x, y);
|
|
||||||
pieceAdderMode = false;
|
|
||||||
} else {
|
|
||||||
myGame.clickCoords(x,y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
repaint();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void setGame(Game simu) {
|
|
||||||
myGame = simu;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void paintComponent(Graphics g) {
|
|
||||||
super.paintComponent(g);
|
|
||||||
this.setBackground(Color.black);
|
|
||||||
if(pieceSelectorMode) {
|
|
||||||
g.drawImage(
|
|
||||||
spriteSheet,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
Math.round(5*cellWidth()),
|
|
||||||
Math.round(2*cellHeight()),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (myGame != null) {
|
|
||||||
// Draw Interface from state of simulator
|
|
||||||
float cellWidth = cellWidth();
|
|
||||||
float cellHeight = cellHeight();
|
|
||||||
|
|
||||||
g.setColor(Color.white);
|
|
||||||
for(int x=0; x<myGame.getWidth();x++) {
|
|
||||||
for (int y=0; y<myGame.getHeight(); y++) {
|
|
||||||
boolean isSelect = myGame.isSelected(x,y);
|
|
||||||
boolean isHighlight = myGame.isHighlighted(x,y);
|
|
||||||
if(isSelect) {
|
|
||||||
g.setColor(Color.blue);
|
|
||||||
}
|
|
||||||
if(isHighlight) {
|
|
||||||
g.setColor(Color.yellow);
|
|
||||||
}
|
|
||||||
if((x+y)%2==1 || isSelect || isHighlight) {
|
|
||||||
g.fillRect(
|
|
||||||
Math.round(x*cellWidth),
|
|
||||||
Math.round(y*cellHeight),
|
|
||||||
Math.round(cellWidth),
|
|
||||||
Math.round(cellHeight)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if(isHighlight || isSelect) {
|
|
||||||
g.setColor(Color.white);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
g.setColor(Color.gray);
|
|
||||||
for(int x=0; x<myGame.getWidth();x++) {
|
|
||||||
int graphX = Math.round(x*cellWidth);
|
|
||||||
g.drawLine(graphX, 0, graphX, this.getHeight());
|
|
||||||
}
|
|
||||||
for (int y=0; y<myGame.getHeight(); y++) {
|
|
||||||
int graphY = Math.round(y*cellHeight);
|
|
||||||
g.drawLine(0, graphY, this.getWidth(), graphY);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Piece piece : myGame.getPieces()) {
|
|
||||||
drawPiece(g,piece);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void drawPiece(Graphics g, Piece piece) {
|
|
||||||
g.drawImage(
|
|
||||||
getChessPieceImageFromType(piece.getType(), piece.isWhite()),
|
|
||||||
MARGIN+(xCoordFromGame(piece.getX())),
|
|
||||||
MARGIN+(yCoordFromGame(piece.getY())),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private Image getChessPieceImageFromType(PieceType type, boolean isWhite) {
|
|
||||||
int x = spriteSheetPositionOfPieceType(type)*PIECE_WIDTH;
|
|
||||||
int y = PIECE_HEIGHT * (isWhite?1:0);
|
|
||||||
Image subImage = spriteSheet.getSubimage(x, y, PIECE_WIDTH, PIECE_HEIGHT);
|
|
||||||
return subImage.getScaledInstance(
|
|
||||||
Math.round(cellWidth())-2*MARGIN,
|
|
||||||
Math.round(cellHeight())-2*MARGIN, 0
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private int spriteSheetPositionOfPieceType(PieceType type) {
|
|
||||||
return 5-type.ordinal();
|
|
||||||
}
|
|
||||||
|
|
||||||
private float cellWidth() {
|
|
||||||
return (float) this.getWidth()/ (float)myGame.getWidth();
|
|
||||||
}
|
|
||||||
private float cellHeight() {
|
|
||||||
return (float)this.getHeight()/ (float)myGame.getHeight();
|
|
||||||
}
|
|
||||||
private int xCoordFromGame(int x) {
|
|
||||||
return Math.round(x*cellWidth());
|
|
||||||
}
|
|
||||||
private int yCoordFromGame(int y) {
|
|
||||||
return Math.round(y*cellHeight());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void togglePieceSelector() {
|
|
||||||
pieceSelectorMode = ! pieceSelectorMode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void toggleAdderMode() {
|
|
||||||
pieceAdderMode = ! pieceAdderMode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isPieceSelectorMode() {
|
|
||||||
return pieceSelectorMode;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public boolean isPieceAdderMode() {
|
|
||||||
return pieceAdderMode;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
=======
|
|
||||||
package windowInterface;
|
package windowInterface;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
|
@ -392,4 +194,3 @@ public class JPanelChessBoard extends JPanel {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
>>>>>>> branch 'master' of https://gitarero.ecam.fr/piranut.phlang/OOP_2B5_Project.git
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
<<<<<<< HEAD
|
|
||||||
package windowInterface;
|
package windowInterface;
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
|
|
@ -268,274 +267,3 @@ public class MyInterface extends JFrame {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
package windowInterface;
|
|
||||||
import java.awt.BorderLayout;
|
|
||||||
import java.awt.Dimension;
|
|
||||||
import java.awt.GridLayout;
|
|
||||||
|
|
||||||
import javax.swing.JFrame;
|
|
||||||
import javax.swing.JPanel;
|
|
||||||
import javax.swing.SwingConstants;
|
|
||||||
import javax.swing.border.EmptyBorder;
|
|
||||||
import javax.swing.event.ChangeEvent;
|
|
||||||
import javax.swing.event.ChangeListener;
|
|
||||||
|
|
||||||
import backend.Game;
|
|
||||||
|
|
||||||
import javax.swing.JButton;
|
|
||||||
import javax.swing.JFileChooser;
|
|
||||||
import javax.swing.JLabel;
|
|
||||||
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.FileReader;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import javax.swing.JList;
|
|
||||||
import javax.swing.AbstractListModel;
|
|
||||||
import javax.swing.JToggleButton;
|
|
||||||
import javax.swing.JRadioButton;
|
|
||||||
import javax.swing.JCheckBox;
|
|
||||||
|
|
||||||
public class MyInterface extends JFrame {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = -6840815447618468846L;
|
|
||||||
private JPanel contentPane;
|
|
||||||
private JLabel turnLabel;
|
|
||||||
private JLabel borderLabel;
|
|
||||||
private JLabel speedLabel;
|
|
||||||
private JPanelChessBoard panelDraw;
|
|
||||||
private Game game;
|
|
||||||
private JLabel actionLabel;
|
|
||||||
private JCheckBox chckbxBlackAI;
|
|
||||||
private JCheckBox chckbxWhiteAI;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create the frame.
|
|
||||||
*/
|
|
||||||
public MyInterface() {
|
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
||||||
setBounds(10, 10, 650, 650);
|
|
||||||
contentPane = new JPanel();
|
|
||||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
|
||||||
contentPane.setLayout(new BorderLayout(0, 0));
|
|
||||||
setContentPane(contentPane);
|
|
||||||
|
|
||||||
JPanel panelTop = new JPanel();
|
|
||||||
contentPane.add(panelTop, BorderLayout.NORTH);
|
|
||||||
JPanel panelRight = new JPanel();
|
|
||||||
contentPane.add(panelRight, BorderLayout.EAST);
|
|
||||||
panelRight.setLayout(new GridLayout(4,1));
|
|
||||||
|
|
||||||
|
|
||||||
actionLabel = new JLabel("Waiting For Start");
|
|
||||||
panelTop.add(actionLabel);
|
|
||||||
|
|
||||||
JButton btnGo = new JButton("Start/Restart");
|
|
||||||
btnGo.addActionListener(new ActionListener() {
|
|
||||||
public void actionPerformed(ActionEvent arg0) {
|
|
||||||
clicButtonStart();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
panelTop.add(btnGo);
|
|
||||||
|
|
||||||
turnLabel = new JLabel("Turn : X");
|
|
||||||
panelTop.add(turnLabel);
|
|
||||||
|
|
||||||
JButton btnLoad = new JButton("Load File");
|
|
||||||
btnLoad.addActionListener(new ActionListener() {
|
|
||||||
public void actionPerformed(ActionEvent arg0) {
|
|
||||||
clicLoadFileButton();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
panelRight.add(btnLoad);
|
|
||||||
|
|
||||||
JButton btnSave = new JButton("Save To File");
|
|
||||||
btnSave.addActionListener(new ActionListener() {
|
|
||||||
public void actionPerformed(ActionEvent arg0) {
|
|
||||||
clicSaveToFileButton();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
panelRight.add(btnSave);
|
|
||||||
|
|
||||||
JButton btnAdder = new JButton("Add Piece");
|
|
||||||
btnAdder.addActionListener(new ActionListener() {
|
|
||||||
public void actionPerformed(ActionEvent arg0) {
|
|
||||||
clickButtonAdder();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
panelRight.add(btnAdder);
|
|
||||||
|
|
||||||
JButton btnPieceSelector = new JButton("Piece Select");
|
|
||||||
btnPieceSelector.addActionListener(new ActionListener() {
|
|
||||||
public void actionPerformed(ActionEvent arg0) {
|
|
||||||
clickButtonSelector();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
panelRight.add(btnPieceSelector);
|
|
||||||
|
|
||||||
JButton btnUndo = new JButton("Undo");
|
|
||||||
btnUndo.addActionListener(new ActionListener() {
|
|
||||||
public void actionPerformed(ActionEvent arg0) {
|
|
||||||
clicUndoButton();
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
panelTop.add(btnUndo);
|
|
||||||
|
|
||||||
chckbxWhiteAI = new JCheckBox("WhiteAI");
|
|
||||||
chckbxWhiteAI.addActionListener(new ActionListener() {
|
|
||||||
public void actionPerformed(ActionEvent arg0) {
|
|
||||||
clicAIToggle(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
panelTop.add(chckbxWhiteAI);
|
|
||||||
|
|
||||||
chckbxBlackAI = new JCheckBox("BlackAI");
|
|
||||||
chckbxBlackAI.addActionListener(new ActionListener() {
|
|
||||||
public void actionPerformed(ActionEvent arg0) {
|
|
||||||
clicAIToggle(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
panelTop.add(chckbxBlackAI);
|
|
||||||
|
|
||||||
panelDraw = new JPanelChessBoard(this);
|
|
||||||
contentPane.add(panelDraw, BorderLayout.CENTER);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStepBanner(String s) {
|
|
||||||
turnLabel.setText(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBorderBanner(String s) {
|
|
||||||
borderLabel.setText(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
public JPanelChessBoard getPanelDessin() {
|
|
||||||
return panelDraw;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void instantiateSimu() {
|
|
||||||
if(game==null) {
|
|
||||||
game = new Game(this);
|
|
||||||
panelDraw.setGame(game);
|
|
||||||
game.start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clicButtonStart() {
|
|
||||||
this.instantiateSimu();
|
|
||||||
game.setDefaultSetup();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickButtonAdder() {
|
|
||||||
panelDraw.toggleAdderMode();
|
|
||||||
}
|
|
||||||
public void clickButtonSelector() {
|
|
||||||
panelDraw.togglePieceSelector();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void clicUndoButton() {
|
|
||||||
if(game == null) {
|
|
||||||
System.out.println("error : can't undo while no game present");
|
|
||||||
} else {
|
|
||||||
game.undoLastMove();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
public void clicAIToggle(boolean isWhite) {
|
|
||||||
if(game == null) {
|
|
||||||
System.out.println("error : can't activate AI while no game present");
|
|
||||||
if(isWhite) {
|
|
||||||
chckbxWhiteAI.setSelected(false);
|
|
||||||
}else {
|
|
||||||
chckbxBlackAI.setSelected(false);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
game.toggleAI(isWhite);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clicLoadFileButton() {
|
|
||||||
Game loadedSim = new Game(this);
|
|
||||||
String fileName=SelectFile();
|
|
||||||
LinkedList<String> lines = new LinkedList<String>();
|
|
||||||
if (fileName.length()>0) {
|
|
||||||
try {
|
|
||||||
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
|
|
||||||
String line = fileContent.readLine();
|
|
||||||
int colorID = 0;
|
|
||||||
while (line != null) {
|
|
||||||
lines.add(line);
|
|
||||||
line = fileContent.readLine();
|
|
||||||
}
|
|
||||||
loadedSim.setBoard(Arrays.stream(lines.toArray()).map(Object::toString).toArray(String[]::new));
|
|
||||||
fileContent.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
game = loadedSim;
|
|
||||||
panelDraw.setGame(game);
|
|
||||||
this.repaint();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clicSaveToFileButton() {
|
|
||||||
String fileName=SelectFile();
|
|
||||||
if (fileName.length()>0) {
|
|
||||||
String[] content = game.getFileRepresentation();
|
|
||||||
writeFile(fileName, content);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String SelectFile() {
|
|
||||||
String s;
|
|
||||||
JFileChooser chooser = new JFileChooser();
|
|
||||||
chooser.setCurrentDirectory(new java.io.File("."));
|
|
||||||
chooser.setDialogTitle("Choose a file");
|
|
||||||
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
|
|
||||||
chooser.setAcceptAllFileFilterUsed(true);
|
|
||||||
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
|
||||||
s=chooser.getSelectedFile().toString();
|
|
||||||
} else {
|
|
||||||
System.out.println("No Selection ");
|
|
||||||
s="";
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void writeFile(String fileName, String[] content) {
|
|
||||||
FileWriter csvWriter;
|
|
||||||
try {
|
|
||||||
csvWriter = new FileWriter(fileName);
|
|
||||||
for (String row : content) {
|
|
||||||
csvWriter.append(row);
|
|
||||||
csvWriter.append("\n");
|
|
||||||
}
|
|
||||||
csvWriter.flush();
|
|
||||||
csvWriter.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void update(int turnCount, boolean turnIsWhite) {
|
|
||||||
turnLabel.setText("Turn : "+turnCount+", "+ (turnIsWhite?"White":"Black"));
|
|
||||||
actionLabel.setText(panelDraw.isPieceAdderMode()?"Adding Piece":
|
|
||||||
(panelDraw.isPieceSelectorMode()?"Selecting Piece to Add":
|
|
||||||
"Playing"));
|
|
||||||
this.repaint();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void eraseLabels() {
|
|
||||||
this.setStepBanner("Turn : X");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
>>>>>>> branch 'master' of https://gitarero.ecam.fr/piranut.phlang/OOP_2B5_Project.git
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue