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(); }



LinkBack URL
About LinkBacks


