////make the move by user choise, also do the computer move private void makeMoveByOpponentType( Player.eTypeOfPlayer i_TypeOfOpponent, Player.eNumberOfPlayer i_WhoMakeMove, Button i_ColChoise, ref bool io_HaveWinner) { bool isTheMoveSucceeded = false; int colFromConputer = 0; if (i_WhoMakeMove == Player.eNumberOfPlayer.User || (i_WhoMakeMove == Player.eNumberOfPlayer.Opponent && i_TypeOfOpponent == Player.eTypeOfPlayer.Human)) { m_LogicGame.PlayOneTurn(i_ColChoise.Text, ref m_countTurns, ref io_HaveWinner, ref isTheMoveSucceeded); updateAndCheckAfterMakeMove(i_ColChoise, isTheMoveSucceeded, io_HaveWinner, i_WhoMakeMove); if (i_TypeOfOpponent == Player.eTypeOfPlayer.Computer && !io_HaveWinner && !m_AnotherGame) { colFromConputer = m_LogicGame.computerTurn(ref io_HaveWinner, ref m_countTurns); updateAndCheckAfterMakeMove( m_TopBoardButtons[colFromConputer - r_IndexFix], isTheMoveSucceeded, io_HaveWinner, Player.eNumberOfPlayer.Opponent); } } }
////make the move that have been choosen public bool MakeMove(string i_ColsFromUser, Player.eNumberOfPlayer i_NumberOfPlayer) { int convertedCols = int.Parse(i_ColsFromUser) - k_IndexFix; Nullable <int> freeIndex; bool isInsertedToBoard = false; freeIndex = m_BoardGame.GetFreeIndex(convertedCols); if (freeIndex != null) { m_BoardGame.PlacePlayerShapeCoint(freeIndex.Value, convertedCols, (char)m_Players[(int)i_NumberOfPlayer].ShapeOfCoin); isInsertedToBoard = true; } return(isInsertedToBoard); }
////return the name of the winner public string extractNameOfWinner(Player.eNumberOfPlayer i_WhoMakeMove) { string nameOfWinner = string.Empty; if (i_WhoMakeMove == Player.eNumberOfPlayer.User) { nameOfWinner = m_Players[0].PlayerName; } else { nameOfWinner = m_Players[1].PlayerName; } return(nameOfWinner); }
////check if someone win public bool checkWinsOfTheMatch(Player.eNumberOfPlayer i_NumberOfPlayer) { Player.eShapeOfCoin shapeOfCurrentPlayer = (Player.eShapeOfCoin)m_Players[(int)i_NumberOfPlayer].ShapeOfCoin; bool foundedFourCoins = false; for (int i = m_BoardGame.Rows - k_IndexFix; i >= 0 && !foundedFourCoins; i--) { for (int j = m_BoardGame.Cols - k_IndexFix; j >= 0 && !foundedFourCoins; j--) { if (m_BoardGame.GetCharByIndexes(i, j) == (char)shapeOfCurrentPlayer) { foundedFourCoins = fourInVerticalDown(i, j, shapeOfCurrentPlayer) || fourInHorizonRight(i, j, shapeOfCurrentPlayer) || fourInDiagonalDownRight(i, j, shapeOfCurrentPlayer) || fourInDiagonalUpRight(i, j, shapeOfCurrentPlayer); } } } return(foundedFourCoins); }
////after every move the method update the board and check for winner/Tie private void updateAndCheckAfterMakeMove( Button i_ColToChoose, bool i_IsTheMoveSucceeded, bool i_HaveWinner, Player.eNumberOfPlayer i_WhoMakeMove) { string nameOfWinner = string.Empty; string topMessage = null; DialogResult resultFromMessageBox = DialogResult.No; m_LimitOfClicksForButton[int.Parse(i_ColToChoose.Text) - r_IndexFix]++; if (i_IsTheMoveSucceeded) { copyLogicBoardToFormBoard(); if (i_HaveWinner || m_LogicGame.BoardGame.IsBoardFull()) { if (i_HaveWinner) { nameOfWinner = m_LogicGame.extractNameOfWinner(i_WhoMakeMove); topMessage = Messages.sr_WinTopForm; } else if (m_LogicGame.BoardGame.IsBoardFull()) { topMessage = Messages.sr_TieTopForm; } m_Message = new Messages(topMessage, nameOfWinner); resultFromMessageBox = MessageBox.Show( m_Message.Message, topMessage, MessageBoxButtons.YesNo); m_countTurns = 0; } else { m_countTurns++; } ////if there is a winner/Tie create messege and show it if (m_Message != null) { if (resultFromMessageBox == DialogResult.No) { this.Close(); } else { m_AnotherGame = true; } m_Message = null; } } ////if the col full the button will not be enabel anymore if (m_LimitOfClicksForButton[int.Parse(i_ColToChoose.Text) - r_IndexFix] == m_Rows) { i_ColToChoose.Enabled = false; } }
////increase the score of player public void IncreaseScore(Player.eNumberOfPlayer i_PlayerToInceaseScore) { m_Players[(int)i_PlayerToInceaseScore].ScoreOfPlayer++; }