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

Given World Methods and Functions.

Try playing the game, not much happens yet. Try typing S. Try clicking on players.

Methods and Functions to Implement

  1. 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.

  2. 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.

  3. 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.

  4. 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).

  5. 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).

  6. 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.