0

I am in trouble with my dice game. I have a task:

The rules of the game are the following: 1. The player rolls the dice and adds up the face values. 2. If the first roll is a 7 or 11, the player wins. 3. If the first roll is a 2, 3 or 12, the player looses. 4. If the first roll is any other number, that sum becomes the player's point. 5. To win, the player must continue rolling the dice until he/she “makes point.” 6. The player loses by rolling a 7 before the point.

1) Define WON and LOST as macros in your program. Use the values of 0 for WON and 1 for LOSE 2) Implement a function, with function prototype int rollDice( void );

rollDice( ) should use rand( ) to randomly generate a number between 1 - 6

return the number generated by rand( )

3) Implement a function, with function prototype int playGame( void ); 

When the player is ready to play, (s)he would use the key ENTER to roll the dice

If the user wins in his/her first roll, congratulate the player and return with WON

If the user looses in his/her first roll, congratulate the player and return with LOSE

Let the user keep playing until (s)he wins / loses, give an appropriate massage and finish the game with the last roll value.

4) Your main( ) should Call your function playGame( )

Ask the user if (s)he wants to continue playing another game, keeping track of the numbers of losses and wins

When the user decides to finish playing, display the number of wins and losses (s)he had.

Give the user an appropriate message depending on the number of wins or losses (s)he had

Return with a value of EXIT_SUCCESS

Here is what I have now, but it tells me that there are mistakes. Can anyone please help me with this task?

#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>

#define WON 0
#define LOSE 1

int rollDice(void);
int playGame(void);

int rollDice(void) {
    return ((rand() % 6) + 1);
}

int playGame(void){
  int dice_1 = 0;
  int dice_2 = 0;
  int sum = 0;
  time_t t;
  srand(time(&t));
  printf("ROLL THE DICE WITH [ENTER]\n");
  dice_1 = rollDice();
  dice_2 = rollDice();
  sum = dice_1 + dice_2;
  if (sum == 7 || sum == 11){
   printf("Congratulations you roll %d and WON at your first try!", sum);
  }
  else {
    printf("Your roll was %d ,you lose try agian.\n", sum);
  }
  return 0;
}

int main (void){
  playGame();
}

The Error is (in gcc linux):

x.c:9:1: error: stray ‘\302’ in program

int rollDice(void);

^

x.c:9:1: error: stray ‘\240’ in program

x.c:10:1: error: stray ‘\302’ in program

int playGame(void);

^

x.c:10:1: error: stray ‘\240’ in program

x.c:12:1: error: stray ‘\302’ in program

