tic tac toe not working properly

This is a discussion on tic tac toe not working properly within the Game Programming forums, part of the General Programming Boards category; I'm making a tic tac toe game but it's not working properly. Sometimes it work properly for a while but ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267

    tic tac toe not working properly

    I'm making a tic tac toe game but it's not working properly. Sometimes it work properly for a while but then becomes unresponsive.
    Code:
    #include <iostream>
    #include "pause.h"
    #include "gotoxy.h"
    #include "mouse.h"
    #include "random.h"
    
    const char cross = 197,
               vline = 179,
               hline = 196;
    #define COMPUTER 0
    #define PLAYER 1
    
    namespace game{
              int turn = random()%2;
              char board[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
              int boardpos[3] = {10, 12, 14};
              int x, y, click;
              void draw();
              void p1move();
              void compmove();
              int checkwin();
    }
    
    using namespace game;
    
    int main(){
        int Continue = true;
        while (Continue){
              Continue = play();
        }
    }
    
    void game::draw(){
          gotoxy(10,10);cout<<board[0][0]<<vline<<board[1][0]<<vline<<board[2][0];
          gotoxy(10,11);cout<<   hline   <<cross<<   hline   <<cross<<   hline   ;
          gotoxy(10,12);cout<<board[0][1]<<vline<<board[1][1]<<vline<<board[2][1];
          gotoxy(10,13);cout<<   hline   <<cross<<   hline   <<cross<<   hline   ;
          gotoxy(10,14);cout<<board[0][2]<<vline<<board[1][2]<<vline<<board[2][2];
    }
    
    void game::p1move(){
          mouse(x, y, click);
          
          if (x == boardpos[0] && y == boardpos[0] && click && board[0][0] == ' '){
             board[0][0] = 'X'; turn = COMPUTER; }
          
          if (x == boardpos[1] && y == boardpos[0] && click && board[0][0] == ' '){
             board[1][0] = 'X'; turn = COMPUTER; }
          
          if (x == boardpos[2] && y == boardpos[0] && click && board[0][0] == ' '){
             board[2][0] = 'X'; turn = COMPUTER; }
          
          if (x == boardpos[0] && y == boardpos[1] && click && board[0][0] == ' '){
             board[0][1] = 'X'; turn = COMPUTER; }
          
          if (x == boardpos[1] && y == boardpos[1] && click && board[0][0] == ' '){
             board[1][1] = 'X'; turn = COMPUTER; }
          
          if (x == boardpos[2] && y == boardpos[1] && click && board[0][0] == ' '){
             board[2][1] = 'X'; turn = COMPUTER; }
          
          if (x == boardpos[0] && y == boardpos[2] && click && board[0][0] == ' '){
             board[0][2] = 'X'; turn = COMPUTER; }
          
          if (x == boardpos[1] && y == boardpos[2] && click && board[0][0] == ' '){
             board[1][2] = 'X'; turn = COMPUTER; }
          
          if (x == boardpos[2] && y == boardpos[2] && click && board[0][0] == ' '){
             board[2][2] = 'X'; turn = COMPUTER; }
    }
    
    void game::compmove(){
          if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == ' ')//horizontally
             board[2][0] = 'O';
          else if (board[0][0] == 'O' && board[2][0] == 'O' && board[1][0] == ' ')
             board[1][0] = 'O';
          else if (board[1][0] == 'O' && board[2][0] == 'O' && board[0][0] == ' ')
             board[0][0] = 'O';
          
          else if (board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == ' ')
             board[2][1] = 'O';
          else if (board[0][1] == 'O' && board[2][1] == 'O' && board[1][1] == ' ')
             board[1][1] = 'O';
          else if (board[1][1] == 'O' && board[2][1] == 'O' && board[0][1] == ' ')
             board[0][1] = 'O';
          
          else if (board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == ' ')
             board[2][2] = 'O';
          else if (board[0][2] == 'O' && board[2][2] == 'O' && board[1][2] == ' ')
             board[1][2] = 'O';
          else if (board[1][2] == 'O' && board[2][2] == 'O' && board[0][2] == ' ')
             board[0][2] = 'O';
          
          else if (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == ' ')//diagonally
             board[2][2] = 'O';
          else if (board[0][0] == 'O' && board[2][2] == 'O' && board[1][1] == ' ')
             board[1][1] = 'O';
          else if (board[1][1] == 'O' && board[2][2] == 'O' && board[0][0] == ' ')
             board[0][0] = 'O';
             
          else if (board[2][0] == 'O' && board[1][1] == 'O' && board[0][2] == ' ')
             board[0][2] = 'O';
          else if (board[2][0] == 'O' && board[0][2] == 'O' && board[1][1] == ' ')
             board[1][1] = 'O';
          else if (board[1][1] == 'O' && board[0][2] == 'O' && board[2][0] == ' ')
             board[2][0] = 'O';
             
          else if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == ' ')//vertically
             board[0][2] = 'O';
          else if (board[0][0] == 'O' && board[0][2] == 'O' && board[0][1] == ' ')
             board[0][1] = 'O';
          else if (board[0][1] == 'O' && board[0][2] == 'O' && board[0][0] == ' ')
             board[0][0] = 'O';
          
          else if (board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == ' ')
             board[1][2] = 'O';
          else if (board[1][0] == 'O' && board[1][2] == 'O' && board[1][1] == ' ')
             board[1][1] = 'O';
          else if (board[1][1] == 'O' && board[1][2] == 'O' && board[1][0] == ' ')
             board[1][0] = 'O';
          
          else if (board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == ' ')
             board[2][2] = 'O';
          else if (board[2][0] == 'O' && board[2][2] == 'O' && board[2][1] == ' ')
             board[2][1] = 'O';
          else if (board[2][1] == 'O' && board[2][2] == 'O' && board[2][0] == ' ')
             board[2][0] = 'O';
          
          else if (board[1][1] == ' ')//searches empty spaces
             board[1][1] = 'O';
          else if (board[1][0] == ' ')
             board[1][0] = 'O';
          else if (board[2][0] == ' ')
             board[2][0] = 'O';
          else if (board[2][1] == ' ')
             board[2][1] = 'O';
          else if (board[2][2] == ' ')
             board[2][2] = 'O';
          else if (board[1][2] == ' ')
             board[1][2] = 'O';
          else if (board[0][2] == ' ')
             board[0][1] = 'O';
          else if (board[0][1] == ' ')
             board[0][1] = 'O';
          else if (board[0][0] == ' ')
             board[0][0] = 'O';
    }
    
    int game::checkwin(){
          if ((board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')||
              (board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O')||
              (board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O')||
              (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')||
              (board[2][0] == 'O' && board[1][1] == 'O' && board[0][2] == 'O')||
              (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')||
              (board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')||
              (board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')){
              gotoxy(1,1);
              cout<<"The computer won";
              gotoxy(1,2);
              cout<<"Play again? (y/n)";
          }
          else if ((board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')||
                   (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')||
                   (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')||
                   (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')||
                   (board[2][0] == 'X' && board[1][1] == 'X' && board[0][2] == 'X')||
                   (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')||
                   (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')||
                   (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')){
              gotoxy(1,1);
              cout<<"You won!";
              gotoxy(1,2);
              cout<<"Play again? (y/n)";
          }
    }
    
    int play(){
        no_cursor();
        int play = true;
    
        while (play){
              
              //----------------------------------------------------------------------------------------------------
              //---------- draw -------------------------------------------------------------------------------START
              //----------------------------------------------------------------------------------------------------
              draw();
              gotoxy(1,1);
              cout<<turn;
              pause(1000);
              
              //----------------------------------------------------------------------------------------------------
              //---------- p1 move ----------------------------------------------------------------------------START
              //----------------------------------------------------------------------------------------------------
              while (turn == PLAYER){
                    p1move();
              }
              gotoxy(1,1);
              cout<<turn;
              pause(1000);
              draw();
              checkwin();
              //----------------------------------------------------------------------------------------------------
              //---------- comp move --------------------------------------------------------------------------START
              //----------------------------------------------------------------------------------------------------
              if (turn == COMPUTER){
                  compmove();
                  turn = PLAYER;
              }
              gotoxy(1,1);
              cout<<turn;
              pause(1000);
              //----------------------------------------------------------------------------------------------------
              //---------- wins/loss --------------------------------------------------------------------------START
              //----------------------------------------------------------------------------------------------------
              checkwin();
              gotoxy(1,1);
              cout<<turn;
              pause(1000);
        }
    }

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Hi there, i too am working on a TTT game. If i had to guess where your error is its probably nested in those If statements in the parentheses. Either that or maybe a variable is not initialized, it sounds like one of those kinds of problems. I got the same thing happening to me. Let me just ask a quick question. How does your AI work or did you choose not to implement any sort of AI? Well good luck with your program, im busy searching for ways to implement a minimax tree in my code. Hope you are more successful than me

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    Posting tons of source saying this doesn't work is not a good way to become well-liked around here.

    1. Post code that pertains.
    2. State your problem.
    3. State possible solutions you've tried.

    We will not parse your code for you looking for something that 'doesn't work'

    I'm making a tic tac toe game but it's not working properly. Sometimes it work properly for a while but then becomes unresponsive.
    Vague.


    Oh yeah my MFC editor doesn't reset the data correctly on a File->Close so when you restart or open a new project from within one, it crashes. Can you help?

    See....you don't know the answer to that and your question has just about the same amount of meaning to us.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making Tic Tac Toe Smarter
    By abh!shek in forum C Programming
    Replies: 15
    Last Post: 06-05-2008, 10:43 AM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Tic Tac Toe... so close...
    By SlayerBlade in forum C Programming
    Replies: 14
    Last Post: 10-10-2005, 08:58 PM
  4. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM
  5. Tic - Tac - Toe...
    By TheUnknowingOne in forum Game Programming
    Replies: 1
    Last Post: 09-10-2002, 06:01 AM

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