Fixed accidentally building the board from player2 perspective.

This commit is contained in:
2021-04-06 19:52:02 -05:00
parent 2d5c6b20b9
commit 05a9c71499
45 changed files with 441 additions and 276 deletions

View File

@@ -11,7 +11,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Gameboard.ShogiUI.BoardState\Gameboard.ShogiUI.BoardState.csproj" /> <ProjectReference Include="..\Gameboard.ShogiUI.BoardState\Gameboard.ShogiUI.Rules.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,7 +1,7 @@
using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines; using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Running; using BenchmarkDotNet.Running;
using Gameboard.ShogiUI.BoardState; using Gameboard.ShogiUI.Rules;
using System; using System;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;

View File

@@ -1,7 +1,7 @@
using System.Diagnostics; using System.Diagnostics;
using System.Numerics; using System.Numerics;
namespace Gameboard.ShogiUI.BoardState namespace Gameboard.ShogiUI.Rules
{ {
[DebuggerDisplay("{From} - {To}")] [DebuggerDisplay("{From} - {To}")]
public class Move public class Move

View File

@@ -1,7 +1,7 @@
using PathFinding; using PathFinding;
using System.Collections.Generic; using System.Collections.Generic;
namespace Gameboard.ShogiUI.BoardState.Pieces namespace Gameboard.ShogiUI.Rules.Pieces
{ {
public class Bishop : Piece public class Bishop : Piece
{ {

View File

@@ -1,7 +1,7 @@
using PathFinding; using PathFinding;
using System.Collections.Generic; using System.Collections.Generic;
namespace Gameboard.ShogiUI.BoardState.Pieces namespace Gameboard.ShogiUI.Rules.Pieces
{ {
public class GoldenGeneral : Piece public class GoldenGeneral : Piece
{ {

View File

@@ -1,7 +1,7 @@
using PathFinding; using PathFinding;
using System.Collections.Generic; using System.Collections.Generic;
namespace Gameboard.ShogiUI.BoardState.Pieces namespace Gameboard.ShogiUI.Rules.Pieces
{ {
public class King : Piece public class King : Piece
{ {

View File

@@ -1,7 +1,7 @@
using PathFinding; using PathFinding;
using System.Collections.Generic; using System.Collections.Generic;
namespace Gameboard.ShogiUI.BoardState.Pieces namespace Gameboard.ShogiUI.Rules.Pieces
{ {
public class Knight : Piece public class Knight : Piece
{ {

View File

@@ -1,7 +1,7 @@
using PathFinding; using PathFinding;
using System.Collections.Generic; using System.Collections.Generic;
namespace Gameboard.ShogiUI.BoardState.Pieces namespace Gameboard.ShogiUI.Rules.Pieces
{ {
public class Lance : Piece public class Lance : Piece
{ {

View File

@@ -1,7 +1,7 @@
using PathFinding; using PathFinding;
using System.Collections.Generic; using System.Collections.Generic;
namespace Gameboard.ShogiUI.BoardState.Pieces namespace Gameboard.ShogiUI.Rules.Pieces
{ {
public class Pawn : Piece public class Pawn : Piece
{ {

View File

@@ -1,7 +1,7 @@
using PathFinding; using PathFinding;
using System.Diagnostics; using System.Diagnostics;
namespace Gameboard.ShogiUI.BoardState.Pieces namespace Gameboard.ShogiUI.Rules.Pieces
{ {
[DebuggerDisplay("{WhichPiece} {Owner}")] [DebuggerDisplay("{WhichPiece} {Owner}")]
public abstract class Piece : IPlanarElement public abstract class Piece : IPlanarElement

View File

@@ -1,7 +1,7 @@
using PathFinding; using PathFinding;
using System.Collections.Generic; using System.Collections.Generic;
namespace Gameboard.ShogiUI.BoardState.Pieces namespace Gameboard.ShogiUI.Rules.Pieces
{ {
public class Rook : Piece public class Rook : Piece
{ {

View File

@@ -1,7 +1,7 @@
using PathFinding; using PathFinding;
using System.Collections.Generic; using System.Collections.Generic;
namespace Gameboard.ShogiUI.BoardState.Pieces namespace Gameboard.ShogiUI.Rules.Pieces
{ {
public class SilverGeneral : Piece public class SilverGeneral : Piece
{ {

View File

@@ -3,7 +3,7 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
namespace Gameboard.ShogiUI.BoardState namespace Gameboard.ShogiUI.Rules
{ {
public class PlanarCollection<T> : IPlanarCollection<T>, IEnumerable<T> where T : IPlanarElement public class PlanarCollection<T> : IPlanarCollection<T>, IEnumerable<T> where T : IPlanarElement
{ {

View File

@@ -1,10 +1,10 @@
using Gameboard.ShogiUI.BoardState.Pieces; using Gameboard.ShogiUI.Rules.Pieces;
using PathFinding; using PathFinding;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics; using System.Numerics;
namespace Gameboard.ShogiUI.BoardState namespace Gameboard.ShogiUI.Rules
{ {
/// <summary> /// <summary>
/// Facilitates Shogi board state transitions, cognisant of Shogi rules. /// Facilitates Shogi board state transitions, cognisant of Shogi rules.
@@ -14,17 +14,21 @@ namespace Gameboard.ShogiUI.BoardState
public class ShogiBoard public class ShogiBoard
{ {
private delegate void MoveSetCallback(Piece piece, Vector2 position); private delegate void MoveSetCallback(Piece piece, Vector2 position);
private readonly bool isValidationBoard;
private readonly PathFinder2D<Piece> pathFinder; private readonly PathFinder2D<Piece> pathFinder;
private ShogiBoard validationBoard; private ShogiBoard validationBoard;
private Vector2 player1King; private Vector2 player1King;
private Vector2 player2King; private Vector2 player2King;
public IReadOnlyDictionary<WhichPlayer, List<Piece>> Hands { get; } public IReadOnlyDictionary<WhichPlayer, List<Piece>> Hands { get; }
public PlanarCollection<Piece> Board { get; } public PlanarCollection<Piece> Board { get; } //TODO: Hide this being a getter method
public List<Move> MoveHistory { get; } public List<Move> MoveHistory { get; }
public WhichPlayer WhoseTurn => MoveHistory.Count % 2 == 0 ? WhichPlayer.Player1 : WhichPlayer.Player2; public WhichPlayer WhoseTurn => MoveHistory.Count % 2 == 0 ? WhichPlayer.Player1 : WhichPlayer.Player2;
public WhichPlayer? InCheck { get; private set; } public WhichPlayer? InCheck { get; private set; }
public bool IsCheckmate { get; private set; } public bool IsCheckmate { get; private set; }
public string Error { get; private set; }
public ShogiBoard() public ShogiBoard()
{ {
Board = new PlanarCollection<Piece>(9, 9); Board = new PlanarCollection<Piece>(9, 9);
@@ -35,8 +39,8 @@ namespace Gameboard.ShogiUI.BoardState
}; };
pathFinder = new PathFinder2D<Piece>(Board); pathFinder = new PathFinder2D<Piece>(Board);
InitializeBoardState(); InitializeBoardState();
player1King = new Vector2(4, 0); player1King = new Vector2(4, 8);
player2King = new Vector2(4, 8); player2King = new Vector2(4, 0);
} }
public ShogiBoard(IList<Move> moves) : this() public ShogiBoard(IList<Move> moves) : this()
@@ -46,13 +50,14 @@ namespace Gameboard.ShogiUI.BoardState
if (!Move(moves[i])) if (!Move(moves[i]))
{ {
// Todo: Add some smarts to know why a move was invalid. In check? Piece not found? etc. // Todo: Add some smarts to know why a move was invalid. In check? Piece not found? etc.
throw new InvalidOperationException($"Unable to construct ShogiBoard with the given move at index {i}."); throw new InvalidOperationException($"Unable to construct ShogiBoard with the given move at index {i}. {Error}");
} }
} }
} }
private ShogiBoard(ShogiBoard toCopy) private ShogiBoard(ShogiBoard toCopy)
{ {
isValidationBoard = true;
Board = new PlanarCollection<Piece>(9, 9); Board = new PlanarCollection<Piece>(9, 9);
for (var x = 0; x < 9; x++) for (var x = 0; x < 9; x++)
for (var y = 0; y < 9; y++) for (var y = 0; y < 9; y++)
@@ -143,8 +148,8 @@ namespace Gameboard.ShogiUI.BoardState
minimumY = WhoseTurn == WhichPlayer.Player1 ? 7 : 1; minimumY = WhoseTurn == WhichPlayer.Player1 ? 7 : 1;
break; break;
} }
if (WhoseTurn == WhichPlayer.Player1 && move.To.Y > minimumY) return false; if (WhoseTurn == WhichPlayer.Player1 && move.To.Y < minimumY) return false;
if (WhoseTurn == WhichPlayer.Player2 && move.To.Y < minimumY) return false; if (WhoseTurn == WhichPlayer.Player2 && move.To.Y > minimumY) return false;
// Mutate the board. // Mutate the board.
Board[move.To.X, move.To.Y] = Hands[WhoseTurn][index]; Board[move.To.X, move.To.Y] = Hands[WhoseTurn][index];
@@ -156,9 +161,21 @@ namespace Gameboard.ShogiUI.BoardState
private bool PlaceFromBoard(Move move) private bool PlaceFromBoard(Move move)
{ {
var fromPiece = Board[move.From.X, move.From.Y]; var fromPiece = Board[move.From.X, move.From.Y];
if (fromPiece == null) return false; // Invalid move if (fromPiece == null)
if (fromPiece.Owner != WhoseTurn) return false; // Invalid move; cannot move other players pieces. {
if (IsPathable(move.From, move.To) == false) return false; // Invalid move; move not part of move-set. Error = $"No piece exists at {nameof(move)}.{nameof(move.From)}.";
return false; // Invalid move
}
if (fromPiece.Owner != WhoseTurn)
{
Error = "Not allowed to move the opponents piece";
return false; // Invalid move; cannot move other players pieces.
}
if (IsPathable(move.From, move.To) == false)
{
Error = $"Illegal move for {fromPiece.WhichPiece}. {nameof(move)}.{nameof(move.To)} is not part of the move-set.";
return false; // Invalid move; move not part of move-set.
}
var captured = Board[move.To.X, move.To.Y]; var captured = Board[move.To.X, move.To.Y];
if (captured != null) if (captured != null)
@@ -171,11 +188,11 @@ namespace Gameboard.ShogiUI.BoardState
//Mutate the board. //Mutate the board.
if (move.IsPromotion) if (move.IsPromotion)
{ {
if (WhoseTurn == WhichPlayer.Player1 && (move.To.Y > 5 || move.From.Y > 5)) if (WhoseTurn == WhichPlayer.Player1 && (move.To.Y < 3 || move.From.Y < 3))
{ {
fromPiece.Promote(); fromPiece.Promote();
} }
else if (WhoseTurn == WhichPlayer.Player2 && (move.To.Y < 3 || move.From.Y < 3)) else if (WhoseTurn == WhichPlayer.Player2 && (move.To.Y > 5 || move.From.Y > 5))
{ {
fromPiece.Promote(); fromPiece.Promote();
} }
@@ -313,12 +330,12 @@ namespace Gameboard.ShogiUI.BoardState
} }
private void ResetFrontRow(WhichPlayer player) private void ResetFrontRow(WhichPlayer player)
{ {
int y = player == WhichPlayer.Player1 ? 2 : 6; int y = player == WhichPlayer.Player1 ? 6 : 2;
for (int x = 0; x < 9; x++) Board[x, y] = new Pawn(player); for (int x = 0; x < 9; x++) Board[x, y] = new Pawn(player);
} }
private void ResetMiddleRow(WhichPlayer player) private void ResetMiddleRow(WhichPlayer player)
{ {
int y = player == WhichPlayer.Player1 ? 1 : 7; int y = player == WhichPlayer.Player1 ? 7 : 1;
Board[0, y] = null; Board[0, y] = null;
for (int x = 2; x < 7; x++) Board[x, y] = null; for (int x = 2; x < 7; x++) Board[x, y] = null;
@@ -336,7 +353,7 @@ namespace Gameboard.ShogiUI.BoardState
} }
private void ResetRearRow(WhichPlayer player) private void ResetRearRow(WhichPlayer player)
{ {
int y = player == WhichPlayer.Player1 ? 0 : 8; int y = player == WhichPlayer.Player1 ? 8 : 0;
Board[0, y] = new Lance(player); Board[0, y] = new Lance(player);
Board[1, y] = new Knight(player); Board[1, y] = new Knight(player);
@@ -350,13 +367,13 @@ namespace Gameboard.ShogiUI.BoardState
} }
private void InitializeBoardState() private void InitializeBoardState()
{ {
ResetRearRow(WhichPlayer.Player1);
ResetMiddleRow(WhichPlayer.Player1);
ResetFrontRow(WhichPlayer.Player1);
ResetEmptyRows();
ResetFrontRow(WhichPlayer.Player2);
ResetMiddleRow(WhichPlayer.Player2);
ResetRearRow(WhichPlayer.Player2); ResetRearRow(WhichPlayer.Player2);
ResetMiddleRow(WhichPlayer.Player2);
ResetFrontRow(WhichPlayer.Player2);
ResetEmptyRows();
ResetFrontRow(WhichPlayer.Player1);
ResetMiddleRow(WhichPlayer.Player1);
ResetRearRow(WhichPlayer.Player1);
} }
#endregion #endregion
} }

View File

@@ -1,4 +1,4 @@
namespace Gameboard.ShogiUI.BoardState namespace Gameboard.ShogiUI.Rules
{ {
public enum WhichPiece public enum WhichPiece
{ {

View File

@@ -1,4 +1,4 @@
namespace Gameboard.ShogiUI.BoardState namespace Gameboard.ShogiUI.Rules
{ {
public enum WhichPlayer public enum WhichPlayer
{ {

View File

@@ -0,0 +1,6 @@
namespace Gameboard.ShogiUI.Domain
{
public class Board
{
}
}

View File

@@ -0,0 +1,30 @@
namespace Gameboard.ShogiUI.Domain
{
public class Match
{
public string Name { get; }
public string Player1 { get; }
public string Player2 { get; }
/// <summary>
/// Initialize pre-existing Match.
/// </summary>
public Match(MatchMeta meta, Board board)
{
Name = meta.Name;
Player1 = meta.Player1;
Player2 = meta.Player2;
}
/// <summary>
/// Create a new Match.
/// </summary>
public Match(string name, string player1)
{
Name = name;
Player1 = player1;
}
}
}

View File

@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Domain
{
public class MatchMeta
{
public string Name { get; }
public string Player1 { get; }
public string Player2 { get; }
}
}

View File

@@ -14,7 +14,7 @@ namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages
{ {
public string Action { get; private set; } public string Action { get; private set; }
public Game Game { get; set; } public Game Game { get; set; }
public IReadOnlyList<Move> Moves { get; set; } public BoardState BoardState { get; set; }
public string Error { get; set; } public string Error { get; set; }
public LoadGameResponse(ClientAction action) public LoadGameResponse(ClientAction action)

View File

@@ -15,7 +15,7 @@ namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages
public string Action { get; } public string Action { get; }
public string Error { get; set; } public string Error { get; set; }
public string GameName { get; set; } public string GameName { get; set; }
public Move Move { get; set; } public BoardState BoardState { get; set; }
public string PlayerName { get; set; } public string PlayerName { get; set; }
public MoveResponse(ClientAction action) public MoveResponse(ClientAction action)

View File

@@ -4,11 +4,6 @@
{ {
public WhichPiece WhichPiece { get; set; } public WhichPiece WhichPiece { get; set; }
/// <summary>
/// True if this piece is controlled by you.
/// </summary>
public bool IsControlledByMe { get; set; }
public bool IsPromoted { get; set; } public bool IsPromoted { get; set; }
} }
} }

View File

@@ -9,13 +9,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gameboard.ShogiUI.Sockets.S
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{F35A56FB-B8D8-4CB7-ABF6-D40049C9B42E}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{F35A56FB-B8D8-4CB7-ABF6-D40049C9B42E}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gameboard.ShogiUI.BoardState", "Gameboard.ShogiUI.BoardState\Gameboard.ShogiUI.BoardState.csproj", "{C5A7C4EF-549F-40A8-A0BD-DA2C7C0A6CF4}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gameboard.ShogiUI.Rules", "Gameboard.ShogiUI.BoardState\Gameboard.ShogiUI.Rules.csproj", "{C5A7C4EF-549F-40A8-A0BD-DA2C7C0A6CF4}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gameboard.ShogiUI.UnitTests", "Gameboard.ShogiUI.UnitTests\Gameboard.ShogiUI.UnitTests.csproj", "{DC8A933A-DBCB-46B9-AA0B-7B3DC9E763F3}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gameboard.ShogiUI.UnitTests", "Gameboard.ShogiUI.UnitTests\Gameboard.ShogiUI.UnitTests.csproj", "{DC8A933A-DBCB-46B9-AA0B-7B3DC9E763F3}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Benchmarking", "Benchmarking\Benchmarking.csproj", "{DADFF5D6-581F-4D69-845D-53ABD6ABF62F}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Benchmarking", "Benchmarking\Benchmarking.csproj", "{DADFF5D6-581F-4D69-845D-53ABD6ABF62F}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PathFinding", "PathFinding\PathFinding.csproj", "{A0AC8C5A-6ADA-45C6-BD1E-EB1061213E47}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PathFinding", "PathFinding\PathFinding.csproj", "{A0AC8C5A-6ADA-45C6-BD1E-EB1061213E47}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gameboard.ShogiUI.Domain", "Gameboard.ShogiUI.Domain\Gameboard.ShogiUI.Domain.csproj", "{2CB188B7-3EE8-44FB-9548-8C0CFBF7E40B}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -47,6 +49,10 @@ Global
{A0AC8C5A-6ADA-45C6-BD1E-EB1061213E47}.Debug|Any CPU.Build.0 = Debug|Any CPU {A0AC8C5A-6ADA-45C6-BD1E-EB1061213E47}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0AC8C5A-6ADA-45C6-BD1E-EB1061213E47}.Release|Any CPU.ActiveCfg = Release|Any CPU {A0AC8C5A-6ADA-45C6-BD1E-EB1061213E47}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0AC8C5A-6ADA-45C6-BD1E-EB1061213E47}.Release|Any CPU.Build.0 = Release|Any CPU {A0AC8C5A-6ADA-45C6-BD1E-EB1061213E47}.Release|Any CPU.Build.0 = Release|Any CPU
{2CB188B7-3EE8-44FB-9548-8C0CFBF7E40B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2CB188B7-3EE8-44FB-9548-8C0CFBF7E40B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2CB188B7-3EE8-44FB-9548-8C0CFBF7E40B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2CB188B7-3EE8-44FB-9548-8C0CFBF7E40B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@@ -31,7 +31,7 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
var isPlayer1 = await manager.IsPlayer1(request.SessionName, userName); var isPlayer1 = await manager.IsPlayer1(request.SessionName, userName);
if (isPlayer1) if (isPlayer1)
{ {
var code = (await repository.PostJoinCode(request.SessionName, userName)).JoinCode; var code = await repository.PostJoinCode(request.SessionName, userName);
return new CreatedResult("", new PostGameInvitationResponse(code)); return new CreatedResult("", new PostGameInvitationResponse(code));
} }
else else
@@ -49,7 +49,7 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
var isPlayer1 = manager.IsPlayer1(request.SessionName, request.GuestId); var isPlayer1 = manager.IsPlayer1(request.SessionName, request.GuestId);
if (isGuest && await isPlayer1) if (isGuest && await isPlayer1)
{ {
var code = (await repository.PostJoinCode(request.SessionName, request.GuestId)).JoinCode; var code = await repository.PostJoinCode(request.SessionName, request.GuestId);
return new CreatedResult("", new PostGameInvitationResponse(code)); return new CreatedResult("", new PostGameInvitationResponse(code));
} }
else else

View File

@@ -1,5 +1,4 @@
using Gameboard.ShogiUI.Sockets.Managers; using Gameboard.ShogiUI.Sockets.Managers;
using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers; using Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers;
using Gameboard.ShogiUI.Sockets.ServiceModels.Api.Messages; using Gameboard.ShogiUI.Sockets.ServiceModels.Api.Messages;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
@@ -15,16 +14,13 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
public class SocketController : ControllerBase public class SocketController : ControllerBase
{ {
private readonly ISocketTokenManager tokenManager; private readonly ISocketTokenManager tokenManager;
private readonly IGameboardRepository gameboardRepository;
private readonly IGameboardRepositoryManager gameboardManager; private readonly IGameboardRepositoryManager gameboardManager;
public SocketController( public SocketController(
ISocketTokenManager tokenManager, ISocketTokenManager tokenManager,
IGameboardRepository gameboardRepository,
IGameboardRepositoryManager gameboardManager) IGameboardRepositoryManager gameboardManager)
{ {
this.tokenManager = tokenManager; this.tokenManager = tokenManager;
this.gameboardRepository = gameboardRepository;
this.gameboardManager = gameboardManager; this.gameboardManager = gameboardManager;
} }
@@ -48,11 +44,10 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
} }
else else
{ {
var response = await gameboardRepository.GetPlayer(request.ClientId); if (await gameboardManager.PlayerExists(request.ClientId))
if (response != null && response.Player != null)
{ {
var token = tokenManager.GenerateToken(response.Player.Name); var token = tokenManager.GenerateToken(request.ClientId);
return new JsonResult(new GetGuestTokenResponse(response.Player.Name, token)); return new JsonResult(new GetGuestTokenResponse(request.ClientId, token));
} }
} }
return new UnauthorizedResult(); return new UnauthorizedResult();

View File

@@ -17,7 +17,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Gameboard.ShogiUI.BoardState\Gameboard.ShogiUI.BoardState.csproj" /> <ProjectReference Include="..\Gameboard.ShogiUI.BoardState\Gameboard.ShogiUI.Rules.csproj" />
<ProjectReference Include="..\Gameboard.ShogiUI.Sockets.ServiceModels\Gameboard.ShogiUI.Sockets.ServiceModels.csproj" /> <ProjectReference Include="..\Gameboard.ShogiUI.Sockets.ServiceModels\Gameboard.ShogiUI.Sockets.ServiceModels.csproj" />
</ItemGroup> </ItemGroup>

View File

@@ -1,4 +1,4 @@
using Gameboard.ShogiUI.BoardState; using Gameboard.ShogiUI.Rules;
using System.Collections.Concurrent; using System.Collections.Concurrent;
namespace Gameboard.ShogiUI.Sockets.Managers namespace Gameboard.ShogiUI.Sockets.Managers
@@ -26,10 +26,5 @@ namespace Gameboard.ShogiUI.Sockets.Managers
return board; return board;
return null; return null;
} }
public string GetBoardState()
{
return string.Empty;
}
} }
} }

View File

@@ -12,16 +12,13 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
// It can be an API route and still tell socket connections about the new session. // It can be an API route and still tell socket connections about the new session.
public class CreateGameHandler : IActionHandler public class CreateGameHandler : IActionHandler
{ {
private readonly ILogger<CreateGameHandler> logger;
private readonly IGameboardRepository repository; private readonly IGameboardRepository repository;
private readonly ISocketCommunicationManager communicationManager; private readonly ISocketCommunicationManager communicationManager;
public CreateGameHandler( public CreateGameHandler(
ILogger<CreateGameHandler> logger,
ISocketCommunicationManager communicationManager, ISocketCommunicationManager communicationManager,
IGameboardRepository repository) IGameboardRepository repository)
{ {
this.logger = logger;
this.repository = repository; this.repository = repository;
this.communicationManager = communicationManager; this.communicationManager = communicationManager;
} }
@@ -29,7 +26,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
public async Task Handle(string json, string userName) public async Task Handle(string json, string userName)
{ {
var request = JsonConvert.DeserializeObject<CreateGameRequest>(json); var request = JsonConvert.DeserializeObject<CreateGameRequest>(json);
var postSessionResponse = await repository.PostSession(new PostSession var sessionName = await repository.PostSession(new PostSession
{ {
SessionName = request.GameName, SessionName = request.GameName,
PlayerName = userName, PlayerName = userName,
@@ -41,12 +38,12 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
PlayerName = userName, PlayerName = userName,
Game = new Game Game = new Game
{ {
GameName = postSessionResponse.SessionName, GameName = sessionName,
Players = new[] { userName } Players = new[] { userName }
} }
}; };
if (string.IsNullOrWhiteSpace(postSessionResponse.SessionName)) if (string.IsNullOrWhiteSpace(sessionName))
{ {
response.Error = "Game already exists."; response.Error = "Game already exists.";
} }

View File

@@ -2,7 +2,6 @@
using Gameboard.ShogiUI.Sockets.Repositories; using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -10,16 +9,13 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{ {
public class JoinByCodeHandler : IActionHandler public class JoinByCodeHandler : IActionHandler
{ {
private readonly ILogger<JoinByCodeHandler> logger;
private readonly IGameboardRepository repository; private readonly IGameboardRepository repository;
private readonly ISocketCommunicationManager communicationManager; private readonly ISocketCommunicationManager communicationManager;
public JoinByCodeHandler( public JoinByCodeHandler(
ILogger<JoinByCodeHandler> logger,
ISocketCommunicationManager communicationManager, ISocketCommunicationManager communicationManager,
IGameboardRepository repository) IGameboardRepository repository)
{ {
this.logger = logger;
this.repository = repository; this.repository = repository;
this.communicationManager = communicationManager; this.communicationManager = communicationManager;
} }
@@ -27,38 +23,38 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
public async Task Handle(string json, string userName) public async Task Handle(string json, string userName)
{ {
var request = JsonConvert.DeserializeObject<JoinByCode>(json); var request = JsonConvert.DeserializeObject<JoinByCode>(json);
var joinGameResponse = await repository.PostJoinPrivateSession(new PostJoinPrivateSession var sessionName = await repository.PostJoinPrivateSession(new PostJoinPrivateSession
{ {
PlayerName = userName, PlayerName = userName,
JoinCode = request.JoinCode JoinCode = request.JoinCode
}); });
if (joinGameResponse.JoinSucceeded) if (sessionName == null)
{ {
// Other members of the game see a regular JoinGame occur. var response = new JoinGameResponse(ClientAction.JoinByCode)
var response = new JoinGameResponse(ClientAction.JoinGame)
{ {
PlayerName = userName, PlayerName = userName,
GameName = joinGameResponse.SessionName GameName = sessionName,
}; Error = "Error joining game."
// At this time, userName hasn't subscribed and won't receive this message.
await communicationManager.BroadcastToGame(joinGameResponse.SessionName, response);
// The player joining sees the JoinByCode occur.
response = new JoinGameResponse(ClientAction.JoinByCode)
{
PlayerName = userName,
GameName = joinGameResponse.SessionName
}; };
await communicationManager.BroadcastToPlayers(response, userName); await communicationManager.BroadcastToPlayers(response, userName);
} }
else else
{ {
var response = new JoinGameResponse(ClientAction.JoinByCode) // Other members of the game see a regular JoinGame occur.
var response = new JoinGameResponse(ClientAction.JoinGame)
{ {
PlayerName = userName, PlayerName = userName,
GameName = joinGameResponse.SessionName, GameName = sessionName
Error = "Error joining game." };
// At this time, userName hasn't subscribed and won't receive this message.
await communicationManager.BroadcastToGame(sessionName, response);
// The player joining sees the JoinByCode occur.
response = new JoinGameResponse(ClientAction.JoinByCode)
{
PlayerName = userName,
GameName = sessionName
}; };
await communicationManager.BroadcastToPlayers(response, userName); await communicationManager.BroadcastToPlayers(response, userName);
} }

View File

@@ -23,7 +23,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{ {
var request = JsonConvert.DeserializeObject<JoinGameRequest>(json); var request = JsonConvert.DeserializeObject<JoinGameRequest>(json);
var joinGameResponse = await gameboardRepository.PutJoinPublicSession(new PutJoinPublicSession var joinSucceeded = await gameboardRepository.PutJoinPublicSession(new PutJoinPublicSession
{ {
PlayerName = userName, PlayerName = userName,
SessionName = request.GameName SessionName = request.GameName
@@ -34,7 +34,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
PlayerName = userName, PlayerName = userName,
GameName = request.GameName GameName = request.GameName
}; };
if (joinGameResponse.JoinSucceeded) if (joinSucceeded)
{ {
await communicationManager.BroadcastToAll(response); await communicationManager.BroadcastToAll(response);
} }

View File

@@ -1,4 +1,4 @@
using Gameboard.ShogiUI.BoardState; using Gameboard.ShogiUI.Rules;
using Gameboard.ShogiUI.Sockets.Repositories; using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
@@ -37,9 +37,8 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
var gameTask = gameboardRepository.GetGame(request.GameName); var gameTask = gameboardRepository.GetGame(request.GameName);
var moveTask = gameboardRepository.GetMoves(request.GameName); var moveTask = gameboardRepository.GetMoves(request.GameName);
var getGameResponse = await gameTask; var sessionModel = await gameTask;
var getMovesResponse = await moveTask; if (sessionModel == null)
if (getGameResponse == null || getMovesResponse == null)
{ {
logger.LogWarning("{action} - {user} was unable to load session named {session}.", ClientAction.LoadGame, userName, request.GameName); logger.LogWarning("{action} - {user} was unable to load session named {session}.", ClientAction.LoadGame, userName, request.GameName);
var response = new LoadGameResponse(ClientAction.LoadGame) { Error = "Game not found." }; var response = new LoadGameResponse(ClientAction.LoadGame) { Error = "Game not found." };
@@ -47,17 +46,17 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
} }
else else
{ {
var sessionModel = new Models.Session(getGameResponse.Session); var moveModels = await moveTask;
var moveModels = getMovesResponse.Moves.Select(_ => new Models.Move(_)).ToList();
communicationManager.SubscribeToGame(sessionModel, userName); communicationManager.SubscribeToGame(sessionModel, userName);
var boardMoves = moveModels.Select(_ => _.ToBoardModel()).ToList(); var boardMoves = moveModels.Select(_ => _.ToBoardModel()).ToList();
boardManager.Add(getGameResponse.Session.Name, new ShogiBoard(boardMoves)); var shogiBoard = new ShogiBoard(boardMoves);
boardManager.Add(sessionModel.Name, shogiBoard);
var response = new LoadGameResponse(ClientAction.LoadGame) var response = new LoadGameResponse(ClientAction.LoadGame)
{ {
Game = sessionModel.ToServiceModel(), Game = sessionModel.ToServiceModel(),
Moves = moveModels.Select(_ => _.ToServiceModel()).ToList(), BoardState = new Models.BoardState(shogiBoard).ToServiceModel()
}; };
await communicationManager.BroadcastToPlayers(response, userName); await communicationManager.BroadcastToPlayers(response, userName);
} }

View File

@@ -5,6 +5,7 @@ using Newtonsoft.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
using Service = Gameboard.ShogiUI.Sockets.ServiceModels.Socket; using Service = Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{ {
public class MoveHandler : IActionHandler public class MoveHandler : IActionHandler
@@ -25,31 +26,40 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
public async Task Handle(string json, string userName) public async Task Handle(string json, string userName)
{ {
var request = JsonConvert.DeserializeObject<Service.Messages.MoveRequest>(json); var request = JsonConvert.DeserializeObject<Service.Messages.MoveRequest>(json);
// Basic move validation
if (request.Move.To.Equals(request.Move.From))
{
var error = new Service.Messages.ErrorResponse(Service.Types.ClientAction.Move)
{
Error = "Error: moving piece from tile to the same tile."
};
await communicationManager.BroadcastToPlayers(error, userName);
return;
}
var moveModel = new Move(request.Move); var moveModel = new Move(request.Move);
var board = boardManager.Get(request.GameName); var board = boardManager.Get(request.GameName);
if (board == null)
{
// TODO: Find a flow for this
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
{
Error = $"Game isn't loaded. Send a message with the {Service.Types.ClientAction.LoadGame} action first."
};
await communicationManager.BroadcastToPlayers(response, userName);
}
var boardMove = moveModel.ToBoardModel(); var boardMove = moveModel.ToBoardModel();
//board.Move() var moveSuccess = board.Move(boardMove);
if (moveSuccess)
{
await gameboardRepository.PostMove(request.GameName, new PostMove(moveModel.ToApiModel())); await gameboardRepository.PostMove(request.GameName, new PostMove(moveModel.ToApiModel()));
var boardState = new BoardState(board);
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move) var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
{ {
GameName = request.GameName, GameName = request.GameName,
PlayerName = userName, PlayerName = userName,
Move = moveModel.ToServiceModel() BoardState = boardState.ToServiceModel()
}; };
await communicationManager.BroadcastToGame(request.GameName, response); await communicationManager.BroadcastToGame(request.GameName, response);
} }
else
{
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
{
Error = "Invalid move."
};
await communicationManager.BroadcastToPlayers(response, userName);
}
}
} }
} }

View File

@@ -0,0 +1,40 @@
using Gameboard.ShogiUI.Rules;
using System.Collections.Generic;
using System.Linq;
using ServiceTypes = Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
namespace Gameboard.ShogiUI.Sockets.Models
{
public class BoardState
{
public Piece[,] Board { get; set; }
public IReadOnlyCollection<Piece> Player1Hand { get; set; }
public IReadOnlyCollection<Piece> Player2Hand { get; set; }
public BoardState(ShogiBoard shogi)
{
Board = new Piece[9, 9];
for (var x = 0; x < 9; x++)
for (var y = 0; y < 9; y++)
Board[x, y] = new Piece(shogi.Board[x, y]);
Player1Hand = shogi.Hands[WhichPlayer.Player1].Select(_ => new Piece(_)).ToList();
Player2Hand = shogi.Hands[WhichPlayer.Player2].Select(_ => new Piece(_)).ToList();
}
public ServiceTypes.BoardState ToServiceModel()
{
var board = new ServiceTypes.Piece[9, 9];
Board = new Piece[9, 9];
for (var x = 0; x < 9; x++)
for (var y = 0; y < 9; y++)
board[x, y] = Board[x, y].ToServiceModel();
return new ServiceTypes.BoardState
{
Board = board,
Player1Hand = Player1Hand.Select(_ => _.ToServiceModel()).ToList(),
Player2Hand = Player2Hand.Select(_ => _.ToServiceModel()).ToList()
};
}
}
}

View File

@@ -1,7 +1,8 @@
using Gameboard.ShogiUI.BoardState; using Gameboard.ShogiUI.Rules;
using Microsoft.FSharp.Core; using Microsoft.FSharp.Core;
using System; using System;
using System.Numerics; using System.Numerics;
using BoardStateMove = Gameboard.ShogiUI.Rules.Move;
using ShogiApi = Gameboard.Shogi.Api.ServiceModels.Types; using ShogiApi = Gameboard.Shogi.Api.ServiceModels.Types;
namespace Gameboard.ShogiUI.Sockets.Models namespace Gameboard.ShogiUI.Sockets.Models
@@ -13,7 +14,6 @@ namespace Gameboard.ShogiUI.Sockets.Models
public Coords To { get; set; } public Coords To { get; set; }
public bool IsPromotion { get; set; } public bool IsPromotion { get; set; }
public Move() { }
public Move(ServiceModels.Socket.Types.Move move) public Move(ServiceModels.Socket.Types.Move move)
{ {
From = Coords.FromBoardNotation(move.From); From = Coords.FromBoardNotation(move.From);
@@ -74,9 +74,9 @@ namespace Gameboard.ShogiUI.Sockets.Models
}; };
return target; return target;
} }
public BoardState.Move ToBoardModel() public BoardStateMove ToBoardModel()
{ {
return new BoardState.Move return new BoardStateMove
{ {
From = new Vector2(From.X, From.Y), From = new Vector2(From.X, From.Y),
IsPromotion = IsPromotion, IsPromotion = IsPromotion,

View File

@@ -0,0 +1,27 @@
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using BoardStatePiece = Gameboard.ShogiUI.Rules.Pieces.Piece;
namespace Gameboard.ShogiUI.Sockets.Models
{
public class Piece
{
public WhichPiece WhichPiece { get; set; }
public bool IsPromoted { get; set; }
public Piece(BoardStatePiece piece)
{
WhichPiece = (WhichPiece)piece.WhichPiece;
IsPromoted = piece.IsPromoted;
}
public ServiceModels.Socket.Types.Piece ToServiceModel()
{
return new ServiceModels.Socket.Types.Piece
{
IsPromoted = IsPromoted,
WhichPiece = WhichPiece
};
}
}
}

View File

@@ -0,0 +1,12 @@
namespace Gameboard.ShogiUI.Sockets.Models
{
public class Player
{
public string Name { get; }
public Player(string name)
{
Name = name;
}
}
}

View File

@@ -16,7 +16,7 @@
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }
}, },
"AspShogiSockets": { "Kestrel": {
"commandName": "Project", "commandName": "Project",
"launchUrl": "Socket/Token", "launchUrl": "Socket/Token",
"environmentVariables": { "environmentVariables": {

View File

@@ -1,7 +1,10 @@
using Gameboard.Shogi.Api.ServiceModels.Messages; using Gameboard.Shogi.Api.ServiceModels.Messages;
using Gameboard.ShogiUI.Sockets.Models;
using Gameboard.ShogiUI.Sockets.Repositories.Utility; using Gameboard.ShogiUI.Sockets.Repositories.Utility;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -11,17 +14,17 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
public interface IGameboardRepository public interface IGameboardRepository
{ {
Task DeleteGame(string gameName); Task DeleteGame(string gameName);
Task<GetSessionResponse> GetGame(string gameName); Task<Session> GetGame(string gameName);
Task<GetSessionsResponse> GetGames(); Task<GetSessionsResponse> GetGames();
Task<GetSessionsResponse> GetGames(string playerName); Task<GetSessionsResponse> GetGames(string playerName);
Task<GetMovesResponse> GetMoves(string gameName); Task<List<Move>> GetMoves(string gameName);
Task<PostSessionResponse> PostSession(PostSession request); Task<string> PostSession(PostSession request);
Task<PostJoinPrivateSessionResponse> PostJoinPrivateSession(PostJoinPrivateSession request); Task<string> PostJoinPrivateSession(PostJoinPrivateSession request);
Task<PutJoinPublicSessionResponse> PutJoinPublicSession(PutJoinPublicSession request); Task<bool> PutJoinPublicSession(PutJoinPublicSession request);
Task PostMove(string gameName, PostMove request); Task PostMove(string gameName, PostMove request);
Task<PostJoinCodeResponse> PostJoinCode(string gameName, string userName); Task<string> PostJoinCode(string gameName, string userName);
Task<GetPlayerResponse> GetPlayer(string userName); Task<Player> GetPlayer(string userName);
Task<HttpResponseMessage> PostPlayer(PostPlayer request); Task<bool> PostPlayer(PostPlayer request);
} }
public class GameboardRepository : IGameboardRepository public class GameboardRepository : IGameboardRepository
@@ -52,12 +55,16 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
return JsonConvert.DeserializeObject<GetSessionsResponse>(json); return JsonConvert.DeserializeObject<GetSessionsResponse>(json);
} }
public async Task<GetSessionResponse> GetGame(string gameName) public async Task<Session> GetGame(string gameName)
{ {
var uri = $"Session/{gameName}"; var uri = $"Session/{gameName}";
var response = await client.GetAsync(Uri.EscapeUriString(uri)); var response = await client.GetAsync(Uri.EscapeUriString(uri));
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<GetSessionResponse>(json); if (string.IsNullOrWhiteSpace(json))
{
return null;
}
return new Session(JsonConvert.DeserializeObject<GetSessionResponse>(json).Session);
} }
public async Task DeleteGame(string gameName) public async Task DeleteGame(string gameName)
@@ -66,36 +73,46 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
await client.DeleteAsync(Uri.EscapeUriString(uri)); await client.DeleteAsync(Uri.EscapeUriString(uri));
} }
public async Task<PostSessionResponse> PostSession(PostSession request) public async Task<string> PostSession(PostSession request)
{ {
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType); var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
var response = await client.PostAsync(PostSessionRoute, content); var response = await client.PostAsync(PostSessionRoute, content);
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<PostSessionResponse>(json); return JsonConvert.DeserializeObject<PostSessionResponse>(json).SessionName;
} }
public async Task<PutJoinPublicSessionResponse> PutJoinPublicSession(PutJoinPublicSession request) public async Task<bool> PutJoinPublicSession(PutJoinPublicSession request)
{ {
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType); var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
var response = await client.PutAsync(JoinSessionRoute, content); var response = await client.PutAsync(JoinSessionRoute, content);
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<PutJoinPublicSessionResponse>(json); return JsonConvert.DeserializeObject<PutJoinPublicSessionResponse>(json).JoinSucceeded;
} }
public async Task<PostJoinPrivateSessionResponse> PostJoinPrivateSession(PostJoinPrivateSession request) public async Task<string> PostJoinPrivateSession(PostJoinPrivateSession request)
{ {
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType); var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
var response = await client.PostAsync(JoinSessionRoute, content); var response = await client.PostAsync(JoinSessionRoute, content);
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<PostJoinPrivateSessionResponse>(json); var deserialized = JsonConvert.DeserializeObject<PostJoinPrivateSessionResponse>(json);
if (deserialized.JoinSucceeded)
{
return deserialized.SessionName;
}
return null;
} }
public async Task<GetMovesResponse> GetMoves(string gameName) public async Task<List<Move>> GetMoves(string gameName)
{ {
var uri = $"Session/{gameName}/Moves"; var uri = $"Session/{gameName}/Moves";
var response = await client.GetAsync(Uri.EscapeUriString(uri)); var get = await client.GetAsync(Uri.EscapeUriString(uri));
var json = await response.Content.ReadAsStringAsync(); var json = await get.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<GetMovesResponse>(json); if (string.IsNullOrWhiteSpace(json))
{
return new List<Move>();
}
var response = JsonConvert.DeserializeObject<GetMovesResponse>(json);
return response.Moves.Select(m => new Move(m)).ToList();
} }
public async Task PostMove(string gameName, PostMove request) public async Task PostMove(string gameName, PostMove request)
@@ -105,27 +122,33 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
await client.PostAsync(Uri.EscapeUriString(uri), content); await client.PostAsync(Uri.EscapeUriString(uri), content);
} }
public async Task<PostJoinCodeResponse> PostJoinCode(string gameName, string userName) public async Task<string> PostJoinCode(string gameName, string userName)
{ {
var uri = $"JoinCode/{gameName}"; var uri = $"JoinCode/{gameName}";
var serialized = JsonConvert.SerializeObject(new PostJoinCode { PlayerName = userName }); var serialized = JsonConvert.SerializeObject(new PostJoinCode { PlayerName = userName });
var content = new StringContent(serialized, Encoding.UTF8, MediaType); var content = new StringContent(serialized, Encoding.UTF8, MediaType);
var json = await (await client.PostAsync(Uri.EscapeUriString(uri), content)).Content.ReadAsStringAsync(); var json = await (await client.PostAsync(Uri.EscapeUriString(uri), content)).Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<PostJoinCodeResponse>(json); return JsonConvert.DeserializeObject<PostJoinCodeResponse>(json).JoinCode;
} }
public async Task<GetPlayerResponse> GetPlayer(string playerName) public async Task<Player> GetPlayer(string playerName)
{ {
var uri = $"Player/{playerName}"; var uri = $"Player/{playerName}";
var response = await client.GetAsync(Uri.EscapeUriString(uri)); var get = await client.GetAsync(Uri.EscapeUriString(uri));
var json = await response.Content.ReadAsStringAsync(); var content = await get.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<GetPlayerResponse>(json); if (!string.IsNullOrWhiteSpace(content))
{
var response = JsonConvert.DeserializeObject<GetPlayerResponse>(content);
return new Player(response.Player.Name);
}
return null;
} }
public async Task<HttpResponseMessage> PostPlayer(PostPlayer request) public async Task<bool> PostPlayer(PostPlayer request)
{ {
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType); var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
return await client.PostAsync(PlayerRoute, content); var response = await client.PostAsync(PlayerRoute, content);
return response.IsSuccessStatusCode;
} }
} }
} }

View File

@@ -9,6 +9,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers
Task<string> CreateGuestUser(); Task<string> CreateGuestUser();
Task<bool> IsPlayer1(string sessionName, string playerName); Task<bool> IsPlayer1(string sessionName, string playerName);
bool IsGuest(string playerName); bool IsGuest(string playerName);
Task<bool> PlayerExists(string playerName);
} }
public class GameboardRepositoryManager : IGameboardRepositoryManager public class GameboardRepositoryManager : IGameboardRepositoryManager
@@ -33,8 +34,8 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers
{ {
PlayerName = clientId PlayerName = clientId
}; };
var response = await repository.PostPlayer(request); var isCreated = await repository.PostPlayer(request);
if (response.IsSuccessStatusCode) if (isCreated)
{ {
return clientId; return clientId;
} }
@@ -45,19 +46,21 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers
public async Task<bool> IsPlayer1(string sessionName, string playerName) public async Task<bool> IsPlayer1(string sessionName, string playerName)
{ {
var session = await repository.GetGame(sessionName); var session = await repository.GetGame(sessionName);
return session?.Session.Player1 == playerName; return session?.Player1 == playerName;
} }
public async Task<string> CreateJoinCode(string sessionName, string playerName) public async Task<string> CreateJoinCode(string sessionName, string playerName)
{ {
var getGameResponse = await repository.GetGame(sessionName); var session = await repository.GetGame(sessionName);
if (playerName == getGameResponse?.Session.Player1) if (playerName == session?.Player1)
{ {
return (await repository.PostJoinCode(sessionName, playerName)).JoinCode; return await repository.PostJoinCode(sessionName, playerName);
} }
return null; return null;
} }
public bool IsGuest(string playerName) => playerName.StartsWith(GuestPrefix); public bool IsGuest(string playerName) => playerName.StartsWith(GuestPrefix);
public async Task<bool> PlayerExists(string playerName) => await repository.GetPlayer(playerName) != null;
} }
} }

View File

@@ -1,10 +1,10 @@
using Gameboard.ShogiUI.BoardState; using Gameboard.ShogiUI.Rules;
using Gameboard.ShogiUI.BoardState.Pieces; using Gameboard.ShogiUI.Rules.Pieces;
using System; using System;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace Gameboard.ShogiUI.UnitTests.BoardState namespace Gameboard.ShogiUI.UnitTests.Rules
{ {
public static class BoardStateExtensions public static class BoardStateExtensions
{ {
@@ -32,7 +32,7 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
var builder = new StringBuilder(); var builder = new StringBuilder();
builder.Append(" Player 2(.)"); builder.Append(" Player 2(.)");
builder.AppendLine(); builder.AppendLine();
for (var y = 8; y > -1; y--) for (var y = 0; y < 9; y++)
{ {
builder.Append("- "); builder.Append("- ");
for (var x = 0; x < 8; x++) builder.Append("- - "); for (var x = 0; x < 8; x++) builder.Append("- - ");

View File

@@ -1,12 +1,12 @@
using FluentAssertions; using FluentAssertions;
using Gameboard.ShogiUI.BoardState; using Gameboard.ShogiUI.Rules;
using Gameboard.ShogiUI.BoardState.Pieces; using Gameboard.ShogiUI.Rules.Pieces;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using System; using System;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
namespace Gameboard.ShogiUI.UnitTests.BoardState namespace Gameboard.ShogiUI.UnitTests.Rules
{ {
[TestClass] [TestClass]
public class ShogiBoardShould public class ShogiBoardShould
@@ -22,7 +22,7 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
// Assert Player1. // Assert Player1.
for (var y = 0; y < 3; y++) for (var y = 0; y < 3; y++)
for (var x = 0; x < 9; x++) for (var x = 0; x < 9; x++)
board[x, y]?.Owner.Should().Be(WhichPlayer.Player1); board[x, y]?.Owner.Should().Be(WhichPlayer.Player2);
board[0, 0].WhichPiece.Should().Be(WhichPiece.Lance); board[0, 0].WhichPiece.Should().Be(WhichPiece.Lance);
board[1, 0].WhichPiece.Should().Be(WhichPiece.Knight); board[1, 0].WhichPiece.Should().Be(WhichPiece.Knight);
board[2, 0].WhichPiece.Should().Be(WhichPiece.SilverGeneral); board[2, 0].WhichPiece.Should().Be(WhichPiece.SilverGeneral);
@@ -33,9 +33,9 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
board[7, 0].WhichPiece.Should().Be(WhichPiece.Knight); board[7, 0].WhichPiece.Should().Be(WhichPiece.Knight);
board[8, 0].WhichPiece.Should().Be(WhichPiece.Lance); board[8, 0].WhichPiece.Should().Be(WhichPiece.Lance);
board[0, 1].Should().BeNull(); board[0, 1].Should().BeNull();
board[1, 1].WhichPiece.Should().Be(WhichPiece.Bishop); board[1, 1].WhichPiece.Should().Be(WhichPiece.Rook);
for (var x = 2; x < 7; x++) board[x, 1].Should().BeNull(); for (var x = 2; x < 7; x++) board[x, 1].Should().BeNull();
board[7, 1].WhichPiece.Should().Be(WhichPiece.Rook); board[7, 1].WhichPiece.Should().Be(WhichPiece.Bishop);
board[8, 1].Should().BeNull(); board[8, 1].Should().BeNull();
for (var x = 0; x < 9; x++) board[x, 2].WhichPiece.Should().Be(WhichPiece.Pawn); for (var x = 0; x < 9; x++) board[x, 2].WhichPiece.Should().Be(WhichPiece.Pawn);
@@ -47,7 +47,7 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
// Assert Player2. // Assert Player2.
for (var y = 6; y < 9; y++) for (var y = 6; y < 9; y++)
for (var x = 0; x < 9; x++) for (var x = 0; x < 9; x++)
board[x, y]?.Owner.Should().Be(WhichPlayer.Player2); board[x, y]?.Owner.Should().Be(WhichPlayer.Player1);
board[0, 8].WhichPiece.Should().Be(WhichPiece.Lance); board[0, 8].WhichPiece.Should().Be(WhichPiece.Lance);
board[1, 8].WhichPiece.Should().Be(WhichPiece.Knight); board[1, 8].WhichPiece.Should().Be(WhichPiece.Knight);
board[2, 8].WhichPiece.Should().Be(WhichPiece.SilverGeneral); board[2, 8].WhichPiece.Should().Be(WhichPiece.SilverGeneral);
@@ -58,9 +58,9 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
board[7, 8].WhichPiece.Should().Be(WhichPiece.Knight); board[7, 8].WhichPiece.Should().Be(WhichPiece.Knight);
board[8, 8].WhichPiece.Should().Be(WhichPiece.Lance); board[8, 8].WhichPiece.Should().Be(WhichPiece.Lance);
board[0, 7].Should().BeNull(); board[0, 7].Should().BeNull();
board[1, 7].WhichPiece.Should().Be(WhichPiece.Rook); board[1, 7].WhichPiece.Should().Be(WhichPiece.Bishop);
for (var x = 2; x < 7; x++) board[x, 7].Should().BeNull(); for (var x = 2; x < 7; x++) board[x, 7].Should().BeNull();
board[7, 7].WhichPiece.Should().Be(WhichPiece.Bishop); board[7, 7].WhichPiece.Should().Be(WhichPiece.Rook);
board[8, 7].Should().BeNull(); board[8, 7].Should().BeNull();
for (var x = 0; x < 9; x++) board[x, 6].WhichPiece.Should().Be(WhichPiece.Pawn); for (var x = 0; x < 9; x++) board[x, 6].WhichPiece.Should().Be(WhichPiece.Pawn);
} }
@@ -73,13 +73,13 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
new Move new Move
{ {
// Pawn // Pawn
From = new Vector2(0, 2), From = new Vector2(0, 6),
To = new Vector2(0, 3) To = new Vector2(0, 5)
} }
}; };
var shogi = new ShogiBoard(moves); var shogi = new ShogiBoard(moves);
shogi.Board[0, 2].Should().BeNull(); shogi.Board[0, 6].Should().BeNull();
shogi.Board[0, 3].WhichPiece.Should().Be(WhichPiece.Pawn); shogi.Board[0, 5].WhichPiece.Should().Be(WhichPiece.Pawn);
} }
[TestMethod] [TestMethod]
@@ -106,11 +106,11 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
var shogi = new ShogiBoard(); var shogi = new ShogiBoard();
// Act - P1 "moves" pawn to the position it already exists at. // Act - P1 "moves" pawn to the position it already exists at.
var moveSuccess = shogi.Move(new Move { From = new Vector2(0, 2), To = new Vector2(0, 2) }); var moveSuccess = shogi.Move(new Move { From = new Vector2(0, 6), To = new Vector2(0, 6) });
// Assert // Assert
moveSuccess.Should().BeFalse(); moveSuccess.Should().BeFalse();
shogi.Board[0, 2].WhichPiece.Should().Be(WhichPiece.Pawn); shogi.Board[0, 6].WhichPiece.Should().Be(WhichPiece.Pawn);
} }
[TestMethod] [TestMethod]
@@ -136,9 +136,11 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
{ {
// Arrange // Arrange
var shogi = new ShogiBoard(); var shogi = new ShogiBoard();
shogi.WhoseTurn.Should().Be(WhichPlayer.Player1);
shogi.Board[8, 2].Owner.Should().Be(WhichPlayer.Player2);
// Act - Move Player2 Pawn when it's Player1 turn. // Act - Move Player2 Pawn when it's Player1 turn.
var moveSuccess = shogi.Move(new Move { From = new Vector2(8, 6), To = new Vector2(8, 5) }); var moveSuccess = shogi.Move(new Move { From = new Vector2(8, 2), To = new Vector2(8, 3) });
// Assert // Assert
moveSuccess.Should().BeFalse(); moveSuccess.Should().BeFalse();
@@ -152,8 +154,8 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
var invalidLanceMove = new Move var invalidLanceMove = new Move
{ {
// Lance moving through the pawn before it. // Lance moving through the pawn before it.
From = new Vector2(0, 0), From = new Vector2(0, 8),
To = new Vector2(0, 5) To = new Vector2(0, 4)
}; };
var shogi = new ShogiBoard(); var shogi = new ShogiBoard();
@@ -170,8 +172,8 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
var invalidKnightMove = new Move var invalidKnightMove = new Move
{ {
// Knight capturing allied Pawn // Knight capturing allied Pawn
From = new Vector2(1, 0), From = new Vector2(1, 8),
To = new Vector2(0, 2) To = new Vector2(0, 6)
}; };
var shogi = new ShogiBoard(); var shogi = new ShogiBoard();
@@ -190,11 +192,11 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
var moves = new[] var moves = new[]
{ {
// P1 Pawn // P1 Pawn
new Move { From = new Vector2(2, 2), To = new Vector2(2, 3) }, new Move { From = new Vector2(2, 6), To = new Vector2(2, 5) },
// P2 Pawn // P2 Pawn
new Move { From = new Vector2(6, 6), To = new Vector2(6, 5) }, new Move { From = new Vector2(6, 2), To = new Vector2(6, 3) },
// P1 Bishop puts P2 in check // P1 Bishop puts P2 in check
new Move { From = new Vector2(1, 1), To = new Vector2(6, 6) } new Move { From = new Vector2(1, 7), To = new Vector2(6, 2) }
}; };
var shogi = new ShogiBoard(moves); var shogi = new ShogiBoard(moves);
@@ -202,7 +204,7 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
shogi.InCheck.Should().Be(WhichPlayer.Player2); shogi.InCheck.Should().Be(WhichPlayer.Player2);
// Act - P2 moves Lance while remaining in check. // Act - P2 moves Lance while remaining in check.
var moveSuccess = shogi.Move(new Move { From = new Vector2(8, 8), To = new Vector2(8, 7) }); var moveSuccess = shogi.Move(new Move { From = new Vector2(0, 8), To = new Vector2(0, 7) });
// Assert // Assert
moveSuccess.Should().BeFalse(); moveSuccess.Should().BeFalse();
@@ -218,61 +220,62 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
var moves = new[] var moves = new[]
{ {
// P1 Pawn // P1 Pawn
new Move { From = new Vector2(2, 2), To = new Vector2(2, 3) }, new Move { From = new Vector2(2, 6), To = new Vector2(2, 5) },
// P2 Pawn // P2 Pawn
new Move { From = new Vector2(0, 6), To = new Vector2(0, 5) }, new Move { From = new Vector2(0, 2), To = new Vector2(0, 3) },
// P1 Bishop takes P2 Pawn // P1 Bishop takes P2 Pawn
new Move { From = new Vector2(1, 1), To = new Vector2(6, 6) }, new Move { From = new Vector2(1, 7), To = new Vector2(6, 2) },
// P2 Gold, block check from P1 Bishop. // P2 Gold, block check from P1 Bishop.
new Move { From = new Vector2(5, 8), To = new Vector2(5, 7) }, new Move { From = new Vector2(5, 0), To = new Vector2(5, 1) },
// P1 Bishop takes P2 Bishop, promotes so it can capture P2 Knight and P2 Lance // P1 Bishop takes P2 Bishop, promotes so it can capture P2 Knight and P2 Lance
new Move { From = new Vector2(6, 6), To = new Vector2(7, 7), IsPromotion = true }, new Move { From = new Vector2(6, 2), To = new Vector2(7, 1), IsPromotion = true },
// P2 Pawn again // P2 Pawn again
new Move { From = new Vector2(0, 5), To = new Vector2(0, 4) }, new Move { From = new Vector2(0, 3), To = new Vector2(0, 4) },
// P1 Bishop takes P2 Knight // P1 Bishop takes P2 Knight
new Move { From = new Vector2(7, 7), To = new Vector2(7, 8) }, new Move { From = new Vector2(7, 1), To = new Vector2(7, 0) },
// P2 Pawn again // P2 Pawn again
new Move { From = new Vector2(0, 4), To = new Vector2(0, 3) }, new Move { From = new Vector2(0, 4), To = new Vector2(0, 5) },
// P1 Bishop takes P2 Lance // P1 Bishop takes P2 Lance
new Move { From = new Vector2(7, 8), To = new Vector2(8, 8) }, new Move { From = new Vector2(7, 0), To = new Vector2(8, 0) },
// P2 Lance (move to make room for attempted P1 Pawn placement) // P2 Lance (move to make room for attempted P1 Pawn placement)
new Move { From = new Vector2(0, 8), To = new Vector2(0, 7) }, new Move { From = new Vector2(0, 0), To = new Vector2(0, 1) },
// P1 arbitrary move // P1 arbitrary move
new Move { From = new Vector2(4, 0), To = new Vector2(4, 1) }, new Move { From = new Vector2(4, 8), To = new Vector2(4, 7) },
// P2 Pawn again, takes P1 Pawn // P2 Pawn again, takes P1 Pawn
new Move { From = new Vector2(0, 3), To = new Vector2(0, 2) }, new Move { From = new Vector2(0, 5), To = new Vector2(0, 6) },
}; };
var shogi = new ShogiBoard(moves); var shogi = new ShogiBoard(moves);
shogi.PrintStateAsAscii();
// Prerequisites // Prerequisites
shogi.Hands[WhichPlayer.Player1].Count.Should().Be(4);
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight); shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight);
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance); shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn); shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn);
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
// Act | Assert - It is P1 turn // Act | Assert - It is P1 turn
/// try illegally placing Knight from the hand. /// try illegally placing Knight from the hand.
shogi.Board[7, 8].Should().BeNull(); shogi.Board[7, 0].Should().BeNull();
var dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Knight, To = new Vector2(7, 8) }); var dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Knight, To = new Vector2(7, 0) });
dropSuccess.Should().BeFalse(); dropSuccess.Should().BeFalse();
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance); shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
shogi.Board[7, 8].Should().BeNull(); shogi.Board[7, 0].Should().BeNull();
dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Knight, To = new Vector2(7, 7) }); dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Knight, To = new Vector2(7, 1) });
dropSuccess.Should().BeFalse(); dropSuccess.Should().BeFalse();
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance); shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
shogi.Board[7, 7].Should().BeNull(); shogi.Board[7, 1].Should().BeNull();
/// try illegally placing Pawn from the hand /// try illegally placing Pawn from the hand
dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Pawn, To = new Vector2(7, 8) }); dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Pawn, To = new Vector2(7, 0) });
dropSuccess.Should().BeFalse(); dropSuccess.Should().BeFalse();
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn); shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn);
shogi.Board[7, 8].Should().BeNull(); shogi.Board[7, 0].Should().BeNull();
/// try illegally placing Lance from the hand /// try illegally placing Lance from the hand
dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Lance, To = new Vector2(7, 8) }); dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Lance, To = new Vector2(7, 0) });
dropSuccess.Should().BeFalse(); dropSuccess.Should().BeFalse();
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance); shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
shogi.Board[7, 8].Should().BeNull(); shogi.Board[7, 0].Should().BeNull();
} }
[TestMethod] [TestMethod]
@@ -282,25 +285,25 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
var moves = new[] var moves = new[]
{ {
// P1 Pawn // P1 Pawn
new Move { From = new Vector2(2, 2), To = new Vector2(2, 3) }, new Move { From = new Vector2(2, 6), To = new Vector2(2, 5) },
// P2 Pawn // P2 Pawn
new Move { From = new Vector2(8, 6), To = new Vector2(8, 5) }, new Move { From = new Vector2(8, 2), To = new Vector2(8, 3) },
// P1 Bishop, check // P1 Bishop, check
new Move { From = new Vector2(1, 1), To = new Vector2(6, 6) }, new Move { From = new Vector2(1, 7), To = new Vector2(6, 2) },
// P2 Gold, block check // P2 Gold, block check
new Move { From = new Vector2(5, 8), To = new Vector2(5, 7) }, new Move { From = new Vector2(5, 0), To = new Vector2(5, 1) },
// P1 arbitrary move // P1 arbitrary move
new Move { From = new Vector2(0, 2), To = new Vector2(0, 3) }, new Move { From = new Vector2(0, 6), To = new Vector2(0, 5) },
// P2 Bishop // P2 Bishop
new Move { From = new Vector2(7, 7), To = new Vector2(8, 6) }, new Move { From = new Vector2(7, 1), To = new Vector2(8, 2) },
// P1 Bishop takes P2 Lance // P1 Bishop takes P2 Lance
new Move { From = new Vector2(6, 6), To = new Vector2(8, 8) }, new Move { From = new Vector2(6, 2), To = new Vector2(8, 0) },
// P2 Bishop // P2 Bishop
new Move { From = new Vector2(8, 6), To = new Vector2(7, 7) }, new Move { From = new Vector2(8, 2), To = new Vector2(7, 1) },
// P1 arbitrary move // P1 arbitrary move
new Move { From = new Vector2(0, 3), To = new Vector2(0, 4) }, new Move { From = new Vector2(0, 5), To = new Vector2(0, 4) },
// P2 Bishop, check // P2 Bishop, check
new Move { From = new Vector2(7, 7), To = new Vector2(2, 2) }, new Move { From = new Vector2(7, 1), To = new Vector2(2, 6) },
}; };
var shogi = new ShogiBoard(moves); var shogi = new ShogiBoard(moves);
@@ -325,22 +328,23 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
var moves = new[] var moves = new[]
{ {
// P1 Pawn // P1 Pawn
new Move { From = new Vector2(2, 2), To = new Vector2(2, 3) }, new Move { From = new Vector2(2, 6), To = new Vector2(2, 5) },
// P2 Pawn // P2 Pawn
new Move { From = new Vector2(6, 6), To = new Vector2(6, 5) }, new Move { From = new Vector2(6, 2), To = new Vector2(6, 3) },
// P1 Bishop, capture P2 Pawn, check // P1 Bishop, capture P2 Pawn, check
new Move { From = new Vector2(1, 1), To = new Vector2(6, 6) }, new Move { From = new Vector2(1, 7), To = new Vector2(6, 2) },
// P2 Gold, block check // P2 Gold, block check
new Move { From = new Vector2(5, 8), To = new Vector2(5, 7) }, new Move { From = new Vector2(5, 0), To = new Vector2(5, 1) },
// P1 Bishop capture P2 Bishop // P1 Bishop capture P2 Bishop
new Move { From = new Vector2(6, 6), To = new Vector2(7, 7) }, new Move { From = new Vector2(6, 2), To = new Vector2(7, 1) },
// P2 arbitrary move // P2 arbitrary move
new Move { From = new Vector2(0, 8), To = new Vector2(0, 7) }, new Move { From = new Vector2(0, 0), To = new Vector2(0, 1) },
}; };
var shogi = new ShogiBoard(moves); var shogi = new ShogiBoard(moves);
// Prerequisites // Prerequisites
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop); shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.Board[4, 0].Should().NotBeNull();
// Act - P1 tries to place Bishop from hand to an already-occupied position // Act - P1 tries to place Bishop from hand to an already-occupied position
var dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Bishop, To = new Vector2(4, 0) }); var dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Bishop, To = new Vector2(4, 0) });
@@ -358,16 +362,14 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
var moves = new[] var moves = new[]
{ {
// P1 Pawn // P1 Pawn
new Move { From = new Vector2(2, 2), To = new Vector2(2, 3) }, new Move { From = new Vector2(2, 6), To = new Vector2(2, 5) },
// P2 Pawn // P2 Pawn
new Move { From = new Vector2(6, 6), To = new Vector2(6, 5) }, new Move { From = new Vector2(6, 2), To = new Vector2(6, 3) },
}; };
var shogi = new ShogiBoard(moves); var shogi = new ShogiBoard(moves);
shogi.PrintStateAsAscii();
// Act - P1 Bishop, check // Act - P1 Bishop, check
shogi.Move(new Move { From = new Vector2(1, 1), To = new Vector2(6, 6) }); shogi.Move(new Move { From = new Vector2(1, 7), To = new Vector2(6, 2) });
// Assert // Assert
shogi.InCheck.Should().Be(WhichPlayer.Player2); shogi.InCheck.Should().Be(WhichPlayer.Player2);
@@ -380,14 +382,14 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
var moves = new[] var moves = new[]
{ {
// P1 Pawn // P1 Pawn
new Move { From = new Vector2(2, 2), To = new Vector2(2, 3) }, new Move { From = new Vector2(2, 6), To = new Vector2(2, 5) },
// P2 Pawn // P2 Pawn
new Move { From = new Vector2(6, 6), To = new Vector2(6, 5) } new Move { From = new Vector2(6, 2), To = new Vector2(6, 3) }
}; };
var shogi = new ShogiBoard(moves); var shogi = new ShogiBoard(moves);
// Act - P1 Bishop captures P2 Bishop // Act - P1 Bishop captures P2 Bishop
var moveSuccess = shogi.Move(new Move { From = new Vector2(1, 1), To = new Vector2(7, 7) }); var moveSuccess = shogi.Move(new Move { From = new Vector2(1, 7), To = new Vector2(7, 1) });
// Assert // Assert
moveSuccess.Should().BeTrue(); moveSuccess.Should().BeTrue();
@@ -396,20 +398,20 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
.Count(piece => piece?.WhichPiece == WhichPiece.Bishop) .Count(piece => piece?.WhichPiece == WhichPiece.Bishop)
.Should() .Should()
.Be(1); .Be(1);
shogi.Board[1, 1].Should().BeNull(); shogi.Board[1, 7].Should().BeNull();
shogi.Board[7, 7].WhichPiece.Should().Be(WhichPiece.Bishop); shogi.Board[7, 1].WhichPiece.Should().Be(WhichPiece.Bishop);
shogi.Hands[WhichPlayer.Player1] shogi.Hands[WhichPlayer.Player1]
.Should() .Should()
.ContainSingle(piece => piece.WhichPiece == WhichPiece.Bishop && piece.Owner == WhichPlayer.Player1); .ContainSingle(piece => piece.WhichPiece == WhichPiece.Bishop && piece.Owner == WhichPlayer.Player1);
// Act - P2 Silver captures P1 Bishop // Act - P2 Silver captures P1 Bishop
moveSuccess = shogi.Move(new Move { From = new Vector2(6, 8), To = new Vector2(7, 7) }); moveSuccess = shogi.Move(new Move { From = new Vector2(6, 0), To = new Vector2(7, 1) });
// Assert // Assert
moveSuccess.Should().BeTrue(); moveSuccess.Should().BeTrue();
shogi.Board[6, 8].Should().BeNull(); shogi.Board[6, 0].Should().BeNull();
shogi.Board[7, 7].WhichPiece.Should().Be(WhichPiece.SilverGeneral); shogi.Board[7, 1].WhichPiece.Should().Be(WhichPiece.SilverGeneral);
shogi.Board shogi.Board
.Cast<Piece>() .Cast<Piece>()
.Count(piece => piece?.WhichPiece == WhichPiece.Bishop) .Count(piece => piece?.WhichPiece == WhichPiece.Bishop)
@@ -426,19 +428,19 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
var moves = new[] var moves = new[]
{ {
// P1 Pawn // P1 Pawn
new Move { From = new Vector2(2, 2), To = new Vector2(2, 3) }, new Move { From = new Vector2(2, 6), To = new Vector2(2, 5) },
// P2 Pawn // P2 Pawn
new Move { From = new Vector2(6, 6), To = new Vector2(6, 5) } new Move { From = new Vector2(6, 2), To = new Vector2(6, 3) }
}; };
var shogi = new ShogiBoard(moves); var shogi = new ShogiBoard(moves);
// Act - P1 moves across promote threshold. // Act - P1 moves across promote threshold.
var moveSuccess = shogi.Move(new Move { From = new Vector2(1, 1), To = new Vector2(6, 6), IsPromotion = true }); var moveSuccess = shogi.Move(new Move { From = new Vector2(1, 7), To = new Vector2(6, 2), IsPromotion = true });
// Assert // Assert
moveSuccess.Should().BeTrue(); moveSuccess.Should().BeTrue();
shogi.Board[1, 1].Should().BeNull(); shogi.Board[1, 7].Should().BeNull();
shogi.Board[6, 6].Should().Match<Piece>(piece => piece.WhichPiece == WhichPiece.Bishop && piece.IsPromoted == true); shogi.Board[6, 2].Should().Match<Piece>(piece => piece.WhichPiece == WhichPiece.Bishop && piece.IsPromoted == true);
} }
[TestMethod] [TestMethod]
@@ -448,32 +450,30 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
var moves = new[] var moves = new[]
{ {
// P1 Rook // P1 Rook
new Move { From = new Vector2(7, 1), To = new Vector2(4, 1) }, new Move { From = new Vector2(7, 7), To = new Vector2(4, 7) },
// P2 Gold // P2 Gold
new Move { From = new Vector2(3, 8), To = new Vector2(2, 7) }, new Move { From = new Vector2(3, 0), To = new Vector2(2, 1) },
// P1 Pawn // P1 Pawn
new Move { From = new Vector2(4, 2), To = new Vector2(4, 3) },
// P2 other Gold
new Move { From = new Vector2(5, 8), To = new Vector2(6, 7) },
// P1 same Pawn
new Move { From = new Vector2(4, 3), To = new Vector2(4, 4) },
// P2 Pawn
new Move { From = new Vector2(4, 6), To = new Vector2(4, 5) }, new Move { From = new Vector2(4, 6), To = new Vector2(4, 5) },
// P2 other Gold
new Move { From = new Vector2(5, 0), To = new Vector2(6, 1) },
// P1 same Pawn
new Move { From = new Vector2(4, 5), To = new Vector2(4, 4) },
// P2 Pawn
new Move { From = new Vector2(4, 2), To = new Vector2(4, 3) },
// P1 Pawn takes P2 Pawn // P1 Pawn takes P2 Pawn
new Move { From = new Vector2(4, 4), To = new Vector2(4, 5) }, new Move { From = new Vector2(4, 4), To = new Vector2(4, 3) },
// P2 King // P2 King
new Move { From = new Vector2(4, 8), To = new Vector2(4, 7) }, new Move { From = new Vector2(4, 0), To = new Vector2(4, 1) },
// P1 Pawn promotes, threatens P2 King // P1 Pawn promotes, threatens P2 King
new Move { From = new Vector2(4, 5), To = new Vector2(4, 6), IsPromotion = true }, new Move { From = new Vector2(4, 3), To = new Vector2(4, 2), IsPromotion = true },
// P2 King retreat // P2 King retreat
new Move { From = new Vector2(4, 7), To = new Vector2(4, 8) }, new Move { From = new Vector2(4, 1), To = new Vector2(4, 0) },
}; };
var shogi = new ShogiBoard(moves); var shogi = new ShogiBoard(moves);
Console.WriteLine("Prereq");
shogi.PrintStateAsAscii();
// Act - P1 Pawn wins by checkmate. // Act - P1 Pawn wins by checkmate.
var moveSuccess = shogi.Move(new Move { From = new Vector2(4, 6), To = new Vector2(4, 7) }); var moveSuccess = shogi.Move(new Move { From = new Vector2(4, 2), To = new Vector2(4, 1) });
// Assert - checkmate // Assert - checkmate
moveSuccess.Should().BeTrue(); moveSuccess.Should().BeTrue();

View File

@@ -4,15 +4,15 @@ namespace PathFinding
{ {
public static class Direction public static class Direction
{ {
public static readonly Vector2 Up = new(0, 1); public static readonly Vector2 Up = new(0, -1);
public static readonly Vector2 Down = new(0, -1); public static readonly Vector2 Down = new(0, 1);
public static readonly Vector2 Left = new(-1, 0); public static readonly Vector2 Left = new(-1, 0);
public static readonly Vector2 Right = new(1, 0); public static readonly Vector2 Right = new(1, 0);
public static readonly Vector2 UpLeft = new(-1, 1); public static readonly Vector2 UpLeft = new(-1, -1);
public static readonly Vector2 UpRight = new(1, 1); public static readonly Vector2 UpRight = new(1, -1);
public static readonly Vector2 DownLeft = new(-1, -1); public static readonly Vector2 DownLeft = new(-1, 1);
public static readonly Vector2 DownRight = new(1, -1); public static readonly Vector2 DownRight = new(1, 1);
public static readonly Vector2 KnightLeft = new(-1, 2); public static readonly Vector2 KnightLeft = new(-1, -2);
public static readonly Vector2 KnightRight = new(1, 2); public static readonly Vector2 KnightRight = new(1, -2);
} }
} }

View File

@@ -6,8 +6,6 @@ namespace PathFinding
{ {
public class PathFinder2D<T> where T : IPlanarElement public class PathFinder2D<T> where T : IPlanarElement
{ {
/// <summary>
/// </summary>
/// <param name="element">Guaranteed to be non-null.</param> /// <param name="element">Guaranteed to be non-null.</param>
/// <param name="position"></param> /// <param name="position"></param>
public delegate void Callback(T collider, Vector2 position); public delegate void Callback(T collider, Vector2 position);
@@ -108,14 +106,8 @@ namespace PathFinding
public static Move FindDirectionTowardsDestination(ICollection<Move> paths, Vector2 origin, Vector2 destination) => public static Move FindDirectionTowardsDestination(ICollection<Move> paths, Vector2 origin, Vector2 destination) =>
paths.Aggregate((a, b) => Vector2.Distance(destination, Vector2.Add(origin, a.Direction)) < Vector2.Distance(destination, Vector2.Add(origin, b.Direction)) ? a : b); paths.Aggregate((a, b) => Vector2.Distance(destination, Vector2.Add(origin, a.Direction)) < Vector2.Distance(destination, Vector2.Add(origin, b.Direction)) ? a : b);
public static bool IsPathable(Vector2 origin, Vector2 destination, T element)
{
var path = FindDirectionTowardsDestination(element.MoveSet.GetMoves(), origin, destination);
return IsPathable(origin, destination, path.Direction);
}
public static bool IsPathable(Vector2 origin, Vector2 destination, Vector2 direction) public static bool IsPathable(Vector2 origin, Vector2 destination, Vector2 direction)
{ {
direction = Vector2.Normalize(direction);
var next = Vector2.Add(origin, direction); var next = Vector2.Add(origin, direction);
if (Vector2.Distance(next, destination) >= Vector2.Distance(origin, destination)) return false; if (Vector2.Distance(next, destination) >= Vector2.Distance(origin, destination)) return false;