Well i need to get this play_again-function to work. Have same problem with another game..
Just dont get it.. you cant use a while(true) and just return true or false from a function? i have tried that.
I will give you the whole code for the game, please have a look!
The game is working except the play_again-function-whateveritis
Code:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<string.h>
#define MAX_TURNS 10
#define MAX_BUFFER 6
#define DRAW 0
#define COMPUTER_WIN 1
#define HUMAN_WIN 2
#define STONE 0
#define SCISSOR 1
#define PAPER 2
#define STONE_STR "s"
#define SCISSOR_STR "k"
#define PAPER_STR "p"
/* ------------------- Utilities ---------------*/
void clear_stdin();
int human_choice(int human);
int computer_choice(int computer);
void print_computer_choice();
int winner(int human,int computer);
void print_winner(int result);
int play_again(int svar);
/***************************************************
*
* MAIN
*
***************************************************/
int main()
{
int human, computer, result, statistic, antalggr,
ggr=0, h=0, c=0;
srand( time(0) );
statistic = 0;
printf("Welcome to paper, stone, scissor-game ...\n");
printf("How many times do you want to play?\n");
scanf("%d", &antalggr);
while (ggr<antalggr) {
for (ggr = 0; ggr<antalggr; ggr++) {
if (result==HUMAN_WIN) {
h++;
}
else if(result==COMPUTER_WIN) {
c++;
}
human=human_choice(human);
computer=computer_choice(computer);
print_computer_choice();
result=winner(human,computer);
print_winner(result);
}
if (c>h){printf("Computer win round\n");
}
else if (h>c){
printf("Human win round\n");
}
else if (h==c) {
printf("Its a draw!\n");
}
/* play again? fix me! */
play_again(svar);
if (svar == true) {
;
}
else if (svar == false) {
break;
}
}
return 0;
}
/******************************************************
*
* DEFINITIONS
*
******************************************************/
void clear_stdin()
{
while( getchar() != '\n' ){;}
}
int human_choice(int human) {
char humanchar;
printf("Choose stones), scissor(k) or bag(p)\n");
printf(" : ");
scanf("%c", &humanchar);
switch(humanchar){
case 's': human = STONE; break;
case 'k': human = SCISSOR; break;
case 'p': human = PAPER; break;
}
clear_stdin();
return human;
}
int computer_choice(computer){
computer = rand() % 3;
return computer;
}
void print_computer_choice(int computer) {
char c_took;
switch(computer){
case 0: c_took = 's'; break;
case 1: c_took = 'k'; break;
case 2: c_took = 'p'; break;
}
printf("Computer choosed '%c'\n", c_took);
}
int winner(int human,int computer)
{
int result=0;
if (computer == human) {
result = DRAW;
}
else if (human == STONE && computer == SCISSOR) {
result = HUMAN_WIN;
}
else if (human == SCISSOR && computer == PAPER) {
result = HUMAN_WIN;
}
else if (human == PAPER && computer == STONE) {
result = HUMAN_WIN;
}
else {
result = COMPUTER_WIN;
}
return result;
}
void print_winner(int result)
{
if(result==DRAW)
printf("Draw!\n");
if(result==COMPUTER_WIN)
printf("Computer won!\n");
if(result==HUMAN_WIN)
printf("You won!\n");
}
int play_again(int svar) {
char getanswer;
printf("Do you want to play again?(j/n)\n");
scanf("%c", getanswer);
if (getanswer == 'j') {
svar = true;
}
else if (getanswer == 'n'){
svar = false;
}
return svar;
}