示例#1
0
        }     //end PlayerVsComp

        public static void PlayerVsPlayer()
        {
            GameBoard board = new GameBoard();

            Console.Write("Enter Name1: ");
            string name1 = Console.ReadLine();

            Console.Write("Enter Name2: ");
            string name2 = Console.ReadLine();
            bool   cont  = false;

            while (true)
            {
                Console.Clear();
                Console.WriteLine("{0}\'s turn:", name1);
                board.OutputBoard();
                board.OutputBoardLocations();
                while (!cont)
                {
                    bool   isNumeric;
                    string inp;
                    int    num;
                    do
                    {
                        Console.Write("Enter move: ");
                        inp       = Console.ReadLine();
                        isNumeric = int.TryParse(inp, out num);
                        if (!isNumeric)
                        {
                            Console.WriteLine("Could not parse the input. Please try again.");
                        }//end if
                        if (num < 0 || num > 8)
                        {
                            Console.WriteLine("Please enter a valid number.");
                        }//end if
                    } while (!isNumeric || num > 8 || num < 0);
                    if (board.GetSpace(num) == " ")
                    {
                        board.SetSpace(num, "X"); cont = true;
                    }
                    else
                    {
                        Console.WriteLine("That space is not available.");
                    }
                }//end while
                if (board.HasWinner("X") || board.HasWinner("O") || board.IsCat())
                {
                    break;
                }
                cont = false;
                Console.Clear();
                Console.WriteLine("{0}\'s turn:", name2);
                board.OutputBoard();
                board.OutputBoardLocations();
                while (!cont)
                {
                    bool   isNumeric;
                    string inp;
                    int    num;
                    do
                    {
                        Console.Write("Enter move: ");
                        inp       = Console.ReadLine();
                        isNumeric = int.TryParse(inp, out num);
                        if (!isNumeric)
                        {
                            Console.WriteLine("Could not parse the input. Please try again.");
                        }//end if
                        if (num < 0 || num > 8)
                        {
                            Console.WriteLine("Please enter a valid number.");
                        }//end if
                    } while (!isNumeric || num > 8 || num < 0);
                    if (board.GetSpace(num) == " ")
                    {
                        board.SetSpace(num, "O"); cont = true;
                    }
                    else
                    {
                        Console.WriteLine("That space is not available.");
                    }
                }//end while
                cont = false;
                if (board.HasWinner("X") || board.HasWinner("O") || board.IsCat())
                {
                    break;
                }
            }//end while
            if (board.HasWinner("X"))
            {
                Console.WriteLine("{0} has won.", name1);
            }
            if (board.HasWinner("O"))
            {
                Console.WriteLine("{0} has won.", name2);
            }
            if (board.IsCat())
            {
                Console.WriteLine("It was a tie.");
            }
            board.OutputBoard();
        } //end PlayerVsPlayer
示例#2
0
        }         //end main

        public static void CompVsComp(int rate1, int rate2)
        {
            GameBoard      board = new GameBoard();
            AI             ai1   = new AI(rate1, 255, true);
            AI             ai2   = new AI(rate2, 255, false);
            ConsoleKeyInfo press;

            while (true)
            {
                int turn = 0;
                while (true)
                {
                    Console.Clear();
                    //turn 1
                    ai1.OutputDes();
                    board.OutputBoard();
                    Console.WriteLine("AI1's turn:");
                    press = Console.ReadKey();
                    if ((press.Modifiers & ConsoleModifiers.Control) != 0)
                    {
                        PlayerVsPreComp(rate1, false, ai1);
                    }
                    if ((press.Modifiers & ConsoleModifiers.Alt) != 0)
                    {
                        PlayerVsPreComp(rate2, true, ai2);
                    }
                    bool[] checkArray = { board.IsSpaceEmpty(0), board.IsSpaceEmpty(1), board.IsSpaceEmpty(2), board.IsSpaceEmpty(3), board.IsSpaceEmpty(4), board.IsSpaceEmpty(5), board.IsSpaceEmpty(6), board.IsSpaceEmpty(7), board.IsSpaceEmpty(8) };
                    board.SetSpace(ai1.Move(checkArray, turn), "X");
                    if (board.HasWinner("X") || board.HasWinner("O") || board.IsCat())
                    {
                        break;
                    }
                    Console.Clear();
                    //turn 2
                    ai2.OutputDes();
                    board.OutputBoard();
                    Console.WriteLine("AI2's turn:");
                    press = Console.ReadKey();
                    if ((press.Modifiers & ConsoleModifiers.Control) != 0)
                    {
                        PlayerVsPreComp(rate1, false, ai1);
                    }
                    if ((press.Modifiers & ConsoleModifiers.Alt) != 0)
                    {
                        PlayerVsPreComp(rate2, true, ai2);
                    }
                    bool[] checkArray2 = { board.IsSpaceEmpty(0), board.IsSpaceEmpty(1), board.IsSpaceEmpty(2), board.IsSpaceEmpty(3), board.IsSpaceEmpty(4), board.IsSpaceEmpty(5), board.IsSpaceEmpty(6), board.IsSpaceEmpty(7), board.IsSpaceEmpty(8) };
                    board.SetSpace(ai2.Move(checkArray2, turn), "O");
                    turn++;
                    if (board.HasWinner("X") || board.HasWinner("O") || board.IsCat())
                    {
                        break;
                    }
                }//end while
                Console.Clear();
                if (board.HasWinner("X"))
                {
                    Console.WriteLine("AI1 has won.");
                    ai1.Eval(true);
                    ai2.Eval(false);
                }//end if
                if (board.HasWinner("O"))
                {
                    Console.WriteLine("AI2 has won.");
                    ai1.Eval(false);
                    ai2.Eval(true);
                }//end if
                if (board.IsCat())
                {
                    Console.WriteLine("It was a tie.");
                    ai1.Eval(false);
                    ai2.Eval(false);
                }//end if
                board.OutputBoard();
                press = Console.ReadKey();
                if ((press.Modifiers & ConsoleModifiers.Control) != 0)
                {
                    PlayerVsPreComp(rate1, false, ai1);
                }
                if ((press.Modifiers & ConsoleModifiers.Alt) != 0)
                {
                    PlayerVsPreComp(rate2, true, ai2);
                }
                board.Clear();
                Console.Clear();
            } //end while
        }     //end compvscomp
