Naughts and Crosses (Tic Tac Toe), subroutine query

641 views Asked by At

So I'm at the final stage of my Noughts and Crosses project and I'm quite dearly stuck, I have done move validation subroutine as well as a subroutine that is solely based on changing the blank space in the box into an " X " or an " O ", yet my code seems to tell me that some part of my code does not exist in the current context and I am completely baffled

The code is:

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        string[,] grid = new string[3, 3] {{" "," "," "},
                                           {" "," "," "},
                                           {" "," "," "}};
        string board = System.IO.File.ReadAllText("E:\\BoardGame.txt");
        Console.WriteLine(board);
        int player = 0;
        var XCoordinate = 0;
        var YCoordinate = 0;
        int x, y;
        GetMoveCoordinates(ref XCoordinate, ref YCoordinate);
        if (player == 0)
        {                
            grid[XCoordinate, YCoordinate] = " X ";
            player++;
        }
        else
        {
            grid[XCoordinate, YCoordinate] = " O ";
            player--;
        }            
        UpdateGrid(grid, box);
        if (player == 1)
        {
        }

    }


    public static void GetMoveCoordinates(ref int XCoordinate, ref int YCoordinate)
    {
        int CommaLocation;
        bool GameHasBeenWon = false;
        string CoordinatesInput;
        string XChar, YChar;
        while (GameHasBeenWon == false)
        {
            try
            {
                Console.Write("Enter your coordinates: (x,y) ");
                CoordinatesInput = Console.ReadLine();
                CommaLocation = CoordinatesInput.IndexOf(",".ToString());
                XChar = CoordinatesInput.Substring(CommaLocation - 1, CommaLocation);
                YChar = CoordinatesInput.Substring(CommaLocation + 1);
                XCoordinate = int.Parse(XChar);
                YCoordinate = int.Parse(YChar);
            }
            catch
            {
                Console.WriteLine("Invalid Input- Please Try Again");
            }
        }
    }


    public static bool CheckValidMove(int XCoordinate, int YCoordinate, string[,] Board)
    {
        if ((XCoordinate >= 1) || (XCoordinate <= 3))
        {
            if ((YCoordinate >= 1) || (YCoordinate <= 3))
            {
                if ((Board[XCoordinate, YCoordinate]) == " ")
                {
                    return true;
                }
                else return false;
            }
            else return false;
        }
        else return false;
    }


    public static void UpdateGrid(string[,] grid, string box)
    {
        Console.Clear();
        for (int x = 0; x < grid.GetLength(0); x++)
        {
            for (int y = 0; y < grid.GetLength(1); y++)
            {
                box = box.Replace((x + 1) + "," + (y + 1), grid[y, x]);
            }
        }
        // In the case not required as clearning the console default the cursor back to 0,0, but left in 
        // as an example
        Console.SetCursorPosition(0, 0);
        Console.WriteLine(box);
    }
}
}

Yet the problem I seem to have is under Main, under the if statement where the code seems to tell me that box in the Update(grid,box), does not exist in the current context, yet it should do in the last subroutine? Am I supposed to do it as a ref statement or am I missing something? Also if you have any tips on how to tidy up the code I'd gladly appreciate it (yes I will add win parameters but I'd like to draw my symbols first).

This is what the grid looks like this:

 +---+---+---+
 |   |   |   |
 +---+---+---+
 |   |   |   |
 +---+---+---+
 |   |   |   |
 +---+---+---+
1

There are 1 answers

0
EJoshuaS - Stand with Ukraine On

There are several bugs here. First, the following won't compile:

UpdateGrid(grid, box);

As Andrew Whitaker indicated in the comments, there is no "box" variable in your main method (you never declared or initialized it). Define what that variable is and initialize it properly and that'll compile again.

Next, a quick stylistic note on the following:

while (GameHasBeenWon == false)

Don't explicitly compare to true and false - the correct way to do this is

while (!GameHasBeenWon)

The next line to comment on has several bugs:

(XCoordinate >= 1) || (XCoordinate <= 3)

This means that XCoordinate >= 1 OR it's less than or equal to 3, which isn't at all what you meant. Actually, by this logic, any integer is valid because it's either greater than 1, equal to 1, less than 3, or equal to 3. (Think about it - for what integers could a statement like this possibly be false?) Also, 3 is specifically not a valid index, but 0 is. Keep in mind that arrays are zero-indexed. Thus, this should actually be

(XCoordinate >= 0) && (XCoordinate < 3)

In terms of your if statements:

if ((Board[XCoordinate, YCoordinate]) == " ")
            {
                return true;
            }
            else return false;

returns true exactly when (Board[XCoordinate, YCoordinate]) == " " is true and false exactly when that statement is false. You could just do

return (Board[XCoordinate, YCoordinate]) == " ";

In fact, you could do that for the whole "if" statement. (I'm not sitting in front of an IDE right now so I apologize if my syntax isn't perfect here).

return ((XCoordinate >= 0) && (XCoordinate < 3) &&
        ((YCoordinate >= 0) && (YCoordinate < 3)) &&
        ((Board[XCoordinate, YCoordinate]) == " "));