示例#1
0
        static void Main(string[] args)
        {
            var ticTacToeObjects = new TicTacToeObjects();
            var inputReader = new InputReader();
            var outputWriter = new OutputWriter();
            ticTacToeObjects.BoardState = new BoardState(outputWriter);
            ticTacToeObjects.HumanPlayer = new HumanPlayer(inputReader);
            ticTacToeObjects.ComputerPlayer = new ComputerPlayer();
            ticTacToeObjects.PieceManager = new PieceManager();

            TicTacToeGame game = new TicTacToeGame(ticTacToeObjects);
            game.RunGame();
            Console.ReadLine();
        }
示例#2
0
        private bool PlaceMove(VisualNode move)
        {
            if (_game.GameEnded)
            {
                return false;
            }

            if (move == null)
            {
                MessageBox.Show("Cannot place the move there!");
                return false;
            }

            s_moves.Push(move);

            tslMoveCount.Text = $"Move: {s_moves.Count}";

            Refresh();

            NodeLocation winDirection;
            if (_game.IsWinningMove(move, out winDirection))
            {
                _game.GameEnded = true;
                // Mark the wining nodes.
                _game.MarkWinningNodes(move, winDirection);
                Refresh();

#if !COMPUTER_AGAINST_ITSELF
                MessageBox.Show($"Winner is player {CurrentPlayer}!!!");
#else

                if (CurrentPlayer == TicTacToeValue.x)
                    winX++;
                else
                    winO++;

                if (s_moves.Count > maxMove)
                    maxMove = s_moves.Count;
                if (s_moves.Count < minMove)
                    minMove = s_moves.Count;

                label3.Text = $"X: {winX}, O: {winO}, min:{minMove}, max:{maxMove}";
#endif

                _game = new TicTacToeGame(NeededForWin);
                s_moves.Clear();
                CurrentPlayer = TicTacToeValue.x;
                tslMoveCount.Text = "";
                Refresh();

#if COMPUTER_AGAINST_ITSELF
                PlaceAIMove();
#endif
                return true;
            }
            else
            {
                ChangePlayer();

#if COMPUTER_AGAINST_ITSELF
                PlaceAIMove();
#endif
            }

            Application.DoEvents();
            return false;
        }
示例#3
0
        static void Main(string[] args)
        {
            TicTacToeGame Game = new TicTacToeGame();

            Game.StartTicTacToe();
        }
 // Given the following board
 public void Given_the_following_board(string[][] tableData)
 {
     _game = new TicTacToeGame(tableData);
 }
 // Given a new game
 public void Given_a_new_game()
 {
     _game = new TicTacToeGame();
 }
示例#6
0
        static void Main(string[] args)
        {
            string playAgain;

            do
            {
                Console.WriteLine("Welcome to Tic Tac Toe game");
                TicTacToeGame tictactoe        = new TicTacToeGame();
                char[]        board            = tictactoe.CreateABoard();
                char          letterOfUser     = tictactoe.ChooseALetter();
                char          letterOfComputer = tictactoe.GetLetterOfComouter(letterOfUser);
                Console.WriteLine("Player choses " + letterOfUser);
                Console.WriteLine("Tic Tac Toe Board :");
                tictactoe.UC3_ShowBoard(board);
                string toss = tictactoe.UC6_WhoPlaysFirst();
                Console.WriteLine();
                Console.WriteLine(toss + " wins the toss.");
                Console.WriteLine();
                if (toss == "computer")
                {
                    Console.WriteLine("Turn for computer");
                    tictactoe.UC8_MakeAMoveForComputer(board, letterOfUser, letterOfComputer);
                    tictactoe.UC3_ShowBoard(board);
                }
                while (true)
                {
                    Console.WriteLine();
                    Console.WriteLine("Turn for user");
                    tictactoe.UC7_MakeAMoveForPlayer(board, letterOfUser);
                    tictactoe.UC3_ShowBoard(board);
                    Console.WriteLine();
                    if (tictactoe.CheckIfWon(board, letterOfUser))
                    {
                        Console.WriteLine("user wins");
                        break;
                    }
                    if (tictactoe.CheckIfBoardIsFull(board))
                    {
                        Console.WriteLine("Its a tie.");
                        break;
                    }
                    Console.WriteLine("Turn for computer");
                    tictactoe.UC8_MakeAMoveForComputer(board, letterOfUser, letterOfComputer);
                    tictactoe.UC3_ShowBoard(board);
                    Console.WriteLine();
                    if (tictactoe.CheckIfWon(board, letterOfComputer))
                    {
                        Console.WriteLine("Computer wins");
                        break;
                    }
                    if (tictactoe.CheckIfBoardIsFull(board))
                    {
                        Console.WriteLine("Its a tie.");
                        break;
                    }
                }
                Console.WriteLine();
                Console.WriteLine("Do you want to play again?(Y/N)");
                playAgain = Console.ReadLine();
                if (playAgain.Equals("y"))
                {
                    playAgain = "Y";
                }
                else if (playAgain.Equals("n"))
                {
                    playAgain = "N";
                }
                while (!(playAgain.Equals("Y") || playAgain.Equals("N")))
                {
                    Console.WriteLine("Enter a valid Input");
                    playAgain = Console.ReadLine();
                    if (playAgain.Equals("y"))
                    {
                        playAgain = "Y";
                    }
                    else if (playAgain.Equals("n"))
                    {
                        playAgain = "N";
                    }
                }
            } while (playAgain != "N");
        }
