예제 #1
0
        private bool CanWin(Mark mark, AIBoard board, out int positionChoice)
        {
            List <int> winPositions = CheckBoardForNearWins(board, mark);

            switch (winPositions.Count)
            {
            case 0:
                positionChoice = 0;
                return(false);

            case 1:
                positionChoice = winPositions[0];
                return(true);

            case 2:
            case 3:
                int random = rng.Next(0, winPositions.Count);
                positionChoice = winPositions[random];
                return(true);

            default:     //this will never happen
                positionChoice = 0;
                return(false);
            }
        }
예제 #2
0
        private bool CanSetupDoubleWin(Mark mark, out int positionChoice)
        {
            AIBoard    simulatedBoard      = null;
            List <int> doubleWinSetupMoves = new List <int>();

            //mark each open spot on the board and check to see if it sets up a double win. Then reset the board and try again.
            foreach (int positionToTry in CurrentBoard.UnmarkedPositions)
            {
                //refresh simulated board
                simulatedBoard = CurrentBoard.Clone();
                //mark the test position
                simulatedBoard.Positions[positionToTry].Mark = mark;

                List <int> simulatedWinPositions = CheckBoardForNearWins(simulatedBoard, mark);
                //if this list count is 1, this simulation set up a single win.
                //if this list count is 2, this simulation set up a double win.
                if (simulatedWinPositions.Count >= 2)
                {
                    doubleWinSetupMoves.Add(positionToTry);
                }
            }

            //now let's return the double win setup move(s). If there are two double win setup moves, let's pick a random one.
            if (doubleWinSetupMoves.Count > 0)
            {
                int random = rng.Next(0, doubleWinSetupMoves.Count);
                positionChoice = doubleWinSetupMoves[random];
                return(true);
            }

            //else no double win setup moves were found.
            positionChoice = 0;
            return(false);
        }
예제 #3
0
        private List <int> CheckBoardForNearWins(AIBoard board, Mark markToCheck)
        {
            List <int> nearWinLocs = new List <int>();
            Position   nearWinLoc  = null;

            /*loop through the array of possible wins and check those locations on the board to see if they are near wins (2 out of 3).
             * If they are, it returns the location of the unmarked loc in the triplet. If not, it returns null.*/
            int upperBound = PossibleWins.GetUpperBound(0);

            for (int i = 0; i <= upperBound; i++)
            {
                nearWinLoc = CheckTripletForNearWin(board.Positions[PossibleWins[i, 0]], board.Positions[PossibleWins[i, 1]], board.Positions[PossibleWins[i, 2]], markToCheck);
                if (nearWinLoc != null)
                {
                    nearWinLocs.Add(nearWinLoc.PositionNum);
                }
            }

            return(nearWinLocs);
        }
예제 #4
0
        public int TakeTurn(string[] inBoard)
        {
            //refresh the current board
            CurrentBoard = new AIBoard(inBoard);
            int positionChoice = 0;

            if (GameManager.DifficultyLevel == 3)
            {
                //process specific turns
                if (TurnsCounter == 0)
                    return ProcessFirstTurn();
                if (TurnsCounter == 1)
                    return ProcessSecondTurn();
                if (TurnsCounter == 2)
                    return ProcessThirdTurn();
                if (TurnsCounter == 3 && GameManager.DifficultyLevel == 3) //special situations for unbeatable difficulty
                {
                    if (DiagonalSandwitchSituation(out positionChoice))
                        return positionChoice;
                    if (HorzOrVertSandwitchSituation(out positionChoice))
                        return positionChoice;
                    if (DiagonalSwordSituation(out positionChoice))
                        return positionChoice;
                }
            }

            //check if there is anywhere that we can win this turn and do it
            if (CanWin(AIMark, CurrentBoard, out positionChoice))
                return positionChoice;

            if (GameManager.DifficultyLevel >= 2)
            {
                //check if we need to block the player from winning and do it
                if (CanWin(OpponentMark, CurrentBoard, out positionChoice))
                    return positionChoice;
            }

            if (GameManager.DifficultyLevel == 3) // lets have easy, normal, and unbeatable.
            {
                //check if we could set ourself up for a double win and do it
                if (TurnsCounter <= 7 && CanSetupDoubleWin(AIMark, out positionChoice))
                    return positionChoice;
            }

            if (GameManager.DifficultyLevel == 3)
            {
                //check if we need to block them from setting up a double win and do it
                if (TurnsCounter <= 7 && CanSetupDoubleWin(OpponentMark, out positionChoice))
                    return positionChoice;
            }

            //check if we could set ourself up for a single win and do it
            if (TurnsCounter <= 7 && CanSetupSingleWin(AIMark, out positionChoice))
                    return positionChoice;

            //I think, at this point, we can pick a random open spot //
            int random = rng.Next(0, CurrentBoard.UnmarkedPositions.Count);
            positionChoice = CurrentBoard.UnmarkedPositions[random];

            return positionChoice;
        }
