Skip to content

Commit

Permalink
[#76] Code review 1:
Browse files Browse the repository at this point in the history
  * Scorer.ScoreListerner replaced GameListener.onScoreChange().
  * Added GameLogic.init()
  * ScoreUI moved out of SwingChemistryLines.
  • Loading branch information
soulne4ny committed Dec 18, 2012
1 parent 8de695a commit 6465737
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 36 deletions.
6 changes: 4 additions & 2 deletions Lib/src/com/yarcat/chemistrylines/game/GameLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public interface GameListener {
/** Called on change on the game field */
public void onFieldChange(GameLogic game);

/** Called on change on the game field */
public void onScoreChange(GameLogic game);
/** Called once before game starts. */
public void onInit(GameLogic game);
}

/**
Expand Down Expand Up @@ -70,4 +70,6 @@ public interface GameListener {
public Scorer getScorer();

public void setChangeListener(GameListener listener);

public void init();
}
16 changes: 9 additions & 7 deletions Lib/src/com/yarcat/chemistrylines/game/LinesGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class LinesGame implements GameLogic {
private final int mNewPortionSize;
private final CompoundScanner mScanner;
private final Scorer mScorer;
private GameListener mChangeListener;
private GameListener mGameListener;
private FieldCleaner mFieldCleaner;
private GameLogger mGameLog;

Expand Down Expand Up @@ -93,7 +93,7 @@ private void addMoreItems() {
}

private void onFieldChange() {
mChangeListener.onFieldChange(this);
mGameListener.onFieldChange(this);
}

private void onElementsAdded(int[] addedCells) {
Expand Down Expand Up @@ -124,7 +124,7 @@ public FieldCleaner getFieldCleaner() {
@Override
public void foundCompound(CompoundReference ref) {
mGameLog.compoundRemoved(mField, ref);
updateScore(ref);
mScorer.update(ref);
}
};

Expand All @@ -141,16 +141,18 @@ public Field getField() {

@Override
public void setChangeListener(GameListener listener) {
mChangeListener = listener;
mGameListener = listener;
}

@Override
public Scorer getScorer() {
return mScorer;
}

private void updateScore(CompoundReference ref) {
mScorer.update(ref);
mChangeListener.onScoreChange(this);
@Override
public void init() {
addItems();
mScorer.init();
mGameListener.onInit(this);
}
}
48 changes: 41 additions & 7 deletions Lib/src/com/yarcat/chemistrylines/game/Scorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,35 @@ public interface Scorer {

public void update(CompoundReference ref);

abstract class BaseInt implements Scorer {
public void init();

public interface ScoreListener {
public void onScoreChange(Scorer scorer);
}

public void setScoreListener(ScoreListener l);

abstract class Base implements Scorer {
ScoreListener mListener;

@Override
public void setScoreListener(ScoreListener l) {
mListener = l;
}

void onScoreChange() {
if (mListener != null) {
mListener.onScoreChange(this);
}
}

@Override
public void init() {
onScoreChange();
}
}

abstract class BaseInt extends Base {
int mScore;

public BaseInt() {
Expand All @@ -26,7 +54,7 @@ public String get() {
}
}

abstract class BaseFloat implements Scorer {
abstract class BaseFloat extends Base {
float mScore;

public BaseFloat() {
Expand All @@ -45,6 +73,7 @@ class AtomScore extends BaseInt {
@Override
public void update(CompoundReference ref) {
mScore += ref.getCompound().atomCount();
onScoreChange();
}
}

Expand All @@ -54,6 +83,7 @@ class AtomExpScore extends BaseInt {
public void update(CompoundReference ref) {
int n = ref.getCompound().atomCount();
mScore += n < 4 ? 1 : 1 << (n - 3);
onScoreChange();
}
}

Expand All @@ -62,10 +92,11 @@ class AtomicWeightScore extends BaseFloat {
@Override
public void update(CompoundReference ref) {
mScore += ref.getCompound().atomicWeight();
onScoreChange();
}
}

class ScoreContainer implements Scorer {
class ScoreContainer extends Base {

Map<String, Scorer> mContents;

Expand All @@ -78,18 +109,21 @@ public ScoreContainer() {

@Override
public String get() {
String r="";
for (String name: mContents.keySet()) {
r = String.format("%s%s\t%s\n", r, name, mContents.get(name).get());
String r = "";
for (String name : mContents.keySet()) {
r =
String.format("%s%s\t%s\n", r, name, mContents
.get(name).get());
}
return r;
}

@Override
public void update(CompoundReference ref) {
for(Scorer s : mContents.values()) {
for (Scorer s : mContents.values()) {
s.update(ref);
}
onScoreChange();
}
}
}
18 changes: 4 additions & 14 deletions Swing/src/com/yarcat/chemistrylines/swing/SwingChemistryLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,25 @@
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JTextArea;

import com.yarcat.chemistrylines.game.GameLogic;
import com.yarcat.chemistrylines.game.GameLogic.GameListener;
import com.yarcat.chemistrylines.game.GameLogic.InvalidMove;
import com.yarcat.chemistrylines.swing.SwingField.FieldButton;
import com.yarcat.chemistrylines.view.SelectionInView;

class SwingChemistryLines implements MouseListener, GameListener {
class SwingChemistryLines implements GameListener, MouseListener {

private final SwingField mFieldUI;
private final GameLogic mGame;
private final JTextArea mScoreUI;
private SwingPreview mPreviewUI;
private DefferedCleanerUI mCleanerUI;

public SwingChemistryLines(GameLogic game, SwingField fieldUI,
SwingPreview previewUI, JTextArea scoreUI) {
SwingPreview previewUI) {
mFieldUI = fieldUI;
mGame = game;
mPreviewUI = previewUI;
mCleanerUI = null;
mScoreUI = scoreUI;
}

void refresh() {
Expand All @@ -34,12 +30,6 @@ void refresh() {
if (mCleanerUI != null) {
mCleanerUI.refresh();
}
refreshScore();
}

private void refreshScore() {
mScoreUI.setText("");
mScoreUI.append(mGame.getScorer().get());
}

private void refreshField() {
Expand Down Expand Up @@ -121,7 +111,7 @@ public void onFieldChange(GameLogic game) {
}

@Override
public void onScoreChange(GameLogic game) {
refreshScore();
public void onInit(GameLogic game) {
refresh();
}
}
20 changes: 20 additions & 0 deletions Swing/src/com/yarcat/chemistrylines/swing/SwingScore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.yarcat.chemistrylines.swing;

import javax.swing.JTextArea;

import com.yarcat.chemistrylines.game.Scorer;
import com.yarcat.chemistrylines.game.Scorer.ScoreListener;

public class SwingScore implements ScoreListener {
public final JTextArea textArea;

public SwingScore() {
textArea = new JTextArea();
}

@Override
public void onScoreChange(Scorer scorer) {
textArea.setText("");
textArea.append(scorer.get());
}
}
15 changes: 9 additions & 6 deletions Swing/src/com/yarcat/chemistrylines/swing/SwingUIFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class Builder {
SwingPreview mPreviewUI;
PreviewButton[] mPreview;
SwingChemistryLines mGameUI;
JTextArea mScoreUI;

public SwingChemistryLines newInstance() {
Field field = new RectField(mCols, mRows);
Expand All @@ -59,9 +58,7 @@ public SwingChemistryLines newInstance() {
mGame = mGameFactory.newInstance(field);
mFieldUI = new SwingField(mGame, mButtons);
mPreviewUI = new SwingPreview(mGame, mPreview);
mScoreUI = new JTextArea();
mGameUI =
new SwingChemistryLines(mGame, mFieldUI, mPreviewUI, mScoreUI);
mGameUI = new SwingChemistryLines(mGame, mFieldUI, mPreviewUI);
mGame.setChangeListener(mGameUI);

JFrame f =
Expand All @@ -76,8 +73,7 @@ public SwingChemistryLines newInstance() {
f.pack();
f.setVisible(true);

mGame.addItems();
mGameUI.refresh();
mGame.init();

return mGameUI;
}
Expand Down Expand Up @@ -134,6 +130,7 @@ protected Container createPreviewPane(PreviewButton[] preview) {
private Component createScorePane() {
Panel p = new Panel();
p.add(style.defaultColor(new JLabel("Score:")));
JTextArea mScoreUI = createScoreUI().textArea;
p.add(mScoreUI);
style.frame(mScoreUI);
mScoreUI.setEditable(false);
Expand All @@ -144,6 +141,12 @@ private Component createScorePane() {
return p;
}

private SwingScore createScoreUI() {
SwingScore r = new SwingScore();
mGame.getScorer().setScoreListener(r);
return r;
}

protected Container createRightPane() {
Panel p = new Panel();
style.defaultColor(p);
Expand Down

0 comments on commit 6465737

Please sign in to comment.