I went to read the statement and started to do the implementation only I ended up traveling too much in the codes and I made an interactive game between the player and the computer kkkk, if anyone is interested here is the implementation:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
// Obtem um número.
int getNumber() {
int number = 0;
scanf("%d*c", &number);
return number;
}
// Escreve a escolha do jogador.
void printChoice(const char* player, int choice, const char* adversary) {
const char *playerChoice = (choice) ? "impar" : "par";
const char *adversaryChoice = (!choice) ? "impar" : "par";
printf("O jogador %s escolheu %s então o adversário %s ficará com %s.\n", player, playerChoice, adversary, adversaryChoice);
}
// Obtem um número aleatorio entre 0 e max.
int getRandomNumber(int max) {
srand(time(NULL));
int number = rand() % max;
return number;
}
// Escreve os números escolhidos pelo jogador e o adversario.
void printNumbers(const char* player, int playerNumber, const char* adversary, int adversaryNumber) {
printf("O jogador %s escolheu %d.\n", player, playerNumber);
printf("O adversário %s escolheu %d.\n", adversary, adversaryNumber);
}
// Escreve o resultado da soma entre o número do jogador e do adversario e se é par ou impar.
void printResult(int playerNumber, int adversaryNumber) {
int result = playerNumber + adversaryNumber;
printf("Resultado: %d é %s.\n", result, (result) % 2 == 0 ? "par" : "impar");
}
// Obtem o nome do vencedor.
const char* getWinner(const char* player, int playerNumber, int choice, const char* adversary, int adversaryNumber) {
int result = playerNumber + adversaryNumber;
// Se o resultado for impar e o player escolheu impar retorne o player se não retorne o adversary.
// Se o resultado for par e o player escolheu par retorne o player se não retorne o adversary.
return (result % 2) ? ((choice) ? player : adversary) : ((!choice) ? player : adversary);
}
// Escreve o quem está competindo nesse round e o nome do vendedor.
void printWinner(const char* player, const char* adversary, const char* winner) {
printf("Round(%s x %s): Vencedor %s.\n", player, adversary, winner);
}
int main() {
printf("[TORNEIO DE PAR OU IMPAR]\n");
char player[80];
printf("Insira seu nome: ");
scanf("%s*c", player);
// Primeiro round: A x B
// --------------------------------------------------
printf("[ROUND %s x B]\n", player);
printf("Jogador %s escolha entre par(0) ou impar(1): ", player);
int aChoice = getNumber();
printChoice(player, aChoice, "B");
printf("Jogador %s escolha um número entre 0 e 10: ", player);
int aNumber = getNumber();
int bNumber = getRandomNumber(10);
printNumbers(player, aNumber, "B", bNumber);
printResult(aNumber, bNumber);
const char* abWinner = getWinner(player, aNumber, aChoice, "B", bNumber);
printWinner(player, "B", abWinner);
int roundOne = strcmp(player, abWinner);
// Segundo round: C x D
// --------------------------------------------------
printf("[ROUND C x D]\n");
printf("Jogador C escolha entre par(0) ou impar(1): ");
int cChoice = getRandomNumber(1);
printf("%d\n", cChoice);
printChoice("C", cChoice, "D");
printf("Jogador C escolha um número entre 0 e 10: ");
int cNumber = getRandomNumber(10);
printf("%d\n", cNumber);
int dNumber = getRandomNumber(10);
printNumbers("C", cNumber, "D", dNumber);
printResult(cNumber, dNumber);
const char* cdWinner = getWinner("C", cNumber, cChoice, "D", dNumber);
printWinner("C", "D", cdWinner);
// Round final
// --------------------------------------------------
printf("[ROUND %s x %s]\n", abWinner, cdWinner);
printf("Jogador %s escolha entre par(0) ou impar(1): ", abWinner);
int fChoice = (!roundOne) ? getNumber() : getRandomNumber(1);
if(roundOne)
printf("%d\n", fChoice);
printChoice(abWinner, fChoice, cdWinner);
printf("Jogador %s escolha um número entre 0 e 10: ", abWinner);
int abNumber = (!roundOne) ? getNumber() : getRandomNumber(10);
if(roundOne)
printf("%d\n", abNumber);
int cdNumber = getRandomNumber(10);
printNumbers(abWinner, abNumber, cdWinner, cdNumber);
printResult(abNumber, cdNumber);
const char* winner = getWinner(abWinner, abNumber, fChoice, cdWinner, cdNumber);
printWinner(abWinner, cdWinner, winner);
if(strcmp(player, winner) == 0) {
printf("Você ganhou!\n");
}else {
printf("Você perdeu!\n");
}
return 0;
}
Thank you very much, really this went unnoticed.
– Matheus Henrique