Thread: My C game ! It's waiting your approval / disapproval

  1. #1
    Registered User
    Join Date
    Feb 2016

    Cool My C game ! It's waiting your approval / disapproval
    My C game ! It's waiting your approval / disapproval-gdoqvy6-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:

    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    int main(int argc, char *argv[] ) {
    	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');
    		} else if (player_ch == 'x') {
    		} 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')) {
    			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')) {
    			printf("\n\n\t---> :( You lose!\n");		
    		} else {
    			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);

  2. #2
    Registered User
    Join Date
    Jun 2011
    Well done.

    Per your request, here are some comments:

    • Code:
      char hyphen[15] = "---------------";
      You're using "hyphen" as a string, which has a size of 15 and has 15 '-' characters ... you're not leaving enough room for the string-terminating null character. You can change this to 16, or in this instance, just let the compiler figure it out:

      char hyphen[] = "---------------";
      But since this string is only for printing, and is not being modified, you could even assign the string literal to a pointer:

      const char *hyphen = "---------------";
    • You can refactor some of this code into separate functions, for ease of testing and development.
    • I don't know if it's standard to assign a score of 3 for a win and 1 for draw, but you should consider making these score values named constants instead of magic numbers, so if you ever want to change them, the modification will be easy.

      #define SCORE_VALUE_WIN  3
      #define SCORE_VALUE_TIE  1
      ccount += SCORE_VALUE_WIN;

    • Code:
      int ccount   = 0; //cpu's score
      int pcount   = 0; //player's score
      You should give these variables clearer names. If you have to describe what a variable is for with a comment, then the variable name is probably not clear enough - and these comments won't be as helpful when you see the variable names elsewhere within the code. "score_computer" and "score_player" are fine variable names.
    • While the code for input validation seems a little clumsy, it does appear to work as intended. I could also suggest alternate means for representing the data (rock/paper/scissors), but what you have should work well for now.

    Also, your formatting is very nice and consistent - bonus points for that.

  3. #3
    Registered User Gibs_Rey's Avatar
    Join Date
    Dec 2015
    It's neat and readable, i like the way you manage the fractions of code, for example the logical operators you have there ;-)


  4. #4
    Registered User
    Join Date
    Feb 2016
    dear Matticus,
    thank you for the great comments. I updated the code, and I'm thinking now how and where to use some functions to reduce the size of main().

    I removed +=3 cause it's "rsp" not football Now just ++ when computer or player wins!

    const char *hyphen = "---------------";
    I don't use that any more as there's no need to make a variable for that; but I am using the same idea for a function (for representing the data in a better way):

    const char *printchoice(char c);
    printf("\tCPU's choice: %s \n", printchoice(computer_ch));
    printf("\tYour  choice: %s \n", printchoice(player_ch));
    const char *printchoice(char c){
        switch (c) {
            case 'r':
                return "Rock";
            case 's':
                return "Scissors";
            case 'p':
                return "Paper";
    Last edited by yvan; 03-04-2016 at 10:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Waiting for a process to end
    By Panda125 in forum C Programming
    Replies: 20
    Last Post: 07-28-2009, 08:42 AM
  2. Waiting for a function
    By fssp in forum C Programming
    Replies: 3
    Last Post: 03-30-2009, 03:13 AM
  3. Draft code for your approval.
    By anonytmouse in forum C Programming
    Replies: 2
    Last Post: 12-21-2002, 11:22 AM
  4. New Graphic Game > Need Your Approval
    By Denethor2000 in forum Game Programming
    Replies: 18
    Last Post: 07-20-2002, 11:52 PM
  5. waiting
    By phantom in forum C++ Programming
    Replies: 7
    Last Post: 01-28-2002, 06:38 AM