Welcome to Dream.In.Code
Click Here
Getting C++ Help is Easy!

Join 117,543 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,749 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Help Dice Game in C

 
Reply to this topicStart new topic

Help Dice Game in C

Needhelpnow134567890
post 19 Jul, 2008 - 08:31 PM
Post #1


New D.I.C Head

*
Joined: 19 Jul, 2008
Posts: 1

CODE
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
void main()
{
     {
       turn = srand()%2 + 1;
     }
  int Player1Total;
  int Player2Total;
  int Player1;
  int Player2;
  int Dice1()
  {
    int roll;
    srand (time(NULL));
    roll = rand() %6 + 1;
    return Roll;
  }
  int Dice2 ()
  {
   int roll;
   srand (time(NULL));
   roll = rand() %6 + 1;
   return Roll;
  }
  while (player1total<100 and playertotal<100)
        {
        do
          {
            printf("Roll again press 1? ")
            scanf("%d", &Player1)
          }
          do
          {
           printf("Roll again press 1? ")
           scanf("%d", &Player2)
          }
        }
}
void Player1 ()
{
int roll1;
int roll2;

roll1 = Dice1
roll1 = Dice2
if(roll1 = 1 OR roll2 = 1)
{
    Points for turn only are lost
    End turn
}
else if(roll1 = 1 and roll2 = 1)
{
      all points are lost
      End turn
}
else
{
     Player1Total = roll1 + roll2
     if(Player1Total >=100)
     {
       Declare winner
     }
}
}

int Player2 ()
{
    int roll1;
    int roll2;
    roll1 = Dice1
    roll2 = Dice2
    if(roll1 = 1 OR roll2 = 1)
    {
       Points for turn are lost
       End Turn
    }
    else if(roll1 = 1 and roll2 = 1)
    {
         all points are lost
         end turn
    }
    else
    {
        Player1Total = roll1 + roll2
        if(playerTotal >= 100)
        {
          Declare Winner
        }
    }
}
            
                    
  system("PAUSE");    
  return 0;
}

I'm trying to do this in Dev C ++ its going in a massive loop I have been on this for two days now and its due tomorrow. Our teacher gave three days to do this if anyone can help please do. The assignment is to play a dice game where you and a computer roll two 6 sided dice and if you or the computer roll two 1's on their turn their total score drops to 0 and it ends your turn. Also if you roll a 1 and another number you just lose your score for that turn and it ends your turn. Lastly I have to put AI into the computer player so that at the beginning of the turn, if the computer’s total score is less than the human’s total score, divide the difference by 7 and roll a maximum of that many times. Also, if the computer’s total score is more than the human’s score, roll a random maximum number of times between 1 and 4, assuming no ones are rolled.
User is offlineProfile CardPM

Go to the top of the page


no2pencil
post 19 Jul, 2008 - 08:47 PM
Post #2


Wet D.I.C.

Group Icon
Joined: 10 May, 2007
Posts: 5,421



Thanked 35 times

Dream Kudos: 2350

Expert In: Goofing Off

My Contributions


CODE

int main(int argc, char *argv[])
void main()
{
     {
       turn = srand()%2 + 1;
     }


Just in the beggining of this code, you have 2 major errors.

1stly, you have declared main twice. There can only be one main function.
2ndly, you are setting a value to the variable turn, however you have not declared the variable yet, so it has no type.
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 20 Jul, 2008 - 11:11 AM
Post #3


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,763



Thanked 36 times

Dream Kudos: 525
My Contributions


You code was so bad that it is not even C. It sorta kinds (if your really not paying attention) looks like C code, but it really REALLY isn't.

What I have done is fixed it so that what is there will compile. That does not mean it is done...

DO NOT RUN THE PROGRAM AS IS: it is an infinite loop -- well providing it starts at all it is an infinite loop, I suppose you have a chance that it will not start at all. (if you do run it, press ctrl+C to exit).


The code currently does nothing but infinitely bug people to press 1.

Some notes:
#1 you must declare a variable before you use it.
#2 Functions are not defined inside of other functions. Generally they are declared at the top of the program, and then defined after main(). (Though this is just convention -- as long as they are declared and/or defined before they are used you are good).
#3 assignment is not the same as comparing for equality ("=" != "=="). To compare if two values are equal use "A == B" not "A = B", the second is an assignment and copies the value of B into A (so then A DOES == B because you just made it so).
#4 Logic statments in C use || for OR and && for AND. So conditions should look like:
if(roll1 == 1 || roll2 == 1)
rather than:
if(roll1 = 1 OR roll2 = 1)

I am sure that I fixed a great many other things (I commented out any code that didn't really have anything to do with C), but you job is to use this as a jumping off point. Good luck:
CODE
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int Dice1();
int Dice2();
void Player1();
int Player2();

int main(int argc, char *argv[])
{
    int Player1Total;
    int Player2Total;
    int Player1;
    int Player2;
    while (Player1Total<100 && Player2Total<100)
    {
        do
        {
            printf("Roll again press 1? ");
            scanf("%d", &Player1);
        } while (Player1 == 1);
        do
        {
            printf("Roll again press 1? ");
            scanf("%d", &Player2);
        } while (Player2 == 1);
    }
    system("pause");
    return 0;
}

void Player1()
{
    int roll1;
    int roll2;
    int Player1Total;
    
    roll1 = Dice1();
    roll1 = Dice2();
    if(roll1 == 1 || roll2 == 1)
    {
//        Points for turn only are lost
//        End turn
    }
    else if(roll1 == 1 && roll2 == 1)
    {
//        all points are lost
//        End turn
    }
    else
    {
        Player1Total = roll1 + roll2;
        if(Player1Total >=100)
        {
//            Declare winner
        }
    }
}

int Player2()
{
    int roll1;
    int roll2;
    int Player2Total;
    
    roll1 = Dice1();
    roll2 = Dice2();
    if(roll1 == 1 || roll2 == 1)
    {
//       Points for turn are lost
//       End Turn
    }
    else if(roll1 == 1 && roll2 == 1)
    {
//         all points are lost
//         end turn
    }
    else
    {
        Player2Total = roll1 + roll2;
        if(Player2Total >= 100)
        {
//            Declare Winner
        }
    }
}

int Dice1()
{
    int roll;
    srand (time(NULL));
    roll = rand() %6 + 1;
    return roll;
}

int Dice2 ()
{
    int roll;
    srand (time(NULL));
    roll = rand() %6 + 1;
    return roll;
}


User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 10/7/08 05:53PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month