Ejemplo n.º 1
0
 internal void ClearBorad()
 {
     LastSelectedCell = null;
     foreach (GameCell boardGameCell in BoardGameCells)
     {
         SetCellState(boardGameCell, Enums.eCellValue.Blank);
     }
 }
Ejemplo n.º 2
0
        private void initializeBoardGameCells()
        {
            BoardGameCells = new GameCell[BoradSize, BoradSize];

            for (int row = 0 ; row < BoradSize ; row++)
            {
                for (int column = 0 ; column < BoradSize ; column++)
                {
                    BoardGameCells[row, column] = new GameCell(row, column);
                }
            }
        }
Ejemplo n.º 3
0
        private void initializeBoardGameCells()
        {
            BoardGameCells = new GameCell[BoradSize, BoradSize];

            for (int row = 0; row < BoradSize; row++)
            {
                for (int column = 0; column < BoradSize; column++)
                {
                    BoardGameCells[row, column] = new GameCell(row, column);
                }
            }
        }
Ejemplo n.º 4
0
        private bool rowEnded(GameCell i_GameCell, Enums.eCellValue i_WantedCellState)
        {
            bool result = true;

            for (int i = 0; i < BoradSize; i++)
            {
                if (i_GameCell != BoradGameCells[i, i_GameCell.ColumnIndex] &&
                    BoradGameCells[i, i_GameCell.ColumnIndex].Value != i_WantedCellState)
                {
                    result = false;
                    break;
                }
            }
            return(result);
        }
Ejemplo n.º 5
0
        private GameCell playComputerMove()
        {
            bool            continueSearching = true;
            List <GameCell> selectedUnWantedCells = new List <GameCell>(r_BoardGame.FreeCellsList.Count);
            GameCell        selectedCell, preferredSelecteCell = null;
            bool            thisCellEndsGameForComputer, thisCellEndsGameForPlayer;

            do
            {
                selectedCell = getRandomFreeCell();
                thisCellEndsGameForComputer = isPlayerEndedGame(selectedCell, k_PlayerTwoValue);
                thisCellEndsGameForPlayer   = isPlayerEndedGame(selectedCell, k_PlayerOneValue);

                //trying to get a cell that doesn't end the game for computer
                // and doesn't help the player
                if (thisCellEndsGameForComputer || thisCellEndsGameForPlayer)
                {
                    selectedUnWantedCells.Add(selectedCell);
                    r_BoardGame.FreeCellsList.Remove(selectedCell);

                    //prefer to select a cell that helps the player
                    //than one that end the game for computer
                    if (thisCellEndsGameForPlayer)
                    {
                        preferredSelecteCell = selectedCell;
                    }
                    if (r_BoardGame.IsThereNoMoreFreeCells)
                    {
                        if (preferredSelecteCell != null)
                        {
                            selectedCell = preferredSelecteCell;
                        }
                        continueSearching = false;
                    }
                }
                else
                {
                    continueSearching = false;
                }
                if (!continueSearching)
                {
                    r_BoardGame.FreeCellsList.AddRange(selectedUnWantedCells);
                }
            } while (continueSearching);

            setCellState(selectedCell);
            return(selectedCell);
        }
Ejemplo n.º 6
0
 internal void SetCellState(GameCell i_GameCell, Enums.eCellValue i_TheState)
 {
     i_GameCell.Value = i_TheState;
     if (i_TheState == Enums.eCellValue.Blank)
     {
         if (!FreeCellsList.Contains(i_GameCell))
         {
             FreeCellsList.Add(i_GameCell);
         }
     }
     else
     {
         LastSelectedCell = i_GameCell;
         FreeCellsList.Remove(i_GameCell);
     }
 }
Ejemplo n.º 7
0
 internal void SetCellState(GameCell i_GameCell, Enums.eCellValue i_TheState)
 {
     i_GameCell.Value = i_TheState;
     if (i_TheState == Enums.eCellValue.Blank)
     {
         if (!FreeCellsList.Contains(i_GameCell))
         {
             FreeCellsList.Add(i_GameCell);
         }
     }
     else
     {
         LastSelectedCell = i_GameCell;
         FreeCellsList.Remove(i_GameCell);
     }
 }
Ejemplo n.º 8
0
        private void computerPlayingLogic(ref bool io_RoundIsOver)
        {
            togglePlayers();
            GameCell computerSelectedCell = playComputerMove();
            bool     isComputerLost       = isPlayerEndedGame(computerSelectedCell, computerSelectedCell.Value);

            if (isComputerLost)
            {
                io_RoundIsOver = true;
                currentPlayerLose();
            }
            else
            {
                if (r_BoardGame.IsThereNoMoreFreeCells)
                {
                    setTie(out io_RoundIsOver);
                }
                else
                {
                    togglePlayers();
                }
            }
        }
