Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $board = "V I F I | F 0 0 F | S 0 0 F | P F F F";
- $beginning = "3 4";
- $moves = "2 4 3 11 11 3 7 8";
- /*$board = $_GET['board'];
- $beginning = $_GET['beginning'];
- $moves = $_GET['moves'];*/
- $board = preg_split("/[\|\s]+/", $board);
- $moves = explode(" ", $moves);
- $totalInns = 0;
- $field = array();
- $counter = 0;
- for($i = 0; $i < 4 ;$i++) {
- $field[$i] = array();
- for($j = 0; $j < 4 ;$j++) {
- $field[$i][$j] = $board[$counter++];
- if($field[$i][$j] == "I") {
- $totalInns++;
- }
- }
- }
- $coins = 50;
- $inns = 0;
- $mover = 0;
- $startRow = $beginning[0] - 1;
- $startCol = $beginning[2] - 1;
- function EvaluatePosition($row, $col) {
- $positionValue = $GLOBALS['field'][$row][$col];
- if($positionValue == "P") {
- $GLOBALS['coins'] -= 5;
- } elseif($positionValue == "I") {
- if($GLOBALS['coins'] > 100) {
- $GLOBALS['inns']++;
- $GLOBALS['coins'] -= 100;
- } else {
- $GLOBALS['coins'] -= 10;
- }
- } elseif($positionValue == "F") {
- $GLOBALS['coins'] += 20;
- } elseif($positionValue == "S") {
- //call the remove rows function and remove the next two rows in the array
- skipMoves();
- } elseif($positionValue == "V") {
- $GLOBALS['coins'] *= 10;
- } elseif($positionValue == "N") {
- echo "<p>You won! Nakov's force was with you!<p>";
- die();
- }
- }
- function skipMoves() {
- $currentMoveIdx = $GLOBALS['mover'];
- if($currentMoveIdx < count($GLOBALS['moves']) - 2) {
- unset($GLOBALS['moves'][++$currentMoveIdx]);
- unset($GLOBALS['moves'][++$currentMoveIdx]);
- $length = count($GLOBALS['moves']);
- $keys = range(0, $length-1);
- $GLOBALS['moves'] = array_combine($keys, $GLOBALS['moves']);
- } else {
- echo "<p>You lost! No more moves! You have ". $GLOBALS['coins'] . " coins!<p>";
- die();
- }
- }
- function getDirection($currRow, $currCol) {
- $direction = "";
- if($currCol == 0 && $currRow != 0) {
- $direction = "up";
- } elseif($currRow == 0 && $currCol != 3) {
- $direction = "right";
- } elseif($currCol == 3 && $currRow != 3) {
- $direction = "down";
- } elseif($currRow == 3 && $currCol != 0) {
- $direction = "left";
- }
- return $direction;
- }
- function nextMove($moveSteps, &$currRow, &$currCol) {
- $temp = $moveSteps;
- $direction = getDirection($currRow, $currCol);
- while($temp > 0) {
- if($direction == "up") {
- $currRow--;
- } elseif($direction == "right") {
- $currCol++;
- } elseif($direction == "down") {
- $currRow++;
- } elseif($direction == "left") {
- $currCol--;
- }
- $direction = getDirection($currRow, $currCol);
- $temp--;
- }
- }
- function gameEnder() {
- if($GLOBALS['inns'] == $GLOBALS['totalInns']) {
- //end game because all inns are MINE
- echo "<p>You won! You own the village now! You have " . $GLOBALS['coins'] ." coins!<p>";
- die();
- }
- if($GLOBALS['coins'] <= 0) {
- //end game because of no money
- echo "<p>You lost! You ran out of money!<p>";
- // die();
- }
- }
- for(; $mover < count($moves) ;$mover++) {
- $coins += $inns * 20;
- nextMove($moves[$mover], $startRow, $startCol);
- EvaluatePosition($startRow, $startCol);
- gameEnder();
- }
- echo "<p>You lost! No more moves! You have " . $coins . " coins!<p>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement