Help with TryParse

Discussion in 'Programming General' started by ukrebel, Nov 25, 2009.

Help with TryParse
  1. Unread #1 - Nov 25, 2009 at 3:22 PM
  2. ukrebel
    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15

    ukrebel Active Member

    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 :)
     
  3. Unread #2 - Nov 25, 2009 at 10:23 PM
  4. Molotov
    Joined:
    Aug 26, 2009
    Posts:
    149
    Referrals:
    0
    Sythe Gold:
    0

    Molotov Active Member
    Banned

    Help with TryParse

    I wish i was good at console's. Im good with GUI's tho..
     
  5. Unread #3 - Nov 26, 2009 at 4:42 AM
  6. ukrebel
    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15

    ukrebel Active Member

    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!
     
  7. Unread #4 - Nov 26, 2009 at 4:57 AM
  8. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    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.
     
  9. Unread #5 - Nov 26, 2009 at 7:51 AM
  10. ukrebel
    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15

    ukrebel Active Member

    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
     
  11. Unread #6 - Nov 26, 2009 at 8:05 AM
  12. ukrebel
    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15

    ukrebel Active Member

    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:
    [​IMG]

    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!
     
  13. Unread #7 - Nov 29, 2009 at 1:42 PM
  14. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    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.
     
  15. Unread #8 - Dec 1, 2009 at 6:20 AM
  16. ukrebel
    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15

    ukrebel Active Member

    Help with TryParse

    I changed what you said but it still doesn't work :(!
     
  17. Unread #9 - Dec 1, 2009 at 12:21 PM
  18. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    Help with TryParse

    Can you post the whole function that you are calling when you send it the column/row the user inputs.
     
  19. Unread #10 - Dec 2, 2009 at 6:56 AM
  20. ukrebel
    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15

    ukrebel Active Member

    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.
     
  21. Unread #11 - Dec 2, 2009 at 1:49 PM
  22. ukrebel
    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15

    ukrebel Active Member

    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!
     
  23. Unread #12 - Dec 2, 2009 at 1:55 PM
  24. ukrebel
    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15

    ukrebel Active Member

    Help with TryParse

    [​IMG]

    That is with everything unhidden^^ :)

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

    Can close this thread now if you wish!.
     
  25. Unread #13 - Jul 3, 2010 at 4:34 AM
  26. TheKraken
    Joined:
    Jul 3, 2010
    Posts:
    27
    Referrals:
    0
    Sythe Gold:
    0

    TheKraken Member

    Help with TryParse

    im interested in minesweep
     
< Simple Console using Lua and Reflection | I need some programming help, specifically in python >

Users viewing this thread
1 guest


 
 
Adblock breaks this site