C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I don't know exactly what you the rules for the game is. But if we assume that the rules are "if the sum gets above 50, no score", then something like this:
You may need to adjust this to match your actual scoring rules, but something like that.Code:int throwscore = 0; ... if (dice1 == dice2) { ... if (dice1 == 6) throw_score = 25; else throw_score = 5; if (score1 + throw_score <= 50) score1 += throw_score; }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Final run:
It works! Thanks matsp, you cleared my ideas! =)Code:#include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { int die1, die2, score1, score2, throwscore; srand((unsigned)time(NULL)); die1 = die2 = score1 = score2 = throwscore = 0; while(score1 < 50 && score2 < 50) { //player 1 die1 = (rand()%6)+1; die2 = (rand()%6)+1; if(die1 == die2) { printf("Player 1 scored double %d\n", die1); if(die1 == 3) throwscore = score1 = 0; else if(die1 == 6) throwscore = 25; else throwscore = 5; if(score1 + throwscore <= 50) score1 += throwscore; printf("Player 1 = %d\n", score1); } //player 2 die1 = (rand()%6)+1; die2 = (rand()%6)+1; if(die1 == die2) { printf("Player 2 scored double %d\n", die1); if(die1 == 3) throwscore = score2 = 0; else if(die2 == 6) throwscore = 25; else throwscore = 5; if(score2 + throwscore <= 50) score2 += throwscore; printf("Player 2 = %d\n", score2); } } if(score1 == score2) printf("Game is a tie\n"); else if(score1 > score2) printf("Player 1 Wins\n"); else printf("Player 2 Wins\n"); return 0; }
Last edited by fsx; 05-13-2009 at 07:29 AM.