Project's files:
game.h main.c game.c
1. game.h
Code:
/********** FUNCTION ARGUMENTS: **********************************************
*
* int SIZE: how many players
* int playerlist[][2]: 2D list of players, each player with ID and score
* char pnames[][100]: 2D list of names, each name can be stored in 100 chars
*
**********/
/**
* to read an int in its range
* retry if no real number is given
* !! function will return max of int if overflow !! program exit.
*/
int getnumber(void);
/**
* auto ID generator
* username register
*/
void setplayers(int playerlist[][2], char pnames[][100], int SIZE);
/*
* to print ranking list of registered players
*/
void getranking(int playerlist[][2],char pnames[][100], int SIZE);
/*
* to guess a number between x and 1
* print average attempts
* call function printavrgscore()
* call function printstats()
*/
void play(int playerlist[][2], char pnames[][100], int SIZE);
/**
* to print average attempts
*/
float printavrgscore(int playerlist[][2], int SIZE);
/*
* to sort player list by attempts
*
*/
void sortplayers(int players[][2], char pnames[][100], int nplayers);
/**
* to process the game
* check if one winner or more players
* with equal minimum of attempts
*/
void process_game(int playerlist[][2], char pnames[][100], int SIZE);
2. main.c
Code:
#include <stdio.h>
#include <string.h>
#include "game.h"
int WINNER;
int main (void) {
srand(time(NULL));
// get (nplayers) number of players
printf(" How many players? ");
int nplayers = getnumber();
WINNER = nplayers;
/*
* declare arrays and initialize to default values
* register the players (ID and username)
*/
int players[nplayers][2];
char pnames[nplayers][100];
int i;
for (i = 0; i < nplayers; i++) {
players[i][0] = 0;
players[i][1] = 0;
strcpy(pnames[i], " ");
}
setplayers (players, pnames, nplayers);
//list players
putchar('\n');
getranking(players, pnames, nplayers);
// let's play
if (nplayers == 1) {
play(players, pnames, 1);
} else {
while (WINNER != 1) {
play(players, pnames, WINNER);
}
}
return 0;
}
3. game.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "game.h"
#define MY_MAX_RANDOM 2.0
#define TRUE 1
int WINNER;
int getnumber() {
while (TRUE) {
char input[11];
fgets(input,11,stdin);
int number;
char c;
if ( (sscanf(input, " %d %c", &number, &c) == 1) && (number > 0) ) {
return number;
} else {
if (strlen(input) == (10)) {
int ch;
while (ch = getchar() != '\n' && ch != EOF);
}
printf(" Try again: ");
}
}
}
void setplayers(int players[][2], char pnames[][100], int nplayers) {
int i, NAME_OK;
// auto assign the next ID
for (i = 0; i < nplayers; i++) {
int id = i+1;
players[i][0] = id;
}
//make sure player is registered
NAME_OK = 0;
while (!NAME_OK) {
for (i = 0; i < nplayers; i++) {
printf(" Username for player %d: ", players[i][0]);
// read names and remove (new line) char
if (fgets(pnames[i], sizeof (pnames[i]), stdin) != NULL) {
size_t length = strlen(pnames[i]);
if (length > 2 && pnames[i][length-1] == '\n') {
pnames[i][--length] = '\0';
NAME_OK = 1;
} else {
i--;
}
}
}
}
}
void getranking(int players[][2], char pnames[][100], int nplayers) {
printf(" Player ID \t Attempts \t Player\n");
int i;
for (i = 0; i < nplayers; i++) {
printf(" %04d \t\t %d \t\t %s\n", players[i][0], players[i][1], pnames[i]);
}
}
void play(int players[][2], char pnames[][100], int nplayers) {
int i;
for (i = 0; i < nplayers; i++) {
//temporary random number
int tmp_rand = (int) (rand() * MY_MAX_RANDOM / (RAND_MAX + 1.0)) + 1;
int attempts = 0;
printf("\n\n\n Player: %s\n\n "
"Guess the number between 1 and %d :\n",
pnames[i], (int)MY_MAX_RANDOM);
while (TRUE) {
//get the guess
int shot = getnumber();
//preemptive increase of attempts
++attempts;
//check if well guessed and break the loop if it's the case
if (shot == tmp_rand) {
printf("\n Cool, player %s! You guessed the number"
" in %d shots!\n\n", pnames[i], attempts);
players[i][1] = attempts;
break;
} else if (shot < tmp_rand) {
puts(" Too low!");
} else {
puts(" Too high!");
}
}
}
//print average attempts
float avrga = printavrgscore(players, nplayers);
printf(" Average attempts: %.2f\n\n", avrga);
// sort players by attempts
sortplayers(players, pnames, nplayers);
//print the ranking
getranking(players, pnames, nplayers);
// process the game
process_game(players, pnames, nplayers);
}
float printavrgscore(int players[][2], int nplayers) {
float tmp = 0;
int i;
for (i = 0; i < nplayers; i++) {
tmp+=players[i][1];
}
return (tmp/nplayers);
}
void sortplayers(int players[][2], char pnames[][100], int nplayers) {
int tmp[nplayers][2];
char ntmp[nplayers][100];
//sort
int i;
for (i = 0; i < nplayers - 1; i++) {
int j;
for(j = 0; j < nplayers - 1; j++) {
if (players[j][1] > players[j+1][1]) {
//switch ID
tmp[j][0] = players[j][0];
players[j][0] = players[j+1][0];
players[j+1][0] = tmp[j][0];
//switch attempts
tmp[j][1] = players[j][1];
players[j][1] = players[j+1][1];
players[j+1][1] = tmp[j][1];
//switch names
strcpy(ntmp[j], pnames[j]);
strcpy(pnames[j], pnames[j+1]);
strcpy(pnames[j+1], ntmp[j]);
}
}
}
}
void process_game(int players[][2], char pnames[][100], int nplayers) {
/*
* mia: minimum of attempts
* the list is already sorted, so
* players[0][1] scored mia
*/
int mia = players[0][1];
/*
* assume it's the final
* 0: just one player with mia
*/
int draw = 0;
/*
* check if other players got also a mia
* if so, increase the draw
*/
int i;
for (i = 0; i < nplayers - 1; i++) {
if (players[i][1] == mia) {
if (players[i][1] == players[i+1][1]) {
draw++;
}
}
}
/*
* if one player with mia,
* beep and print this winner
*/
if (draw == 0) {
printf("\n ");
for (i=0; i<20; i++) {
printf("-");
Sleep(100);
}
printf("->%c%c",7,7);
if (mia == 1) {
printf(" Winner: %s scored in just one shot!!",
pnames[0], mia);
} else {
printf(" Winner: %s scored in %d shots",
pnames[0], mia);
}
// set winner to 1
WINNER = 1;
} else {
//play-off
WINNER = draw + 1;
}
}