Adblock breaks this site

Help with TryParse

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

  1. ukrebel

    ukrebel Active Member

    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15
    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 :)
     
  2. Molotov

    Molotov Active Member
    Banned

    Joined:
    Aug 26, 2009
    Posts:
    149
    Referrals:
    0
    Sythe Gold:
    0
    Help with TryParse

    I wish i was good at console's. Im good with GUI's tho..
     
  3. ukrebel

    ukrebel Active Member

    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15
    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!
     
  4. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    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.
     
  5. ukrebel

    ukrebel Active Member

    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15
    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
     
  6. ukrebel

    ukrebel Active Member

    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15
    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!
     
  7. blindkilla

    blindkilla Guru
    $25 USD Donor New

    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord
    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.
     
  8. ukrebel

    ukrebel Active Member

    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15
    Help with TryParse

    I changed what you said but it still doesn't work :(!
     
  9. blindkilla

    blindkilla Guru
    $25 USD Donor New

    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord
    Help with TryParse

    Can you post the whole function that you are calling when you send it the column/row the user inputs.
     
  10. ukrebel

    ukrebel Active Member

    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15
    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.
     
  11. ukrebel

    ukrebel Active Member

    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15
    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. ukrebel

    ukrebel Active Member

    Joined:
    Nov 6, 2005
    Posts:
    196
    Referrals:
    1
    Sythe Gold:
    15
    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!.
     
  13. TheKraken

    TheKraken Member

    Joined:
    Jul 3, 2010
    Posts:
    27
    Referrals:
    0
    Sythe Gold:
    0
    Help with TryParse

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


 
 
Adblock breaks this site