This commit is contained in:
2021-08-01 17:32:43 -05:00
parent 178cb00253
commit b10f61a489
76 changed files with 1655 additions and 1185 deletions

View File

@@ -1,4 +1,5 @@
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
using Gameboard.ShogiUI.Sockets.Utilities;
using PathFinding;
using System;
using System.Collections.Generic;
@@ -19,8 +20,10 @@ namespace Gameboard.ShogiUI.Sockets.Models
private Shogi? validationBoard;
private Vector2 player1King;
private Vector2 player2King;
public IReadOnlyDictionary<WhichPlayer, List<Piece>> Hands { get; }
public PlanarCollection<Piece> Board { get; } //TODO: Hide this being a getter method
private List<Piece> Hand => WhoseTurn == WhichPlayer.Player1 ? Player1Hand : Player2Hand;
public List<Piece> Player1Hand { get; }
public List<Piece> Player2Hand { get; }
public CoordsToNotationCollection Board { get; } //TODO: Hide this being a getter method
public List<Move> MoveHistory { get; }
public WhichPlayer WhoseTurn => MoveHistory.Count % 2 == 0 ? WhichPlayer.Player1 : WhichPlayer.Player2;
public WhichPlayer? InCheck { get; private set; }
@@ -30,15 +33,13 @@ namespace Gameboard.ShogiUI.Sockets.Models
public Shogi()
{
Board = new PlanarCollection<Piece>(9, 9);
Board = new CoordsToNotationCollection();
MoveHistory = new List<Move>(20);
Hands = new Dictionary<WhichPlayer, List<Piece>> {
{ WhichPlayer.Player1, new List<Piece>()},
{ WhichPlayer.Player2, new List<Piece>()},
};
pathFinder = new PathFinder2D<Piece>(Board);
player1King = new Vector2(4, 8);
player2King = new Vector2(4, 0);
Player1Hand = new List<Piece>();
Player2Hand = new List<Piece>();
pathFinder = new PathFinder2D<Piece>(Board, 9, 9);
player1King = new Vector2(4, 0);
player2King = new Vector2(4, 8);
Error = string.Empty;
InitializeBoardState();
@@ -58,24 +59,16 @@ namespace Gameboard.ShogiUI.Sockets.Models
private Shogi(Shogi toCopy)
{
Board = new PlanarCollection<Piece>(9, 9);
for (var x = 0; x < 9; x++)
for (var y = 0; y < 9; y++)
{
var piece = toCopy.Board[y, x];
if (piece != null)
{
Board[y, x] = new Piece(piece.WhichPiece, piece.Owner, piece.IsPromoted);
}
}
pathFinder = new PathFinder2D<Piece>(Board);
MoveHistory = new List<Move>(toCopy.MoveHistory);
Hands = new Dictionary<WhichPlayer, List<Piece>>
Board = new CoordsToNotationCollection();
foreach (var kvp in toCopy.Board)
{
{ WhichPlayer.Player1, new List<Piece>(toCopy.Hands[WhichPlayer.Player1]) },
{ WhichPlayer.Player2, new List<Piece>(toCopy.Hands[WhichPlayer.Player2]) }
};
Board[kvp.Key] = kvp.Value == null ? null : new Piece(kvp.Value);
}
pathFinder = new PathFinder2D<Piece>(Board, 9, 9);
MoveHistory = new List<Move>(toCopy.MoveHistory);
Player1Hand = new List<Piece>(toCopy.Player1Hand);
Player2Hand = new List<Piece>(toCopy.Player2Hand);
player1King = toCopy.player1King;
player2King = toCopy.player2King;
Error = toCopy.Error;
@@ -115,6 +108,8 @@ namespace Gameboard.ShogiUI.Sockets.Models
: validationBoard.PlaceFromBoard(move);
if (!isValid)
{
// Surface the error description.
Error = validationBoard.Error;
// Invalidate the "throw away" board.
validationBoard = null;
return false;
@@ -137,37 +132,55 @@ namespace Gameboard.ShogiUI.Sockets.Models
/// <returns>True if the move was successful.</returns>
private bool PlaceFromHand(Move move)
{
if (move.PieceFromHand.HasValue == false) return false; //Invalid move
var index = Hands[WhoseTurn].FindIndex(p => p.WhichPiece == move.PieceFromHand);
if (index < 0) return false; // Invalid move
if (Board[move.To.Y, move.To.X] != null) return false; // Invalid move; cannot capture while playing from the hand.
var index = Hand.FindIndex(p => p.WhichPiece == move.PieceFromHand);
if (index < 0)
{
Error = $"{move.PieceFromHand} does not exist in the hand.";
return false;
}
if (Board[move.To] != null)
{
Error = $"Illegal move - attempting to capture while playing a piece from the hand.";
return false;
}
var minimumY = 0;
switch (move.PieceFromHand.Value)
switch (move.PieceFromHand!.Value)
{
case WhichPiece.Knight:
// Knight cannot be placed onto the farthest two ranks from the hand.
minimumY = WhoseTurn == WhichPlayer.Player1 ? 6 : 2;
break;
{
// Knight cannot be placed onto the farthest two ranks from the hand.
if ((WhoseTurn == WhichPlayer.Player1 && move.To.Y > 6)
|| (WhoseTurn == WhichPlayer.Player2 && move.To.Y < 2))
{
Error = $"Knight has no valid moves after placed.";
return false;
}
break;
}
case WhichPiece.Lance:
case WhichPiece.Pawn:
// Lance and Pawn cannot be placed onto the farthest rank from the hand.
minimumY = WhoseTurn == WhichPlayer.Player1 ? 7 : 1;
break;
{
// Lance and Pawn cannot be placed onto the farthest rank from the hand.
if ((WhoseTurn == WhichPlayer.Player1 && move.To.Y == 8)
|| (WhoseTurn == WhichPlayer.Player2 && move.To.Y == 0))
{
Error = $"{move.PieceFromHand} has no valid moves after placed.";
return false;
}
break;
}
}
if (WhoseTurn == WhichPlayer.Player1 && move.To.Y < minimumY) return false;
if (WhoseTurn == WhichPlayer.Player2 && move.To.Y > minimumY) return false;
// Mutate the board.
Board[move.To.Y, move.To.X] = Hands[WhoseTurn][index];
Hands[WhoseTurn].RemoveAt(index);
Board[move.To] = Hand[index];
Hand.RemoveAt(index);
return true;
}
/// <returns>True if the move was successful.</returns>
private bool PlaceFromBoard(Move move)
{
var fromPiece = Board[move.From.Value.Y, move.From.Value.X];
var fromPiece = Board[move.From!.Value];
if (fromPiece == null)
{
Error = $"No piece exists at {nameof(move)}.{nameof(move.From)}.";
@@ -184,28 +197,28 @@ namespace Gameboard.ShogiUI.Sockets.Models
return false; // Invalid move; move not part of move-set.
}
var captured = Board[move.To.Y, move.To.X];
var captured = Board[move.To];
if (captured != null)
{
if (captured.Owner == WhoseTurn) return false; // Invalid move; cannot capture your own piece.
captured.Capture();
Hands[captured.Owner].Add(captured);
Hand.Add(captured);
}
//Mutate the board.
if (move.IsPromotion)
{
if (WhoseTurn == WhichPlayer.Player1 && (move.To.Y < 3 || move.From.Value.Y < 3))
if (WhoseTurn == WhichPlayer.Player1 && (move.To.Y > 5 || move.From.Value.Y > 5))
{
fromPiece.Promote();
}
else if (WhoseTurn == WhichPlayer.Player2 && (move.To.Y > 5 || move.From.Value.Y > 5))
else if (WhoseTurn == WhichPlayer.Player2 && (move.To.Y < 3 || move.From.Value.Y < 3))
{
fromPiece.Promote();
}
}
Board[move.To.Y, move.To.X] = fromPiece;
Board[move.From.Value.Y, move.From.Value.X] = null;
Board[move.To] = fromPiece;
Board[move.From!.Value] = null;
if (fromPiece.WhichPiece == WhichPiece.King)
{
if (fromPiece.Owner == WhichPlayer.Player1)
@@ -225,7 +238,7 @@ namespace Gameboard.ShogiUI.Sockets.Models
private bool IsPathable(Vector2 from, Vector2 to)
{
var piece = Board[from.Y, from.X];
var piece = Board[from];
if (piece == null) return false;
var isObstructed = false;
@@ -239,56 +252,66 @@ namespace Gameboard.ShogiUI.Sockets.Models
#region Rules Validation
private bool EvaluateCheckAfterMove(Move move, WhichPlayer whichPlayer)
{
if (whichPlayer == InCheck) return true; // If we already know the player is in check, don't bother.
var isCheck = false;
var kingPosition = whichPlayer == WhichPlayer.Player1 ? player1King : player2King;
// Check if the move put the king in check.
if (pathFinder.PathTo(move.To, kingPosition)) return true;
// Get line equation from king through the now-unoccupied location.
var direction = Vector2.Subtract(kingPosition, move.From.Value);
var slope = Math.Abs(direction.Y / direction.X);
// If absolute slope is 45°, look for a bishop along the line.
// If absolute slope is 0° or 90°, look for a rook along the line.
// if absolute slope is 0°, look for lance along the line.
if (float.IsInfinity(slope))
if (move.From.HasValue)
{
// if slope of the move is also infinity...can skip this?
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
// Get line equation from king through the now-unoccupied location.
var direction = Vector2.Subtract(kingPosition, move.From!.Value);
var slope = Math.Abs(direction.Y / direction.X);
// If absolute slope is 45°, look for a bishop along the line.
// If absolute slope is 0° or 90°, look for a rook along the line.
// if absolute slope is 0°, look for lance along the line.
if (float.IsInfinity(slope))
{
if (piece.Owner != whichPlayer)
// if slope of the move is also infinity...can skip this?
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
{
switch (piece.WhichPiece)
if (piece.Owner != whichPlayer)
{
case WhichPiece.Rook:
isCheck = true;
break;
case WhichPiece.Lance:
if (!piece.IsPromoted) isCheck = true;
break;
switch (piece.WhichPiece)
{
case WhichPiece.Rook:
isCheck = true;
break;
case WhichPiece.Lance:
if (!piece.IsPromoted) isCheck = true;
break;
}
}
}
});
}
else if (slope == 1)
{
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
});
}
else if (slope == 1)
{
if (piece.Owner != whichPlayer && piece.WhichPiece == WhichPiece.Bishop)
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
{
isCheck = true;
}
});
}
else if (slope == 0)
{
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
if (piece.Owner != whichPlayer && piece.WhichPiece == WhichPiece.Bishop)
{
isCheck = true;
}
});
}
else if (slope == 0)
{
if (piece.Owner != whichPlayer && piece.WhichPiece == WhichPiece.Rook)
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
{
isCheck = true;
}
});
if (piece.Owner != whichPlayer && piece.WhichPiece == WhichPiece.Rook)
{
isCheck = true;
}
});
}
}
else
{
// TODO: Check for illegal move from hand. It is illegal to place from the hand such that you check-mate your opponent.
// Go read the shogi rules to be sure this is true.
}
return isCheck;
@@ -299,12 +322,11 @@ namespace Gameboard.ShogiUI.Sockets.Models
// Assume true and try to disprove.
var isCheckmate = true;
Board.ForEachNotNull((piece, x, y) => // For each piece...
Board.ForEachNotNull((piece, from) => // For each piece...
{
// Short circuit
if (!isCheckmate) return;
var from = new Vector2(x, y);
if (piece.Owner == InCheck) // ...owned by the player in check...
{
// ...evaluate if any move gets the player out of check.
@@ -328,82 +350,108 @@ namespace Gameboard.ShogiUI.Sockets.Models
}
#endregion
#region Initialize
private void ResetEmptyRows()
{
for (int y = 3; y < 6; y++)
for (int x = 0; x < 9; x++)
Board[y, x] = null;
}
private void ResetFrontRow(WhichPlayer player)
{
int y = player == WhichPlayer.Player1 ? 6 : 2;
for (int x = 0; x < 9; x++) Board[y, x] = new Piece(WhichPiece.Pawn, player);
}
private void ResetMiddleRow(WhichPlayer player)
{
int y = player == WhichPlayer.Player1 ? 7 : 1;
Board[y, 0] = null;
for (int x = 2; x < 7; x++) Board[y, x] = null;
Board[y, 8] = null;
if (player == WhichPlayer.Player1)
{
Board[y, 1] = new Piece(WhichPiece.Bishop, player);
Board[y, 7] = new Piece(WhichPiece.Rook, player);
}
else
{
Board[y, 1] = new Piece(WhichPiece.Rook, player);
Board[y, 7] = new Piece(WhichPiece.Bishop, player);
}
}
private void ResetRearRow(WhichPlayer player)
{
int y = player == WhichPlayer.Player1 ? 8 : 0;
Board[y, 0] = new Piece(WhichPiece.Lance, player);
Board[y, 1] = new Piece(WhichPiece.Knight, player);
Board[y, 2] = new Piece(WhichPiece.SilverGeneral, player);
Board[y, 3] = new Piece(WhichPiece.GoldGeneral, player);
Board[y, 4] = new Piece(WhichPiece.King, player);
Board[y, 5] = new Piece(WhichPiece.GoldGeneral, player);
Board[y, 6] = new Piece(WhichPiece.SilverGeneral, player);
Board[y, 7] = new Piece(WhichPiece.Knight, player);
Board[y, 8] = new Piece(WhichPiece.Lance, player);
}
private void InitializeBoardState()
{
ResetRearRow(WhichPlayer.Player2);
ResetMiddleRow(WhichPlayer.Player2);
ResetFrontRow(WhichPlayer.Player2);
ResetEmptyRows();
ResetFrontRow(WhichPlayer.Player1);
ResetMiddleRow(WhichPlayer.Player1);
ResetRearRow(WhichPlayer.Player1);
Board["A1"] = new Piece(WhichPiece.Lance, WhichPlayer.Player1);
Board["B1"] = new Piece(WhichPiece.Knight, WhichPlayer.Player1);
Board["C1"] = new Piece(WhichPiece.SilverGeneral, WhichPlayer.Player1);
Board["D1"] = new Piece(WhichPiece.GoldGeneral, WhichPlayer.Player1);
Board["E1"] = new Piece(WhichPiece.King, WhichPlayer.Player1);
Board["F1"] = new Piece(WhichPiece.GoldGeneral, WhichPlayer.Player1);
Board["G1"] = new Piece(WhichPiece.SilverGeneral, WhichPlayer.Player1);
Board["H1"] = new Piece(WhichPiece.Knight, WhichPlayer.Player1);
Board["I1"] = new Piece(WhichPiece.Lance, WhichPlayer.Player1);
Board["A2"] = null;
Board["B2"] = new Piece(WhichPiece.Bishop, WhichPlayer.Player1);
Board["C2"] = null;
Board["D2"] = null;
Board["E2"] = null;
Board["F2"] = null;
Board["G2"] = null;
Board["H2"] = new Piece(WhichPiece.Rook, WhichPlayer.Player1);
Board["I2"] = null;
Board["A3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["B3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["C3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["D3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["E3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["F3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["G3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["H3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["I3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
Board["A4"] = null;
Board["B4"] = null;
Board["C4"] = null;
Board["D4"] = null;
Board["E4"] = null;
Board["F4"] = null;
Board["G4"] = null;
Board["H4"] = null;
Board["I4"] = null;
Board["A5"] = null;
Board["B5"] = null;
Board["C5"] = null;
Board["D5"] = null;
Board["E5"] = null;
Board["F5"] = null;
Board["G5"] = null;
Board["H5"] = null;
Board["I5"] = null;
Board["A6"] = null;
Board["B6"] = null;
Board["C6"] = null;
Board["D6"] = null;
Board["E6"] = null;
Board["F6"] = null;
Board["G6"] = null;
Board["H6"] = null;
Board["I6"] = null;
Board["A7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["B7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["C7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["D7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["E7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["F7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["G7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["H7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["I7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
Board["A8"] = null;
Board["B8"] = new Piece(WhichPiece.Rook, WhichPlayer.Player2);
Board["C8"] = null;
Board["D8"] = null;
Board["E8"] = null;
Board["F8"] = null;
Board["G8"] = null;
Board["H8"] = new Piece(WhichPiece.Bishop, WhichPlayer.Player2);
Board["I8"] = null;
Board["A9"] = new Piece(WhichPiece.Lance, WhichPlayer.Player2);
Board["B9"] = new Piece(WhichPiece.Knight, WhichPlayer.Player2);
Board["C9"] = new Piece(WhichPiece.SilverGeneral, WhichPlayer.Player2);
Board["D9"] = new Piece(WhichPiece.GoldGeneral, WhichPlayer.Player2);
Board["E9"] = new Piece(WhichPiece.King, WhichPlayer.Player2);
Board["F9"] = new Piece(WhichPiece.GoldGeneral, WhichPlayer.Player2);
Board["G9"] = new Piece(WhichPiece.SilverGeneral, WhichPlayer.Player2);
Board["H9"] = new Piece(WhichPiece.Knight, WhichPlayer.Player2);
Board["I9"] = new Piece(WhichPiece.Lance, WhichPlayer.Player2);
}
#endregion
public BoardState ToServiceModel()
{
var board = new ServiceModels.Socket.Types.Piece[9, 9];
for (var x = 0; x < 9; x++)
for (var y = 0; y < 9; y++)
{
var piece = Board[y, x];
if (piece != null)
{
board[y, x] = piece.ToServiceModel();
}
}
return new BoardState
{
Board = board,
Board = Board.ToDictionary(kvp => kvp.Key, kvp => kvp.Value?.ToServiceModel()),
PlayerInCheck = InCheck,
WhoseTurn = WhoseTurn,
Player1Hand = Hands[WhichPlayer.Player1].Select(_ => _.ToServiceModel()).ToList(),
Player2Hand = Hands[WhichPlayer.Player2].Select(_ => _.ToServiceModel()).ToList()
Player1Hand = Player1Hand.Select(_ => _.ToServiceModel()).ToList(),
Player2Hand = Player2Hand.Select(_ => _.ToServiceModel()).ToList()
};
}
}