Example #1
0
        public bool MakeMove(Player player, GameMove gameMove)
        {
            if (!validateGameMove(player, gameMove))
            {
                return(false);
            }
            //Write the move in the GamePad
            int rowIndex, columnIndex;

            rowIndex    = getRowIndex(gameMove.Position);
            columnIndex = getColumnIndex(gameMove.Position);
            gamePad.SetValue(gameMove.XOSymbol, rowIndex, columnIndex);

            //Switch the player turns
            switchPlayers();

            //Process Game status to see whether there is a Winner, or the game ends
            processGame();

            //Drive the PCPlayer to Play
            if (currentPlayerTurn.Equals(PCPlayer) && !IsGameOver)
            {
                pcPlayer.Play();
            }
            return(true);
        }
Example #2
0
        public override void Play()
        {
            GameMove gameMove = iplayerBrain.Think(GameEngine.GamePad, this.XOSymbol);

            this.GameMove = gameMove;
            GameEngine.MakeMove(this, gameMove);
        }
Example #3
0
        /// <summary>
        /// Rules:
        /// 1- The Position does not have already an Symbol
        /// </summary>
        /// <param name="gameMove"></param>
        /// <returns></returns>
        private bool validatePosition(GameMove gameMove)
        {
            int rowIndex, columnIndex;

            rowIndex    = getRowIndex(gameMove.Position);
            columnIndex = getColumnIndex(gameMove.Position);
            return((XOSymbol)gamePad.GetValue(rowIndex, columnIndex) == XOSymbol.EMPTY);
        }
Example #4
0
        public void AddMove(GameMove gameMove)
        {
            if (pnlGamePad.GetControlFromPosition(gameMove.ColumnIndex, gameMove.RowIndex) != null && gameEngine != null)
                return;

            XOSymbolUC xoSymbolUC = new XOSymbolUC(gameMove.XOSymbol);
            this.pnlGamePad.Controls.Add(xoSymbolUC, gameMove.ColumnIndex, gameMove.RowIndex);
        }
Example #5
0
 /// <summary>
 /// Rules:
 /// 1- The Position does not have already an Symbol
 /// </summary>
 /// <param name="gameMove"></param>
 /// <returns></returns>
 private bool validatePosition(GameMove gameMove)
 {
     int rowIndex, columnIndex;
     rowIndex = getRowIndex(gameMove.Position);
     columnIndex = getColumnIndex (gameMove.Position);
     return (XOSymbol)gamePad.GetValue(rowIndex, columnIndex) == XOSymbol.EMPTY;
 }
Example #6
0
 /// <summary>
 /// Rules to be validated
 /// 1- Game is not over AND
 /// 2- The player own this turn AND
 /// 3- The XOSymbol position is valid
 /// </summary>
 /// <param name="player"></param>
 /// <param name="gameMove"></param>
 /// <returns></returns>
 private bool validateGameMove(Player player, GameMove gameMove)
 {
     return (!IsGameOver && player.Equals(currentPlayerTurn) && validatePosition(gameMove));
 }
Example #7
0
        public bool MakeMove(Player player, GameMove gameMove)
        {
            if (!validateGameMove(player, gameMove))
            {
                return false;
            }
            //Write the move in the GamePad
            int rowIndex, columnIndex;
            rowIndex = getRowIndex(gameMove.Position);
            columnIndex = getColumnIndex(gameMove.Position);
            gamePad.SetValue(gameMove.XOSymbol, rowIndex, columnIndex);

            //Switch the player turns
            switchPlayers();

            //Process Game status to see whether there is a Winner, or the game ends
            processGame();

            //Drive the PCPlayer to Play
            if (currentPlayerTurn.Equals(PCPlayer) && !IsGameOver)
                pcPlayer.Play();
            return true;
        }
Example #8
0
 /// <summary>
 /// Rules to be validated
 /// 1- Game is not over AND
 /// 2- The player own this turn AND
 /// 3- The XOSymbol position is valid
 /// </summary>
 /// <param name="player"></param>
 /// <param name="gameMove"></param>
 /// <returns></returns>
 private bool validateGameMove(Player player, GameMove gameMove)
 {
     return(!IsGameOver && player.Equals(currentPlayerTurn) && validatePosition(gameMove));
 }
Example #9
0
        protected void pnlGamePad_MouseClick(object sender, MouseEventArgs e)
        {
            int cellSize = pnlGamePad.Height / 3;
            int row = e.Y / cellSize;
            int column = e.X / cellSize;

            if (pnlGamePad.GetControlFromPosition(column, row) != null)
                return;

            int position = (row * 3) + column;
            GameMove gameMove = new GameMove(position, gameEngine.HumanPlayer.XOSymbol);
            AddMove(gameMove);
            gameEngine.HumanPlayer.GameMove = gameMove;
            gameEngine.HumanPlayer.Play();
            if (gameEngine.IsGameOver && (gameEngine.WinnerPlayer == gameEngine.HumanPlayer))
            {
                GameFinished(gameEngine);
                return;
            }
            AddMove(gameEngine.PCPlayer.GameMove);
            if (gameEngine.IsGameOver)
            {
                GameFinished(gameEngine);
                return;
            }
            MovePlayed(gameEngine);
        }