- Timestamp:
- 01/30/09 16:41:23 (13 years ago)
- Location:
- chess
- Files:
-
- 1 added
- 26 edited
Legend:
- Unmodified
- Added
- Removed
-
chess/chess.jad
r4 r5 1 1 MIDlet-Description: Chess bluetooth game 2 MIDlet-Jar-Size: 82 17972 MIDlet-Jar-Size: 828464 3 3 MIDlet-Jar-URL: chess.jar 4 4 MIDlet-Name: Chess -
chess/src/chess/MSG.properties
r4 r5 16 16 17 17 wait-for-bluetooth.title = \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f bluetooth. \u041f\u043e\u0434\u043e\u0436\u0434\u0438\u0442\u0435... 18 19 wait-for-opponent.title = \u041e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0445\u043e\u0434\u0430 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u0438\u043a\u0430... -
chess/src/chess/control/BaseController.java
r4 r5 1 1 package chess.control; 2 3 import javax.microedition.lcdui.Command; 4 import javax.microedition.lcdui.Displayable; 2 5 3 6 import chess.game.*; … … 109 112 public void keyPressed( final int code ) { 110 113 ui.clearHighlightedCells(); 114 final Location fc = ui.getFocusedCell(); 115 int x, y; 116 if ( fc == null ) { 117 x = y = 0; 118 } else { 119 x = fc.x; 120 y = fc.y; 121 } 122 if ( code == ui.leftKey ) { 123 x -= 1; 124 if ( x < 0 ) { 125 x = game.getWidth() - 1; 126 } 127 } else if ( code == ui.rightKey ) { 128 x += 1; 129 if ( x >= game.getWidth() ) { 130 x = 0; 131 } 132 } else if ( code == ui.upKey ) { 133 y -= 1; 134 if ( y < 0 ) { 135 y = game.getHeight() - 1; 136 } 137 138 } else if ( code == ui.downKey ) { 139 y += 1; 140 if ( y >= game.getHeight() ) { 141 y = 0; 142 } 143 } 144 111 145 final Location selectedCell = ui.getSelectedCell(); 112 146 if ( selectedCell == null ) { 113 final Location fc = ui.getFocusedCell();114 int x, y;115 if ( fc == null ) {116 x = y = 0;117 } else {118 x = fc.x;119 y = fc.y;120 }121 if ( code == ui.leftKey ) {122 x -= 1;123 if ( x < 0 ) {124 x = game.getWidth() - 1;125 }126 } else if ( code == ui.rightKey ) {127 x += 1;128 if ( x >= game.getWidth() ) {129 x = 0;130 }131 } else if ( code == ui.upKey ) {132 y -= 1;133 if ( y < 0 ) {134 y = game.getHeight() - 1;135 }136 137 } else if ( code == ui.downKey ) {138 y += 1;139 if ( y >= game.getHeight() ) {140 y = 0;141 }142 }143 147 final Location focused = new Location( x, y ); 144 148 ui.setFocusedCell( focused ); 145 149 final IUnit unit = game.getUnit( focused ); 146 150 if ( unit != null ) { 147 final Location[] available = unit.canMoveTo(); 148 for ( int i = 0; i < available.length; i++ ) { 149 final Location l = available[i]; 150 ui.addHighlightedCell( l, false ); 151 } 152 153 final Location[] attacked = unit.canAttack(); 154 for ( int i = 0; i < attacked.length; i++ ) { 155 final Location l = attacked[i]; 156 ui.addHighlightedCell( l, true ); 157 } 151 highlightDomain( unit ); 158 152 } 159 153 160 154 if ( code == ui.fireKey && unit != null ) { 161 155 ui.setSelectedCell( focused ); 156 ui.addCommand( app.getMidlet().backCommand ); 162 157 } 163 158 } else { 164 159 final IUnit unit = game.getUnit( selectedCell ); 165 160 if ( unit != null ) { 166 final Location[] available = unit.canMoveTo();167 f or ( int i = 0; i < available.length; i++ ) {168 final Location l = available[i];169 ui. addHighlightedCell( l, false);161 final Location[] domain = highlightDomain( unit ); 162 final int index = findNearest( domain, x, y ); 163 if ( index >= 0 ) { 164 ui.setFocusedCell( domain[index] ); 170 165 } 171 172 final Location[] attacked = unit.canAttack(); 173 for ( int i = 0; i < attacked.length; i++ ) { 174 final Location l = attacked[i]; 175 ui.addHighlightedCell( l, true ); 166 if ( code == ui.fireKey ) { 167 game.doAction( 168 new Action( 169 player, 170 0, 171 selectedCell, 172 ui.getFocusedCell() 173 ) 174 ); 175 ui.setFocusedCell( null ); 176 ui.setSelectedCell( null ); 177 ui.clearHighlightedCells(); 178 post( new SystemAction( SystemAction.YOUR_TURN_TYPE, player ) ); 176 179 } 177 } 178 if ( code == ui.fireKey ) { 180 } else { 179 181 ui.setSelectedCell( null ); 180 182 } 181 } 183 184 } 185 } 186 187 private static int findNearest( final Location[] ls, 188 final int x, 189 final int y ) { 190 int d = Integer.MAX_VALUE; 191 int index = -1; 192 for ( int i = 0; i < ls.length; i++ ) { 193 final Location l = ls[i]; 194 final int _d = l.ldistance( x, y ); 195 if ( _d < d ) { 196 d = _d; 197 index = i; 198 } 199 } 200 201 return index; 202 } 203 204 public void commandAction( final Command command, 205 final Displayable displayable ) { 206 if ( command == app.getMidlet().backCommand ) { 207 ui.removeCommand( command ); 208 ui.setSelectedCell( null ); 209 } 210 } 211 212 private Location[] highlightDomain( final IUnit unit ) { 213 final Location[] available = unit.canMoveTo(); 214 for ( int i = 0; i < available.length; i++ ) { 215 final Location l = available[i]; 216 final IUnit u = game.getUnit( l ); 217 final boolean attack = u != null && unit.isOpponent( u ); 218 ui.addHighlightedCell( l, attack ); 219 } 220 return available; 182 221 } 183 222 } -
chess/src/chess/game/Action.java
r4 r5 25 25 } 26 26 27 public Action( final Location from, 27 public Action( final Player player, 28 final int duration, 29 final Location from, 28 30 final Location to ) { 31 this.player = player; 32 this.duration = duration; 29 33 this.from = from; 30 34 this.to = to; 31 35 } 32 36 33 public Action( final int xFrom, 37 public Action( final Player player, 38 final int duration, 39 final int xFrom, 34 40 final int yFrom, 35 41 final int xTo, 36 42 final int yTo ) { 37 this( 38 39 43 this( player, duration, 44 new Location( xFrom, yFrom ), 45 new Location( xTo, yTo ) 40 46 ); 41 47 } -
chess/src/chess/game/App.java
r4 r5 2 2 3 3 import chess.control.BaseController; 4 import chess.control.ClientController; 4 5 import chess.control.MasterController; 5 import chess.control.ClientController;6 6 import chess.remote.IConnection; 7 7 import chess.ui.GameCanvas; 8 import chess.ui.MyMIDlet; 8 9 9 10 … … 23 24 private GameCanvas canvas; 24 25 26 private MyMIDlet midlet; 27 25 28 private boolean initialized = false; 26 29 27 public App( final Game game, 30 public App( final MyMIDlet midlet, 31 final Game game, 28 32 final GameCanvas canvas, 29 33 final IConnection conn, 30 34 final Player player ) { 35 this.midlet = midlet; 31 36 this.game = game; 32 37 this.player = player; … … 34 39 canvas.init( game ); 35 40 if ( master ) { 36 remote = new MasterController( this, canvas, game, conn, player);41 remote = new MasterController( this, canvas, game, conn, player ); 37 42 } else { 38 43 remote = new ClientController( this, canvas, game, conn ); … … 51 56 initialized = true; 52 57 } 58 59 public BaseController getRemote() { 60 return remote; 61 } 62 63 public boolean isMaster() { 64 return master; 65 } 66 67 public Player getPlayer() { 68 return player; 69 } 70 71 public Game getGame() { 72 return game; 73 } 74 75 public GameCanvas getCanvas() { 76 return canvas; 77 } 78 79 public MyMIDlet getMidlet() { 80 return midlet; 81 } 53 82 } -
chess/src/chess/game/IUnit.java
r4 r5 4 4 5 5 /** 6 * Class 6 * Ôèãóðà íà ïîëå 7 * <p/> 7 8 * User: BegemoT<br/> 8 9 * Date: 28.01.2009<br/> … … 14 15 public Location getLocation(); 15 16 17 /** 18 * Âîçâðàùàåò ÿ÷åéêè, äîñòóïíûå äëÿ ôèãóðû íà ýòîì õîäó. 19 * Ýòî ëèáî ñâîáîäíûå ÿ÷åéêè, íà êîòîðûå ôèãóðà ìîæåò 20 * ïåðåìåñòèòüñÿ, ëèáî ÿ÷åéêè, çàíÿòûå ôèãóðàìè ïðîòèâíèêà, 21 * åñëè îíè ìîãóò áûòü àòàêîâàíû. 22 * @return 23 */ 16 24 public Location[] canMoveTo(); 17 25 26 /** 27 * @deprecated ïîêà íå î÷åâèäíî, íóæåí ëè ýòîò ìåòîä 28 */ 18 29 public Location[] canAttack(); 19 30 31 /** 32 * ìîæåò ëè ôèãóðà ïðåâðàòèòüñÿ â äðóãóþ ôèãóðó. 33 * ïðèìåð (åäèíñòâåíûé): ïåøêà->ôåðçü 34 */ 20 35 public boolean canTransform(); 21 36 37 /** 38 * @return âîçâðàùàåò íîâóþ ôèãóðó 39 * todo âîîáùå-òî ïåøêà ìîæåò ïðåâðàòèòüñÿ â ëþáóþ ôèãóðó 40 * íóæåí ìåõàíèçì âûáîðà -- â êàêóþ èìåííî 41 */ 22 42 public IUnit transform(); 23 43 … … 27 47 final int width, 28 48 final int height ); 49 50 public boolean isOpponent( final IUnit u ); 29 51 } -
chess/src/chess/game/Location.java
r4 r5 26 26 } 27 27 28 final Location l ocation= ( Location )o;28 final Location l = ( Location )o; 29 29 30 if ( x != location.x ) { 31 return false; 32 } 33 if ( y != location.y ) { 34 return false; 35 } 36 37 return true; 30 return x == l.x && y == l.y; 38 31 } 39 32 … … 53 46 return ( 'A' + x ) + "" + ( y + 1 ); 54 47 } 48 49 public int ldistance( final int x, 50 final int y ) { 51 return Math.abs( this.x - x ) + Math.abs( this.y - y ); 52 } 53 54 public int ldistance( final Location l ) { 55 return ldistance( l.x, l.y ); 56 } 55 57 } -
chess/src/chess/game/Player.java
r4 r5 40 40 } 41 41 42 public int getDirection() { 43 if ( this == WHITE ) { 44 return 1; 45 } else { 46 return -1; 47 } 48 } 49 42 50 public Player opponent() { 43 51 if ( this == WHITE ) { -
chess/src/chess/game/units/AUnit.java
r4 r5 75 75 throw new IllegalArgumentException( "Queen can't transform" ); 76 76 } 77 78 public final boolean isOpponent( final IUnit u ) { 79 return u.getOwner() != player; 80 } 81 82 protected static Location[] convertBuffer() { 83 if ( _temp.isEmpty() ) { 84 return EMPTY; 85 } 86 87 final Location[] res = new Location[_temp.size()]; 88 _temp.copyInto( res ); 89 _temp.removeAllElements(); 90 return res; 91 } 92 93 protected static boolean checkAndAdd( final AUnit unit, 94 final IGame game, 95 final int x, 96 final int y ) { 97 if ( !game.isValid( x, y ) ) { 98 return false; 99 } 100 final IUnit u = game.getUnit( x, y ); 101 if ( u != null ) { 102 if ( unit.isOpponent( u ) ) { 103 _temp.addElement( new Location( x, y ) ); 104 } 105 return false; 106 } else { 107 _temp.addElement( new Location( x, y ) ); 108 } 109 return true; 110 } 77 111 } -
chess/src/chess/game/units/Bishop.java
r4 r5 2 2 3 3 import chess.game.IGame; 4 import chess.game.IUnit; 4 5 import chess.game.Location; 5 6 import chess.game.Player; … … 20 21 21 22 public Location[] canMoveTo() { 22 return new Location[0]; 23 _temp.removeAllElements(); 24 final Location l = location; 25 26 final int width = game.getWidth(); 27 final int height = game.getHeight(); 28 for ( int i = 1; i < width; i++ ) { 29 final int x = l.x + i; 30 final int y = l.y + i; 31 if ( !checkAndAdd( this, game, x, y ) ) { 32 break; 33 } 34 } 35 for ( int i = 1; i < width; i++ ) { 36 final int x = l.x - i; 37 final int y = l.y - i; 38 if ( !checkAndAdd( this, game, x, y ) ) { 39 break; 40 } 41 } 42 for ( int i = 1; i < width; i++ ) { 43 final int x = l.x + i; 44 final int y = l.y - i; 45 if ( !checkAndAdd( this, game, x, y ) ) { 46 break; 47 } 48 } 49 for ( int i = 1; i < width; i++ ) { 50 final int x = l.x - i; 51 final int y = l.y + i; 52 if ( !checkAndAdd( this, game, x, y ) ) { 53 break; 54 } 55 } 56 return convertBuffer(); 23 57 } 24 58 25 59 public Location[] canAttack() { 26 return new Location[0];60 throw new IllegalStateException( "Not implemented yet" ); 27 61 } 28 62 } -
chess/src/chess/game/units/Castle.java
r4 r5 1 1 package chess.game.units; 2 3 import java.util.Vector;4 2 5 3 import chess.game.IGame; … … 28 26 final int height = game.getHeight(); 29 27 for ( int x = l.x + 1; x < width; x++ ) { 30 if ( ! game.isValid(x, l.y ) ) {28 if ( !checkAndAdd( this, game, x, l.y ) ) { 31 29 break; 32 30 } 33 if ( game.getUnit( x, l.y ) != null ) { 31 } 32 for ( int x = l.x - 1; x >= 0; x-- ) { 33 if ( !checkAndAdd( this, game, x, l.y ) ) { 34 34 break; 35 35 } 36 _temp.addElement( new Location( x, l.y ) );37 }38 for ( int x = l.x - 1; x >= 0; x-- ) {39 if ( !game.isValid( x, l.y ) ) {40 break;41 }42 if ( game.getUnit( x, l.y ) != null ) {43 break;44 }45 _temp.addElement( new Location( x, l.y ) );46 36 } 47 37 48 38 for ( int y = l.y + 1; y < height; y++ ) { 49 if ( ! game.isValid(l.x, y ) ) {39 if ( !checkAndAdd( this, game, l.x, y ) ) { 50 40 break; 51 41 } 52 if ( game.getUnit( l.x, y ) != null ) { 42 } 43 for ( int y = l.y - 1; y >= 0; y-- ) { 44 if ( !checkAndAdd( this, game, l.x, y ) ) { 53 45 break; 54 46 } 55 _temp.addElement( new Location( l.x, y ) );56 47 } 57 for ( int y = l.y - 1; y >= 0; y-- ) {58 if ( !game.isValid( l.x, y ) ) {59 break;60 }61 if ( game.getUnit( l.x, y ) != null ) {62 break;63 }64 _temp.addElement( new Location( l.x, y ) );65 }66 final Location[] res = new Location[_temp.size()];67 _temp.copyInto( res );68 _temp.removeAllElements();69 48 //fixme: äîáàâèòü âîçìîæíîñòü ðîêèðîâêè 70 return res;49 return convertBuffer(); 71 50 } 72 51 -
chess/src/chess/game/units/King.java
r4 r5 36 36 37 37 public Location[] canMoveTo() { 38 return new Location[0]; 38 _temp.removeAllElements(); 39 40 final Location l = location; 41 42 for ( int i = -1; i <= 1; i++ ) { 43 for ( int j = -1; j <= 1; j++ ) { 44 if ( i == 0 && j == 0 ) { 45 continue; 46 } 47 final int x = l.x + i; 48 final int y = l.y + j; 49 checkAndAdd( this, game, x, y ); 50 } 51 } 52 //todo äîáàâèòü âîçìîæíîñòü ðîêèðîâêè 53 return convertBuffer(); 39 54 } 40 55 41 56 public Location[] canAttack() { 42 return new Location[0];57 throw new IllegalStateException( "Not implemented yet" ); 43 58 } 44 59 } -
chess/src/chess/game/units/Knight.java
r4 r5 32 32 final int x = xBuff[i]; 33 33 final int y = yBuff[i]; 34 if ( game.isValid( x, y ) && game.isFree( x, y ) ) { 35 _temp.addElement( new Location( x, y ) ); 34 if ( game.isValid( x, y ) ) { 35 if ( game.isFree( x, y ) 36 || isOpponent( game.getUnit( x, y ) ) ) { 37 _temp.addElement( new Location( x, y ) ); 38 } 36 39 } 37 40 } 38 if ( _temp.isEmpty() ) { 39 return EMPTY; 40 } 41 42 final Location[] res = new Location[_temp.size()]; 43 _temp.copyInto( res ); 44 _temp.removeAllElements(); 45 return res; 41 return convertBuffer(); 46 42 } 47 43 … … 83 79 } 84 80 } 85 if ( _temp.isEmpty() ) { 86 return EMPTY; 87 } 88 89 final Location[] res = new Location[_temp.size()]; 90 _temp.copyInto( res ); 91 _temp.removeAllElements(); 92 return res; 81 return convertBuffer(); 93 82 } 94 83 } -
chess/src/chess/game/units/Pawn.java
r4 r5 13 13 * Time: 13:43:45<br/> 14 14 */ 15 public class Pawn extends AUnit { 15 public class Pawn extends AUnit { 16 16 17 17 private boolean virgin = true; … … 38 38 public Location[] canMoveTo() { 39 39 final Location l = getLocation(); 40 if ( virgin ) { 41 //ïåðâûé õîä 42 if ( player == Player.WHITE ) { 43 //áåëûå ñíèçó (y=0) ââåðõ 44 final Location l1 = new Location( l.x, l.y + 1 ); 45 final Location l2 = new Location( l.x, l.y + 2 ); 46 final boolean l1Valid = game.isValid( l1 ) && game.isFree( l1 ); 47 final boolean l2Valid = game.isValid( l2 ) && game.isFree( l2 ); 48 if ( l1Valid && l2Valid ) { 49 return new Location[]{ l1, l2 }; 50 } else if ( l1Valid ) { 51 return new Location[]{ l1 }; 52 } 53 } else { 54 final Location l1 = new Location( l.x, l.y - 1 ); 55 final Location l2 = new Location( l.x, l.y - 2 ); 56 final boolean l1Valid = game.isValid( l1 ) && game.isFree( l1 ); 57 final boolean l2Valid = game.isValid( l2 ) && game.isFree( l2 ); 58 if ( l1Valid && l2Valid ) { 59 return new Location[]{ l1, l2 }; 60 } else if ( l1Valid ) { 61 return new Location[]{ l1 }; 62 } 63 } 64 } else { 65 if ( getOwner() == Player.WHITE ) { 66 //áåëûå ñíèçó (y=0) ââåðõ 67 final Location l1 = new Location( l.x, l.y + 1 ); 68 if ( game.isValid( l1 ) && game.isFree( l1 ) ) { 69 return new Location[]{ l1 }; 70 } 71 } else { 72 final Location l1 = new Location( l.x, l.y - 1 ); 73 if ( game.isValid( l1 ) && game.isFree( l1 ) ) { 74 return new Location[]{ l1 }; 75 } 40 _temp.removeAllElements(); 41 final int dir = player.getDirection(); 42 43 int x = l.x; 44 int y = l.y + dir; 45 if ( game.isValid( x, y ) && game.isFree( x, y ) ) { 46 _temp.addElement( new Location( x, y ) ); 47 } 48 x = l.x - 1; 49 if ( game.isValid( x, y ) ) { 50 final IUnit u = game.getUnit( x, y ); 51 if ( u != null && isOpponent( u ) ) { 52 _temp.addElement( new Location( x, y ) ); 76 53 } 77 54 } 78 return EMPTY; 55 x = l.x + 1; 56 if ( game.isValid( x, y ) ) { 57 final IUnit u = game.getUnit( x, y ); 58 if ( u != null && isOpponent( u ) ) { 59 _temp.addElement( new Location( x, y ) ); 60 } 61 } 62 if ( virgin ) { 63 x = l.x; 64 y = l.y + 2 * dir; 65 if ( game.isValid( x, y ) && game.isFree( x, y ) ) { 66 _temp.addElement( new Location( x, y ) ); 67 } 68 } 69 if ( _temp.isEmpty() ) { 70 return EMPTY; 71 } 72 final Location[] res = new Location[_temp.size()]; 73 _temp.copyInto( res ); 74 _temp.removeAllElements(); 75 return res; 79 76 } 80 77 -
chess/src/chess/game/units/Queen.java
r4 r5 20 20 21 21 public Location[] canMoveTo() { 22 return new Location[0]; 22 _temp.removeAllElements(); 23 24 final Location l = location; 25 final int width = game.getWidth(); 26 final int height = game.getHeight(); 27 for ( int x = l.x + 1; x < width; x++ ) { 28 if ( !checkAndAdd( this, game, x, l.y ) ) { 29 break; 30 } 31 } 32 for ( int x = l.x - 1; x >= 0; x-- ) { 33 if ( !checkAndAdd( this, game, x, l.y ) ) { 34 break; 35 } 36 } 37 38 for ( int y = l.y + 1; y < height; y++ ) { 39 if ( !checkAndAdd( this, game, l.x, y ) ) { 40 break; 41 } 42 } 43 for ( int y = l.y - 1; y >= 0; y-- ) { 44 if ( !checkAndAdd( this, game, l.x, y ) ) { 45 break; 46 } 47 } 48 49 for ( int i = 1; i < width; i++ ) { 50 final int x = l.x + i; 51 final int y = l.y + i; 52 if ( !checkAndAdd( this, game, x, y ) ) { 53 break; 54 } 55 } 56 for ( int i = 1; i < width; i++ ) { 57 final int x = l.x - i; 58 final int y = l.y - i; 59 if ( !checkAndAdd( this, game, x, y ) ) { 60 break; 61 } 62 } 63 for ( int i = 1; i < width; i++ ) { 64 final int x = l.x + i; 65 final int y = l.y - i; 66 if ( !checkAndAdd( this, game, x, y ) ) { 67 break; 68 } 69 } 70 for ( int i = 1; i < width; i++ ) { 71 final int x = l.x - i; 72 final int y = l.y + i; 73 if ( !checkAndAdd( this, game, x, y ) ) { 74 break; 75 } 76 } 77 return convertBuffer(); 23 78 } 24 79 25 80 public Location[] canAttack() { 26 return new Location[0];81 throw new IllegalStateException( "Not implemented yet" ); 27 82 } 28 83 } -
chess/src/chess/remote/IRemoteManager.java
r4 r5 11 11 public void waitForIncoming( final IServerListener l ); 12 12 13 public IRemoteNode[] getAvailable( final boolean refresh ); 13 public void getAvailable( final boolean force, 14 final IClientListener l ); 14 15 15 16 } -
chess/src/chess/remote/IRemoteNode.java
r4 r5 1 1 package chess.remote; 2 3 import java.io.IOException; 2 4 3 5 /** … … 10 12 public String getName(); 11 13 12 public IConnection connect() ;14 public IConnection connect() throws IOException; 13 15 } -
chess/src/chess/remote/impl/DummyRemoteManager.java
r4 r5 6 6 import javax.microedition.io.StreamConnection; 7 7 8 import chess.remote.IClientListener; 8 9 import chess.remote.IRemoteManager; 9 10 import chess.remote.IRemoteNode; … … 34 35 } 35 36 36 public IRemoteNode[] getAvailable( final boolean refresh ) { 37 return new IRemoteNode[0]; 37 public void getAvailable( final boolean force, 38 final IClientListener l ) { 39 timer.schedule( 40 new TimerTask() { 41 public void run() { 42 l.finished( new IRemoteNode[0] ); 43 } 44 }, 45 100 46 ); 38 47 } 39 48 -
chess/src/chess/remote/impl/RemoteManager.java
r4 r5 2 2 3 3 import java.io.IOException; 4 import java.util.Vector; 4 5 import javax.bluetooth.*; 5 6 import javax.microedition.io.Connector; … … 7 8 import javax.microedition.io.StreamConnectionNotifier; 8 9 10 import chess.remote.IClientListener; 9 11 import chess.remote.IRemoteManager; 10 12 import chess.remote.IRemoteNode; … … 22 24 private final LocalDevice localDevice; 23 25 26 private final Vector devices = new Vector(); 27 private static final IRemoteNode[] EMPTY = new IRemoteNode[0]; 28 24 29 private RemoteManager() throws Exception { 25 30 localDevice = LocalDevice.getLocalDevice(); 31 fillupCache(); 32 } 33 34 private void fillupCache() { 35 final DiscoveryAgent agent = localDevice.getDiscoveryAgent(); 36 final RemoteDevice[] preknownDevices = agent.retrieveDevices( DiscoveryAgent.PREKNOWN ); 37 if ( preknownDevices != null ) { 38 for ( int i = 0; i < preknownDevices.length; i++ ) { 39 final RemoteDevice device = preknownDevices[i]; 40 devices.addElement( device ); 41 } 42 } 43 final RemoteDevice[] cachedDevices = agent.retrieveDevices( DiscoveryAgent.CACHED ); 44 if ( cachedDevices != null ) { 45 for ( int i = 0; i < cachedDevices.length; i++ ) { 46 final RemoteDevice device = cachedDevices[i]; 47 devices.addElement( device ); 48 } 49 } 26 50 } 27 51 … … 35 59 } 36 60 37 public IRemoteNode[] getAvailable( final boolean refresh ) { 38 final DiscoveryAgent agent = localDevice.getDiscoveryAgent(); 39 final RemoteDevice[] preknownDevices = agent.retrieveDevices( DiscoveryAgent.PREKNOWN ); 40 final RemoteDevice[] cachedDevices = agent.retrieveDevices( DiscoveryAgent.CACHED ); 61 public void getAvailable( final boolean refresh, 62 final IClientListener l ) { 41 63 if ( refresh ) { 42 64 //todo 43 65 try { 66 final DiscoveryAgent agent = localDevice.getDiscoveryAgent(); 44 67 agent.startInquiry( 45 68 DiscoveryAgent.GIAC, … … 61 84 62 85 public void inquiryCompleted( final int discType ) { 63 86 devices.removeAllElements(); 87 fillupCache(); 88 l.finished( getNodes() ); 64 89 } 65 90 } … … 67 92 } catch ( BluetoothStateException e ) { 68 93 e.printStackTrace(); 94 l.finished( null ); 95 } 96 } else { 97 final IRemoteNode[] nodes = getNodes(); 98 l.finished( nodes ); 99 } 100 } 101 102 private IRemoteNode[] getNodes() { 103 if ( devices.isEmpty() ) { 104 return EMPTY; 105 } 106 final IRemoteNode[] nodes = new IRemoteNode[devices.size()]; 107 for ( int i = 0; i < devices.size(); i++ ) { 108 try { 109 final RemoteDevice device = ( RemoteDevice )devices.elementAt( i ); 110 final RemoteNode node = new RemoteNode( device ); 111 nodes[i] = node; 112 } catch ( IOException e ) { 113 e.printStackTrace(); 69 114 } 70 115 } 71 //Connector.open() 72 return new IRemoteNode[0]; 116 return nodes; 73 117 } 74 118 75 public static IRemoteManager create() {76 //return new RemoteManager();77 return new DummyRemoteManager();119 public static IRemoteManager create() throws Exception { 120 return new RemoteManager(); 121 //return new DummyRemoteManager(); 78 122 } 79 123 -
chess/src/chess/remote/impl/RemoteNode.java
r4 r5 4 4 import java.io.InputStream; 5 5 import java.io.OutputStream; 6 import javax.bluetooth.RemoteDevice; 7 import javax.microedition.io.Connector; 6 8 import javax.microedition.io.StreamConnection; 7 9 … … 17 19 public class RemoteNode implements IRemoteNode, IConnection { 18 20 21 private String name; 22 23 private RemoteDevice device; 19 24 private IConnectionListener listener = null; 20 25 private StreamConnection connection = null; … … 23 28 private transient OutputStream out; 24 29 30 public RemoteNode( final RemoteDevice device ) throws IOException { 31 this.device = device; 32 name = device.getFriendlyName( false ); 33 } 25 34 26 35 public RemoteNode( final StreamConnection conn ) { 27 this.connection = conn; 36 this( "", conn ); 37 } 38 39 public RemoteNode( final String name, 40 final StreamConnection connection ) { 41 this.name = name; 42 this.connection = connection; 28 43 } 29 44 30 45 public String getName() { 31 return "";//fixme:46 return name; 32 47 } 33 48 34 public IConnection connect() {35 if ( connection == null ) {49 public IConnection connect() throws IOException { 50 if ( connection == null && device == null ) { 36 51 throw new IllegalStateException( "Already closed node" ); 52 } else if ( connection == null ) { 53 connection = ( StreamConnection )Connector.open( device.getBluetoothAddress() ); 37 54 } 55 38 56 try { 39 57 in = connection.openInputStream(); -
chess/src/chess/ui/GameCanvas.java
r4 r5 3 3 import javax.microedition.lcdui.*; 4 4 5 import chess.MSG; 5 6 import chess.game.*; 6 7 … … 127 128 128 129 public void clearHighlightedCells() { 129 attackedCells.clear();130 highlightedCells.clear(); 130 131 attackedCells.clear(); 131 132 repaint(); … … 221 222 cellSize + 2, cellSize + 2 222 223 ); 224 //System.out.println( "queried repaint:[" + x + "," + y + "]" ); 225 //repaint(); 223 226 } 224 227 … … 228 231 listener.keyPressed( keyCode ); 229 232 } 230 //if ( keyCode == fireKey ) {231 // //message = "FIRE";232 //} else if ( keyCode == leftKey ) {233 // //message = "LEFT";234 //} else if ( keyCode == rightKey ) {235 // //message = "RIGHT";236 //} else if ( keyCode == upKey ) {237 // //message = "UP";238 //} else if ( keyCode == downKey ) {239 // //message = "DOWN";240 //} else {241 // //message = getKeyName( keyCode );242 //}243 233 } 244 234 … … 249 239 } else if ( command == midlet.menuCommand ) { 250 240 midlet.showStartScreen(); 241 } else if ( listener != null ) { 242 listener.commandAction( command, displayable ); 251 243 } 252 244 } … … 258 250 } 259 251 252 private final Screen waitScreen = new WaitScreen( MSG.getMessage( "wait-for-opponent.title" ) ); 253 260 254 public void setStatus( final int status ) { 261 //todo262 255 this.status = status; 256 //todo move it to MyMIDlet? 257 if ( status != ACTIVE ) { 258 midlet.display.setCurrent( waitScreen ); 259 } else { 260 midlet.display.setCurrent( this ); 261 } 263 262 } 264 263 -
chess/src/chess/ui/IUIListener.java
r4 r5 1 1 package chess.ui; 2 3 import javax.microedition.lcdui.CommandListener; 2 4 3 5 /** … … 7 9 * Time: 16:38:00<br/> 8 10 */ 9 public interface IUIListener {10 public void keyPressed( final int code);11 public interface IUIListener extends CommandListener { 12 public void keyPressed( final int code ); 11 13 } -
chess/src/chess/ui/JoinGameScreen.java
r4 r5 4 4 5 5 import chess.MSG; 6 import chess.remote.I RemoteManager;6 import chess.remote.IClientListener; 7 7 import chess.remote.IRemoteNode; 8 import chess.remote.impl.RemoteManager;9 8 10 9 /** … … 14 13 * Time: 15:30:43<br/> 15 14 */ 16 public class JoinGameScreen extends List implements CommandListener {15 public class JoinGameScreen extends List implements CommandListener, IClientListener { 17 16 private final MyMIDlet midlet; 18 17 19 18 private IRemoteNode[] nodes; 19 private boolean force; 20 21 private final Alert nothingFoundAlert = new Alert( 22 "Nothing found", 23 "No servers available", 24 null, 25 AlertType.INFO 26 ); 27 28 private final Alert scanningNetworkAlert = new Alert( 29 "Scanning...", 30 "Looking for game servers", 31 null, 32 AlertType.INFO 33 ); 20 34 21 35 public JoinGameScreen( final MyMIDlet midlet ) { … … 23 37 this.midlet = midlet; 24 38 25 final IRemoteManager man = RemoteManager.create(); 26 nodes = man.getAvailable( false ); 27 for ( int i = 0; i < nodes.length; i++ ) { 28 final IRemoteNode node = nodes[i]; 29 append( node.getName(), null ); 30 } 39 scanningNetworkAlert.setTimeout( Alert.FOREVER ); 40 //refresh( false ); 31 41 32 42 addCommand( midlet.okCommand ); … … 36 46 setSelectCommand( midlet.okCommand ); 37 47 48 } 49 50 public void refresh( final boolean force ) { 51 this.force = force; 52 if ( force && isShown() ) { 53 midlet.display.setCurrent( scanningNetworkAlert ); 54 } 55 midlet.getRemoteManager().getAvailable( this.force, this ); 56 } 57 58 public void finished( final IRemoteNode[] servers ) { 59 if ( !force ) { 60 if ( servers == null || servers.length == 0 ) { 61 refresh( true ); 62 return; 63 } 64 } 65 nodes = servers; 66 if ( nodes == null || nodes.length == 0 ) { 67 midlet.display.setCurrent( nothingFoundAlert ); 68 } else { 69 for ( int i = 0; i < nodes.length; i++ ) { 70 final IRemoteNode node = nodes[i]; 71 append( node.getName(), null ); 72 } 73 midlet.display.setCurrent( this ); 74 } 38 75 } 39 76 … … 50 87 } else if ( command == midlet.refreshCommand ) { 51 88 //todo wait screen 52 89 refresh( true ); 53 90 } 54 91 } -
chess/src/chess/ui/MyMIDlet.java
r4 r5 1 1 package chess.ui; 2 2 3 import java.io.IOException; 3 4 import javax.microedition.lcdui.*; 4 5 import javax.microedition.midlet.MIDlet; … … 38 39 39 40 /* GUI */ 40 p rivatefinal Display display;41 public final Display display; 41 42 42 43 private final Screen startForm = new StartScreen( this ); 43 44 private final Screen waitScreen = new WaitScreen( MSG.getMessage( "wait-for-bluetooth.title" ) ); 44 private final Screen searchForm = new JoinGameScreen( this );45 private final JoinGameScreen searchForm = new JoinGameScreen( this ); 45 46 private final GameCanvas gameCanvas = new GameCanvas( this ); 46 47 private final Screen helpScreen = new HelpScreen( this ); 47 48 48 49 /* network */ 49 private final IRemoteManager remoteManager = RemoteManager.create();50 private final IRemoteManager remoteManager; 50 51 51 52 /* current game application */ 52 53 private App app; 53 54 54 public MyMIDlet() {55 public MyMIDlet() throws Exception { 55 56 display = Display.getDisplay( this ); 57 remoteManager = RemoteManager.create(); 58 } 59 60 public IRemoteManager getRemoteManager() { 61 return remoteManager; 56 62 } 57 63 … … 82 88 new IServerListener() { 83 89 public synchronized boolean incoming( final IRemoteNode client ) { 84 final IConnection conn = client.connect(); 85 app = new App( new Game(), gameCanvas, conn, Player.WHITE ); 86 display.setCurrent( gameCanvas ); 87 return true; 90 try { 91 final IConnection conn = client.connect(); 92 app = new App( 93 MyMIDlet.this, 94 new Game(), 95 gameCanvas, 96 conn, 97 Player.WHITE//todo allow user to choose 98 ); 99 display.setCurrent( gameCanvas ); 100 return true; 101 } catch ( IOException e ) { 102 e.printStackTrace(); 103 return false; 104 } 88 105 } 89 106 90 107 public void stopped() { 91 108 display.setCurrent( 92 new Alert( "Can't connect" ) 109 new Alert( "Can't connect" ) 93 110 ); 94 111 } … … 98 115 99 116 protected void searchGame() { 117 searchForm.refresh( false ); 100 118 display.setCurrent( searchForm ); 101 119 } -
chess/tests/chess/game/BitSet64Test.java
r4 r5 14 14 } 15 15 16 public void test () {16 public void testSetGet() { 17 17 final BitSet64 set = new BitSet64(); 18 18 for ( int j = 0; j < 64; j++ ) { … … 27 27 } 28 28 } 29 30 public void testSetClear() { 31 final BitSet64 set = new BitSet64(); 32 for ( int j = 0; j < 64; j++ ) { 33 set.set( j ); 34 assertTrue( "" + j, set.is( j ) ); 35 set.clear( j ); 36 for ( int i = 0; i < 64; i++ ) { 37 assertFalse( j + ", " + i, set.is( i ) ); 38 } 39 } 40 } 41 42 public void testClear() { 43 final BitSet64 set = new BitSet64(); 44 set.set( 0 ); 45 set.set( 3 ); 46 set.set( 30 ); 47 set.set( 63 ); 48 set.clear(); 49 assertFalse( set.is( 0 ) ); 50 assertFalse( set.is( 3 ) ); 51 assertFalse( set.is( 30 ) ); 52 assertFalse( set.is( 63 ) ); 53 } 29 54 } -
chess/tests/chess/game/GameTest.java
r4 r5 64 64 assertTrue( game.getActionsLog().isEmpty() ); 65 65 66 game.doAction( new Action( 0, 1, 0, 2 ) );//A2->A366 game.doAction( new Action( Player.WHITE, 0, 0, 1, 0, 2 ) );//A2->A3 67 67 68 68 assertEquals( 1, game.getActionsLog().size() ); 69 69 70 game.doAction( new Action( 0, 2, 0, 3 ) );//A3->A470 game.doAction( new Action( Player.WHITE, 0, 0, 2, 0, 3 ) );//A3->A4 71 71 72 72 assertEquals( 2, game.getActionsLog().size() ); … … 82 82 } 83 83 ); 84 game.doAction( new Action( 0, 1, 0, 2 ) );//A2->A384 game.doAction( new Action( Player.WHITE, 0, 0, 1, 0, 2 ) );//A2->A3 85 85 86 86 assertEquals( 1, counter[0] ); 87 87 88 game.doAction( new Action( 0, 2, 0, 3 ) );//A3->A488 game.doAction( new Action( Player.WHITE, 0, 0, 2, 0, 3 ) );//A3->A4 89 89 90 90 assertEquals( 2, counter[0] );
Note: See TracChangeset
for help on using the changeset viewer.