示例#7
0
 static void Main(string[] args)
 {
     TicTacToeGame tttg = new TicTacToeGame();
 }
示例#8
0
 public TicTacToeMove(TicTacToeGame ticTacToe, int i, char playerSymbol)
 {
     _ticTacToe   = ticTacToe;
     PlayerSymbol = playerSymbol;
     Index        = i;
 }
示例#9
0
        private static void Main(string[] args)
        {
            var game = new TicTacToeGame();

            game.StartGame();
        }
示例#10
0
 public void Setup()
 {
     X    = new MinimaxTicTacToePlayer("player-one (AI)", TicTacToeToken.X);
     O    = new HumanTicTacToePlayer("player-two (Human)", TicTacToeToken.O);
     Game = new TicTacToe.TicTacToeGame(X, O);
 }
示例#11
0
        static void Main(string[] args)
        {
            TicTacToeGame game = new TicTacToeGame();

            Console.WriteLine("Winning positions for playerO:");
            List <Board>  history = new List <Board>();
            Queue <Board> q       = new Queue <Board>();

            q.Enqueue(game.GetInitNode());
            int total = 0;

            while (q.Count > 0)
            {
                Board b    = q.Dequeue();
                Board next = b.FindNextMove(9);
                if (Math.Abs(b.RecursiveScore) >= 200 && next != null)
                {
                    if (b.RecursiveScore < 0 &&
                        !next.GameOver &&
                        history.Find(x => Board.IsSimilarBoard(x, b)) == null)
                    {
                        history.Add(b);
                        Console.WriteLine("[{0}] Winner is {1}:\n{2}, next move is:\n{3}", total, b.RecursiveScore < 0 ? "PlayerO" : "PlayerX", b, next);
                        total++;
                    }
                }
                else
                {
                    foreach (Board c in b.GetChildren())
                    {
                        q.Enqueue(c);
                    }
                }
            }

            bool stop = false;

            while (!stop)
            {
                bool userFirst = false;
                game = new TicTacToeGame();
                Console.WriteLine("User play against computer, Do you place the first step?[y/n]");
                if (Console.ReadLine().StartsWith("y", StringComparison.InvariantCultureIgnoreCase))
                {
                    userFirst = true;
                }

                int depth = 8;
                Console.WriteLine("Please select level:[1..8]. 1 is easiet, 8 is hardest");
                int.TryParse(Console.ReadLine(), out depth);

                Console.WriteLine("{0} play first, level={1}", userFirst ? "User" : "Computer", depth);

                while (!game.Current.IsTerminalNode())
                {
                    if (userFirst)
                    {
                        game.GetNextMoveFromUser();
                        game.ComputerMakeMove(depth);
                    }
                    else
                    {
                        game.ComputerMakeMove(depth);
                        game.GetNextMoveFromUser();
                    }
                }
                Console.WriteLine("The final result is \n" + game.Current);
                if (game.Current.RecursiveScore < -200)
                {
                    Console.WriteLine("PlayerO has won.");
                }
                else if (game.Current.RecursiveScore > 200)
                {
                    Console.WriteLine("PlayerX has won.");
                }
                else
                {
                    Console.WriteLine("It is a tie.");
                }

                Console.WriteLine("Try again?[y/n]");
                if (!Console.ReadLine().StartsWith("y", StringComparison.InvariantCultureIgnoreCase))
                {
                    stop = true;
                }
            }

            Console.WriteLine("bye");
        }
