I need some quick help with code.

This is a discussion on I need some quick help with code. within the C Programming forums, part of the General Programming Boards category; Hey I really need help figuring out this problem for my program study group. I need to show this code ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    I need some quick help with code.

    Hey I really need help figuring out this problem for my program study group. I need to show this code in my presentation. I was just wondering if anyone could help me figue it out. Heres the problem.

    The Problem

    Input Specification
    Assume in all situations, the user enters valid input.

    Many games store a player's score and allow the player to have multiple lives. The final score of playing a game is the score the player attains at the point in time that they have no lives left. Furthermore, many of these games have several rounds or boards. If a player completes a round without losing a life, rather than the opponent getting a chance to play, that player gets to continue playing. Using these general guidelines, you will design the framework for a game program without knowing any detailed information of the game. You will only be given the function prototype for a function that executes a single round of a game as well as some other specifications for how you should run the game.

    the goal is to show how a good functional design can be used to split up work amongst mostly independently working programmers, an actual function will not be provided until after the due date to the assignment.

    Here is the prototype of the function you will have to call that executes a single round of a game:

    // Pre-condition: name is the name of the current player. numlives is a reference to
    // the variable storing the number of lives left of the current player.
    // score is a reference to the variable storing the score of the current
    // player, and round is the round the player is currently on.
    // Post-conditions: A single round of the game will be executed. The player's score
    // and number of lives will be adjusted accordingly. The round
    // will be completed unless the player has no more lives left.
    void playround(char name[], int* numlives, int* score, int round);

    You will execute a competition between two players in this mystery game. Before the game starts, ask each player their name and set their scores to 0 and their total number of lives to 5.

    After this, the game begins. Allow player 1 to go first. On a particular round, if a player does NOT lose any lives, then do NOT change the turn to the next player. Instead, allow the same player to continue their turn playing the next round.

    In the situation that the player does lose at least one life, the subsequent turn is taken by the other player. The only exception to either of these rules is if one player has 0 lives. If one player has 0 lives, then all turns will be given to his/her competitor till they lose all their lives.

    Your main goal will be to call the playround function when appropriate, control who's turn it is and quit the game and print out the final results of both players. You should print out a status report for each player after they finish a round. (Or if they do not succeed in finishing a round and end up with 0 lives while playing a round.)

    Note: This setup is DIFFERENT than standard video games where your opponent always gets to go after you lose a life. Here the change in turn is indicated by finishing a round in which you could lose more than one life. Furthermore, if you finish a round without losing a life, the turn doesn't change, but you DO have to make a new call to the playround function. It is also possible for you to finish a round, lose a life or more in the process, but not lose one at the end of the round. In this case, play advances to the other player if they have lives left.

    Thanks so much guys!

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    There is no such thing as a humble opinion

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    Unhappy No but really.

    I am just looking for the general idea on how to complete this problem. I have no idea where to begin. Im just learning all of this stuff. Sorry.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    This won't be the last time you'll run into this problem - you need a strategy! Start by 'stubbing-out' the program (getting it up and running with 'dummy' variables). Once you have the stub running correctly, slowly add code to it till it performs to spec. Finally, using the first attempt as a reference, rewrite the program from scratch. (That phase will solidify everything you learned coding the first draft.)

    Good luck!
    Code:
    #include <ip.hpp>

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: No but really.

    Originally posted by stehigs321
    I am just looking for the general idea on how to complete this problem. I have no idea where to begin. Im just learning all of this stuff. Sorry.
    Start by defining this stuff:
    // Pre-condition: name is the name of the current player. numlives is a reference to
    // the variable storing the number of lives left of the current player.
    // score is a reference to the variable storing the score of the current
    // player, and round is the round the player is currently on.
    Then program this part:
    ... Before the game starts, ask each player their name and set their scores to 0 and their total number of lives to 5.
    Then if this is a board game, decide what would you and your opponent do next?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    What do you mean defining?

    What do you mean define??

    like this:
    Code:
     
    #include <stdio.h> 
    
    #DEFINE numlives = 5 
    #DEFINE score = 0 
    #DEFINE round = 1 
    
    int main(void) { 
    
    char name; 
    
    printf("Player one, what is your name?\n"); 
    scanf(
    now what do i do? how do i store a name??
    how do i define name?

    Start by defining this stuff:
    quote:
    --------------------------------------------------------------------------------

    // Pre-condition: name is the name of the current player. numlives is a reference to
    // the variable storing the number of lives left of the current player.
    // score is a reference to the variable storing the score of the current
    // player, and round is the round the player is currently on.
    --------------------------------------------------------------------------------
    Last edited by stehigs321; 10-26-2003 at 03:15 PM.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    What next??

    should numlives be a variable too since it isntr a constant??

    i have this now. this look corect so far. What should i do now?
    Code:
    #include <stdio.h>
    
    int numlives = 5
    int score = 0
    int round = 1
    
    int main(void) {
    
      char name[100] = {0};
    printf("Player one, what is your name?\n");
    scanf(  HOW DO I SCAN A NAME?  i know it has to go in the name[100])

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Stick to one thread.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by XSquared
    Stick to one thread.
    Sorry, I'll try harder next time.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Wasn't talking to you, Hammer. I was talking to the OP.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  12. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    so far.. what next??

    Code:
    #include <stdio.h>
    
    int numlives = 5
    int score = 0
    int round = 1
    
    int main(void) {
    
      char name[100] = {0};
    
       printf("Player one, what is your name?\n")
       scanf("%s", name);
       printf("Player two, what is your name?\n")
       scanf("%s", name);
    hows this look so far can i put the two peoples names in the same array??? like so?? what should i do next??? please help guys. I dont think this is that tough. Im just a newbie. Im getting better. Thanks so much for everything already.

  13. #13
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: so far.. what next??

    Originally posted by stehigs321
    Code:
    #include <stdio.h>
    
    int numlives = 5
    int score = 0
    int round = 1
    
    int main(void) {
    
      char name[100] = {0};
    
       printf("Player one, what is your name?\n")
       scanf("%s", name);
       printf("Player two, what is your name?\n")
       scanf("%s", name);
    hows this look so far can i put the two peoples names in the same array??? like so?? what should i do next??? please help guys. I dont think this is that tough. Im just a newbie. Im getting better. Thanks so much for everything already.
    Nope. The second scanf() overwrites the first name entered. Also, scanf() if a flakey input mechanism. Try this instead:

    Code:
    #include <stdio.h>
    #define MAXBUF 100
    int numlives = 5
    int score = 0
    int round = 1
    
    int main(void) {
    
      char name1[MAXBUF] = {0};
      char name2[MAXBUF] = {0};
    
       printf("Player one, what is your name?\n")
       fgets(name1, MAXBUF, stdin);
    
       printf("Player two, what is your name?\n")
       fgets(name2, MAXBUF, stdin);
    
      printf("[%s] [%s] \n", name1, name2);
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  14. #14
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    I cant use that

    I can only use basic c programming. I need to use functions in this excercise and probably along with a loop. we havent gotten to fgets yet in my studies. I hope you cna still help me.
    thanks so much everybody

  15. #15
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: I cant use that

    Originally posted by stehigs321
    I can only use basic c programming. I need to use functions in this excercise and probably along with a loop. we havent gotten to fgets yet in my studies. I hope you cna still help me.
    thanks so much everybody
    Use scanf() if you absolutely have to, just be aware that it's prone to doing things its way, not your way. Try it, if it works, so much the better.

    By the way, I forgot one thing. If you do use the fgets, you'll want to strip off the trailing return:
    name1[strlen(name1)-1] = '\0';
    and include string.h
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 06:52 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21