Dice Game Problem

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 2140 Views - Last Post: 15 February 2014 - 03:58 PM Rate Topic: -----

#16 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Dice Game Problem

Posted 14 February 2014 - 07:27 PM

Mostly because the Lovecraft amuses.

I'd use and enum. I'd also use a function to keep the names for you.

e.g.
#include <stdio.h> 
#include <stdlib.h> 

typedef enum {
	SY_YELLOWSIGN, SY_TENTACLE, SY_ELDERSIGN, SY_CTHULHU, SY_EYE, SY_COUNT
} Symbol;

const char *getSymbolName(Symbol n) {
	static const char *names[SY_COUNT] = {
		"YELLOW SIGN", "TENTACLE", "ELDER SIGN", "CTHULHU", "EYE"
	};
	return names[n];
}

Symbol getSymbolForRoll(int roll) {
	if (roll <= 3) { return SY_YELLOWSIGN; }
	if (roll <= 6) { return SY_TENTACLE; }
	if (roll <= 8) { return SY_ELDERSIGN; }
	if (roll <= 11) { return SY_CTHULHU; }
	return SY_EYE;
}

int getRandRoll() { return 1 + (rand() % 12); }

int main() {
	int turns, activePlayer;

	activePlayer = 1;
	for (turns = 1; turns <= 5; turns++) {
		int roll = getRandRoll();
		const char *symbol = getSymbolName(getSymbolForRoll(roll));

		for (activePlayer = 1; activePlayer <= 2; ++activePlayer) {
			printf("T# %d\t P# %d\t rolls a %d - %s\n", turns, activePlayer, roll, symbol);
		}
	}

	return 0;
}




Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#17 proprsk8   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 10-November 13

Re: Dice Game Problem

Posted 14 February 2014 - 07:55 PM

Since I haven't learned enum or arrays to the extent of the way you are using them, is it possible to have each symbol have it's on class?

for example,
#define SY_YELLOWSIGN 901
 void SY_YELLOWSIGN(int roll);
...

 void SY_YELLOWSIGN(int roll)
{
  if (roll <= 3){
   printf("Yellowsign");
  }
}



Actually, I don't think that'll work. Would you be able to call that in a printf statement like we did with symbol?
Was This Post Helpful? 0
  • +
  • -

#18 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1594
  • View blog
  • Posts: 5,010
  • Joined: 20-March 10

Re: Dice Game Problem

Posted 14 February 2014 - 09:05 PM

Bit long winded but

you could do this with baavgai's code


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

#define SY_YELLOWSIGN 0
#define SY_TENTACLE 1
#define SY_ELDERSIGN 2
#define SY_CTHULHU 3
#define SY_EYE 4




int getSymbolForRoll(int roll)
{
    if (roll <= 3)
    {
        return SY_YELLOWSIGN;
    }
    if (roll <= 6)
    {
        return SY_TENTACLE;
    }
    if (roll <= 8)
    {
        return SY_ELDERSIGN;
    }
    if (roll <= 11)
    {
        return SY_CTHULHU;
    }
    return SY_EYE;
}

int getRandRoll()
{
    return 1 + (rand() % 12);
}

int main()
{
    srand(time(NULL));
    int turns=0, activePlayer=0, sym=0;
    char* symbol;

    activePlayer = 1;
    for (turns = 1; turns <= 5; turns++)
    {

        for (activePlayer = 1; activePlayer <= 2; ++activePlayer)
        {

            int roll = getRandRoll();
            sym = getSymbolForRoll(roll);
            if(sym==0) symbol = "Yellow Sign";
            if(sym==1) symbol = "Tentacle";
            if(sym==2) symbol = "Eldersign";
            if(sym==3) symbol = "Cthulhu";
            if(sym==4) symbol = "Eye";
            printf("T# %d\t P# %d\t rolls a %d - %s\n", turns, activePlayer, roll, symbol);
        }
    }

    return 0;
}






Regards

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#19 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Dice Game Problem

Posted 14 February 2014 - 09:06 PM

View Postproprsk8, on 14 February 2014 - 05:02 PM, said:

Won't that just print the number of the symbols instead of printing the actual name of the symbol.
//I'm trying to make it look like this
/*
  T# 1   P# 1    rolls a 1 - Yellowsign
  T# 2   P# 2    rolls a 3 - Yellowsign
*/


