// TicTac AI @cid "YGtA"; @name "TicTac AI"; @dbname "TicTac AI"; // ---------- Variables ---------- int Turn, MoveN, Lvl; string temp; int Arr[ 9 ]; pointer Brd[ 3 ]; int OffSet; // ---------- Functions ---------- Reset(); Won(); Full(); DblTrp(); AI(); #include "TicTacAILib.txt" main(){ int X, Y; // Initialize for( X = 0; X < 3; X++ ) Brd[ X ] = Arr + 3*X ; textattr( 2, 1, 2 ); textalign( 11 ); title( "Tic Tac Toe" ); do{ clear(); puts( "Select level:\n1 - Easy\n2 - Medium\n3 - Hard\n4 - Level Discription" ); Lvl = (int)getc() - 48; if( Lvl == 4 ){ clear(); puts( LvlDisc ); event( 1 ); } }while( Lvl < 1 || Lvl > 3 ); graph_on(); do{ Reset(); do{ if( (OffSet + MoveN)%2 ){ waitp(); X = (int)(penx() / 53); Y = (int)(peny() / 53); } else{ temp = AI(); X = strleft( temp, 1 ); Y = strright( temp, 1 ); } if( Brd[X][Y] ) alert( "Spot's taken!" ); else{ Brd[X][Y] = Turn; if( Turn == 1 ) rect( 1, X*53+8, Y*53+8, X*53+45, Y*53+45, 0 ); else frame( 1, X*53+8, Y*53+8, X*53+45, Y*53+45, 18 ); Turn = (-1) * Turn; MoveN++; } }while( !Won() && !Full() ); if( Won() ){ if( -Turn == 1 ) text( 80, 80, " Box won! " ); else text( 80, 80, " Circle won! " ); }else text( 80, 80, " No winners " ); event( 1 ); event( 1 ); }while( confirm( "Another game?" ) ); } // ---------- Resets ---------- Reset(){ int X, Y; Turn = 1; MoveN = 1; clearg(); rect( 0, 0, 0, 180, 180, 0 ); for( X = 0; X <= 160; X = X + 53 ) line( 1, X, 0, X, 180 ); for( Y = 0; Y <= 160; Y = Y+ 53 ) line( 1, 0, Y, 180, Y ); OffSet = confirm( "Should Palm go first?" ); for( Y = 0; Y < 3; Y++ ) for( X = 0; X < 3; X++ ) Brd[ X ][ Y ] = 0; } // ---------- Check if won ---------- Won(){ int i, j, k; for( i = 0; i < 3; i++ ){ j = Brd[i][0] + Brd[i][1] + Brd[i][2]; k = Brd[0][i] + Brd[1][i] + Brd[2][i]; if( k == -3 * Turn || j == -3 * Turn ) return 1; } j = Brd[0][0] + Brd[1][1] + Brd[2][2]; k = Brd[0][2] + Brd[1][1] + Brd[2][0]; if( k == -3*Turn || j == -3*Turn ) return 1; return 0; } // ---------- Check if full ---------- Full(){ int X, Y; for( X = 0; X < 3; X++) for( Y = 0; Y < 3; Y++) if( Brd[X][Y] == 0 ) return 0; return 1; }