Guest User

TicTacToe

a guest
Jun 4th, 2011
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 15.05 KB | None | 0 0
  1. #include <a_samp>
  2.  
  3. #define kmd(%1) if(!strcmp(%1,cmd,true))
  4. #define if_%1() if(!strcmp(%1,cmd,true))
  5.  
  6. #define CheckStarted(); \
  7.     if(started == J)\
  8.     {\
  9.         print("The game is already runing, type \"exitgame\" to start a new game later");\
  10.         return 1;\
  11.     }
  12.  
  13. #define Z        (0)
  14. #define J        (1)
  15.  
  16. #define X        (11)
  17. #define O        (12)
  18.  
  19. #define N        (-1)
  20.  
  21. #define easy     (0)
  22. #define normal   (1)
  23. #define hard     (2)
  24.  
  25. #define D        (10)
  26.  
  27. #define s_edge   (0)
  28. #define s_center (1)
  29. #define s_corner (2)
  30.  
  31. new
  32.      started
  33.     ,movecount = 0
  34.     ,enemymovecount = 0
  35.     ,diff
  36.     ,strategy = N
  37.     ,side
  38.     ,block[9]
  39.     ,win
  40.     ,ai
  41.     ,winblock[8][3] =
  42.     {
  43.         {0,1,2},
  44.         {3,4,5},
  45.         {6,7,8},
  46.         {0,3,6},
  47.         {1,4,7},
  48.         {2,5,8},
  49.         {2,4,6},
  50.         {0,4,8}
  51.     }
  52. ;
  53.  
  54. public OnFilterScriptInit()
  55. {
  56.     print("Welcome to PAWN-tic-tac-toe");
  57.     print("Choose your side:(type X or O [and all other commands] in console without the \"/\" character)");
  58.    
  59.     print("+------------+-----------+");
  60.     print("|     X      |     O     |");
  61.     print("+-------+----+---+-------+");
  62.     print("|   XX  |   OO   |  XX   |");
  63.     print("|   XX  |   OO   |  XX   |");
  64.     print("+-------+--------+-------+");
  65.     print("|   OO  |   XX   |  OO   |");
  66.     print("|   OO  |   XX   |  OO   |");
  67.     print("+-------+--------+-------+");
  68.     print("|   XX  |   OO   |  XX   |");
  69.     print("|   XX  |   OO   |  XX   |");
  70.     print("+-------+--------+-------+");
  71.    
  72.     started   = Z;
  73.     diff      = normal;
  74.    
  75.     block[0]  = N;
  76.     block[1]  = N;
  77.     block[2]  = N;
  78.     block[3]  = N;
  79.     block[4]  = N;
  80.     block[5]  = N;
  81.     block[6]  = N;
  82.     block[7]  = N;
  83.     block[8]  = N;
  84.    
  85.     win       = N;
  86.     ai        = J;
  87.     side      = X;
  88.     strategy  = N;
  89.    
  90.     movecount = 0;
  91.     enemymovecount = 0;
  92.    
  93.     return 1;
  94. }
  95.  
  96. forward Reset();
  97. public Reset()
  98. {
  99.     print("Welcome to PAWN-tic-tac-toe");
  100.     print("Choose your side:(type X or O [and all other commands] in console without the \"/\" character)");
  101.  
  102.     print("+------------+-----------+");
  103.     print("|     X      |     O     |");
  104.     print("+-------+----+---+-------+");
  105.     print("|   XX  |   OO   |  XX   |");
  106.     print("|   XX  |   OO   |  XX   |");
  107.     print("+-------+--------+-------+");
  108.     print("|   OO  |   XX   |  OO   |");
  109.     print("|   OO  |   XX   |  OO   |");
  110.     print("+-------+--------+-------+");
  111.     print("|   XX  |   OO   |  XX   |");
  112.     print("|   XX  |   OO   |  XX   |");
  113.     print("+-------+--------+-------+");
  114.  
  115.     started   = Z;
  116.     diff      = normal;
  117.  
  118.     block[0]  = N;
  119.     block[1]  = N;
  120.     block[2]  = N;
  121.     block[3]  = N;
  122.     block[4]  = N;
  123.     block[5]  = N;
  124.     block[6]  = N;
  125.     block[7]  = N;
  126.     block[8]  = N;
  127.  
  128.     win       = N;
  129.     ai        = J;
  130.     side      = X;
  131.     strategy  = N;
  132.    
  133.     movecount = 0;
  134.     enemymovecount = 0;
  135.  
  136.     return 1;
  137. }
  138.  
  139. public OnRconCommand(cmd[])
  140. {
  141.  
  142.     kmd("human")
  143.     {
  144.         CheckStarted();
  145.  
  146.         ai = Z;
  147.         side = X;
  148.         print("You have chosen the Human VS Human Mode, type \"startgame\" to start the game");
  149.         return 1;
  150.     }
  151.    
  152.     kmd("ai")
  153.     {
  154.         CheckStarted();
  155.  
  156.         ai = J;
  157.         print("You have chosen the Human VS AI Mode, type \"startgame\" to start the game");
  158.         return 1;
  159.     }
  160.    
  161.     kmd("X")
  162.     {
  163.         CheckStarted();
  164.  
  165.         side = X;
  166.         print("You have chosen \"X\" as your side, type \"startgame\" to start the game");
  167.         return 1;
  168.     }
  169.    
  170.     kmd("O")
  171.     {
  172.         CheckStarted();
  173.  
  174.         side = O;
  175.         print("You have chosen \"O\" as your side, type \"startgame\" to start the game");
  176.         print("WARNING: Chosing O as first side enables Human VS AI mode! Human VS Human disabled.");
  177.         ai = J;
  178.         return 1;
  179.     }
  180.    
  181.     kmd("easy")
  182.     {
  183.         CheckStarted();
  184.  
  185.         diff = easy;
  186.         print("You have chosen \"EASY\" , type \"startgame\" to start the game");
  187.         return 1;
  188.     }
  189.    
  190.     kmd("normal")
  191.     {
  192.         CheckStarted();
  193.  
  194.         diff = normal;
  195.         print("You have chosen \"NORMAL\" , type \"startgame\" to start the game");
  196.         return 1;
  197.     }
  198.    
  199.     kmd("hard")
  200.     {
  201.         CheckStarted();
  202.        
  203.         diff = hard;
  204.         print("You have chosen \"HARD\" , type \"startgame\" to start the game");
  205.         return 1;
  206.     }
  207.    
  208.     kmd("startgame")
  209.     {
  210.         if(side == N)return print("Choose a side first! (\"X\" or \"O\")");
  211.  
  212.         CheckStarted();
  213.  
  214.         started = J;
  215.        
  216.         if(side == X)
  217.         {
  218.             DrawBoard();
  219.             print("Choose a number to place your X");
  220.         }
  221.         else
  222.         if(side == O)
  223.         {
  224.             PlaceEnemy();
  225.            
  226.             print("Choose a number to place your O");
  227.         }
  228.         return 1;
  229.     }
  230.    
  231.     kmd("exitgame")
  232.     {
  233.         Reset();
  234.         return 1;
  235.     }
  236.    
  237.     for(new i; i < 9; i++)
  238.     {
  239.         new command[256];
  240.         format(command,256,"%d",i+1);
  241.         kmd(command)
  242.         {
  243.             if(started == Z)
  244.             {
  245.                 print("The game is not running, type \"startgame\" to start a new game");
  246.                 return 1;
  247.             }
  248.            
  249.             PlaceMe(i);
  250.             return 1;
  251.         }
  252.     }
  253.    
  254.     return 1;
  255. }
  256.  
  257. forward PlaceMe(field);
  258. public PlaceMe(field)
  259. {
  260.     if(block[field] != (N))return print("The chosen field is already taken, choose another one");
  261.    
  262.     if(side == X)block[field] = X;
  263.     else
  264.     if(side == O)block[field] = O;
  265.    
  266.     printf("You have chosen \"%d\" , object has been placed",field+1);
  267.  
  268.     movecount++;
  269.    
  270.     DrawBoard();
  271.    
  272.     win = CheckWinner();
  273.    
  274.     if(win == X)
  275.     {
  276.         print("End of game, resetting all... Please start a new game :)");
  277.         print("---X--- HAS WON THE GAME!! :)");
  278.         Reset();
  279.     }
  280.     else
  281.     if(win == O)
  282.     {
  283.         print("End of game, resetting all... Please start a new game :)");
  284.         print("---O--- HAS WON THE GAME!! :)");
  285.         Reset();
  286.     }
  287.     else
  288.     if(win == D)
  289.     {
  290.         print("End of game, resetting all... Please start a new game :)");
  291.         print("Nobody has won... :P");
  292.         Reset();
  293.     }
  294.     else
  295.     {
  296.         if(ai == J)
  297.         {
  298.             PlaceEnemy();
  299.         }
  300.         else
  301.         {
  302.             print("Next side turn, please choose a number");
  303.             if(side == X)
  304.             {
  305.                 side = O;
  306.                 return O;
  307.             }
  308.             else
  309.             {
  310.                 side = X;
  311.                 return X;
  312.             }
  313.         }
  314.     }
  315.     return 5;
  316. }
  317.  
  318. forward PlaceEnemy();
  319. public PlaceEnemy()
  320. {
  321.     enemymovecount++;
  322.     print("Enemy turn...");
  323.     if(side == X)
  324.     {
  325.         if(diff == easy)
  326.         {
  327.             EasyPlace(O);
  328.         }
  329.         else if(diff == normal)
  330.         {
  331.             NormalPlace(O);
  332.         }
  333.         else if(diff == hard)
  334.         {
  335.             HardPlace(O);
  336.         }
  337.     }
  338.     else
  339.     if(side == O)
  340.     {
  341.         if(diff == easy)
  342.         {
  343.             EasyPlace(X);
  344.         }
  345.         else if(diff == normal)
  346.         {
  347.             NormalPlace(X);
  348.         }
  349.         else if(diff == hard)
  350.         {
  351.             HardPlace(X);
  352.         }
  353.     }
  354.    
  355.     DrawBoard();
  356.     win = CheckWinner();
  357.  
  358.     if(win == X)
  359.     {
  360.         print("End of game, resetting all... Please start a new game :)");
  361.         print("---X--- HAS WON THE GAME!! :)");
  362.         Reset();
  363.     }
  364.     else
  365.     if(win == O)
  366.     {
  367.         print("End of game, resetting all... Please start a new game :)");
  368.         print("---O--- HAS WON THE GAME!! :)");
  369.         Reset();
  370.     }
  371.     else
  372.     if(win == D)
  373.     {
  374.         print("End of game, resetting all... Please start a new game :)");
  375.         print("Nobody has won... :P");
  376.         Reset();
  377.     }
  378.    
  379.     return 1;
  380. }
  381.  
  382. forward DrawBoard();
  383. public DrawBoard()
  384. {
  385.     print("+-------+");
  386.  
  387.     new
  388.          string[3][10]
  389.         ,_char
  390.         ,pos
  391.         ,index
  392.         ;
  393.  
  394.     format(string[0],10,"| 1 2 3 |");
  395.     format(string[1],10,"| 4 5 6 |");
  396.     format(string[2],10,"| 7 8 9 |");
  397.  
  398.     for(new d; d < 3; d++)
  399.     {
  400.         for(new i; i < 3; i++)
  401.         {
  402.             index = i+(d*3);
  403.             pos = ((i*2)+2);
  404.             if(block[index] == X)
  405.             {
  406.                 _char = 'X';
  407.                 string[d][pos] = _char;
  408.             }
  409.             else if(block[index] == O)
  410.             {
  411.                 _char = 'O';
  412.                 string[d][pos] = _char;
  413.             }
  414.         }
  415.         print(string[d]);
  416.     }
  417.    
  418.     print("+-------+");
  419. }
  420.  
  421. forward CheckWinner();
  422. public CheckWinner()
  423. {
  424.     for(new i; i < 8; i++)
  425.     {
  426.         if(block[(winblock[i][0])] == X && block[(winblock[i][1])] == X && block[(winblock[i][2])] == X)return X;
  427.         if(block[(winblock[i][0])] == O && block[(winblock[i][1])] == O && block[(winblock[i][2])] == O)return O;
  428.     }
  429.  
  430.     if(
  431.         (block[0] != (N)) &&
  432.         (block[1] != (N)) &&
  433.         (block[2] != (N)) &&
  434.         (block[3] != (N)) &&
  435.         (block[4] != (N)) &&
  436.         (block[5] != (N)) &&
  437.         (block[6] != (N)) &&
  438.         (block[7] != (N)) &&
  439.         (block[8] != (N))
  440.     )
  441.     return D;
  442.  
  443.     return N;
  444. }
  445.  
  446. forward IsFieldClear();
  447. public IsFieldClear()
  448. {
  449.     for(new i; i < 9; i++)if(block[i] != N)return 0;
  450.     return 1;
  451. }
  452.  
  453. forward EasyPlace(param);
  454. public EasyPlace(param)//theoretically possible infite loop so watch out.
  455. {
  456.     new rand;
  457.     begin:
  458.     rand = random(9);
  459.     if(block[rand] == N)block[rand] = param;else
  460.     goto begin;//because of this
  461. }
  462.  
  463. forward NormalPlace(param);
  464. public NormalPlace(param)
  465. {
  466.     new tryblock;
  467.     if(param == X)
  468.     {
  469.         tryblock = EstimateWinblock(X);
  470.  
  471.         if(tryblock != N && block[tryblock] == N)
  472.         {
  473.             block[tryblock] = X;
  474.         }
  475.         else
  476.         {
  477.             tryblock = EstimateWinblock(O);
  478.             if(tryblock != N && block[tryblock] == N)
  479.             {
  480.                 block[tryblock] = X;
  481.             }
  482.             else
  483.             {
  484.                 EasyPlace(X);
  485.             }
  486.         }
  487.     }
  488.     else if(param == O)
  489.     {
  490.         tryblock = EstimateWinblock(O);
  491.  
  492.         if(tryblock != N && block[tryblock] == N)
  493.         {
  494.             block[tryblock] = O;
  495.         }
  496.         else
  497.         {
  498.             tryblock = EstimateWinblock(X);
  499.             if(tryblock != N && block[tryblock] == N)
  500.             {
  501.                 block[tryblock] = O;
  502.             }
  503.             else
  504.             {
  505.                 EasyPlace(O);
  506.             }
  507.         }
  508.     }
  509. }
  510.  
  511. forward IsEdge(_block);
  512. public IsEdge(_block)
  513. {
  514.     if(_block == 1 || _block == 3 || _block == 5 || _block == 7)return J;
  515.     return Z;
  516. }
  517.  
  518. forward IsCorner(_block);
  519. public IsCorner(_block)
  520. {
  521.     if(_block == 0 || _block == 2 || _block == 6 || _block == 8)return J;
  522.     return Z;
  523. }
  524.  
  525. forward IsCenter(_block);
  526. public IsCenter(_block)
  527. {
  528.     if(_block == 4)return J;
  529.     return Z;
  530. }
  531.  
  532. forward TotalMoves();
  533. public TotalMoves()
  534. {
  535.     return movecount+enemymovecount;
  536. }
  537.  
  538. forward BlockCount(param);
  539. public BlockCount(param)
  540. {
  541.     new count = 0;
  542.     for(new i; i < 9; i++)if(block[i] == param)count++;
  543.     return count;
  544. }
  545.  
  546. forward GetOnlyBlock(param);
  547. public GetOnlyBlock(param)
  548. {
  549.     for(new i; i < 9; i++)if(block[i] == param)return i;
  550.     return N;
  551. }
  552.  
  553. forward HardPlace(param);
  554. public HardPlace(param)
  555. {
  556.     if(param == X)
  557.     {
  558.         if(TotalMoves() == J)
  559.         {
  560.             block[4] = X;
  561.         }
  562.         else
  563.         {
  564.             if(BlockCount(O) == J && BlockCount(X) == J && strategy == N)
  565.             {
  566.                 new id = GetOnlyBlock(O);
  567.                 if(id != N)
  568.                 {
  569.                     if(IsEdge(id) == J)
  570.                     {
  571.                         new rand = random(2);
  572.                         if(id == 1)
  573.                         {
  574.                             if(rand == 0)block[6] = X;else
  575.                             block[8] = X;
  576.                         }
  577.                         else
  578.                         if(id == 3)
  579.                         {
  580.                             if(rand == 0)block[8] = X;else
  581.                             block[2] = X;
  582.                         }
  583.                         else
  584.                         if(id == 5)
  585.                         {
  586.                             if(rand == 0)block[0] = X;else
  587.                             block[6] = X;
  588.                         }
  589.                         else
  590.                         if(id == 7)
  591.                         {
  592.                             if(rand == 0)block[2] = X;else
  593.                             block[0] = X;
  594.                         }
  595.                         strategy = s_edge;
  596.                     }
  597.                     else
  598.                     {
  599.                         new tryblock = EstimateLine_corner();
  600.                         block[tryblock] = X;
  601.                         strategy = s_corner;
  602.                     }
  603.                 }
  604.             }
  605.             else if(strategy == s_edge)
  606.             {
  607.                 new tryblock = EstimateWinblock(X);
  608.  
  609.                 if(tryblock != N && block[tryblock] == N)
  610.                 {
  611.                     block[tryblock] = X;
  612.                 }
  613.                 else
  614.                 {
  615.                     tryblock = EstimateWinblock(O);
  616.                     if(tryblock != N && block[tryblock] == N)
  617.                     {
  618.                         block[tryblock] = X;
  619.                     }
  620.                     else
  621.                     {
  622.                         EasyPlace(X);
  623.                     }
  624.                 }
  625.             }
  626.             else if(strategy == s_corner)
  627.             {
  628.                 new tryblock = EstimateWinblock(X);
  629.                 if(tryblock != N && block[tryblock] == N)
  630.                 {
  631.                     block[tryblock] = X;
  632.                 }
  633.                 else
  634.                 {
  635.                     tryblock = EstimateWinblock(O);
  636.                     if(tryblock != N && block[tryblock] == N)
  637.                     {
  638.                         block[tryblock] = X;
  639.                     }
  640.                     else
  641.                     {
  642.                         EasyPlace(X);
  643.                     }
  644.                 }
  645.             }
  646.         }
  647.     }
  648.     else if(param == O)
  649.     {
  650.         if(TotalMoves() == 2)
  651.         {
  652.             new id = GetOnlyBlock(X);
  653.             if(id != N)
  654.             {
  655.                 if(IsCenter(id) == J)strategy = s_center;else
  656.                 if(IsEdge(id) == J)strategy = s_edge;else
  657.                 strategy = s_corner;
  658.             }
  659.             if(strategy == s_center)
  660.             {
  661.                 block[6] = O;
  662.             }
  663.             else if(strategy == s_edge)
  664.             {
  665.                 block[4] = O;
  666.             }
  667.             else if(strategy == s_corner)
  668.             {
  669.                 block[4] = O;
  670.             }
  671.         }
  672.         else
  673.         {
  674.             if(strategy == s_center)
  675.             {
  676.                 new tryblock = EstimateWinblock(O);
  677.                 if(tryblock != N && block[tryblock] == N)
  678.                 {
  679.                     block[tryblock] = O;
  680.                 }
  681.                 else
  682.                 {
  683.                     tryblock = EstimateWinblock(X);
  684.                     if(tryblock != N && block[tryblock] == N)
  685.                     {
  686.                         block[tryblock] = O;
  687.                     }
  688.                     else
  689.                     {
  690.                         EasyPlace(O);
  691.                     }
  692.                 }
  693.             }
  694.             else if(strategy == s_edge)
  695.             {
  696.                 new tryblock = EstimateWinblock(O);
  697.                 if(tryblock != N && block[tryblock] == N)
  698.                 {
  699.                     block[tryblock] = O;
  700.                 }
  701.                 else
  702.                 {
  703.                     tryblock = EstimateWinblock(X);
  704.                     if(tryblock != N && block[tryblock] == N)
  705.                     {
  706.                         block[tryblock] = O;
  707.                     }
  708.                     else
  709.                     {
  710.                         EasyPlace(O);
  711.                     }
  712.                 }
  713.             }
  714.             else if(strategy == s_corner)
  715.             {
  716.                 new tryblock = EstimateWinblock(O);
  717.                 if(tryblock != N && block[tryblock] == N)
  718.                 {
  719.                     block[tryblock] = O;
  720.                 }
  721.                 else
  722.                 {
  723.                     tryblock = EstimateWinblock(X);
  724.                     if(tryblock != N && block[tryblock] == N)
  725.                     {
  726.                         block[tryblock] = O;
  727.                     }
  728.                     else
  729.                     {
  730.                         EasyPlace(O);
  731.                     }
  732.                 }
  733.             }
  734.         }
  735.     }
  736.     return J;
  737. }
  738.  
  739. forward EstimateWinblock(type);
  740. public EstimateWinblock(type)
  741. {
  742.     new r = N;
  743.     for(new i; i < 8; i++)
  744.     {
  745.         if(block[(winblock[i][0])] == type && block[(winblock[i][1])] == type)
  746.             r = winblock[i][2];
  747.         if(r != N)if(block[r] == N)return r;
  748.         if(block[(winblock[i][0])] == type && block[(winblock[i][2])] == type)
  749.             r = winblock[i][1];
  750.         if(r != N)if(block[r] == N)return r;
  751.         if(block[(winblock[i][1])] == type && block[(winblock[i][2])] == type)
  752.             r = winblock[i][0];
  753.         if(r != N)if(block[r] == N)return r;
  754.     }
  755.     return r;
  756. }
  757.  
  758. forward EstimateLine();
  759. public EstimateLine()
  760. {
  761.     new r = N;
  762.     for(new i; i < 8; i++)
  763.     {
  764.         if(block[(winblock[i][0])] == X && block[(winblock[i][1])] == O)
  765.             r = winblock[i][2];
  766.         if(block[(winblock[i][0])] == O && block[(winblock[i][1])] == X)
  767.             r = winblock[i][2];
  768.         if(block[(winblock[i][0])] == X && block[(winblock[i][2])] == O)
  769.             r = winblock[i][1];
  770.         if(block[(winblock[i][0])] == O && block[(winblock[i][2])] == X)
  771.             r = winblock[i][1];
  772.         if(block[(winblock[i][1])] == X && block[(winblock[i][2])] == O)
  773.             r = winblock[i][0];
  774.         if(block[(winblock[i][1])] == O && block[(winblock[i][2])] == X)
  775.             r = winblock[i][0];
  776.     }
  777.     return r;
  778. }
  779.  
  780. forward EstimateLine_corner();
  781. public EstimateLine_corner()
  782. {
  783.     new r = N;
  784.     if(block[4] == X)
  785.     {
  786.         if(block[0] == O)r = 8;else
  787.         if(block[2] == O)r = 6;else
  788.         if(block[6] == O)r = 2;else
  789.         if(block[8] == O)r = 0;
  790.     }
  791.     else
  792.     if(block[4] == O)
  793.     {
  794.         if(block[0] == X)r = 8;else
  795.         if(block[2] == X)r = 6;else
  796.         if(block[6] == X)r = 2;else
  797.         if(block[8] == X)r = 0;
  798.     }
  799.     return r;
  800. }
Advertisement
Add Comment
Please, Sign In to add comment