예제 #1
0
        static void Main(string[] args)
        {
            TicTacToe game = new TicTacToe();

            do
            {
                game.NextPlayer(); // Check who plays next and set that players token X || O
                game.DrawBoard(); // Draw the game board
                Console.Write($"Player {game.PlayerToken} Move: ");
                game.PlayerMove(); // Ask the player for input [1-9] and place token on that square.

                if (game.CheckWin()) // After each turn check if someone won the game
                {
                    game.DrawBoard(); // Draw the board again
                    Console.WriteLine($"Player {game.PlayerToken} wins!");
                    game.Over = true;
                }
                // If no one won, check if there been 9 turns.
                // If so all empty slots on the boards are full and its a draw
                else if (game.Turn == 9)
                {
                    game.DrawBoard();
                    Console.WriteLine("It's a Draw!");
                    game.Over = true;
                }
            }
            while (!game.Over); // Continue as long as the game is not won or a draw.
        }
예제 #2
0
 /// <summary>
 /// The main entry point for the application.
 /// </summary>
 static void Main(string[] args)
 {
     using (TicTacToe game = new TicTacToe())
     {
         game.Run();
     }
 }
예제 #3
0
        static void Main(string[] args)
        {
            string choice = "";

            bool moveSuccess;
            TicTacToe _game = null;

            bool _gameStart = false;

            do
            {
                if (!_gameStart)
                {
                    _gameStart = true;

                    Console.WriteLine("Do you want to start?(Y/N):");
                    choice = Console.ReadLine();

                    _game = new TicTacToe();
                    _game.setStartingPlayer(choice);
                    Console.WriteLine("YOU ARE " + _game.playerMark + "'s");
                }

                if (_game.isPlayersTurn())
                { // player's turn. wait for input
                    Console.WriteLine(_game.ShowBoard());
                    Console.Write("Input Row and Col (i.e. \"A1\"):");
                    choice = Console.ReadLine();
                    moveSuccess = _game.HandleInput(choice);

                    if (!moveSuccess)
                        Console.WriteLine("\"" + choice + "\"Invalid move.");
                }
                else
                {
                    _game.HandleAI();
                }

                if (_game.CheckGameOver() != TicTacToe.GAME_RUNNING)
                {
                    switch (_game.CheckGameOver())
                    {
                        case TicTacToe.GAME_DRAW:
                            Console.WriteLine("~~~GAME DRAW!~~~");
                            break;
                        case TicTacToe.GAME_PLAYER:
                            Console.WriteLine("~~~PLAYER WIN!~~~");
                            break;
                        case TicTacToe.GAME_CPU:
                            Console.WriteLine("~~~COMPUTER WIN!~~~");
                            break;
                    }

                    Console.WriteLine(_game.ShowBoard());

                    _gameStart = false; // restart
                }
            } while (choice != "exit");
        }
예제 #4
0
 public Form1()
 {
     InitializeComponent();
     buttons = new Button[9] { this.field0, this.field1, this.field2, this.field3, this.field4, this.field5, this.field6, this.field7, this.field8 };
     Game = new TicTacToe(this);
     this.NeuralNetwork = new Network(30, 2, 2, 1);
     ifc = new interfaceNetworkGame(this.Game, this.NeuralNetwork);
 }
예제 #5
0
        public static TicTacToe Rematch(TicTacToe lastRoundGame)
        {
            if (!lastRoundGame.IsFinished)
            {
                throw new InvalidOperationException("Last round game was not finished. Play it!");
            }

            return(new TicTacToe(lastRoundGame.PlayerTurn));
        }
예제 #6
0
        }//end class

        /// <summary>
        /// calls all the Play and PrintBoard function to start the game.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            TicTacToe game = new TicTacToe();

            Console.Write("Player 1: X  and  Player 2: O\n\n");
            game.PrintBoard();
            game.Play();
            Console.ReadKey();
        }
예제 #7
0
        public void ShowErrorMessageWhenPlayer2PlaceAnXAsTheSecondMove()
        {
            //Arrange
            TicTacToe ticTacToe = new TicTacToe();

            ticTacToe.Play(Token.X, 0, 0);

            //Assert
            Assert.Throws <Exception>((() => ticTacToe.Play(Token.X, 0, 1)));
        }
