Advertisement
Valleri

IT village

Sep 1st, 2014
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.48 KB | None | 0 0
  1. <?php
  2. $board = "V I F I | F 0 0 F | S 0 0 F | P F F F";
  3. $beginning = "3 4";
  4. $moves = "2 4 3 11 11 3 7 8";
  5.  
  6. /*$board = $_GET['board'];
  7. $beginning = $_GET['beginning'];
  8. $moves = $_GET['moves'];*/
  9.  
  10. $board = preg_split("/[\|\s]+/", $board);
  11. $moves = explode(" ", $moves);
  12. $totalInns = 0;
  13. $field = array();
  14. $counter = 0;
  15. for($i = 0; $i < 4 ;$i++) {
  16.     $field[$i] = array();
  17.     for($j = 0; $j < 4 ;$j++) {
  18.         $field[$i][$j] = $board[$counter++];
  19.         if($field[$i][$j] == "I") {
  20.             $totalInns++;
  21.         }
  22.     }
  23. }
  24.  
  25. $coins = 50;
  26. $inns = 0;
  27. $mover = 0;
  28. $startRow = $beginning[0] - 1;
  29. $startCol = $beginning[2] - 1;
  30.  
  31. function EvaluatePosition($row, $col) {
  32.     $positionValue = $GLOBALS['field'][$row][$col];
  33.     if($positionValue == "P") {
  34.         $GLOBALS['coins'] -= 5;
  35.     } elseif($positionValue == "I") {
  36.         if($GLOBALS['coins'] > 100) {
  37.             $GLOBALS['inns']++;
  38.             $GLOBALS['coins'] -= 100;
  39.         } else {
  40.             $GLOBALS['coins'] -= 10;
  41.         }
  42.     } elseif($positionValue == "F") {
  43.         $GLOBALS['coins'] += 20;
  44.     } elseif($positionValue == "S") {
  45.         //call the remove rows function and remove the next two rows in the array
  46.         skipMoves();
  47.     } elseif($positionValue == "V") {
  48.         $GLOBALS['coins'] *= 10;
  49.     } elseif($positionValue == "N") {
  50.         echo "<p>You won! Nakov's force was with you!<p>";
  51.         die();
  52.     }
  53. }
  54.  
  55. function skipMoves() {
  56.     $currentMoveIdx = $GLOBALS['mover'];
  57.     if($currentMoveIdx < count($GLOBALS['moves']) - 2) {
  58.         unset($GLOBALS['moves'][++$currentMoveIdx]);
  59.         unset($GLOBALS['moves'][++$currentMoveIdx]);
  60.  
  61.         $length = count($GLOBALS['moves']);
  62.         $keys = range(0, $length-1);
  63.         $GLOBALS['moves'] = array_combine($keys, $GLOBALS['moves']);
  64.     } else {
  65.         echo "<p>You lost! No more moves! You have ". $GLOBALS['coins'] . " coins!<p>";
  66.         die();
  67.     }
  68. }
  69.  
  70. function getDirection($currRow, $currCol) {
  71.     $direction = "";
  72.     if($currCol == 0 && $currRow != 0) {
  73.         $direction = "up";
  74.     } elseif($currRow == 0 && $currCol != 3) {
  75.         $direction = "right";
  76.     } elseif($currCol == 3 && $currRow != 3) {
  77.         $direction = "down";
  78.     } elseif($currRow == 3 && $currCol != 0) {
  79.         $direction = "left";
  80.     }
  81.     return $direction;
  82. }
  83. function nextMove($moveSteps, &$currRow, &$currCol) {
  84.     $temp = $moveSteps;
  85.     $direction = getDirection($currRow, $currCol);
  86.  
  87.     while($temp > 0) {
  88.         if($direction == "up") {
  89.             $currRow--;
  90.         } elseif($direction == "right") {
  91.             $currCol++;
  92.         } elseif($direction == "down") {
  93.             $currRow++;
  94.         } elseif($direction == "left") {
  95.             $currCol--;
  96.         }
  97.  
  98.         $direction = getDirection($currRow, $currCol);
  99.         $temp--;
  100.     }
  101. }
  102.  
  103. function gameEnder() {
  104.     if($GLOBALS['inns'] == $GLOBALS['totalInns']) {
  105.         //end game because all inns are MINE
  106.         echo "<p>You won! You own the village now! You have " . $GLOBALS['coins'] ." coins!<p>";
  107.         die();
  108.     }
  109.     if($GLOBALS['coins'] <= 0) {
  110.         //end game because of no money
  111.         echo "<p>You lost! You ran out of money!<p>";
  112.        // die();
  113.     }
  114. }
  115.  
  116. for(; $mover < count($moves) ;$mover++) {
  117.     $coins += $inns * 20;
  118.     nextMove($moves[$mover], $startRow, $startCol);
  119.     EvaluatePosition($startRow, $startCol);
  120.     gameEnder();
  121. }
  122.  
  123. echo "<p>You lost! No more moves! You have " . $coins . " coins!<p>";
  124.  
  125. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement