Skip to content

Commit

Permalink
Added players. Updated board accordingly.
Browse files Browse the repository at this point in the history
  • Loading branch information
obi1kenobi committed Sep 23, 2012
1 parent 27e7102 commit ee48319
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 26 deletions.
55 changes: 31 additions & 24 deletions Mazebuilder/src/com/mazebuilder/gameplay/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,38 @@
import com.mazebuilder.renderer.Renderable;

public interface Board extends Renderable {

/** Number of tiles in the horizontal board direction. **/
int tilesAcross();

/** Number of tiles in the vertical board direction. **/
int tilesDown();

/** Returns the location of the given player. Uses identity (==) comparisons. **/
Location getPlayerLocation(Player p);

/**
* Returns the new location of the given player. Will return the same location if the move is impossible.
* Does not jump walls. Uses identity (==) comparisons.
**/
Location movePlayer(Player p, Direction d);

/**

/** Number of tiles in the horizontal board direction. **/
int tilesAcross();

/** Number of tiles in the vertical board direction. **/
int tilesDown();

/** Returns the location of the given player. Uses identity (==) comparisons. **/
Location getPlayerLocation(Player p);

/**
* Returns the new location of the given player. Will return the same location if the move is impossible. Does not jump walls. Uses identity (==)
* comparisons.
**/
Location movePlayer(Player p, Direction d);

/**
* Returns the new location of the given player. Will return the same location if the move is impossible. Does not jump walls. Uses identity (==)
* comparisons.
**/
Location movePlayerWithBonus(Player p, Direction d);

/**
* Returns the new location of the given player. Will return the same location if the move is impossible (player cannot jump or no wall to jump).
* Uses identity (==) comparisons.
**/
Location jumpPlayer(Player p, Direction d, Direction... bonusesToSpend);

/**
* Sets a wall on the given tile, in the direction given. Returns true if the wall was successfully set, false otherwise (if a wall is already there).
*/
boolean putWall(Location l, Direction d);

Location jumpPlayer(Player p, Direction d, Direction... bonusesToSpend);

/**
* Sets a wall on the given tile, in the direction given. Returns true if the wall was successfully set, false otherwise (if a wall is already
* there).
*/
boolean putWall(Location l, Direction d);

}
89 changes: 89 additions & 0 deletions Mazebuilder/src/com/mazebuilder/gameplay/ChaserPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.mazebuilder.gameplay;

import java.util.Random;

import org.newdawn.slick.Graphics;

import com.google.common.collect.EnumMultiset;
import com.google.common.collect.Multiset;
import com.google.common.collect.Multisets;
import com.mazebuilder.renderer.PlayerRenderer;

public final class ChaserPlayer implements Player {

private final PlayerRenderer renderer;
private final String name;
private final Multiset<Direction> bonuses;
private final Random rand;

private int turnsToBonus = BONUS_INTERVAL;

private static final boolean BONUSES_EQUAL = true;
private static final int BONUS_INTERVAL = 2;
private static final int BONUSES_TO_JUMP = 2;

public ChaserPlayer(PlayerRenderer renderer, String name) {
this.renderer = renderer;
this.name = name;
this.bonuses = EnumMultiset.create(Direction.class);
this.rand = new Random();
}

@Override
public void render(Graphics g, int xOffset, int yOffset) {
renderer.drawPlayer(g, xOffset, yOffset);
}

@Override
public String getName() {
return name;
}

@Override
public boolean canJump() {
return true;
}

@Override
public void executeTurn() {
turnsToBonus--;
if (turnsToBonus == 0) {
turnsToBonus = BONUS_INTERVAL;
switch (rand.nextInt(4)) {
case 0:
bonuses.add(Direction.LEFT);
break;
case 1:
bonuses.add(Direction.UP);
break;
case 2:
bonuses.add(Direction.RIGHT);
break;
case 3:
bonuses.add(Direction.DOWN);
break;
}
}
}

@Override
public Multiset<Direction> getBonuses() {
return Multisets.unmodifiableMultiset(bonuses);
}

@Override
public int bonusesToJump() {
return BONUSES_TO_JUMP;
}

@Override
public boolean bonusesEqual() {
return BONUSES_EQUAL;
}

@Override
public boolean spendBonus(Direction d) {
return bonuses.remove(d);
}

}
15 changes: 15 additions & 0 deletions Mazebuilder/src/com/mazebuilder/gameplay/DefaultBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.newdawn.slick.Graphics;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.mazebuilder.renderer.BoardRenderer;

Expand Down Expand Up @@ -112,6 +113,20 @@ public Location movePlayer(Player p, Direction d) {
return l;
}

@Override
public Location movePlayerWithBonus(Player p, Direction d) {
Preconditions.checkArgument(p.canJump(), "Player " + Strings.nullToEmpty(p.getName()) + " cannot jump.");
Location l = getPlayerLocation(p);
if (!walls.isWall(l, d)) {
Location moved = l.move(d);
if (isBoardLocation(moved) && p.spendBonus(d)) {
l = moved;
players.put(p, l);
}
}
return l;
}

@Override
public Location jumpPlayer(Player p, Direction d, Direction... bonusesToSpend) {
Preconditions.checkArgument(p.canJump(), "Player " + p.getName() + " cannot jump walls.");
Expand Down
4 changes: 2 additions & 2 deletions Mazebuilder/src/com/mazebuilder/gameplay/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface Player extends Renderable {
boolean canJump();

/** Should be called every turn -- will handle matters such as generating movement bonuses etc. **/
boolean executeTurn();
void executeTurn();

/** Returns the set of bonuses for the player. **/
Multiset<Direction> getBonuses();
Expand All @@ -21,7 +21,7 @@ public interface Player extends Renderable {
/** Returns true if the bonuses spent to jump should be equal, or false otherwise. **/
boolean bonusesEqual();

/** Removes a bonus from the player. **/
/** Removes a bonus from the player. Returns true if the bonus was successfully removed, or false otherwise. **/
boolean spendBonus(Direction d);

}
61 changes: 61 additions & 0 deletions Mazebuilder/src/com/mazebuilder/gameplay/RunnerPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.mazebuilder.gameplay;

import org.newdawn.slick.Graphics;

import com.google.common.collect.EnumMultiset;
import com.google.common.collect.Multiset;
import com.mazebuilder.renderer.PlayerRenderer;

public final class RunnerPlayer implements Player {

private final PlayerRenderer renderer;
private final String name;
private final Multiset<Direction> bonuses;

public RunnerPlayer(PlayerRenderer renderer, String name) {
this.renderer = renderer;
this.name = name;
this.bonuses = EnumMultiset.create(Direction.class);
}

@Override
public void render(Graphics g, int xOffset, int yOffset) {
renderer.drawPlayer(g, xOffset, yOffset);
}

@Override
public String getName() {
return name;
}

@Override
public boolean canJump() {
return false;
}

@Override
public void executeTurn() {
// no-op
}

@Override
public Multiset<Direction> getBonuses() {
return bonuses;
}

@Override
public int bonusesToJump() {
return Integer.MAX_VALUE;
}

@Override
public boolean bonusesEqual() {
return true;
}

@Override
public boolean spendBonus(Direction d) {
throw new RuntimeException("Cannot spend bonus - RunnerPlayer is not allowed to have bonuses.");
}

}

0 comments on commit ee48319

Please sign in to comment.