Esempio n. 1
0
        public static bool CheckHorizontalLine(GameBoard board, int placedRow, int placedCol, string piece, int toCheck, int lineLength)
        {
            int checkedSpots  = 0;
            int filledByPiece = 0;

            // Check initial spot
            if (board.GetGameBoardSpace(placedRow, placedCol) == piece)
            {
                checkedSpots++;
                filledByPiece++;
                // Check spots to the right
                for (int col = placedCol + 1; col < board.cols; col++)
                {
                    if (board.GetGameBoardSpace(placedRow, col) == piece)
                    {
                        checkedSpots++;
                        filledByPiece++;
                    }
                    else
                    {
                        break;
                    }
                    if (checkedSpots == toCheck)
                    {
                        break;
                    }
                }
                // Check spots to the left
                if (checkedSpots < toCheck)
                {
                    for (int col = placedCol - 1; col >= 0; col--)
                    {
                        if (board.GetGameBoardSpace(placedRow, col) == piece)
                        {
                            checkedSpots++;
                            filledByPiece++;
                        }
                        else
                        {
                            checkedSpots++;
                            break;
                        }
                        if (checkedSpots == toCheck)
                        {
                            break;
                        }
                    }
                }
            }
            //
            if (filledByPiece == lineLength && checkedSpots == toCheck)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 2
0
        public static bool CheckVerticalLine(GameBoard board, int placedRow, int placedCol, string piece, int toCheck, int lineLength)
        {
            int checkedSpots  = 0;
            int filledByPiece = 0;

            // Check initial spot
            if (board.GetGameBoardSpace(placedRow, placedCol) == piece)
            {
                checkedSpots++;
                filledByPiece++;
                // Check spots below
                for (int row = placedRow + 1; row < board.rows; row++)
                {
                    if (board.GetGameBoardSpace(row, placedCol) == piece)
                    {
                        checkedSpots++;
                        filledByPiece++;
                    }
                    else
                    {
                        break;
                    }
                    if (checkedSpots == toCheck)
                    {
                        break;
                    }
                }
                //Check spots above
                if (checkedSpots < toCheck)
                {
                    for (int row = placedRow - 1; row >= 0; row--)
                    {
                        if (board.GetGameBoardSpace(row, placedCol) == piece)
                        {
                            checkedSpots++;
                            filledByPiece++;
                        }
                        else
                        {
                            checkedSpots++;
                            break;
                        }
                        if (checkedSpots == toCheck)
                        {
                            break;
                        }
                    }
                }
            }
            if (filledByPiece == lineLength && checkedSpots == toCheck)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 3
0
        public static void ExpertMove(GameBoard board, GameBoard aiboard, Player player, TicTacToeGameRules myGameRules, out int row, out int col)
        {
            row = 0;
            col = 0;
            int bestScore = -1;
            int score;

            aiboard.InitializeGameBoard();

            if (board.rows == 3 && board.cols == 3 && board.GetGameBoardSpace(1, 1) == " ")
            {
                row = 1;
                col = 1;
            }
            else
            {
                for (int y = 0; y < board.rows; y++)
                {
                    for (int x = 0; x < board.cols; x++)
                    {
                        if (board.GetGameBoardSpace(y, x) == " ")
                        {
                            board.SetGameBoardSpace(y, x, player.myPiece);
                            score = Minimax(board, myGameRules.minimaxDepth, false, y, x, -1, myGameRules);
                            board.SetGameBoardSpace(y, x, " ");
                            if (score > bestScore)
                            {
                                bestScore = score;
                            }
                            aiboard.SetGameBoardSpace(y, x, score.ToString());
                            //aiGameBoard.DrawGameBoard();
                        }
                    }
                }
                // See scores
                //aiboard.DrawGameBoard();
                //Console.WriteLine($"bestScore = {bestScore}");
                //Console.ReadLine();

                // Pick Random From Best
                Random rand = new Random();
                do
                {
                    row = rand.Next(board.rows);
                    col = rand.Next(board.cols);
                } while (aiboard.GetGameBoardSpace(row, col) != bestScore.ToString());
            }
        }
Esempio n. 4
0
 private bool PlacePiece(GameBoard board, int row, int col, Player player)
 {
     if (row >= 0 && row < board.rows && col >= 0 && col < board.cols && board.GetGameBoardSpace(row, col) == " ")
     {
         board.SetGameBoardSpace(row, col, player.myPiece);
         return(true);
     }
     else
     {
         if (player.myType == "Human")
         {
             Console.WriteLine(" Invalid space -- try again.");
         }
         return(false);
     }
 }
Esempio n. 5
0
        public static bool CheckForDraw(GameBoard board)
        {
            int emptySpaces = 0;

            for (int row = 0; row < board.rows; row++)
            {
                for (int col = 0; col < board.cols; col++)
                {
                    if (board.GetGameBoardSpace(row, col) == " ")
                    {
                        emptySpaces++;
                    }
                }
            }
            if (emptySpaces > 0)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Esempio n. 6
0
        private static int Minimax(GameBoard board, int depth, bool isMaximizing, int row, int col, int parentBestScore, TicTacToeGameRules myGameRules)
        {
            int maxScore = 1;
            int minScore = -1;
            int score;

            // See move
            //board.DrawGameBoard();
            //Console.ReadLine();
            if (TicTacToeGameRules.CheckForWin(board, row, col, myGameRules.currentPlayer.myPiece))
            {
                score = 1;
                return(score);
            }
            else if (TicTacToeGameRules.CheckForWin(board, row, col, myGameRules.opponent.myPiece))
            {
                score = -1;
                return(score);
            }
            else if (TicTacToeGameRules.CheckForDraw(board) || depth <= 0)
            {
                score = 0;
                return(score);
            }

            if (isMaximizing)
            {
                int bestScore = minScore;
                for (int y = 0; y < board.rows; y++)
                {
                    for (int x = 0; x < board.cols; x++)
                    {
                        if (board.GetGameBoardSpace(y, x) == " ")
                        {
                            board.SetGameBoardSpace(y, x, myGameRules.currentPlayer.myPiece);
                            score = Minimax(board, depth - 1, false, y, x, minScore, myGameRules);
                            board.SetGameBoardSpace(y, x, " ");
                            if (score > bestScore)
                            {
                                bestScore = score;
                            }
                            if (bestScore >= parentBestScore)
                            {
                                return(bestScore);
                            }
                        }
                    }
                }
                return(bestScore);
            }
            else
            {
                int bestScore = maxScore;
                for (int y = 0; y < board.rows; y++)
                {
                    for (int x = 0; x < board.cols; x++)
                    {
                        if (board.GetGameBoardSpace(y, x) == " ")
                        {
                            board.SetGameBoardSpace(y, x, myGameRules.opponent.myPiece);
                            score = Minimax(board, depth - 1, true, y, x, maxScore, myGameRules);
                            board.SetGameBoardSpace(y, x, " ");
                            if (score < bestScore)
                            {
                                bestScore = score;
                            }
                            if (bestScore <= parentBestScore)
                            {
                                return(bestScore);
                            }
                        }
                    }
                }
                return(bestScore);
            }
        }
Esempio n. 7
0
        public static bool CheckDiagonal2Line(GameBoard board, int placedRow, int placedCol, string piece, int toCheck, int lineLength)
        {
            int checkedSpots  = 0;
            int filledByPiece = 0;

            // Check initial spot
            if (board.GetGameBoardSpace(placedRow, placedCol) == piece)
            {
                checkedSpots++;
                filledByPiece++;
                // Check spots down and to left
                int col = placedCol - 1;
                for (int row = placedRow + 1; row < board.rows; row++)
                {
                    if (col >= 0)
                    {
                        if (board.GetGameBoardSpace(row, col) == piece)
                        {
                            checkedSpots++;
                            filledByPiece++;
                            col--;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                    if (checkedSpots == toCheck)
                    {
                        break;
                    }
                }
                // Check spots up and to right
                if (checkedSpots < toCheck)
                {
                    col = placedCol + 1;
                    for (int row = placedRow - 1; row >= 0; row--)
                    {
                        if (col < board.cols)
                        {
                            if (board.GetGameBoardSpace(row, col) == piece)
                            {
                                checkedSpots++;
                                filledByPiece++;
                                col++;
                            }
                            else
                            {
                                checkedSpots++;
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                        if (checkedSpots == toCheck)
                        {
                            break;
                        }
                    }
                }
            }
            if (filledByPiece == lineLength && checkedSpots == toCheck)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }