Commit 44221cef authored by eetu's avatar eetu
Browse files

initial

parents
public class Move {
int oldX;
int oldY;
int newX;
int newY;
public Move(int oldX, int oldY, int newX, int newY) {
this.oldX = oldX;
this.oldY = oldY;
this.newX = newX;
this.newY = newY;
}
public Move(int oldX, int oldY, Direction dir) {
this.oldX = oldX;
this.oldY = oldY;
switch (dir) {
case UP:
newX = oldX;
newY = oldY - 1;
break;
case DOWN:
newX = oldX;
newY = oldY + 1;
break;
case LEFT:
newX = oldX - 1;
newY = oldY;
break;
case RIGHT:
newX = oldX + 1;
newY = oldY;
case NE:
newX = oldX + 1;
newY = oldY - 1;
break;
case SE:
newX = oldX + 1;
newY = oldY + 1;
break;
case SW:
newX = oldX - 1;
newY = oldY + 1;
break;
case NW:
newX = oldX - 1;
newY = oldY - 1;
break;
}
}
}
enum Direction {
UP, DOWN, LEFT, RIGHT, NE, SE, SW, NW
}
public class Piece {
Color color;
Type type;
public Piece(Color color, Type type) {
this.color = color;
this.type = type;
}
@Override
public String toString() {
return color.toString().substring(0,1) + type.toString().substring(0,1);
}
}
enum Color {
WHITE, BLACK
}
enum Type {
PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING
}
public class Position {
private Square[][] board;
// initial position (state)
public Position() {
board = new Square[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = new Square();
}
}
board[4][4].setPiece(new Piece(Color.WHITE, Type.KING));
}
// resulting position after a move (result(action))
public Position(Position pos, Move move) {
this.board = pos.board;
Piece p = board[move.oldY][move.oldX].getPiece();
board[move.oldY][move.oldX].setPiece(null);
board[move.newY][move.newX].setPiece(p);
}
public Piece getPieceAt(int x, int y) {
return board[x][y].getPiece();
}
public Location getPieceLocation(Color color, Type type){
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);
}
}
}
}
return null;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Square[] row : board) {
for (Square s : row) {
sb.append(s);
}
sb.append("\n");
}
return sb.toString();
}
}
class Location {
int x;
int y;
public Location(int x, int y) {
this.x = x;
this.y = y;
}
}
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);
kingLocation = pos.getPieceLocation(Color.WHITE, Type.KING);
pos = new Position(pos, new Move(kingLocation.x, kingLocation.y, Direction.UP));
System.out.println(pos);
}
}
\ No newline at end of file
public class Square {
private Piece piece;
public Square() {
}
public Square(Piece piece) {
this.piece = piece;
}
public Piece getPiece() {
return piece;
}
public void setPiece(Piece piece) {
this.piece = piece;
}
@Override
public String toString() {
return isEmpty() ? "_ " : piece.toString();
}
boolean isEmpty() {
return piece == null;
}
}
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