Game of Pig in C-Language

#include<stdio.h> #include<stdlib.h> #include<time.h> #include<stdbool.h> int sumPlayer1=0; int player2(); int sum1player2=0; int player1(); int main() { printf("*************************** Created By Hasan Dawood ****************************"); printf("\n\n ** Welcome To The Game of Pig ** \n\n"); srand(time(NULL)); player1(); printf("\n\n*************************** Created By Hasan Dawood ****************************"); } int roll() { int num = (rand()%6)+1; return num; } int player1() { _Bool k=true; int sum=0; int dice; char turn; while(k) { printf("\n Player 1 - (r)oll or (h)old?\n"); turn = getchar(); while(getchar()!='\n'); if(turn=='r') { dice = roll(); if(dice!=1) { sum+=dice; printf(" Player 1 Rolled = %i \n P1 score = %i\n",dice,sum); } else { printf(" Sorry, you rolled 1, P1 total score is 0 \n\n Next player turn."); k=false; } } else if(turn =='h') { k=false; sumPlayer1+=sum; printf(" P1 total score is = %i\n",sumPlayer1);; } } if(sumPlayer1 < 25) { player2(); } else { printf("\n\n\t ** PLAYER 2 WINS ** \n\n"); } return 0; } int player2() { _Bool j=true; int sum1=0; int dice; char turn; while(j) { printf("\n Player 2- (r)oll or (h)old?\n"); turn = getchar(); while(getchar()!='\n'); if(turn=='r') { dice = roll(); if(dice!=1) { sum1+=dice; printf(" Player 2 Rolled = %i \n P2 score = %i\n",dice,sum1); } else { printf(" Sorry, you rolled 1, P2 score is 0 \n\n Next player turn."); j=false; } } else if(turn =='h') { j=false; sum1player2+=sum1; printf("\n P2 total score is = %i\n",sum1player2); } } if(sum1player2 < 25) { player1(); } else { printf("\n\n\t ** PLAYER 1 WINS ** \n\n"); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.