Esempio n. 1
0
        //Find out random play position
        private PlayPosition findRandomPlayPosition()
        {
            PlayPosition posBest = null;

            PlayPosition[] freepos = new PlayPosition[9];
            int            nosFree = 0;

            //Find all empty play positions
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (_arrBoard[i][j].Value == ' ')
                    {
                        freepos[nosFree]   = new PlayPosition();
                        freepos[nosFree].X = i;
                        freepos[nosFree].Y = j;
                        nosFree++;
                    }
                }
            }
            if (nosFree > 0)
            {
                Random rnd = new Random();

                int temp = rnd.Next(0, nosFree - 1);

                posBest = freepos[temp];
            }

            return(posBest);
        }
Esempio n. 2
0
        //Find next move for a play char X or O
        private PlayPosition FindNextMove(char playChar)
        {
            BoardCell    cell    = null;
            PlayPosition posBest = null;

            do
            {
                for (int i = 0; i < 3; i++)
                {
                    cell = findEmptyCell(_arrBoard[0][i], _arrBoard[1][i], _arrBoard[2][i], playChar);//|1 |2 |3

                    if (cell != null)
                    {
                        posBest = cell.CellPos;
                        break;
                    }
                }

                if (posBest != null)
                {
                    break;
                }

                cell = findEmptyCell(_arrBoard[0][0], _arrBoard[1][1], _arrBoard[2][2], playChar);//\
                if (cell != null)
                {
                    posBest = cell.CellPos;
                    break;
                }
                cell = findEmptyCell(_arrBoard[0][2], _arrBoard[1][1], _arrBoard[2][0], playChar);// /
                if (cell != null)
                {
                    posBest = cell.CellPos;
                    break;
                }

                for (int i = 0; i < 3; i++)
                {
                    cell = findEmptyCell(_arrBoard[i][0], _arrBoard[i][1], _arrBoard[i][2], playChar);//--1 --2 --3
                    if (cell != null)
                    {
                        posBest = cell.CellPos;
                        break;
                    }
                }
            } while (false);

            return(posBest);
        }
Esempio n. 3
0
        //Play a position
        public void Play(PlayPosition playPos)
        {
            _nosMoves++;

            _lastTurn = _playTurn;

            if (_playTurn == PlayerNumber.FirstPlayer)//First Player turn
            {
                _gameBoard.SetAt(playPos.X, playPos.Y, (char)PlayChars.First);
                _playTurn = PlayerNumber.SecondPlayer;
            }
            else if (_playTurn == PlayerNumber.SecondPlayer)//Second player turn
            {
                _gameBoard.SetAt(playPos.X, playPos.Y, (char)PlayChars.Second);
                _playTurn = PlayerNumber.FirstPlayer;
            }

            //Check game is over only after 5 play moves.
            if (_nosMoves >= 5)
            {
                if (_gameBoard.IsGameOver())
                {
                    _gameOver = true;
                }
            }

            //If game is over then update WhoWon and score
            if (_nosMoves == 9 || _gameOver)
            {
                _gameOver = true;

                if (_gameBoard.whoWon == (char)PlayChars.First)
                {
                    _wonPlayer = PlayerNumber.FirstPlayer;
                }
                else if (_gameBoard.whoWon == (char)PlayChars.Second)
                {
                    _wonPlayer = PlayerNumber.SecondPlayer;
                }

                if (_wonPlayer > 0)
                {
                    _players[((int)_wonPlayer) - 1].Score++;
                }
            }
        }
Esempio n. 4
0
        //Find execute computer play
        public GameResult DoComputerPlay()
        {
            GameResult gameRslt = null;

            //At end if next player is computer type then execute next play too.
            if (_objGame.NextPlayer == PlayerNumber.SecondPlayer && _objGame.Players[1].Type == PlayerType.Computer && !_objGame.gameOver)//if 2nd player is a computer
            {
                //Find our next best play
                PlayPosition nextPlay = ((ComputerPlayer)_objGame.Players[1]).GetNextMove(_objGame.GameBoard);

                //Play that move
                gameRslt = this.UserPlay(nextPlay);
            }
            else
            {
                //Otherwise just return current gameboard
                gameRslt           = new GameResult();
                gameRslt.GameBoard = _objGame.GameBoard;
            }

            return(gameRslt);
        }
Esempio n. 5
0
        //Execute a user play
        public GameResult UserPlay(PlayPosition playPos)
        {
            GameResult gameRslt = new GameResult();

            //Play
            _objGame.Play(playPos);

            //Prepare play result
            gameRslt.LastPlayedPosition = playPos;
            gameRslt.GameBoard          = _objGame.GameBoard;
            gameRslt.GameOver           = _objGame.gameOver;
            gameRslt.Players            = _objGame.Players;

            //Check game over flag
            if (_objGame.gameOver)
            {
                gameRslt.WhoWon   = _objGame.wonPlayer;
                gameRslt.GameOver = _objGame.gameOver;
            }

            return(gameRslt);
        }
Esempio n. 6
0
        //Get next play position for computer type player
        public PlayPosition getNextBestPlay()
        {
            PlayPosition posBest = null;

            posBest = FindNextMove((char)PlayChars.Second);//Check any Winning move

            if (posBest != null)
            {
                return(posBest);
            }

            posBest = FindNextMove((char)PlayChars.First);//Check any defending move

            if (posBest != null)
            {
                return(posBest);
            }

            posBest = findRandomPlayPosition();//Find a random play

            return(posBest);
        }
Esempio n. 7
0
        //Find next best move of computer type player
        public PlayPosition GetNextMove(IGameBoard board)
        {
            PlayPosition pos = board.getNextBestPlay();

            return(pos);
        }