https://upload.wikimedia.org/wikiped...issors.svg.png
It is rock-paper-scissors game, of some sort !
It is one player against CPU, but in the future I want to make it for two human players.
Some questions:
1. Is there a better "algorithm" ? just a hint please.
2. Some comments or hints to improve the code.
3. Security and is it safe? easy to crack, garbage .. thins like that.
And thanks in advance.
My source code:
Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(int argc, char *argv[] ) {
srand(time(NULL));
char hyphen[15] = "---------------";
int game_played = 0;
int ccount = 0; //cpu's score
int pcount = 0; //player's score
char choice[3] = {'r','p','s'}; // Rock-Paper-Scissors
printf ("\n\n\t%sRPS GAME%s\n\n",hyphen,hyphen);
while (1) {
int rand_ch = rand() % 3; // rand number between 0 and 2
printf("\n\tCPU played, now your turn!\n");
char computer_ch = choice[rand_ch]; // cpu's choice: spr
printf("\tEnter(r/p/s) to play or (x) to exit: ");
char player_ch = getchar(); // get choice from human player
// exit if no rps
if (player_ch != 'r' && player_ch != 'p' && player_ch != 's' && player_ch != 'x') {
while (getchar() != '\n');
continue;
} else if (player_ch == 'x') {
break;
} else {
game_played = 1;
}
while (getchar() != '\n' ); // clean buffer
if ((player_ch == 's' && computer_ch == 'p')
|| (player_ch == 'p' && computer_ch == 'r')
|| (player_ch == 'r' && computer_ch == 's')) {
pcount+=3;
printf("\n\n\t---> :) You win!\n\n");
} else if ((computer_ch == 's' && player_ch == 'p')
|| (computer_ch == 'p' && player_ch == 'r')
|| (computer_ch == 'r' && player_ch == 's')) {
ccount+=3;
printf("\n\n\t---> :( You lose!\n");
} else {
ccount++;
pcount++;
printf("\n\n\t---> :/ A draw!\n");
}
printf("\tCPU's choice: %c \n", computer_ch);
printf("\tYour choice: %c \n", player_ch);
}
//end game
if (game_played == 1) {
printf("\n\t%s%s\n", hyphen,hyphen);
printf("\tComputer score %d. \n", ccount);
printf("\tAnd your score %d. \n", pcount);
if (ccount > pcount) {
printf("\n\tYou lost the game!\n");
} else if (ccount < pcount) {
printf("\n\tYou won the game!\n");
} else {
printf("\n\tA big tie!\n");
}
printf("\t%s%s\n\n", hyphen,hyphen);
}
}