Thread: Tic Tac Toe game - Playing against the computer

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    3

    Tic Tac Toe game - Playing against the computer

    Hi I'm currently doing a project in first year regarding a Tic Tac Toe game. In the code I currently have I have most of it in order but I'm having quite alot of trouble trying to include playing against the computer instead of player 1 vs player 2. I need to implement random numbers and making it that it's me against the computer but I have tried different solutions without success (the function I created for the computer is empty because the previous code wasn't working)

    Can someone point out what I should do to fix this? Thanks

    Code:
    #include <stdio.h>#include <stdlib.h> 
    
    
    
    
    void board_design();
    void marking_board(int, char);
    int check_winner();
    
    
    char grid[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    
    
    int main()
    {
        int player = 1, i, selection;
        int computer = 1;
        char mark;
        
        do
        {
            board_design();
            player = (player % 2) ? 1 : 2;
    
    
            printf("Player %d, enter a number: ", player);
            scanf("%d", &selection);
    
    
            mark = (player == 1) ? 'X' : 'O';
    
    
            computer_picks();
    
    
            marking_board(selection, mark);
            i = check_winner();
    
    
            player++;
            computer++;
    
    
        } while (i == -1);
    
    
        board_design();
    
    
        if (i == 1)
            printf("Congratulations! YOU ARE THE WINNER! ", --player);
        else
            printf("Game draw");
    
    
        return 0;
    }
    
    
    void board_design()
    {
        printf("\n\n\tTic Tac Toe Game\n\n");
    
    
        printf("Player 1 (X)  -  Computer (O)\n\n\n");
    
    
    
    
        printf("     |     |     \n");
        printf("  %c  |  %c  |  %c \n", grid[1], grid[2], grid[3]);
    
    
        printf("_____|_____|_____\n");
        printf("     |     |     \n");
    
    
        printf("  %c  |  %c  |  %c \n", grid[4], grid[5], grid[6]);
    
    
        printf("_____|_____|_____\n");
        printf("     |     |     \n");
    
    
        printf("  %c  |  %c  |  %c \n", grid[7], grid[8], grid[9]);
    
    
        printf("     |     |     \n\n");
    }
    
    
    void computer_picks()
    
    
    
    
    void marking_board(int selection, char mark)
    {
        if (selection == 1 && grid[1] == '1')
            grid[1] = mark;
    
    
        else if (selection == 2 && grid[2] == '2')
            grid[2] = mark;
    
    
        else if (selection == 3 && grid[3] == '3')
            grid[3] = mark;
    
    
        else if (selection == 4 && grid[4] == '4')
            grid[4] = mark;
    
    
        else if (selection == 5 && grid[5] == '5')
            grid[5] = mark;
    
    
        else if (selection == 6 && grid[6] == '6')
            grid[6] = mark;
    
    
        else if (selection == 7 && grid[7] == '7')
            grid[7] = mark;
    
    
        else if (selection == 8 && grid[8] == '8')
            grid[8] = mark;
    
    
        else if (selection == 9 && grid[9] == '9')
            grid[9] = mark;
    
    
        else
        {
            printf("Invalid move, please try again ");
        }
    }
    
    
    int check_winner() {
        
        if (grid[1] == grid[2] && grid[2] == grid[3])
            return 1;
    
    
        else if (grid[4] == grid[5] && grid[5] == grid[6])                //Checking to see if the winner has an horizontal match
            return 1;
    
    
        else if (grid[7] == grid[8] && grid[8] == grid[9])
            return 1;
    
    
        else if (grid[1] == grid[4] && grid[4] == grid[7])
            return 1;
    
    
        else if (grid[2] == grid[5] && grid[5] == grid[8])                  //Checking to see if winner has vertical match
            return 1;
    
    
        else if (grid[3] == grid[6] && grid[6] == grid[9])
            return 1;
    
    
    
    
        else if (grid[1] == grid[5] && grid[5] == grid[9])                   // Check if winner has diagonal match
            return 1;
       
        else if (grid[3] == grid[5] && grid[5] == grid[7])
            return 1;
    
    
    
    
        else if (grid[1] != '1' && grid[2] != '2' && grid[3] != '3' && grid[4] != '4' && grid[5] != '5'
            && grid[6] != '6' && grid[7] != '7' && grid[8] != '8' && grid[9] != '9')                 // No match in the game
    
    
            return 0;
        else
            return -1;
    }

  2. #2
    Registered User
    Join Date
    Nov 2020
    Posts
    3
    Anyone that can help? Just need some guidance regarding this

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,157
    What you're basically trying to do is to replace the user input with something generated by the computer. So, since the user input consists of a number representing a square selection, you just need computer_picks() to return a number in the same range, then your marking_board function should do the job but with a different mark representing the computer's selection.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2020
    Posts
    3
    Quote Originally Posted by laserlight View Post
    What you're basically trying to do is to replace the user input with something generated by the computer. So, since the user input consists of a number representing a square selection, you just need computer_picks() to return a number in the same range, then your marking_board function should do the job but with a different mark representing the computer's selection.
    Thank you for the reply back. I have tried using randon numbers (rand) to get the computer to pick a number between 1 and 9 but it doesn't seem to be working for me the way I want it too which is totally confusing me how it's meant to be implemented

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,157
    Try writing out the steps on paper and then "executing" your algorithm on paper.

    It looks like you might be getting confused between whether you want this to be a player versus player or player versus computer, so think carefully about that. You also might want to simplify a bit first, e.g., assume that the player goes first and the computer goes second. Remember that you need to determine not only if the player has won, but if the computer has won.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    311

    Post

    random numbers and making it that it's me against the computer


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int valid = 1, endGame = 0, currentPlayer = 1 ;
    
    #define playerTwoIsComputer 1
    
    #define playerOne 'X'
    #define playerTwo 'O'
    
    char selection, grid[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    
    void draw_title() {
      system( "cls || clear" ) ;
      printf( "\n\tTic Tac Toe Game\n\n");
      if (playerTwoIsComputer) {
        printf(" Player 1 (%c)  -  Computer (%c) ", playerOne, playerTwo);
      } else {
        printf(" Player 1 (%c)  -  Player 2 (%c) ", playerOne, playerTwo);
      }  
      printf( "\n\n\n");
    }
    
    void draw_board() {
      draw_title();
      printf("\t     |     |     \n");
      printf("\t  %c  |  %c  |  %c \n", grid[1], grid[2], grid[3]);
      printf("\t_____|_____|_____\n");
      printf("\t     |     |     \n");
      printf("\t  %c  |  %c  |  %c \n", grid[4], grid[5], grid[6]);
      printf("\t_____|_____|_____\n");
      printf("\t     |     |     \n");
      printf("\t  %c  |  %c  |  %c \n", grid[7], grid[8], grid[9]);
      printf("\t     |     |     \n\n\n");
    }
    
    int drawPiece(char character,int index) {
      int onBlock = ( index + 48 );
      if ( grid[index] == onBlock ) {
        grid[index] = character;
        return 1;
      } else { 
        return 0; 
      }
    }
    
    void ask_menu() {
      printf(" Player %i, enter a number: ", currentPlayer);
      scanf("%i", &selection);
    }
    
    void computerPlayerTwo() {
      int hasValid = 0;
      for (int i=1; i<9; i++) {
        if ((grid[i] != playerOne) && (grid[i] != playerTwo)) {
          hasValid = 1;
        }
      }
     if (hasValid) {
      do {
        selection = ((rand() % 8)+1);
        valid = drawPiece( playerTwo, selection );
      } while (!valid);
     } else {
        draw_board();
        printf("Game Over. Tie.");
        exit(0);
     }
    }
    
    int check_results() {
     int cPoint = 0, cLog = 0;
      cPoint = 1; if (grid[cPoint] == grid[cPoint+1] && grid[cPoint+1] == grid[cPoint+2]) return 1;
      cPoint = 4; if (grid[cPoint] == grid[cPoint+1] && grid[cPoint+1] == grid[cPoint+2]) return 1;
      cPoint = 7; if (grid[cPoint] == grid[cPoint+1] && grid[cPoint+1] == grid[cPoint+2]) return 1;  
      cPoint = 1; if (grid[cPoint] == grid[cPoint+3] && grid[cPoint+3] == grid[cPoint+6]) return 1;
      cPoint = 2; if (grid[cPoint] == grid[cPoint+3] && grid[cPoint+3] == grid[cPoint+6]) return 1;
      cPoint = 3; if (grid[cPoint] == grid[cPoint+3] && grid[cPoint+3] == grid[cPoint+6]) return 1;
      cPoint = 1; if (grid[cPoint] == grid[cPoint+4] && grid[cPoint+4] == grid[cPoint+8]) return 1;
      cPoint = 3; if (grid[cPoint] == grid[cPoint+2] && grid[cPoint+2] == grid[cPoint+4]) return 1;
     cPoint = 0; return cPoint;
    };
    
    int main( int args, char * argv[] ) { srand(time(0));
    
    draw_board(); void game_over();
    
    while (!endGame) {
    
      if (currentPlayer == 1) {
       ask_menu();
       valid = drawPiece( playerOne, selection );
      } else {
       if (playerTwoIsComputer) {
          computerPlayerTwo();
       } else {
          ask_menu();
          valid = drawPiece( playerTwo, selection );
       }
      } 
      
      if (valid) {
        if (check_results()) { draw_board(); game_over(); };
        currentPlayer++; 
        if (currentPlayer > 2) currentPlayer = 1;
        draw_board();
      } else {
        draw_board();
        printf(" Invalid selection: %i\nTry again ", selection );
      }
    
    };
    
    return 0;
    }
    
    void game_over() {
      if (playerTwoIsComputer && currentPlayer == 2) {
        printf("\nGame Over. Computer won. \n" ); 
      } else {           
        printf("\nGame Over. Player %i won. \n", currentPlayer);
      }
      exit(0);        
    }
    Last edited by Structure; 11-20-2020 at 09:12 AM.
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a game against the computer!
    By james34 in forum C Programming
    Replies: 4
    Last Post: 11-24-2017, 11:46 AM
  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