jQuery vs JavaScript Performance Comparison

JavaScript performance comparison

Revision 68 of this test case created by Halning

Preparation code

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="reset.css">
        <link type="text/css" rel="stylesheet" href="StyleSheet1.css">
        <script type="text/javascript" src="js/libs/jquery/jquery.js"></script>
        <script type="text/javascript" src="js/libs/jqueryui/jquery-ui.js"></script>
        <script type="text/javascript" src="indexjs.js"></script>
        <title></title>
    </head>
    <body class="bodyall">


        <div class="main">
            <button class="mave" ></button>
            <div id="sides">
                <button class="side"></button>
                <button class="side"></button>
                <button class="side"></button>
                <p><button class="side"></button>
                    <button class="side"></button>
                    <button class="side"></button></p>
                <p><button class="side"></button>
                    <button class="side"></button>
                    <button class="side"></button></p>
            </div>




            <pre>
                <?php
                session_start();



                //error_reporting(-1);
                mb_internal_encoding('utf-8');

//+-------------------------------------------------------------------------1---
//                              Parent class Animals
//------------------------------------------------------------------------------

                class Animals {

                    protected $x;
                    protected $y;
                    protected $look;
                    protected $lookMap;
                    protected $seeMouseField;

                }

//+-------------------------------------------------------------------------2---
//                              Class Cat
//------------------------------------------------------------------------------

                class Cat extends Animals {

                    public $look = "C ";
                    public $lookMap = "<div id='cat' class='cats'>    </div>";

                    function __construct($x, $y) {
                        $this->x = $x;
                        $this->y = $y;
                    }

//----------------------------Method Cat sees map-------------------------2.1---

                    public function seeCat($moveCat, $sleep) {

                        $mouseN = 0;
                        $selectionMouseSee = 1;
                        $catClose = array("x" => array(), "y" => array());
                        $vectors = 0;
                        $mouseHwoCatch = array("vectors" => 100000, "x" => 0, "y" => 0, "look" => "");

                        if ($this->look == "P " || $sleep == TRUE) {
                            $this->look = "@ ";
                            $this->lookMap = "<div class='catEat'>    </div>";
                            return 0;
                        } elseif ($this->look == "@ ") {
                            $this->look = "C ";
                            $this->lookMap = "<div id='cat' class='cats'>    </div>";
                        }

//-----------------------------------------------2.1.1-cat sees mouse and choses

                        foreach ($moveCat as $value) {

                            if ($this->x == $value->x && $this->y == $value->y) {
                                continue;
                            }

                            $pMouseX = ($this->x - $value->x);
                            ($pMouseX < 0) ? ($pMouseX *= -1) : ('');
                            $pMouseY = $this->y - $value->y;
                            ($pMouseY < 0) ? ($pMouseY *= -1) : ('');
                            $vectors = max($pMouseX, $pMouseY);

                            if (($value->look !== "C " && $value->look !== "@ " && $value->look !== "P ") && ($vectors < $mouseHwoCatch[vectors])) {

                                $mouseHwoCatch[vectors] = $vectors;
                                $mouseHwoCatch[x] = $value->x;
                                $mouseHwoCatch[y] = $value->y;
                                $mouseHwoCatch[look] = $value->look;
                            }
                        }

//---------------------2.1.2-cat sees how many mouses close with selection Mouse

                        foreach ($moveCat as $value) {

                            if ($this->x == $value->x && $this->y == $value->y) {
                                continue;
                            }

                            $x = $mouseHwoCatch[x] - $value->x;
                            $y = $mouseHwoCatch[y] - $value->y;

                            if (($value->look !== "C ") && ($value->look !== "@ ") && ($value->look !== "P ") && $x <= $selectionMouseSee && $x >= $selectionMouseSee * -1 && $y <= $selectionMouseSee && $y >= $selectionMouseSee * -1) {

                                $mouseN ++;
                            }
//------------------------------------------2.1.3-cat sees other cats on his way

                            $xC = $this->x - $value->x;
                            $yC = $this->y - $value->y;

                            if (($value->look == "C " || $value->look == "@ " || $value->look == "P ") && $xC <= $selectionMouseSee && $xC >= $selectionMouseSee * -1 && $yC <= $selectionMouseSee && $yC >= $selectionMouseSee * -1) {
                                $catClose[x][] = $value->x;
                                $catClose[y][] = $value->y;
                            }
                        }

//----------------------------------------------------2.1.4-Cat chooses the path

                        $x = $this->x - $mouseHwoCatch[x];
                        $y = $this->y - $mouseHwoCatch[y];

                        if ($mouseN >= 3) {
                            return FALSE;
                        } else {
                            if ($y > 0 && $x == 0) {
                                $rA = array("left" => '');
                            } elseif ($y == 0 && $x < 0) {
                                $rA = array("up" => '');
                            } elseif ($y < 0 && $x == 0) {
                                $rA = array("right" => '');
                            } elseif ($y == 0 && $x > 0) {
                                $rA = array("down" => '');
                            } elseif ($y > 0 && $x < 0) {
                                $rA = array("up-left" => '');
                            } elseif ($y < 0 && $x < 0) {
                                $rA = array("up-right" => '');
                            } elseif ($y < 0 && $x > 0) {
                                $rA = array("down-right" => '');
                            } elseif ($y > 0 && $x > 0) {
                                $rA = array("down-left" => '');
                            }
                        }

//----------------------------------------------------2.1.5-other cat on the way

                        $rAkey = array_rand($rA);

                        foreach ($catClose as $k => $value) {
                            foreach ($value as $key => $val) {
                                $catClose[$k][$key] = $this->$k - $val;
                            }
                        }

                        $rAr0 = array();
                        $rAr1 = array();
                        $rAr2 = array();

                        for ($g = 0; $g < count($catClose[x]); $g++) {

                            $j = FALSE;

                            if ($rAkey == "left" && $catClose[y][$g] > 0 && $catClose[x][$g] == 0) {
                                $rArCat = array("down-left" => '', "up-left" => '');
                                $j = TRUE;
                            } elseif ($rAkey == "up" && $catClose[y][$g] == 0 && $catClose[x][$g] < 0) {
                                $rArCat = array("up-right" => '', "up-left" => '');
                                $j = TRUE;
                            } elseif ($rAkey == "right" && $catClose[y][$g] < 0 && $catClose[x][$g] == 0) {
                                $rArCat = array("up-right" => '', "down-right" => '');
                                $j = TRUE;
                            } elseif ($rAkey == "down" && $catClose[y][$g] == 0 && $catClose[x][$g] > 0) {
                                $rArCat = array("down-left" => '', "down-right" => '');
                                $j = TRUE;
                            } elseif ($rAkey == "up-left" && $catClose[y][$g] > 0 && $catClose[x][$g] < 0) {
                                $rArCat = array("up" => '', "left" => '');
                                $j = TRUE;
                            } elseif ($rAkey == "up-right" && $catClose[y][$g] < 0 && $catClose[x][$g] < 0) {
                                $rArCat = array("up" => '', "right" => '');
                                $j = TRUE;
                            } elseif ($rAkey == "down-right" && $catClose[y][$g] < 0 && $catClose[x][$g] > 0) {
                                $rArCat = array("down" => '', "right" => '');
                                $j = TRUE;
                            } elseif ($rAkey == "down-left" && $catClose[y][$g] > 0 && $catClose[x][$g] > 0) {
                                $rArCat = array("down" => '', "left" => '');
                                $j = TRUE;
                            }

                            if ($g == 0) {
                                $rAr0 = $rArCat;
                            } elseif ($g == 1) {
                                $rAr1 = $rArCat;
                            } if ($g == 2) {
                                $rAr2 = $rArCat;
                            }
                        }

                        if ($j == TRUE) {

                            $rA = array_diff_key($rAr0, $rAr1, $rAr2);
                        }

                        return $this->goCat($rA, $mouseHwoCatch);
                    }

//----------------------------Method cat goes-----------------------------2.2---

                    private function goCat($moveCat, $mouseHwoCatch) {

                        $move = array_rand($moveCat);

                        if ($move == "up") {
                            $this->x ++;
                        } elseif ($move == "up-right") {
                            $this->x ++;
                            $this->y ++;
                        } elseif ($move == "right") {
                            $this->y ++;
                        } elseif ($move == "down-right") {
                            $this->x --;
                            $this->y ++;
                        } elseif ($move == "down") {
                            $this->x --;
                        } elseif ($move == "down-left") {
                            $this->x --;
                            $this->y --;
                        } elseif ($move == "left") {
                            $this->y --;
                        } elseif ($move == "up-left") {
                            $this->x ++;
                            $this->y --;
                        }

//-------------------------------------------------------------2.2.1-catch Mouse

                        if ($mouseHwoCatch[x] == $this->x && $mouseHwoCatch[y] == $this->y) {
                            $this->look = "P ";
                            $this->lookMap = "<div class='catEat'>    </div>";
                            echo"\nМышку съели!!!";
                            return $mouseHwoCatch[look];
                        }
                    }

                }

//+-------------------------------------------------------------------------3---
//                              Class Mouse
//+-----------------------------------------------------------------------------

                class Mouse extends Animals {

                    public $lookMap = "<div class='mouse'>    </div>";

                    function __construct($x, $y, $look, $seeMouseField) {
                        $this->x = $x;
                        $this->y = $y;
                        $this->look = $look;
                        $this->seeMouseField = $seeMouseField;
                    }

//+--------------------------- Method mouse sees--------------------------3.1---

                    public function seeMouse($mouseCatArray, $seeMouseField, $border) {

//+---------------------------------------------------------3.1.1-mouse sees cat

                        $catInField = 0;
                        $randomArray = array("up" => '', "right" => '', "down" => '', "left" => '');

                        foreach ($mouseCatArray as $value) {

                            if ($this->x == $value->x && $this->y == $value->y) {
                                continue;
                            }


                            $seeMouse = ($seeMouseField - 1) / 2;

                            $xS = $this->x - $value->x;
                            $yS = $this->y - $value->y;

                            if (($value->look == "C " || $value->look == "@ " || $value->look == "P ") && $xS <= $seeMouse && $xS >= -$seeMouse && $yS <= $seeMouse && $yS >= -$seeMouse) {
                                $catInField ++;
                                $x = $xS;
                                $y = $yS;
                            }

//+---------------------------------3.1.2-mouse sees mouses and chooses the path

                            if ($value->look != "C " && $yS == 1 && $xS == 0) {
                                unset($randomArray["left"]);
                            } elseif ($value->look != "C " && $yS == 0 && $xS == -1) {
                                unset($randomArray["up"]);
                            } elseif ($value->look != "C " && $yS == -1 && $xS == 0) {
                                unset($randomArray["right"]);
                            } elseif ($value->look != "C " && $yS == 0 && $xS == 1) {
                                unset($randomArray["down"]);
                            }
                        }

//-------------------------------3.1.3-If a number of cat mouse chooses the path

                        if ($catInField > 1) {
                            return FALSE;
                        } elseif ($catInField == 1) {

                            if ($y > 0 && $x == 0) {
                                unset($randomArray["left"]);
                            } elseif ($y == 0 && $x < 0) {
                                unset($randomArray["up"]);
                            } elseif ($y < 0 && $x == 0) {
                                unset($randomArray["right"]);
                            } elseif ($y == 0 && $x > 0) {
                                unset($randomArray["down"]);
                            } elseif ($y > 0 && $x < 0) {
                                unset($randomArray["up"], $randomArray["left"]);
                            } elseif ($y < 0 && $x < 0) {
                                unset($randomArray["up"], $randomArray["right"]);
                            } elseif ($y < 0 && $x > 0) {
                                unset($randomArray["right"], $randomArray["down"]);
                            } elseif ($y > 0 && $x > 0) {
                                unset($randomArray["left"], $randomArray["down"]);
                            }
                        }

//---------------------------------------------------3.1.4-mouse sees the border

                        if (($this->x + 1) == $border[x]) {
                            unset($randomArray["up"]);
                        } elseif (($this->x - 1) == -1) {
                            unset($randomArray["down"]);
                        } elseif (($this->y + 1) == $border[y]) {
                            unset($randomArray["right"]);
                        } elseif (($this->y - 1) == -1) {
                            unset($randomArray["left"]);
                        }

                        if ($this->x + 1 == $border[x] && $this->y + 1 == $border[y]) {
                            unset($randomArray["right"]);
                        } elseif ($this->x - 1 == -1 && $this->y - 1 == -1) {
                            unset($randomArray["left"]);
                        } elseif ($this->x - 1 == -1 && $this->y + 1 == $border[y]) {
                            unset($randomArray["right"]);
                        } elseif ($this->x + 1 == $border[x] && $this->y - 1 == -1) {
                            unset($randomArray["left"]);
                        }

                        $this->goMouse($randomArray);
                    }

//----------------------------Method go mouse-----------------------------3.2---

                    private function goMouse($moveMouse) {

                        $move = array_rand($moveMouse);

                        if ($move == "up") {
                            $this->x ++;
                        } elseif ($move == "right") {
                            $this->y ++;
                        } elseif ($move == "down") {
                            $this->x --;
                        } elseif ($move == "left") {
                            $this->y --;
                        }
                    }

                }

//+-------------------------------------------------------------------------4---
//                              Class Map
//------------------------------------------------------------------------------

                class Map extends Animals {

                    private $height;
                    private $width;

                    protected function __construct($height, $width) {
                        $this->height = $height;
                        $this->width = $width;
                    }

//+-------------------------------------------------------------1.4.1-output map

                    protected function createMap() {

                        echo "\n\n\n";

                        for ($n = 0; $n < $this->height; $n++) {
                            $valueMap[] = "<div class='mapArray'>    </div>";
                        }
                        for ($i = 0; $i < $this->width; $i++) {
                            $outputMap[] = $valueMap;
                        }

                        $outputMap = array_reverse($outputMap, true);

                        foreach ($this->kor as $key => $value) {
                            foreach ($value as $key1 => $val) {
                                $outputMap[$key][$key1] = $val;
                            }
                        }

                        foreach ($outputMap as $value) {
                            echo "\n";
                            foreach ($value as $val) {
                                echo "{$val}";
                            }
                        }
                        echo "\n";
                        return $border = array("x" => $this->height, "y" => $this->width);
                    }

//+-------------------------------------------------1.4.2-add animals on the map

                    protected function add($animal) {
                        $this->kor[$animal->x][$animal->y] = $animal->lookMap;
                        return $this->kor;
                    }

                }