示例#12
0
 // Given the following board
 public void Given_the_following_board(string[][] tableData)
 {
     _game = new TicTacToeGame(tableData);
 }
示例#13
0
 // Given a new game
 public void Given_a_new_game()
 {
     _game = new TicTacToeGame();
 }
示例#14
0
 public void Setup()
 {
     _game = new TicTacToeGame();
 }
 public static void Main(string[] args)
 {
     var game = new TicTacToeGame();
     game.Play();
 }
		/// <summary>
		/// Gets the game started.  Players must have already been added.
		/// </summary>
		public void LaunchGame()
		{
			if (players.Count != 2)
				throw new Exception ("There must be two players for this game!");

			game = new TicTacToeGame (players [0].PlayerPiece, players [1].PlayerPiece);
		}
示例#17
0
        public static void Main(string[] args)
        {
            var game = new TicTacToeGame();

            game.Play();
        }
示例#18
0
 static void Main()
 {
     TicTacToeGame game = new TicTacToeGame();
     game.start();
     
 }
示例#19
0
        static void Main(string[] args)
        {
            bool          Player1Turn = true;
            TicTacToeGame game        = new TicTacToeGame();

            string[] input;
            int      row, col;
            bool     isLegalMove = true;

            while (!game.isGameOver)
            {
                if (isLegalMove)
                {
                    Console.Clear();
                    game.ShowBoard();
                }

                if (Player1Turn)
                {
                    Console.Write("\nPlayer 1, enter row and col seperated by comma: ");
                }
                else
                {
                    Console.Write("\nPlayer 2, enter row and col seperated by comma: ");
                }

                input = Console.ReadLine().Trim().Split(',');
                if (input.Length == 2)
                {
                    if (int.TryParse(input[0].Trim(), out row) && int.TryParse(input[1].Trim(), out col))
                    {
                        // Turn row and col to index
                        row--;
                        col--;

                        if (Player1Turn)
                        {
                            isLegalMove = game.SetMove(row, col, eCellType.X);
                        }
                        else
                        {
                            isLegalMove = game.SetMove(row, col, eCellType.O);
                        }

                        if (isLegalMove)
                        {
                            Player1Turn = !Player1Turn;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid row and/or col");
                        isLegalMove = false;
                    }
                }
                else
                {
                    Console.WriteLine("Invalid amount of inputs");
                    isLegalMove = false;
                }
            }

            Console.Clear();
            game.ShowBoard();
            Console.WriteLine("Game Over");
        }
示例#20
0
        static void Main()
        {
            var game = new TicTacToeGame();

            game.Cols = 3;
            game.Rows = 3;
            game.NewGame();

            var winner = TicTacToeGame.TicTacToePlayer.None;

            while (winner == TicTacToeGame.TicTacToePlayer.None)
            {
                Console.SetCursorPosition(0, 0);
                game.PrintBoard();

                game.SetCursorLocation();
                var key = Console.ReadKey(false);

                if (key.Key == ConsoleKey.UpArrow)
                {
                    game.MoveUp();
                }
                if (key.Key == ConsoleKey.DownArrow)
                {
                    game.MoveDown();
                }
                if (key.Key == ConsoleKey.RightArrow)
                {
                    game.MoveRight();
                }
                if (key.Key == ConsoleKey.LeftArrow)
                {
                    game.MoveLeft();
                }

                if (key.Key == ConsoleKey.Spacebar)
                {
                    game.PlayLocation();
                    winner = game.CheckForWinner();
                }
                if (key.Key == ConsoleKey.R)
                {
                    game.NewGame();
                }
                if (key.Key == ConsoleKey.Escape)
                {
                    return;
                }
            }

            Console.SetCursorPosition(0, 0);
            game.PrintBoard();

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine($"Winner! {winner} ({TicTacToeGame.GetPlayerChar(winner)})");

            Console.WriteLine();
            Console.WriteLine("Press any key to continue...");
            Console.CursorVisible = false;
            Console.ReadKey(true);
            Console.CursorVisible = true;
        }