Commit 4792ed39 authored by eetu's avatar eetu
Browse files

humanplayer toimii jotenkin, syöminen onnistuu, positionin deepcopy

parent cd825bfb
import java.util.ArrayList;
public class ComputerPlayer extends Player {
public ComputerPlayer(String name) {
super(name);
}
@Override
Move chooseMove(ArrayList<Move> legalMoves) {
return null;
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class HumanPlayer extends Player {
public HumanPlayer(String name) {
super(name);
}
@Override
Move chooseMove(ArrayList<Move> legalMoves) {
Scanner scanner = new Scanner(System.in);
return legalMoves.get(scanner.nextInt());
}
}
......@@ -12,32 +12,32 @@ public class Piece {
if (color == Color.WHITE) {
switch (type) {
case PAWN:
return "";
return "P";
case ROOK:
return "";
return "R";
case KNIGHT:
return "";
return "N";
case BISHOP:
return "";
return "B";
case QUEEN:
return "";
return "Q";
case KING:
return "";
return "K";
}
} else {
switch (type) {
case PAWN:
return "";
return "p";
case ROOK:
return "";
return "r";
case KNIGHT:
return "";
return "n";
case BISHOP:
return "";
return "b";
case QUEEN:
return "";
return "q";
case KING:
return "";
return "k";
}
}
return "error";
......
import java.util.ArrayList;
public abstract class Player {
String name;
public Player(String name) {
this.name = name;
}
abstract Move chooseMove(ArrayList<Move> legalMoves);
}
......@@ -6,7 +6,6 @@ import java.util.Arrays;
public class Position {
private Square[][] board;
// initial position (state)
public Position() {
board = new Square[8][8];
......@@ -17,35 +16,36 @@ public class Position {
}
}
board[0][0].setPiece(new Piece(Color.WHITE, Type.KING));
board[0][1].setPiece(new Piece(Color.BLACK, Type.KING));
// 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[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));
//
// for (int i = 0; i < 8; i++) {
// board[1][i].setPiece(new Piece(Color.BLACK, Type.PAWN));
// }
//
// 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[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));
//
// for (int i = 0; i < 8; i++) {
// board[6][i].setPiece(new Piece(Color.WHITE, Type.PAWN));
// }
board[0][0].setPiece(new Piece(Color.WHITE, Type.KING));
board[0][1].setPiece(new Piece(Color.BLACK, Type.KING));
}
......@@ -54,7 +54,7 @@ public class Position {
Square[][] boardCopy = new Square[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
boardCopy[i][j] = board[i][j];
boardCopy[i][j] = board[i][j].deepCopy();
}
}
this.board = boardCopy;
......@@ -73,7 +73,7 @@ public class Position {
}
public Piece getPieceAt(int x, int y) {
return board[x][y].getPiece();
return board[y][x].getPiece();
}
public Location getPieceLocation(Piece piece) {
......@@ -108,6 +108,16 @@ public class Position {
return pieces;
}
void printLegalMoves(Color color) {
ArrayList<Move> legalMoves = getLegalMoves(color);
for (int i = 0; i < legalMoves.size(); i++) {
System.out.println("Move " + i + ": " + getPieceAt(legalMoves.get(i).oldX, legalMoves.get(i).oldY).type.toString() + " to " + legalMoves.get(i).newX +
", " + legalMoves.get(i).newY);
}
}
ArrayList<Move> getLegalMoves(Color color) {
ArrayList<Move> legalMoves = new ArrayList<>();
......@@ -154,7 +164,12 @@ public class Position {
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 {
}
else if (board[m.newY][m.newX].getPiece().color != color){
legalMoves.add(new Move(x, y, m.newX, m.newY));
break;
}
else {
break;
}
m = new Move(m.newX, m.newY, dir);
......@@ -179,7 +194,7 @@ public class Position {
for (Move m : moves) {
if (m.newX >= 0 && m.newX <= 7 && m.newY >= 0 && m.newY <= 7) {
if (board[m.newY][m.newX].isEmpty()) {
if (board[m.newY][m.newX].isEmpty() || board[m.newY][m.newX].getPiece().color != color) {
legalMoves.add(m);
}
}
......@@ -194,7 +209,13 @@ public class Position {
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 {
}
else if (board[m.newY][m.newX].getPiece().color != color){
legalMoves.add(new Move(x, y, m.newX, m.newY));
break;
}
else {
break;
}
m = new Move(m.newX, m.newY, dir);
......@@ -211,7 +232,12 @@ public class Position {
if (board[m.newY][m.newX].isEmpty()) {
legalMoves.add(new Move(x, y, m.newX, m.newY));
} else {
}
else if (board[m.newY][m.newX].getPiece().color != color){
legalMoves.add(new Move(x, y, m.newX, m.newY));
break;
}
else {
break;
}
m = new Move(m.newX, m.newY, dir);
......
import java.lang.instrument.Instrumentation;
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);
pos = pos.result(new Move(0, 0, Direction.RIGHT));
Position posCopy = new Position(pos.getBoard());
System.out.println(pos);
System.out.println(posCopy);
Player human = new HumanPlayer("human");
pos = pos.result(new Move(1, 0, Direction.RIGHT));
posCopy = posCopy.result(new Move(1, 0, Direction.LEFT));
Position poscopy = new Position(pos.getBoard());
System.out.println(pos);
System.out.println(posCopy);
for (int i = 0; i < 10; i++) {
System.out.println(pos);
pos.printLegalMoves(Color.WHITE);
Move m = human.chooseMove(pos.getLegalMoves(Color.WHITE));
pos.result(m);
}
ArrayList<Move> legalMoves = pos.getLegalMoves(Color.WHITE);
System.out.println(legalMoves);
System.out.println(poscopy);
//
// ArrayList<Move> legalMoves = pos.getLegalMoves(Color.WHITE);
//
// for (int i = 0; i < legalMoves.size(); i++) {
// for (int i = 0; i < le1galMoves.size(); i++) {
// Move m = legalMoves.get(i);
// pos.result(m);
// System.out.println(pos);
// pos = new Position();
// }
//
// pos = new Position(savedpos.getBoard());
// }
// legalMoves = pos.getLegalMoves(Color.BLACK);
//
// for (int i = 0; i < legalMoves.size(); i++) {
......@@ -44,6 +44,7 @@ public class Shakki {
}
}
\ No newline at end of file
......@@ -24,4 +24,13 @@ public class Square {
boolean isEmpty() {
return piece == null;
}
public Square deepCopy () {
if(isEmpty()) {
return new Square();
}
else {
return new Square(new Piece(piece.color, piece.type));
}
}
}
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