Whats wrong here, getting wrong results from the game. Looks like things stay in stdin and produces wrong results. And also, you have too answer the question twice. As i see the code is flawless (apperently not). But anyway, im desperate for helt here so.. This is my second week with C so..
Code:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<string.h>
#define MAX_TURNS 10
#define MAX_BUFFER 6 /* Tricky 5 won't do */
#define DRAW 0
#define COMPUTER_WIN 1
#define HUMAN_WIN 2
#define STONE 0
#define SCISSOR 1
#define PAPER 2
#define STONE_STR "s" /* Used in logic not IO */
#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);
/***************************************************
*
* MAIN
*
***************************************************/
int main()
{
int human, computer, result, statistic;
srand( time(0) );
statistic = 0;
printf("Welcome to paper, scissor, stone game ...\n");
human=human_choice(human);
computer=computer_choice(computer);
print_computer_choice();
result=winner(human,computer);
print_winner(result);
return 0;
}
/******************************************************
*
* DEFINITIONS
*
******************************************************/
void clear_stdin()
{
while( getchar() != '\n' ){;}
}
int human_choice(int human) {
char humanchar;
printf("Stone(s), 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)
{
human=human_choice(human);
computer=computer_choice(computer);
int result=0;
if (computer == human) {
result = 4;
}
else if (human == 0 && computer == 1) {
result = 5;
}
else if (human == 1 && computer == 2) {
result = 5;
}
else if (human == 2 && computer == 0) {
result = 5;
}
else {
result = 6;
}
return result;
}
void print_winner(int result)
{
if(result==4)
printf("None win!\n");
if(result==6)
printf("Computer win!\n");
if(result==5)
printf("You win!\n");
}