Thread: Can you spot debug? [snake]

  1. #46
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Ok, thanks.
    gavra.

  2. #47
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I just focused on the move function.

    But you have two variables there. One is direction which is global. We cannot "follow" it exactly, but it is called "direction" and it is assigned to the board[][]. So we can understand what it means, since you also compare nicely the board with definitions UP, DOWN, LEFT, RIGHT.
    The other is x which is global and passed to move. No idea what it does. Somebody will have to read the whole code to understand what it does.

    As a rule, global variables should have a name to explain themselves. Even if you need a really long name to do that.

    Anyhow, I liked your code, it was just difficult for someone else to debug it Recently I have written a 500 length code that was so messy that now I couldn't even understand it. But it works fine. And I was able to debug it when I was writing it because I remembered what everything did.

    C isn't really easy to read. Actually it is hard. In order for a code in C to be readable you have to follow strickly some rules. Like mastp mentioned and as I mentioned above. On the other hand, you don't need to. If your code isn't meant to be shown or changed from a 3rd person then you can write it as it suits you.

    Don't get me wrong. Your coding style isn't bad. You just have some details wrong, which as noted by mastp can cause bugs. When I enabled the -Wall option to compile it gave a lot of warnings. For my part, I couldn't read only the move() function and spot a bug, cause I had no idea what global variable x is suppose to do.

  3. #48
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    x is updated in the function "input()", and is the key pressed by the user - at least that's how I've interpreted it. But it is a good example of how you would get lost because of global variables [even with good names, but obviously x for "current input" is not a good name].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #49
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    I think it's my only "insignificant variable name".
    It seems to be you havent noticed that I have already solved the problem - no one will have to read the whole code.

    Have anyone here play my game? if so have you seen the cpu plays?
    gavra.

  5. #50
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I played it until the tail turned infinite. But I haven't played with the cpu (if that's what you mean).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #51
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    First of all take the move function from the previous page and replace with the old one.
    Now I intended that you'll see the cpu plays not that you'll play with the cpu even though it doesn't sound a bad idea.
    gavra.

  7. #52
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I played it for quite long. When it was bugged. I liked it. There is a requirement for snake games that divides in good and bad. If they can make a really sharp turn. Yours pass

    For the cpu haven't really seen it. I will though

  8. #53
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Cool [:
    If you want to see the cpu plays you musn't hit anykey for several seconds (7s I think).
    There are some bugs with the cpu he is a fool one cause his lazy programmer dedicated only 5-10 minutes for writing his part XD
    gavra.

  9. #54
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yeah, saw it. Well its a demo, so it's ok
    Next step is to put graphics on your game. Since your at it
    Shouldn't be very difficult and you can have your full own-made snake game.

    I also mentioned some minor bugs. Like the fact that if you press any other key, except arrows or P, the head freezes, the tail follows its trail and ends on the head. So if you start again, hitting another key you have a small one-sized snake.

    Also, when you press up and down on the menu sometimes the arrow doesn't fucntion right. Though this is really a small thing, don't even bother fixing it.

  10. #55
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Yeah I have seen it.
    It's not a big dill just a few seconds [:
    I will add graphics but the only graphics that I know is "borlnad graphics" \:
    I wrote about "allegro" but no relevant comments..
    gavra.

  11. #56
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by gavra View Post
    Yeah I have seen it.
    It's not a big dill just a few seconds [:
    I will add graphics but the only graphics that I know is "borlnad graphics" \:
    I wrote about "allegro" but no relevant comments..
    Well, in java this would be done very easily. You would create window (JPanel, JFrame). Then a you would store your board[][] values in a specific place and call draw() which would update the window.

    I suppose there are similar tools for C or at least C++. Maybe it can be done on WinAPI?

    Don't know personally any tools...

  12. #57
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    JAVA AS C# is easy to work with especially with graphics (this ".net" ruins our world!).
    There is but the problem is that API isn't simple and creating a "quality" graphics in C is a long hard labor \:
    so borland graphics may be enough for lazy programmers like me :]

    What do you know about "forms in C" ?
    gavra.

  13. #58
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Tell me something funny dude have you read my last post?
    gavra.

  14. #59
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I read it. You said Borland graphics was going to be enough for this program.

    I posted a program that used Borland graphics. I have no idea wtf amuses you, but your attitude is somewhat odd for someone who has received so much help, here.

    I guess I can delete the program as easily as I posted it.

    Now that's funny.

  15. #60
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Admittedly, graphics aren't easy in C. Mostly because of the poor API functions available to do so, and partly because it's... well, C.
    But there's no easy way in C++ either due to a lacking easy-to-use framework for doing such.

    But this is C, not C++, or Java.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  2. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  3. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  4. DNS Query
    By Simpsonia in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-24-2006, 12:42 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM