Skip to content

Instantly share code, notes, and snippets.

@Plankiton
Created June 30, 2022 01:55
Show Gist options
  • Select an option

  • Save Plankiton/f4f805e6e60c8bb183c258842651cf64 to your computer and use it in GitHub Desktop.

Select an option

Save Plankiton/f4f805e6e60c8bb183c258842651cf64 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Tabuleiro
int board[50], down[4], up[4];
// Jogadores
int play[2];
int play_dice() {
int dice = rand() % 6;
while (dice == 0) {
dice = rand() % 6;
}
return dice;
}
int main() {
srand(time(NULL));
for (int i = 0; i < 4; i++) {
down[i] = 0;
up[i] = 0;
}
for (int i = 0; i < 4; i++) {
int random = rand() % 49;
down[i] = random;
int count = 0;
do {
random = rand() % 49;
if (down[i] == random)
up[i] = random;
if (count++ == 9999) {
up[i] = random;
break;
}
} while (up[i] == 0);
}
puts("");
int winner = 0;
while (winner == 0) {
for (int i = 0; i < 2; i++) {
printf("Pressione enter para jogar o dado (jogador %i)", i + 1);
getchar();
int dice = play_dice();
if (play[i] + dice >= 50) {
printf("O valor do dado foi %i, mas o jogador %i deve tirar %i para "
"ganhar\n",
dice, i + 1, 50 - play[i]);
continue;
}
play[i] += dice;
printf("O valor do dado foi %i e o jogador %i deverá avançar para a casa "
"%i\n",
dice, i + 1, play[i]);
for (int j = 0; j < 4; j++) {
int dice = play_dice();
if (down[j] == play[i]) {
printf("O jogador %i caiu em uma casa de penalidade, voltará %i "
"casas (para a casa %i)\n",
i + 1, dice, play[i] - dice);
if (play[i] + dice < 0) {
play[i] = 0;
continue;
}
play[i] -= dice;
} else if (up[j] == play[i]) {
printf("O jogador %i caiu em uma casa de penalidade, avançará %i "
"casas (para a casa %i)\n",
i + 1, dice, play[i] + dice);
if (play[i] + dice >= 50) {
printf("O valor do dado foi %i, mas o jogador %i (casa %i) deve "
"tirar %i para "
"ganhar\n",
dice, i + 1, play[i], 50 - play[i]);
continue;
}
play[i] += dice;
}
}
if (play[i] == 49) {
printf("O jogador %i é o campeão!\n", i + 1);
winner = i + 1;
break;
}
getchar();
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment