Commit 4e001947 authored by Eetu Rantala's avatar Eetu Rantala
Browse files

asd

parent 44221cef
......@@ -31,6 +31,7 @@ public class Move {
case RIGHT:
newX = oldX + 1;
newY = oldY;
break;
case NE:
newX = oldX + 1;
newY = oldY - 1;
......@@ -50,9 +51,17 @@ public class Move {
}
}
@Override
public String toString() {
return "Move{" +
"oldX=" + oldX +
", oldY=" + oldY +
", newX=" + newX +
", newY=" + newY +
'}';
}
}
enum Direction {
UP, DOWN, LEFT, RIGHT, NE, SE, SW, NW
UP, DOWN, LEFT, RIGHT, NE, SE, SW, NW;
}
......@@ -9,7 +9,38 @@ public class Piece {
@Override
public String toString() {
return color.toString().substring(0,1) + type.toString().substring(0,1);
if (color == Color.WHITE) {
switch (type) {
case PAWN:
return "♙";
case ROOK:
return "♖";
case KNIGHT:
return "♘";
case BISHOP:
return "♗";
case QUEEN:
return "♕";
case KING:
return "♔";
}
} else {
switch (type) {
case PAWN:
return "♟";
case ROOK:
return "♜";
case KNIGHT:
return "♞";
case BISHOP:
return "♝";
case QUEEN:
return "♛";
case KING:
return "♚";
}
}
return "error";
}
}
......
import java.util.ArrayList;
public class Position {
private Square[][] board;
......@@ -13,8 +15,24 @@ public class Position {
}
board[0][0].setPiece(new Piece(Color.BLACK, Type.ROOK));
board[0][1].setPiece(new Piece(Color.BLACK, Type.KNIGHT));
board[0][2].setPiece(new Piece(Color.BLACK, Type.BISHOP));
board[0][3].setPiece(new Piece(Color.BLACK, Type.QUEEN));
board[0][4].setPiece(new Piece(Color.BLACK, Type.KING));
board[0][5].setPiece(new Piece(Color.BLACK, Type.BISHOP));
board[0][6].setPiece(new Piece(Color.BLACK, Type.KNIGHT));
board[0][7].setPiece(new Piece(Color.BLACK, Type.ROOK));
board[7][0].setPiece(new Piece(Color.WHITE, Type.ROOK));
board[7][1].setPiece(new Piece(Color.WHITE, Type.KNIGHT));
board[7][2].setPiece(new Piece(Color.WHITE, Type.BISHOP));
board[7][3].setPiece(new Piece(Color.WHITE, Type.QUEEN));
board[7][4].setPiece(new Piece(Color.WHITE, Type.KING));
board[7][5].setPiece(new Piece(Color.WHITE, Type.BISHOP));
board[7][6].setPiece(new Piece(Color.WHITE, Type.KNIGHT));
board[7][7].setPiece(new Piece(Color.WHITE, Type.ROOK));
board[4][4].setPiece(new Piece(Color.WHITE, Type.KING));
}
......@@ -22,6 +40,12 @@ public class Position {
// resulting position after a move (result(action))
public Position(Position pos, Move move) {
this.board = pos.board;
Piece p = this.board[move.oldY][move.oldX].getPiece();
this.board[move.oldY][move.oldX].setPiece(null);
this.board[move.newY][move.newX].setPiece(p);
}
public void result(Move move) {
Piece p = board[move.oldY][move.oldX].getPiece();
board[move.oldY][move.oldX].setPiece(null);
board[move.newY][move.newX].setPiece(p);
......@@ -31,13 +55,13 @@ public class Position {
return board[x][y].getPiece();
}
public Location getPieceLocation(Color color, Type type){
public Location getPieceLocation(Piece piece) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (!board[i][j].isEmpty()) {
Piece p = board[i][j].getPiece();
if (p.color == color && p.type == type) {
return new Location(i,j);
if (piece.equals(p)) {
return new Location(j, i);
}
}
......@@ -46,17 +70,144 @@ public class Position {
return null;
}
public ArrayList<Piece> getPieces(Color color) {
ArrayList<Piece> pieces = new ArrayList<>();
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (!board[i][j].isEmpty()) {
Piece p = board[i][j].getPiece();
if (p.color == color) {
pieces.add(p);
}
}
}
}
return pieces;
}
ArrayList<Move> getLegalMoves(Color color) {
ArrayList<Move> legalMoves = new ArrayList<>();
for (Piece piece : getPieces(color)) {
Type type = piece.type;
int x = getPieceLocation(piece).x;
int y = getPieceLocation(piece).y;
switch (type) {
case PAWN:
break;
case ROOK:
for (Direction dir : new Direction[]{Direction.UP, Direction.RIGHT, Direction.DOWN, Direction.LEFT}) {
Move m = new Move(x, y, dir);
while (m.newX >= 0 && m.newX <= 7 && m.newY >= 0 && m.newY <= 7) {
if (board[m.newY][m.newX].isEmpty()) {
legalMoves.add(new Move(x, y, m.newX, m.newY));
} else {
break;
}
m = new Move(m.newX, m.newY, dir);
}
}
break;
case KNIGHT:
Move[] moves = {
new Move(x, y, x + 1, y - 2),
new Move(x, y, x + 2, y - 1),
new Move(x, y, x + 2, y + 1),
new Move(x, y, x + 1, y + 2),
new Move(x, y, x - 1, y + 2),
new Move(x, y, x - 2, y + 1),
new Move(x, y, x - 2, y - 1),
new Move(x, y, x - 1, y - 2)
};
for (Move m : moves) {
if (m.newX >= 0 && m.newX <= 7 && m.newY >= 0 && m.newY <= 7) {
if (board[m.newY][m.newX].isEmpty()) {
legalMoves.add(m);
}
}
}
break;
case BISHOP:
for (Direction dir : new Direction[]{Direction.NE, Direction.SE, Direction.SW, Direction.NW}) {
Move m = new Move(x, y, dir);
while (m.newX >= 0 && m.newX <= 7 && m.newY >= 0 && m.newY <= 7) {
if (board[m.newY][m.newX].isEmpty()) {
legalMoves.add(new Move(x, y, m.newX, m.newY));
} else {
break;
}
m = new Move(m.newX, m.newY, dir);
}
}
break;
case QUEEN:
for (Direction dir : Direction.values()) {
Move m = new Move(x, y, dir);
while (m.newX >= 0 && m.newX <= 7 && m.newY >= 0 && m.newY <= 7) {
if (board[m.newY][m.newX].isEmpty()) {
legalMoves.add(new Move(x, y, m.newX, m.newY));
} else {
break;
}
m = new Move(m.newX, m.newY, dir);
}
}
break;
case KING:
for (Direction dir : Direction.values()) {
Move m = new Move(x, y, dir);
if (m.newX >= 0 && m.newX <= 7 && m.newY >= 0 && m.newY <= 7) {
if (board[m.newY][m.newX].isEmpty()) {
legalMoves.add(m);
}
}
}
break;
}
}
return legalMoves;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(" 0 1 2 3 4 5 6 7 \n");
int i = 0;
for (Square[] row : board) {
sb.append(i + " ");
for (Square s : row) {
sb.append(s);
sb.append(s + " ");
}
sb.append("\n");
sb.append(i + "\n");
i++;
}
sb.append(" 0 1 2 3 4 5 6 7 \n");
return sb.toString();
}
}
......
import java.util.ArrayList;
import java.util.Random;
public class Shakki {
public static void main(String[] args) {
Position pos = new Position();
System.out.println(pos);
Location kingLocation = pos.getPieceLocation(Color.WHITE, Type.KING);
pos = new Position(pos, new Move(kingLocation.x, kingLocation.y, Direction.UP));
System.out.println(pos);
ArrayList<Move> legalMoves = pos.getLegalMoves(Color.WHITE);
kingLocation = pos.getPieceLocation(Color.WHITE, Type.KING);
pos = new Position(pos, new Move(kingLocation.x, kingLocation.y, Direction.UP));
System.out.println(pos);
for (int i = 0; i < legalMoves.size(); i++) {
Move m = legalMoves.get(i);
pos.result(m);
System.out.println(pos);
pos = new Position();
}
legalMoves = pos.getLegalMoves(Color.BLACK);
for (int i = 0; i < legalMoves.size(); i++) {
Move m = legalMoves.get(i);
pos.result(m);
System.out.println(pos);
pos = new Position();
}
}
}
\ No newline at end of file
......@@ -18,7 +18,7 @@ public class Square {
@Override
public String toString() {
return isEmpty() ? "_ " : piece.toString();
return isEmpty() ? "_" : piece.toString();
}
boolean isEmpty() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment