Solving Jars Game with PHP

Have you ever played this game before? This is a logic game which require logics and memory. In this game, the objective is to make some jars with desired water level. In this case, you have 3 jars. Each jars have different maximum water level. the first jar is 7, second is 13 and last is 19. Initial water level is… you can see the picture above… first jar and second jar are at the maximum capacity of water and the other is empty. You have to pour the water to another jar and make sure you don’t spill the water.

Here is my experiment by doing some PHP script that may solve the case. I didn’t write some special algorithms to trace the shortest way to do that. What I only wrote is probing with a random approach.

The algorithms are simple:

  1. Pour water to random jar
  2. Check each jars level
  3. Memorise the level of each jars
  4. Pour the water again to random jar but don’t pour it if the level of each jar will be the same as before
  5. Check each jars level again
  6. If the jars are in a desired water level, the problem solved Razz
  7. If you face the dead end, then restart Mad
  8. If you face the dead end several time, then surrender Cry

Save this as functions.php. This function used to distribute an array value to another array.

 

Save this as JarsGame.php. This file has a class in it and the HTML to visually show the result.

 

Result:

Lap Activity State
0 - 0 , 13 , 7
1 [1] => [0] 13 , 0 , 7
2 [2] => [1] 13 , 7 , 0
3 [0] => [2] 6 , 7 , 7
4 [2] => [1] 6 , 13 , 1
5 [1] => [0] 19 , 0 , 1
6 [2] => [1] 19 , 1 , 0
7 [0] => [2] 12 , 1 , 7
8 [2] => [1] 12 , 8 , 0
9 [0] => [2] 5 , 8 , 7
10 [2] => [1] 5 , 13 , 2
11 [2] => [0] 7 , 13 , 0
12 [1] => [2] 7 , 6 , 7
13 [2] => [0] 14 , 6 , 0
14 [1] => [2] 14 , 0 , 6
15 [0] => [1] 1 , 13 , 6
16 [1] => [2] 1 , 12 , 7
17 [2] => [0] 8 , 12 , 0
18 [1] => [2] 8 , 5 , 7
19 [2] => [0] 15 , 5 , 0
20 [1] => [2] 15 , 0 , 5
21 [0] => [1] 2 , 13 , 5
22 [2] => [1] 2 , 13 , 5
23 [1] => [2] 2 , 11 , 7
24 [2] => [0] 9 , 11 , 0
25 [1] => [2] 9 , 4 , 7
26 [2] => [0] 16 , 4 , 0
27 [1] => [2] 16 , 0 , 4
28 [0] => [1] 3 , 13 , 4
29 [1] => [2] 3 , 10 , 7
30 [2] => [0] 10 , 10 , 0

Of Course you may change the initial level, maximum level and desired water level by changing the parameter.

That’s it. Thank’s for reading. Handshake

One Reply to “Solving Jars Game with PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

Click to Insert Smiley

SmileBig SmileGrinLaughFrownBig FrownCryNeutralWinkKissRazzChicCoolAngryReally AngryConfusedQuestionThinkingPainShockYesNoLOLSillyBeautyLashesCuteShyBlushKissedIn LoveDroolGiggleSnickerHeh!SmirkWiltWeepIDKStruggleSide FrownDazedHypnotizedSweatEek!Roll EyesSarcasmDisdainSmugMoney MouthFoot in MouthShut MouthQuietShameBeat UpMeanEvil GrinGrit TeethShoutPissed OffReally PissedMad RazzDrunken RazzSickYawnSleepyDanceClapJumpHandshakeHigh FiveHug LeftHug RightKiss BlowKissingByeGo AwayCall MeOn the PhoneSecretMeetingWavingStopTime OutTalk to the HandLoserLyingDOH!Fingers CrossedWaitingSuspenseTremblePrayWorshipStarvingEatVictoryCurseAlienAngelClownCowboyCyclopsDevilDoctorFemale FighterMale FighterMohawkMusicNerdPartyPirateSkywalkerSnowmanSoldierVampireZombie KillerGhostSkeletonBunnyCatCat 2ChickChickenChicken 2CowCow 2DogDog 2DuckGoatHippoKoalaLionMonkeyMonkey 2MousePandaPigPig 2SheepSheep 2ReindeerSnailTigerTurtleBeerDrinkLiquorCoffeeCakePizzaWatermelonBowlPlateCanFemaleMaleHeartBroken HeartRoseDead RosePeaceYin YangUS FlagMoonStarSunCloudyRainThunderUmbrellaRainbowMusic NoteAirplaneCarIslandAnnouncebrbMailCellPhoneCameraFilmTVClockLampSearchCoinsComputerConsolePresentSoccerCloverPumpkinBombHammerKnifeHandcuffsPillPoopCigarette