示例#3
0
        }     //end PlayerVsComp

        public static void PlayerVsPreComp(int rate, bool pFirst, AI ai)
        {
            GameBoard board = new GameBoard();

            while (true)
            {
                bool cont = false;
                int  turn = 0;
                while (true)
                {
                    if (pFirst)
                    {
                        Console.Clear();
                        ai.OutputDes();
                        Console.WriteLine("Your turn:");
                        board.OutputBoard();
                        board.OutputBoardLocations();
                        while (!cont)
                        {
                            bool   isNumeric;
                            string inp;
                            int    num;
                            do
                            {
                                Console.Write("Enter move: ");
                                inp       = Console.ReadLine();
                                isNumeric = int.TryParse(inp, out num);
                                if (!isNumeric)
                                {
                                    Console.WriteLine("Could not parse the input. Please try again.");
                                }//end if
                                if (num < 0 || num > 8)
                                {
                                    Console.WriteLine("Please enter a valid number.");
                                }//end if
                            } while (!isNumeric || num > 8 || num < 0);
                            if (board.GetSpace(num) == " ")
                            {
                                board.SetSpace(num, "X"); cont = true;
                            }
                            else
                            {
                                Console.WriteLine("That space is not available.");
                            }
                        }//end while
                        if (board.HasWinner("X") || board.HasWinner("O") || board.IsCat())
                        {
                            break;
                        }
                        bool[] checkArray = { board.IsSpaceEmpty(0), board.IsSpaceEmpty(1), board.IsSpaceEmpty(2), board.IsSpaceEmpty(3), board.IsSpaceEmpty(4), board.IsSpaceEmpty(5), board.IsSpaceEmpty(6), board.IsSpaceEmpty(7), board.IsSpaceEmpty(8) };
                        board.SetSpace(ai.Move(checkArray, turn), "O");
                    }
                    else
                    {
                        bool[] checkArray = { board.IsSpaceEmpty(0), board.IsSpaceEmpty(1), board.IsSpaceEmpty(2), board.IsSpaceEmpty(3), board.IsSpaceEmpty(4), board.IsSpaceEmpty(5), board.IsSpaceEmpty(6), board.IsSpaceEmpty(7), board.IsSpaceEmpty(8) };
                        board.SetSpace(ai.Move(checkArray, turn), "X");
                        if (board.HasWinner("X") || board.HasWinner("O") || board.IsCat())
                        {
                            break;
                        }
                        Console.Clear();
                        ai.OutputDes();
                        Console.WriteLine("Your turn:");
                        board.OutputBoard();
                        board.OutputBoardLocations();
                        while (!cont)
                        {
                            bool   isNumeric;
                            string inp;
                            int    num;
                            do
                            {
                                Console.Write("Enter move: ");
                                inp       = Console.ReadLine();
                                isNumeric = int.TryParse(inp, out num);
                                if (!isNumeric)
                                {
                                    Console.WriteLine("Could not parse the input. Please try again.");
                                }//end if
                                if (num < 0 || num > 8)
                                {
                                    Console.WriteLine("Please enter a valid number.");
                                }//end if
                            } while (!isNumeric || num > 8 || num < 0);
                            if (board.GetSpace(num) == " ")
                            {
                                board.SetSpace(num, "O"); cont = true;
                            }
                            else
                            {
                                Console.WriteLine("That space is not available.");
                            }
                        } //end while
                    }     //end if
                    turn += 1;
                    cont  = false;
                    if (board.HasWinner("X") || board.HasWinner("O") || board.IsCat())
                    {
                        break;
                    }
                }//end while
                Console.Clear();
                if (board.HasWinner("O"))
                {
                    Console.WriteLine("Player has won.");
                    ai.Eval(false);
                }//end if
                if (board.HasWinner("X"))
                {
                    Console.WriteLine("Computer has won.");
                    ai.Eval(true);
                }//end if
                if (board.IsCat())
                {
                    Console.WriteLine("It was a tie.");
                    ai.Eval(false);
                }//end if
                board.OutputBoard();
                Console.ReadKey();
                board.Clear();
                Console.Clear();
            } //end while
        }     //end PlayerVsComp