Code c language
Code c language
h>
#include <stdlib.h>
#include <time.h>
#define WINNING_POSITION 30
typedef struct {
char name[20];
int position;
} Player;
int rollDice() {
return (rand() % 6) + 1;
}
void printPosition(Player p) {
printf("%s is at position %d\n", p.name, p.position);
}
int main() {
Player p1 = {"Player 1", 0};
Player p2 = {"Player 2", 0};
int dice;
int turn = 1;
while (1) {
if (turn == 1) {
printf("\n%s's turn. Press Enter to roll the dice...", p1.name);
getchar();
dice = rollDice();
printf("%s rolled a %d\n", p1.name, dice);
p1.position += dice;
if (p1.position > WINNING_POSITION) {
p1.position = WINNING_POSITION;
}
printPosition(p1);
if (p1.position == WINNING_POSITION) {
printf("🎉 %s wins! 🎉\n", p1.name);
break;
}
turn = 2;
} else {
printf("\n%s's turn. Press Enter to roll the dice...", p2.name);
getchar();
dice = rollDice();
printf("%s rolled a %d\n", p2.name, dice);
p2.position += dice;
if (p2.position > WINNING_POSITION) {
p2.position = WINNING_POSITION;
}
printPosition(p2);
if (p2.position == WINNING_POSITION) {
printf("🎉 %s wins! 🎉\n", p2.name);
break;
}
turn = 1;
}
}
printf("Game Over!\n");
return 0;
}