Exemplo n.º 1
0
        //Begin EASY

        public int minimaxEasy(char[] newBoard, char player)
        {
            char[] availSpots = emptyIndexies(newBoard);

            List <Move> moves = new List <Move>();

            // loop through available spots
            for (int i = 0; i < availSpots.Length; i++)
            {
                //create an object for each and store the index of that spot
                Move move = new Move();

                move.setIndex((int)char.GetNumericValue(newBoard[(int)char.GetNumericValue(availSpots[i])]));

                // set the empty spot to the current player
                newBoard[((int)char.GetNumericValue(availSpots[i]))] = player;


                // reset the spot to empty
                newBoard[(((int)char.GetNumericValue(availSpots[i])))] = Char.Parse(move.getIndex().ToString());

                // push the object to the array
                moves.Add(move);
            }

            int    bestMove = 0;
            Random rnd      = new Random();

            bestMove = rnd.Next(0, moves.Count);

            // return the chosen move (object) from the moves array
            if (moves.Count == 0)
            {
                return(0);
            }
            else
            {
                return(moves[bestMove].getIndex());
            }
        }
Exemplo n.º 2
0
        //End EASY

        //Begin MEDIUM

        public int minimaxMedium(char[] newBoard, char player)
        {
            char [] availSpots = emptyIndexies(newBoard);

            if (Winning(newBoard, huPlayer))
            {
                return(-10);
            }
            else if (Winning(newBoard, aiPlayer))
            {
                return(10);
            }
            else if (availSpots.Length == 0)
            {
                return(0);
            }


            List <Move> moves = new List <Move>();

            // loop through available spots
            for (int i = 0; i < availSpots.Length; i++)
            {
                //create an object for each and store the index of that spot
                Move move = new Move();

                move.setIndex((int)char.GetNumericValue(newBoard[(int)char.GetNumericValue(availSpots[i])]));

                // set the empty spot to the current player
                newBoard[((int)char.GetNumericValue(availSpots[i]))] = player;

                /*collect the score resulted from calling minimax
                 * on the opponent of the current player*/
                if (player == aiPlayer)
                {
                    int result = minimaxMedium(newBoard, huPlayer);
                    move.setScore(result);
                }
                else
                {
                    int result = minimaxMedium(newBoard, aiPlayer);
                    move.setScore(result);
                }

                // reset the spot to empty
                newBoard[(((int)char.GetNumericValue(availSpots[i])))] = Char.Parse(move.getIndex().ToString());

                // push the object to the array
                moves.Add(move);
            }

            // if it is the computer's turn loop over the moves and choose the move with the highest score
            int bestMove = 0;

            if (player == aiPlayer)
            {
                int bestScore = -10000;
                for (int i = 0; i < moves.Count; i++)
                {
                    if (moves[i].getScore() > bestScore)
                    {
                        bestScore = moves[i].getScore();
                        bestMove  = i;
                    }
                }
            }
            else
            {
                // else loop over the moves and choose the move with the lowest score
                int bestScore = 10000;
                for (int i = 0; i < moves.Count; i++)
                {
                    if (moves[i].getScore() < bestScore)
                    {
                        bestScore = moves[i].getScore();
                        bestMove  = i;
                    }
                }
            }

            // return the chosen move (object) from the moves array
            return(moves[bestMove].getIndex());
        }