Example #1
0
        private bool getPlayerHalfMove(Player i_Player, Board i_Board, ref GameCell io_SelectedCell)
        {
            MessageDisplayer.DisplayMessage(i_Player.Name + MessageDisplayer.Turn);
            if (i_Player.Type == ePlayerType.Human)
            {
                MessageDisplayer.DisplayMessage(MessageDisplayer.EnterMove);
            }

            io_SelectedCell = i_Player.PlayerMove(i_Board);
            bool wantsToPlay = io_SelectedCell != null;

            return(wantsToPlay);
        }
Example #2
0
        private GameCell[,] createBoard()
        {
            GameCell[,] boardCells = new GameCell[r_Height, r_Width];
            int counterOfFilledCells = 0;

            for (int i = 0; i < r_Height; i++)
            {
                for (int j = 0; j < r_Width; j++)
                {
                    char temporaryChar = (char)('A' + (counterOfFilledCells / 2));
                    boardCells[i, j] = new GameCell(temporaryChar);
                    counterOfFilledCells++;
                }
            }

            return(boardCells);
        }
Example #3
0
        internal Player ExecuteMove(Player i_CurrentPlayer, GameCell i_FirstCell, GameCell i_SecondCell)
        {
            Player nextPlayer = i_CurrentPlayer;

            if (i_FirstCell.Letter == i_SecondCell.Letter)
            {
                i_CurrentPlayer.Score++;
                m_Board.RemainingCouples--;
            }
            else
            {
                coverCells(i_FirstCell, i_SecondCell);
                nextPlayer = togglePlayer(i_CurrentPlayer);
            }

            Thread.Sleep(2000);

            return(nextPlayer);
        }
Example #4
0
        private bool runSingleGame(Player i_FirstPlayer, Player i_SecondPlayer, Board i_Board)
        {
            GameManager  gameManager     = new GameManager(i_FirstPlayer, i_SecondPlayer, i_Board);
            bool         gameStillActive = true;
            Player       currentPlayer   = i_FirstPlayer;
            BoardPainter boardPainter    = new BoardPainter(i_Board);

            while (gameStillActive)
            {
                GameCell firstCell  = null;
                GameCell secondCell = null;

                //// Get a move from the player / computer - if player quits returns false
                gameStillActive = getPlayerMove(currentPlayer, i_Board, boardPainter, ref firstCell, ref secondCell);
                if (!gameStillActive)
                {
                    break;
                }

                if (i_SecondPlayer.Type == ePlayerType.Computer)
                {
                    i_SecondPlayer.ComputerRememberCell(firstCell);
                    i_SecondPlayer.ComputerRememberCell(secondCell);
                }

                boardPainter.ClearAndPaintBoard();

                currentPlayer = gameManager.ExecuteMove(currentPlayer, firstCell, secondCell);
                if (gameManager.IsGameOver())
                {
                    break;
                }
            }

            if (gameStillActive)
            { // Finished game without quitting
                announceWinner(i_FirstPlayer, i_SecondPlayer);
                gameStillActive = stillWantToPlay();
            }

            return(gameStillActive);
        }
Example #5
0
        private void shuffleBoard()
        {
            int    numberOfCells = r_Height * r_Width;
            Random random        = new Random();

            for (int i = 0; i < numberOfCells - 1; i++)
            {
                int firstRow    = i / r_Width;
                int firstColumn = i % r_Width;

                // Pick a random cell between i and the end of the array.
                int randomizeSecondIndex = random.Next(i + 1);
                int secondRow            = randomizeSecondIndex / r_Width;
                int secondColumn         = randomizeSecondIndex % r_Width;

                GameCell temp = m_BoardCells[firstRow, firstColumn];
                m_BoardCells[firstRow, firstColumn]   = m_BoardCells[secondRow, secondColumn];
                m_BoardCells[secondRow, secondColumn] = temp;
            }
        }
Example #6
0
        internal GameCell ComputerAiMove(Board i_Board, GameCell i_FirstRevealedCell) // Second half of move is intelligent
        {
            GameCell selectedCell;

            if (m_ComputerMemory.TryGetValue(i_FirstRevealedCell.Letter, out selectedCell)) // Letter found in memory
            {
                //// Make sure it's not the same cell
                selectedCell = selectedCell != i_FirstRevealedCell ? selectedCell : ComputerRandomMove(i_Board);
            }
            else
            {
                Thread.Sleep(500); // wait a little before playing - for UX
                selectedCell = ComputerRandomMove(i_Board);
            }

            selectedCell.IsRevealed = true;
            i_Board.UnRevealedCells.Remove(selectedCell);

            return(selectedCell);
        }