Advertisement
austinh115

[PHP] Connect4 AI [OLD]

Jul 11th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.73 KB | None | 0 0
  1. class Connect4 {
  2.     public $red = 1;
  3.     public $yellow = 2;
  4.     public $outside = 3;
  5.     public $empty = 0;
  6.     public $field = array();
  7.     public $height = array();
  8.  
  9.     public function __construct() {
  10.         $this->field  = array(array(0,0,0,0,0,0),array(0,0,0,0,0,0),array(0,0,0,0,0,0),array(0,0,0,0,0,0),array(0,0,0,0,0,0),array(0,0,0,0,0,0),array(0,0,0,0,0,0));
  11.         $this->height = array(5,5,5,5,5,5,5);
  12.     }
  13.  
  14.     public function get($column, $row) {
  15.         if (($column < 0) || ($column > 6) || ($row < 0) || ($row > 5)) {
  16.             return $this->outside;
  17.         } else {
  18.             return $this->field[$column][$row];
  19.         }
  20.     }
  21.  
  22.     public $won=false;
  23.  
  24.     public function set($column) {
  25.         if(($column < 0) || ($column > 6) || ($this->height[$column] < 0) || ($this->height[$column] > 5)) {
  26.             return 666;
  27.         } else {
  28.             $this->field[$column][$this->height[$column]] = $this->red;
  29.             $this->height[$column] = $this->height[$column] - 1;
  30.             if ($this->check($column,$this->height[$column]+1,4,$this->red,false) == true) {
  31.                 $this->won=true;
  32.                 return 1000;
  33.             }
  34.             if (($this->height[0] == -1) && ($this->height[1] == -1) && ($this->height[2] == -1) && ($this->height[3] == -1) && ($this->height[4] == -1)  && ($this->height[5] == -1) && ($this->height[6] == -1)) {
  35.                 return 50;
  36.             }
  37.             if ($this->won != true) return $this->computer();      
  38.         }
  39.     }
  40.  
  41.     public function check($x,$y,$quantity,$color,$check_bei_2) {
  42.         $yes = false;
  43.         if ($color == $this->red) {$color2 = $this->yellow;} else {$color2 = $this->red;}
  44.         for ($k=0;$k<=3;$k++) {
  45.             $sum1 = $sum2 = $sum3 = $sum4 = $sum12 = $sum22 = $sum32 = $sum42 = 0;
  46.             for($j=0;$j<=3;$j++) {
  47.                 if ($this->get($x-$k+$j,$y) == $color) {$sum1++;}
  48.                 if ($this->get($x,$y-$k+$j) == $color) {$sum2++;}
  49.                 if ($this->get($x-$k+$j,$y-$k+$j) == $color) {$sum3++;}
  50.                 if ($this->get($x+$k-$j,$y-$k+$j) == $color) {$sum4++;}
  51.                 if ($this->get($x-$k+$j,$y) == $color2) {$sum12++;}
  52.                 if ($this->get($x,$y-$k+$j) == $color2) {$sum22++;}
  53.                 if ($this->get($x-$k+$j,$y-$k+$j) == $color2) {$sum32++;}
  54.                 if ($this->get($x+$k-$j,$y-$k+$j) == $color2) {$sum42++;}
  55.                 if ($this->get($x-$k+$j,$y) == $this->outside) {$sum12++;}
  56.                 if ($this->get($x,$y-$k+$j) == $this->outside) {$sum22++;}
  57.                 if ($this->get($x-$k+$j,$y-$k+$j) == $this->outside) {$sum32++;}
  58.                 if ($this->get($x+$k-$j,$y-$k+$j) == $this->outside) {$sum42++;}
  59.             }
  60.             if (($sum1 >= $quantity) && ($sum12 == 0)) {$yes = true;} else
  61.             if (($sum2 >= $quantity) && ($sum22 == 0)) {$yes = true;} else
  62.             if (($sum3 >= $quantity) && ($sum32 == 0)) {$yes = true;} else
  63.             if (($sum4 >= $quantity) && ($sum42 == 0)) $yes = true;//changed this to make it smaller could be the reason
  64.             if (($yes == true) && ($check_bei_2 == true)) {
  65.                 $sum12 = $sum22 = $sum32 = $sum42 = 0;
  66.                 $this->field[$x][$y] = $color;
  67.                 $this->height[$x]--;
  68.                 for($j=0;$j<=3;$j++) {
  69.                     if (($sum1 >= $quantity) && ($this->get($x-$k+$j,$y) == $this->empty) && ($this->get($x-$k+$j,$this->height[$x-$k+$j]+1) == $this->empty)) $sum12++;
  70.                     if (($sum2 >= $quantity) && ($this->get($x,$y-$k+$j) == $this->empty) && ($this->get($x,$this->height[$x]+1) == $this->empty)) $sum22++;
  71.                     if (($sum3 >= $quantity) && ($this->get($x-$k+$j,$y-$k+$j) == $this->empty) && ($this->get($x-$k+$j,$this->height[$x-$k+$j]+1) == $this->empty)) $sum32++;
  72.                     if (($sum4 >= $quantity) && ($this->get($x+$k-$j,$y-$k+$j) == $this->empty) && ($this->get($x+$k-$j,$this->height[$x+$k-$j]+1) == $this->empty)) $sum42++;
  73.                 }
  74.                 if (($sum12 == 1) || ($sum22 == 1) || ($sum32 == 1) || ($sum42 == 1)) $yes = false;
  75.                 $this->height[$x]++;
  76.                 $this->field[$x][$y] = $this->empty;
  77.             }
  78.         }
  79.         return $yes;
  80.     }
  81.    
  82.     public function random() {
  83.         return (mt_rand() / mt_getrandmax());
  84.     }
  85.  
  86.     public function computer() {
  87.         $chance = array(0,0,0,0,0,0,0);
  88.         $chance[0] = 13+$this->random()*4;
  89.         $chance[1] = 13+$this->random()*4;
  90.         $chance[2] = 16+$this->random()*4;
  91.         $chance[3] = 16+$this->random()*4;
  92.         $chance[4] = 16+$this->random()*4;
  93.         $chance[5] = 13+$this->random()*4;
  94.         $chance[6] = 13+$this->random()*4;
  95.         for ($i=0;$i<=6;$i++) if ($this->height[$i] < 0) $chance[$i] = $chance[$i]-30000;
  96.         for ($i=0;$i<=6;$i++) {
  97.             if ($this->check($i,$this->height[$i],3,$this->yellow,false) == true) $chance[$i] = $chance[$i] + 20000;
  98.             if ($this->check($i,$this->height[$i],3,$this->red,false) == true) $chance[$i] = $chance[$i] + 10000;
  99.             if ($this->check($i,$this->height[$i]-1,3,$this->red,false) == true) $chance[$i] = $chance[$i] -4000;
  100.             if ($this->check($i,$this->height[$i]-1,3,$this->yellow,false) == true) $chance[$i] = $chance[$i] -200;
  101.             if ($this->check($i,$this->height[$i],2,$this->red,false) == true) $chance[$i] = $chance[$i] +50+$this->random()*3;
  102.             if (($this->check($i,$this->height[$i],2,$this->yellow,true) == true) && ($this->height[$i] > 0)) {
  103.                 $this->field[$i][$this->height[$i]] = $this->yellow;
  104.                 $this->height[$i]--;
  105.                 $count = 0;
  106.                 for($j=0;$j<=6;$j++) if($this->check($j,$this->height[$j],3,$this->yellow,false) == true) $count++;
  107.                 if ($count == 0) {$chance[$i] = $chance[$i]+60+$this->random()*2;} else {$chance[$i] = $chance[$i] - 60;}
  108.                 $this->height[$i]++;
  109.                 $this->field[$i][$this->height[$i]] = $this->empty;
  110.             }
  111.             if ($this->check($i,$this->height[$i]-1,2,$this->red,false) == true) $chance[$i] = $chance[$i] -10;
  112.             if ($this->check($i,$this->height[$i]-1,2,$this->yellow,false) == true) $chance[$i] = $chance[$i] -8;
  113.             if ($this->check($i,$this->height[$i],1,$this->red,false) == true) $chance[$i] = $chance[$i] +5+$this->random()*2;
  114.             if ($this->check($i,$this->height[$i],1,$this->yellow,false) == true) $chance[$i] = $chance[$i] +5+$this->random()*2;
  115.             if ($this->check($i,$this->height[$i]-1,1,$this->red,false) == true) $chance[$i] = $chance[$i] -2;
  116.             if ($this->check($i,$this->height[$i]-1,1,$this->yellow,false) == true) $chance[$i] = $chance[$i] +1;
  117.             if (($this->check($i,$this->height[$i],2,$this->yellow,true) == true) && ($this->height[$i] > 0)) {
  118.                 $this->field[$i][$this->height[$i]] = $this->yellow;
  119.                 $this->height[$i]--;
  120.                 for($k=0;$k<=6;$k++)      
  121.                     if (($this->check($k,$this->height[$k],3,$this->yellow,false) == true) && ($this->height[$k] > 0)) {
  122.                         $this->field[$k][$this->height[$k]] = $this->red;
  123.                         $this->height[$k]--;
  124.                         for($j=0;$j<=6;$j++)
  125.                             if ($this->check($j,$this->height[$j],3,$this->yellow,false) == true) $chance[$i] = $chance[$i] + 2000;
  126.                         $this->height[$k]++;
  127.                         $this->field[$k][$this->height[$k]] = $this->empty;
  128.                     }
  129.                 $this->height[$i]++;
  130.                 $this->field[$i][$this->height[$i]] = $this->empty;
  131.             }
  132.             if (($this->check($i,$this->height[$i],2,$this->red,true) == true) && ($this->height[$i] > 0)) {
  133.                 $this->field[$i][$this->height[$i]] = $this->red;
  134.                 $this->height[$i]--;
  135.                 for($k=0;$k<=6;$k++)
  136.                     if (($this->check($k,$this->height[$k],3,$this->red,false) == true) && ($this->height[$k] > 0)) {
  137.                         $this->field[$k][$this->height[$k]] = $this->yellow;
  138.                         $this->height[$k]--;
  139.                         for($j=0;$j<=6;$j++)
  140.                             if ($this->check($j,$this->height[$j],3,$this->red,false) == true) $chance[$i] = $chance[$i] + 1000;
  141.                         $this->height[$k]++;
  142.                         $this->field[$k][$this->height[$k]] = $this->empty;
  143.                     }
  144.                 $this->height[$i]++;
  145.                 $this->field[$i][$this->height[$i]] = $this->empty;
  146.             }
  147.             if (($this->check($i,$this->height[$i]-1,2,$this->red,true) == true) && ($this->height[$i] > 1)) {
  148.                 $this->field[$i][$this->height[$i]] = $this->red;
  149.                 $this->height[$i]--;
  150.                 for($k=0;$k<=6;$k++)
  151.                     if (($this->check($k,$this->height[$k]-1,3,$this->red,false) == true) && ($this->height[$k] > 0)) {
  152.                         $this->field[$k][$this->height[$k]] = $this->yellow;
  153.                         $this->height[$k]--;
  154.                         for($j=0;$j<=6;$j++)
  155.                             if ($this->check($j,$this->height[$j]-1,3,$this->red,false) == true) $chance[$i] = $chance[$i] - 500;
  156.                         $this->height[$k]++;
  157.                         $this->field[$k][$this->height[$k]] = $this->empty;
  158.                     }
  159.                 $this->height[$i]++;
  160.                 $this->field[$i][$this->height[$i]] = $this->empty;
  161.             }
  162.         }
  163.         $column = 0;
  164.         $x = -10000;
  165.         for($i=0;$i<=6;$i++)
  166.             if ($chance[$i] > $x) {
  167.                 $x = $chance[$i];
  168.                 $column = $i;
  169.             }
  170.         if(($column < 0) || ($column > 6) || ($this->height[$column] < 0) || ($this->height[$column] > 5)) {
  171.             echo "\nBot tried to cheat\n";
  172.             return $this->computer();
  173.         }
  174.         $this->field[$column][$this->height[$column]] = $this->yellow;
  175.         $this->height[$column] = $this->height[$column] - 1;
  176.         if ($this->check($column,$this->height[$column]+1,4,$this->yellow,false) == true) {
  177.             return array(-1000, $column);
  178.         }
  179.         if (($this->height[0] == -1) && ($this->height[1] == -1) && ($this->height[2] == -1) && ($this->height[3] == -1) && ($this->height[4] == -1)  && ($this->height[5] == -1) && ($this->height[6] == -1)) {
  180.             return array(51, $column);
  181.         }
  182.         return $column;
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement