Thread: Need help with a game against the computer!

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    1

    Need help with a game against the computer!

    Hi im trying to design a game, like 21, but it is up to 42.

    the game will start and the user will choose whether they want another generated random number. they will keep being asked until they bust (at over 42) or they enter n and the game ends.


    once the player has had their turn, the computer will then play the game and the computer will either bust or stop after a given amount of turns the computer chooses.

    the person or player closest to 42 will win...... I have designed the players code.. but im struggling with then allowing the computer to play.. any help would be great.. thank you




    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main(void)
    {
        char answer=0; // y or n
        int score=0;
        int randomnumber=0;
        int computerscore=0;
        int gameover=0;
        int arandomnumber=0;
        int brandomnumber=0;
        int crandomnumber=0;
        int score2=0;
        char answer2=0;
        //game starts
        
    
        
        printf("Do you want to play the game? Y or N?:\n ");
        answer=getch();
        printf("%d", answer);
        
    
        while(answer == 'y' || answer == 'Y')
        {
            srand(time(NULL));
            randomnumber=rand()%21+1;
            
            
            printf("Your number is: %d\n\n", randomnumber);
            score=score+randomnumber;
            printf("Your score is: %d\n\n", score);
            
            if(answer == 'n'){
                printf("game over");
                
            }
            
            if (score>42){
                printf("You Bust\n\n");
                gameover=1;
                break;
                }
            
            printf("Do you want another number? Y or N?: \n\n");
            answer=getch();
            printf("%d\n", answer);
            
            }
            printf("now the computer plays\n\n");
            
            {
            srand(time(NULL));
            arandomnumber=rand()%21+1;
            printf("PC number is: %d\n\n", arandomnumber);
            score2=score2+arandomnumber;
            printf("The computers score is: %d\n\n", score2);
            
            
        }
         
            if (score<score2){
                printf("The computer won!");
            }
            
            if (score>score2){
                printf("Well done you won!");
            }
            
            
            
    
            
        
         system("pause");
        return 0;
    }
    Last edited by james34; 11-22-2017 at 02:03 PM.

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    In the real game 21, the dealer never makes any choice as to what options to take. The dealer has to play by specific rules. If your game is similar to 21, then the computer player should be doing the same. In 21, the dealer must take a card if it has less than 17 points. Thus in your game, you just have to give the computer player automatic random numbers if the total score of the computer play lies below the score threshold.
    Code:
    // computer total is 0
    // while computer score is less than 34
        // draw a random number and add it to computer total
    // if computer total is over 42, the computer busts
    // otherwise compare computer score to player score and determine winner

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I didn't even get to play really, it just ran through it leaving me with this
    Code:
    $ ./computer_42
    Do you want to play the game? Y or N?:
     y
    121Your number is: 19
    
    Your score is: 19
    
    Do you want another number? Y or N?: 
    
    10
    now the computer plays
    
    PC number is: 19
    
    The computers score is: 19
    
    sh: pause: command not found
    userx@slackwhere:~/bin
    I had to make a few adjustments to get that to get that far. getch is that a real function or just a windows function, I had to change it to getchar, and your a lot of unused data types going to waste.
    Code:
    gcc -Wall -lm -o "computer_42" "computer_42.c" (in directory: /home/userx/bin)
    computer_42.c: In function 'main':
    computer_42.c:21:12: warning: implicit declaration of function 'getch' [-Wimplicit-function-declaration]
         answer=getch();
                ^
    computer_42.c:15:10: warning: unused variable 'answer2' [-Wunused-variable]
         char answer2=0;
              ^
    computer_42.c:13:9: warning: unused variable 'crandomnumber' [-Wunused-variable]
         int crandomnumber=0;
             ^
    computer_42.c:12:9: warning: unused variable 'brandomnumber' [-Wunused-variable]
         int brandomnumber=0;
             ^
    computer_42.c:10:9: warning: variable 'gameover' set but not used [-Wunused-but-set-variable]
         int gameover=0;
             ^
    computer_42.c:9:9: warning: unused variable 'computerscore' [-Wunused-variable]
         int computerscore=0;
             ^
    /tmp/ccnkfvq1.o: In function `main':
    computer_42.c:(.text+0x5d): undefined reference to `getch'
    computer_42.c:(.text+0x12b): undefined reference to `getch'
    collect2: error: ld returned 1 exit status
    Compilation failed.
    Last edited by userxbw; 11-22-2017 at 03:07 PM.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    some of your brackets .. take a closer look are they were you really need or want them?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main(void)
    {
        char answer=0; // y or n
        int score=0;
        int randomnumber=0;
        int computerscore=0;
        int gameover=0;
        int arandomnumber=0;
        int brandomnumber=0;
        int crandomnumber=0;
        int score2=0;
        char answer2=0;
        //game starts
         
     
         
        printf("Do you want to play the game? Y or N?:\n ");
        answer=getchar();
        printf("%d", answer);
        
        while(answer == 'y' || answer == 'Y')
        {
            srand(time(NULL));
            randomnumber=rand()%21+1;
             
             
            printf("Your number is: %d\n\n", randomnumber);
            score=score+randomnumber;
            printf("Your score is: %d\n\n", score);
             
    
     
            if(answer == 'n'){
                printf("game over");
                 
            }
             
            if (score>42){
                printf("You Bust\n\n");
                gameover=1;
                break;
                }
             
            printf("Do you want another number? Y or N?: \n\n");
            answer=getchar();
            printf("%d\n", answer);
        } // end while loop 
            
            printf("now the computer plays\n\n");  
             
            { // open bracket
            srand(time(NULL));
            arandomnumber=rand()%21+1;
            printf("PC number is: %d\n\n", arandomnumber);
            score2=score2+arandomnumber;
            printf("The computers score is: %d\n\n", score2);
             
             
            } // close bracket  
          
            if (score<score2){
                printf("The computer won!");
            }
             
            if (score>score2){
                printf("Well done you won!");
            }
             
             
             
     
             
         
         system("pause");
        return 0;
    }
    getch():
    getch() is a nonstandard function and is present in conio.h header file which is mostly used by MS-DOS compilers like Turbo C. It is not part of the C standard library or ISO C, nor is it defined by POSIX (Source: conio.h - Wikipedia)
    Like above functions, it reads also a single character from keyboard. But it does not use any buffer, so the entered character is immediately returned without waiting for the enter key.
    Developer(s)
    Borland
    Initial release May 1990
    Stable release 2006 / September 5, 2006
    if you cannot afford a standard compiler you can install Linux and get one for free... just a suggestion.
    Last edited by userxbw; 11-22-2017 at 03:40 PM.

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    30
    If you are running this on Linux, you cannot use getch() from <conio.h>. If you still want that functionality, you need to use the following:
    Code:
    #include <termios.h>
    #include <unistd.h>
    
    int getch (void)
    {
        int ch;
        struct termios oldt, newt;
    
        tcgetattr(STDIN_FILENO, &oldt);
        newt = oldt;
        newt.c_lflag &= ~(ICANON|ECHO);
        tcsetattr(STDIN_FILENO, TCSANOW, &newt);
        ch = getchar();
        tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    
        return(ch);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game won't run DX but computer runs it fine
    By bunyan in forum Game Programming
    Replies: 7
    Last Post: 12-11-2013, 05:21 PM
  2. Game: Computer Guess My Number
    By Laythe in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2012, 11:25 PM
  3. a computer trivia game
    By Leeman_s in forum Game Programming
    Replies: 2
    Last Post: 01-22-2003, 08:30 PM
  4. Warlords the computer game
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-18-2001, 06:53 PM

Tags for this Thread