//--------------------------------------------------------------------------5---
//                          Class Game
//------------------------------------------------------------------------------

                class Game extends Map {

//----------------------------------------------------5.1-add animals on the map

                    static function addOnMapAnimals($mouseCatArray, $map) {
                        foreach ($mouseCatArray as $value) {
                            $map->add($value);
                        }

                        return $map->createMap();
                    }

//-------------------------Method controls the game----------------------5.2---


                    static function startGame($sleepCatMove, $height, $width, $mouseArray, $catArray) {

//------------------------------------------------------5.2.1-starting positions

                        if (!isset($_SESSION['counter'])) {


                            $sleepCat = $sleepCatMove;
                            $map = new Map($height, $width);


                            $mouseCatArray = array_merge($mouseArray, $catArray);

                            $border = Game::addOnMapAnimals($mouseCatArray, $map);

                            $_SESSION['mouseCatArray'] = $mouseCatArray;
                            $_SESSION['border'] = $border;
                            $_SESSION['sleepCat'] = $sleepCat;
                        } else {
                            $map = new Map($height, $width);

                            $mouses = count($mouseArray);
                            $cats = count($catArray);



                            foreach ($mouseArray as $value) {
                                $value->seeMouse($_SESSION['mouseCatArray'], $value->seeMouseField, $_SESSION['border']);
                            }


                            if ($_SESSION['counter'] === $_SESSION['sleepCat']) {
                                $_SESSION['sleepCat'] += $sleepCatMove;
                                $sleep = TRUE;
                            } else {
                                $sleep = False;
                            }


                            foreach ($catArray as $value) {
                                $catchMouseCat[] = $value->seeCat($_SESSION['mouseCatArray'], $sleep);
                            }


                            foreach ($catchMouseCat as $key => $value) {

                                for ($l = 0; $l <= $mouses + 5; $l++) {

                                    settype($value, "integer");

                                    if ($value == $l) {
                                        unset($_SESSION['mouseArray'][$value]);
                                    }
                                }
                            }
                            unset($catchMouseCat);

                            echo "\nХод : {$_SESSION['counter']}\n";
                            echo "Мышук на поле: {$mouses}\n";
                            echo "Кошек на поле: $cats";

                            if (count($_SESSION['mouseCatArray']) == $cats) {
                                echo "\nВсех мышок съели!!!\n";
                                unset($_SESSION); // или $_SESSION = array() для очистки всех данных сессии
                                session_destroy();
                                header('Location: main.php');
                            }

                            $_SESSION['mouseCatArray'] = array_merge($mouseArray, $catArray);

                            Game::addOnMapAnimals($_SESSION['mouseCatArray'], $map);
                        }
                    }

                }

//--------------------------------------------------------------------------6---
//                             Enter all animals
//------------------------------------------------------------------------------

                if (!isset($_SESSION['counter'])) {




                    $catsNum = htmlspecialchars($_POST['cat']);
                    $mouseNum = htmlspecialchars($_POST['mouse']);


                    if ($mouseNum >= 3) {
                        $mouse1 = new Mouse(25, 25, "1 ", 9);                               //enter x,y,look,seeMouseField
                        $mouseArray[1] = $mouse1;

                        $mouse2 = new Mouse(29, 29, "2 ", 9);
                        $mouseArray[2] = $mouse2;

                        $mouse3 = new Mouse(29, 3, "3 ", 9);
                        $mouseArray[3] = $mouse3;
                    }


                    if ($mouseNum >= 4) {
                        $mouse4 = new Mouse(1, 1, "4 ", 9);
                        $mouseArray[4] = $mouse4;
                    }

                    if ($mouseNum >= 5) {
                        $mouse5 = new Mouse(3, 16, "5 ", 9);
                        $mouseArray[5] = $mouse5;
                    }

                    if ($mouseNum >= 6) {
                        $mouse6 = new Mouse(5, 19, "6 ", 9);
                        $mouseArray[6] = $mouse6;
                    }

                    if ($mouseNum >= 7) {
                        $mouse7 = new Mouse(19, 5, "7 ", 9);
                        $mouseArray[7] = $mouse7;
                    }



                    if ($catsNum >= 1) {
                        $cat1 = new Cat(5, 7);                                        //enter x, y,look
                        $catArray[] = $cat1;
                    }

                    if ($catsNum >= 2) {
                        $cat2 = new Cat(29, 6);
                        $catArray[] = $cat2;
                    }

                    if ($catsNum >= 3) {
                        $cat3 = new Cat(23, 0);
                        $catArray[] = $cat3;
                    }

                    if ($catsNum >= 4) {
                        $cat4 = new Cat(15, 15);
                        $catArray[] = $cat4;
                    }

                    $_SESSION['catArray'] = $catArray;
                    $_SESSION['mouseArray'] = $mouseArray;
//------------------------------------------------------------------------
//-----------------------------------------------------------------
//-------------------------------------------------------------------


                    $nameAnon = htmlspecialchars($_POST['name']);


                    echo "\nПривет, {$nameAnon}!\n";


                    $choseAnimal = htmlspecialchars($_POST['animal']);
                    if ($choseAnimal == "cat") {
                        echo "Вы выбрали кошку!\n";

                        $cat1->lookMap = "<div id='playerCat' class='playerAnimal'>    </div>";
                    } elseif ($choseAnimal == "mouse") {
                        echo "Вы выбрали мышку!\n";
                        $mouse1->lookMap = "<div id='playerMouse' class='playerAnimal'>    </div>";
                    } else {
                        echo "Вы не выбрали персонажа, играют только боты!\n";
                    }

                    echo "Начало игры. Все на позиции!!!";
                }


//-------enter ( $sleepCatMove, $widthFild, $heightFild);
                Game::startGame(8, 30, 30, $_SESSION['mouseArray'], $_SESSION['catArray']);

                $_SESSION['counter'] ++;
                $_SESSION['key'] = $_POST['key'];
                echo "{$_SESSION['key']}\n";
                ?>
            </pre>
            <div rel="mouse" class="hover">Это мышь она может ходить только на одну клетку вверх-вниз и влево-вправо и видит область в 9X9 клеток вокруг себя.</div>
            <div rel="cats" class="hover">Кошка она может ходит на одну клетку в любую сторону, видит всю карту и выбирает ближайщую мышку для охоты.</div>
            <div rel="playerCat" class="hover">Кошка которой вы управляете!</div>
            <div rel="playerMouse" class="hover">Мышка которой вы управляете!</div>
            <div rel="catEat" class="hover">Кошка отдыхает!</div>
            <div rel="sides" class="hover">Выбирите куда идти!</div>
             <div rel="mave" class="hover">Следующий ход!</div>

        </div>


    </body>
</html>


    

Preparation code output

<!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" type="text/css" href="reset.css"> <link type="text/css" rel="stylesheet" href="StyleSheet1.css"> <script type="text/javascript" src="js/libs/jqueryui/jquery-ui.js"></script> <script type="text/javascript" src="indexjs.js"></script> <title></title> </head> <body class="bodyall"> <div class="main"> <button class="mave" ></button> <div id="sides"> <button class="side"></button> <button class="side"></button> <button class="side"></button> <p><button class="side"></button> <button class="side"></button> <button class="side"></button></p> <p><button class="side"></button> <button class="side"></button> <button class="side"></button></p> </div> <pre> <?php session_start(); //error_reporting(-1); mb_internal_encoding('utf-8'); //+-------------------------------------------------------------------------1--- // Parent class Animals //------------------------------------------------------------------------------ class Animals { protected $x; protected $y; protected $look; protected $lookMap; protected $seeMouseField; } //+-------------------------------------------------------------------------2--- // Class Cat //------------------------------------------------------------------------------ class Cat extends Animals { public $look = "C "; public $lookMap = "<div id='cat' class='cats'> </div>"; function __construct($x, $y) { $this->x = $x; $this->y = $y; } //----------------------------Method Cat sees map-------------------------2.1--- public function seeCat($moveCat, $sleep) { $mouseN = 0; $selectionMouseSee = 1; $catClose = array("x" => array(), "y" => array()); $vectors = 0; $mouseHwoCatch = array("vectors" => 100000, "x" => 0, "y" => 0, "look" => ""); if ($this->look == "P " || $sleep == TRUE) { $this->look = "@ "; $this->lookMap = "<div class='catEat'> </div>"; return 0; } elseif ($this->look == "@ ") { $this->look = "C "; $this->lookMap = "<div id='cat' class='cats'> </div>"; } //-----------------------------------------------2.1.1-cat sees mouse and choses foreach ($moveCat as $value) { if ($this->x == $value->x && $this->y == $value->y) { continue; } $pMouseX = ($this->x - $value->x); ($pMouseX < 0) ? ($pMouseX *= -1) : (''); $pMouseY = $this->y - $value->y; ($pMouseY < 0) ? ($pMouseY *= -1) : (''); $vectors = max($pMouseX, $pMouseY); if (($value->look !== "C " && $value->look !== "@ " && $value->look !== "P ") && ($vectors < $mouseHwoCatch[vectors])) { $mouseHwoCatch[vectors] = $vectors; $mouseHwoCatch[x] = $value->x; $mouseHwoCatch[y] = $value->y; $mouseHwoCatch[look] = $value->look; } } //---------------------2.1.2-cat sees how many mouses close with selection Mouse foreach ($moveCat as $value) { if ($this->x == $value->x && $this->y == $value->y) { continue; } $x = $mouseHwoCatch[x] - $value->x; $y = $mouseHwoCatch[y] - $value->y; if (($value->look !== "C ") && ($value->look !== "@ ") && ($value->look !== "P ") && $x <= $selectionMouseSee && $x >= $selectionMouseSee * -1 && $y <= $selectionMouseSee && $y >= $selectionMouseSee * -1) { $mouseN ++; } //------------------------------------------2.1.3-cat sees other cats on his way $xC = $this->x - $value->x; $yC = $this->y - $value->y; if (($value->look == "C " || $value->look == "@ " || $value->look == "P ") && $xC <= $selectionMouseSee && $xC >= $selectionMouseSee * -1 && $yC <= $selectionMouseSee && $yC >= $selectionMouseSee * -1) { $catClose[x][] = $value->x; $catClose[y][] = $value->y; } } //----------------------------------------------------2.1.4-Cat chooses the path $x = $this->x - $mouseHwoCatch[x]; $y = $this->y - $mouseHwoCatch[y]; if ($mouseN >= 3) { return FALSE; } else { if ($y > 0 && $x == 0) { $rA = array("left" => ''); } elseif ($y == 0 && $x < 0) { $rA = array("up" => ''); } elseif ($y < 0 && $x == 0) { $rA = array("right" => ''); } elseif ($y == 0 && $x > 0) { $rA = array("down" => ''); } elseif ($y > 0 && $x < 0) { $rA = array("up-left" => ''); } elseif ($y < 0 && $x < 0) { $rA = array("up-right" => ''); } elseif ($y < 0 && $x > 0) { $rA = array("down-right" => ''); } elseif ($y > 0 && $x > 0) { $rA = array("down-left" => ''); } } //----------------------------------------------------2.1.5-other cat on the way $rAkey = array_rand($rA); foreach ($catClose as $k => $value) { foreach ($value as $key => $val) { $catClose[$k][$key] = $this->$k - $val; } } $rAr0 = array(); $rAr1 = array(); $rAr2 = array(); for ($g = 0; $g < count($catClose[x]); $g++) { $j = FALSE; if ($rAkey == "left" && $catClose[y][$g] > 0 && $catClose[x][$g] == 0) { $rArCat = array("down-left" => '', "up-left" => ''); $j = TRUE; } elseif ($rAkey == "up" && $catClose[y][$g] == 0 && $catClose[x][$g] < 0) { $rArCat = array("up-right" => '', "up-left" => ''); $j = TRUE; } elseif ($rAkey == "right" && $catClose[y][$g] < 0 && $catClose[x][$g] == 0) { $rArCat = array("up-right" => '', "down-right" => ''); $j = TRUE; } elseif ($rAkey == "down" && $catClose[y][$g] == 0 && $catClose[x][$g] > 0) { $rArCat = array("down-left" => '', "down-right" => ''); $j = TRUE; } elseif ($rAkey == "up-left" && $catClose[y][$g] > 0 && $catClose[x][$g] < 0) { $rArCat = array("up" => '', "left" => ''); $j = TRUE; } elseif ($rAkey == "up-right" && $catClose[y][$g] < 0 && $catClose[x][$g] < 0) { $rArCat = array("up" => '', "right" => ''); $j = TRUE; } elseif ($rAkey == "down-right" && $catClose[y][$g] < 0 && $catClose[x][$g] > 0) { $rArCat = array("down" => '', "right" => ''); $j = TRUE; } elseif ($rAkey == "down-left" && $catClose[y][$g] > 0 && $catClose[x][$g] > 0) { $rArCat = array("down" => '', "left" => ''); $j = TRUE; } if ($g == 0) { $rAr0 = $rArCat; } elseif ($g == 1) { $rAr1 = $rArCat; } if ($g == 2) { $rAr2 = $rArCat; } } if ($j == TRUE) { $rA = array_diff_key($rAr0, $rAr1, $rAr2); } return $this->goCat($rA, $mouseHwoCatch); } //----------------------------Method cat goes-----------------------------2.2--- private function goCat($moveCat, $mouseHwoCatch) { $move = array_rand($moveCat); if ($move == "up") { $this->x ++; } elseif ($move == "up-right") { $this->x ++; $this->y ++; } elseif ($move == "right") { $this->y ++; } elseif ($move == "down-right") { $this->x --; $this->y ++; } elseif ($move == "down") { $this->x --; } elseif ($move == "down-left") { $this->x --; $this->y --; } elseif ($move == "left") { $this->y --; } elseif ($move == "up-left") { $this->x ++; $this->y --; } //-------------------------------------------------------------2.2.1-catch Mouse if ($mouseHwoCatch[x] == $this->x && $mouseHwoCatch[y] == $this->y) { $this->look = "P "; $this->lookMap = "<div class='catEat'> </div>"; echo"\nМышку съели!!!"; return $mouseHwoCatch[look]; } } } //+-------------------------------------------------------------------------3--- // Class Mouse //+----------------------------------------------------------------------------- class Mouse extends Animals { public $lookMap = "<div class='mouse'> </div>"; function __construct($x, $y, $look, $seeMouseField) { $this->x = $x; $this->y = $y; $this->look = $look; $this->seeMouseField = $seeMouseField; } //+--------------------------- Method mouse sees--------------------------3.1--- public function seeMouse($mouseCatArray, $seeMouseField, $border) { //+---------------------------------------------------------3.1.1-mouse sees cat $catInField = 0; $randomArray = array("up" => '', "right" => '', "down" => '', "left" => ''); foreach ($mouseCatArray as $value) { if ($this->x == $value->x && $this->y == $value->y) { continue; } $seeMouse = ($seeMouseField - 1) / 2; $xS = $this->x - $value->x; $yS = $this->y - $value->y; if (($value->look == "C " || $value->look == "@ " || $value->look == "P ") && $xS <= $seeMouse && $xS >= -$seeMouse && $yS <= $seeMouse && $yS >= -$seeMouse) { $catInField ++; $x = $xS; $y = $yS; } //+---------------------------------3.1.2-mouse sees mouses and chooses the path if ($value->look != "C " && $yS == 1 && $xS == 0) { unset($randomArray["left"]); } elseif ($value->look != "C " && $yS == 0 && $xS == -1) { unset($randomArray["up"]); } elseif ($value->look != "C " && $yS == -1 && $xS == 0) { unset($randomArray["right"]); } elseif ($value->look != "C " && $yS == 0 && $xS == 1) { unset($randomArray["down"]); } } //-------------------------------3.1.3-If a number of cat mouse chooses the path if ($catInField > 1) { return FALSE; } elseif ($catInField == 1) { if ($y > 0 && $x == 0) { unset($randomArray["left"]); } elseif ($y == 0 && $x < 0) { unset($randomArray["up"]); } elseif ($y < 0 && $x == 0) { unset($randomArray["right"]); } elseif ($y == 0 && $x > 0) { unset($randomArray["down"]); } elseif ($y > 0 && $x < 0) { unset($randomArray["up"], $randomArray["left"]); } elseif ($y < 0 && $x < 0) { unset($randomArray["up"], $randomArray["right"]); } elseif ($y < 0 && $x > 0) { unset($randomArray["right"], $randomArray["down"]); } elseif ($y > 0 && $x > 0) { unset($randomArray["left"], $randomArray["down"]); } } //---------------------------------------------------3.1.4-mouse sees the border if (($this->x + 1) == $border[x]) { unset($randomArray["up"]); } elseif (($this->x - 1) == -1) { unset($randomArray["down"]); } elseif (($this->y + 1) == $border[y]) { unset($randomArray["right"]); } elseif (($this->y - 1) == -1) { unset($randomArray["left"]); } if ($this->x + 1 == $border[x] && $this->y + 1 == $border[y]) { unset($randomArray["right"]); } elseif ($this->x - 1 == -1 && $this->y - 1 == -1) { unset($randomArray["left"]); } elseif ($this->x - 1 == -1 && $this->y + 1 == $border[y]) { unset($randomArray["right"]); } elseif ($this->x + 1 == $border[x] && $this->y - 1 == -1) { unset($randomArray["left"]); } $this->goMouse($randomArray); } //----------------------------Method go mouse-----------------------------3.2--- private function goMouse($moveMouse) { $move = array_rand($moveMouse); if ($move == "up") { $this->x ++; } elseif ($move == "right") { $this->y ++; } elseif ($move == "down") { $this->x --; } elseif ($move == "left") { $this->y --; } } } //+-------------------------------------------------------------------------4--- // Class Map //------------------------------------------------------------------------------ class Map extends Animals { private $height; private $width; protected function __construct($height, $width) { $this->height = $height; $this->width = $width; } //+-------------------------------------------------------------1.4.1-output map protected function createMap() { echo "\n\n\n"; for ($n = 0; $n < $this->height; $n++) { $valueMap[] = "<div class='mapArray'> </div>"; } for ($i = 0; $i < $this->width; $i++) { $outputMap[] = $valueMap; } $outputMap = array_reverse($outputMap, true); foreach ($this->kor as $key => $value) { foreach ($value as $key1 => $val) { $outputMap[$key][$key1] = $val; } } foreach ($outputMap as $value) { echo "\n"; foreach ($value as $val) { echo "{$val}"; } } echo "\n"; return $border = array("x" => $this->height, "y" => $this->width); } //+-------------------------------------------------1.4.2-add animals on the map protected function add($animal) { $this->kor[$animal->x][$animal->y] = $animal->lookMap; return $this->kor; } } //--------------------------------------------------------------------------5--- // Class Game //------------------------------------------------------------------------------ class Game extends Map { //----------------------------------------------------5.1-add animals on the map static function addOnMapAnimals($mouseCatArray, $map) { foreach ($mouseCatArray as $value) { $map->add($value); } return $map->createMap(); } //-------------------------Method controls the game----------------------5.2--- static function startGame($sleepCatMove, $height, $width, $mouseArray, $catArray) { //------------------------------------------------------5.2.1-starting positions if (!isset($_SESSION['counter'])) { $sleepCat = $sleepCatMove; $map = new Map($height, $width); $mouseCatArray = array_merge($mouseArray, $catArray); $border = Game::addOnMapAnimals($mouseCatArray, $map); $_SESSION['mouseCatArray'] = $mouseCatArray; $_SESSION['border'] = $border; $_SESSION['sleepCat'] = $sleepCat; } else { $map = new Map($height, $width); $mouses = count($mouseArray); $cats = count($catArray); foreach ($mouseArray as $value) { $value->seeMouse($_SESSION['mouseCatArray'], $value->seeMouseField, $_SESSION['border']); } if ($_SESSION['counter'] === $_SESSION['sleepCat']) { $_SESSION['sleepCat'] += $sleepCatMove; $sleep = TRUE; } else { $sleep = False; } foreach ($catArray as $value) { $catchMouseCat[] = $value->seeCat($_SESSION['mouseCatArray'], $sleep); } foreach ($catchMouseCat as $key => $value) { for ($l = 0; $l <= $mouses + 5; $l++) { settype($value, "integer"); if ($value == $l) { unset($_SESSION['mouseArray'][$value]); } } } unset($catchMouseCat); echo "\nХод : {$_SESSION['counter']}\n"; echo "Мышук на поле: {$mouses}\n"; echo "Кошек на поле: $cats"; if (count($_SESSION['mouseCatArray']) == $cats) { echo "\nВсех мышок съели!!!\n"; unset($_SESSION); // или $_SESSION = array() для очистки всех данных сессии session_destroy(); header('Location: main.php'); } $_SESSION['mouseCatArray'] = array_merge($mouseArray, $catArray); Game::addOnMapAnimals($_SESSION['mouseCatArray'], $map); } } } //--------------------------------------------------------------------------6--- // Enter all animals //------------------------------------------------------------------------------ if (!isset($_SESSION['counter'])) { $catsNum = htmlspecialchars($_POST['cat']); $mouseNum = htmlspecialchars($_POST['mouse']); if ($mouseNum >= 3) { $mouse1 = new Mouse(25, 25, "1 ", 9); //enter x,y,look,seeMouseField $mouseArray[1] = $mouse1; $mouse2 = new Mouse(29, 29, "2 ", 9); $mouseArray[2] = $mouse2; $mouse3 = new Mouse(29, 3, "3 ", 9); $mouseArray[3] = $mouse3; } if ($mouseNum >= 4) { $mouse4 = new Mouse(1, 1, "4 ", 9); $mouseArray[4] = $mouse4; } if ($mouseNum >= 5) { $mouse5 = new Mouse(3, 16, "5 ", 9); $mouseArray[5] = $mouse5; } if ($mouseNum >= 6) { $mouse6 = new Mouse(5, 19, "6 ", 9); $mouseArray[6] = $mouse6; } if ($mouseNum >= 7) { $mouse7 = new Mouse(19, 5, "7 ", 9); $mouseArray[7] = $mouse7; } if ($catsNum >= 1) { $cat1 = new Cat(5, 7); //enter x, y,look $catArray[] = $cat1; } if ($catsNum >= 2) { $cat2 = new Cat(29, 6); $catArray[] = $cat2; } if ($catsNum >= 3) { $cat3 = new Cat(23, 0); $catArray[] = $cat3; } if ($catsNum >= 4) { $cat4 = new Cat(15, 15); $catArray[] = $cat4; } $_SESSION['catArray'] = $catArray; $_SESSION['mouseArray'] = $mouseArray; //------------------------------------------------------------------------ //----------------------------------------------------------------- //------------------------------------------------------------------- $nameAnon = htmlspecialchars($_POST['name']); echo "\nПривет, {$nameAnon}!\n"; $choseAnimal = htmlspecialchars($_POST['animal']); if ($choseAnimal == "cat") { echo "Вы выбрали кошку!\n"; $cat1->lookMap = "<div id='playerCat' class='playerAnimal'> </div>"; } elseif ($choseAnimal == "mouse") { echo "Вы выбрали мышку!\n"; $mouse1->lookMap = "<div id='playerMouse' class='playerAnimal'> </div>"; } else { echo "Вы не выбрали персонажа, играют только боты!\n"; } echo "Начало игры. Все на позиции!!!"; } //-------enter ( $sleepCatMove, $widthFild, $heightFild); Game::startGame(8, 30, 30, $_SESSION['mouseArray'], $_SESSION['catArray']); $_SESSION['counter'] ++; $_SESSION['key'] = $_POST['key']; echo "{$_SESSION['key']}\n"; ?> </pre> <div rel="mouse" class="hover">Это мышь она может ходить только на одну клетку вверх-вниз и влево-вправо и видит область в 9X9 клеток вокруг себя.</div> <div rel="cats" class="hover">Кошка она может ходит на одну клетку в любую сторону, видит всю карту и выбирает ближайщую мышку для охоты.</div> <div rel="playerCat" class="hover">Кошка которой вы управляете!</div> <div rel="playerMouse" class="hover">Мышка которой вы управляете!</div> <div rel="catEat" class="hover">Кошка отдыхает!</div> <div rel="sides" class="hover">Выбирите куда идти!</div> <div rel="mave" class="hover">Следующий ход!</div> </div> </body> </html>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
jQ ID Selector
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$('.hover').hide();
$(document).ready(function() {

  $(".mave").hover(
    function() {
      $(this).addClass('activeMave');

    },
    function() {
      $(this).removeClass('activeMave');

    }
  );

  $(".side").hover(
    function() {
      $(this).addClass('activeSide');

    },
    function() {
      $(this).removeClass('activeSide');

    }
  );


  //--------------------------------------------------------------------------
  $('.cats .mouse').hover(
    function() {
      $(this).addClass('active');
      $('.mapArray').addClass('activeMap');
    },
    function() {
      $(this).removeClass('active');
      $('.mapArray').removeClass('activeMap');
    }
  );

  /*$(".mouse").hover(
            function () {
                $(this).addClass('active');
                $('.mapArray').addClass('activeMap');
            },
            function () {
                $(this).removeClass('active');
                $('.mapArray').removeClass('activeMap');
            }
    );*/

  $(".playerAnimal").hover(
    function() {
      $(this).addClass('activePlayerAnimal');
      $('.mapArray').addClass('activeMap');
    },
    function() {
      $(this).removeClass('activePlayerAnimal');
      $('.mapArray').removeClass('activeMap');
    }
  );

  $(".catEat").hover(
    function() {
      $(this).addClass('active');
      $('.mapArray').addClass('activeMap');
    },
    function() {
      $(this).removeClass('active');
      $('.mapArray').removeClass('activeMap');
    }
  );




  $('.hover').each(function() {
    var hover = $(this);
    hover.hide();

    $("#" + hover.attr('rel')).hover(function() {
      hover.toggle(200);

    });
    $("." + hover.attr('rel')).hover(function() {
      hover.toggle(200);

    });
  });

  ;


  /*  $(document).keydown(function (key) {
     switch (parseInt(key.which, 10)) {
     // Left arrow key pressed
     case 37:
     var side = 'left';
     break;
     // Up Arrow Pressed
     case 38:
     var side = 'up';
     break;
     // Right Arrow Pressed
     case 39:
     var side = 'right';
     break;
     // Down Arrow Pressed
     case 40:
     var side = 'down';
     break;
     }
     
     
     });*/

  $.ajax({
    type: "POST",
    url: 'index.php',
    //data: "key=" + 0,
    //dataType : "html",
    success: function(data) {
      $('.mave').click(function() {

        $(".main").empty();

        $(".main").append(data);

      });
    }

  });
});
pending…
JS ID Selector
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$('.hover').hide();
$(document).ready(function() {

  $(".mave").hover(
    function() {
      $(this).addClass('activeMave');

    },
    function() {
      $(this).removeClass('activeMave');

    }
  );

  $(".side").hover(
    function() {
      $(this).addClass('activeSide');

    },
    function() {
      $(this).removeClass('activeSide');

    }
  );


  //--------------------------------------------------------------------------
  $('.cats .mouse').hover(
    function() {
      $(this).addClass('active');
      $('.mapArray').addClass('activeMap');
    },
    function() {
      $(this).removeClass('active');
      $('.mapArray').removeClass('activeMap');
    }
  );

  /*$(".mouse").hover(
            function () {
                $(this).addClass('active');
                $('.mapArray').addClass('activeMap');
            },
            function () {
                $(this).removeClass('active');
                $('.mapArray').removeClass('activeMap');
            }
    );*/

  $(".playerAnimal").hover(
    function() {
      $(this).addClass('activePlayerAnimal');
      $('.mapArray').addClass('activeMap');
    },
    function() {
      $(this).removeClass('activePlayerAnimal');
      $('.mapArray').removeClass('activeMap');
    }
  );

  $(".catEat").hover(
    function() {
      $(this).addClass('active');
      $('.mapArray').addClass('activeMap');
    },
    function() {
      $(this).removeClass('active');
      $('.mapArray').removeClass('activeMap');
    }
  );




  $('.hover').each(function() {
    var hover = $(this);
    hover.hide();

    $("#" + hover.attr('rel')).hover(function() {
      hover.toggle(200);

    });
    $("." + hover.attr('rel')).hover(function() {
      hover.toggle(200);

    });
  });

  ;


  /*  $(document).keydown(function (key) {
     switch (parseInt(key.which, 10)) {
     // Left arrow key pressed
     case 37:
     var side = 'left';
     break;
     // Up Arrow Pressed
     case 38:
     var side = 'up';
     break;
     // Right Arrow Pressed
     case 39:
     var side = 'right';
     break;
     // Down Arrow Pressed
     case 40:
     var side = 'down';
     break;
     }
     
     
     });*/

  $.ajax({
    type: "POST",
    url: 'index.php',
    //data: "key=" + 0,
    //dataType : "html",
    success: function(data) {
      $('.mave').click(function() {

        $(".main").empty();

        $(".main").append(data);

      });
    }

  });
});
pending…
JS generic Selector
var $el = document.querySelector('#hello');
pending…
jQ Class Selector
var $el = $('.bye');
pending…
JS Class Selector
var $el = document.getElementsByClassName('bye');
pending…
JS generic Selector
var $el = document.querySelectorAll('.bye');
pending…
jQ multiselect
var $el = $('#hello .bye');
pending…
JS multiselect
var $el = document.querySelectorAll('#hello .bye');
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments

                      
"; function __construct($x, $y) { $this->x = $x; $this->y = $y; } //----------------------------Method Cat sees map-------------------------2.1--- public function seeCat($moveCat, $sleep) { $mouseN = 0; $selectionMouseSee = 1; $catClose = array("x" => array(), "y" => array()); $vectors = 0; $mouseHwoCatch = array("vectors" => 100000, "x" => 0, "y" => 0, "look" => ""); if ($this->look == "P " || $sleep == TRUE) { $this->look = "@ "; $this->lookMap = "
"; return 0; } elseif ($this->look == "@ ") { $this->look = "C "; $this->lookMap = "
"; } //-----------------------------------------------2.1.1-cat sees mouse and choses foreach ($moveCat as $value) { if ($this->x == $value->x && $this->y == $value->y) { continue; } $pMouseX = ($this->x - $value->x); ($pMouseX < 0) ? ($pMouseX *= -1) : (''); $pMouseY = $this->y - $value->y; ($pMouseY < 0) ? ($pMouseY *= -1) : (''); $vectors = max($pMouseX, $pMouseY); if (($value->look !== "C " && $value->look !== "@ " && $value->look !== "P ") && ($vectors < $mouseHwoCatch[vectors])) { $mouseHwoCatch[vectors] = $vectors; $mouseHwoCatch[x] = $value->x; $mouseHwoCatch[y] = $value->y; $mouseHwoCatch[look] = $value->look; } } //---------------------2.1.2-cat sees how many mouses close with selection Mouse foreach ($moveCat as $value) { if ($this->x == $value->x && $this->y == $value->y) { continue; } $x = $mouseHwoCatch[x] - $value->x; $y = $mouseHwoCatch[y] - $value->y; if (($value->look !== "C ") && ($value->look !== "@ ") && ($value->look !== "P ") && $x <= $selectionMouseSee && $x >= $selectionMouseSee * -1 && $y <= $selectionMouseSee && $y >= $selectionMouseSee * -1) { $mouseN ++; } //------------------------------------------2.1.3-cat sees other cats on his way $xC = $this->x - $value->x; $yC = $this->y - $value->y; if (($value->look == "C " || $value->look == "@ " || $value->look == "P ") && $xC <= $selectionMouseSee && $xC >= $selectionMouseSee * -1 && $yC <= $selectionMouseSee && $yC >= $selectionMouseSee * -1) { $catClose[x][] = $value->x; $catClose[y][] = $value->y; } } //----------------------------------------------------2.1.4-Cat chooses the path $x = $this->x - $mouseHwoCatch[x]; $y = $this->y - $mouseHwoCatch[y]; if ($mouseN >= 3) { return FALSE; } else { if ($y > 0 && $x == 0) { $rA = array("left" => ''); } elseif ($y == 0 && $x < 0) { $rA = array("up" => ''); } elseif ($y < 0 && $x == 0) { $rA = array("right" => ''); } elseif ($y == 0 && $x > 0) { $rA = array("down" => ''); } elseif ($y > 0 && $x < 0) { $rA = array("up-left" => ''); } elseif ($y < 0 && $x < 0) { $rA = array("up-right" => ''); } elseif ($y < 0 && $x > 0) { $rA = array("down-right" => ''); } elseif ($y > 0 && $x > 0) { $rA = array("down-left" => ''); } } //----------------------------------------------------2.1.5-other cat on the way $rAkey = array_rand($rA); foreach ($catClose as $k => $value) { foreach ($value as $key => $val) { $catClose[$k][$key] = $this->$k - $val; } } $rAr0 = array(); $rAr1 = array(); $rAr2 = array(); for ($g = 0; $g < count($catClose[x]); $g++) { $j = FALSE; if ($rAkey == "left" && $catClose[y][$g] > 0 && $catClose[x][$g] == 0) { $rArCat = array("down-left" => '', "up-left" => ''); $j = TRUE; } elseif ($rAkey == "up" && $catClose[y][$g] == 0 && $catClose[x][$g] < 0) { $rArCat = array("up-right" => '', "up-left" => ''); $j = TRUE; } elseif ($rAkey == "right" && $catClose[y][$g] < 0 && $catClose[x][$g] == 0) { $rArCat = array("up-right" => '', "down-right" => ''); $j = TRUE; } elseif ($rAkey == "down" && $catClose[y][$g] == 0 && $catClose[x][$g] > 0) { $rArCat = array("down-left" => '', "down-right" => ''); $j = TRUE; } elseif ($rAkey == "up-left" && $catClose[y][$g] > 0 && $catClose[x][$g] < 0) { $rArCat = array("up" => '', "left" => ''); $j = TRUE; } elseif ($rAkey == "up-right" && $catClose[y][$g] < 0 && $catClose[x][$g] < 0) { $rArCat = array("up" => '', "right" => ''); $j = TRUE; } elseif ($rAkey == "down-right" && $catClose[y][$g] < 0 && $catClose[x][$g] > 0) { $rArCat = array("down" => '', "right" => ''); $j = TRUE; } elseif ($rAkey == "down-left" && $catClose[y][$g] > 0 && $catClose[x][$g] > 0) { $rArCat = array("down" => '', "left" => ''); $j = TRUE; } if ($g == 0) { $rAr0 = $rArCat; } elseif ($g == 1) { $rAr1 = $rArCat; } if ($g == 2) { $rAr2 = $rArCat; } } if ($j == TRUE) { $rA = array_diff_key($rAr0, $rAr1, $rAr2); } return $this->goCat($rA, $mouseHwoCatch); } //----------------------------Method cat goes-----------------------------2.2--- private function goCat($moveCat, $mouseHwoCatch) { $move = array_rand($moveCat); if ($move == "up") { $this->x ++; } elseif ($move == "up-right") { $this->x ++; $this->y ++; } elseif ($move == "right") { $this->y ++; } elseif ($move == "down-right") { $this->x --; $this->y ++; } elseif ($move == "down") { $this->x --; } elseif ($move == "down-left") { $this->x --; $this->y --; } elseif ($move == "left") { $this->y --; } elseif ($move == "up-left") { $this->x ++; $this->y --; } //-------------------------------------------------------------2.2.1-catch Mouse if ($mouseHwoCatch[x] == $this->x && $mouseHwoCatch[y] == $this->y) { $this->look = "P "; $this->lookMap = "
"; echo"\nМышку съели!!!"; return $mouseHwoCatch[look]; } } } //+-------------------------------------------------------------------------3--- // Class Mouse //+----------------------------------------------------------------------------- class Mouse extends Animals { public $lookMap = "
"; function __construct($x, $y, $look, $seeMouseField) { $this->x = $x; $this->y = $y; $this->look = $look; $this->seeMouseField = $seeMouseField; } //+--------------------------- Method mouse sees--------------------------3.1--- public function seeMouse($mouseCatArray, $seeMouseField, $border) { //+---------------------------------------------------------3.1.1-mouse sees cat $catInField = 0; $randomArray = array("up" => '', "right" => '', "down" => '', "left" => ''); foreach ($mouseCatArray as $value) { if ($this->x == $value->x && $this->y == $value->y) { continue; } $seeMouse = ($seeMouseField - 1) / 2; $xS = $this->x - $value->x; $yS = $this->y - $value->y; if (($value->look == "C " || $value->look == "@ " || $value->look == "P ") && $xS <= $seeMouse && $xS >= -$seeMouse && $yS <= $seeMouse && $yS >= -$seeMouse) { $catInField ++; $x = $xS; $y = $yS; } //+---------------------------------3.1.2-mouse sees mouses and chooses the path if ($value->look != "C " && $yS == 1 && $xS == 0) { unset($randomArray["left"]); } elseif ($value->look != "C " && $yS == 0 && $xS == -1) { unset($randomArray["up"]); } elseif ($value->look != "C " && $yS == -1 && $xS == 0) { unset($randomArray["right"]); } elseif ($value->look != "C " && $yS == 0 && $xS == 1) { unset($randomArray["down"]); } } //-------------------------------3.1.3-If a number of cat mouse chooses the path if ($catInField > 1) { return FALSE; } elseif ($catInField == 1) { if ($y > 0 && $x == 0) { unset($randomArray["left"]); } elseif ($y == 0 && $x < 0) { unset($randomArray["up"]); } elseif ($y < 0 && $x == 0) { unset($randomArray["right"]); } elseif ($y == 0 && $x > 0) { unset($randomArray["down"]); } elseif ($y > 0 && $x < 0) { unset($randomArray["up"], $randomArray["left"]); } elseif ($y < 0 && $x < 0) { unset($randomArray["up"], $randomArray["right"]); } elseif ($y < 0 && $x > 0) { unset($randomArray["right"], $randomArray["down"]); } elseif ($y > 0 && $x > 0) { unset($randomArray["left"], $randomArray["down"]); } } //---------------------------------------------------3.1.4-mouse sees the border if (($this->x + 1) == $border[x]) { unset($randomArray["up"]); } elseif (($this->x - 1) == -1) { unset($randomArray["down"]); } elseif (($this->y + 1) == $border[y]) { unset($randomArray["right"]); } elseif (($this->y - 1) == -1) { unset($randomArray["left"]); } if ($this->x + 1 == $border[x] && $this->y + 1 == $border[y]) { unset($randomArray["right"]); } elseif ($this->x - 1 == -1 && $this->y - 1 == -1) { unset($randomArray["left"]); } elseif ($this->x - 1 == -1 && $this->y + 1 == $border[y]) { unset($randomArray["right"]); } elseif ($this->x + 1 == $border[x] && $this->y - 1 == -1) { unset($randomArray["left"]); } $this->goMouse($randomArray); } //----------------------------Method go mouse-----------------------------3.2--- private function goMouse($moveMouse) { $move = array_rand($moveMouse); if ($move == "up") { $this->x ++; } elseif ($move == "right") { $this->y ++; } elseif ($move == "down") { $this->x --; } elseif ($move == "left") { $this->y --; } } } //+-------------------------------------------------------------------------4--- // Class Map //------------------------------------------------------------------------------ class Map extends Animals { private $height; private $width; protected function __construct($height, $width) { $this->height = $height; $this->width = $width; } //+-------------------------------------------------------------1.4.1-output map protected function createMap() { echo "\n\n\n"; for ($n = 0; $n < $this->height; $n++) { $valueMap[] = "
"; } for ($i = 0; $i < $this->width; $i++) { $outputMap[] = $valueMap; } $outputMap = array_reverse($outputMap, true); foreach ($this->kor as $key => $value) { foreach ($value as $key1 => $val) { $outputMap[$key][$key1] = $val; } } foreach ($outputMap as $value) { echo "\n"; foreach ($value as $val) { echo "{$val}"; } } echo "\n"; return $border = array("x" => $this->height, "y" => $this->width); } //+-------------------------------------------------1.4.2-add animals on the map protected function add($animal) { $this->kor[$animal->x][$animal->y] = $animal->lookMap; return $this->kor; } } //--------------------------------------------------------------------------5--- // Class Game //------------------------------------------------------------------------------ class Game extends Map { //----------------------------------------------------5.1-add animals on the map static function addOnMapAnimals($mouseCatArray, $map) { foreach ($mouseCatArray as $value) { $map->add($value); } return $map->createMap(); } //-------------------------Method controls the game----------------------5.2--- static function startGame($sleepCatMove, $height, $width, $mouseArray, $catArray) { //------------------------------------------------------5.2.1-starting positions if (!isset($_SESSION['counter'])) { $sleepCat = $sleepCatMove; $map = new Map($height, $width); $mouseCatArray = array_merge($mouseArray, $catArray); $border = Game::addOnMapAnimals($mouseCatArray, $map); $_SESSION['mouseCatArray'] = $mouseCatArray; $_SESSION['border'] = $border; $_SESSION['sleepCat'] = $sleepCat; } else { $map = new Map($height, $width); $mouses = count($mouseArray); $cats = count($catArray); foreach ($mouseArray as $value) { $value->seeMouse($_SESSION['mouseCatArray'], $value->seeMouseField, $_SESSION['border']); } if ($_SESSION['counter'] === $_SESSION['sleepCat']) { $_SESSION['sleepCat'] += $sleepCatMove; $sleep = TRUE; } else { $sleep = False; } foreach ($catArray as $value) { $catchMouseCat[] = $value->seeCat($_SESSION['mouseCatArray'], $sleep); } foreach ($catchMouseCat as $key => $value) { for ($l = 0; $l <= $mouses + 5; $l++) { settype($value, "integer"); if ($value == $l) { unset($_SESSION['mouseArray'][$value]); } } } unset($catchMouseCat); echo "\nХод : {$_SESSION['counter']}\n"; echo "Мышук на поле: {$mouses}\n"; echo "Кошек на поле: $cats"; if (count($_SESSION['mouseCatArray']) == $cats) { echo "\nВсех мышок съели!!!\n"; unset($_SESSION); // или $_SESSION = array() для очистки всех данных сессии session_destroy(); header('Location: main.php'); } $_SESSION['mouseCatArray'] = array_merge($mouseArray, $catArray); Game::addOnMapAnimals($_SESSION['mouseCatArray'], $map); } } } //--------------------------------------------------------------------------6--- // Enter all animals //------------------------------------------------------------------------------ if (!isset($_SESSION['counter'])) { $catsNum = htmlspecialchars($_POST['cat']); $mouseNum = htmlspecialchars($_POST['mouse']); if ($mouseNum >= 3) { $mouse1 = new Mouse(25, 25, "1 ", 9); //enter x,y,look,seeMouseField $mouseArray[1] = $mouse1; $mouse2 = new Mouse(29, 29, "2 ", 9); $mouseArray[2] = $mouse2; $mouse3 = new Mouse(29, 3, "3 ", 9); $mouseArray[3] = $mouse3; } if ($mouseNum >= 4) { $mouse4 = new Mouse(1, 1, "4 ", 9); $mouseArray[4] = $mouse4; } if ($mouseNum >= 5) { $mouse5 = new Mouse(3, 16, "5 ", 9); $mouseArray[5] = $mouse5; } if ($mouseNum >= 6) { $mouse6 = new Mouse(5, 19, "6 ", 9); $mouseArray[6] = $mouse6; } if ($mouseNum >= 7) { $mouse7 = new Mouse(19, 5, "7 ", 9); $mouseArray[7] = $mouse7; } if ($catsNum >= 1) { $cat1 = new Cat(5, 7); //enter x, y,look $catArray[] = $cat1; } if ($catsNum >= 2) { $cat2 = new Cat(29, 6); $catArray[] = $cat2; } if ($catsNum >= 3) { $cat3 = new Cat(23, 0); $catArray[] = $cat3; } if ($catsNum >= 4) { $cat4 = new Cat(15, 15); $catArray[] = $cat4; } $_SESSION['catArray'] = $catArray; $_SESSION['mouseArray'] = $mouseArray; //------------------------------------------------------------------------ //----------------------------------------------------------------- //------------------------------------------------------------------- $nameAnon = htmlspecialchars($_POST['name']); echo "\nПривет, {$nameAnon}!\n"; $choseAnimal = htmlspecialchars($_POST['animal']); if ($choseAnimal == "cat") { echo "Вы выбрали кошку!\n"; $cat1->lookMap = "
"; } elseif ($choseAnimal == "mouse") { echo "Вы выбрали мышку!\n"; $mouse1->lookMap = "
"; } else { echo "Вы не выбрали персонажа, играют только боты!\n"; } echo "Начало игры. Все на позиции!!!"; } //-------enter ( $sleepCatMove, $widthFild, $heightFild); Game::startGame(8, 30, 30, $_SESSION['mouseArray'], $_SESSION['catArray']); $_SESSION['counter'] ++; $_SESSION['key'] = $_POST['key']; echo "{$_SESSION['key']}\n"; ?>
Это мышь она может ходить только на одну клетку вверх-вниз и влево-вправо и видит область в 9X9 клеток вокруг себя.
Кошка она может ходит на одну клетку в любую сторону, видит всю карту и выбирает ближайщую мышку для охоты.
Кошка которой вы управляете!
Мышка которой вы управляете!
Кошка отдыхает!
Выбирите куда идти!
Следующий ход!