예제 #8
0
 public Form1()
 {
     InitializeComponent();
     buttons = new Button[9] {
         this.field0, this.field1, this.field2, this.field3, this.field4, this.field5, this.field6, this.field7, this.field8
     };
     Game = new TicTacToe(this);
     this.NeuralNetwork = new Network(30, 2, 2, 1);
     ifc = new interfaceNetworkGame(this.Game, this.NeuralNetwork);
 }
예제 #9
0
        public void ShowErrorWhenPlayingInAUsedPosition()
        {
            TicTacToe ticTacToe = new TicTacToe();

            ticTacToe.Play(Token.X, 0, 0);
            ticTacToe.Play(Token.O, 1, 2);
            ticTacToe.Play(Token.X, 0, 1);

            Assert.Throws <Exception>(() => ticTacToe.Play(Token.O, 1, 2));
        }
예제 #10
0
        private void btnDefaultName_Click(object sender, EventArgs e)
        {
            Game.player1Name = "Player 1: O";
            Game.player2Name = "Player 2: X";

            this.Hide();
            TicTacToe ttt = new TicTacToe();

            ttt.ShowDialog();
            this.Close();
        }
예제 #11
0
        private void btnPlay_Click(object sender, EventArgs e)
        {
            Game.player1Name = txtFirstPlayer.Text.Trim() + ": O";
            Game.player2Name = txtSecondPlayer.Text.Trim() + ": X";

            this.Hide();
            TicTacToe ttt = new TicTacToe();

            ttt.ShowDialog();
            this.Close();
        }
예제 #12
0
        public void ShowErrorMessageWhenThirdMoveIsAnO()
        {
            //Arrange
            TicTacToe ticTacToe = new TicTacToe();

            ticTacToe.Play(Token.X, 0, 0);
            ticTacToe.Play(Token.O, 0, 1);

            //Assert
            Assert.Throws <Exception>((() => ticTacToe.Play(Token.O, 0, 3)));
        }
예제 #13
0
        protected void Page_Load(object sender, EventArgs e)
        {
            //FIRST TIME LOADING
            if (!IsPostBack)
            {
                ViewState["CurrentPlayer"] = Players.X;
                ViewState["PlayerOne"]     = 0;
                ViewState["PlayerTwo"]     = 0;
                ViewState["PlayerOneWins"] = 0;
                ViewState["PlayerTwoWins"] = 0;
            }
            //Making a Panel To Hold the Buttons
            Panel Panel1 = new Panel();

            Panel1.ID        = "GameBoard";
            Panel1.BackColor = System.Drawing.Color.LightGreen;
            Panel1.Style[HtmlTextWriterStyle.Left]   = "500px";
            Panel1.Style[HtmlTextWriterStyle.Top]    = "500px";
            Panel1.Style[HtmlTextWriterStyle.Width]  = "180px";
            Panel1.Style[HtmlTextWriterStyle.Height] = "180px";
            //Creating the Buttons then Adding them into the Panel
            _PlayBoard = new TicTacToe[9];
            for (int i = 0; i < _PlayBoard.Length; i++)
            {
                _PlayBoard[i] = new TicTacToe(_BitValues[i]);
                _PlayBoard[i].Style[HtmlTextWriterStyle.Width]  = "60px";
                _PlayBoard[i].Style[HtmlTextWriterStyle.Height] = "60px";
                _PlayBoard[i].Text   = " ";
                _PlayBoard[i].Click += Main_Click;
                Panel1.Controls.Add(_PlayBoard[i]);
            }
            //Adding the Panel to the Page
            form1.Controls.Add(Panel1);
            //Showing Who is the Current Player
            Label currentPlayer = new Label();

            currentPlayer.Text = "The Current Player is: Player " + ViewState["CurrentPlayer"].ToString();
            currentPlayer.ID   = "currentPlayer";
            form1.Controls.Add(currentPlayer);
            form1.Controls.Add(new LiteralControl("<br/>"));
            //Creating Labels for the Players to See the Points
            Label playerOne = new Label();

            playerOne.ID   = "playerOne";
            playerOne.Text = "Player One Score: " + ViewState["PlayerOneWins"].ToString();
            form1.Controls.Add(playerOne);
            form1.Controls.Add(new LiteralControl("<br/>"));
            Label playerTwo = new Label();

            playerTwo.ID   = "playerTwo";
            playerTwo.Text = "Player Two Score: " + ViewState["PlayerTwoWins"].ToString();
            form1.Controls.Add(playerTwo);
        }
