Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conformed Bystander directory to Java conventions. #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/main/java/chapter11/bystander/Bystander.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package chapter11.bystander;

public class Bystander extends RightShoe
{
public void learn(Dance dance)
{
public class Bystander extends RightShoe {
@Override
public void learn(Dance dance) {
setSteps("-1 -7 ");
}
}
10 changes: 4 additions & 6 deletions src/main/java/chapter11/bystander/BystanderRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;

public class BystanderRunner extends CongaNumber
{
public class BystanderRunner extends CongaNumber {
@Override
public void setUpDancers(ActorWorld world)
{
public void setUpDancers(ActorWorld world) {
super.setUpDancers(world);
world.add(new Location(0, 9), new Bystander());
}
public static void main(String[] args)
{

public static void main(String[] args) {
DanceNumber number = new BystanderRunner();
number.run(new ActorWorld(), new Conga());
}
Expand Down
44 changes: 25 additions & 19 deletions src/main/java/chapter11/bystander/Conga.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package chapter11.bystander;

public class Conga implements Dance
{
private static String[] steps =
{
"00 -- 00 -- -- 77 -- 02 37 -- 40 -- -- 37 -- 41 ",
"-- 00 -- 07 00 -- 01 -- -- 40 -- 47 40 -- 41 -- ",
"00 -- 00 -- -- 77 -- 02 37 -- 40 -- -- 37 -- 41 ",
"-- 00 -- 07 00 -- 01 -- -- 40 -- 47 40 -- 41 -- ",
"00 -- 01 -- -- 37 -- 40 00 -- 07 -- -- 51 -- 40 ",
"60 -- 60 -- -- 60 -- 64 60 -- 60 -- -- 60 -- 64 ",
};
public class Conga implements Dance {
private static String[] steps =
{
"00 -- 00 -- -- 77 -- 02 37 -- 40 -- -- 37 -- 41 ",
"-- 00 -- 07 00 -- 01 -- -- 40 -- 47 40 -- 41 -- ",
"00 -- 00 -- -- 77 -- 02 37 -- 40 -- -- 37 -- 41 ",
"-- 00 -- 07 00 -- 01 -- -- 40 -- 47 40 -- 41 -- ",
"00 -- 01 -- -- 37 -- 40 00 -- 07 -- -- 51 -- 40 ",
"60 -- 60 -- -- 60 -- 64 60 -- 60 -- -- 60 -- 64 ",
};

private static int[] beat = {2, 2, 1, 1};
private static int[] beat = {2, 2, 1, 1};

public String getName()
{
return "Conga";
}
@Override
public String getName() {
return "Conga";
}

public String getSteps(int m) { return steps[m-1]; }
public int[] getBeat() { return beat; }
}
@Override
public String getSteps(int m) {
return steps[m - 1];
}

@Override
public int[] getBeat() {
return beat;
}
}
39 changes: 19 additions & 20 deletions src/main/java/chapter11/bystander/CongaNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@

import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;

import java.awt.Color;

public class CongaNumber extends DanceNumber
{
public void setUpDancers(ActorWorld world)
{
super.setUpDancers(world);
public class CongaNumber extends DanceNumber {
@Override
public void setUpDancers(ActorWorld world) {
super.setUpDancers(world);

world.add(new Location(5, 3), new LeftSandal());
world.add(new Location(5, 4), new RightSandal());
world.add(new Location(5, 6), new LeftShoe());
world.add(new Location(5, 7), new RightShoe());
world.add(new Location(7, 2), new DancingBug(Color.YELLOW));
world.add(new Location(7, 4), new DancingBug(Color.GREEN));
world.add(new Location(7, 6), new DancingBug(Color.YELLOW));
world.add(new Location(7, 8), new DancingBug());
world.add(new Location(8, 6), new DancingCrab());
}
world.add(new Location(5, 3), new LeftSandal());
world.add(new Location(5, 4), new RightSandal());
world.add(new Location(5, 6), new LeftShoe());
world.add(new Location(5, 7), new RightShoe());
world.add(new Location(7, 2), new DancingBug(Color.YELLOW));
world.add(new Location(7, 4), new DancingBug(Color.GREEN));
world.add(new Location(7, 6), new DancingBug(Color.YELLOW));
world.add(new Location(7, 8), new DancingBug());
world.add(new Location(8, 6), new DancingCrab());
}

public static void main(String[] args)
{
DanceNumber number = new CongaNumber();
number.run(new ActorWorld(), new Conga());
}
public static void main(String[] args) {
DanceNumber number = new CongaNumber();
number.run(new ActorWorld(), new Conga());
}
}
11 changes: 6 additions & 5 deletions src/main/java/chapter11/bystander/Dance.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package chapter11.bystander;

public interface Dance
{
String getName();
String getSteps(int m);
int[] getBeat();
public interface Dance {
String getName();

String getSteps(int m);

int[] getBeat();
}
51 changes: 23 additions & 28 deletions src/main/java/chapter11/bystander/DanceNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,31 @@
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

public class DanceNumber
{
public void setUpDancers(ActorWorld world)
{
world.add(new Location(0, 0), new Maracas());
}
public class DanceNumber {
public void setUpDancers(ActorWorld world) {
world.add(new Location(0, 0), new Maracas());
}

public void teach(ActorWorld world, Dance dance)
{
world.setMessage(dance.getName());
public void teach(ActorWorld world, Dance dance) {
world.setMessage(dance.getName());

Grid<Actor> gr = world.getGrid();
for (Location loc : gr.getOccupiedLocations())
{
Actor a = gr.get(loc);
if (a instanceof Dancer)
((Dancer)a).learn(dance);
Grid<Actor> gr = world.getGrid();
for (Location loc : gr.getOccupiedLocations()) {
Actor a = gr.get(loc);
if (a instanceof Dancer) {
((Dancer) a).learn(dance);
}
}
}
}

public void run(ActorWorld world, Dance dance)
{
setUpDancers(world);
teach(world, dance);
world.show();
}
public void run(ActorWorld world, Dance dance) {
setUpDancers(world);
teach(world, dance);
world.show();
}

public static void main(String[] args)
{
DanceNumber number = new DanceNumber();
number.run(new ActorWorld(), new MusicOnly());
}
}
public static void main(String[] args) {
DanceNumber number = new DanceNumber();
number.run(new ActorWorld(), new MusicOnly());
}
}
22 changes: 8 additions & 14 deletions src/main/java/chapter11/bystander/DanceRunner.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package chapter11.bystander;

public class DanceRunner
{
public static void main(String[] args)
{
int k = (int)(3 * Math.random());
switch (k)
{
case 0:
WaltzNumber.main(args); break;
case 1:
YMCANumber.main(args); break;
case 2:
CongaNumber.main(args); break;
public class DanceRunner {
public static void main(String[] args) {
int k = (int) (3 * Math.random());
switch (k) {
case 0 -> WaltzNumber.main(args);
case 1 -> YMCANumber.main(args);
case 2 -> CongaNumber.main(args);
}
}
}
}
92 changes: 43 additions & 49 deletions src/main/java/chapter11/bystander/Dancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,55 @@

import info.gridworld.actor.Actor;
import info.gridworld.grid.Location;

import java.awt.Color;

public abstract class Dancer extends Actor
{
private String steps;
private int stepsCount;

public Dancer()
{
setColor(null);
}

public Dancer(Color color)
{
setColor(color);
}

public void setSteps(String steps)
{
this.steps = steps;
stepsCount = 0;
}

public void act()
{
if (getGrid() != null && steps != null)
{
takeStep(steps.substring(stepsCount, stepsCount + 3));
stepsCount += 3;
if (stepsCount >= steps.length() - 2)
public abstract class Dancer extends Actor {
private String steps;
private int stepsCount;

public Dancer() {
setColor(null);
}

public Dancer(Color color) {
setColor(color);
}

public void setSteps(String steps) {
this.steps = steps;
stepsCount = 0;
}
}

private void takeStep(String step)
{
char d = step.charAt(0);
if (Character.isDigit(d))
{
int dir = Character.digit(d, 10) * 45;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection() + dir);
if (getGrid().isValid(next))
moveTo(next);
else
removeSelfFromGrid();

public void act() {
if (getGrid() != null && steps != null) {
takeStep(steps.substring(stepsCount, stepsCount + 3));
stepsCount += 3;
if (stepsCount >= steps.length() - 2) {
stepsCount = 0;
}
}
}

d = step.charAt(1);
if (Character.isDigit(d))
{
int dir = Character.digit(d, 10) * 45;
setDirection(getDirection() + dir);
private void takeStep(String step) {
char d = step.charAt(0);
if (Character.isDigit(d)) {
int dir = Character.digit(d, 10) * 45;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection() + dir);
if (getGrid().isValid(next)) {
moveTo(next);
} else {
removeSelfFromGrid();
}
}

d = step.charAt(1);
if (Character.isDigit(d)) {
int dir = Character.digit(d, 10) * 45;
setDirection(getDirection() + dir);
}
}
}

public abstract void learn(Dance dance);
public abstract void learn(Dance dance);
}
22 changes: 9 additions & 13 deletions src/main/java/chapter11/bystander/DancingBug.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

import java.awt.Color;

public class DancingBug extends Dancer
{
public DancingBug()
{
}
public class DancingBug extends Dancer {
public DancingBug() {}

public DancingBug(Color color)
{
super(color);
}
public DancingBug(Color color) {
super(color);
}

public void learn(Dance dance)
{
setSteps(dance.getSteps(5));
}
@Override
public void learn(Dance dance) {
setSteps(dance.getSteps(5));
}
}
11 changes: 5 additions & 6 deletions src/main/java/chapter11/bystander/DancingCrab.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package chapter11.bystander;

public class DancingCrab extends Dancer
{
public void learn(Dance dance)
{
setSteps(dance.getSteps(6));
}
public class DancingCrab extends Dancer {
@Override
public void learn(Dance dance) {
setSteps(dance.getSteps(6));
}
}
Loading