Thursday, December 26, 2024

JavaScript Based Man-Demon Puzzle Simulator

Man-demon puzzle is like a classic river-crossing puzzle. The missionaries-cannibals—one of the river-crossing puzzles—is a well-known game problem in artificial intelligence, where it is used as an example of problem representation. Not only logic but also reasoning and memory skills are required to solve these kinds of puzzles.

This program demonstrates how you can design the man-demon puzzle using JavaScript code. The logic in this puzzle can be applied to electronics to simulate fuzzy logic and how a human brain interprets and studies a problem under constricted environments. The logic can be applied in the field of robotics to develop artificial neural networks that help in artificial intelligence development.

How it works
In the man-demon river-crossing puzzle, three men and three demons have to cross a river using a single boat. There are three conditions: First, all six of them will have to cross the river using this boat only. Second, the boat can carry a maximum of two passengers at a time. Third, at any time, when both men and demons are standing on either bank, the demons will eat men if men on that bank are outnumbered by demons. All men should cross the river without being eaten by demons.

- Advertisement -
45F_pic
Screenshot of the man-demon puzzle (simulator program output)

The algorithm of this puzzle has been taken from Wikipedia (http://en.wikipedia.org/wiki/Missionaries_and_cannibals_problem).

Software program
MDP.htm is the main program written in JavaScript. Men and demons are represented by blocks and designed using element in HTML.

- Advertisement -

Each green block represents a man, a red block represents a demon and the brown block represents a boat (refer screenshot). White blocks represent blank spaces. At least twelve block movements are needed to reach the solution. You need to remember which blocks have been moved previously and which blocks need to be moved next.

In case you are stuck while solving the problem, a chart with steps to move the correct blocks is also included in the program. Just click ‘Solution Algorithm’ in the instructions section to see the chart table. This table helps you to pick man, demon and the right combination of the two at a time, which makes the program user-friendly.

The program uses PNG (portable network graphics) images to depict men, demons, boat and blank spaces. Make sure that these files are placed in the same folder as the MDP.htm file.

The various functions used in the program are:

Padavani_Itukatu_Taralinchu (Evarini). It is used to shift the boat from the left side of the river to the right side and vice-versa.

Lekka_pettuko( ). It is used to check the steps so that the algorithm satisfies the constraints without any loss of men. It also alerts you when a wrong step is taken; in this case, a man is eaten by demons.

Saripoindo_ledo_choodu( ). It checks whether all men and demons have crossed the river safely without any loss of men.

Edama_odduki_cherchu( ). It shifts the laden boat to the left bank from the right bank of the river, in the same manner.

Kudi_odduki_cherchu( ). It shifts the boat from the left bank to the right bank of the river.

Modati_nundi( ). It resets the puzzle to the start.

Naaku_teliyatamledu_Ela_Cheyyalo_Cheppava( ). It is used to get step-by-step help by launching ‘Solution Algorithm’ mode.

Download source code: click here


The author, a B.Tech in electronics and communications engineering, likes coding and has keen interest in space technology (satellite communication) and mobile communication

2 COMMENTS

SHARE YOUR THOUGHTS & COMMENTS

EFY Prime

Unique DIY Projects

Truly Innovative Electronics

Electronics News

Latest DIY Videos

Electronics Components

Electronics Jobs

Calculators For Electronics