• Log in or sign up for an account here: https://www.sythe.org/login

Help with TryParse

ukrebel

Active Member
Joined
Nov 6, 2005
Sythe Gold
15
Messages
196
do
{
Console.WriteLine("Please enter an X co-ordinate ( Between 0 - 8)");

X = int.TryParse(Console.ReadLine());

}
while (X <0 || X >8) ;


I am just wondering, if anyone would be able to help me to get it to catch if a person enters a wrong value (such as t or &), for it either to then let it carry on through the loop but set the X to -1 so the while loop catches it. or loops me back to the beginning on the do loop :)


Any help would be much appreciated!

I'm creating MineSweeper if anyone is interested :)
 
I wish i was good at console's. Im good with GUI's tho..
 
So do I :P!

Well I have done the minesweeper game now, just addding some error stopping features.

I had to create this for my uni assignment, hate using the console! can't wait till I can start actually using GUI :)
Had to do it in notepad too which is bloody annoying lol!
 
Member of the Month Winner
Sythe's 10th Anniversary
What you need to do is look at the exceptions int.TryParse() throws, then encapsulate your code in a try/catch block to handle those exceptions.
 
Just been to a lecture and he showed us how to do it, was pretty simple really!

here is how I solved it if anyone is bothered :):

do
{
try
{
Console.WriteLine("Please enter an X co-ordinate ( Between 0 - 8)");

X = int.Parse(Console.ReadLine());
}
catch
{
X = 20;
}


}
while (X < 0 || X > 8);


Whats basically is that the catch part of this is setting the variable X to value 20, this then would get the while part of the loop to loop it back to the beginning to enter a value again :)
I know I have proberly done this a long winded way, but I'm just learning :P
 
Ok Swan you seem to be fairly clever with c# ;)

Wonder if you could help me with the counting mines.

Here is what I have got it to do so far:
6xql8p.jpg


I need to know how to check the mines around the co-ordinate they choose, then I want it to be displayed in that co-ordinate.


Heres what I was using to count the mines:

Code:
 int numberofsurroundingmines = 0;
                                for (int checkCol = cols - 1; checkCol < cols + 2; checkCol++)
                                {
                                    for (int checkrows = rows - 1; checkrows < cols + 2; checkrows++)
                                    {
                                        if ((board[checkCol, checkrows] == MINE) || (board[checkCol, checkrows] == DIFUSED))
                                        {
                                            numberofsurroundingmines++;


                                        }
                                    }

                                }
                                            Console.Write(numberofsurroundingmines);
                                            Console.Write(" ");

Then I set the co-ordinate they choose to VISIBLE.
But it doesn't get them accurately. If anyone can spot an error, or help me find a better solution that would be appreciate!


(obviously I don't want it just to be given to me, I just want to pointers, advice. Otherwise I would'nt be learnign anything :P)

Thanks in advance

Brad!
 
From what I see, I think you're pretty much on the right track. So, if they pick a coordinate, you want to check the surrounding spaces for mines.

Code:
 int numberofsurroundingmines = 0;
                                //changed from + 2 to + 1
                                for (int checkCol = cols - 1; checkCol < cols + 1; checkCol++)
                                {
                                    //changed from + 2 to + 1, and changed cols to rows in the condition
                                    for (int checkrows = rows - 1; checkrows < rows + 1; checkrows++)
                                    {
                                        if ((board[checkCol, checkrows] == MINE) || (board[checkCol, checkrows] == DIFUSED))
                                        {
                                            numberofsurroundingmines++;


                                        }
                                    }

                                }

I changed just some little things. This way, it will in order count the spaces around the coordinate.

If they chose 2-2, it would check:
Outer Loop 1: 1-1, 1-2, 1-3
Outer Loop 2: 2-1, 2-2, 2-3
Outer Loop 3: 3-1, 3-2, 3-3

Each outer loop would go through a row. Let me know if you understand it.
 
I changed what you said but it still doesn't work :(!
 
It's okay buddy, I managed to get it to work!

I'll show you my new code later on, not on the PC with the code on atm :)

Thanks for your help.
 
Code:
static bool minehere(int X, int Y)
        {
            if (X < 0 || X > 8 || Y < 0 || Y > 8)
            {
                return false;
            }
            if (board[X, Y] == MINE)
            {
                return true;

            }
            else
            {
                return false;
            }

        }

        static int countmines(int X, int Y)
        {
            int result = 0;
            if (minehere(X - 1, Y - 1)) result++;
            if (minehere(X + 1, Y + 1)) result++;
            if (minehere(X + 1, Y - 1)) result++;
            if (minehere(X + 1, Y)) result++;
            if (minehere(X, Y - 1)) result++;
            if (minehere(X - 1, Y)) result++;
            if (minehere(X - 1, Y + 1)) result++;
            if (minehere(X, Y + 1)) result++;



            return result;
        }


So basically the user enters the X and Y co-ordinate then it checks the 8 surrounding squares. I have also checked so it can't go outside of the array in the minehere method.


Any questions, or if you want to suggest any changes just post :P!
 
2w7guuo.png


That is with everything unhidden^^ :)

Well got it all sorted now, thanks for your help.

Can close this thread now if you wish!.
 
Runescape Gamblingrunescape gambling
Back
Top