CompSci 4 - Classwork - Oct 27, 2005
5 pts
Today's classwork is Problem Solving - GateKeeper.
Start by downloading the Alice world GateKeeperSetup.a2w.
This world has four players (animals), a barn, and four invisible
tennisballs that mark the positions where players stand.
The GateKeeper problem is the following. There are four players facing
you. One of them has randomly been assigned to be the GateKeeper. When you
click on a player it should swap places with the player that is furthest
away. When the GateKeeper is in the far right position, the game is over.
Setup
- The tennisballs indicate where players should stand and should not be
moved. The balls are beside the current players and are (from left to
right),
tennisBall, tennisBall1, tennisBall2 and tennisBall3.
- Each player has been given a new property called "IsGateKeeper" that
is initially false.
Given World Methods and Functions.
- world.setup - This method runs when the world starts. It calls
SetGatekeeper (which doesn't work yet) and then checks to see if the world is over.
- world.showAnswer - This event handler has the barn tell you which
player
has been set to the gateKeeper. When you press S, this event handler is
invoked.
- world.isPlayer - returns true if the object clicked on is a player.
- world.randomInteger1toMax - This method when given an argument X,
returns
an integer value from 1 to X inclusive. The random method in Alice is
a bit buggy and doesn't always return an integer, so this function was
written
to make sure it returns an integer. Use it to generate a random integer.
- world.runGame - This is an event handler that is invoked whenever you
click on a player. It swaps the player with the player furthest away by
calling swap (which is not implemented yet) and then checks to see if the
game is over.
Try playing the game, not much happens yet. Try typing S. Try clicking on
players.
Methods and Functions to Implement
- Write the method world.setGatekeeper. This method should randomly
assign
one of the players to be the gatekeeper (that is to set one of the players'
IsGateKeeper variable to true).
Test this method by writing a test method that prints out the values of
all four players IsGateKeeper variable. One should be true after it is set.
- Write the function world.whichPlayerIsGateKeeper that returns the
player
that is the GateKeeper. This method assumes that one of the players has
been
assigned to be the GateKeeper.
This method is invoked each time you click on a player, so you can test
it by just clicking on players.
- Write the function world.isGameOver. This function returns true if the
GateKeeper is in the far right position (which would be a distance < .15
meters
from tennisBall4).
Test this method by playing it repeatedly until the rightmost player
is
randomly assigned as the GateKeeper, then the game should be over before it
starts, so a message should be displayed.
- Write the function world.WhichPlayerNearBall that has a ball
parameter,
and it returns the player that is at that ball ( that is, the player
that is < .15m to the ball).
- Write the function world.WhichBallNearPlayer that has a player parameter,
and returns the ball that is at that player (that is, the ball that is
< .15m to the ball).
- Write the world.swap method. Given a player as a parameter, it
swaps the player with the player that is furthest away. You must use the
two previous functions that you just wrote.
Note that given a player, you can find out which ball it is closest to.
If that ball is tennisBall, then you need to swap the player that is
closest
to tennisBall4.
After implementing this method, the game should work! Test it out.
If you finish early, feel free to embelish your animations.