예제 #14
0
        public MainForm()
        {
            InitializeComponent();

            game = TicTac.Create((int)numericUpDown1.Value, (int)numericUpDown2.Value, (int)numericUpDown3.Value);

            game.Moved  += MovedEvent;
            game.Winner += game_Winner;

            viewGame = new ViewTic();
            viewGame.Create(game, tLP, button_Click);
        }
예제 #15
0
 private void Confirm_Btn_Click(object sender, EventArgs e)
 {
     if (Username_Txt.TextLength >= 4)
     {
         Form game = new TicTacToe(Username_Txt.Text);
         game.Show();
         this.Hide();
     }
     else
     {
         MessageBox.Show("The username must contain at least 4 characters");
     }
 }
예제 #16
0
        public void NamePlayerXTheWinnerWhenHeHas3XInTheThirdColumn()
        {
            TicTacToe ticTacToe = new TicTacToe();

            ticTacToe.Play(Token.X, 0, 2);
            ticTacToe.Play(Token.O, 1, 1);
            ticTacToe.Play(Token.X, 1, 2);
            ticTacToe.Play(Token.O, 2, 1);

            var result = ticTacToe.Play(Token.X, 2, 2);

            Assert.Equal("Player X Wins", result);
        }
예제 #17
0
 static void Main(string[] args)
 {
     try
     {
         TicTacToe.startGame();
     }
     catch (Exception ex)
     {
         Console.WriteLine("Caught unhandled exception: {0}", ex.Message);
         Console.ReadLine();
         throw;
     }
 }
예제 #18
0
        public void NamePlayerOTheWinnerWhenHeHas3OInAColumn()
        {
            TicTacToe ticTacToe = new TicTacToe();

            ticTacToe.Play(Token.X, 0, 0);
            ticTacToe.Play(Token.O, 0, 1);
            ticTacToe.Play(Token.X, 0, 2);
            ticTacToe.Play(Token.O, 1, 1);
            ticTacToe.Play(Token.X, 1, 0);

            var result = ticTacToe.Play(Token.O, 2, 1);

            Assert.Equal("Player O Wins", result);
        }
예제 #19
0
        static void Main(string[] args)
        {
            var game    = new TicTacToe();
            var input   = new ConsoleInput();
            var printer = new ConsolePrinter();

            while (!game.IsGameOver)
            {
                var coords = input.GetInput();
                game.Input(coords.X, coords.Y);
                game.Print(printer);
            }

            Console.WriteLine("Game over!");
        }
예제 #20
0
        private void button3_Click(object sender, EventArgs e)
        {
            Refrech(sender, e);

            viewGame.Clear(tLP);

            try
            {
                game = TicTac.Create((int)numericUpDown1.Value, (int)numericUpDown2.Value, (int)numericUpDown3.Value);

                game.Moved  += MovedEvent;
                game.Winner += game_Winner;
                viewGame.Create(game, tLP, button_Click);
            }
            catch { }
        }
예제 #21
0
        /// <summary>
        /// Starts game
        /// </summary>
        private void StartGame()
        {
            _ticTacToe?.Reset();
            _view.BtnRefresh.Enabled = false;
            _board.Clean();
            RefreshButtons();

            var opponentComboBox = _view.ComboBoxOpponent;

            Enum.TryParse(opponentComboBox.SelectedItem.ToString(), out PlayerType opponentType);

            var difficultyComboBox = _view.ComboBoxDifficulty;

            Enum.TryParse(difficultyComboBox.SelectedItem.ToString(), out DifficultyType difficultyType);

            Player player1 = new HumanPlayer();
            Player player2 = null;

            if (opponentType == PlayerType.Human)
            {
                player2 = new HumanPlayer();
            }
            else
            {
                player2 = new ComputerPlayer(difficultyType);
            }

            player1.PlayersTurn = TicTacToe.Turn.PlayerX;
            player2.PlayersTurn = TicTacToe.Turn.PlayerO;

            _ticTacToe = new TicTacToe(_board, player1, player2, difficultyType);
            _ticTacToe.GameFinishedWin += _ticTacToe_GameFinishedWin;
            _ticTacToe.GameFinishedTie += _ticTacToe_GameFinishedTie;
            _ticTacToe.UIBoardChanged  += _ticTacToe_UIBoardChanged;
            _ticTacToe.TurnChanged     += _ticTacToe_TurnChanged;

            if (!_playerUseX)
            {
                player2.PlayersTurn = TicTacToe.Turn.PlayerX;
                player1.PlayersTurn = TicTacToe.Turn.PlayerO;

                _ticTacToe.PlayerX = player2;
                _ticTacToe.PlayerO = player1;
            }

            _ticTacToe.Start();
        }
예제 #22
0
        //This method runs the two player mode
        private static void gameModeTwo()
        {
            Console.WriteLine("\nWelcome to game mode 2!");
            //intilize the gameboard
            TicTacToe game          = new TicTacToe();
            int       winner        = 0;
            int       currentPlayer = 1;
            int       spotChoice    = 0;

            //This while loop goes on until we have a winner
            while (winner == 0)
            {
                Console.WriteLine($"Player {currentPlayer}, its your turn");
                //display game board
                Console.WriteLine(game.ToString());

                Console.Write("Please enter the spot number: ");
                spotChoice = int.Parse(Console.ReadLine());
                game.PlayTurn(spotChoice, currentPlayer);
                //Determine if a player has won
                if (game.GameResults(game.GameBoard, currentPlayer) != 0 && game.GameResults(game.GameBoard, currentPlayer) != 3)
                {
                    Console.WriteLine(game.ToString());
                    Console.WriteLine($"Congrats player {currentPlayer} is the winner ");
                    winner = currentPlayer;
                }
                //determine if the game ended in a draw
                else if (game.GameResults(game.GameBoard, currentPlayer) == 3)
                {
                    Console.WriteLine(game.ToString());
                    Console.WriteLine($"Good Job all, its a tie !");
                    winner = currentPlayer;
                }
                //switch turns between players
                else
                {
                    if (currentPlayer == 1)
                    {
                        currentPlayer = 2;
                    }
                    else if (currentPlayer == 2)
                    {
                        currentPlayer = 1;
                    }
                }
            }
        }
예제 #23
0
파일: Start.cs 프로젝트: JensR93/TicTacToe
        static public void Main(String[] args)
        {
            var ui = new ConsoleInterface();

            var ConsoleBoard = new ConsoleBoard(3, 5);
            var TicTacToe    =
                new TicTacToe(
                    ui,
                    ConsoleBoard,
                    new[]
            {
                new Player("Player 1", 'X'),
                new Player("Player 2", 'O')
            });

            TicTacToe.Start();
        }
예제 #24
0
        public void ShowMessageWhenTheMatchFinishesInADraw()
        {
            TicTacToe ticTacToe = new TicTacToe();

            ticTacToe.Play(Token.X, 0, 1);
            ticTacToe.Play(Token.O, 0, 0);
            ticTacToe.Play(Token.X, 1, 1);
            ticTacToe.Play(Token.O, 1, 0);
            ticTacToe.Play(Token.X, 1, 2);
            ticTacToe.Play(Token.O, 0, 2);
            ticTacToe.Play(Token.X, 2, 0);
            ticTacToe.Play(Token.O, 2, 1);

            var result = ticTacToe.Play(Token.X, 2, 2);

            Assert.Equal("It's a draw", result);
        }
예제 #25
0
        static void Main(string[] args)
        {
            // Run until game is over or repeat until players want to quit?
            while (true)
            {
                // Show board options (should we do this outside the loop?)
                string board = TicTacToe.CreateDisplayBoard();
                Console.WriteLine(board);

                // Ask player 1 to play

                // Ask player 2 to play

                // Check if game is over
                // Do other logic
                Console.ReadKey();
            }
        }
예제 #26
0
        static void Main(string[] args)
        {
            Console.WriteLine("Lets Play!");
            int    n           = int.Parse(Console.ReadLine());
            string player1Name = Console.ReadLine();
            string player2Name = Console.ReadLine();

            TicTacToe Game = new TicTacToe(n, player1Name, player2Name);

            while (Game.GameStatus == GameStatus.Active)
            {
                bool turn = bool.Parse(Console.ReadLine());
                int  row  = int.Parse(Console.ReadLine());
                int  col  = int.Parse(Console.ReadLine());
                Game.addMove(turn, row, col);
            }
            Console.WriteLine(Game.GameStatus);
            return;
        }
