简体   繁体   中英

Trouble converting arrays to methods

I am attempting to take a chunck of code that displayed a tic tac toe board and convert it to methods. This is proving to be extremely difficult and although I feel like Im following the rules, I cant get anything to work. I need help figuring out how to make the code work together (for example check in the win or tie box from the user input at the top.

static void()
      {
        int computerSquare = 0;
        int userSquare = 0;
        bool playerTurn = true;
        int whoFirst = 0;
        string winCheck = null;
        string[,] theBoard = new string[3, 3]
        {   { " ", " ", " "},
                { " ", " ", " "},
                { " ", " ", " "}   };
        int placesFilled = 0;


        Random myRandomGenerator = new Random();  //Initializing the random generator

        //Explain the game to the player
        Console.WriteLine("Welcome to Tic-Tac-Toe.  You will be X and the computer will be O.\n");
        Console.WriteLine("On your turn please indicate which square you want to select by entering" +
            " the number as shown below.\n");
        Console.WriteLine(" 1 | 2 | 3");
        Console.WriteLine("---|---|---");
        Console.WriteLine(" 4 | 5 | 6");
        Console.WriteLine("---|---|---");
        Console.WriteLine(" 7 | 8 | 9");
        Console.WriteLine("\nPlease enter any key when you are ready to begin playing. ");
        Console.ReadKey();

        //figure who goes first here, set firstTurn appropriately
        whoFirst = myRandomGenerator.Next(0, 2);
        if (whoFirst == 0)
        {
            playerTurn = false;
            Console.WriteLine("The computer will go first");
            Console.ReadKey();
        }

        //Display the blank board
        Console.Clear();

        Console.WriteLine($" {theBoard[0, 0]} | {theBoard[0, 1]} | {theBoard[0, 2]}");
        Console.WriteLine("---|---|---");
        Console.WriteLine($" {theBoard[1, 0]} | {theBoard[1, 1]} | {theBoard[1, 2]}");
        Console.WriteLine("---|---|---");
        Console.WriteLine($" {theBoard[2, 0]} | {theBoard[2, 1]} | {theBoard[2, 2]}");

    }

    static void CheckForSquare()
    {
        int computerSquare = 0;
        int userSquare = 0;
        bool playerTurn = true;
        int whoFirst = 0;
        string winCheck = null;
        string[,] theBoard = new string[3, 3]
        {   { " ", " ", " "},
                { " ", " ", " "},
                { " ", " ", " "}   };
        int placesFilled = 0; 

        if (playerTurn)
        {
            Console.Write($"\nWhere would you like to place your X? ");

            try
            {
                userSquare = Convert.ToInt32(Console.ReadLine());
            }
            catch (FormatException)
            {
                Console.Clear();
                Console.Write("You must enter an integer. Press any key to continue: ");
                Console.ReadKey();
                Console.Clear();
            }
            catch (OverflowException)
            {
                Console.Clear();
                Console.Write("That value is too large. Press any key to continue: ");
                Console.ReadKey();
                Console.Clear();
            }


            if (userSquare < 4)
            {
                if (theBoard[0, userSquare - 1] != " ")
                {
                    Console.WriteLine("That square is occupied.  Try again.");
                    continue;
                }
                theBoard[0, userSquare - 1] = "X";
                placesFilled++;
            }
            else if (userSquare < 7)
            {
                if (theBoard[1, userSquare - 4] != " ")
                {
                    Console.WriteLine("That square is occupied.  Try again.");
                    continue;
                }
                theBoard[1, userSquare - 4] = "X";
                placesFilled++;
            }
            else if (userSquare < 10)
            {
                if (theBoard[2, userSquare - 7] != " ")
                {
                    Console.WriteLine("That square is occupied.  Try again.");
                    continue;
                }
                theBoard[2, userSquare - 7] = "X";
                placesFilled++;
            }
            else
            {
                Console.WriteLine("You must select a value from 1 - 9");
            }
            playerTurn = false;
            winCheck = "X";
        }
        else
        {
            computerSquare = myRandomGenerator.Next(1, 10);
            //Console.WriteLine(computerSquare);

            if (computerSquare < 4)
            {
                if (theBoard[0, computerSquare - 1] != " ")
                {

                    continue;
                }
                theBoard[0, computerSquare - 1] = "O";
                placesFilled++;
                //break;
            }
            else if (computerSquare < 7)
            {
                if (theBoard[1, computerSquare - 4] != " ")
                {

                    continue;
                }
                theBoard[1, computerSquare - 4] = "O";
                placesFilled++;
                //break;
            }
            else
            {
                if (theBoard[2, computerSquare - 7] != " ")
                {

                    continue;
                }
                theBoard[2, computerSquare - 7] = "O";
                placesFilled++;
                //break;
            }
            playerTurn = true;
            winCheck = "O";
        }
    }

}