Ejemplo n.º 9
0
        private bool diagonalEnded(GameCell i_GameCell, Enums.eCellValue i_WantedCellState)
        {
            bool firstDiagonalIsTrue = true, secondDiagonal = true;

            if (i_GameCell.RowIndex == i_GameCell.ColumnIndex)
            {
                for (int i = 0; i < BoradSize; i++)
                {
                    if (i_GameCell != BoradGameCells[i, i] && BoradGameCells[i, i].Value != i_WantedCellState)
                    {
                        firstDiagonalIsTrue = false;
                        break;
                    }
                }
            }
            else
            {
                firstDiagonalIsTrue = false;
            }
            if (i_GameCell.RowIndex + i_GameCell.ColumnIndex == BoradSize - 1)
            {
                for (int i = 0; i < BoradSize; i++)
                {
                    if (i_GameCell != BoradGameCells[i, BoradSize - 1 - i] &&
                        BoradGameCells[i, BoradSize - 1 - i].Value != i_WantedCellState)
                    {
                        secondDiagonal = false;
                        break;
                    }
                }
            }
            else
            {
                secondDiagonal = false;
            }
            return(firstDiagonalIsTrue || secondDiagonal);
        }
Ejemplo n.º 10
0
 private void setCellState(GameCell i_GameCell)
 {
     Enums.eCellValue cellState = CurrentPlayerTurn == Enums.ePlayer.PlayerOne
         ? k_PlayerOneValue : k_PlayerTwoValue;
     r_BoardGame.SetCellState(i_GameCell, cellState);
 }
Ejemplo n.º 11
0
 private bool rowEnded(GameCell i_GameCell, Enums.eCellValue i_WantedCellState)
 {
     bool result = true;
     for (int i = 0 ; i < BoradSize ; i++)
     {
         if (i_GameCell != BoradGameCells[i, i_GameCell.ColumnIndex]
             && BoradGameCells[i, i_GameCell.ColumnIndex].Value != i_WantedCellState)
         {
             result = false;
             break;
         }
     }
     return result;
 }
Ejemplo n.º 12
0
 private bool isPlayerEndedGame(GameCell i_GameCell,
     Enums.eCellValue i_WantedCellState)
 {
     return rowEnded(i_GameCell, i_WantedCellState) ||
            columnEnded(i_GameCell, i_WantedCellState) ||
            diagonalEnded(i_GameCell, i_WantedCellState);
 }
Ejemplo n.º 13
0
 private bool diagonalEnded(GameCell i_GameCell, Enums.eCellValue i_WantedCellState)
 {
     bool firstDiagonalIsTrue = true, secondDiagonal = true;
     if (i_GameCell.RowIndex == i_GameCell.ColumnIndex)
     {
         for (int i = 0 ; i < BoradSize ; i++)
         {
             if (i_GameCell != BoradGameCells[i, i] && BoradGameCells[i, i].Value != i_WantedCellState)
             {
                 firstDiagonalIsTrue = false;
                 break;
             }
         }
     }
     else
     {
         firstDiagonalIsTrue = false;
     }
     if (i_GameCell.RowIndex + i_GameCell.ColumnIndex == BoradSize - 1)
     {
         for (int i = 0 ; i < BoradSize ; i++)
         {
             if (i_GameCell != BoradGameCells[i, BoradSize - 1 - i]
                 && BoradGameCells[i, BoradSize - 1 - i].Value != i_WantedCellState)
             {
                 secondDiagonal = false;
                 break;
             }
         }
     }
     else
     {
         secondDiagonal = false;
     }
     return firstDiagonalIsTrue || secondDiagonal;
 }
Ejemplo n.º 14
0
 internal void NextPlayerMove(GameCell i_GameCell, out bool o_RoundIsOver)
 {
     setCellState(i_GameCell);
     o_RoundIsOver = false;
     bool isPlayerLost = isPlayerEndedGame(i_GameCell, i_GameCell.Value);
     if (isPlayerLost)
     {
         currentPlayerLose();
         o_RoundIsOver = true;
     }
     else
     {
         if (r_BoardGame.IsThereNoMoreFreeCells)
         {
             setTie(out o_RoundIsOver);
         }
         else
         {
             if (GameType == Enums.eGameType.PlayerVsComputer)
             {
                 computerPlayingLogic(ref o_RoundIsOver);
             }
             else
             {
                 togglePlayers();
             }
         }
     }
 }
Ejemplo n.º 15
0
 private void setCellState(GameCell i_GameCell)
 {
     Enums.eCellValue cellState = CurrentPlayerTurn == Enums.ePlayer.PlayerOne
         ? k_PlayerOneValue : k_PlayerTwoValue;
     r_BoardGame.SetCellState(i_GameCell, cellState);
 }