0% found this document useful (0 votes)
1 views

Code c language

This document contains a C program for a simple two-player dice game called Mini Ludo. Players take turns rolling a die to move towards a winning position of 30, and the game announces the winner when a player reaches that position. The program uses random number generation for dice rolls and includes basic input/output functions to interact with the players.

Uploaded by

zcdn68pwtt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Code c language

This document contains a C program for a simple two-player dice game called Mini Ludo. Players take turns rolling a die to move towards a winning position of 30, and the game announces the winner when a player reaches that position. The program uses random number generation for dice rolls and includes basic input/output functions to interact with the players.

Uploaded by

zcdn68pwtt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

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;

srand(time(NULL)); // Seed random number generator

printf("=== Welcome to Mini Ludo ===\n");


printf("First to reach position %d wins!\n", WINNING_POSITION);

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;
}

You might also like