예제 #27
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Please enter a size greater than 3 and less than 10: ");
            int       size = Convert.ToInt32(Console.ReadLine());
            TicTacToe game = new TicTacToe(size);

            game.InitializeBoard();
            game.EstablishWinningBoards();
            while (true)
            {
                if (!game.DidSomeoneWin())
                {
                    game.PrintBoard();
                    game.MakeASelection();
                }
                else
                {
                    break;
                }
            }
        }
예제 #28
0
        /// <summary>
        /// Überladener Konstruktor, der von ApplyMove aufgerufen wird
        /// </summary>
        /// <param name="move"></param>
        /// <param name="oldTicTacToe"></param>
        public TicTacToe(Move move, TicTacToe oldTicTacToe)
        {
            for (int i = 0; i < Height; ++i)
            {
                for (int j = 0; j < Width; ++j)
                {
                    this.allFields[i, j] = oldTicTacToe.allFields[i, j];
                }
            }

            this[move] = oldTicTacToe.ActivePlayer;

            if (oldTicTacToe.ActivePlayer == field.circle)
            {
                this.ActivePlayer = field.cross;
            }
            else
            {
                this.ActivePlayer = field.circle;
            }
        }
예제 #29
0
        static void Main(string[] args)
        {
            string playerName = "";
            bool   gameOver   = false;

            //create players
            Console.WriteLine("Enter player 1 name: ");
            playerName = Console.ReadLine();
            TicTacToe player1 = new TicTacToe(1, playerName);

            Console.WriteLine("Enter player 2 name: (Type 'computer' if you want to play against computer");  //will be computer or human player depending on name
            playerName = Console.ReadLine();
            TicTacToe player2 = new TicTacToe(2, playerName);

            while (!gameOver)  //while loop for determining the game state
            {
                TicTacToe.intBoard();
                while (!player1.Playgame() && !player2.Playgame())
                {
                    gameOver = true;
                }

                if (gameOver)
                {
                    Console.WriteLine("Do you want to play another game Press ESC to quit");
                    if (Console.ReadKey(true).Key == ConsoleKey.Escape)
                    {
                        Console.WriteLine("Good Bye");
                    }
                    else
                    {
                        gameOver = false;
                    }
                }
            }
            Console.ReadLine();
        }
예제 #30
0
        static void Main(string[] args)
        {
            var game = new TicTacToe();

            game.Init();

            while (true)
            {
                game.Render();


                if (game.FoundWinner())
                {
                    Console.WriteLine("We have got a winner.");
                    break;
                }

                if (game.IsFull())
                {
                    break;
                }

                Console.Write("Where do you want to place `{0}` [1-9]: ", game.sign);
                string pos = Console.ReadLine();

                if (game.PlaceMark(pos))
                {
                    game.ChangePlayer();
                }
                else
                {
                    Console.WriteLine("!!You must do something wrong.");
                }
            }
            Console.WriteLine("Game Finished");
            Console.ReadLine();
        }
예제 #31
0
        public IGame ApplyMove(Move move)
        {
            Fields?[,] nextBoard = new Fields?[Width, Height];
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    nextBoard[i, j] = this.Board[i, j];
                }
            }
            switch (this.Player)
            {
            case Fields.cross:
                nextBoard[move.XProperty, move.YProperty] = Fields.cross;
                break;

            case Fields.circle:
                nextBoard[move.XProperty, move.YProperty] = Fields.circle;
                break;
            }
            TicTacToe nextTurn = new TicTacToe(Width, Height, nextBoard, Player == Fields.cross ? Fields.circle : Fields.cross);

            return(nextTurn);
        }
예제 #32
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome To TicTacToe");
            char Choice = 'Y';

            while (Choice == 'Y')
            {
                //Initialise game
                TicTacToe TicTacToeGame = new TicTacToe();

                //choose letter
                TicTacToeGame.ChooseLetter();

                //show blank board
                TicTacToeGame.ShowGameBoard();

                TicTacToeGame.FlipToss();

                TicTacToeGame.PlayUntilGameOver();

                Console.WriteLine("Play again? Y | N :");
                Choice = Console.ReadLine().ToUpper()[0];
            }
        }
