Creating “PacMan” With AgentCubes Online


Part 2 – Making the Ghost Chase the PacMan



Yüklə 253,32 Kb.
səhifə2/3
tarix14.10.2017
ölçüsü253,32 Kb.
#4760
1   2   3
Part 2 – Making the Ghost Chase the PacMan


So far, your Ghost just moves randomly, either just on the floor, or on the floor and the pellets…he doesn’t actually chase the PacMan, does he? That’s about to change!

The Ghost will intelligently seek the PacMan agent using a computational thinking pattern called “searching.” In this instance, we will use a specific method of searching called Hill Climbing. Imagine the PacMan agent emits a scent. Hill climbing is a procedure or algorithm to find the direction in which the scent is strongest.

The scent will spread out, or be propagated, by the ground agents using a computational thinking pattern called “diffusion.” Diffusion is a fundamental process (physical, biological, and social) by which objects move from areas of highest concentration to areas of lowest concentrations. The closer to the source of the scent, the greater its value.

This phase of the project introduces the concept of an “agent attribute,” which is unique information that is stored within each occurrence of an agent. Computer scientists call this agent attribute a local variable.




Step 1: The best way to initialize PacMan’s S agent attribute is to set it when PacMan is drawn on the world because then PacMan s attribute will always start at the same value.
To do this, create a new Method by clicking on the +Method button. Click on the word “on” in the new method’s black and yellow striped tape and change the label from “on” to “when-creating-new-agent”.
Your when-creating-new-agent method should look as follows:

If you use this method to set PacMan’s S attribute, make sure that you erase and redraw PacMan and then SAVE the World.
Important Note: If you forget to save the world, PacMan may not have any value set for S when the world is reloaded.

Checking the Value of PacMan’s S agent attribute:


  1. Double click on PacMan with the big arrow tool .

  2. You should see this window appear:

  3. If S is not visible in the window, it did not get set to a value yet.

  4. Erase and redraw PacMan and then save the world. Then S should appear in the Attributes window.


Step 2:


Why do we multiply by 0.25?

When you find the average of a set of numbers, you add them up and divide by the number of numbers.

In this case, dividing by 4 is the same as multiplying by ¼ which equals 0.25
Now, since the scent is diffusing, or spreading out, we need to find the average of the scent from the area around a ground agent or a pellet agent. Think of it as the smells are coming in from the North, South, East and West. The smell in the center, then, is the average of these four smells. How will you create that programmatically?
Diffuse the scent using the pellet agents

The pellet agent will have the behavior below; the single action is to calculate and store the average of the four surrounding agents’ agent attributes. Remember, you named the agent attribute “S” (for scent).


The “set” action sets each pellet agent’s attribute “S” to the average of the attributes in the agents above, below, and on each side:

S = 0.25*(s[up]+s[down]+s[right]+s[left])



NOW…diffuse the scent across the ground by adding a rule to the ground agents!
https://638f557e1f-custmedia.vresp.com/eea9a372ab/helpfulhints.jpg


