public bool IsSourceSquareOccupied(Board i_Board, int i_ISource, int i_JSource) { bool isSourceSquareOccupied = true; if (i_Board.GetBoard[i_ISource, i_JSource].Equals(" ")) { isSourceSquareOccupied = false; } return isSourceSquareOccupied; }
public bool IsKing(Board i_Board, int i_ISource, int i_JSource) { bool isKing = false; if (i_Board.GetBoard[i_ISource, i_JSource].Equals("K") || i_Board.GetBoard[i_ISource, i_JSource].Equals("U")) { isKing = true; } return isKing; }
public bool IsInputStepLegit(Board i_Board, int i_ISource, int i_JSource, int i_IDestination, int i_JDestination) { bool isInputStepLegit = false; if (i_Board.GetBoard[i_ISource, i_JSource].Equals("X")) { if (i_ISource - i_IDestination == 1) { if (Math.Abs(i_JDestination - i_JSource) == 1) { isInputStepLegit = true; } } } else if (i_Board.GetBoard[i_ISource, i_JSource].Equals("O")) { if (i_IDestination - i_ISource == 1) { if (Math.Abs(i_JDestination - i_JSource) == 1) { isInputStepLegit = true; } } } else if (IsKing(i_Board, i_ISource, i_JSource)) { if (Math.Abs(i_IDestination - i_ISource) == 1) { if (Math.Abs(i_JDestination - i_JSource) == 1) { isInputStepLegit = true; } } } return isInputStepLegit; }
public int CountTroop(Board i_Board, string i_Troop) { int count = 0; string kingToCount; if (i_Troop.Equals("X")) { kingToCount = "K"; } else { kingToCount = "U"; } for (int i = 0; i < i_Board.BoardSize; i++) { for (int j = 0; j < i_Board.BoardSize; j++) { if (i_Troop == i_Board.GetBoard[i, j] || kingToCount == i_Board.GetBoard[i, j]) { if (IsKing(i_Board, i, j)) { count = count + 4; } else { count++; } } } } return count; }
public bool IsDraw(Board i_Board) { string troop; bool isAnyOtherStepAvailable1, isAnyOtherStepAvailable2; isAnyOtherStepAvailable1 = IsAnyOtherStepAvailable(i_Board, out troop); SwitchTurn(); isAnyOtherStepAvailable2 = IsAnyOtherStepAvailable(i_Board, out troop); SwitchTurn(); return !isAnyOtherStepAvailable1 && !isAnyOtherStepAvailable2 && !IsAnyOtherJumpsAvailable(i_Board); }
public void Move(ref Board i_Board, int i_ISource, int i_JSource, int i_IDestination, int i_JDestination) { if (i_Board.GetBoard[i_ISource, i_JSource].Equals("X") && i_IDestination == 0) { i_Board.GetBoard[i_IDestination, i_JDestination] = "K"; } else if (i_Board.GetBoard[i_ISource, i_JSource].Equals("O") && i_IDestination == i_Board.BoardSize - 1) { i_Board.GetBoard[i_IDestination, i_JDestination] = "U"; } else { i_Board.GetBoard[i_IDestination, i_JDestination] = i_Board.GetBoard[i_ISource, i_JSource]; } i_Board.GetBoard[i_ISource, i_JSource] = " "; if (Math.Abs(i_ISource - i_IDestination) == 2) { EatTroop(ref i_Board, i_ISource, i_JSource, i_IDestination, i_JDestination); } }
public bool IsTargetSquerePermitted(Board i_Board, int i_ISource, int i_JSource, int i_IDestination, int i_JDestination) { bool isTargetSquerePermitted = true; if (!i_Board.GetBoard[i_IDestination, i_JDestination].Equals(" ")) { isTargetSquerePermitted = false; } return isTargetSquerePermitted; }
public Move GetComputerMove(Board i_Board) { int i; Random random = new Random(); CreatePossibleMovesPoolForComputer(i_Board); i = random.Next(0, m_MovesArray.Count); return m_MovesArray[i]; }
public void CreatePossibleMovesPoolForComputer(Board i_Board) { Move move; m_MovesArray = new List<Move>(); for (int i = 0; i < i_Board.BoardSize; i++) { for (int j = 0; j < i_Board.BoardSize; j++) { if (i_Board.GetBoard[i, j].Equals("O") || i_Board.GetBoard[i, j].Equals("U")) { if (i + 2 < i_Board.BoardSize && j - 2 >= 0) { if (IsValidMove(i_Board, i, j, i + 2, j - 2)) { move = new Move(i, j, i + 2, j - 2); m_MovesArray.Add(move); } } if (i + 2 < i_Board.BoardSize && j + 2 < i_Board.BoardSize) { if (IsValidMove(i_Board, i, j, i + 2, j + 2)) { move = new Move(i, j, i + 2, j + 2); m_MovesArray.Add(move); } } if (i + 1 < i_Board.BoardSize && j + 1 < i_Board.BoardSize) { if (IsValidMove(i_Board, i, j, i + 1, j + 1)) { move = new Move(i, j, i + 1, j + 1); m_MovesArray.Add(move); } } if (i + 1 < i_Board.BoardSize && j - 1 >= 0) { if (IsValidMove(i_Board, i, j, i + 1, j - 1)) { move = new Move(i, j, i + 1, j - 1); m_MovesArray.Add(move); } } if (i_Board.GetBoard[i, j].Equals("U")) { if (i - 2 >= 0 && j - 2 >= 0) { if (IsValidMove(i_Board, i, j, i - 2, j - 2)) { move = new Move(i, j, i - 2, j - 2); m_MovesArray.Add(move); } } if (i - 2 >= 0 && j + 2 < i_Board.BoardSize) { if (IsValidMove(i_Board, i, j, i - 2, j + 2)) { move = new Move(i, j, i - 2, j + 2); m_MovesArray.Add(move); } } if (i - 1 >= 0 && j - 1 >= 0) { if (IsValidMove(i_Board, i, j, i - 1, j - 1)) { move = new Move(i, j, i - 1, j - 1); m_MovesArray.Add(move); } } if (i - 1 >= 0 && j + 1 < i_Board.BoardSize) { if (IsValidMove(i_Board, i, j, i - 1, j + 1)) { move = new Move(i, j, i - 1, j + 1); m_MovesArray.Add(move); } } } } } } }
public bool AnyAdditionalMove(Board i_Board, int i_ISource, int i_JSource, int i_Gap) { string troop = i_Board.GetBoard[i_ISource, i_JSource]; return (i_Gap == 2) && (IsRightJumpDiagonalPossible(i_Board, i_ISource, i_JSource) || IsLeftJumpDiagonalPossible(i_Board, i_ISource, i_JSource)); }
public bool HaveJumpedIfNeeded(Board i_Board, int i_ISource, int i_JSource, int i_IDestination, int i_JDestination) { bool haveJumpedIfNeeded = true; if (!IsInputJumpDiagonalLegit(i_Board, i_ISource, i_JSource, i_IDestination, i_JDestination)) { if (IsAnyOtherJumpsAvailable(i_Board)) { haveJumpedIfNeeded = false; } } return haveJumpedIfNeeded; }
public bool IsAnyOtherStepAvailable(Board i_Board, out string o_WinningTroop) { bool isAnyOtherStepAvailable = false; string troop, king; if (m_Player1Turn) { troop = "X"; king = "K"; } else { troop = "O"; king = "U"; } for (int i = 0; i < i_Board.BoardSize; i++) { for (int j = 0; j < i_Board.BoardSize; j++) { if (i_Board.GetBoard[i, j].Equals(troop) || i_Board.GetBoard[i, j].Equals(king)) { if (IsLeftStepDiagonalPossible(i_Board, i, j) || IsRightStepDiagonalPossible(i_Board, i, j)) { isAnyOtherStepAvailable = true; break; } } } } if (isAnyOtherStepAvailable) { o_WinningTroop = " "; } else { if (m_Player1Turn) { o_WinningTroop = "O"; } else { o_WinningTroop = "X"; } } return isAnyOtherStepAvailable; }
public bool IsLeftStepDiagonalPossible(Board i_Board, int i_ISource, int i_JSource) { bool isLeftStepDiagonalPossible = true; if (i_Board.GetBoard[i_ISource, i_JSource].Equals("X")) { if (i_JSource == 0) { isLeftStepDiagonalPossible = false; } else { if (!i_Board.GetBoard[i_ISource - 1, i_JSource - 1].Equals(" ")) { isLeftStepDiagonalPossible = false; } } } else if (i_Board.GetBoard[i_ISource, i_JSource].Equals("O")) { if (i_JSource == i_Board.BoardSize - 1) { isLeftStepDiagonalPossible = false; } else { if (!i_Board.GetBoard[i_ISource + 1, i_JSource + 1].Equals(" ")) { isLeftStepDiagonalPossible = false; } } } else if (IsKing(i_Board, i_ISource, i_JSource)) { if ((i_ISource == 0 && i_JSource == i_Board.BoardSize - 1) || (i_ISource == i_Board.BoardSize - 1 && i_JSource == 0)) { isLeftStepDiagonalPossible = false; } else if (i_JSource == i_Board.BoardSize - 1 || i_ISource == i_Board.BoardSize - 1) { if (!i_Board.GetBoard[i_ISource - 1, i_JSource - 1].Equals(" ")) { isLeftStepDiagonalPossible = false; } } else if (i_JSource == 0 || i_ISource == 0) { if (!i_Board.GetBoard[i_ISource + 1, i_JSource + 1].Equals(" ")) { isLeftStepDiagonalPossible = false; } } else { if (!i_Board.GetBoard[i_ISource - 1, i_JSource - 1].Equals(" ") && !i_Board.GetBoard[i_ISource + 1, i_JSource + 1].Equals(" ")) { isLeftStepDiagonalPossible = false; } } } return isLeftStepDiagonalPossible; }
public bool IsInputJumpDiagonalLegit(Board i_Board, int i_ISource, int i_JSource, int i_IDestination, int i_JDestination) { bool isInputJumpDiagonalLegit = false; bool isRightDiagonalPossible = IsRightJumpDiagonalPossible(i_Board, i_ISource, i_JSource); bool isLeftDiagonalPossible = IsLeftJumpDiagonalPossible(i_Board, i_ISource, i_JSource); if (isRightDiagonalPossible && isLeftDiagonalPossible) { isInputJumpDiagonalLegit = CheckRightSide(i_Board, i_ISource, i_JSource, i_IDestination, i_JDestination); if (!isInputJumpDiagonalLegit) { isInputJumpDiagonalLegit = CheckLeftSide(i_Board, i_ISource, i_JSource, i_IDestination, i_JDestination); } } else if (isRightDiagonalPossible) { isInputJumpDiagonalLegit = CheckRightSide(i_Board, i_ISource, i_JSource, i_IDestination, i_JDestination); } else if (isLeftDiagonalPossible) { isInputJumpDiagonalLegit = CheckLeftSide(i_Board, i_ISource, i_JSource, i_IDestination, i_JDestination); } return isInputJumpDiagonalLegit; }
public bool CheckLeftSide(Board i_Board, int i_ISource, int i_JSource, int i_IDestination, int i_JDestination) { string sourceTroop, destinationTroop, followingSpot; bool isLeftSideLegit = false; if (i_Board.GetBoard[i_ISource, i_JSource].Equals("X")) { if (i_IDestination == i_ISource - 2 && i_JDestination == i_JSource - 2) { isLeftSideLegit = true; } } else if (i_Board.GetBoard[i_ISource, i_JSource].Equals("O")) { if (i_IDestination == i_ISource + 2 && i_JDestination == i_JSource + 2) { isLeftSideLegit = true; } } else if (IsKing(i_Board, i_ISource, i_JSource)) { sourceTroop = i_Board.GetBoard[i_ISource, i_JSource]; followingSpot = i_Board.GetBoard[i_IDestination, i_JDestination]; if (i_ISource - 2 >= 0 && i_JSource - 2 >= 0) { destinationTroop = i_Board.GetBoard[i_ISource - 1, i_JSource - 1]; if (i_IDestination == i_ISource - 2 && i_JDestination == i_JSource - 2 && CheckIfTroopEatIsLegit(sourceTroop, destinationTroop, followingSpot)) { isLeftSideLegit = true; } } if (i_ISource + 2 < i_Board.BoardSize && i_JSource + 2 < i_Board.BoardSize && !isLeftSideLegit) { destinationTroop = i_Board.GetBoard[i_ISource + 1, i_JSource + 1]; if (i_IDestination == i_ISource + 2 && i_JDestination == i_JSource + 2 && CheckIfTroopEatIsLegit(sourceTroop, destinationTroop, followingSpot)) { isLeftSideLegit = true; } } } return isLeftSideLegit; }
public bool IsGameOver(Board i_Board, bool i_Quit, out string o_WinningTroop) { bool gameOver = true; if (i_Quit) { if (m_Player1Turn) { o_WinningTroop = "O"; } else { o_WinningTroop = "X"; } } else if (CountTroop(i_Board, "X") == 0 && CountTroop(i_Board, "K") == 0) { o_WinningTroop = "O"; } else if (CountTroop(i_Board, "O") == 0 && CountTroop(i_Board, "U") == 0) { o_WinningTroop = "X"; } else if (IsAnyOtherStepAvailable(i_Board, out o_WinningTroop)) { gameOver = false; } return gameOver; }
public bool IsLeftJumpDiagonalPossible(Board i_Board, int i_ISource, int i_JSource) { string sourceTroop = i_Board.GetBoard[i_ISource, i_JSource]; string destinationTroop, followingSpot; bool result = true; if (sourceTroop.Equals("X")) { if (i_JSource <= 1 || i_ISource <= 1) { result = false; } else { destinationTroop = i_Board.GetBoard[i_ISource - 1, i_JSource - 1]; followingSpot = i_Board.GetBoard[i_ISource - 2, i_JSource - 2]; result = CheckIfTroopEatIsLegit(sourceTroop, destinationTroop, followingSpot); } } else if (sourceTroop.Equals("O")) { if (i_JSource >= i_Board.BoardSize - 2 || i_ISource >= i_Board.BoardSize - 2) { result = false; } else { destinationTroop = i_Board.GetBoard[i_ISource + 1, i_JSource + 1]; followingSpot = i_Board.GetBoard[i_ISource + 2, i_JSource + 2]; result = CheckIfTroopEatIsLegit(sourceTroop, destinationTroop, followingSpot); } } else if (sourceTroop.Equals("K") || sourceTroop.Equals("U")) { if ((i_JSource <= 1 && i_ISource >= i_Board.BoardSize - 2) || (i_JSource >= i_Board.BoardSize - 2 && i_ISource <= 1)) { result = false; } else if (i_JSource >= i_Board.BoardSize - 2) { destinationTroop = i_Board.GetBoard[i_ISource - 1, i_JSource - 1]; followingSpot = i_Board.GetBoard[i_ISource - 2, i_JSource - 2]; result = CheckIfTroopEatIsLegit(sourceTroop, destinationTroop, followingSpot); } else if (i_ISource <= 1 || i_JSource <= 1) { destinationTroop = i_Board.GetBoard[i_ISource + 1, i_JSource + 1]; followingSpot = i_Board.GetBoard[i_ISource + 2, i_JSource + 2]; result = CheckIfTroopEatIsLegit(sourceTroop, destinationTroop, followingSpot); } else if (i_ISource >= i_Board.BoardSize - 2) { destinationTroop = i_Board.GetBoard[i_ISource - 1, i_JSource - 1]; followingSpot = i_Board.GetBoard[i_ISource - 2, i_JSource - 2]; result = CheckIfTroopEatIsLegit(sourceTroop, destinationTroop, followingSpot); } else { destinationTroop = i_Board.GetBoard[i_ISource - 1, i_JSource - 1]; followingSpot = i_Board.GetBoard[i_ISource - 2, i_JSource - 2]; if (!CheckIfTroopEatIsLegit(sourceTroop, destinationTroop, followingSpot)) { destinationTroop = i_Board.GetBoard[i_ISource + 1, i_JSource + 1]; followingSpot = i_Board.GetBoard[i_ISource + 2, i_JSource + 2]; result = CheckIfTroopEatIsLegit(sourceTroop, destinationTroop, followingSpot); } } } return result; }
public bool IsMoveOpponentTroop(Board i_Board, int i_ISource, int i_JSource) { bool isMoveOpponentTroop = false; if (m_Player1Turn) { if (i_Board.GetBoard[i_ISource, i_JSource].Equals("O") || i_Board.GetBoard[i_ISource, i_JSource].Equals("U")) { isMoveOpponentTroop = true; } } else if (!m_Player1Turn) { if (i_Board.GetBoard[i_ISource, i_JSource].Equals("X") || i_Board.GetBoard[i_ISource, i_JSource].Equals("K")) { isMoveOpponentTroop = true; } } return isMoveOpponentTroop; }
public int CalculateScore(Board i_Board, string i_WinningTroop, bool i_Quit) { int score = 0, winningTroopCounter, losingTroopCounter, deltaScore; winningTroopCounter = CountTroop(i_Board, i_WinningTroop); losingTroopCounter = CountTroop(i_Board, GetLosingTroop(i_WinningTroop)); deltaScore = winningTroopCounter - losingTroopCounter; if (deltaScore == 0 || i_Quit) { score = winningTroopCounter; } else { score = deltaScore; } return score; }
public bool IsValidMove(Board i_Board, int i_ISource, int i_JSource, int i_IDestination, int i_JDestination) { bool isValidMove = true; bool isSourceSquareOccupied; bool haveJumpedIfNeeded; bool isMoveDiagonallyFarward; bool isTargetSquerePermitted; bool isMoveOpponentTroop; bool isQuit = IsQuit(i_ISource, i_JSource, i_IDestination, i_JDestination); if (isQuit) { isValidMove = true; } else { isSourceSquareOccupied = IsSourceSquareOccupied(i_Board, i_ISource, i_JSource); haveJumpedIfNeeded = HaveJumpedIfNeeded(i_Board, i_ISource, i_JSource, i_IDestination, i_JDestination); isMoveDiagonallyFarward = IsMoveDiagonallyFarward(i_Board, i_ISource, i_JSource, i_IDestination, i_JDestination); isTargetSquerePermitted = IsTargetSquerePermitted(i_Board, i_ISource, i_JSource, i_IDestination, i_JDestination); isMoveOpponentTroop = IsMoveOpponentTroop(i_Board, i_ISource, i_JSource); if (!isSourceSquareOccupied || !isMoveDiagonallyFarward || !haveJumpedIfNeeded || !isTargetSquerePermitted || isMoveOpponentTroop) { isValidMove = false; } } return isValidMove; }
public bool IsMoveDiagonallyFarward(Board i_Board, int i_ISource, int i_JSource, int i_IDestination, int i_JDestination) { bool isMoveDiagonallyFarward = false; if (Math.Abs(i_IDestination - i_ISource) == 1) { if (IsInputStepLegit(i_Board, i_ISource, i_JSource, i_IDestination, i_JDestination)) { isMoveDiagonallyFarward = true; } } else if (Math.Abs(i_IDestination - i_ISource) == 2) { if (IsInputJumpDiagonalLegit(i_Board, i_ISource, i_JSource, i_IDestination, i_JDestination)) { isMoveDiagonallyFarward = true; } } return isMoveDiagonallyFarward; }
public void EatTroop(ref Board i_Board, int i_ISource, int i_JSource, int i_IDestination, int i_JDestination) { if (i_ISource - i_IDestination == 2) { if (i_JDestination < i_JSource) { i_Board.GetBoard[i_ISource - 1, i_JDestination + 1] = " "; } else { i_Board.GetBoard[i_ISource - 1, i_JDestination - 1] = " "; } } else if (i_IDestination - i_ISource == 2) { if (i_JDestination < i_JSource) { i_Board.GetBoard[i_ISource + 1, i_JDestination + 1] = " "; } else { i_Board.GetBoard[i_ISource + 1, i_JDestination - 1] = " "; } } }
public bool IsComputerHasRemainingMoves(Board i_Board) { string troop; return IsAnyOtherJumpsAvailable(i_Board) || IsAnyOtherStepAvailable(i_Board, out troop); }
private void InitControls() { int j, buttonTop = 50, buttonLeft = 20, currentButtonLeft = buttonLeft; m_LabelPlayer1.Text = m_GameProperties.Player1.Name + ":"; m_LabelPlayer1.AutoSize = true; m_LabelPlayer1.Top = 8; m_LabelPlayer1.Left = (this.ClientSize.Width / 4) - (m_LabelPlayer1.Width / 2); m_LabelPlayer1.ForeColor = Color.Green; m_LabelPlayer1.Font = new Font(m_LabelPlayer1.Font.Name, 12, FontStyle.Bold); m_LabelPlayer1Score.Text = m_GameProperties.Player1.Score.ToString(); m_LabelPlayer1Score.AutoSize = true; m_LabelPlayer1Score.Top = 8; m_LabelPlayer1Score.Left = m_LabelPlayer1.Right; m_LabelPlayer1Score.ForeColor = Color.Blue; m_LabelPlayer1Score.Font = new Font(m_LabelPlayer1Score.Font.Name, 12, FontStyle.Bold); m_LabelPlayer2.Text = m_GameProperties.Player2.Name + ":"; m_LabelPlayer2.AutoSize = true; m_LabelPlayer2.Top = 8; m_LabelPlayer2.Left = (this.ClientSize.Width / 2) + (this.ClientSize.Width / 4) - (m_LabelPlayer2.Width / 2); m_LabelPlayer2.ForeColor = Color.Blue; m_LabelPlayer2.Font = new Font(m_LabelPlayer2.Font.Name, 12, FontStyle.Bold); m_LabelPlayer2Score.Text = m_GameProperties.Player2.Score.ToString(); m_LabelPlayer2Score.AutoSize = true; m_LabelPlayer2Score.Top = 8; m_LabelPlayer2Score.Left = m_LabelPlayer2.Right + 5; m_LabelPlayer2Score.ForeColor = Color.Blue; m_LabelPlayer2Score.Font = new Font(m_LabelPlayer2Score.Font.Name, 12, FontStyle.Bold); m_Buttons = new CheckersButton[m_GameProperties.BoardSize, m_GameProperties.BoardSize]; m_Board = new Board(m_GameProperties.BoardSize); for (int i = 0; i < m_GameProperties.BoardSize; i++) { for (j = 0; j < m_GameProperties.BoardSize; j++) { m_Buttons[i, j] = new CheckersButton(m_GameProperties.BoardSize); m_Buttons[i, j].Size = new Size(80, 80); m_Buttons[i, j].Text = m_Board.GetBoard[i, j]; m_Buttons[i, j].Font = new Font(m_Buttons[i, j].Font.Name, 20, FontStyle.Bold); m_Buttons[i, j].AutoSize = true; m_Buttons[i, j].Top = buttonTop; m_Buttons[i, j].Left = currentButtonLeft; if (Board.IsValidSpot(i, j)) { m_Buttons[i, j].BackColor = Color.White; m_Buttons[i, j].i = i; m_Buttons[i, j].j = j; m_Buttons[i, j].Click += new EventHandler(buttonFirstClick_Click); } else { m_Buttons[i, j].Enabled = false; m_Buttons[i, j].BackColor = Color.Gray; } currentButtonLeft = m_Buttons[i, j].Right; this.Controls.Add(m_Buttons[i, j]); } currentButtonLeft = buttonLeft; buttonTop = m_Buttons[i, j - 1].Bottom; } this.Controls.AddRange(new Control[] { m_LabelPlayer1, m_LabelPlayer1Score, m_LabelPlayer2, m_LabelPlayer2Score }); }