예제 #5
0
        private List<int> CheckBoardForNearWins(AIBoard board, Mark markToCheck)
        {
            List<int> nearWinLocs = new List<int>();
            Position nearWinLoc = null;

            /*loop through the array of possible wins and check those locations on the board to see if they are near wins (2 out of 3).
            If they are, it returns the location of the unmarked loc in the triplet. If not, it returns null.*/
            int upperBound = PossibleWins.GetUpperBound(0);
            for (int i = 0; i <= upperBound; i++)
            {
                nearWinLoc = CheckTripletForNearWin(board.Positions[PossibleWins[i, 0]], board.Positions[PossibleWins[i, 1]], board.Positions[PossibleWins[i, 2]], markToCheck);
                if (nearWinLoc != null)
                {
                    nearWinLocs.Add(nearWinLoc.PositionNum);
                }
            }

            return nearWinLocs;
        }
예제 #6
0
 private bool CanWin(Mark mark, AIBoard board, out int positionChoice )
 {
     List<int> winPositions = CheckBoardForNearWins(board, mark);
     switch (winPositions.Count)
     {
         case 0:
             positionChoice = 0;
             return false;
         case 1:
             positionChoice = winPositions[0];
             return true;
         case 2:
         case 3:
             int random = rng.Next(0, winPositions.Count);
             positionChoice = winPositions[random];
             return true;
         default: //this will never happen
             positionChoice = 0;
             return false;
     }
 }
예제 #7
0
        public int TakeTurn(string[] inBoard)
        {
            //refresh the current board
            CurrentBoard = new AIBoard(inBoard);
            int positionChoice = 0;

            if (GameManager.DifficultyLevel == 3)
            {
                //process specific turns
                if (TurnsCounter == 0)
                {
                    return(ProcessFirstTurn());
                }
                if (TurnsCounter == 1)
                {
                    return(ProcessSecondTurn());
                }
                if (TurnsCounter == 2)
                {
                    return(ProcessThirdTurn());
                }
                if (TurnsCounter == 3 && GameManager.DifficultyLevel == 3) //special situations for unbeatable difficulty
                {
                    if (DiagonalSandwitchSituation(out positionChoice))
                    {
                        return(positionChoice);
                    }
                    if (HorzOrVertSandwitchSituation(out positionChoice))
                    {
                        return(positionChoice);
                    }
                    if (DiagonalSwordSituation(out positionChoice))
                    {
                        return(positionChoice);
                    }
                }
            }


            //check if there is anywhere that we can win this turn and do it
            if (CanWin(AIMark, CurrentBoard, out positionChoice))
            {
                return(positionChoice);
            }

            if (GameManager.DifficultyLevel >= 2)
            {
                //check if we need to block the player from winning and do it
                if (CanWin(OpponentMark, CurrentBoard, out positionChoice))
                {
                    return(positionChoice);
                }
            }


            if (GameManager.DifficultyLevel == 3) // lets have easy, normal, and unbeatable.
            {
                //check if we could set ourself up for a double win and do it
                if (TurnsCounter <= 7 && CanSetupDoubleWin(AIMark, out positionChoice))
                {
                    return(positionChoice);
                }
            }

            if (GameManager.DifficultyLevel == 3)
            {
                //check if we need to block them from setting up a double win and do it
                if (TurnsCounter <= 7 && CanSetupDoubleWin(OpponentMark, out positionChoice))
                {
                    return(positionChoice);
                }
            }


            //check if we could set ourself up for a single win and do it
            if (TurnsCounter <= 7 && CanSetupSingleWin(AIMark, out positionChoice))
            {
                return(positionChoice);
            }

            //I think, at this point, we can pick a random open spot //
            int random = rng.Next(0, CurrentBoard.UnmarkedPositions.Count);

            positionChoice = CurrentBoard.UnmarkedPositions[random];

            return(positionChoice);
        }