board init
This commit is contained in:
parent
60e86f5471
commit
59431ad622
|
|
@ -1,9 +1,22 @@
|
|||
import backend.Board;
|
||||
import backend.Move;
|
||||
import backend.Piece;
|
||||
import backend.PieceType;
|
||||
import windowInterface.MyInterface;
|
||||
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,19 +3,31 @@ package backend;
|
|||
import java.util.ArrayList;
|
||||
|
||||
public class Board {
|
||||
|
||||
public int width;
|
||||
public int height;
|
||||
public ArrayList<ArrayList<Piece>> board = new ArrayList<>();
|
||||
private int selectPosition = 0;
|
||||
public Board(int colNum, int lineNum) {
|
||||
//TODO
|
||||
this.width = colNum;
|
||||
this.height = lineNum;
|
||||
int rows = 8;
|
||||
int cols = 8;
|
||||
for (int i = 0; i < rows; i++) {
|
||||
ArrayList<Piece> row = new ArrayList<>();
|
||||
for (int j = 0; j < cols; j++) {
|
||||
row.add(null); // Fill with null
|
||||
}
|
||||
this.board.add(row);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
//TODO
|
||||
return 0;
|
||||
return this.width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
//TODO
|
||||
return 0;
|
||||
return this.height;
|
||||
}
|
||||
|
||||
public int getTurnNumber() {
|
||||
|
|
@ -28,32 +40,75 @@ public class Board {
|
|||
return false;
|
||||
}
|
||||
|
||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||
//TODO
|
||||
public void setPiece(int x, int y, PieceType type, boolean isWhite) {
|
||||
Piece piece = new Piece(x,y,type,isWhite);
|
||||
board.get(y).set(x, piece);
|
||||
}
|
||||
|
||||
public void populateBoard() {
|
||||
//TODO
|
||||
for (int x = 0; x < this.width;x++) {
|
||||
for (int y = 0; y < this.height; y++) {
|
||||
if (y ==1 || y == 6) {
|
||||
if (y == 1) {
|
||||
this.setPiece(x,y,PieceType.Pawn,false);
|
||||
}
|
||||
else if (y == 6) {this.setPiece(x,y,PieceType.Pawn,true);}
|
||||
}
|
||||
if (y == 0) {
|
||||
boolean col = false;
|
||||
if (x == 0 || x == 7) {
|
||||
this.setPiece(x,y,PieceType.Rook,col);
|
||||
} else if (x == 1 || x == 6) {this.setPiece(x,y,PieceType.Knight,col);}
|
||||
else if (x == 2 || x == 5) {this.setPiece(x,y,PieceType.Bishop,col);}
|
||||
else if (x == 3) {this.setPiece(x,y,PieceType.Queen,col);}
|
||||
else if (x == 4) {this.setPiece(x,y,PieceType.King,col);}
|
||||
} else if (y == 7) {
|
||||
boolean col = true;
|
||||
if (x == 0 || x == 7) {
|
||||
this.setPiece(x,y,PieceType.Rook,col);
|
||||
} else if (x == 1 || x == 6) {this.setPiece(x,y,PieceType.Knight,col);}
|
||||
else if (x == 2 || x == 5) {this.setPiece(x,y,PieceType.Bishop,col);}
|
||||
else if (x == 3) {this.setPiece(x,y,PieceType.Queen,col);}
|
||||
else if (x == 4) {this.setPiece(x,y,PieceType.King,col);}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanBoard() {
|
||||
//TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
//TODO
|
||||
return "";
|
||||
return "Board [width=" + width + ", height=" + height + ", board=" + board + "]";
|
||||
}
|
||||
|
||||
public ArrayList<Piece> getPieces() {
|
||||
ArrayList<Piece> pieces = new ArrayList<>();
|
||||
//TODO
|
||||
|
||||
return pieces;
|
||||
for (ArrayList<Piece> row : board) {
|
||||
for (Piece piece : row) {
|
||||
if (piece != null) {
|
||||
pieces.add(piece);
|
||||
}
|
||||
}
|
||||
}
|
||||
return pieces;
|
||||
}
|
||||
|
||||
public Piece getPiece(int x, int y) {
|
||||
return board.get(x).get(y);
|
||||
}
|
||||
|
||||
public void userTouch(int x, int y) {
|
||||
//TODO
|
||||
Piece pieceToMove = new Piece();
|
||||
while (this.selectPosition < 2) {
|
||||
if (this.selectPosition == 0) {
|
||||
pieceToMove = this.getPiece(y,x);
|
||||
} else if (this.selectPosition == 1) {
|
||||
this.setPiece(x, y, pieceToMove.getType(), pieceToMove.isWhite());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -66,8 +66,8 @@ public class Game extends Thread {
|
|||
|
||||
}
|
||||
|
||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||
board.setPiece(isWhite, type, x, y);
|
||||
public void setPiece(int x, int y, PieceType type, boolean isWhite) {
|
||||
board.setPiece(x, y, type, isWhite);
|
||||
}
|
||||
|
||||
public String[] getFileRepresentation() {
|
||||
|
|
|
|||
|
|
@ -1,21 +1,36 @@
|
|||
package backend;
|
||||
|
||||
public class Piece {
|
||||
|
||||
public int x;
|
||||
public int y;
|
||||
public PieceType type;
|
||||
public boolean isWhite;
|
||||
public Piece() {
|
||||
}
|
||||
public Piece(int x, int y, PieceType type, boolean isWhite) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.type = type;
|
||||
this.isWhite = isWhite;
|
||||
}
|
||||
public int getX() {
|
||||
return 0;
|
||||
return this.x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return 0;
|
||||
return this.y;
|
||||
}
|
||||
|
||||
public PieceType getType() {
|
||||
return null;
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public boolean isWhite() {
|
||||
return false;
|
||||
return this.isWhite;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Piece [x=" + x + ", y=" + y + ", type=" + type + ", isWhite=" + isWhite + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ public class JPanelChessBoard extends JPanel {
|
|||
int y = (me.getY()*myGame.getHeight())/getHeight();
|
||||
if(pieceAdderMode) {
|
||||
//TODO
|
||||
myGame.setPiece(selectedPieceIsWhite,selectedPieceType, x, y);
|
||||
myGame.setPiece(x, y, selectedPieceType, selectedPieceIsWhite);
|
||||
pieceAdderMode = false;
|
||||
} else {
|
||||
myGame.clickCoords(x,y);
|
||||
|
|
|
|||
Loading…
Reference in New Issue