예제 #33
0
        public void RunMenu()
        {
            while (endGame)
            {
                TicTacToe tictactoe = new TicTacToe();
                while (continueToRun)
                {
                    File.ReadAllLines(@"C:\Users\13174\Downloads\tictactoe.txt").ToList().ForEach(c => Console.WriteLine(c));
                    if (tictactoe.xTurn < tictactoe.oTurn)
                    {
                        Console.WriteLine("X it's your turn!");
                    }
                    else
                    {
                        Console.WriteLine("O, it's your turn!");
                    }
                    string topRow    = String.Format("   _{0}_|_{1}_|_{2}_", tictactoe.r1c1, tictactoe.r1c2, tictactoe.r1c3);
                    string middleRow = String.Format("   _{0}_|_{1}_|_{2}_", tictactoe.r2c1, tictactoe.r2c2, tictactoe.r2c3);
                    string bottomRow = String.Format("    {0} | {1} | {2} ", tictactoe.r3c1, tictactoe.r3c2, tictactoe.r3c3);
                    Console.WriteLine(topRow);
                    System.Threading.Thread.Sleep(200);
                    Console.WriteLine(middleRow);
                    System.Threading.Thread.Sleep(200);
                    Console.WriteLine(bottomRow);
                    System.Threading.Thread.Sleep(200);
                    Console.WriteLine("Enter the number of the square you'd like to claim:");
                    userInput = (Console.ReadLine()).Trim();
                    userInput.Trim();
                    if (tictactoe.xTurn < tictactoe.oTurn)
                    {
                        switch (userInput)
                        {
                        case "1":
                            if (tictactoe.r1c1 == "O" || "X" == tictactoe.r1c1)
                            {
                                tictactoe.xTurn--; break;
                            }
                            else
                            {
                                tictactoe.r1c1 = "X";
                            }
                            break;

                        case "2":
                            if (tictactoe.r1c2 == "O" || "X" == tictactoe.r1c2)
                            {
                                tictactoe.xTurn--; break;
                            }
                            else
                            {
                                tictactoe.r1c2 = "X";
                            }
                            break;

                        case "3":
                            if (tictactoe.r1c3 == "O" || "X" == tictactoe.r1c3)
                            {
                                tictactoe.xTurn--; break;
                            }
                            else
                            {
                                tictactoe.r1c3 = "X";
                            }
                            break;

                        case "4":
                            if (tictactoe.r2c1 == "O" || "X" == tictactoe.r2c1)
                            {
                                tictactoe.xTurn--; break;
                            }
                            else
                            {
                                tictactoe.r2c1 = "X";
                            }
                            break;

                        case "5":
                            if (tictactoe.r2c2 == "O" || "X" == tictactoe.r2c2)
                            {
                                tictactoe.xTurn--; break;
                            }
                            else
                            {
                                tictactoe.r2c2 = "X";
                            }
                            break;

                        case "6":
                            if (tictactoe.r2c3 == "O" || "X" == tictactoe.r2c3)
                            {
                                tictactoe.xTurn--; break;
                            }
                            else
                            {
                                tictactoe.r2c3 = "X";
                            }
                            break;

                        case "7":
                            if (tictactoe.r3c1 == "O" || "X" == tictactoe.r3c1)
                            {
                                tictactoe.xTurn--; break;
                            }
                            else
                            {
                                tictactoe.r3c1 = "X";
                            }
                            break;

                        case "8":
                            if (tictactoe.r3c2 == "O" || "X" == tictactoe.r3c2)
                            {
                                tictactoe.xTurn--; break;
                            }
                            else
                            {
                                tictactoe.r3c2 = "X";
                            }
                            break;

                        case "9":
                            if (tictactoe.r3c3 == "O" || "X" == tictactoe.r3c3)
                            {
                                tictactoe.xTurn--; break;
                            }
                            else
                            {
                                tictactoe.r3c3 = "X";
                            }
                            break;

                        default:
                            tictactoe.xTurn--;
                            break;
                        }
                        tictactoe.xTurn++;
                    }
                    else
                    {
                        switch (userInput)
                        {
                        case "1":
                            if (tictactoe.r1c1 == "X" || "O" == tictactoe.r1c1)
                            {
                                tictactoe.oTurn--; break;
                            }
                            else
                            {
                                tictactoe.r1c1 = "O";
                            }
                            break;

                        case "2":
                            if (tictactoe.r1c2 == "X" || "O" == tictactoe.r1c2)
                            {
                                tictactoe.oTurn--; break;
                            }
                            else
                            {
                                tictactoe.r1c2 = "O";
                            }
                            break;

                        case "3":
                            if (tictactoe.r1c3 == "X" || "O" == tictactoe.r1c3)
                            {
                                tictactoe.oTurn--; break;
                            }
                            else
                            {
                                tictactoe.r1c3 = "O";
                            }
                            break;

                        case "4":
                            if (tictactoe.r2c1 == "X" || "O" == tictactoe.r2c1)
                            {
                                tictactoe.oTurn--; break;
                            }
                            else
                            {
                                tictactoe.r2c1 = "O";
                            }
                            break;

                        case "5":
                            if (tictactoe.r2c2 == "X" || "O" == tictactoe.r2c2)
                            {
                                tictactoe.oTurn--; break;
                            }
                            else
                            {
                                tictactoe.r2c2 = "O";
                            }
                            break;

                        case "6":
                            if (tictactoe.r2c3 == "X" || "O" == tictactoe.r2c3)
                            {
                                tictactoe.oTurn--; break;
                            }
                            else
                            {
                                tictactoe.r2c3 = "O";
                            }
                            break;

                        case "7":
                            if (tictactoe.r3c1 == "X" || "O" == tictactoe.r3c1)
                            {
                                tictactoe.oTurn--; break;
                            }
                            else
                            {
                                tictactoe.r3c1 = "O";
                            }
                            break;

                        case "8":
                            if (tictactoe.r3c2 == "X" || "O" == tictactoe.r3c2)
                            {
                                tictactoe.oTurn--; break;
                            }
                            else
                            {
                                tictactoe.r3c2 = "O";
                            }
                            break;

                        case "9":
                            if (tictactoe.r3c3 == "X" || "O" == tictactoe.r3c3)
                            {
                                tictactoe.oTurn--; break;
                            }
                            else
                            {
                                tictactoe.r3c3 = "O";
                            }
                            break;

                        default:
                            tictactoe.oTurn--;
                            break;
                        }
                        tictactoe.oTurn++;
                    }
                    Console.Clear();
                    string winner = tictactoe.CheckWinCondition();
                    if (winner == "X" || winner == "O")
                    {
                        continueToRun = false;
                        break;
                    }
                    else if (tictactoe.xTurn == 5 && tictactoe.oTurn == 4.1)
                    {
                        Console.WriteLine("We have a draw");
                        break;
                    }
                    continue;
                }
                File.ReadAllLines(@"C:\Users\13174\Downloads\tictactoe.txt").ToList().ForEach(c => Console.WriteLine(c));
                Console.WriteLine("Would you like to play again?\n" +
                                  "\n" +
                                  "\n" +
                                  "1. Play Again\n" +
                                  "2. Exit");
                endInput = (Console.ReadLine()).Trim();
                switch (endInput)
                {
                case "1":
                    continueToRun = true;
                    Console.Clear();
                    continue;

                case "2":
                    endGame = false;
                    break;
                }
            }
        }