static void PrintBoard()
{
    string[,] theBoard = new string[3, 3]
       {   { " ", " ", " "},
                { " ", " ", " "},
                { " ", " ", " "}   };

    Console.Clear();
    Console.WriteLine($" {theBoard[0, 0]} | {theBoard[0, 1]} | {theBoard[0, 2]}");
    Console.WriteLine("---|---|---");
    Console.WriteLine($" {theBoard[1, 0]} | {theBoard[1, 1]} | {theBoard[1, 2]}");
    Console.WriteLine("---|---|---");
    Console.WriteLine($" {theBoard[2, 0]} | {theBoard[2, 1]} | {theBoard[2, 2]}");
}

static void CheckForWinTie()
{
    int computerSquare = 0;
    int userSquare = 0;
    bool playerTurn = true;
    int whoFirst = 0;
    string winCheck = null;
    string[,] theBoard = new string[3, 3]
    {   { " ", " ", " "},
                { " ", " ", " "},
                { " ", " ", " "}   };
    int placesFilled = 0;

    if (theBoard[0, 0] == winCheck && theBoard[0, 1] == winCheck && theBoard[0, 2] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();

        }
    }

    if (theBoard[1, 0] == winCheck && theBoard[1, 1] == winCheck && theBoard[1, 2] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();

        }

    }
    if (theBoard[2, 0] == winCheck && theBoard[2, 1] == winCheck && theBoard[2, 2] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();

        }

    }


    if (theBoard[0, 0] == winCheck && theBoard[1, 0] == winCheck && theBoard[2, 0] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();

        }
    }
    if (theBoard[0, 1] == winCheck && theBoard[1, 1] == winCheck && theBoard[2, 1] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();

        }


    }
    if (theBoard[0, 2] == winCheck && theBoard[1, 2] == winCheck && theBoard[2, 2] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();

        }
    }

    if (theBoard[0, 0] == winCheck && theBoard[1, 1] == winCheck && theBoard[2, 2] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();

        }


    }

    if (theBoard[0, 2] == winCheck && theBoard[1, 1] == winCheck && theBoard[0, 0] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();
        }


    }

    if (placesFilled == 9)
    {
        Console.WriteLine("\nIt's a tie");
        Console.ReadKey();

    }
}

Console.Write("\n(Y) to play again: ");
    string playAgain = Console.ReadLine();
    if (playAgain != "Y" && playAgain != "y")
    {
        break;
    }
Console.Clear();

}

I think what you are asking is about method parameters and return types.

public int Add(int x, int y)
{
   return x + y;
}

In that example Add is the method name, int is the return type of the method, int x and int y are the parameters. When you say you want 'use the data in the previous' I think what you want is to pass data from one method to another. Let's say I wanted to get a number from the user. I could have a method like this:

public int GetUserAnswer()
{
   Console.Write("Enter a number");
   if(int.TryParse(Console.ReadLine(), out int number))
     return number;
   return -1;
}

Then I could have a method that puts the two together.

public void Foo()
{
   int userNumber = GetUserAnswer();
   int userNumber2 = GetUserAnswer();
   int sum = Add(userNumber, userNumber2);
   //do more stuff
}

Now you have two methods working together. The first gets a number form the user and returns it. The second takes 2 numbers that we previously got from the user, adds them together and returns the sum. Hopefully that helps.

With regard to your question about how and where to break up the larger code, look for places where either a block of code is repeated or places where several statements fit together and only do one thing. For example,

public int HandleUserSqaureLessThan4(string[,] theBoard, int userSquare, int PlacesFilled)
{
   if (theBoard[0, userSquare - 1] != " ")
            {
                Console.WriteLine("That square is occupied.  Try again.");
                continue;
            }
            theBoard[0, userSquare - 1] = "X";
            return placesFilled++;
}

Then you could have something like this:

if (userSquare < 4)
{
    HandleUserSqaureLessThan4(theBoard, userSquare, placesFilled);
}
... //the rest of the code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM