 |

11-25-2009, 08:22 PM
|
|
Active Member
|
|
Join Date: Nov 2005
Posts: 158
|
|
Help with TryParse
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 
Last edited by ukrebel : 11-25-2009 at 09:05 PM.
|

11-26-2009, 03:23 AM
|
|
Active Member
BANNED
|
|
Join Date: Aug 2009
Location: New York
Posts: 149
|
|
Re: Help with TryParse
I wish i was good at console's. Im good with GUI's tho..
|

11-26-2009, 09:42 AM
|
|
Active Member
|
|
Join Date: Nov 2005
Posts: 158
|
|
Re: Help with TryParse
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!
|

11-26-2009, 09:57 AM
|
 |
When They Cry...
|
|
Join Date: Jan 2007
Location: Japan
Posts: 4,404
|
|
Re: Help with TryParse
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.
__________________
Quote:
[14:54:13] <&Filefragg> x9 is made of 5 glow in the dark gorilla dildos vibrating in unison.
[14:54:19] <%TDD> hot
[14:54:22] <+Aoi> Win
[14:54:23] <x9> I TOLD YOU NOT TO TELL ANYONE, DAMNIT
|
|

11-26-2009, 12:51 PM
|
|
Active Member
|
|
Join Date: Nov 2005
Posts: 158
|
|
Re: Help with TryParse
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
Last edited by ukrebel : 11-26-2009 at 12:52 PM.
|

11-26-2009, 01:05 PM
|
|
Active Member
|
|
Join Date: Nov 2005
Posts: 158
|
|
Re: Help with TryParse
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:
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!
|

11-29-2009, 06:42 PM
|
|
Guru
|
|
Join Date: Jun 2005
Location: Canada
Posts: 1,844
|
|
Re: Help with TryParse
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.
Last edited by blindkilla : 11-29-2009 at 07:27 PM.
|

12-01-2009, 11:20 AM
|
|
Active Member
|
|
Join Date: Nov 2005
Posts: 158
|
|
Re: Help with TryParse
I changed what you said but it still doesn't work  !
|

12-01-2009, 05:21 PM
|
|
Guru
|
|
Join Date: Jun 2005
Location: Canada
Posts: 1,844
|
|
Re: Help with TryParse
Quote:
Originally Posted by ukrebel
I changed what you said but it still doesn't work  !
|
Can you post the whole function that you are calling when you send it the column/row the user inputs.
|

12-02-2009, 11:56 AM
|
|
Active Member
|
|
Join Date: Nov 2005
Posts: 158
|
|
Re: Help with TryParse
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.
|

12-02-2009, 06:49 PM
|
|
Active Member
|
|
Join Date: Nov 2005
Posts: 158
|
|
Re: Help with TryParse
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!
|

12-02-2009, 06:55 PM
|
|
Active Member
|
|
Join Date: Nov 2005
Posts: 158
|
|
Re: Help with TryParse
That is with everything unhidden^^
Well got it all sorted now, thanks for your help.
Can close this thread now if you wish!.
|

07-03-2010, 09:34 AM
|
 |
Member
|
|
Join Date: Jul 2010
Posts: 27
|
|
Re: Help with TryParse
im interested in minesweep
|
 |
|