예제 #34
0
 static void Main(string [] args)
 {
     using (var game = new TicTacToe()) {
         game.Run();
     }
 }
예제 #35
0
 static void Main(string[] args)
 {
     TicTacToe game = new TicTacToe();
     game.PrintBoard();
     game.Play();
 }
예제 #36
0
 public void Setup()
 {
     sut = new TicTacToe();
 }
예제 #37
0
        static void Main(string[] args)
        {
            TicTacToe BLineTCT = new TicTacToe();
            Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);
            Console.WriteLine("\n******* Game of Tic-Tac-Toe*******\n");
            Console.WriteLine(" Developed By: Saroj Poudyal [email protected]");
            Console.Write("\n=================================================================================================");
            Console.WriteLine("\n\n Press any key to play or Press 'H' for help.");

            String input = Console.ReadLine().ToString();
            if (input.ToUpper() == "H")
            {
                //print help - Pulled from Wikipedia :)
                Console.Write("\n\n=====================================================================================================================================\n\n");
                Console.Write("\n Tic-tac-toe is a game for two players, X and O, who take turns marking their marks in a 3×3 grid.\n");
                Console.Write("\n The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal row wins the game.\n");
                Console.Write("\n The game supports one player playing against an automated computer player. The automated computer player picks moves at random --no A.I.\n");
                Console.Write("\n picture of the 3X3 grid displayed below:\n\n");

                BLineTCT.DrawBoard("Help");

                Console.Write("\n\n=====================================================================================================================================\n\n");

                Console.Write("\n  Press any key to play the game. \n");
                Console.ReadLine();
            }
            Console.Clear();
            Console.Write("\n Enter your Name:");
            BLineTCT.humanName = Console.ReadLine().ToString();
            if (BLineTCT.humanName == "")
                BLineTCT.humanName = "Human";

            Console.Write("\n\n {0} since you are human, you always get to go first, pick Nought (O) or Cross (X) as your sign.", BLineTCT.humanName);
            Console.Write("\n\n Press 'O' for Nought (O) or 'X' for Cross (X): ");
            input = Console.ReadLine().ToString();
            while (input.ToUpper() != "O" && input.ToUpper() != "X")
            {
                Console.Write("\n Wrong input, you can only pick 'O' or 'X', Try again: ");
                input = Console.ReadLine().ToString();
            }

            BLineTCT.humanSign = input.ToUpper().ToCharArray()[0];
            if (input.ToUpper() == "O")
                BLineTCT.computerSign = 'X';
            else
                BLineTCT.computerSign = 'O';

            BLineTCT.WinnerFound = false;
            while (!BLineTCT.WinnerFound)
            {
                Console.Clear();
                BLineTCT.DrawBoard("Help");
                if (!BLineTCT.WinnerFound)
                {
                    BLineTCT.HumanMove();
                }
                if (!BLineTCT.WinnerFound)
                {
                    BLineTCT.ComputerMove();
                }
            }
            Console.WriteLine(" Game over.Press any key to continue.");
            Console.ReadLine();
        }
