Advertisement
Guest User

Untitled

a guest
Oct 11th, 2012
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.79 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Запускать с ключами командной строки:
  4.  * 1 - размер поля (поле квадратное)
  5.  * 2 - количество фигур в линии, которых достаточно для выигрыша
  6.  *
  7.  * В рантайме принимает команды:
  8.  * X,Y (два числа через запятую) - координаты, считая с нуля
  9.  * see - посмотреть поле
  10.  */
  11.  
  12.  
  13.  
  14. $dimension = $argv[1];
  15. $to_win = $argv[2];
  16. define('MY', 'o');
  17. define('HIS', 'x');
  18. define('LINE', $to_win);
  19. define('DIM', $dimension);
  20.  
  21. $field = array();
  22. for ($i = 0; $i < $dimension; $i++) {
  23.     $field[$i] = array();
  24.     $field[$i] = array_fill(0, $dimension, '-');
  25. }
  26.  
  27. $f = fopen('php://stdin', 'r');
  28.  
  29. while ($line = fgets($f)) {
  30.     list($x, $y) = explode(',', trim($line));
  31.     if (isset($x) && isset($y)) {
  32.         $field[$x][$y] = HIS;
  33.         ai($field);
  34.     }elseif(trim($line) == 'see'){
  35.         echoField($field);
  36.     }
  37. }
  38.  
  39. function echoField(&$field) {
  40.     foreach ($field as $row) {
  41.         foreach ($row as $value) {
  42.             echo $value . "\t";
  43.         }
  44.         echo "\n";
  45.     }
  46. }
  47.  
  48. /**
  49.  * ai =)
  50.  */
  51.  
  52. function ai(&$field) {
  53.     for ($i = 0; $i < DIM; $i++) {
  54.         for ($j = 0; $j < DIM; $j++) {
  55.             if ($field[$i][$j] == '-') {
  56.                 $empty[] = array($i, $j);
  57.             }
  58.             $threat = checkLine($i, $j, LINE - 2, 0, $field);
  59.             if ($threat && $threat[0] + 1 < DIM) {
  60.                 makeTurn($threat[0] + 1, $threat[1], $field);
  61.                 return;
  62.             }
  63.             $threat = checkLine($i, $j, 0, LINE - 2, $field);
  64.             if ($threat && $threat[1] + 1 < DIM) {
  65.                 makeTurn($threat[0], $threat[1] + 1, $field);
  66.                 return;
  67.             }
  68.             $threat = checkLine($x, $y, LINE - 2, LINE - 2, $field);
  69.             if ($threat && $threat[0] + 1 < DIM && $threat[1] + 1 < DIM) {
  70.                 makeTurn($threat[0] + 1, $threat[1] + 1, $field);
  71.                 return;
  72.             }
  73.         }
  74.     }
  75.     $xy = array_rand($empty);
  76.     makeTurn($empty[$xy][0], $empty[$xy][1], $field);
  77. }
  78.  
  79. function makeTurn($x, $y, &$field) {
  80.     $field[$x][$y] = MY;
  81.     echo "my turn: " . ($x) . ',' . ($y) . "\n";
  82. }
  83.  
  84. function checkLine($x, $y, $r, $d, &$field) {
  85.     $what_to_check = false;
  86.     if ($field[$x][$y] == '-' || $field[$x][$y] == NULL) {
  87.         return false;
  88.     } else {
  89.         $what_to_check = $field[$x][$y];
  90.     }
  91.     for ($i = $x; $i <= $x + $r; $i++) {
  92.         for ($j = $y; $j <= $y + $d; $j++) {
  93.             //echo "$i, $j \n";
  94.             if ($field[$i][$j] !== $what_to_check) {
  95.                 return false;
  96.             }
  97.         }
  98.     }
  99.     return array($i - 1, $j - 1);
  100. }
  101. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement