private void btnSubmit_Click(object sender, EventArgs e) { if (NotChooseSomething()) { ShowMessage("Please choose mode, color and time"); return; } GameMode mode = this.GetGameMode(); PieceSide side = this.GetSide(); int time = this.GetTime(); this.gameManager.Mode = mode; this.gameManager.MySide = side; this.gameManager.Time = time * 60; int skin = this.GetSkin(); UpdatePieceSkin(skin); if (mode != GameMode.OnLan) { this.Hide(); var form1 = new Form1(); form1.Closed += (s, args) => this.Close(); form1.Show(); } else { this.Hide(); var form1 = new frmFindGame(); form1.Closed += (s, args) => this.Close(); form1.Show(); } }
public Move DoMove(BoardData board, PieceSide turn) { boardHelper.Config(firstCall, board, turn); Move move = MiniMaxAB(boardHelper, turn); return(move); }
public void CreatePiece( PlayerColor playerOwner, PieceType front, PieceType back, PieceSide side, int fileNum, int rankNum) { var piece = prefabsDictionary.Instantiate("Piece"); var pieceImpl = piece.GetComponent <PieceImpl>(); var pieceLocationMoveImpl = piece.GetComponent <PieceLocationMoveImpl>(); var pieceHighlightOwnerImpl = piece.GetComponent <PieceHighlightOwnerImpl>(); entityFactory.BuildEntity <PieceED>(piece.GetInstanceID(), piece.GetComponents <IImplementor>()); pieceImpl.Front = front; pieceImpl.Back = back; pieceImpl.PieceType = side == PieceSide.FRONT ? front : back; pieceImpl.Direction = DirectionService.CalcDirection(playerOwner); pieceHighlightOwnerImpl.PlayerColor = playerOwner; Vector2 location = CommonService.CalcTransformPosition(fileNum, rankNum); piece.transform.position = new Vector3(location.x, location.y, 1); pieceLocationMoveImpl.Location = new Vector2(fileNum, rankNum); pieceHighlightOwnerImpl.PlayChangeColor = true; }
/// <summary> /// Checks to see if the king for a player is in check. This function /// works by pretending the king is each of the different board pieces and seeing if it can attack /// any of the same type of price from its current position. /// </summary> /// <param name="b">Board state</param> /// <param name="king">the currnet player</param> /// <returns>Is in check</returns> public static bool isCheck(BoardHelper b, PieceSide king) { if (b.Kings.Count == 0) { return(true); } Position king_pos = b.Kings[king]; if (king_pos.number < 0 || king_pos.letter < 0) { return(true); } PieceType[] pieces = { PieceType.Pawn, PieceType.Rook, PieceType.Knight, PieceType.Bishop, PieceType.Queen, PieceType.King }; BoardHelper tempBoard = new BoardHelper(b); for (int i = 0; i < 6; i++) { tempBoard.Grid[king_pos.number][king_pos.letter] = new piece_t(pieces[i], king); List <Position> moves = getLegalMove(tempBoard, king_pos, false); foreach (var move in moves) { if (b.Grid[move.number][move.letter].piece == pieces[i] && b.Grid[move.number][move.letter].player != king) { return(true); } } } return(false); }
private void OnPressed(int entityId, PieceSide answer) { ModalEV modal = modalService.FindModalEV(entitiesDB); modal.Visibility.IsVisible.value = false; // Forcibly close confirm modal NextAction(modal, answer); }
public void Config(bool firstCall, BoardData board, PieceSide turn) { if (firstCall) { AI.GetInstance().UpdateFirstCall(false); Grid = new piece_t[Const.RowCount][]; for (int i = 0; i < Const.RowCount; i++) { Grid[i] = new piece_t[Const.ColCount]; for (int j = 0; j < Const.ColCount; j++) { if (board.ArrPiece[j, i] != null) { Grid[i][j] = new piece_t(board.ArrPiece[j, i].Type, board.ArrPiece[j, i].Side); } else { Grid[i][j] = new piece_t(PieceType.None, PieceSide.White); } } } LastMove = new Dictionary <PieceSide, Position>(); LastMove[PieceSide.Black] = new Position(); LastMove[PieceSide.White] = new Position(); Kings = new Dictionary <PieceSide, Position>(); Pieces = new Dictionary <PieceSide, List <Position> >(); List <Position> blackPos = new List <Position>(); List <Position> whitePos = new List <Position>(); for (int i = 0; i < Const.RowCount; i++) { for (int j = 0; j < Const.ColCount; j++) { Piece temp = BoardData.GetInstance().ArrPiece[j, i]; if (temp != null) { if (temp.Side == PieceSide.Black) { if (temp.Type == PieceType.King) { Kings[PieceSide.Black] = new Position(temp.Position.X, temp.Position.Y); } blackPos.Add(new Position(temp.Position.X, temp.Position.Y)); } else { if (temp.Type == PieceType.King) { Kings[PieceSide.White] = new Position(temp.Position.X, temp.Position.Y); } whitePos.Add(new Position(temp.Position.X, temp.Position.Y)); } } } } Pieces.Add(PieceSide.Black, blackPos); Pieces.Add(PieceSide.White, whitePos); } }
/// <summary> /// Slide along the path steps until you hit something. Return path to point and if it ends attacking with the attack. /// </summary> private static List <Position> Slide(BoardHelper board, PieceSide p, Position pos, Position step) { List <Position> moves = new List <Position>(); for (int i = 1; i < 8; i++) { Position moved = new Position(pos.letter + i * step.letter, pos.number + i * step.number); if (moved.letter < 0 || moved.letter > 7 || moved.number < 0 || moved.number > 7) { break; } if (board.Grid[moved.number][moved.letter].piece != PieceType.None) { if (board.Grid[moved.number][moved.letter].player != p) { moves.Add(moved); } break; } moves.Add(moved); } return(moves); }
public bool DoesSideConflict(PieceSide thisSide, string otherSideMaskString) { // Get the relevant side's mask string. Reverse it get it aligned with the other mask string. StringBuilder maskBuilder = new StringBuilder(); maskBuilder.Append(GetSideMask(thisSide).ToCharArray().Reverse().ToArray()); string thisSideMaskString = maskBuilder.ToString(); // Ensure our sideMasks if (thisSideMaskString.Length != 5 || otherSideMaskString.Length != 5) { throw new Exception(); } for (int i = 0; i < 5; i++) { if (thisSideMaskString[i] == '1' && otherSideMaskString[i] == '1') { return(true); } // Check the center 3 pegs to see if any are empty on both pieces. if (i >= 1 && i <= 3) { if (thisSideMaskString[i] == '0' && otherSideMaskString[i] == '0') { return(true); } } } return(false); }
public ChessPieceViewModel(ChessPiece i_ChessPiece, PieceColor i_PieceColor, PieceSide i_PieceSide, int i_File, int i_Rank) { ChessCoord tempChessCoord = new ChessCoord(); tempChessCoord.File = i_File; tempChessCoord.Rank = i_Rank; m_ChessPieceModel = new ChessPieceModel(i_ChessPiece, i_PieceColor, i_PieceSide, tempChessCoord); }
public void Update(Point src, Point des) { PieceSide turn = Grid[src.Y][src.X].player; // Update King if (Grid[src.Y][src.X].piece == PieceType.King) { Kings[turn] = new Position(des.X, des.Y); } // Update LastMove LastMove[turn] = new Position(des.X, des.Y); // Update Grid Grid[des.Y][des.X].lastPosition = new Position(src.Y, src.X); Grid[des.Y][des.X].piece = Grid[src.Y][src.X].piece; Grid[des.Y][des.X].player = Grid[src.Y][src.X].player; Grid[src.Y][src.X].piece = PieceType.None; // Update piece list for (int i = 0; i < Pieces[turn].Count; i++) { if (Pieces[turn][i].letter == src.X && Pieces[turn][i].number == src.Y) { Pieces[turn][i] = new Position(des.X, des.Y); break; } } }
private bool DoesNotViolateTerritoryDrop(ref DropPrepStepState token, PieceSide side, IEntitiesDB entitiesDB) { TurnEV turnPlayer = turnService.GetCurrentTurnEV(entitiesDB); return(!HasDropAbility(ref token.HandPiece, side, DropAbility.TERRITORY_DROP) || turnService.IsRankWithinTerritory(turnPlayer, token.DestinationTile.Location.Location.y)); }
public bool DropReleasesCheck( PieceEV pieceToDrop, Vector2 location, TurnEV turn, PieceSide side, IEntitiesDB entitiesDB) { bool returnValue = false; PieceEV?topPieceAtLocation = pieceFindService.FindTopPieceByLocation(location, entitiesDB); pieceSetService.SetTopOfTowerToFalse(topPieceAtLocation, entitiesDB); int tier = topPieceAtLocation.HasValue ? topPieceAtLocation.Value.Tier.Tier + 1 : 1; pieceSetService.SetPieceLocationAndTier(pieceToDrop, location, tier, entitiesDB); pieceSetService.SetPiecePlayerOwner(pieceToDrop, turn.TurnPlayer.PlayerColor, entitiesDB); pieceSetService.SetPieceSide(pieceToDrop, side, entitiesDB); returnValue = !IsCommanderInCheck(turn.TurnPlayer.PlayerColor, entitiesDB); pieceSetService.SetPieceLocationToHandLocation(pieceToDrop, entitiesDB); if (topPieceAtLocation.HasValue) { pieceSetService.SetTopOfTower(topPieceAtLocation.Value, entitiesDB); } return(returnValue); }
private bool DoesNotViolateTerritoryDrop( ref HandPieceEV handPiece, ref Vector2 location, PieceSide side, IEntitiesDB entitiesDB) { TurnEV turnPlayer = turnService.GetCurrentTurnEV(entitiesDB); return(!HasDropAbility(ref handPiece, side, DropAbility.TERRITORY_DROP) || turnService.IsRankWithinTerritory(turnPlayer, location.y)); }
private static bool allowCastle(BoardHelper board, PieceSide player, Position pos, bool isRight) { bool isValid = true; int rookPos; int kingDirection; if (isRight) { rookPos = 7; kingDirection = 1; } else { rookPos = 0; kingDirection = -1; } //Check for valid right castling // Is the peice at H,7 a rook owned by the player and has it moved if (board.Grid[pos.number][rookPos].piece == PieceType.Rook && board.Grid[pos.number][rookPos].player == player && board.Grid[pos.number][rookPos].lastPosition.Equals(new Position(-1, -1))) { // Check that the adjacent two squares are empty for (int i = 0; i < 2; i++) { if (board.Grid[pos.number][pos.letter + (i + 1) * kingDirection].piece != PieceType.None) { isValid = false; break; } } // Don't bother running secondary checks if the way isn't even clear if (isValid) { for (int i = 0; i < 2; i++) { // Move kings postion over i squares to check if king is passing over an attackable // square BoardHelper b2 = LegalMoveSet.move(board, new Move(pos, new Position(pos.letter + (i + 1) * kingDirection, pos.number))); // Attackable square is in between king and rook so // its not possible to castle to the right rook if (isCheck(b2, player)) { isValid = false; break; } } } } else { isValid = false; } return(isValid); }
public void SetPieceSide(PieceEV pieceEV, PieceSide side, IEntitiesDB entitiesDB) { entitiesDB.ExecuteOnEntity( pieceEV.ID, (ref PieceEV pieceToChange) => { pieceToChange.Piece.PieceType = side == PieceSide.FRONT ? pieceToChange.Piece.Front : pieceToChange.Piece.Back; }); }
private void NextAction(ModalEV modal, PieceSide answer) { var dropToken = new DropStepState { HandPiece = handService.FindHandPiece(modal.DropFrontBackModal.HandPieceReferenceId, entitiesDB), DestinationTile = tileService.FindTileEV(modal.DropFrontBackModal.TileReferenceId, entitiesDB), Side = modal.DropFrontBackModal.Answer.value }; dropModalAnswerSequence.Next(this, ref dropToken); }
private bool NoOtherSameTypesInFile(ref DropPrepStepState token, PieceSide side, IEntitiesDB entitiesDB) { PlayerColor playerColor = token.HandPiece.PlayerOwner.PlayerColor; return(pieceFindService.FindPiecesByTypeAndFile( GetPieceType(ref token.HandPiece, side), token.DestinationTile.Location.Location.x, entitiesDB ).Where(piece => piece.PlayerOwner.PlayerColor == playerColor).ToList() .Count == 0); }
internal void EndGame(PieceSide loseTurn) { this.SetBlockBoard(true); string side = loseTurn == PieceSide.Black ? "Black chess " : "White chess "; DialogResult res = MessageBox.Show(side + "is lose.\nPress OK to quit game", "End game", MessageBoxButtons.OK); if (res == DialogResult.OK) { this.Close(); } }
private void NextActionDrop(ref DropPrepStepState token, PieceSide pieceSide) { var dropToken = new DropStepState { DestinationTile = token.DestinationTile, HandPiece = token.HandPiece, Side = pieceSide }; dropSequence.Next(this, ref dropToken, (int)StepAB.B); }
internal override void SetTurn(PieceSide turn) { if (turn != this.gameManager.MySide) { this.gameManager.SetBlockBoard(true); } else { this.gameManager.SetBlockBoard(false); } }
/// <summary> /// Determine if the provided player has any valid moves. /// </summary> /// <param name="b">The state of the game.</param> /// <param name="player">The player.</param> /// <returns>True if the player has moves.</returns> public static bool hasMoves(BoardHelper b, PieceSide player) { foreach (Position pos in b.Pieces[player]) { if (b.Grid[pos.number][pos.letter].piece != PieceType.None && b.Grid[pos.number][pos.letter].player == player && getLegalMove(b, pos).Count > 0) { return(true); } } return(false); }
private void SwitchRunningClock(PieceSide turn) { if (turn == PieceSide.Black) { lblClocks[PieceSide.White].BackColor = Const.ColorPauseClock; lblClocks[PieceSide.Black].BackColor = Const.ColorRunningClock; } else { lblClocks[PieceSide.Black].BackColor = Const.ColorPauseClock; lblClocks[PieceSide.White].BackColor = Const.ColorRunningClock; } //TODO: run and pause clock }
public int fitness(PieceSide max) { int fitness = 0; int[] blackPieces = { 0, 0, 0, 0, 0, 0 }; int[] whitePieces = { 0, 0, 0, 0, 0, 0 }; int blackMoves = 0; int whiteMoves = 0; // sum up the number of moves and pieces foreach (Position pos in Pieces[PieceSide.Black]) { blackMoves += LegalMoveSet.getLegalMove(this, pos).Count; blackPieces[(int)Grid[pos.number][pos.letter].piece]++; } // sum up the number of moves and pieces foreach (Position pos in Pieces[PieceSide.White]) { whiteMoves += LegalMoveSet.getLegalMove(this, pos).Count; whitePieces[(int)Grid[pos.number][pos.letter].piece]++; } // if viewing from black side if (max == PieceSide.Black) { // apply weighting to piece counts for (int i = 0; i < 6; i++) { fitness += pieceWeights[i] * (blackPieces[i] - whitePieces[i]); } // apply move value fitness += (int)(0.5 * (blackMoves - whiteMoves)); } else { // apply weighting to piece counts for (int i = 0; i < 6; i++) { fitness += pieceWeights[i] * (whitePieces[i] - blackPieces[i]); } // apply move value fitness += (int)(0.5 * (whiteMoves - blackMoves)); } return(fitness); }
private bool DropReleasesCheck( PieceEV pieceToDrop, Vector2 location, PlayerColor playerColor, PieceSide side, HandPieceEV handPiece, DropCheckmateLevel recursionLevel, IEntitiesDB entitiesDB) { bool returnValue; PieceEV?topPieceAtLocation = pieceFindService.FindTopPieceByLocation(location, entitiesDB); pieceSetService.SetTopOfTowerToFalse(topPieceAtLocation, entitiesDB); int tier = topPieceAtLocation.HasValue ? topPieceAtLocation.Value.Tier.Tier + 1 : 1; pieceSetService.SetPieceLocationAndTier(pieceToDrop, location, tier, entitiesDB); pieceSetService.SetPiecePlayerOwner(pieceToDrop, playerColor, entitiesDB); pieceSetService.SetPieceSide(pieceToDrop, side, entitiesDB); handService.DecrementHandPiece(ref handPiece); PlayerColor enemyPlayerColor = TurnService.CalcOtherTurnPlayer(playerColor); returnValue = DropCheckmateNotViolated( pieceToDrop, location, playerColor, side, handPiece, recursionLevel, entitiesDB); if (returnValue) { returnValue = !checkService.IsCommanderInCheck(playerColor, entitiesDB); } handService.IncrementHandPiece(ref handPiece); pieceSetService.SetPieceLocationToHandLocation(pieceToDrop, entitiesDB); if (topPieceAtLocation.HasValue) { pieceSetService.SetTopOfTower(topPieceAtLocation.Value, entitiesDB); } return(returnValue); }
public ChessPieceModel(ChessPiece i_ChessPiece, PieceColor i_PieceColor, PieceSide i_PieceSide, ChessCoord i_ChessCoord) { ChessPiece = i_ChessPiece; PieceColor = i_PieceColor; PieceSide = i_PieceSide; ChessCoord = i_ChessCoord; string PieceColorStr; if (i_PieceColor == PieceColor.White) { PieceColorStr = "White"; } else { PieceColorStr = "Black"; } switch (i_ChessPiece) { case ChessPiece.King: ResourceURI = @"/ChessAI;component/Resources/Pieces/" + PieceColorStr + "_King.png"; break; case ChessPiece.Queen: ResourceURI = @"/ChessAI;component/Resources/Pieces/" + PieceColorStr + "_Queen.png"; break; case ChessPiece.Bishop: ResourceURI = @"/ChessAI;component/Resources/Pieces/" + PieceColorStr + "_Bishop.png"; break; case ChessPiece.Knight: ResourceURI = @"/ChessAI;component/Resources/Pieces/" + PieceColorStr + "_Knight.png"; break; case ChessPiece.Rook: ResourceURI = @"/ChessAI;component/Resources/Pieces/" + PieceColorStr + "_Rook.png"; break; case ChessPiece.Pawn: ResourceURI = @"/ChessAI;component/Resources/Pieces/" + PieceColorStr + "_Pawn.png"; break; } }
public bool DropReleasesCheck( PieceEV pieceToDrop, Vector2 location, PlayerColor playerColor, PieceSide side, HandPieceEV handPiece, IEntitiesDB entitiesDB) { return(DropReleasesCheck( pieceToDrop, location, playerColor, side, handPiece, DropCheckmateLevel.FIRST_TURN_PLAYER_CHECK, entitiesDB)); }
public string GetSideMask(PieceSide side) { switch (side) { case PieceSide.Bottom: return(BottomSideMaskString); case PieceSide.Left: return(LeftSideMaskString); case PieceSide.Right: return(RightSideMaskString); case PieceSide.Top: return(TopSideMaskString); default: throw new Exception(); } }
public Bitmap GetPieceResourceByType(PieceType type, PieceSide color) { PIECE pieceAdapt = PIECE.BlackKnight; if (color == PieceSide.Black) { switch (type) { case PieceType.Bishop: pieceAdapt = PIECE.BlackBishop; break; case PieceType.Rook: pieceAdapt = PIECE.BlackRook; break; case PieceType.Knight: pieceAdapt = PIECE.BlackKnight; break; case PieceType.Queen: pieceAdapt = PIECE.BlackQueen; break; case PieceType.King: pieceAdapt = PIECE.BlackKing; break; case PieceType.Pawn: pieceAdapt = PIECE.BlackPawn; break; } } else { switch (type) { case PieceType.Bishop: pieceAdapt = PIECE.WhiteBishop; break; case PieceType.Rook: pieceAdapt = PIECE.WhiteRook; break; case PieceType.Knight: pieceAdapt = PIECE.WhiteKnight; break; case PieceType.Queen: pieceAdapt = PIECE.WhiteQueen; break; case PieceType.King: pieceAdapt = PIECE.WhiteKing; break; case PieceType.Pawn: pieceAdapt = PIECE.WhitePawn; break; } } return(this.pieceStrategy.GetPiece(pieceAdapt)); }
public void DropPiece( ref PieceEV pieceToDrop, ref TileEV destinationTile, PieceSide side, PlayerColor playerOwner, IEntitiesDB entitiesDB) { Vector2 location = destinationTile.Location.Location; List <PieceEV> piecesAtLocation = pieceFindService.FindPiecesByLocation(location, entitiesDB); if (piecesAtLocation.Count > 0) { pieceSetService.SetTopOfTowerToFalse(piecesAtLocation[piecesAtLocation.Count - 1], entitiesDB); } pieceSetService.SetPiecePlayerOwner(pieceToDrop, playerOwner, entitiesDB); pieceSetService.SetPieceLocationAndTier(pieceToDrop, location, piecesAtLocation.Count + 1, entitiesDB); pieceSetService.SetPieceSide(pieceToDrop, side, entitiesDB); pieceToDrop.MovePiece.NewLocation = location; pieceToDrop.Visibility.IsVisible.value = true; pieceToDrop.ChangeColorTrigger.PlayChangeColor = true; }
private bool DropCheckmateNotViolated( PieceEV pieceToDrop, Vector2 location, PlayerColor playerColor, PieceSide side, HandPieceEV handPiece, DropCheckmateLevel recursionLevel, IEntitiesDB entitiesDB) { bool returnValue = true; PlayerColor enemyPlayerColor = TurnService.CalcOtherTurnPlayer(playerColor); if (recursionLevel != DropCheckmateLevel.FINAL_TURN_PLAYER_CHECK && // Prevent infinite recursion AbilityToPiece.HasAbility(DropAbility.CANNOT_DROP_CHECKMATE, pieceToDrop.Piece.PieceType) && checkService.IsCommanderInCheck(enemyPlayerColor, entitiesDB)) { recursionLevel = recursionLevel == DropCheckmateLevel.FIRST_TURN_PLAYER_CHECK ? DropCheckmateLevel.SECOND_ENEMY_PLAYER_CHECK : DropCheckmateLevel.FINAL_TURN_PLAYER_CHECK; returnValue = AnyValidMoves(enemyPlayerColor, entitiesDB, recursionLevel); } return(returnValue); }
private bool DoesNotViolateDoubleFileDrop(ref DropPrepStepState token, PieceSide side, IEntitiesDB entitiesDB) { return(!HasDropAbility(ref token.HandPiece, side, DropAbility.DOUBLE_FILE_DROP) || NoOtherSameTypesInFile(ref token, side, entitiesDB)); }
private bool DoesNotViolateForcedRearrangementFrontDrop(ref DropPrepStepState token, PieceSide side, IEntitiesDB entitiesDB) { TurnEV currentTurn = turnService.GetCurrentTurnEV(entitiesDB); return(currentTurn.ForcedRearrangementStatus.ForcedRearrangmentActive && side == PieceSide.BACK ? false : true); }