In that case you want an array of C strings, not integers.
#define SY_COUNT 5

int main(int argc, const char * argv[])
{
	// Why [SY_COUNT][14]?
	char some_array[SY_COUNT][14] = {"SY_YELLOWSIGN","SY_TENTACLE","SY_ELDERSIGN","SY_CTHULHU","SY_EYE"};

	for (int i = 0; i < SY_COUNT; i++) {
		printf("%s \t",some_array[i]); // Why %s? Wasn't it %d before?
	}
	printf("\n");

	return 0;
}


This post has been edited by CTphpnwb: 14 February 2014 - 09:07 PM

Was This Post Helpful? 1
  • +
  • -

#20 proprsk8   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 10-November 13

Re: Dice Game Problem

Posted 15 February 2014 - 11:52 AM

I finally got it working, thank you guys soo much! However I still can't figure out why it won't increment turns for each player.
for example:
/*
T# 1   P# 1   
T# 2   P# 2
*/


What I'm getting:
/*
T# 1   P# 1
T# 1   P# 2
*/


I feel as if my for loops are wrong.
this is my skeleton of my for loops:
 for(turns =1; turns <= 5; turns++){
  //code
    for(activePlayer=1; activePlayer <=1; activePlayer++){ //activePlayer being the variable
                                                          //keeping track of each player.
      printf("#T %d\t P# %d\t ...",turns, activePlayer);
    }
 }


This post has been edited by proprsk8: 15 February 2014 - 11:55 AM

Was This Post Helpful? 0
  • +
  • -

#21 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1594
  • View blog
  • Posts: 5,010
  • Joined: 20-March 10

Re: Dice Game Problem

Posted 15 February 2014 - 11:58 AM

Shouldnt it be

for(activePlayer=1; activePlayer <=2; activePlayer++)


or

 for(activePlayer=1; activePlayer <3; activePlayer++)



Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#22 proprsk8   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 10-November 13

Re: Dice Game Problem

Posted 15 February 2014 - 01:43 PM

whoops! Yeah it is! I typed it from memory but in my actual code it's:
for (activePlayer = 1; activePlayer <= 2; ++activePlayer)
//I have pre-inc because post-inc was not alternating correctly


Was This Post Helpful? 0
  • +
  • -

#23 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Dice Game Problem

Posted 15 February 2014 - 02:08 PM

Quote

/*
T# 1 P# 1
T# 1 P# 2
*/

This makes sense because you're looping through players after turns. The next should look like this:

Quote

/*
T# 2 P# 1
T# 2 P# 2
*/

If you want turns to increment any time anyone takes a turn then turns shouldn't be in its own loop. Instead it should increment for each player.
Was This Post Helpful? 1
  • +
  • -

#24 proprsk8   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 10-November 13

Re: Dice Game Problem

Posted 15 February 2014 - 03:32 PM

I definitely understand what you're saying but how would I be able to get more turns if I'm only iterating the the player loop until it reaches 2. I'm sorry I should've clarified that. could I use a while loop?

while( turns <=30){

  for(activePlayer=1; activePlayer<=2; ++activePlayer){
     printf("");
     turns++;
  }
}



I'm pretty sure that does the same thing as before.

This post has been edited by proprsk8: 15 February 2014 - 03:37 PM

Was This Post Helpful? 0
  • +
  • -

#25 proprsk8   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 10-November 13

Re: Dice Game Problem

Posted 15 February 2014 - 03:39 PM

Awesome! I figured it out :D! The code above worked!
Was This Post Helpful? 1
  • +
  • -

#26 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Dice Game Problem

Posted 15 February 2014 - 03:46 PM

You could use a while loop. Fix the printf in the code you posted and see what you get.

Whenever possible you should test things out in a separate project you keep just for testing things. You'll learn much faster and deeper by doing this as opposed to getting the answers here or elsewhere on the web. Really, you should only come to the web when this method doesn't work for you.

Hmm, I guess I type slow. Still, I'm glad you got it, and on your own too!
Was This Post Helpful? 1
  • +
  • -

#27 proprsk8   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 10-November 13

Re: Dice Game Problem

Posted 15 February 2014 - 03:58 PM

Yeah, I understand I'm definitely not trying to find answers here. I just work better getting different ideas and thinking out loud. Thank you for mentioning that I should have a separate project just for testing that has helped a lot.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2