tic tac toe

This is a discussion on tic tac toe within the Game Programming forums, part of the General Programming Boards category; To test my C++ skills, i tried making a tic tac toe game. There is an error though. Occasionally it ...

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    tic tac toe

    To test my C++ skills, i tried making a tic tac toe game. There is an error though. Occasionally it will put the X in the wrong spot, or it will put 2 letters on the board. Can you please tell me why itll put 2 letters on or why it doesnt put it in the right spot? Thanks!!!

    Code:
    #include <iostream>
    using namespace std;
    
    class board {
     char board[2][2];
    
     public:
      board() {
       for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 3; j++) {
         board[i][j] = ' ';
        }
       }
      }
    
      void display() {
       cout << "     X     \n";
       cout << " 0 | 1 | 2\n\n";
       for(int i = 0; i < 3; i++) {
        cout << " " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << " ";
        cout << "  " << i;
        if(i == 1) {
         cout << " Y ";
        }
        if(i != 2) {
         cout << "\n---|---|---  -\n";
        }
       }
      }
    
      void playerMove() {
       int x, y;
       cout << "\nEnter x coordinate: ";
       cin >> x;
       cout << "\nEnter y coordinate: ";
       cin >> y;
       if(board[x][y] != ' ') {
        cout << "Incorrect coordinate. Please enter again.\n";
        playerMove();
       } else {
        board[x][y] = 'X';
       }
      }
    
      void computerMove() {
       if((board[0][0] == board[1][1]) && (board[2][2] == ' ')) {
        board[2][2] = 'O';
        return;
       }
       if((board[1][1] == board[2][2]) && (board[0][0] == ' ')) {
        board[0][0] = 'O';
        return;
       }
       if((board[0][0] == board[2][2]) && (board[1][1] == ' ')) {
        board[1][1] = 'O';
        return;
       }
       if((board[0][2] == board[1][1]) && (board[2][0] == ' ')) {
         board[2][0] = 'O';
         return;
       }
       if((board[2][0] == board[1][1]) && (board[0][2] == ' ')) {
        board[0][2] = 'O';
        return;
       }
       if((board[2][0] == board[0][2]) && (board[1][1] == ' ')) {
        board[1][1] = 'O';
        return;
       }
       for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 3; j++) {
         if(board[i][j] == ' ') {
          board[i][j] = 'O';
          return;
         }
        }
       }
      }
    
      char checkWinner() {
       for(int i = 0; i < 3; i++) {
        if((board[i][0] == board[i][1]) && (board[i][1] == board[i][2])) {
         return board[i][0];
        }
        if((board[0][i] == board[1][i]) && (board[1][i] == board[2][i])) {
         return board[0][i];
        }
       }
       if((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) {
        return board[0][0];
       }
       if((board[0][2] == board[1][1]) && (board[1][1] == board[2][0])) {
        return board[0][2];
       }
       return ' ';
      }
    };
    
    int main() {
     board b;
     char win;
     do {
      b.display();
      b.playerMove();
      win = b.checkWinner();
      if(win != ' ') {
       break;
      }
      b.computerMove();
      win = b.checkWinner();
     } while(win == ' ');
     if(win == 'X') {
      cout << "\nYOU WIN!\n\n";
     } else {
      cout << "\nTHE COMPUTER WINS!\n\n";
     }
     b.display();
     return 0;
    }
    Last edited by Lurker; 07-28-2003 at 12:55 AM.
    Do not make direct eye contact with me.

  2. #2
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    you have your X and Y mixed up.

    in a 2D array, you have:

    0:[0][1][2]
    1:[0][1][2]
    2:[0][1][2]

    where the numbers on the left are the first array dimension, and the numbers in the brackets are the second.

    so a coordinate of (0, 1) would land you here:

    [ ][X][ ]
    [ ][ ][ ]
    [ ][ ][ ]

    what this means is, the first number in your coordinate is actually your Y value. when the user puts in X first then Y and then you send the coordinates (X, Y) to the array, you are actually sending in the coordinates backwards.

    also, you are defining your array as an array of [2][2], but these numbers are actually the size of your array. you need to make them [3][3], and that will get rid of that nasty crash when your game ends

    hope this helped.

  3. #3
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    a little late, arent we? :P
    I came up with a cool phrase to put down here, but i forgot it...

  4. #4
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Woo hoo! Thank you guys !
    Do not make direct eye contact with me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 07:25 PM
  2. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 10:59 AM
  3. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 09:33 AM
  4. tic tac toe game
    By Leeman_s in forum Game Programming
    Replies: 9
    Last Post: 04-24-2002, 04:24 AM
  5. my tic tac toe game, please try it
    By Leeman_s in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 06:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21