From 640a0833447a56c9646c703d6d5b2737839dc4d4 Mon Sep 17 00:00:00 2001 From: eetu <earant@utu.fi> Date: Sun, 15 Apr 2018 18:21:08 +0300 Subject: [PATCH] =?UTF-8?q?humanplayer=20toimii=20jotenkin,=20sy=C3=B6mine?= =?UTF-8?q?n=20onnistuu,=20positionin=20deepcopy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ChessGame.java | 39 ++++----------------------------------- src/ComputerPlayer.java | 3 +-- src/HumanPlayer.java | 4 ++-- src/Move.java | 6 +++--- src/Piece.java | 2 +- src/Player.java | 4 +--- src/Position.java | 41 ++++++++++++++++++++--------------------- src/Square.java | 10 +++++----- 8 files changed, 37 insertions(+), 72 deletions(-) diff --git a/src/ChessGame.java b/src/ChessGame.java index 19a8897..55ed57a 100644 --- a/src/ChessGame.java +++ b/src/ChessGame.java @@ -1,15 +1,11 @@ -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 diff --git a/src/ComputerPlayer.java b/src/ComputerPlayer.java index 91d1b30..32dbf56 100644 --- a/src/ComputerPlayer.java +++ b/src/ComputerPlayer.java @@ -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())); } - } diff --git a/src/HumanPlayer.java b/src/HumanPlayer.java index bb77b21..e4fe596 100644 --- a/src/HumanPlayer.java +++ b/src/HumanPlayer.java @@ -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; diff --git a/src/Move.java b/src/Move.java index 4506061..29b4be5 100644 --- a/src/Move.java +++ b/src/Move.java @@ -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; diff --git a/src/Piece.java b/src/Piece.java index 227e8d0..5b2967f 100644 --- a/src/Piece.java +++ b/src/Piece.java @@ -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; } diff --git a/src/Player.java b/src/Player.java index d15397a..fe9ee01 100644 --- a/src/Player.java +++ b/src/Player.java @@ -1,9 +1,7 @@ -import java.util.ArrayList; - public abstract class Player { String name; - public Player(String name) { + Player(String name) { this.name = name; } diff --git a/src/Position.java b/src/Position.java index 24210b1..d903190 100644 --- a/src/Position.java +++ b/src/Position.java @@ -1,19 +1,11 @@ -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; + } } + } + + diff --git a/src/Square.java b/src/Square.java index 33d017d..f0de340 100644 --- a/src/Square.java +++ b/src/Square.java @@ -1,18 +1,18 @@ 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(); } -- GitLab