Sythe.Org Forums     Register     FAQ     Members List     Calendar     Mark Forums Read    
 
Sythe.Org Forums  
   Runescape Gold

Sythe.org — A Virtual Goods Trading Hub

Make real cash! buying and selling in-game items.

We have a no-scam policy.

You can make thousands playing your favourite games here at Sythe.org.

Just sign up an account and follow the rules!


Take me to

Runescape Markets

Other Game Markets

Support Center

Register an Account

Close
Help with TryParse
Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 11-25-2009, 08:22 PM
Active Member
 
Join Date: Nov 2005
Posts: 158
Default 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.
Reply With Quote
  #2  
Old 11-26-2009, 03:23 AM
Active Member

BANNED
 
Join Date: Aug 2009
Location: New York
Posts: 149
Send a message via MSN to Molotov
Default Re: Help with TryParse

I wish i was good at console's. Im good with GUI's tho..
Reply With Quote
  #3  
Old 11-26-2009, 09:42 AM
Active Member
 
Join Date: Nov 2005
Posts: 158
Default 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!
Reply With Quote
  #4  
Old 11-26-2009, 09:57 AM
Swan's Avatar
When They Cry...
Ex-Moderator
 
Join Date: Jan 2007
Location: Japan
Posts: 4,404
Default 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
Reply With Quote
  #5  
Old 11-26-2009, 12:51 PM
Active Member
 
Join Date: Nov 2005
Posts: 158
Default 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.
Reply With Quote
  #6  
Old 11-26-2009, 01:05 PM
Active Member
 
Join Date: Nov 2005
Posts: 158
Default 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!
Reply With Quote
  #7  
Old 11-29-2009, 06:42 PM
Guru
 
Join Date: Jun 2005
Location: Canada
Posts: 1,844
Default 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.
Reply With Quote
  #8  
Old 12-01-2009, 11:20 AM
Active Member
 
Join Date: Nov 2005
Posts: 158
Default Re: Help with TryParse

I changed what you said but it still doesn't work !
Reply With Quote
  #9  
Old 12-01-2009, 05:21 PM
Guru
 
Join Date: Jun 2005
Location: Canada
Posts: 1,844
Default Re: Help with TryParse

Quote:
Originally Posted by ukrebel View Post
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.
__________________

Reply With Quote
  #10  
Old 12-02-2009, 11:56 AM
Active Member
 
Join Date: Nov 2005
Posts: 158
Default 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.
Reply With Quote
  #11  
Old 12-02-2009, 06:49 PM
Active Member
 
Join Date: Nov 2005
Posts: 158
Default 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!
Reply With Quote
  #12  
Old 12-02-2009, 06:55 PM
Active Member
 
Join Date: Nov 2005
Posts: 158
Default 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!.
Reply With Quote
Reply



Cheap RS Gold Store  Runescape Gold

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

All times are GMT +1. The time now is 04:51 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.6.1