Skip to content

Commit

Permalink
Extracting the score and stats to a Scoreboard class. Refined the end…
Browse files Browse the repository at this point in the history
… game dialog to show the stats of the game.
  • Loading branch information
Sebastian Hermida committed May 11, 2011
1 parent 51e497b commit f749f60
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 72 deletions.
44 changes: 11 additions & 33 deletions src/com/happyprog/pairhero/game/Game.java
Expand Up @@ -7,32 +7,23 @@

public class Game {

public static final int _1X_MULTIPLIER = 1;
public static final int _2_MULTIPLIER = 2;
public static final int _4X_MULTIPLIER = 4;

public static final int GREEN_TEST_POINTS = 10;
public static final int REFACTORING_POINTS = 2;
public static final int ROLE_SWITCH_POINTS = 1;

private int score;
private int timeSinceSwitch;

private final Timer timer;
private final MainView view;
private final Programmer leftProgrammer;
private final Programmer rightProgrammer;
private final JUnitSubscriber testSubscriber;
private final RefactoringSubscriber refactoringSubscriber;
private final Scoreboard scoreboard;

public Game(MainView view, Timer timer, Programmer leftProgrammer, Programmer rightProgrammer,
JUnitSubscriber testSubscriber, RefactoringSubscriber refactoringSubscriber) {
JUnitSubscriber testSubscriber, RefactoringSubscriber refactoringSubscriber, Scoreboard scoreboard) {
this.view = view;
this.timer = timer;
this.leftProgrammer = leftProgrammer;
this.rightProgrammer = rightProgrammer;
this.testSubscriber = testSubscriber;
this.refactoringSubscriber = refactoringSubscriber;
this.scoreboard = scoreboard;
}

public void start() {
Expand All @@ -55,43 +46,30 @@ public void onTimeChange(int seconds) {
view.onTimeChange(seconds);
leftProgrammer.onTimeChange();
rightProgrammer.onTimeChange();
timeSinceSwitch++;
scoreboard.onTimeChange();

if (seconds <= 0) {
stop();
view.onGameFinished("Awesome!");
view.onGameFinished();
return;
}
}

public void onSwitchRole() {
score += ROLE_SWITCH_POINTS;

int multiplier = getMultiplier();
score *= multiplier;

leftProgrammer.switchRole();
rightProgrammer.switchRole();
view.onSwitchRole(score, multiplier);
scoreboard.addSwitch();
view.onSwitchRole();
}

public void onGreenTest() {
score += GREEN_TEST_POINTS;
view.onGreenTest(score);
scoreboard.addGreenTest();
view.onGreenTest();
}

public void onRefactoring() {
score += REFACTORING_POINTS;
view.onRefactoring(score);
}

private int getMultiplier() {
if (timeSinceSwitch < 30) {
return _4X_MULTIPLIER;
} else if (timeSinceSwitch >= 30 && timeSinceSwitch < 120) {
return _2_MULTIPLIER;
}
return _1X_MULTIPLIER;
scoreboard.addRefactoring();
view.onRefactoring();
}

}
86 changes: 86 additions & 0 deletions src/com/happyprog/pairhero/game/Scoreboard.java
@@ -0,0 +1,86 @@
package com.happyprog.pairhero.game;

public class Scoreboard {

public static final int GREEN_TEST_POINTS = 10;
public static final int REFACTORING_POINTS = 2;
public static final int SWITCHING_POINTS = 1;
public static final int MULTIPLIER_2X = 2;
public static final int MULTIPLIER_4X = 4;
public static final int MULTIPLIER_1X = 1;

private int score;
private int secondsSinceLastSwitch;

private int greenTestsCounter;
private int refactoringCounter;
private int lastMultiplier;
private int _4xMultiplierCounter;
private int _2xMultiplierCounter;
private int _1xMultiplierCounter;

public void addGreenTest() {
score += GREEN_TEST_POINTS;
greenTestsCounter++;
}

public int getScore() {
return score;
}

public void addRefactoring() {
score += REFACTORING_POINTS;
refactoringCounter++;
}

public void addSwitch() {
score += SWITCHING_POINTS;

if (secondsSinceLastSwitch <= 30) {
score *= 4;
lastMultiplier = MULTIPLIER_4X;
_4xMultiplierCounter++;
} else if (secondsSinceLastSwitch <= 60) {
score *= 2;
lastMultiplier = MULTIPLIER_2X;
_2xMultiplierCounter++;
} else {
lastMultiplier = MULTIPLIER_1X;
_1xMultiplierCounter++;
}

secondsSinceLastSwitch = 0;
}

public int getNumberOfGreenTests() {
return greenTestsCounter;
}

public int getNumberOfRefactorings() {
return refactoringCounter;
}

public void onTimeChange() {
secondsSinceLastSwitch++;
}

public int getSecondsSinceLastSwitch() {
return secondsSinceLastSwitch;
}

public int getLastMultiplier() {
return lastMultiplier;
}

public int getNumberOf4xMultipliers() {
return _4xMultiplierCounter;
}

public int getNumberOf2xMultipliers() {
return _2xMultiplierCounter;
}

public int getNumberOf1xMultipliers() {
return _1xMultiplierCounter;
}
}
2 changes: 1 addition & 1 deletion src/com/happyprog/pairhero/time/Timer.java
Expand Up @@ -7,7 +7,7 @@
public class Timer implements Runnable {

private static final int ONE_SECOND = 1000;
public static final int _25_MINS = 5;// 1500;
public static final int _25_MINS = 1500;

private Game game;
private int countdownInSeconds = _25_MINS;
Expand Down
30 changes: 23 additions & 7 deletions src/com/happyprog/pairhero/views/EndDialog.java
Expand Up @@ -13,14 +13,15 @@
import org.eclipse.swt.widgets.Shell;

import com.happyprog.pairhero.Activator;
import com.happyprog.pairhero.game.Scoreboard;

public class EndDialog extends Dialog {

private final String message;
private final Scoreboard scoreboard;

public EndDialog(Shell parentShell, String message) {
public EndDialog(Shell parentShell, Scoreboard scoreboard) {
super(parentShell);
this.message = message;
this.scoreboard = scoreboard;
}

@Override
Expand All @@ -31,7 +32,7 @@ protected Control createDialogArea(Composite parent) {
composite.setLayout(layout);

createLogo(composite);
createLabel(composite);
createStats(composite);
createButton(composite);

return composite;
Expand All @@ -41,9 +42,24 @@ private void createLogo(Composite composite) {
new Label(composite, SWT.NONE).setImage(Activator.getDefault().getImageFromKey("times-up"));
}

private void createLabel(Composite composite) {
Label label = new Label(composite, SWT.NONE);
label.setText(message);
private void createStats(Composite composite) {
Composite group = new Composite(composite, SWT.NONE);
RowLayout layout = new RowLayout();
layout.type = SWT.VERTICAL;
group.setLayout(layout);

new Label(group, SWT.NONE).setText(String.format("Your score = %d", scoreboard.getScore()));

new Label(group, SWT.NONE).setImage(Activator.getDefault().getImageFromKey("div-bar"));

new Label(group, SWT.NONE).setText(String.format("Passed Tests = %d", scoreboard.getNumberOfGreenTests()));
new Label(group, SWT.NONE).setText(String.format("Refactorings = %d", scoreboard.getNumberOfRefactorings()));

new Label(group, SWT.NONE).setImage(Activator.getDefault().getImageFromKey("div-bar"));

new Label(group, SWT.NONE).setText(String.format("4x Multipliers = %d", scoreboard.getNumberOf4xMultipliers()));
new Label(group, SWT.NONE).setText(String.format("2x Multipliers = %d", scoreboard.getNumberOf2xMultipliers()));
new Label(group, SWT.NONE).setText(String.format("1x Multipliers = %d", scoreboard.getNumberOf1xMultipliers()));
}

private void createButton(Composite composite) {
Expand Down
35 changes: 19 additions & 16 deletions src/com/happyprog/pairhero/views/MainView.java
Expand Up @@ -18,6 +18,7 @@
import com.happyprog.pairhero.actions.StopAction;
import com.happyprog.pairhero.game.Game;
import com.happyprog.pairhero.game.Programmer;
import com.happyprog.pairhero.game.Scoreboard;
import com.happyprog.pairhero.subscribers.JUnitSubscriber;
import com.happyprog.pairhero.subscribers.RefactoringSubscriber;
import com.happyprog.pairhero.time.TimeFormatter;
Expand All @@ -43,25 +44,26 @@ public class MainView extends ViewPart {

private StopAction stopButton;

private Scoreboard scoreboard;

@Override
public void createPartControl(Composite parent) {
this.parent = parent;
parent.setLayout(createLayout());
createStartButton();
leftProgrammer = new Programmer(parent);
rightProgrammer = new Programmer(parent);
createScoreboard(parent);
scoreboard = new Scoreboard();
createScoreArea(parent);

parent.layout();
}

private void createScoreboard(Composite parent) {
private void createScoreArea(Composite parent) {
Composite group = new Composite(parent, SWT.BORDER);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
group.setLayout(layout);
// group.setBackground(new Color(PlatformUI.getWorkbench().getDisplay(),
// 255, 255, 255));

new Label(group, SWT.NONE).setText("Score:");
scoreLabel = new Label(group, SWT.NONE);
Expand Down Expand Up @@ -109,7 +111,7 @@ public void onStart() {

private void startGame() {
game = new Game(this, new Timer(), leftProgrammer, rightProgrammer, new JUnitSubscriber(),
new RefactoringSubscriber());
new RefactoringSubscriber(), scoreboard);
game.start();
}

Expand Down Expand Up @@ -137,8 +139,8 @@ public void setFocus() {

}

public void onGameFinished(String message) {
EndDialog dialog = new EndDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), message);
public void onGameFinished() {
EndDialog dialog = new EndDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), scoreboard);
dialog.open();
startButton.setEnabled(true);
stopButton.setEnabled(false);
Expand Down Expand Up @@ -170,16 +172,16 @@ public void onStop() {
}
}

public void onSwitchRole(int score, int multiplier) {
showMessageAndUpdateScore(getSwitchRoleImage(multiplier), score);
public void onSwitchRole() {
showMessageAndUpdateScore(getSwitchRoleImage(), scoreboard.getScore());
}

public void onRefactoring(int score) {
showMessageAndUpdateScore("refactoring", score);
public void onRefactoring() {
showMessageAndUpdateScore("refactoring", scoreboard.getScore());
}

public void onGreenTest(int score) {
showMessageAndUpdateScore("green", score);
public void onGreenTest() {
showMessageAndUpdateScore("green", scoreboard.getScore());
}

private void showMessageAndUpdateScore(String imageKey, int score) {
Expand Down Expand Up @@ -208,10 +210,11 @@ public void run() {
});
}

private String getSwitchRoleImage(int multiplier) {
if (multiplier == Game._4X_MULTIPLIER) {
private String getSwitchRoleImage() {
int multiplier = scoreboard.getLastMultiplier();
if (multiplier == Scoreboard.MULTIPLIER_4X) {
return "switch-4x";
} else if (multiplier == Game._2_MULTIPLIER) {
} else if (multiplier == Scoreboard.MULTIPLIER_2X) {
return "switch-2x";
} else {
return "switch";
Expand Down

0 comments on commit f749f60

Please sign in to comment.