Commit 640a0833 authored by eetu's avatar eetu
Browse files

humanplayer toimii jotenkin, syöminen onnistuu, positionin deepcopy

parent 977aa25e
import java.lang.instrument.Instrumentation;
import java.util.ArrayList;
import java.util.Random;
public class ChessGame {
Player white;
Player black;
Position currentPos;
private Player white;
private Player black;
private Position currentPos;
// moves list?
public ChessGame(Player white, Player black, Position currentPos) {
ChessGame(Player white, Player black, Position currentPos) {
this.white = white;
this.black = black;
this.currentPos = currentPos;
......@@ -39,33 +35,6 @@ public class ChessGame {
chess.currentPos = chess.currentPos.result(move);
}
//
// ArrayList<Move> legalMoves = pos.getLegalMoves(Color.WHITE);
//
// for (int i = 0; i < le1galMoves.size(); i++) {
// Move m = legalMoves.get(i);
// pos.result(m);
// System.out.println(pos);
// pos = new Position(savedpos.getBoard());
// }
// 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
......@@ -3,7 +3,7 @@ import java.util.Random;
public class ComputerPlayer extends Player {
public ComputerPlayer(String name) {
ComputerPlayer(String name) {
super(name);
}
......@@ -14,5 +14,4 @@ public class ComputerPlayer extends Player {
return legalMoves.get(random.nextInt(legalMoves.size()));
}
}
......@@ -3,7 +3,7 @@ import java.util.Scanner;
public class HumanPlayer extends Player {
public HumanPlayer(String name) {
HumanPlayer(String name) {
super(name);
}
......@@ -14,7 +14,7 @@ public class HumanPlayer extends Player {
System.out.println(pos);
Move move = null;
Move move;
boolean repeat = true;
......
......@@ -4,14 +4,14 @@ public class Move {
int newX;
int newY;
public Move(int oldX, int oldY, int newX, int newY) {
Move(int oldX, int oldY, int newX, int newY) {
this.oldX = oldX;
this.oldY = oldY;
this.newX = newX;
this.newY = newY;
}
public Move(String move) {
Move(String move) {
String letters = "abcdefgh";
this.oldX = letters.indexOf(move.charAt(0));
......@@ -22,7 +22,7 @@ public class Move {
}
public Move(int oldX, int oldY, Direction dir) {
Move(int oldX, int oldY, Direction dir) {
this.oldX = oldX;
this.oldY = oldY;
......
......@@ -2,7 +2,7 @@ public class Piece {
Color color;
Type type;
public Piece(Color color, Type type) {
Piece(Color color, Type type) {
this.color = color;
this.type = type;
}
......
import java.util.ArrayList;
public abstract class Player {
String name;
public Player(String name) {
Player(String name) {
this.name = name;
}
......
import javafx.geometry.Pos;
import java.util.ArrayList;
import java.util.Arrays;
public class Position {
private Square[][] board;
public Color getMovesNext() {
return movesNext;
}
private Color movesNext;
// initial position (state)
public Position() {
Position() {
movesNext = Color.WHITE;
board = new Square[8][8];
......@@ -59,7 +51,7 @@ public class Position {
}
// copy position with this constructor
public Position(Position pos) {
Position(Position pos) {
Square[][] boardCopy = new Square[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
......@@ -70,7 +62,7 @@ public class Position {
this.movesNext = pos.movesNext;
}
public Position result(Move move) {
Position result(Move move) {
Position posCopy = new Position(this);
Piece p = posCopy.board[move.oldY][move.oldX].getPiece();
......@@ -87,11 +79,15 @@ public class Position {
return posCopy;
}
public Piece getPieceAt(int x, int y) {
Color getMovesNext() {
return movesNext;
}
Piece getPieceAt(int x, int y) {
return board[y][x].getPiece();
}
public Location getPieceLocation(Piece piece) {
Location getPieceLocation(Piece piece) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (!board[i][j].isEmpty()) {
......@@ -106,7 +102,7 @@ public class Position {
return null;
}
public ArrayList<Piece> getPieces() {
ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>();
for (int i = 0; i < 8; i++) {
......@@ -301,14 +297,17 @@ public class Position {
sb.append(" a b c d e f g h \n");
return sb.toString();
}
}
class Location {
int x;
int y;
class Location {
int x;
int y;
public Location(int x, int y) {
this.x = x;
this.y = y;
public Location(int x, int y) {
this.x = x;
this.y = y;
}
}
}
public class Square {
private Piece piece;
public Square() {
Square() {
}
public Square(Piece piece) {
Square(Piece piece) {
this.piece = piece;
}
public Piece getPiece() {
Piece getPiece() {
return piece;
}
public void setPiece(Piece piece) {
void setPiece(Piece piece) {
this.piece = piece;
}
......@@ -25,7 +25,7 @@ public class Square {
return piece == null;
}
public Square deepCopy () {
Square deepCopy () {
if(isEmpty()) {
return new Square();
}
......
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