chess/src/main/java/com/oracle/chess/client/htmljava/BoardModel.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 24 Sep 2013 22:20:24 +0200
branchchess
changeset 49 945fbfff28f3
permissions -rw-r--r--
Advanced version of the chess game
     1 /**
     2  * The MIT License (MIT)
     3  *
     4  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     5  *
     6  * Permission is hereby granted, free of charge, to any person obtaining a copy
     7  * of this software and associated documentation files (the "Software"), to deal
     8  * in the Software without restriction, including without limitation the rights
     9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10  * copies of the Software, and to permit persons to whom the Software is
    11  * furnished to do so, subject to the following conditions:
    12  *
    13  * The above copyright notice and this permission notice shall be included in
    14  * all copies or substantial portions of the Software.
    15  *
    16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    22  * THE SOFTWARE.
    23  */
    24 package com.oracle.chess.client.htmljava;
    25 
    26 import com.oracle.chess.client.htmljava.Communication.AlertType;
    27 import java.util.Arrays;
    28 import java.util.Collections;
    29 import java.util.List;
    30 import java.util.Timer;
    31 import java.util.TimerTask;
    32 import net.java.html.json.ComputedProperty;
    33 import net.java.html.json.Function;
    34 import net.java.html.json.Model;
    35 import net.java.html.json.ModelOperation;
    36 import net.java.html.json.OnPropertyChange;
    37 import net.java.html.json.Property;
    38 import net.java.html.sound.AudioClip;
    39 
    40 @Model(className="Board", properties={
    41     @Property(name = "player", type = String.class),
    42     @Property(name = "gameId", type = String.class),
    43     @Property(name = "whitePlayer", type = String.class),
    44     @Property(name = "blackPlayer", type = String.class),
    45     @Property(name = "alert", type = Alert.class),
    46     @Property(name = "alertMessage", type = String.class),
    47     @Property(name = "status", type = String.class),
    48     @Property(name = "rows", type = Row.class, array = true),
    49     @Property(name = "turn", type = Color.class),
    50     @Property(name = "moves", type = Move.class, array = true),
    51     @Property(name = "pendingMove", type = Move.class),
    52     @Property(name = "active", type = boolean.class),
    53 })
    54 public class BoardModel {
    55     @ComputedProperty static String title(String status, String gameId) {
    56         String t = status != null ? status : gameId;
    57         if (t != null && t.length() > 10) {
    58             return t.substring(0, 10);
    59         }
    60         return t;
    61     }
    62 
    63     @ModelOperation static void updateSummary(Board b, String summary) {
    64         if (summary != null) {
    65             b.setStatus(summary);
    66         }
    67     }
    68     
    69     @ComputedProperty static boolean myTurn(String player, String whitePlayer, String blackPlayer, Color turn) {
    70         if (turn != null && player != null) switch (turn) {
    71             case B: return player.equals(blackPlayer);
    72             case W: return player.equals(whitePlayer);
    73         }
    74         return false;
    75     }
    76     
    77     @ComputedProperty static boolean justObserving(String player) {
    78         return player == null;
    79     }
    80     
    81     private static final AudioClip MOVE = AudioClip.create("sounds/move.mp3");
    82     @OnPropertyChange("moves") static void playMove() {
    83         MOVE.play();
    84     }
    85 
    86     private static final AudioClip CHECK = AudioClip.create("sounds/check.mp3");
    87     private static final AudioClip CHECKMATE = AudioClip.create("sounds/checkmate.mp3");
    88     @OnPropertyChange("alert") static void warnCheckAndMate(Board b) {
    89         if (b.getAlert() == null) {
    90             return;
    91         }
    92         if (b.getAlert().getType() == AlertType.CHECK) {
    93             CHECK.play();
    94         }
    95         if (b.getAlert().getType() == AlertType.CHECKMATE) {
    96             CHECKMATE.play();
    97         }
    98     }
    99     
   100     @Function static void selected(Board b, Square data) {
   101         if (!b.isMyTurn()) {
   102             b.setAlertMessage("Not your turn!");
   103             return;
   104         }
   105         
   106         Square previoslySelected = findSelectedSquare(b);
   107         if (previoslySelected == null) {
   108             if (data.getPiece() != null && data.getPieceColor() == b.getTurn()) {
   109                 data.setSelected(true);
   110                 Rules.computeAccessible(b, data);
   111             }
   112         } else {
   113             if (previoslySelected == data) {
   114                 data.setSelected(false);
   115                 Rules.computeAccessible(b, null);
   116                 return;
   117             }
   118             if (data.getPiece() != null && data.getPieceColor() == previoslySelected.getPieceColor()) {
   119                 previoslySelected.setSelected(false);
   120                 data.setSelected(true);
   121                 Rules.computeAccessible(b, data);
   122                 return;
   123             }
   124             if (data.isAccessible()) {
   125                 previoslySelected.setSelected(false);
   126 
   127                 Move newMove = new Move();
   128                 newMove.setFrom(previoslySelected.getPosition());
   129                 newMove.setTo(data.getPosition());
   130                 newMove.setRound(b.getMoves().size() / 2 + 1);
   131                 newMove.setPiece(previoslySelected.getPiece());
   132                 newMove.setTurn(previoslySelected.getPieceColor());
   133                 newMove.setTakes(data.getPiece() != null);
   134                 b.getMoves().add(newMove);
   135                 b.setPendingMove(newMove);
   136 
   137                 data.setPending(true);
   138                 data.setPieceColor(previoslySelected.getPieceColor());
   139                 data.setPiece(previoslySelected.getPiece());
   140                 b.setTurn(null);
   141                 previoslySelected.setPiece(null);
   142                 previoslySelected.setPieceColor(null);
   143                 Rules.computeAccessible(b, null);
   144 
   145                 Request sm = new Request();
   146                 sm.setMsg(MsgType.SendMove);
   147                 sm.setGameId(b.getGameId());
   148                 sm.setFrom(newMove.getFrom().getLocation());
   149                 sm.setTo(newMove.getTo().getLocation());
   150                 sm.setColor(newMove.getTurn());
   151                 final UI ui = UIModel.findUI(b);
   152                 if (ui != null) {
   153                     ui.sendMsg(sm);
   154                 }
   155             }
   156         }
   157     }
   158     
   159     static class NextMove extends TimerTask {
   160         private static final Timer T = new Timer("Animate moves");
   161         private final Board b;
   162         private final Move m;
   163 
   164         public NextMove(Board b, Move m) {
   165             this.b = b;
   166             this.m = m;
   167             T.schedule(this, 1000);
   168         }
   169         
   170         
   171         @Override
   172         public void run() {
   173             b.showPosition(m);
   174         }
   175     }
   176     
   177     @ModelOperation @Function static void showPosition(Board b, Move data) {
   178         Rules.initBoard(b);
   179         boolean found = false;
   180         for (Move m : b.getMoves()) {
   181             if (found) {
   182                 b.setTurn(null);
   183                 new NextMove(b, m);
   184                 return;
   185             }
   186             Square from = findSquare(b, (char)m.getFrom().getX(), m.getFrom().getY());
   187             Square to = findSquare(b, (char)m.getTo().getX(), m.getTo().getY());
   188             to.setPiece(from.getPiece());
   189             to.setPieceColor(from.getPieceColor());
   190             from.setPiece(null);
   191             from.setPieceColor(null);
   192             if (m == data) {
   193                 found = true;
   194             }
   195         }
   196         b.setTurn(b.getMoves().size() % 2 == 0 ? Color.W : Color.B);
   197     }
   198     
   199     
   200 
   201     @Function static void rotateBoard(Board b) {
   202         Collections.reverse(b.getRows());
   203         for (Row r : b.getRows()) {
   204             Collections.reverse(r.getColumns());
   205         }
   206         Square sq = findSelectedSquare(b);
   207         if (sq != null) {
   208             sq.setSelected(false);
   209             Rules.computeAccessible(b, null);
   210         }
   211     }
   212     
   213     @ComputedProperty static boolean whiteTurn(Color turn) {
   214         return turn == Color.W;
   215     }
   216 
   217     @ComputedProperty static boolean blackTurn(Color turn) {
   218         return turn == Color.B;
   219     }
   220     
   221     @ComputedProperty static List<String> columnNames(List<Row> rows) {
   222         boolean whiteDown = rows.isEmpty() || rows.get(0).getY() == 8;
   223         String[] arr = new String[8];
   224         for (int i = 0; i < 8; i++) {
   225             String s;
   226             if (whiteDown) {
   227                 s = "" + (char)('A' + i);
   228             } else {
   229                 s = "" + (char)('H' - i);
   230             }
   231             arr[i] = s;
   232         }
   233         return Arrays.asList(arr);
   234     }
   235     
   236     static Square findSquare(Board b, char column, int row) {
   237         for (Row r : b.getRows()) {
   238             for (Square square : r.getColumns()) {
   239                 if (square.getPosition().getX() == column && square.getPosition().getY() == row) {
   240                     return square;
   241                 }
   242             }
   243         }
   244         return null;
   245     }
   246 
   247     private static Square findSquare(Board b, Position to) {
   248         return findSquare(b, (char)to.getX(), to.getY());
   249     }
   250     
   251     static Square findSelectedSquare(Board b) {
   252         for (Row row : b.getRows()) {
   253             for (Square square : row.getColumns()) {
   254                 if (square.isSelected()) {
   255                     return square;
   256                 }
   257             }
   258         }
   259         return null;
   260     }
   261 
   262     static void moveResponse(final Board b, final String errMsg, final List<String> whites, final List<String> blacks, final Color turn,
   263         Alert alert
   264     ) {
   265         if (errMsg != null) {
   266             b.getMoves().remove(b.getPendingMove());
   267             b.setPendingMove(null);
   268             b.setAlertMessage(errMsg);
   269         } else {
   270             b.setTurn(turn);
   271             b.setAlertMessage(null);
   272         }
   273         b.setAlert(alert);
   274         Rules.initBoard(b, whites, blacks, turn);
   275     }
   276 
   277     static void moveUpdate(final Board b, final Move move, final List<String> whites, final List<String> blacks, final Color turn, Alert alert) {
   278         final Square from = BoardModel.findSquare(b, move.getFrom());
   279         final Square to = BoardModel.findSquare(b, move.getTo());
   280         move.setPiece(from.getPiece());
   281         move.setTurn(from.getPieceColor());
   282         if (to.getPiece() != null) {
   283             move.setTakes(true);
   284         }
   285         b.setAlert(alert);
   286         b.setAlertMessage(alert == null ? null : alert.getMessage());
   287         move.setRound(b.getMoves().size() / 2 + 1);
   288         b.getMoves().add(move);
   289         Rules.initBoard(b, whites, blacks, turn);
   290     }
   291 
   292     @Model(className="Row", properties = {
   293         @Property(name = "columns", type = Square.class, array = true)
   294     })
   295     static class RowsImpl {
   296         @ComputedProperty static int y(List<Square> columns) {
   297             return columns.isEmpty() ? 0 : columns.get(0).getY();
   298         }
   299     }
   300     
   301     enum PieceType {
   302         PAWN(5), ROCK(2), KNIGHT(4), BISHOP(3), QUEEN(1), KING(0);
   303 
   304         final int entityIndex;
   305         
   306         PieceType(int ei) {
   307             this.entityIndex = ei;
   308         }
   309 
   310         static PieceType fromNotation(char notation) {
   311             switch (notation) {
   312                 case 'R': return ROCK;
   313                 case 'N': return KNIGHT;
   314                 case 'B': return BISHOP;
   315                 case 'Q': return QUEEN;
   316                 case 'K': return KING;
   317                 case 'P': return PAWN;
   318             }
   319             throw new IllegalStateException("Unexpected: " + notation);
   320         }
   321         
   322         String computeEntity(Color color) {
   323             if (color == null) {
   324                 color = Color.W;
   325             }
   326             int base;
   327             switch (color) {
   328                 case W: base = 12; break;
   329                 case B: base = 18; break;
   330                 default:
   331                     throw new AssertionError();
   332             }
   333             return "&#98" + String.valueOf(base + entityIndex) + ";";
   334         }
   335     }
   336     
   337     @Model(className="Position", properties = {
   338         @Property(name = "x", type = char.class),
   339         @Property(name = "y", type = int.class),
   340     })
   341     static class PositionImpl {
   342         @ComputedProperty static String location(int x, int y) {
   343             return "" + (char)(x - 'A' + 'a') + y;
   344         }
   345     }
   346     
   347     @Model(className="Square", properties = {
   348         @Property(name = "position", type = Position.class),
   349         @Property(name = "color", type = Color.class),
   350         @Property(name = "piece", type = PieceType.class),
   351         @Property(name = "pieceColor", type = Color.class),
   352         @Property(name = "selected", type = boolean.class),
   353         @Property(name = "accessible", type = boolean.class),
   354         @Property(name = "pending", type = boolean.class),
   355     })
   356     static class SquareModel {
   357         @ComputedProperty static String pieceEntity(
   358             PieceType piece, Color pieceColor
   359         ) {
   360             if (piece == null) {
   361                 return "";
   362             }
   363             return piece.computeEntity(pieceColor);
   364         }
   365         
   366         @ComputedProperty static String squareColor(
   367             Color color, boolean selected, boolean accessible, boolean pending
   368         ) {
   369             if (selected) {
   370                 return "selected";
   371             }
   372             if (accessible) {
   373                 return "accessible";
   374             }
   375             if (pending) {
   376                 return "pending";
   377             }
   378             
   379             if (color == null) {
   380                 return "";
   381             } else {
   382                 if (color == Color.W) {
   383                     return "white";
   384                 } else {
   385                     return "black";
   386                 }
   387             }
   388         }
   389         
   390         @ComputedProperty static char x(Position position) {
   391             return position == null ? 'A' : position.getX();
   392         }
   393 
   394         @ComputedProperty static int y(Position position) {
   395             return position == null ? 1 : position.getY();
   396         }
   397     }
   398     
   399     @Model(className = "Move", properties = {
   400         @Property(name = "round", type = int.class),
   401         @Property(name = "turn", type = Color.class),
   402         @Property(name = "piece", type = PieceType.class),
   403         @Property(name = "from", type = Position.class),
   404         @Property(name = "to", type = Position.class),
   405         @Property(name = "promoted", type = PieceType.class),
   406         @Property(name = "takes", type = boolean.class),
   407         @Property(name = "check", type = boolean.class),
   408     })
   409     static class MoveImpl {
   410         @ComputedProperty static boolean whiteMove(Color turn) {
   411             return turn == Color.W;
   412         }
   413         
   414         @ComputedProperty static String html(
   415             Position from, Position to, boolean takes, PieceType piece, Color turn
   416         ) {
   417             if (from == null || to == null) {
   418                 return "";
   419             }
   420             StringBuilder sb = new StringBuilder();
   421             if (piece != null && piece != PieceType.PAWN) {
   422                 sb.append(piece.computeEntity(turn));
   423             }
   424             
   425             sb.append(from.getLocation());
   426             if (takes) {
   427                 sb.append("x");
   428             }
   429             sb.append(to.getLocation());
   430             return sb.toString();
   431         }
   432 
   433         static Move valueOf(String move) {
   434             move = move.toUpperCase();
   435             Move m = new Move();
   436             {
   437                 Position p = new Position();
   438                 p.setX(move.charAt(0));
   439                 p.setY(move.charAt(1) - '0');
   440                 m.setFrom(p);
   441             }
   442             {
   443                 Position p = new Position();
   444                 p.setX(move.charAt(2));
   445                 p.setY(move.charAt(3) - '0');
   446                 m.setTo(p);
   447             }
   448             return m;
   449         }
   450     }
   451 }