예제 #38
0
 public AI(TicTacToe t)
 {
     _game = t;
 }
예제 #39
0
        static void Main(string[] args)
        {

            while (true)
            {
                Console.WriteLine("Please enter X or O as your token for the game: ");
                char userToken = Console.ReadLine().Trim().ToUpper()[0];
                if (userToken == '0')
                    userToken = 'O';
                char aiToken = userToken.Equals('X') ? 'O' : 'X';

                Console.WriteLine();
                Console.WriteLine("Do you want to start the game?");
                Console.WriteLine("Type Y for YES");
                Console.WriteLine("Type N for NO");
                char start = Console.ReadLine().Trim().ToUpper()[0];

                bool userStarts = start.Equals('Y') ? true : false;

                TicTacToe ttt = new TicTacToe(userToken, aiToken, userStarts);
                AI ai = new AI(ttt);

                bool isThereWinner = false;
                bool isDraw = false;

                while (!isThereWinner && !isDraw)
                {
                    ttt.toggleTurn();
                    if (ttt.isUserTurn())
                    {
                        short pos;
                        do
                        {
                            Console.WriteLine("Enter a valid position to insert your token: ");
                            pos = Int16.Parse(Console.ReadLine()[0].ToString());
                        } while (!ttt.isValidMove(pos));
                        
                        ttt.play(pos);
                    }
                    else
                    {
                        ai.play();
                    }
                    isThereWinner = ttt.isThereAWinner();
                    isDraw = ttt.isDraw();
                }

                if (isThereWinner)
                {
                    Console.WriteLine("We have a WINNER!");
                    Console.WriteLine(ttt.getWinner() + " is the winner!!!");
                }
                else
                {
                    Console.WriteLine("Its a DRAW!");
                }
                
                Console.WriteLine();
                Console.WriteLine("Do you want to play another game? If yes, press Y else press any other key.");
                string toContinue = Console.ReadLine()[0].ToString();
                if (!toContinue.Equals("Y", StringComparison.InvariantCultureIgnoreCase))
                    break;
            }
        }
예제 #40
0
 public interfaceNetworkGame(TicTacToe Game, Network Net)
 {
     this.Game = Game;
     this.Net = Net;
     field = new float[18];
     output = new float[9];
 }