public void TestCheckForWinner()
        {
            TicTacToe game = new TicTacToe("Player1", "Player2");

            // Ath vinningsstöðuna [0, 1, 2]
            game.PlayAction(game.Player1, 0);
            Assert.AreEqual(game.CheckForWinner(), null);

            game.PlayAction(game.Player1, 1);
            game.PlayAction(game.Player1, 2);
            Assert.AreEqual(game.Player1.Name, game.CheckForWinner().Name);
        }
Пример #2
0
    // Handle click events from the image buttons on the web page
    protected void ImageButton_Click(object sender, ImageClickEventArgs e)
    {
        // Get a reference to the image button that was clicked
        ImageButton imgBtn = ((ImageButton)sender);
        // Get the number of the button that was clicked
        int bNum = int.Parse(imgBtn.ID.Substring(11));

        // Put an X or O in into the GameGrid array
        int row = bNum / TicTacToe.COLS;    // Get a zero based table row number
        int col = bNum % TicTacToe.COLS;    // Get a zero based table column number

        if (!tttGame.AddMark(row, col))
        {
            lblMessage.Text = "You can't change that!";
        }

        // See if we have a winner
        char winner = tttGame.CheckForWinner();

        if (winner != ' ')
        {
            lblMessage.Text = winner + " Won!";
        }

        UpdateImage(row, col);
        //  ReloadPageData();
    }
Пример #3
0
        // Determines whether X or O won
        public string GetWinner()
        {
            string message = "";
            char   winner  = game.CheckForWinner();

            if (winner != ' ')
            {
                message = winner + " is the winner!";
            }
            return(message);
        }
Пример #4
0
        [InlineData(0, 2, 1, 1, 2, 0)] // diagonal 2
        public void CheckForXWinTest(int r1, int c1, int r2, int c2, int r3, int c3)
        {
            var grid = game.Grid;  // get a byte array to work with

            grid[RowColToIndex(r1, c1)] = (byte)'X';
            grid[RowColToIndex(r2, c2)] = (byte)'X';
            grid[RowColToIndex(r3, c3)] = (byte)'X';
            game.Grid = grid;

            Assert.Equal('X', game.CheckForWinner());
        }
        /// <summary>
        /// Plays the game, create new board, asks the user for input
        /// </summary>
        private void playGame()
        {
            ITicTacToe Ittt = new TicTacToe(user1, user2);
            Play[] board = Ittt.GetGameBoard();

            for (int i = 0; i < board.Length; i++)
            {
                DrawGameBoard(board);
                if (player1ToDo)
                {
                    GetPlayerInput(user1, Ittt);
                    player1ToDo = false;

                    // koma í veg fyrir að hægt sé að velja sama reit oftar en 1 sinni	(player1)
                    player1Move:	// goto playerMove
                    if(board[selectedSquare - 1].Equals(Play.NotPlayed))
                    {
                        board[selectedSquare - 1] = Play.Player1;
                    }
                    else
                    {
                        Console.WriteLine("This square is already selected, please choose another one");
                        GetPlayerInput(user1, Ittt);
                        goto player1Move;
                    }
                }
                else
                {
                    GetPlayerInput(user2, Ittt);
                    player1ToDo = true;
                    // koma í veg fyrir að hægt sé að velja sama reit oftar en 1 sinni	(player2)
                    player2Move:	// goto playerMove
                    if (board[selectedSquare - 1].Equals(Play.NotPlayed))
                    {
                        board[selectedSquare - 1] = Play.Player2;
                    }
                    else
                    {
                        Console.WriteLine("This square is already selected, please choose another one");
                        GetPlayerInput(user2, Ittt);
                        goto player2Move;
                    }
                }

                // Check for a winner or if nobody has yet won at the end of the game
                var check = Ittt.CheckForWinner();
                if (i >= 4 && check != null)
                {
                    DrawGameBoard(board);
                    Console.WriteLine("Player: {0} won the game! woohoo", check.Name);
                    break;
                }
                else if (i == 8)
                {
                    DrawGameBoard(board);
                    Console.WriteLine("Nobody won the game, booo");
                    break;
                }
            }
            Console.WriteLine("Do you want to play again? Press N to quit or any other key to continue");
            playAgain = Console.ReadKey().KeyChar;
        }