int rollDice(void) {

^

x.c:12:1: error: stray ‘\240’ in program

x.c:16:1: error: stray ‘\302’ in program

int playGame(void){

^

x.c:16:1: error: stray ‘\240’ in program

AnaF
  • 63
  • 2
  • 2
  • 9
  • 1
    it appears you have nothing right now... – Marc B Sep 25 '15 at 19:44
  • I'm voting to close this question as off-topic because SO is no "do my homework" site. – too honest for this site Sep 25 '15 at 19:44
  • i pasted into ideone, and rolled an 11, your code works perfectly - well done – pm100 Sep 25 '15 at 19:49
  • 1
    *"it tells me that there are mistakes"*. What "mistakes?" I get one warning, about the argument type for `srand`, which won't stop it working. – Weather Vane Sep 25 '15 at 19:49
  • 1
    btw - i disagree wiht the close votes, he posted code (that works), he just didnt ask a clear question – pm100 Sep 25 '15 at 19:50
  • 1
    I assume the "mistake" is the fact that you don't exit with a value of `EXIT_SUCCESS` as you were asked to do... add in a `return 0;` at the end of of your `main`. – R_Kapp Sep 25 '15 at 19:50
  • I also disagree with closing this. Anyway, does it tell that there are mistakes or warnings? In the case of warnings it's not a problem as it works – Mr. E Sep 25 '15 at 19:51
  • Thank you! I'm trying to complile it in gcc in linux and it shows me an error.. – AnaF Sep 25 '15 at 19:56
  • *************** SHOW US THE ERROR ****************** - sorry to shout but u ignored the nice request – pm100 Sep 25 '15 at 19:59
  • I'm so sorry, the error is: x.c:9:1: error: stray ‘\302’ in program int rollDice(void); ^ x.c:9:1: error: stray ‘\240’ in program x.c:10:1: error: stray ‘\302’ in program int playGame(void); ^ x.c:10:1: error: stray ‘\240’ in program x.c:12:1: error: stray ‘\302’ in program int rollDice(void) { ^ x.c:12:1: error: stray ‘\240’ in program x.c:16:1: error: stray ‘\302’ in program int playGame(void){ ^ x.c:16:1: error: stray ‘\240’ in program – AnaF Sep 25 '15 at 20:01
  • @AnaF POST IT IN THE QUESTION. – Weather Vane Sep 25 '15 at 20:03
  • Since readers aren't having such trouble with your code, may I suggest it's a text editor problem? Start a new version, and copy/paste the code you posted here. – Weather Vane Sep 25 '15 at 20:07
  • Better to call `srand(time(&t));` once early in the program, rather than each time `playGame(void)` – chux - Reinstate Monica Sep 25 '15 at 20:31
  • 2
    the error message you are seeing is because somehow you messed up using the editor. There are some invisible (to you but not the compiler) characters in the text. You need to get rid of those characters by either starting again or using a different editor that might show the bad ones – pm100 Sep 25 '15 at 22:34
  • @pm100 Thank you for your help! – AnaF Sep 25 '15 at 22:54
  • Re: Stray errors: The sequence \302 \240 is well known. 302 240 (octal) → 0xC2 0xA0 (hexadecimal) → UTF-8 sequence for Unicode code point U+00A0 ([NO-BREAK SPACE](https://www.charset.org/utf-8)). It can be searched for (and replaced with regular space) by the regular expression `\x{00A0}` in any modern text editor or IDE (note: The notation is different in Visual Studio Code (and probably others): `\u00A0` (instead of `\x{00A0}`)) – Peter Mortensen May 01 '23 at 17:08
  • cont' - *NO-BREAK SPACE* is positively known to be introduced by using ***Skype Chat*** (and probably by other chat applications) to copy / exchange code. Ask me how I know... – Peter Mortensen May 01 '23 at 17:09
  • This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen May 01 '23 at 17:11
  • There are really two questions here: 1) The stray errors 2) The logical errors (hinted at in the existing answers—answers by inspection). The stray errors are the ones reported a symptom for. This ought to have been two questions, one for the stray errors and one for the logical errors. They are two completely different problems. – Peter Mortensen May 01 '23 at 17:23
  • Does this answer your question? [Compilation error: stray ‘\302’ in program, etc](https://stackoverflow.com/questions/19198332/compilation-error-stray-302-in-program-etc) – Peter Mortensen May 23 '23 at 20:17

2 Answers2

1

You haven't yet satisfied the rules of the game. Your code takes 7 and 11 as winners and any other number as a loser.

After the 7/11 check, you need to check for 2, 3, or 12 and print a "lose" message if true. If not, you have the point number, and you need to prompt the user to keep rolling until he either gets the point number (win) or a 7 (lose).

You also need to keep track of wins/losses in main. You'll need to call playGame in a loop, prompting the user to continue on each iteration.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Thank you! I don't really understand how to call playGame() in main – AnaF Sep 25 '15 at 19:55
  • @AnaF You're already doing it. What you need to do however is return `WIN` or `LOSE` as appropriate from `playGame` and capture that in `main`, updating the appropriate counter. – dbush Sep 25 '15 at 19:58
1

There are a few things wrong here.

  1. You're not reading/using the return value from playGame(). You should be storing the result and acting on it.
  2. Your logic isn't complete, as the criteria for "playing for point" and a loss both are the same.
  3. You don't have anything in place that forces the program to wait for the user to press ENTER.

I have included a completed code listing for you below.

Code Listing


/*******************************************************************************
 * Preprocessor directives
 ******************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>

#define WON 0
#define LOSE 1


/*******************************************************************************
 * Function prototypes
 ******************************************************************************/
int rollDice(void);
int playGame(void);


/*******************************************************************************
 * Function definitions
 ******************************************************************************/
/*----------------------------------------------------------------------------*/
int rollDice(void) {
    return ((rand() % 6) + 1);
}

/*----------------------------------------------------------------------------*/
int playGame(void){
    int dice_1 = 0;
    int dice_2 = 0;
    int sum = 0;
    int result;
    int point = 0;
    time_t t;
    bool playForPoint = false;

    srand(time(&t)); // Initialize random seed
    printf("ROLL THE DICE WITH [ENTER]\n");
    fgetc(stdin);
    dice_1 = rollDice();
    dice_2 = rollDice();
    sum = dice_1 + dice_2;
    printf("D1:%2d - D2:%2d - Sum:%2d\n", dice_1, dice_2, sum);

    switch ( sum )
    {
        case 7:
        case 11:
            result = WON;
            break;
        case 2:
        case 3:
        case 12:
            result = LOSE;
            break;
        default:
            playForPoint = true;
            point = sum;
            printf("Playing for point:%d. Please hit enter.\n", point);
            fgetc(stdin);
            break;
    }

    while ( playForPoint )
    {
        dice_1 = rollDice();
        dice_2 = rollDice();
        sum = dice_1 + dice_2;
        printf("D1:%2d - D2:%2d - Sum:%2d\n", dice_1, dice_2, sum);
        if ( sum == 7 ) {
            playForPoint = false;
            result = LOSE;
        } else if ( sum == point ) {
            playForPoint = false;
            result = WON;
        } else {
            printf("Please roll the dice again with [ENTER].\n");
            fgetc(stdin);
        }
    }

    return result;
}

/*----------------------------------------------------------------------------*/
int main (void){
    int result = playGame();
    switch ( result )
    {
        case WON:
            printf("You won the game.\n");
            break;
        case LOSE:
            printf("You lost the game.\n");
            break;
        default:
            printf("Something went wrong in the program.\n");
            break;
    }

    return 0;
}

Sample Output


ROLL THE DICE WITH [ENTER]

D1: 3 - D2: 5 - Sum: 8
Playing for point:8. Please hit enter.

D1: 3 - D2: 1 - Sum: 4
Please roll the dice again with [ENTER].

D1: 3 - D2: 2 - Sum: 5
Please roll the dice again with [ENTER].

D1: 1 - D2: 5 - Sum: 6
Please roll the dice again with [ENTER].

D1: 3 - D2: 2 - Sum: 5
Please roll the dice again with [ENTER].

D1: 2 - D2: 6 - Sum: 8
You won the game.
Cloud
  • 18,753
  • 15
  • 79
  • 153
  • 1
    @AnaF No problem. If this adequately answered your question, please check on the checkmark near the top of the question, and consider upvoting it. – Cloud Sep 25 '15 at 20:45
  • 1
    @PhillyNJ I used to have a strong objection to this, but since the `homework` tag is no longer allowed, I don't make any effort to discriminate against homework vs self-driven learning vs general questions. I posted general points at the the top of the code, and then provided the updated code listing. If there is a strong objection against the approach I took, I advise you to open a new debate on the homework tag/topic. http://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions – Cloud Sep 25 '15 at 21:02
  • 2
    @Dogbert - no objections at all. But his comment in the answer by @dbush `don't really understand how to call playGame() in main` struck me as odd, as if he copied and pasted the code from somewhere else. Its a moot point now. He'll get an A on your code :) - No worries – PhillyNJ Sep 25 '15 at 21:10
  • 1
    @PhillyNJ My rationalization is that if the code is copied verbatim, it'll turn some heads, especially if switch-case statements haven't been taught yet. I'm hoping this helps TC along, rather than just postponing an inevitable catastrophe. – Cloud Sep 25 '15 at 21:13
  • 1
    I will not fully copy this code, it gave me a chance to understand structure of the program. Anyway, I will read about some points that I haven't used before to UNDERSTAND why are they used. Thank you very much, Dogbert :) – AnaF Sep 25 '15 at 21:25
  • 2
    and his actual problem was garbage characters in the program text. He actually did a fair job of writing the code (compared to a lot of stuff seen here) - it actually ran and did roughly the right thing – pm100 Sep 25 '15 at 22:32