8 Replies - 1615 Views - Last Post: 22 March 2013 - 02:21 AM Rate Topic: -----

#1 shuhei69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-March 13

project-guessing game

Posted 18 March 2013 - 05:02 PM

hi,im new in programming, and im having a problem.
Write a C program that simulates a guessing game. Each turn you choose among 9 possible guesses. As many as five guesses may be made a turn. For each turn the program will generate a random number between 1 and 36. Each correct guess will be rewarded with points based on how many of your current points risked. A game board divides the numbers into rows and columns. The board provides the basis for your guesses. You can guess whether the random is even or odd. In this case, you get 1 point for each point risked when you guess correctly. You can guess whether the number is low (1-12), medium (13-24), or high (25-36). In this case, you will get 2 points for each point risked. You can also guess left, center or right. In this case you get 2 points for each point risked when your guess is correct. Finally, you can guess a specific number between 0 adn 36. In this case you get 36 points for each point risked when your guess is correct. To make the game more interesting each round allows up to five guesses.
here is my example code.
#include<stdio.h>
#include<conio.h>
#include <time.h>

main() 
{
srand(time(NULL));
int gu,guess,wins,number = rand() % 36,r,p,t,b =1+(2*rand ()) %10;
char option, go;


do {



printf("=======================================================\n");
printf("Guesses Choice:\n");
printf("O-Odd E-Even F-Left C-Center R-Right N-Number\n");
printf("=======================================================\n");

printf("Enter your choice: ");
scanf("%c", &option);



switch (option) {

case 'O':
case 'o':
     
printf("Odd number from 1-35\n");

printf("\nenter number: ");
scanf("%d",&guess);
printf("Point at risk? ");
scanf("%d",&r);

if(guess<b || guess>B)/>{
                p=r;
printf("You lost %d points",p);
}

                
if(guess==B)/>
{
 t=r+r;                
printf("plus points:%d ",t);}

break;

case 'E':
case 'e':
printf("Even number\n");
break;
case 'L':
case 'l':
printf("Low\n");
break;
case 'M':
case 'm':
printf("Medium\n");
break;
case 'H':
case 'h':
printf("High\n");
break;
case 'F':
case 'f':
printf("Left\n");
break;
case 'C':
case 'c':
printf("Center\n");
break;
case 'R':
case 'r':
printf("Right\n");
break;

case 'N':
case 'n':
printf("\n\nNumber\n");

printf("\nenter number: ");
scanf("%d",&guess);
printf("Point at risk? ");
scanf("%d",&r);

if(guess<number || guess>number){
                p=r;
printf("You lost %d points",p);
}

                
if(guess==number)
{
 t=r+r;                
printf("plus points:%d ",t);}
break;



default:
printf("Try again!\n");
}

printf("\nContinue (Y/N)? ");
scanf("%c", &go);

} while (go == 'Y' || go == 'y');


getch();

}




Is This A Good Question/Topic? 0
  • +

Replies To: project-guessing game

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,631
  • Joined: 19-February 09

Re: project-guessing game

Posted 18 March 2013 - 06:43 PM

If the user chooses odd then you can check whether the number is odd, and if so give the player some points.

Do you know how to check whether a number is odd?

It might help to have a function that plays one game.
Was This Post Helpful? 0
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: project-guessing game

Posted 18 March 2013 - 09:19 PM

Also look up tolower() and toupper() or implement your own instead of checking each uppercase or lowercase character explicitly.
Was This Post Helpful? 0
  • +
  • -

#4 shuhei69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-March 13

Re: project-guessing game

Posted 19 March 2013 - 11:14 PM

im having problem with the loop i guess.. there should be an input for "enter how many guesses you want".. dont know what to do.
here's my code so far
#include<stdio.h>
#include<conio.h>
#include <time.h>
#include <stdlib.h>

main() 
{
srand(time(NULL));

int gu,guess,r;
char option, go;




do {

printf("=========================\n");
printf("Guesses Choice:\n");
printf("O-Odd \nE-Even \nL-Low \nM-Medium \nH-High \nF-Left \nC-Center \nR-Right \nN-Number\n");
printf("=========================\n");

printf("Enter your choice: ");
scanf("%c", &option);



switch (option) {

case 'O':
case 'o':    
     odd();break;

case 'E':
case 'e':
    even();break;

case 'L':
case 'l':
    low();break;

case 'M':
case 'm':
     medium();break;

case 'H':
case 'h':
     high();break;
     
case 'F':
case 'f':
     
printf("Left\n");
break;

case 'C':
case 'c':
printf("Center\n");
break;

case 'R':
case 'r':
printf("Right\n");
break;

case 'N':
case 'n':
   number();break;

default:
printf("Try again!\n");
}

printf("\nContinue (Y/N)? ");
scanf("%c", &go);

} while (go == 'Y' || go == 'y');
getch();
}


int odd(guess,r)
{
    int b =1+(2*rand ()) %36,an,b1,b2;
    
    printf("\nOdd number from 1-35\n");
     printf("\nenter number: ");
     scanf("%d",&guess);
     printf("Point at risk? ");
     scanf("%d",&r);

     if(guess<b || guess>B)/>{
                an=b;
     printf("Winning number is : %d",an);
                b1=r;
                printf("\nYou lost %d points",b1);
                }              
                if(guess==B)/>
                {
                b2=r+r;                
                printf("plus points:%d ",b2);}
    
}

