+ Reply to Thread
Results 1 to 7 of 7

Thread: Need Help With Assignment (Using C)

  1. #1
    Newbie fantanoice is an unknown quantity at this point fantanoice's Avatar
    Join Date
    Apr 2009
    Posts
    26

    Need Help With Assignment (Using C)

    Please don't provide the code for this as I do not want to plagiarize anybody's work. I need help understanding and using the code more than anything so I can write the code myself. Thanks in advance to anybody that can help.

    So, I'm doing an assignment for my course and although I've done some programming before, I am completely new to C. The instructions for using the code are very unclear as well, considering we've never used this particular code library before (<conio.h>).

    So like I said, I'm using the <conio.h> library (and of course <stdio.h>). My assignment is a snail game, where the snail is represent by an "*" and leaves a "." trail behind it when it moves. If the snail crosses its path, the score goes down. If it crosses an empty space, the score goes up. (The score is shown in the top corner). The snail cannot move outside the boundaries of the game screen (24 x 80).

    So basically, I need help with developing the code. I've successfully managed to get the '*' to appear in the centre of the screen, but I need to know how the program can get a key from the keyboard (the NumPad arrow keys) to make the snail move in the corresponding directions. The assignment criteria sheet provides some code used to help (such as char getch()) but I have no clue how to interpret what they've written.

    They provide us with these <conio.h> functions:
    - void clrscr() clear the (DOS) screen
    - char getch() get a character from the keyboard without displaying it on the screen (blocking†)
    - void putchxy(int x, int y, char c) place the character c on the screen at
    coordinates (x,y)
    - void gotoxy(int x, int y) move the cursor to the position at
    coordinate (x,y)
    - void cputsxy(int x, int y, char *s) print the string pointed to by s on
    the screen at (x,y)
    - _conio_gettext(int x, int y, int x2, int y2, char *buffer) Go to position (x, y), select all characters between (x, y) and (x2, y2) and copy them into the char array called buffer. NOTE: if x==x2 and y==y2, only one character (at (x,y)) will be copied. You must first create a char array to receive the copied character(s). For this assignment, a buffer with a size of [4] will be sufficient.
    (As you can see, they provide us with what we can use, but not how to use them)


    Please, like I said before, do not provide the code to do my assignment. I must write it all myself. I need explanations on how to write up my code. Heck, maybe even simple "English" explanations of what those functions are, what they do and how to use them will be enough for me to do it all myself.


    So anyway, thanks in advance. I really hope this isn't asking too much (or too little. I'm kinda new here if you hadn't noticed.)

  2. #2
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,374

    Re: Need Help With Assignment (Using C)

    I only know C# on .NET so I can't even make the code for you. But let me release some of my brain steam.

    For first, you need data representation. Ok, I made that term up, but I am still right. So for example, a 2-dimentional array of bools for dots (the trail) and x,y integers for asterisk (the slurm) will do. And some integer for score, obviously. I am sure you figured those out already.

    Now how to make the game. Most of games usually have a common loop, that gets input from keyboard and then updates the variables and then displays them on screen. You could divide your code into 3 such sections, maybe 3 methods in a loop.

    Getting arrowkeys state could be tricky, I think. Something tells me that it is only possible using non-blocking function but I can't tell really. I only use .NET code. :001_unsure:

    When you wanna move the snail you should make few things, in some correct order. You can update the array of trail using before-move coordinates of the slurm. You can check is he moving over his own (blah) trail using after-move coordinates. And those are determined by keyboard arrowkeys.

    And now the 3rd part of code, the displaing. You see, you can update the whole screen every time, like clear all then put whole trail and slurm every time. Another way is to redraw only before-move trail point, and after-move slurm. Drawing a wall to show where you cannot move with slurm would be a good idea, too.

    Be aware that if slurm is at the edge you should make it impossible to move out of the array dimentions. Otherwise he will fall and die horrible death.

    Regards,
    Arek Bulski.

  3. #3
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,576
    Blog Entries
    57

    Re: Need Help With Assignment (Using C)

    I would handle placing your characters with "void putchxy(int x, int y, char c)" Experiment a little to get a sense of what it does, but I would expect it to place a character c at position (x,y), where the upper left is (0,0) and x increases to the right, y increases down.

    From what I recall, an arrow key actually sends 2 characters, so you will need to use getch() twice to first detect that a special key has been pressed, then which one (F1-F12, arrows etc).
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #4
    Newbie fantanoice is an unknown quantity at this point fantanoice's Avatar
    Join Date
    Apr 2009
    Posts
    26

    Re: Need Help With Assignment (Using C)

    @ Arek
    Well, I have done game programming before (just not in C), so I pretty much know the general idea of how games function. I just don't know how to use the code provided for me. (They really didn't prepare us well for the assignment.)

    @ WP
    I'm pretty sure the arrows I use are the NumPad ones (2, 4, 6, 8). Also, the void putchxy is the only thing I (somehow) managed to get working.


    Well, thanks for trying to help.

  5. #5
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,374

    Re: Need Help With Assignment (Using C)

    To be honest, using C seems *so* intimidating to me, that I don't know should I open my mounth at all. :001_huh:

    I dig up a page with some getch() example. Let me sketch some C code, I hope it will be of any help to you. I know that it is using A/D but at least you can make the game running using those for a start, no?
    C And C++ - Getch() Function | DreamInCode.net

    Code:
    int c = 1;
    while(1)
    {
    c = getch();
    if(c == 27) exit(1);
    if(c == 'a' || c == 'A') moveslurmleft();
    if(c == 'd' || c == 'D') moveslurmright();
    }
    PS. What compiler do you use? And any other tools?

    Regards,
    Arek Bulski.

  6. #6
    Newbie fantanoice is an unknown quantity at this point fantanoice's Avatar
    Join Date
    Apr 2009
    Posts
    26

    Re: Need Help With Assignment (Using C)

    Quote Originally Posted by ArekBulski View Post
    PS. What compiler do you use? And any other tools?
    I use Quincy, which is the same one we're using at school.

    I've been able to make the snail move, but I think my if statements are written wrong somehow. I assigned variable ("i") to getch() but no matter what that value is, it always seems to use any character to make it move in one direction...

    I dunno, it's weird.

    EDIT: Assignment finished. You can close this now.
    Last edited by fantanoice; 04-05-2009 at 10:24 AM.

  7. #7
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,374

    Re: Need Help With Assignment (Using C)

    Wow, great. Pity you haven't posted your working code. I would like much to have a look at it. :001_unsure:

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Assignment Data Structures Need Help plzzz
    By passenger in forum C and C++
    Replies: 5
    Last Post: 04-01-2009, 06:54 AM
  2. Homework assignment
    By iceonfire in forum C and C++
    Replies: 2
    Last Post: 03-25-2009, 12:11 PM
  3. "while" loop assignment..help!
    By palomainak in forum C and C++
    Replies: 4
    Last Post: 02-11-2009, 07:22 AM
  4. Replies: 3
    Last Post: 08-26-2008, 12:20 AM
  5. NEED C++ Assignment HELP URGENT!
    By Sakinah in forum C and C++
    Replies: 7
    Last Post: 08-06-2007, 01:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts