public async Task <ChessGameState> MovePieceAsync(string gameId, string playerName, string from, string to) { var dictActiveGames = await GetActiveGameDict(); var dictCompletedGames = await GetCompletedGameDict(); ChessGameState newGameState; var board = new Board(); using (var tx = StateManager.CreateTransaction()) { var game = await GetActiveGameAsync(tx, gameId); if (playerName != game.White.Name && playerName != game.Black.Name) { throw new ArgumentException("Player not in the game."); } board.PerformMoves(game.MoveHistory); var fromField = ChessGameUtils.FieldFromString(from); var toField = ChessGameUtils.FieldFromString(to); var piece = board[fromField.Item1, fromField.Item2]; if (!piece.MoveTo(toField.Item1, toField.Item2)) { throw new ArgumentException("Illegal move."); } var newGameInfo = new ChessGameInfo(game.GameId, game.White, game.Black, board.ToMovesString()); if (board.IsCheckmate || board.IsDraw) { await dictActiveGames.TryRemoveAsync(tx, gameId); await dictCompletedGames.TryAddAsync(tx, gameId, newGameInfo); } else { await dictActiveGames.TryUpdateAsync(tx, gameId, newGameInfo, game); } await tx.CommitAsync(); newGameState = new ChessGameState(newGameInfo); } var chessSignalRClient = proxyFactory.CreateServiceProxy <IChessFabrickSignalRService>(chessSignalRUri /*, ChessFabrickUtils.GuidPartitionKey(gameId)*/); chessSignalRClient.PieceMovedAsync(playerName, from, to, newGameState); if (newGameState.GetCurrentPlayer().IsBot() && !board.IsCheckmate && !board.IsDraw) { var actor = ActorProxy.Create <IChessFabrickActor>(new ActorId(gameId), chessActorUri); actor.PerformMove(); } return(newGameState); }
public void SetPieceChars(List <char> pieceChars) { var pieces = new List <Piece>(pieceChars.Count); foreach (var c in pieceChars) { pieces.Add(ChessGameUtils.FromChar(c)); } SetPieces(pieces); }
public async Task <List <string> > ListPieceMovesAsync(string gameId, string from) { Board board; using (var tx = StateManager.CreateTransaction()) { board = await GetActiveGameBoardAsync(tx, gameId); } var moves = new List <string>(); var fromField = ChessGameUtils.FieldFromString(from); var piece = board[fromField.Item1, fromField.Item2]; foreach (var move in piece.GetPossibleMoves()) { moves.Add(ChessGameUtils.FieldToString(move.Item1, move.Item2)); } return(moves); }
public ChessGameState(ChessGameInfo gameInfo) { GameInfo = gameInfo; var board = new Board(); board.PerformMoves(gameInfo.MoveHistory); var sb = new StringBuilder(); for (int i = 0; i < Board.SIZE; ++i) { for (int j = 0; j < Board.SIZE; ++j) { if (board[i, j] != null) { sb.Append(board[i, j].ToChar()); } sb.Append(','); } sb.Remove(sb.Length - 1, 1).Append(';'); } ChessBoard = sb.Remove(sb.Length - 1, 1).ToString(); CapturedPieces = new List <char>(); foreach (var piece in board.GetCaptured()) { CapturedPieces.Add(piece.ToChar()); } CheckingPieces = new List <string>(); foreach (var piece in board.CheckingPieces) { CheckingPieces.Add(ChessGameUtils.FieldToString(piece.X, piece.Y)); } IsDraw = board.IsDraw; IsCheckmate = board.IsCheckmate; OnTurn = board.TurnColor; }
private Tuple <string, string> GetRandomMove(Board board) { var pieces = board.GetAlive(board.TurnColor); while (true) { var piece = pieces[rand.Next(0, pieces.Count)]; var x = piece.X; var y = piece.Y; var moves = piece.GetPossibleMoves(); while (moves.Count > 0) { var move = moves[rand.Next(0, moves.Count)]; if (piece.MoveTo(move.Item1, move.Item2)) { return(Tuple.Create(ChessGameUtils.FieldToString(x, y), ChessGameUtils.FieldToString(move.Item1, move.Item2))); } moves.Remove(move); } pieces.Remove(piece); } }
private async void FieldBox_Click(object sender, EventArgs e) { var fieldBox = sender as ChessFieldBox; var field = fieldBox.Tag as Tuple <int, int>; showLastMove = false; if (selectedField == null) { selectedField = field; var selectedPiece = board[selectedField.Item1, selectedField.Item2]; if (selectedPiece?.Color != playerColor || selectedPiece?.Color != gameState.OnTurn) { selectedField = null; } if (selectedField != null) { fieldBox.Style = ChessFieldBox.BoxStyle.Selected; fieldBox.Invalidate(); try { possibleMoves = new List <Tuple <int, int> >(); var newPossibleMoves = new List <Tuple <int, int> >(); var moves = await connection.InvokeAsync <List <string> >("GetPieceMoves", gameId, ChessGameUtils.FieldToString(selectedField.Item1, selectedField.Item2) ); foreach (var move in moves) { newPossibleMoves.Add(ChessGameUtils.FieldFromString(move)); } possibleMoves = newPossibleMoves; } catch (HubException ex) { selectedField = null; possibleMoves = null; Console.Error.WriteLine(ex); txbMessage.Text = ex.Message; } } } else { if (possibleMoves.Contains(field)) { try { gameState = await connection.InvokeAsync <ChessGameState>("MovePiece", gameId, ChessGameUtils.FieldToString(selectedField.Item1, selectedField.Item2), ChessGameUtils.FieldToString(field.Item1, field.Item2) ); } catch (HubException ex) { Console.Error.WriteLine(ex); txbMessage.Text = ex.Message; } } selectedField = null; possibleMoves = null; } UpdateBoard(); }
private void RefreshViews() { if (board == null) { return; } labPlayerNames.Text = $"White: \n{gameState?.GameInfo?.White?.Name} \n\nBlack: \n{gameState?.GameInfo?.Black?.Name}"; if (gameState == null) { btnAddBot.Visible = true; return; } btnAddBot.Visible = false; if (gameState.IsCheckmate) { panTable.Enabled = false; grbPlaying.Text = "Checkmate"; cfbPlaying.Image = PieceImageUtils.King(gameState.OnTurn.Other()); if (playerColor != null) { cfbPlaying.BackColor = gameState.OnTurn != playerColor ? Color.YellowGreen : Color.OrangeRed; } } else if (gameState.IsDraw) { panTable.Enabled = false; grbPlaying.Text = "Draw"; cfbPlaying.Image = Properties.Resources.king_multi; cfbPlaying.BackColor = Control.DefaultBackColor; } else { panTable.Enabled = true; grbPlaying.Text = "On turn"; cfbPlaying.Image = PieceImageUtils.Pawn(gameState.OnTurn); if (playerColor != null) { cfbPlaying.BackColor = gameState.OnTurn == playerColor ? Color.YellowGreen : Control.DefaultBackColor; } } pieceListPanelBlack.SetPieceChars(gameState.CapturedPieces.Where(c => c >= 'a' && c <= 'z').ToList()); pieceListPanelWhite.SetPieceChars(gameState.CapturedPieces.Where(c => c >= 'A' && c <= 'Z').ToList()); for (int i = 0; i < 8; ++i) { for (int j = 0; j < 8; ++j) { var fieldBox = fieldBoxes[i, j]; fieldBox.SuspendLayout(); fieldBox.Style = ChessFieldBox.BoxStyle.None; fieldBox.Image = board[i, j]?.Image(); } } if (gameState.CheckingPieces?.Count > 0) { var king = board.King(board.TurnColor); fieldBoxes[king.X, king.Y].Style = ChessFieldBox.BoxStyle.Checked; foreach (var piece in gameState.CheckingPieces) { var field = ChessGameUtils.FieldFromString(piece); fieldBoxes[field.Item1, field.Item2].Style = ChessFieldBox.BoxStyle.Checking; } } if (selectedField != null) { fieldBoxes[selectedField.Item1, selectedField.Item2].Style = ChessFieldBox.BoxStyle.Selected; if (possibleMoves != null) { var selectedPiece = board[selectedField.Item1, selectedField.Item2]; foreach (var field in possibleMoves) { fieldBoxes[field.Item1, field.Item2].Style = board[field.Item1, field.Item2] != null || (selectedPiece is Pawn && selectedPiece.X != field.Item1) ? ChessFieldBox.BoxStyle.Attack : ChessFieldBox.BoxStyle.Move; } } } if (board.LastMove != null) { var lastMove = board.LastMove; if (lastMove.ConnectedMove) { lastMove = lastMove.LastMove; } labLastMove.Text = $"Last move:\n{ChessGameUtils.FieldToString(lastMove.FromX, lastMove.FromY)} => {ChessGameUtils.FieldToString(lastMove.ToX, lastMove.ToY)}"; if (showLastMove) { fieldBoxes[lastMove.FromX, lastMove.FromY].Style = ChessFieldBox.BoxStyle.MoveFrom; fieldBoxes[lastMove.ToX, lastMove.ToY].Style = lastMove.CapturedPiece != null ? ChessFieldBox.BoxStyle.MoveToCapture : ChessFieldBox.BoxStyle.MoveTo; } } foreach (var fieldBox in fieldBoxes) { fieldBox.ResumeLayout(false); fieldBox.Invalidate(); } }