Ability to paint fences
authorJaroslav Tulach <jtulach@netbeans.org>
Sun, 24 May 2009 16:27:38 +0200
changeset 23d4d8dc9d052b
parent 22 d49847581d6e
child 24 fdd6c390fa5f
Ability to paint fences
quoridor/src/main/java/cz/xelfi/quoridor/Board.java
quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java
visidor/src/visidor/Viewer.java
     1.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Sun May 24 16:26:08 2009 +0200
     1.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Sun May 24 16:27:38 2009 +0200
     1.3 @@ -658,7 +658,7 @@
     1.4      private static BitSet computeOccupied (
     1.5          Collection<Player> players, Collection<Fence> fences
     1.6      ) throws IllegalPositionException {
     1.7 -        BitSet occupied = new BitSet (17 * 17);
     1.8 +        BitSet occupied = new BitSet (20 * 30); // TBD this is just an upper guess
     1.9          
    1.10          for (Player p : players) {
    1.11              if (p.getX () % 2 == 1) throw new IllegalPositionException ("Wrong player position: " + p); // NOI18N
     2.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Sun May 24 16:26:08 2009 +0200
     2.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Sun May 24 16:27:38 2009 +0200
     2.3 @@ -25,6 +25,7 @@
     2.4   */
     2.5  package cz.xelfi.quoridor;
     2.6  
     2.7 +import cz.xelfi.quoridor.Fence.Orientation;
     2.8  import java.util.Iterator;
     2.9  import java.util.List;
    2.10  import junit.framework.TestCase;
    2.11 @@ -76,7 +77,7 @@
    2.12          assertEquals (10, list.get (1).getFences ());
    2.13      }
    2.14      
    2.15 -    public void testFences () {
    2.16 +    public void testFences () throws IllegalPositionException {
    2.17          assertEquals ("No on board", 0, board.getFences ().size ());
    2.18          try {
    2.19              board.getFences ().add (null);
    2.20 @@ -84,6 +85,38 @@
    2.21          } catch (java.lang.UnsupportedOperationException ex) {
    2.22              // ok
    2.23          }
    2.24 +
    2.25 +        {
    2.26 +            Board b = board.apply(Move.fence('A', 1, Orientation.HORIZONTAL));
    2.27 +            assertEquals("One fence placed: ", 1, b.getFences().size());
    2.28 +            Fence f = b.getFences().iterator().next();
    2.29 +            assertEquals("Row", 1, f.getRow());
    2.30 +            assertEquals("Column", 'A', f.getColumn());
    2.31 +        }
    2.32 +
    2.33 +        {
    2.34 +            Board b = board.apply(Move.fence('A', 1, Orientation.VERTICAL));
    2.35 +            assertEquals("One fence placed: ", 1, b.getFences().size());
    2.36 +            Fence f = b.getFences().iterator().next();
    2.37 +            assertEquals("Row", 1, f.getRow());
    2.38 +            assertEquals("Column", 'A', f.getColumn());
    2.39 +        }
    2.40 +
    2.41 +        {
    2.42 +            Board b = board.apply(Move.fence('H', 8, Orientation.HORIZONTAL));
    2.43 +            assertEquals("One fence placed: ", 1, b.getFences().size());
    2.44 +            Fence f = b.getFences().iterator().next();
    2.45 +            assertEquals("Row", 8, f.getRow());
    2.46 +            assertEquals("Column", 'H', f.getColumn());
    2.47 +        }
    2.48 +
    2.49 +        {
    2.50 +            Board b = board.apply(Move.fence('H', 8, Orientation.VERTICAL));
    2.51 +            assertEquals("One fence placed: ", 1, b.getFences().size());
    2.52 +            Fence f = b.getFences().iterator().next();
    2.53 +            assertEquals("Row", 8, f.getRow());
    2.54 +            assertEquals("Column", 'H', f.getColumn());
    2.55 +        }
    2.56      }
    2.57      
    2.58      public void testFourTimesInAgainstEachOtherResultsInInvalidPosition () throws Exception {
     3.1 --- a/visidor/src/visidor/Viewer.java	Sun May 24 16:26:08 2009 +0200
     3.2 +++ b/visidor/src/visidor/Viewer.java	Sun May 24 16:27:38 2009 +0200
     3.3 @@ -5,6 +5,11 @@
     3.4  
     3.5  package visidor;
     3.6  
     3.7 +import cz.xelfi.quoridor.Board;
     3.8 +import cz.xelfi.quoridor.Fence;
     3.9 +import cz.xelfi.quoridor.Fence.Orientation;
    3.10 +import cz.xelfi.quoridor.IllegalPositionException;
    3.11 +import cz.xelfi.quoridor.Move;
    3.12  import java.awt.Color;
    3.13  import java.awt.Point;
    3.14  import java.awt.Rectangle;
    3.15 @@ -29,7 +34,19 @@
    3.16      /**
    3.17       * @param args the command line arguments
    3.18       */
    3.19 -    public static void main(String[] args) {
    3.20 +    public static void main(String[] args) throws IllegalPositionException {
    3.21 +        Board b = Board.empty();
    3.22 +        for (int i = 1; i <= 8; i++) {
    3.23 +            b = b.apply(Move.fence('A', i, Orientation.values()[i % 2]));
    3.24 +        }
    3.25 +        for (int i = 1; i <= 8; i++) {
    3.26 +            b = b.apply(Move.fence('H', i, Orientation.values()[(i + 1) % 2]));
    3.27 +        }
    3.28 +        //b = b.apply(Move.fence('H', 5, Orientation.HORIZONTAL));
    3.29 +        view(b);
    3.30 +    }
    3.31 +
    3.32 +    private static void view(Board b) {
    3.33          Scene scene = new Scene();
    3.34  //        Scene layerBoard = scene;
    3.35          final LayerWidget layerLines = new LayerWidget(scene);
    3.36 @@ -81,6 +98,15 @@
    3.37          verticalWall.setBorder(BorderFactory.createLineBorder());
    3.38          layerBoard.addChild(verticalWall);
    3.39  
    3.40 +        for (Fence f : b.getFences()) {
    3.41 +            Rectangle r = fenceRectangle(f.getColumn(), f.getRow(), f.getOrientation());
    3.42 +            ImageWidget fenceWall = new ImageWidget(scene);
    3.43 +            fenceWall.setPreferredBounds(r);
    3.44 +            fenceWall.setBackground(Color.BLACK);
    3.45 +            fenceWall.setBorder(BorderFactory.createLineBorder());
    3.46 +            layerBoard.addChild(fenceWall);
    3.47 +        }
    3.48 +
    3.49          JFrame f = new JFrame();
    3.50          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    3.51          
    3.52 @@ -89,6 +115,19 @@
    3.53          f.setVisible(true);
    3.54      }
    3.55  
    3.56 +    private static Rectangle fenceRectangle(char column, int row, Fence.Orientation o) {
    3.57 +        int w, h;
    3.58 +        switch (o) {
    3.59 +            case HORIZONTAL: w = 45; h = 5; break;
    3.60 +            case VERTICAL: w = 5; h = 45; break;
    3.61 +            default: throw new IllegalStateException();
    3.62 +        }
    3.63 +
    3.64 +        return new Rectangle(
    3.65 +            (column - 'A' + 1) * 50 + 25 - w, row * 50 + 25 - h, 2 * w, 2 * h
    3.66 +        );
    3.67 +    }
    3.68 +
    3.69      final boolean horizontal;
    3.70  
    3.71      Point orig;