console piece movement should work
This commit is contained in:
@@ -1,11 +1,16 @@
|
|||||||
package chess.view.consolerender;
|
package chess.view.consolerender;
|
||||||
|
|
||||||
|
import chess.controller.Command;
|
||||||
import chess.controller.CommandExecutor;
|
import chess.controller.CommandExecutor;
|
||||||
import chess.controller.OutputSystem;
|
import chess.controller.OutputSystem;
|
||||||
import chess.controller.commands.GetPieceAtCommand;
|
import chess.controller.commands.GetPieceAtCommand;
|
||||||
|
import chess.controller.commands.MoveCommand;
|
||||||
|
import chess.controller.commands.PromoteCommand;
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Move;
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
|
import chess.model.pieces.*;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
@@ -19,29 +24,73 @@ public class Console implements OutputSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Piece pieceAt(int x, int y) {
|
private Piece pieceAt(int x, int y) {
|
||||||
GetPieceAtCommand command = new GetPieceAtCommand(new Coordinate(x, y));
|
return pieceAt(new Coordinate(x, y));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Piece pieceAt(Coordinate coordinate) {
|
||||||
|
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
|
||||||
this.commandExecutor.executeCommand(command);
|
this.commandExecutor.executeCommand(command);
|
||||||
return command.getPiece();
|
return command.getPiece();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Coordinate stringToCoordinate(String coordinates) {
|
||||||
|
char xPos = coordinates.charAt(0);
|
||||||
|
char yPos = coordinates.charAt(1);
|
||||||
|
int x = xPos - 'a';
|
||||||
|
int y = 7 - (yPos - '1');
|
||||||
|
return new Coordinate(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean playerPickedMove() {
|
||||||
|
System.out.println("Piece to move: ");
|
||||||
|
Coordinate start = stringToCoordinate(scanner.nextLine());
|
||||||
|
System.out.println("New position: ");
|
||||||
|
Coordinate end = stringToCoordinate(scanner.nextLine());
|
||||||
|
return (this.commandExecutor.executeCommand(new MoveCommand(new Move(start, end))) == Command.CommandResult.Moved;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void playerTurn(Color color) {
|
public void playerTurn(Color color) {
|
||||||
|
System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET);
|
||||||
|
boolean endTurn = false;
|
||||||
|
do {
|
||||||
|
System.out.println("""
|
||||||
|
Pick your move:
|
||||||
|
1 - Move
|
||||||
|
2 - Show potential moves
|
||||||
|
3 - Surrender
|
||||||
|
""");
|
||||||
|
endTurn = switch (scanner.nextLine()) {
|
||||||
|
case "1" -> playerPickedMove();
|
||||||
|
case "2" -> playerPickedShowMoves();
|
||||||
|
case "3" -> playerPickedSurrender();
|
||||||
|
default -> false;
|
||||||
|
};
|
||||||
|
} while (!endTurn);
|
||||||
|
System.out.println(Colors.RED + "Turn ended." + Colors.RESET);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean playerPickedShowMoves() {
|
||||||
|
System.out.println("Piece to examine: ");
|
||||||
|
Coordinate piece = stringToCoordinate(scanner.nextLine());
|
||||||
|
// todo
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void winnerIs(Color color) {
|
public void winnerIs(Color color) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void kingIsInCheck() {
|
public void kingIsInCheck() {
|
||||||
System.out.println("Check!");
|
System.out.println(Colors.RED + "Check!" + Colors.RESET);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void kingIsInMat() {
|
public void kingIsInMat() {
|
||||||
System.out.println("Checkmate!");
|
System.out.println(Colors.RED + "Checkmate!" + Colors.RESET);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -63,15 +112,21 @@ public class Console implements OutputSystem {
|
|||||||
@Override
|
@Override
|
||||||
public void promotePawn(Coordinate pieceCoords) {
|
public void promotePawn(Coordinate pieceCoords) {
|
||||||
System.out.println("The pawn on the " + pieceCoords + " coordinates needs to be promoted.");
|
System.out.println("The pawn on the " + pieceCoords + " coordinates needs to be promoted.");
|
||||||
System.out.println("Enter 1 to promote it into a Bishop, 2 for a Knight, 3 for a Queen, 4 for a Rook.");
|
System.out.println("Enter 'B' to promote it into a Bishop, 'N' for a Knight, 'Q' for a Queen, 'R' for a Rook.");
|
||||||
boolean valid = false;
|
boolean valid = false;
|
||||||
|
PromoteCommand.PromoteType newPiece;
|
||||||
do {
|
do {
|
||||||
try {
|
try {
|
||||||
int promotion = scanner.nextInt();
|
String promotion = scanner.next();
|
||||||
if (promotion < 1 || promotion > 4) throw new Exception();
|
newPiece = switch (promotion) {
|
||||||
System.out.println("blablabla");
|
case ("B") -> PromoteCommand.PromoteType.Bishop;
|
||||||
|
case ("N") -> PromoteCommand.PromoteType.Knight;
|
||||||
|
case ("Q") -> PromoteCommand.PromoteType.Queen;
|
||||||
|
case ("R") -> PromoteCommand.PromoteType.Rook;
|
||||||
|
default -> throw new Exception();
|
||||||
|
};
|
||||||
valid = true;
|
valid = true;
|
||||||
|
this.commandExecutor.executeCommand(new PromoteCommand(newPiece));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Invalid input!");
|
System.out.println("Invalid input!");
|
||||||
}
|
}
|
||||||
@@ -84,7 +139,7 @@ public class Console implements OutputSystem {
|
|||||||
StringBuilder string = new StringBuilder();
|
StringBuilder string = new StringBuilder();
|
||||||
string.append(" a b c d e f g h \n");
|
string.append(" a b c d e f g h \n");
|
||||||
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||||
string.append(8-i + " ");
|
string.append(8 - i).append(" ");
|
||||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
Piece p = pieceAt(j, i);
|
Piece p = pieceAt(j, i);
|
||||||
if ((i+j)%2==0) {
|
if ((i+j)%2==0) {
|
||||||
@@ -97,7 +152,7 @@ public class Console implements OutputSystem {
|
|||||||
string.append(" " + Colors.RESET);
|
string.append(" " + Colors.RESET);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
string.append(" " + consolePieceName.getString(p) + " " + Colors.RESET);
|
string.append(" ").append(consolePieceName.getString(p)).append(" ").append(Colors.RESET);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
string.append("\n");
|
string.append("\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user