Commits

Eric Mignot committed ba0b2fa

introduce PieceRenderer to remove duplication

Comments (0)

Files changed (7)

src/main/java/gui/GameFrame.java

 
     private List<Piece> pieces;
     public void display(Piece piece) {
-        Renderer renderer = new RendererFactory().rendererOf( piece );
+        PieceRenderer renderer = new RendererFactory().rendererOf( piece );
         getButtonNamed( piece.getPosition() ).setText( renderer.toString() );
         getButtonNamed( piece.getPosition() ).setForeground( renderer.getColor() );
-        if (!pieces.contains(piece)) {
+        if (!pieces.contains( piece )) {
             pieces.add( piece );
         }
     }

src/main/java/gui/PawnRenderer.java

 package gui;
 
-import chess.Pawn;
+import chess.Piece;
 
-import java.awt.*;
+public class PawnRenderer extends PieceRenderer {
 
-import static java.awt.Color.black;
-import static java.awt.Color.white;
-
-public class PawnRenderer implements Renderer {
-
-    private Pawn pawn;
-
-    public PawnRenderer(Pawn pawn) {
-        this.pawn = pawn;
+    public PawnRenderer(Piece piece) {
+        super( piece );
     }
 
     public String toString() {
         return "P";
     }
 
-    public Color getColor() {
-        return pawn.isWhite() ? white : black;
-    }
 }

src/main/java/gui/PieceRenderer.java

+package gui;
+
+import chess.Piece;
+
+import java.awt.*;
+
+import static java.awt.Color.black;
+import static java.awt.Color.white;
+
+public abstract class PieceRenderer {
+
+    private Piece piece;
+
+    public PieceRenderer(Piece piece) {
+        this.piece = piece;
+    }
+
+    public Color getColor() {
+        return piece.isWhite() ? white : black;
+    }
+
+    public abstract String toString();
+}

src/main/java/gui/QueenRenderer.java

 package gui;
 
-import chess.Queen;
+import chess.Piece;
 
-import java.awt.*;
+public class QueenRenderer extends PieceRenderer {
 
-import static java.awt.Color.black;
-import static java.awt.Color.white;
-
-public class QueenRenderer implements Renderer {
-
-    private Queen queen;
-
-    public QueenRenderer(Queen queen) {
-        this.queen = queen;
+    public QueenRenderer(Piece piece) {
+        super( piece );
     }
 
-    public Color getColor() {
-        return queen.isWhite() ? white : black;
-    }
-    
     public String toString() {
         return "Q";
     }

src/main/java/gui/Renderer.java

-package gui;
-
-import java.awt.*;
-
-public interface Renderer {
-
-    public Color getColor();
-    public String toString();
-}

src/main/java/gui/RendererFactory.java

 
 public class RendererFactory {
 
-    public Renderer rendererOf(Piece piece) {
+    public PieceRenderer rendererOf(Piece piece) {
         if (piece instanceof Queen) {
             return new QueenRenderer((Queen) piece);
         }
         return null;
     }
     
-    public static Renderer queen() {
+    public static PieceRenderer queen() {
         return new QueenRenderer(null);
     }
 
-    public static Renderer pawn() {
+    public static PieceRenderer pawn() {
         return new PawnRenderer(null);
     }
 }

src/test/java/support/GameDriver.java

 import com.objogate.wl.swing.driver.JFrameDriver;
 import com.objogate.wl.swing.driver.JLabelDriver;
 import com.objogate.wl.swing.gesture.GesturePerformer;
+import gui.PieceRenderer;
 
 import javax.swing.*;
 
         button( cell ).hasText( equalTo( "" ) );
     }
 
-    public void has(gui.Renderer renderer, String location) {
+    public void has(PieceRenderer renderer, String location) {
         button( location ).hasText( equalTo( renderer.toString() ));
     }