Commit 977aa25e authored by eetu's avatar eetu
Browse files

humanplayer toimii jotenkin, syöminen onnistuu, positionin deepcopy

parent 4792ed39
......@@ -2,23 +2,44 @@ 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();
public class ChessGame {
Player white;
Player black;
Position currentPos;
// moves list?
Player human = new HumanPlayer("human");
public ChessGame(Player white, Player black, Position currentPos) {
this.white = white;
this.black = black;
this.currentPos = currentPos;
}
Position poscopy = new Position(pos.getBoard());
public static void main(String[] args) {
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);
Player white = new HumanPlayer("human");
Player black = new ComputerPlayer("computer");
Position initialPos = new Position();
ChessGame chess = new ChessGame(white, black, initialPos);
while(true) {
Move move;
if(chess.currentPos.getMovesNext() == Color.WHITE) {
System.out.println(white.name + " turn");
move = white.chooseMove(chess.currentPos);
System.out.println();
}
else {
System.out.println(black.name + " turn");
move = black.chooseMove(chess.currentPos);
System.out.println(black.name + " chose " + move + "\n");
//chess.currentPos.printMove(move);
}
chess.currentPos = chess.currentPos.result(move);
}
System.out.println(poscopy);
//
......
import java.util.ArrayList;
import java.util.Random;
public class ComputerPlayer extends Player {
......@@ -7,8 +8,10 @@ public class ComputerPlayer extends Player {
}
@Override
Move chooseMove(ArrayList<Move> legalMoves) {
return null;
Move chooseMove(Position pos) {
Random random = new Random();
ArrayList<Move> legalMoves = pos.getLegalMoves();
return legalMoves.get(random.nextInt(legalMoves.size()));
}
......
......@@ -8,9 +8,35 @@ public class HumanPlayer extends Player {
}
@Override
Move chooseMove(ArrayList<Move> legalMoves) {
Move chooseMove(Position pos) {
ArrayList<Move> legalMoves = pos.getLegalMoves();
Scanner scanner = new Scanner(System.in);
return legalMoves.get(scanner.nextInt());
System.out.println(pos);
Move move = null;
boolean repeat = true;
while(repeat) {
try {
System.out.print("Choose your move: ");
move = new Move(scanner.nextLine());
if(legalMoves.contains(move)) {
return move;
}
else {
System.out.println("thats not a legal move");
throw new Exception();
}
} catch (Exception e) {
repeat = true;
System.out.println("write your move formated like this: a0 h7");
}
}
return null;
}
}
......@@ -11,6 +11,17 @@ public class Move {
this.newY = newY;
}
public Move(String move) {
String letters = "abcdefgh";
this.oldX = letters.indexOf(move.charAt(0));
this.oldY = Character.getNumericValue(move.charAt(1));
this.newX = letters.indexOf(move.charAt(3));
this.newY = Character.getNumericValue(move.charAt(4));
}
public Move(int oldX, int oldY, Direction dir) {
this.oldX = oldX;
this.oldY = oldY;
......@@ -53,13 +64,21 @@ public class Move {
@Override
public String toString() {
return "Move{" +
"oldX=" + oldX +
", oldY=" + oldY +
", newX=" + newX +
", newY=" + newY +
'}';
String letters = "abcdefgh";
return "" + letters.charAt(oldX) + oldY + " " + letters.charAt(newX) + newY;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Move move = (Move) o;
return oldX == move.oldX &&
oldY == move.oldY &&
newX == move.newX &&
newY == move.newY;
}
}
enum Direction {
......
......@@ -7,5 +7,5 @@ public abstract class Player {
this.name = name;
}
abstract Move chooseMove(ArrayList<Move> legalMoves);
abstract Move chooseMove(Position pos);
}
......@@ -6,8 +6,16 @@ import java.util.Arrays;
public class Position {
private Square[][] board;
public Color getMovesNext() {
return movesNext;
}
private Color movesNext;
// initial position (state)
public Position() {
movesNext = Color.WHITE;
board = new Square[8][8];
for (int i = 0; i < 8; i++) {
......@@ -50,26 +58,33 @@ public class Position {
}
public Position(Square[][] board) {
// copy position with this constructor
public Position(Position pos) {
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].deepCopy();
boardCopy[i][j] = pos.board[i][j].deepCopy();
}
}
this.board = boardCopy;
this.movesNext = pos.movesNext;
}
public Position 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);
Position posCopy = new Position(this);
return new Position(board);
}
Piece p = posCopy.board[move.oldY][move.oldX].getPiece();
posCopy.board[move.oldY][move.oldX].setPiece(null);
posCopy.board[move.newY][move.newX].setPiece(p);
public Square[][] getBoard() {
return board;
if (posCopy.movesNext == Color.WHITE) {
posCopy.movesNext = Color.BLACK;
}
else {
posCopy.movesNext = Color.WHITE;
}
return posCopy;
}
public Piece getPieceAt(int x, int y) {
......@@ -91,14 +106,14 @@ public class Position {
return null;
}
public ArrayList<Piece> getPieces(Color color) {
public ArrayList<Piece> getPieces() {
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) {
if (p.color == movesNext) {
pieces.add(p);
}
}
......@@ -108,8 +123,8 @@ public class Position {
return pieces;
}
void printLegalMoves(Color color) {
ArrayList<Move> legalMoves = getLegalMoves(color);
void printLegalMoves() {
ArrayList<Move> legalMoves = getLegalMoves();
for (int i = 0; i < legalMoves.size(); i++) {
......@@ -118,11 +133,16 @@ public class Position {
}
}
ArrayList<Move> getLegalMoves(Color color) {
void printMove(Move move) {
System.out.println(getPieceAt(move.oldX, move.oldY).type.toString() + " to " + move.newX +
", " + move.newY);
}
ArrayList<Move> getLegalMoves() {
ArrayList<Move> legalMoves = new ArrayList<>();
for (Piece piece : getPieces(color)) {
for (Piece piece : getPieces()) {
Type type = piece.type;
int x = getPieceLocation(piece).x;
......@@ -132,7 +152,7 @@ public class Position {
switch (type) {
case PAWN:
if (color == Color.WHITE) {
if (movesNext == Color.WHITE) {
if (y == 6) {
legalMoves.add(new Move(x, y, x, y - 2));
legalMoves.add(new Move(x, y, x, y - 1));
......@@ -165,7 +185,7 @@ public class Position {
if (board[m.newY][m.newX].isEmpty()) {
legalMoves.add(new Move(x, y, m.newX, m.newY));
}
else if (board[m.newY][m.newX].getPiece().color != color){
else if (board[m.newY][m.newX].getPiece().color != movesNext){
legalMoves.add(new Move(x, y, m.newX, m.newY));
break;
}
......@@ -194,7 +214,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() || board[m.newY][m.newX].getPiece().color != color) {
if (board[m.newY][m.newX].isEmpty() || board[m.newY][m.newX].getPiece().color != movesNext) {
legalMoves.add(m);
}
}
......@@ -211,7 +231,7 @@ public class Position {
legalMoves.add(new Move(x, y, m.newX, m.newY));
}
else if (board[m.newY][m.newX].getPiece().color != color){
else if (board[m.newY][m.newX].getPiece().color != movesNext){
legalMoves.add(new Move(x, y, m.newX, m.newY));
break;
}
......@@ -233,7 +253,7 @@ public class Position {
legalMoves.add(new Move(x, y, m.newX, m.newY));
}
else if (board[m.newY][m.newX].getPiece().color != color){
else if (board[m.newY][m.newX].getPiece().color != movesNext){
legalMoves.add(new Move(x, y, m.newX, m.newY));
break;
}
......@@ -251,7 +271,7 @@ public class Position {
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() || board[m.newY][m.newX].getPiece().color != color) {
if (board[m.newY][m.newX].isEmpty() || board[m.newY][m.newX].getPiece().color != movesNext) {
legalMoves.add(m);
}
}
......@@ -267,7 +287,7 @@ public class Position {
@Override
public String toString() {
StringBuilder sb = new StringBuilder(" 0 1 2 3 4 5 6 7 \n");
StringBuilder sb = new StringBuilder(" a b c d e f g h \n");
int i = 0;
for (Square[] row : board) {
......@@ -278,7 +298,7 @@ public class Position {
sb.append(i + "\n");
i++;
}
sb.append(" 0 1 2 3 4 5 6 7 \n");
sb.append(" a b c d e f g h \n");
return sb.toString();
}
}
......
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