Thread: TicTacToe game doesn't work

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102

    Unhappy TicTacToe game doesn't work

    I'm making a tictactoe game, user interface works, but cpu player can't put in O's. The code is crappy, but take a look only at the ending, where cpu has to put in the answer. Who can help?



    Code:
    //MADE BY MAXTO
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    const int LOW = 1;
    const int HIGH = 9;
    char a='a',b='b',c='c',d='d',e='e',f='f',g='g',h='h',i='i';
    char location;
    char location1;
    int cpu_line;
    /*Map viewing*/
    
    inline void showmap()
    {
    cout<<"Field map:\n";
    cout<<"  "<<a<<" | "<<b<<"  |  "<<c<<" "<<endl;
    cout<<"____|____|____"<<endl;
    cout<<"  "<<d<<" | "<<e<<"  | "<<f<<"  "<<endl;
    cout<<"____|____|____"<<endl;
    cout<<"  "<<g<<" | "<<h<<"  | "<<i<<"  "<<endl;
    cout<<"    |    |    \n";
    }
    
    
    
    
    
    
    /*CPU AI*/
    inline void cgennum(){
        int cpu_line;
        time_t seconds;
        time(&seconds);
        srand((unsigned int)seconds);
        cpu_line = rand() % ( HIGH - LOW + 1) + LOW;
    }
    
    int main()
    
    
    
    {   
    
        
        
        /*tic tac toe game field*/
    /*
    cout<<"  a | b  | c
    cout<<"____|____|____"<<endl;
    cout<<"  d | e  | f  "<<endl;
    cout<<"____|____|____"<<endl;
    cout<<"  g | h  | i  "<<endl;
    cout<<"    |    |    "<<endl;
    */
    
    
    
    cout<<"    |    |    "<<endl;         
    cout<<"____|____|____"<<endl;
    cout<<"    |    |    "<<endl;
    cout<<"____|____|____"<<endl;
    cout<<"    |    |    "<<endl;
    cout<<"    |    |    "<<endl;
    cout<<endl;
    showmap();
    cin>>location;
    cin.ignore();
    //========location>>"X";===========
    switch (location){
    case 'a' : a = 'X' ;
    break;
    case 'b' : b = 'X' ;
    break;
    case 'c' : c = 'X' ;
    break;
    case 'd' : d = 'X' ;
    break;
    case 'e' : e = 'X' ;
    break;
    case 'f' : f = 'X' ;
    break;
    case 'g' : g = 'X' ;
    break;
    case 'h' : h = 'X' ;
    break;
    case 'i' : i = 'X' ;
    break;
    default:cout<<"---\n" ;
    }
    showmap();
    cin.get();
    cgennum();
    
    /*AI coding try 1*/
        int cpu_line;
        time_t seconds;
        time(&seconds);
        srand((unsigned int)seconds);
        cpu_line = rand() % ( HIGH - LOW + 1) + LOW;
        
        switch (location1){
    case 'a' : a = 'O' ;
    break;
    case 'b' : b = 'O' ;
    break;
    case 'c' : c = 'O' ;
    break;
    case 'd' : d = 'O' ;
    break;
    case 'e' : e = 'O' ;
    break;
    case 'f' : f = 'O' ;
    break;
    case 'g' : g = 'O' ;
    break;
    case 'h' : h = 'O' ;
    break;
    case 'i' : i = 'O' ;
    break;
    
    if (cpu_line == 1){
                 while (a == 'X') {cgennum();}
                 while (a == 'O') {cgennum();}
                 if ( a != 'O' && 'X'){
                      a = 'O'; 
                 }
                 }             
    else if (cpu_line == 2){
                 if (b == 'X') {cgennum();}
                 while (b == 'X') {cgennum();}
                 while (b == 'O') {cgennum();}
                 if ( b != 'O' && 'X'){
                      b = 'O';
                 }
                 }
    else if (cpu_line == 3){
                 if (c == 'X') {cgennum();}
                 while (c == 'X') {cgennum();}
                 while (c == 'O') {cgennum();}
                 if ( c != 'O' && 'X'){
                      c = 'O';
                 }
                 }
    else if (cpu_line == 4){
                 if (d == 'X') {cgennum();}
                 while (d == 'X') {cgennum();}
                 while (d == 'O') {cgennum();}
                 if ( d != 'O' && 'X'){
                      d = 'O';
                 }
                 }
    else if (cpu_line == 5){
                 if (e == 'X') {cgennum();}
                 while (e == 'X') {cgennum();}
                 while (e == 'O') {cgennum();}
                 if ( e != 'O' && 'X'){
                      e = 'O';
                 }
                 }
    else if (cpu_line == 6){
                 if (f == 'X') {cgennum();}
                 while (f == 'X') {cgennum();}
                 while (f == 'O') {cgennum();}
                 if ( f != 'O' && 'X'){
                      f = 'O';
                 }
                 }
    else if (cpu_line == 7){
                 if (g == 'X') {cgennum();}
                 while (g == 'X') {cgennum();}
                 while (g == 'O') {cgennum();}
                 if ( g != 'O' && 'X'){
                      g = 'O';
                 }
                 }
    else if (cpu_line == 8){
                 if (h == 'X') {cgennum();}
                 while (h == 'X') {cgennum();}
                 while (h == 'O') {cgennum();}
                 if ( h != 'O' && 'X'){
                      h = 'O';
                 }
                 }
    else if (cpu_line == 9){
                 if (i == 'X') {cgennum();}
                 while (i == 'X') {cgennum();}
                 while (i == 'O') {cgennum();}
                 if ( i != 'O' && 'X'){
                      i = 'O';
                 }
                 }
    }
    cout<<"Field map:\n";
    cout<<"  "<<a<<" | "<<b<<"  |  "<<c<<" "<<endl;
    cout<<"____|____|____"<<endl;
    cout<<"  "<<d<<" | "<<e<<"  | "<<f<<"  "<<endl;
    cout<<"____|____|____"<<endl;
    cout<<"  "<<g<<" | "<<h<<"  | "<<i<<"  "<<endl;
    cout<<"    |    |    \n";
    cin.get();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > The code is crappy
    Thanks for letting us know, and yes, it surely is pretty crappy.

    Two consequences of this
    1. It means you're careless, don't give a rats ass about your readers, and generally makes it hard for you to debug anyway.
    2. It puts off everyone else from even bothering to look any deeper than, "oh my god, what a fsking mess"

  3. #3
    Dragoon Lover wyvern's Avatar
    Join Date
    Jul 2005
    Location
    dragooncity
    Posts
    28
    soo complicated stuff?

    u could do this just like this

    Code:
    string map[3][3]
    int main()
    {
    
    
          while(1)
    {
    
    cout<<"THYE WHOLE MAP";
    string x;
    x = toupper(getch());
    system("cls");
    
    if( x == "A")
    {
           map[0][0] = "X";
    }
    else if //// BLABLABLA
    
    // NOW FOR THE CPU
    
    //MAKE RANDOM BLABLA ( the clock_t stuff )
    
    int x = rand()% 9+1;
    
    if ( x == 1 )
    {
    map[0][0];
    }
    else if ( x == 2 )
    {
    map[0][1]
    }
    //......
    }
    }
    try this
    http://img76.imageshack.us/img76/1808/expl7pb.png

    AN EXPLOSION DOESNT HAPPEN CUZ GOD WANTS, IT HAPPENS WHEN A SOMETHING "EXPLODES

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  2. Need book to program game into multiplayer...
    By edomingox in forum Game Programming
    Replies: 3
    Last Post: 10-02-2008, 09:26 AM
  3. i need this for my tictactoe game. pls help.
    By riel in forum C Programming
    Replies: 1
    Last Post: 01-20-2008, 01:31 AM
  4. Commerical MMORPG Developer Opp
    By Th3Guy in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 01-22-2007, 11:28 AM
  5. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM