hasertag.blogg.se

Missionaries and cannibals game
Missionaries and cannibals game






MISSIONARIES AND CANNIBALS GAME CODE

Note that the right side is calculated by subtracting from 3, so 3 indicates 0 on the right and 0 indicates 3 of a type on the right side.ĭownload Complete Code here. Is the program able to determine a tree where there only needs to be 2 of each person on the right side as a goal 1,1,right. Another test would be to see if the test still works if there are less than three missionaries and cannibals on the left side to start 2,2,left.

missionaries and cannibals game

We will then modify the goal states, to see if this algorithm is able to search for more missionaries and cannibals on the left side for the starting state 4,4,left. Since this will indicate that the starting state is 3 missionaries and 3 cannibals on the left and none on the right, and the goal state will be that there are 3 of each on the right and none on the left. This is a simple check to make sure that any move being taken is legal at the given point. The move commands correspond to what is being moved, and what direction they are moving in, and what output to provide to the user when queried. In this format, A are cannibals on the left side, B are missionaries on the left side, while C are cannibals on the right side and D are missionaries on the right side. Here we find the constraints on every type of move. All references to Out just are taking the output described in the below section of all the possible types of moves. Line 3 in this block only allows the move to be used if it is legal, line 4 makes sure that it is not attempting the same node as before. Here we are able to see the actual tree being traversed to find the valid moves. Thus, the MoveList from the bottom code is passed over and then output to the screen once this step is reached. Here, we find the base case for the recursion. The output and write sections are what actually print out the results of the query on the screen, as shown in the testing documentation below. In this case, it has the parameters of 3 missionaries and 3 cannibals on the left side, with the goal of 0 cannibals and 0 missionaries on the right side. The main control block is what happens upon the execution of the find command when the Prolog is queried. This section of code is by far the simplest.

missionaries and cannibals game

Furthermore, as this problem is to be solved recursively, it is essential that there is a base case so that the recursive call that will solve the problem will actually succeed. This is the longest section of code, and also the simplest.Ī legal move as a bound must also be defined as well, to prevent the wrong moves from being executed and thus violating the rules of the game. With the ability to construct a knowledge base in Prolog, the list of available moves must be put into the program. Thus, with these constraints in mind, they can be programmed into Prolog fairly simply, and a solution can be searched for.

missionaries and cannibals game

However, there are constraints on the problem, most notably that there can never be more Cannibals than Missionaries on either side of the river as they will be eaten! Missionaries and Cannibals is a notable problem in Artificial Intelligence in which three Missionaries and three Cannibals attempt to cross a river to the other side using a boat.






Missionaries and cannibals game