int even(guess,r)
{
    int c =2+(2*rand ()) %36,an2,c1,c2;
    
     printf("\nEven number from 2-36\n");
     printf("\nenter number: ");
     scanf("%d",&guess);
     printf("Point at risk? ");
     scanf("%d",&r);
     if(guess<c || guess>c){
                an2=c;
     printf("Winning number is : %d",an2);
                c1=r;
                printf("\nYou lost %d points",c1);
                }              
                if(guess==c)
                {
                c2=r+r;                
                printf("plus points:%d ",c2);}
}

int low(guess,r)
{
    int l=rand() % 12+1,l2,l3,ans1;
    
     printf("\nLow [1-12]\n");
     printf("\nenter number: ");
     scanf("%d",&guess);
     printf("Point at risk? ");
     scanf("%d",&r);

     if(guess<l || guess>l){
                ans1=l;
     printf("Winning number is : %d",ans1);
                l2=r;
                printf("\nYou lost %d points",l2);
                }

                
                if(guess==l)
                {
                l3=r+r;                
                printf("plus points:%d ",l3);}
}

int medium(guess,r)
{
    int m=rand() % 12+13,m2,m3,ans2;
    
    printf("Medium\n");
 printf("\nMedium [16-24]\n");
     printf("\nenter number: ");
     scanf("%d",&guess);
     printf("Point at risk? ");
     scanf("%d",&r);


     if(guess<m || guess>m){
                ans2=m;
     printf("Winning number is : %d",ans2);
                m2=r;
                printf("\nYou lost %d points",m2);
                }

                
                if(guess==m)
                {
                m3=r+r;                
                printf("plus points:%d ",m3);}
}

int high(guess,r)
{
    int h=rand() % 12+25,h2,h3,ans3;
    
    printf("\nHigh [25-36]\n");
     printf("\nenter number: ");
     scanf("%d",&guess);
     
     printf("Point at risk? ");
     scanf("%d",&r);

     if(guess<h || guess>h){           
                 ans3=h;
     printf("Winning number is : %d",ans3);
                h2=r;
                printf("\nYou lost %d points",h2);
                }

                
                if(guess==h)
                {
                h3=r+r;                
                printf("plus points:%d ",h3);}
}

int number(guess,r)
{
   int number = rand() % 36,ans,p,t;
   
     printf("\n\nNumber\n");
     printf("\nenter number: ");
     scanf("%d",&guess);
     printf("Point at risk? ");
     scanf("%d",&r);

     if(guess<number || guess>number){
     ans=number;
     printf("Winning number is : %d",ans);
     
                p=r;
                printf("\nYou lost %d points",p);
                }               
                if(guess==number)
                {
                 t=r+r;                
                 printf("plus points:%d ",t);}
}





Was This Post Helpful? 0
  • +
  • -

#5 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: project-guessing game

Posted 20 March 2013 - 01:35 AM

Before any scanf() where you want a char, not an int, or a string, just chars - put a space before the %.

Like so:

scanf(" %c",&myChar); //note the space before the % !!


That will cause baggage in the keyboard buffer to be pulled out and your char to get the one seat in the game of musical chars here. ;)
Was This Post Helpful? 0
  • +
  • -

#6 shuhei69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-March 13

Re: project-guessing game

Posted 20 March 2013 - 07:25 PM

View PostAdak, on 20 March 2013 - 01:35 AM, said:

Before any scanf() where you want a char, not an int, or a string, just chars - put a space before the %.

Like so:

scanf(" %c",&myChar); //note the space before the % !!


That will cause baggage in the keyboard buffer to be pulled out and your char to get the one seat in the game of musical chars here. ;)/>


ah see, i just tried it and it works. thans a lot man, it helps alot. :D

View Postshuhei69, on 20 March 2013 - 07:25 PM, said:

View PostAdak, on 20 March 2013 - 01:35 AM, said:

Before any scanf() where you want a char, not an int, or a string, just chars - put a space before the %.

Like so:

scanf(" %c",&myChar); //note the space before the % !!


That will cause baggage in the keyboard buffer to be pulled out and your char to get the one seat in the game of musical chars here. ;)/>/>


ah see, i just tried and it works. thanks a lot man, it helps a lot. :D/>

Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,631
  • Joined: 19-February 09

Re: project-guessing game

Posted 20 March 2013 - 11:09 PM

Hi, is the random number between 1 and 36 or 0 and 36?

The random_number could be generated before or after the guesses.

If before, then the user makes a guess such as odd and x points are gambled. The random_number can then be checked to see if it is odd or not. There is no need to ask the user for a number in this case.
Was This Post Helpful? 0
  • +
  • -

#8 shuhei69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-March 13

Re: project-guessing game

Posted 22 March 2013 - 01:27 AM

the output should be like this..

How many points would you like? 2000
Guesses choices
o-odd
e-even
n-number
etc..

how many guess would you like? ex..5
guess 1
Enter your choice? o
Point at risk?10
winning number is:

guess 2
etc..

after that..
you won 100(example) points in this turn.
your new balance is:
Do you want to play again(Y/N)?

i also having a problem on looping:
int array[10],i,guesses;
printf("How many guesses?");
scanf("%d",&guesses);

for(i=0;i<guesses;i++)
{
printf("Guess %d",i);
scanf("%d",array[i])
}


..then the choices..
(still in learning process..urgg..)
Was This Post Helpful? 0
  • +
  • -

#9 shuhei69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-March 13

Re: project-guessing game

Posted 22 March 2013 - 02:21 AM

im done with the guess looping. i did it. YES! :)
the problem now is how to connect the points and point at risk, to add or subtract, and print. any idea guys? thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1