Can now play game against ownself chess
authorJaroslav Tulach <jtulach@netbeans.org>
Wed, 25 Sep 2013 04:18:47 +0200
branchchess
changeset 53bc0094a5f88c
parent 52 6bb4070d2c20
child 54 386d1f27e2e6
Can now play game against ownself
chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java
chess/src/main/java/org/apidesign/html/demo/chess/Rules.java
chess/src/main/webapp/pages/index.html
chess/src/test/java/org/apidesign/html/demo/chess/BoardModelTest.java
     1.1 --- a/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java	Tue Sep 24 23:52:32 2013 +0200
     1.2 +++ b/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java	Wed Sep 25 04:18:47 2013 +0200
     1.3 @@ -64,18 +64,6 @@
     1.4          }
     1.5      }
     1.6      
     1.7 -    @ComputedProperty static boolean myTurn(String player, String whitePlayer, String blackPlayer, Color turn) {
     1.8 -        if (turn != null && player != null) switch (turn) {
     1.9 -            case B: return player.equals(blackPlayer);
    1.10 -            case W: return player.equals(whitePlayer);
    1.11 -        }
    1.12 -        return false;
    1.13 -    }
    1.14 -    
    1.15 -    @ComputedProperty static boolean justObserving(String player) {
    1.16 -        return player == null;
    1.17 -    }
    1.18 -    
    1.19      private static final AudioClip MOVE = AudioClip.create("sounds/move.mp3");
    1.20      @OnPropertyChange("moves") static void playMove() {
    1.21          MOVE.play();
    1.22 @@ -85,11 +73,6 @@
    1.23      private static final AudioClip CHECKMATE = AudioClip.create("sounds/checkmate.mp3");
    1.24      
    1.25      @Function static void selected(Board b, Square data) {
    1.26 -        if (!b.isMyTurn()) {
    1.27 -            b.setAlertMessage("Not your turn!");
    1.28 -            return;
    1.29 -        }
    1.30 -        
    1.31          Square previoslySelected = findSelectedSquare(b);
    1.32          if (previoslySelected == null) {
    1.33              if (data.getPiece() != null && data.getPieceColor() == b.getTurn()) {
    1.34 @@ -121,12 +104,11 @@
    1.35                  b.getMoves().add(newMove);
    1.36                  b.setPendingMove(newMove);
    1.37  
    1.38 -                data.setPending(true);
    1.39                  data.setPieceColor(previoslySelected.getPieceColor());
    1.40                  data.setPiece(previoslySelected.getPiece());
    1.41 -                b.setTurn(null);
    1.42                  previoslySelected.setPiece(null);
    1.43                  previoslySelected.setPieceColor(null);
    1.44 +                b.setTurn(b.getTurn() == Color.W ? Color.B : Color.W);
    1.45                  Rules.computeAccessible(b, null);
    1.46              }
    1.47          }
    1.48 @@ -235,22 +217,6 @@
    1.49          return null;
    1.50      }
    1.51  
    1.52 -    static void moveResponse(
    1.53 -        final Board b, final String errMsg, 
    1.54 -        final List<String> whites, final List<String> blacks, 
    1.55 -        final Color turn, Object alert
    1.56 -    ) {
    1.57 -        if (errMsg != null) {
    1.58 -            b.getMoves().remove(b.getPendingMove());
    1.59 -            b.setPendingMove(null);
    1.60 -            b.setAlertMessage(errMsg);
    1.61 -        } else {
    1.62 -            b.setTurn(turn);
    1.63 -            b.setAlertMessage(null);
    1.64 -        }
    1.65 -        Rules.initBoard(b, whites, blacks, turn);
    1.66 -    }
    1.67 -
    1.68      static void moveUpdate(
    1.69          final Board b, final Move move, 
    1.70          final List<String> whites, final List<String> blacks, 
    1.71 @@ -330,7 +296,6 @@
    1.72          @Property(name = "pieceColor", type = Color.class),
    1.73          @Property(name = "selected", type = boolean.class),
    1.74          @Property(name = "accessible", type = boolean.class),
    1.75 -        @Property(name = "pending", type = boolean.class),
    1.76      })
    1.77      static class SquareModel {
    1.78          @ComputedProperty static String pieceEntity(
    1.79 @@ -343,7 +308,7 @@
    1.80          }
    1.81          
    1.82          @ComputedProperty static String squareColor(
    1.83 -            Color color, boolean selected, boolean accessible, boolean pending
    1.84 +            Color color, boolean selected, boolean accessible
    1.85          ) {
    1.86              if (selected) {
    1.87                  return "selected";
    1.88 @@ -351,9 +316,6 @@
    1.89              if (accessible) {
    1.90                  return "accessible";
    1.91              }
    1.92 -            if (pending) {
    1.93 -                return "pending";
    1.94 -            }
    1.95              
    1.96              if (color == null) {
    1.97                  return "";
     2.1 --- a/chess/src/main/java/org/apidesign/html/demo/chess/Rules.java	Tue Sep 24 23:52:32 2013 +0200
     2.2 +++ b/chess/src/main/java/org/apidesign/html/demo/chess/Rules.java	Wed Sep 25 04:18:47 2013 +0200
     2.3 @@ -176,7 +176,7 @@
     2.4                      new Position(j, i),
     2.5                      (i + j) % 2 == 1 ? Color.W : Color.B,
     2.6                      null, null, // figure
     2.7 -                    false, false, false
     2.8 +                    false, false
     2.9                  );
    2.10              }
    2.11              addRows.add(new Row(arr));
    2.12 @@ -197,7 +197,6 @@
    2.13                  for (char j = 'A'; j <= 'H'; j++) {
    2.14                      Square s = r.getColumns().get(j - 'A');
    2.15                      s.setAccessible(false);
    2.16 -                    s.setPending(false);
    2.17                      r.getColumns().set(j - 'A', s);
    2.18                      initialPosition(s, init);
    2.19                  }
    2.20 @@ -206,7 +205,6 @@
    2.21              for (Row r : b.getRows()) {
    2.22                  for (Square square : r.getColumns()) {
    2.23                      square.setAccessible(false);
    2.24 -                    square.setPending(false);
    2.25                      square.setPiece(null);
    2.26                      square.setPieceColor(null);
    2.27                      square.setSelected(false);
     3.1 --- a/chess/src/main/webapp/pages/index.html	Tue Sep 24 23:52:32 2013 +0200
     3.2 +++ b/chess/src/main/webapp/pages/index.html	Wed Sep 25 04:18:47 2013 +0200
     3.3 @@ -50,8 +50,6 @@
     3.4                  <a id="apptitle" class="brand" data-bind="css: { active: viewGamesActive }, click: activateSettings" href="#">JavaOne Chess</a>
     3.5                  <ul class="nav" data-bind="foreach: boards">
     3.6                      <li data-bind="css: { active: active }, click: $root.activateGame">
     3.7 -                        <span data-bind="visible: myTurn" class="badge badge-warning myturn">&rarrhk;</span> 
     3.8 -                        <span data-bind="visible: justObserving" class="badge myturn">&infin;</span> 
     3.9                          <a href="#" data-bind="text: title"></a>
    3.10                      </li>
    3.11                  </ul>
     4.1 --- a/chess/src/test/java/org/apidesign/html/demo/chess/BoardModelTest.java	Tue Sep 24 23:52:32 2013 +0200
     4.2 +++ b/chess/src/test/java/org/apidesign/html/demo/chess/BoardModelTest.java	Wed Sep 25 04:18:47 2013 +0200
     4.3 @@ -67,15 +67,6 @@
     4.4          
     4.5          BoardModel.selected(b, e4);
     4.6          
     4.7 -        assertTrue(e4.isPending(), "e4 marked as pending move");
     4.8 -        assertNull(e2.getPiece(), "No pawn at e2");
     4.9 -
    4.10 -        // ignore all other figures than the two pawns
    4.11 -        BoardModel.moveResponse(b, null, Collections.singletonList("Pe4"), 
    4.12 -            Collections.singletonList("Pe7"), Color.B, null
    4.13 -        );
    4.14 -
    4.15 -        assertFalse(e4.isPending(), "e4 now the move is real");
    4.16          assertNull(e2.getPiece(), "No pawn at e2");
    4.17          assertEquals(e4.getPiece(), BoardModel.PieceType.PAWN, "Pawn moved successfully");
    4.18          assertNull(BoardModel.findSelectedSquare(b), "No square selected");
    4.19 @@ -95,8 +86,6 @@
    4.20          
    4.21          
    4.22          Square e7 = BoardModel.findSquare(b, 'E', 7);
    4.23 -        BoardModel.selected(b, e7);
    4.24 -        assertNull(BoardModel.findSelectedSquare(b), "Can't select anything when I am white player");
    4.25          
    4.26          Move blackMv = BoardModel.MoveImpl.valueOf("E7E6");
    4.27          BoardModel.moveUpdate(b, blackMv, 
    4.28 @@ -153,23 +142,10 @@
    4.29          
    4.30          BoardModel.selected(b, e4);
    4.31          
    4.32 -        assertTrue(e4.isPending(), "e4 marked as pending move");
    4.33          assertNull(e2.getPiece(), "No pawn at e2");
    4.34          
    4.35          assertEquals(b.getMoves().size(), 1, "One move recorded");
    4.36          assertEquals(b.getMoves().get(0), b.getPendingMove(), "Pending move is waiting");
    4.37 -
    4.38 -        // ignore all other figures than the two pawns
    4.39 -        BoardModel.moveResponse(b, "No way, can't play like this", Collections.singletonList("PE2"), 
    4.40 -            Collections.singletonList("Pe7"), Color.W, null
    4.41 -        );
    4.42 -        
    4.43 -        assertEquals(b.getAlertMessage(), "No way, can't play like this");
    4.44 -        assertNull(e4.getPiece(), "No piece on e4");
    4.45 -        assertEquals(e2.getPiece(), PieceType.PAWN, "Pawn is back");
    4.46 -        
    4.47 -        assertEquals(b.getMoves().size(), 0, "Move was discarded");
    4.48 -        assertNull(b.getPendingMove(), "No pending moves");
    4.49      }
    4.50      
    4.51      @Test public void cantSelectEmptySquare() {
    4.52 @@ -235,21 +211,11 @@
    4.53          BoardModel.selected(b, e2);
    4.54          BoardModel.selected(b, e4);
    4.55          
    4.56 -        // ignore all other figures than the two pawns
    4.57 -        BoardModel.moveResponse(b, null, Collections.singletonList("PE4"), 
    4.58 -            Collections.singletonList("PD7"), Color.B, null
    4.59 -        );
    4.60 -        
    4.61          setPlayer(b, Color.B);
    4.62          
    4.63          BoardModel.selected(b, d7);
    4.64          BoardModel.selected(b, d5);
    4.65          
    4.66 -        // ignore all other figures than the two pawns
    4.67 -        BoardModel.moveResponse(b, null, Collections.singletonList("PE4"), 
    4.68 -            Collections.singletonList("PD5"), Color.W, null
    4.69 -        );
    4.70 -        
    4.71          setPlayer(b, Color.W);
    4.72          
    4.73          BoardModel.selected(b, e4);
    4.74 @@ -272,31 +238,16 @@
    4.75          BoardModel.selected(b, e2);
    4.76          BoardModel.selected(b, e4);
    4.77          
    4.78 -        // ignore all other figures than the three pawns
    4.79 -        BoardModel.moveResponse(b, null, Collections.singletonList("PE4"), 
    4.80 -            Arrays.asList("PD7", "PE7"), Color.B, null
    4.81 -        );
    4.82 -        
    4.83          setPlayer(b, Color.B);
    4.84          
    4.85          BoardModel.selected(b, d7);
    4.86          BoardModel.selected(b, d5);
    4.87          
    4.88 -        // ignore all other figures than the three pawns
    4.89 -        BoardModel.moveResponse(b, null, Collections.singletonList("PE4"), 
    4.90 -            Arrays.asList("PD5", "PE7"), Color.W, null
    4.91 -        );
    4.92 -        
    4.93          setPlayer(b, Color.W);
    4.94          
    4.95          BoardModel.selected(b, e4);
    4.96          BoardModel.selected(b, e5);
    4.97          
    4.98 -        // ignore all other figures than the three pawns
    4.99 -        BoardModel.moveResponse(b, null, Collections.singletonList("PE5"), 
   4.100 -            Arrays.asList("PD5", "PE7"), Color.B, null
   4.101 -        );
   4.102 -        
   4.103          setPlayer(b, Color.B);
   4.104          
   4.105          BoardModel.selected(b, e7);
   4.106 @@ -315,11 +266,6 @@
   4.107          BoardModel.selected(b, e2);
   4.108          BoardModel.selected(b, e4);
   4.109          
   4.110 -        // ignore all other figures than the two pawns
   4.111 -        BoardModel.moveResponse(b, null, Collections.singletonList("Pe4"), 
   4.112 -            Collections.singletonList("PD7"), Color.B, null
   4.113 -        );
   4.114 -        
   4.115          Move blackMv = BoardModel.MoveImpl.valueOf("D7D5");
   4.116          BoardModel.moveUpdate(b, blackMv, 
   4.117              Collections.singletonList("PE4"),