This is supposed to be a Rock-Paper-Scissors game. Everything's just going fine until I inserted graphics. What I was supposed to do is to display the figures I've poorly made whenever a choice is made. I can't really explain it so please take a look at my code, thanks (I'm hoping for a kind response.)

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<graphics.h>

void input()
{
     int choice, b, c, i, Cscore=0, Pscore=0;
     srand(time(NULL));


     gotoxy (20,1);
     textcolor ( GREEN );
     cprintf("- - - - - - - - - - - - - - - - - - - - - - - ");
     gotoxy (38,2);
     printf("PLAYER\n\n\n");
     gotoxy (33,3);
     printf("Enter your input.\n\n\n");
     gotoxy (23,4);
     printf("Please input the number and press Enter:\n");
     gotoxy (23,5);
     textcolor (BLUE);
     cprintf("****************************************");
     gotoxy (23,6);
     textcolor (YELLOW);
     cprintf("1 - Rock      2 - Paper     3 - Scissors\n\n\n");
     gotoxy (20,7);
     textcolor (GREEN);
     cprintf("- - - - - - - - - - - - - - - - - - - - - - - ");
     for (i=0;i<5;i++)
     {
     gotoxy (33,8);
     printf("Your input is: ");
     scanf("%d", &choice);

     system("CLS");

     int computer=rand()%3+1;
        if(choice==1)
        {


           int gd=DETECT,gm,points[]={135,350,135,300,125,270,125,240,175,230,185,250,175,300,175,350,135,350};

           initgraph(&gd, &gm, "C:\\TC\\BGI");

           fillpoly(9, points);





        if(computer==1)
        {
            printf("\t\t\tComputer picked (1) Rock.\n");
            printf("\n\t\t\tDraw.\n");
            printf("\n\t\t\tPlayer Score: %d\n",Pscore);
            printf("\n\t\t\tComputer Score: %d\n\n\n",Cscore);




            int gd=DETECT,gm,points[]={535,350,535,300,525,270,525,240,575,230,585,250,575,300,575,350,535,350};

            initgraph(&gd, &gm, "C:\\TC\\BGI");

            fillpoly(9, points);



        }
        if(computer==2)
        {

            printf("\t\t\tComputer picked (2) Paper.\n");
            printf("\n\t\t\tComputer Wins!\n");
            Cscore= Cscore + 1;
            printf("\n\t\t\tPlayer Score: %d\n",Pscore);
            printf("\n\t\t\tComputer Score: %d\n\n\n",Cscore);





            int gd=DETECT,gm,points[]={535,350,535,300,520,270,520,205,530,205,530,240,535,240,535,150,545,150,545,240,550,240,550,147,560,147,560,240,565,240,565,150,575,150,575,260,580,260,580,210,590,210,590,270,575,300,575,350,530,350};

            initgraph(&gd, &gm, "C:\\TC\\BGI");

            fillpoly(25, points);




        }
        if(computer==3)
        {
            printf("\t\t\tComputer picked (3) Scissors.\n");
            printf("\n\t\t\tPlayer Wins!\n");
            Pscore = Pscore + 1;
            printf("\n\t\t\tPlayer Score: %d\n",Pscore);
            printf("\n\t\t\tComputer Score: %d\n\n\n",Cscore);




            int gd=DETECT,gm,points[]={535,350,535,300,520,270,520,240,530,240,530,235,540,235,540,145,553,145,553,230,560,230,560,148,573,148,573,235,590,260,575,300,575,350,530,350};

            initgraph(&gd, &gm, "C:\\TC\\BGI");

            fillpoly(18, points);




        }
        }
          else  if(choice==2)
          {


           int gd=DETECT,gm,points[]={135,350,135,300,120,270,120,205,130,205,130,240,135,240,135,150,145,150,145,240,150,240,150,147,160,147,160,240,165,240,165,150,175,150,175,260,180,260,180,210,190,210,190,270,175,300,175,350,130,350};

           initgraph(&gd, &gm, "C:\\TC\\BGI");

           fillpoly(25, points);






            if(computer==2)
            {
            printf("\t\t\tComputer picked (2) Paper.\n");
            printf("\n\t\t\tDraw.\n");
            printf("\n\t\t\tPlayer Score: %d\n",Pscore);
            printf("\n\t\t\tComputer Score: %d\n\n\n",Cscore);
            }
            if(computer==3)
            {
            printf("\t\t\tComputer picked (3) Scissors.\n");
            printf("\n\t\t\tPlayer Wins!\n");
            Pscore = Pscore + 1;
            printf("\n\t\t\tPlayer Score: %d\n",Pscore);
            printf("\n\t\t\tComputer Score: %d\n\n\n",Cscore);
            }
            if(computer==1)
            {
            printf("\t\t\tComputer picked (1) Rock.\n");
            printf("\n\t\t\tComputer Wins!\n");
            Cscore= Cscore + 1;
            printf("\n\t\t\tPlayer Score: %d\n",Pscore);
            printf("\n\t\t\tComputer Score: %d\n\n\n",Cscore);
            }
        }
        else if(choice==3)
        {

           int gd=DETECT,gm,points[]={135,350,135,300,120,270,120,240,130,240,130,235,140,235,140,145,153,145,153,230,160,230,160,148,173,148,173,235,190,260,175,300,175,350,130,350};

           initgraph(&gd, &gm, "C:\\TC\\BGI");

           fillpoly(18, points);



            if(computer==3)
            {
            printf("\t\t\tComputer picked (3) Scissors.\n");
            printf("\n\t\t\tDraw.\n");
            printf("\n\t\t\tPlayer Score: %d\n",Pscore);
            printf("\n\t\t\tComputer Score: %d\n\n\n",Cscore);
            }
            if(computer==2)
            {
            printf("\t\t\tComputer picked (2) Paper.\n");
            printf("\n\t\t\tComputer Wins!\n");
            Cscore= Cscore + 1;
            printf("\n\t\t\tPlayer Score: %d\n",Pscore);
            printf("\n\t\t\tScore is %d\n\n\n",Cscore);
            }
            if(computer==1)
            {
            printf("\t\t\t\tComputer picked (1) Rock.\n");
            printf("\n\t\t\tPlayer Wins!\n");
            Pscore = Pscore + 1;
            printf("\n\t\t\tPlayer Score: %d\n",Pscore);
            printf("\n\t\t\tComputer Score: %d\n\n\n",Cscore);
            }
        }
    else{
        printf("\t\t\tWrong Answer.\n\n\n");
        }
        }

       if(Cscore > Pscore)

        {
        printf("\t\t\tComputer wins %d to %d.\n",Cscore,Pscore);
        }

        else if(Cscore < Pscore )

        {
        printf("\t\t\tPlayer wins %d to %d.\n",Pscore,Cscore);
        }

       else if(Cscore ==  Pscore )

        {
        printf("\t\t\tNo winner, it is a draw!\n");
        }


     getch();

}

main(void)

{
      clrscr();
      char x,y,flag = 'p';
      gotoxy(12,1);
      printf("This is a game called Jack-en-poy or Rock, Paper, Scissors\n");
      gotoxy(20,2);
      printf("You are going to play against the computer.\n");
      gotoxy(25,3);
      printf("Press any key to continue. . .");
      getch();
      system("CLS");
            do{
            system("CLS");
            input();
            system("CLS");
            printf("Press any key to exit:\n\nTo play again, press P\n\n");

            flag = getch();
            } while (flag == 'p');
      system("CLS");
      printf("Program Terminating. . .");

      getch();
      return(0);
}

Recommended Answers

All 2 Replies

Sadly, my earlier recommendation is as kind a response as I can make; it really is terrible for any professor to be forcing their students to use something as long outdated as Turbo C++, and students shouldn't put up with it. However, seeing how you are stuck in this position, I can give you some practical advice about getting your program to work.

The first thing I recommend is to break the input() function into several smaller functions; right now, there is a tremendous amount of redundancy in the code that could be simplified by modularizing the program better. For example, you could have a function that takes the outcome of the turn and prints whether it is win, lose or draw.

Second, you can structure the program better if, when testing a single variable's value for multiple outcomes, you used a switch statement instead of a series of if statements. This makes the intention clearer, and makes certain kinds of errors less likely.

You can also clarify things by naming the different types of casts, rather than using the magic numbers directly as you are doing now. You can use #define for this purpose, but since there are a sequence of values, you would be better off using enum types for this:

enum outcome {DRAW, PLAYER_WIN, COMPUTER_WIN};
enum hand {ROCK, PAPER, SCISSORS};

These can then be used like so:

outcome get_result(hand player, hand computer) 
{
    switch(player)
    {
        case ROCK:
            switch(computer)
            {
                case ROCK:
                     return DRAW:
                case PAPER:
                     return COMPUTER_WIN;
                case SCISSORS:
                     return PLAYER_WIN;
            }
        case PAPER:
            switch(computer)
            {
                case ROCK:
                     return PLAYER_WIN:
                case PAPER:
                     return DRAW;
                case SCISSORS:
                     return COMPUTER_WIN;
            }
        case SCISSORS:
            switch(computer)
            {
                case ROCK:
                     return COMPUTER_WIN:
                case PAPER:
                     return PLAYER_WIN;
                case SCISSORS:
                     return DRAW;
            }
    }
}

This still isn't optimal, but it would make it much, much easier to handle determining the results.

I also recommend that, rather than hard-coding the image bitmaps into the C++ code, that you save the data in some files and load it when the program begins; this will make it easier to change them if needed. I don't know if your course has covered file handling yet, though, so this may not be an option for you.

Finally, I should point out that, if you have to use <conio.h>, you may as well use it to full advantage; replace the system("CLS") calls with calls to clrscr(), which is faster than shelling out to the system command line.

Adding to what I wrote earlier, here's a simple trick to simplify the printout of the individual hands:

char* get_hand_name(hand h)
{
    char names[][] = {"Rock", "Paper", "Scissors"};

    return names[h];
}

char* get_result_description(outcome result)
{
    char descriptions[][] = {"Draw.", "win for the Player.", "win for the Computer."}
    return descriptions[result];
}

You can then make your printout much simpler:

void print_outcome(hand player, hand computer)
{
    printf("The player chose %s", get_hand_name(player));
    printf("The computer chose %s", get_hand_name(computer);
    printf("This match is a %s", get_result_description(get_result(player, computer)));
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.