Match both the parentheses “(” and the brackets “[” as shown in the equation.



What do FIRE ALARMS have to do with coding?

A METHOD is a set of rules with a name…rules to follow in a specific situation. These are done when there is a specific call for them…much like the fire alarm means you follow different rules. You can create a METHOD by clicking the +Method button below an agent’s behavior.


Step 3: For the Ghost to know which way to walk, he has to determine where the scent is the strongest. We call this HILL CLIMBING. If this were real life, he would smell up, smell down, smell left and smell right. Wherever the smell was strongest, he would walk in that direction. We need to program the Ghost to do this.
We will create a METHOD for the Ghost to follow a set of rules.
Take a look at the programming below.
The rule in the while running method says

“ONCE EVERY 0.5 seconds, follow the Chase PacMan procedure”.

The rule in the Chase PacMan method says

“IF the smell above you is greater than or equal to any of the other smells in different directions (down, left or right), THEN move up.”




Now, add the three more rules to the Chase Pacman method so that the Ghost knows what to do if the smell down (S[down]) is greater. What if the smell to the left is greater? What about the smell to the right?


Run your game to see if the Ghost chases the PacMan!

If it isn’t working, check the following:



    • Erase and redraw PacMan, then save the world so the initial value of S is saved.

    • In the Chaser’s rules, the method name must be the same in the message action and the black and yellow striped method name tag of the hill climbing method!

    • Use of parentheses “(“ and brackets “[“ in the ground and pellet agent rules must be correct. Check the picture of the ground agent’s equation 2 pages ago and compare it to the equations in your ground and pellet agents.

    • Check your hill climbing rules again and make sure that the arrows in the actions point the correct direction and that the conditions for each rule are correct.





More realistic Ghost catching

Have you noticed how your Ghost will “catch” PacMan when it is next to him, or even diagonal from him. How could you now change your loose condition for when the ghosts catch PacMan to make the Ghosts only “catch” him when they are above him? Give it a try.


Why didn’t this work before you added Hill Climbing? Discuss with a partner.

Shortcut for Hill Climbing

AgentCubes Online has an even better way to handle the process of Hill Climbing. The use of the Hill Climbing action (see rule below) makes future extensions in the game, such as having the ghosts run from the PacMan, easier.


There is a single action, hill climb, that replaces all the rules in the Chase PacMan method.


It is possible to eliminate the Chase PacMan method by simply putting the hill climb action in the rule in the ghost’s while running method.


Test out the options in the hill climb action.

What happens if the ghosts search in 8 directions?

Can PacMan escape?

Would it help to use fewer ghosts?

After you have run your game several times, choose whether your ghosts will search in 4 or 8 directions and decide on the number of ghosts in your game.

Student Handout:

Troubleshooting Guide for Diffusion and Hill Climbing –

Part 1: Tracking the Ghost One Step at a Time

To determine what is happening in your game, it is helpful to look at the agent attributes.



  1. On your world, click run until the ghosts move out of the box, and then click stop.

  2. Running the game briefly made PacMan’s scent diffuse across the world.

  3. Check PacMan’s s attribute by double clicking on him with the big arrow tool .

  4. This window will appear:

  5. Make sure that you erased and redrew PacMan on the world and then SAVED the world.

  6. To see how PacMan’s scent (the value of S) has diffused, double click with the big arrow tool anywhere on the ground or on a pellet.

  7. A window will appear that lists the S attribute value for that agent.

  8. Click around the world. Is S biggest close to PacMan and smaller far away from PacMan? Before you do so, MAKE SURE YOU TEMPORARILY REMOVE THE “once every 0.5” seconds” so you get more immediate feedback.

Check the attributes of the four boxes around the Ghost (up, down, left and right) and then single-step the game using this button so you can see if your Ghost is moving towards the agent with the largest S value.



If the Ghost moves the wrong way or does not move, go back and check your rules in the Chase PacMan method. Compare your rules with a friend’s rules.



  • Are the conditions correct?

  • Are the arrows in the move actions correct?

  • If you used the hill climb action, check that you changed “value” in the hill climb action to S.




Now put the “once every 0.5 seconds” back in so the ghosts don’t move too fast


  1. You can also try erasing all but one of the ghosts to see if it moves towards PacMan. This helps because the Ghosts do not propagate the scent.


Critical thinking question: Why don’t you want the Ghosts to propagate the scent? What would happen if they do?

Part2: Using a plot action to visualize S values:

We can use the plot action to visualize the value of S in the ground agents and the pellets. The plot action will plot the values of S in a 3D surface above the world. The peak on the plot represents the highest value of S in the world. What agent should be under the peak?

Make a new rule at the bottom of PacMan’s while-running method and add the plot action:

To make the plot action work



  1. This rule must be the last rule in PacMan’s while-running method!

  2. Enter “S” for the name of the agent attribute.

  3. Edit the row and column numbers so that values from the entire world are plotted. Remember that the world size for PacMan is 16 rows and 21 columns. Computer scientists always count from 0 to (Number – 1), in this case, rows 0 to 15 and columns 0 to 20. If you changed the default size, you must count from 0 to (New Size -1).

  4. Pick a color for the plot that will show up against your background.

  5. Change the elevation to 1.0.

  6. Make sure to choose “logarithmically” as the plot type. This option works better than plotting linearly when some of the values are quite large and others are very close to zero.

  7. Make a test world that just has a layer of ground agents, a layer of pellets, PacMan and one ghost.

  8. Use the Rotate tool to tilt your world so that it is nearly horizontal and you can see the agents below the plot surface.

  9. Run the game and move PacMan around. The peak, which represents the high value, will follow him. The ghost will move towards the high value (the peak).

  10. Add a couple of rows of wall agents. What happens to the shape of the plot when the walls are added?

  11. The walls have no S value so the plot gets some wrinkles and valleys.

  12. Watch the ghost go around the walls following the increasing S values towards PacMan!

  13. Now run your PacMan world with the complete maze on it and see what happens to the plot of the S values!

  14. You may find that reloading the world does not erase the plot surface. Reload your browser window or click on the AgentCubes Online logo in the upper right corner of the AgentCubes Online window and click the edit button for your project again.



Figure 1. Plot of the S values with peak over PacMan.

Why is there a low spot over the ghost? Does the ghost have an S value?

Figure 2. Plot of the S values with some walls in the world.

PacMan and the ghost are under the peak. Why is there a valley over the wall?

Teacher Instructions

Part 3 – Making the game more sophisticated –

Polling and Broadcast
Background:

In a classroom, when students are working on an assignment, teachers regularly ‘poll’ the room to see if everyone is done yet. A teacher does this by asking students to raise their hand if they are still working. If no one raises a hand, the teacher knows everyone is done. Once everyone is done, the assignment is finished. Students will use this same concept to change their game to make it more challenging.



Introduction to students:

Using the example of the classroom, guide your students through a discussion of how to poll for answers. Now, tell them they are going to use this same concept to change their game. This time, the game looks like this:



In our game, the game ends when PacMan is next to a ghost. Now we need to find a way to win the game when all of the pellets are gone.

Give students a couple of minutes to discuss this programming activity.



Consider these prompts:

  • Who will poll (look to see if there are still more pellets to be collected)

  • What stops the game?

  • What steps (code) will change?

[Give students a minute or two to discuss this with the person next to them. Then solicit their ideas.]

Students will struggle with the idea of who polls. Introduce the idea of a controller, an agent that is responsible for tracking the number of pellets left on the world. Remind the students that they should take time to think through each programming step so they can use these skills later.



Hand out handout 3

No code is provided in the teacher instructions as all code is provided for students.

The student pages can be found on page 11 of the STANDARD packet and page 15 of the ALTERNATIVE packet.




Student Handout 3 Part 3:

Making the game more sophisticated – Polling and Broadcast
In this enhancement to the PacMan project, the PacMan must “eat” all of the pellets in order to win. Polling will tell us when all the pellets are gone and PacMan has won.
Polling uses a simulation property, also called a global variable by computer scientists, which is a piece of information that all agents in the simulation or game may check or set if they have the correct rules. A controller agent does the polling by sending out a message at intervals to all the agents that must be counted. These agents respond by adding one to the simulation property. The controller determines when all the pellet agents are gone and PacMan has won.


The teacher has given an assignment to the class and wants to know if everyone is finished. She says to the class, “Put your hand up if you are still working.” Hands go up. She counts them – there are five students still working. “Okay, put your hands down and keep working.”

A few minutes later, she does it again. She says to the class, “Put your hand up if you are still working.” Hands go up. She counts them – there are two students still working. “Okay, put your hands down and keep working.”

A few minutes later, she does it again. She says to the class, “Put your hand up if you are still working.” This time, no hands go up. “Everyone is done, put your books away.”

That’s what this programming will look like.




Definition: Computer scientists call the process of making a decision by sending a message to multiple recipients and checking responses polling.

The Controller will say, “Pellet count starts at zero” (like the classroom, no hands are up when the teacher asks who is still working).


When the pellets ‘hear’ the Controller ask (broadcast) the question, the pellets respond back (raise their hands).
The Controller counts the pellets. If the answer is more than zero, nothing happens and the game continues. If the answer is zero (meaning that there are no remaining pellets on the board), the game ends.
Step 1: Create the Pellets simulation property as described in the green box below.


How do Simulation Properties Work?

  1. Simulation properties may be added, saved or deleted in the Simulation Properties window.



  1. Open this window by clicking on the gear button on the top right side of the AgentCubes Online window and choosing “Show Simulation Properties”.



  1. Make the Pellets simulation property by clicking on the + button at the bottom of the simulation properties window and typing the name “Pellets”.



  1. Click on the Save button so that the new simulation property is saved!



  1. The value of a simulation property can be changed or checked by any of the agents in the game or world.



  1. All conditions and actions which check or change the value of a simulation property must place an “@” before the simulation property name.





Step 2: Create the Controller agent.

  • Use +Agent to make a Controller agent and choose any image.

  • Place the Controller agent on top of a wall in your PacMan world and SAVE the world!


Step 3: Add a rule to the Controller agent’s while running method.

  1. Set the number of pellets to zero. (this is like the teacher saying “hands down”)

Set @Pellets to zero

  1. Ask the pellets if they are still on the world

Broadcast to Pellet agents to do “Count”

  1. Check the number of pellets to see if the game is done.

Send a Checkwin message to myself.
Step 4: Program the Controller agent’s Checkwin method.

  • If there are no pellets left, tell the player that PacMan won and stop the game.


Step 5: Program the Pellet Agent’s Count method.

  1. Make a new method for the Pellet agent.

  2. Name it Count.

The name must exactly match the name broadcast by the Controller.

  1. Add a rule with an action that sets the value of “@Pellets” to

@Pellets + 1”.

This is how programmers add 1 to a number.


Try setting up these rules now!

Count is not part of the continually running “While Running” method. It must be a separate method since it only runs when called by the controller agent. https://638f557e1f-custmedia.vresp.com/eea9a372ab/helpfulhints.jpg

Check your program:

Here is the Controller agent behavior with the rule in the while running method that makes the pellet agents count themselves and the new Checkwin method that ends the game if PacMan has eaten all the pellets:




Here is the Pellet behavior with the new Count method that allows each Pellet agent to add 1 to the Pellets simulation property:




The Bigger Picture: Communication between Agents

Polling introduces a technique that allows agents to create a complex behavior by cooperating: a particular set of conditions cause one type of agent to send a message to another type of agent to do a named method that contains a special set of rules. Count was the special method in the polling example.

This type of communication between different types of agents can be used to create interesting games. For example, if PacMan eats a power pill, then PacMan can broadcast a message to all ghosts to “Get_Scared”. The Get_Scared method can change the ghosts’ appearance so that they look different as they run away instead of chasing PacMan. Or the Traveler in Journey can fire ice arrows at Chasers. When an ice arrow hits a Chaser, it sends the Chaser agent a message that makes it freeze if it is unfrozen. Frozen chasers cannot move so the Traveler can collect the treasures without being caught by the Chaser.

However, when the goal is just to count up the number of agents and stick the value in a simulation property, AgentCubes Online has a simpler method for the controller to do this:



These two actions have been deleted from the controller’s while running method:

And replaced by this action:

The set action contains a specialized communication between the controller and the Pellet agents, the agents_of_type(“Pellet”) message, which makes the pellets count themselves without the need for us to code a separate count method.

You learned polling so that you would understand how to make different types of agents communicate. But there is usually more than one solution to a programming problem so now you have seen an alternate way to keep track of the number of any kind of agent.


Yüklə 253,32 Kb.

Dostları ilə paylaş:
1   2   3




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə