- Timestamp:
- 01/31/09 20:36:41 (13 years ago)
- Location:
- chess
- Files:
-
- 3 added
- 19 edited
Legend:
- Unmodified
- Added
- Removed
-
chess/chess.jad
r5 r7 1 MIDlet-Description: Chess bluetooth game 2 MIDlet-Jar-Size: 828464 1 MIDlet-Jar-Size: 711758 3 2 MIDlet-Jar-URL: chess.jar 4 3 MIDlet-Name: Chess … … 7 6 MicroEdition-Configuration: CLDC-1.1 8 7 MicroEdition-Profile: MIDP-2.1 8 Midlet-1: Chess, , chess.ui.ChessMIDlet -
chess/src/chess/MSG.properties
r5 r7 1 1 command.ok=OK 2 command.exit= Exit3 command.back= Back4 command.refresh= Refresh5 command. menu=Menu6 command.new-game= New game7 command.join-game= Join game8 command.help= Help2 command.exit=\u0412\u044b\u0445\u043e\u0434 3 command.back=\u041d\u0430\u0437\u0430\u0434 4 command.refresh=\u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c 5 command.close=\u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c 6 command.new-game=\u041d\u043e\u0432\u0430\u044f \u0438\u0433\u0440\u0430 7 command.join-game=\u041d\u0430\u0439\u0442\u0438 \u0438\u0433\u0440\u0443 8 command.help=\u0421\u043f\u0440\u0430\u0432\u043a\u0430 9 9 10 start-menu.title= Choose11 start-menu.new-game= New game12 start-menu.join-game= Join game13 start-menu.help= Help10 start-menu.title= \u041c\u044b \u0432\u0430\u0441 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u043c! 11 start-menu.new-game= \u041d\u043e\u0432\u0430\u044f \u0438\u0433\u0440\u0430 12 start-menu.join-game= \u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c\u0441\u044f 13 start-menu.help= \u0421\u043f\u0440\u0430\u0432\u043a\u0430 14 14 15 join-game.title= Choose15 join-game.title=\u0412\u044b\u0431\u0435\u0440\u0438\u0442\u0435 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 18 19 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... 20 21 choose-player.title = \u0412\u044b\u0431\u0435\u0440\u0438\u0442\u0435 \u0441\u0442\u043e\u0440\u043e\u043d\u0443: 22 23 help.title = \u0421\u043f\u0440\u0430\u0432\u043a\u0430 24 help.content = \u0421\u0434\u0435\u043b\u0430\u0442\u044c: \u041e\u0431\u0449\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e\u0431 \u0438\u0433\u0440\u0435 25 26 player.white = \u0411\u0435\u043b\u044b\u0435 27 player.black = \u0427\u0435\u0440\u043d\u044b\u0435 -
chess/src/chess/control/BaseController.java
r6 r7 5 5 6 6 import chess.game.*; 7 import chess.remote.IConnection; 8 import chess.remote.IConnectionListener; 9 import chess.remote.IDatagram; 10 import chess.remote.SystemAction; 7 import chess.remote.*; 11 8 import chess.ui.GameCanvas; 12 9 import chess.ui.IUIListener; … … 20 17 */ 21 18 public abstract class BaseController implements IConnectionListener, IUIListener { 22 public static final int WAITING_FOR_LOCAL = 0; 23 public static final int WAITING_FOR_NETWORK = 1; 24 public static final int WAITING_FOR_OPPONENT = 2; 25 public static final int WAITING_CONNECT = 3; 26 27 protected final App app; 28 protected final GameCanvas ui; 29 protected final IGame game; 30 protected Player player; 31 protected final IConnection conn; 32 33 protected int status; 34 35 public BaseController( final App app, 36 final GameCanvas ui, 37 final IGame game, 38 final Player player, 39 final IConnection conn ) { 40 this.app = app; 41 this.ui = ui; 42 this.game = game; 43 this.player = player; 44 this.conn = conn; 45 conn.setListener( this ); 46 ui.setUIListener( this ); 47 } 48 49 public void received( final IDatagram data ) { 50 if ( data instanceof Action ) { 51 processAction( ( Action )data ); 52 } else if ( data instanceof SystemAction ) { 53 final SystemAction sa = ( SystemAction )data; 54 processSystemAction( sa ); 55 } 56 } 57 58 protected void processAction( final Action a ) { 59 game.doAction( a ); 60 } 61 62 protected void processSystemAction( final SystemAction sa ) { 63 if ( sa.type() == SystemAction.SET_PLAYER ) { 64 player = sa.getPlayer(); 65 if ( isMaster() ) { 66 throw new IllegalStateException( "master should not got such commands" ); 67 } 68 } else if ( sa.type() == SystemAction.YOUR_TURN_TYPE ) { 69 if ( player == null ) { 70 throw new IllegalStateException( "Player ==null" ); 71 } 72 setStatus( WAITING_FOR_LOCAL ); 73 } 74 } 75 76 public void sent( final IDatagram data ) { 77 setStatus( WAITING_FOR_OPPONENT ); 78 } 79 80 public void closed() { 81 setStatus( WAITING_CONNECT ); 82 } 83 84 public void post( final IDatagram data ) { 85 setStatus( WAITING_FOR_NETWORK ); 86 conn.send( data ); 87 } 88 89 public void setStatus( final int status ) { 90 //todo 91 this.status = status; 92 switch ( status ) { 93 case WAITING_FOR_LOCAL: 94 ui.setStatus( GameCanvas.ACTIVE ); 95 break; 96 case WAITING_FOR_NETWORK: 97 ui.setStatus( GameCanvas.WAITING_FOR_NETWORK ); 98 break; 99 case WAITING_FOR_OPPONENT: 100 ui.setStatus( GameCanvas.WAITING_FOR_OPPONENT ); 101 break; 102 case WAITING_CONNECT: 103 ui.setStatus( GameCanvas.DISCONNECTED ); 104 break; 105 default: 106 throw new IllegalStateException( "Unknown status:" + status ); 107 } 108 } 109 110 public abstract boolean isMaster(); 111 112 public void keyPressed( final int code ) { 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 145 final Location selectedCell = ui.getSelectedCell(); 146 if ( selectedCell == null ) { 147 final Location focused = new Location( x, y ); 148 ui.setFocusedCell( focused ); 149 final IUnit unit = game.getUnit( focused ); 150 if ( unit != null ) { 151 highlightDomain( unit ); 152 } 153 154 if ( code == ui.fireKey && unit != null && unit.getOwner() == player ) { 155 ui.setSelectedCell( focused ); 156 ui.addCommand( app.getMidlet().backCommand ); 157 } 158 } else { 159 final IUnit unit = game.getUnit( selectedCell ); 160 if ( unit != null ) { 161 final Location[] domain = highlightDomain( unit ); 162 final int index = findNearest( domain, x, y ); 163 if ( index >= 0 ) { 164 ui.setFocusedCell( domain[index] ); 165 } 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 ) ); 179 } 180 } else { 181 ui.setSelectedCell( null ); 182 } 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; 221 } 19 public static final int WAITING_FOR_LOCAL = 0; 20 public static final int WAITING_FOR_NETWORK = 1; 21 public static final int WAITING_FOR_OPPONENT = 2; 22 public static final int WAITING_CONNECT = 3; 23 24 protected final App app; 25 protected final GameCanvas ui; 26 protected final IGame game; 27 protected Player player; 28 protected final IConnection conn; 29 30 protected int status; 31 32 public BaseController( final App app, 33 final GameCanvas ui, 34 final IGame game, 35 final Player player, 36 final IConnection conn ) { 37 this.app = app; 38 this.ui = ui; 39 this.game = game; 40 this.player = player; 41 this.conn = conn; 42 conn.setListener( this ); 43 ui.setUIListener( this ); 44 } 45 46 public void received( final IDatagram data ) { 47 if( data instanceof Action ) { 48 processAction( ( Action ) data ); 49 } else if( data instanceof SystemAction ) { 50 final SystemAction sa = ( SystemAction ) data; 51 processSystemAction( sa ); 52 } 53 } 54 55 protected void processAction( final Action a ) { 56 game.doAction( a ); 57 } 58 59 protected void processSystemAction( final SystemAction sa ) { 60 if( sa.type() == SystemAction.SET_PLAYER ) { 61 player = sa.getPlayer(); 62 if( isMaster() ) { 63 throw new IllegalStateException( "master should not got such commands" ); 64 } 65 } else if( sa.type() == SystemAction.YOUR_TURN_TYPE ) { 66 if( player == null ) { 67 throw new IllegalStateException( "Player ==null" ); 68 } 69 setStatus( WAITING_FOR_LOCAL ); 70 } 71 } 72 73 public void sent( final IDatagram data ) { 74 setStatus( WAITING_FOR_OPPONENT ); 75 } 76 77 public void closed() { 78 setStatus( WAITING_CONNECT ); 79 } 80 81 public void post( final IDatagram data ) { 82 setStatus( WAITING_FOR_NETWORK ); 83 conn.send( data ); 84 } 85 86 public void setStatus( final int status ) { 87 //todo 88 this.status = status; 89 switch( status ) { 90 case WAITING_FOR_LOCAL: 91 ui.setStatus( GameCanvas.ACTIVE ); 92 break; 93 case WAITING_FOR_NETWORK: 94 ui.setStatus( GameCanvas.WAITING_FOR_NETWORK ); 95 break; 96 case WAITING_FOR_OPPONENT: 97 ui.setStatus( GameCanvas.WAITING_FOR_OPPONENT ); 98 break; 99 case WAITING_CONNECT: 100 ui.setStatus( GameCanvas.DISCONNECTED ); 101 break; 102 default: 103 throw new IllegalStateException( "Unknown status:" + status ); 104 } 105 } 106 107 public abstract boolean isMaster(); 108 109 public void keyPressed( final int code ) { 110 ui.clearHighlightedCells(); 111 final Location fc = ui.getFocusedCell(); 112 int x, y; 113 if( fc == null ) { 114 x = y = 0; 115 } else { 116 x = fc.x; 117 y = fc.y; 118 } 119 if( code == ui.leftKey ) { 120 x -= 1; 121 if( x < 0 ) { 122 x = game.getWidth() - 1; 123 } 124 } else if( code == ui.rightKey ) { 125 x += 1; 126 if( x >= game.getWidth() ) { 127 x = 0; 128 } 129 } else if( code == ui.downKey ) { 130 y -= 1; 131 if( y < 0 ) { 132 y = game.getHeight() - 1; 133 } 134 135 } else if( code == ui.upKey ) { 136 y += 1; 137 if( y >= game.getHeight() ) { 138 y = 0; 139 } 140 } 141 142 final Location selectedCell = ui.getSelectedCell(); 143 if( selectedCell == null ) { 144 final Location focused = new Location( x, y ); 145 ui.setFocusedCell( focused ); 146 final IUnit unit = game.getUnit( focused ); 147 if( unit != null ) { 148 highlightDomain( unit ); 149 } 150 151 if( code == ui.fireKey 152 && unit != null 153 && unit.getOwner() == player ) { 154 ui.setSelectedCell( focused ); 155 ui.setFocusedCell( null ); 156 157 ui.addCommand( app.getMidlet().backCommand ); 158 } 159 } else { 160 final IUnit unit = game.getUnit( selectedCell ); 161 if( unit != null ) { 162 final Location[] domain = highlightDomain( unit ); 163 final int index = findNearest( domain, x, y ); 164 if( index >= 0 ) { 165 ui.setFocusedCell( domain[index] ); 166 } 167 if( code == ui.fireKey ) { 168 game.doAction( 169 new Action( 170 player, 171 0, 172 selectedCell, 173 ui.getFocusedCell() 174 ) 175 ); 176 ui.setFocusedCell( null ); 177 ui.setSelectedCell( null ); 178 ui.clearHighlightedCells(); 179 post( new SystemAction( SystemAction.YOUR_TURN_TYPE, player ) ); 180 } 181 } else { 182 ui.setSelectedCell( null ); 183 } 184 185 } 186 } 187 188 private static int findNearest( final Location[] ls, 189 final int x, 190 final int y ) { 191 int d = Integer.MAX_VALUE; 192 int index = -1; 193 for( int i = 0; i < ls.length; i++ ) { 194 final Location l = ls[i]; 195 final int _d = l.ldistance( x, y ); 196 if( _d < d ) { 197 d = _d; 198 index = i; 199 } 200 } 201 202 return index; 203 } 204 205 public void commandAction( final Command command, 206 final Displayable displayable ) { 207 if( command == app.getMidlet().backCommand ) { 208 ui.removeCommand( command ); 209 ui.setSelectedCell( null ); 210 ui.clearHighlightedCells(); 211 } 212 } 213 214 private Location[] highlightDomain( final IUnit unit ) { 215 final Location[] available = unit.canMoveTo(); 216 for( int i = 0; i < available.length; i++ ) { 217 final Location l = available[i]; 218 final IUnit u = game.getUnit( l ); 219 final boolean attack = u != null && unit.isOpponent( u ); 220 ui.addHighlightedCell( l, attack ); 221 } 222 return available; 223 } 222 224 } -
chess/src/chess/game/App.java
r5 r7 5 5 import chess.control.MasterController; 6 6 import chess.remote.IConnection; 7 import chess.ui.ChessMIDlet; 7 8 import chess.ui.GameCanvas; 8 import chess.ui.MyMIDlet;9 9 10 10 … … 16 16 */ 17 17 public class App { 18 18 private BaseController remote; 19 19 20 21 20 private boolean master; 21 private Player player; 22 22 23 24 23 private Game game; 24 private GameCanvas canvas; 25 25 26 private MyMIDlet midlet;26 private ChessMIDlet midlet; 27 27 28 private boolean initialized = false;29 28 30 public App( final MyMIDlet midlet, 31 final Game game, 32 final GameCanvas canvas, 33 final IConnection conn, 34 final Player player ) { 35 this.midlet = midlet; 36 this.game = game; 37 this.player = player; 38 master = player != null; 39 canvas.init( game ); 40 if ( master ) { 41 remote = new MasterController( this, canvas, game, conn, player ); 42 } else { 43 remote = new ClientController( this, canvas, game, conn ); 44 } 45 initialized(); 46 } 29 /** client mode */ 30 public App( final ChessMIDlet midlet, 31 final Game game, 32 final GameCanvas canvas, 33 final IConnection conn ) { 34 this( midlet, game, canvas, conn, null ); 35 } 47 36 48 //public void doAction( final Action a ) { 49 // game.doAction( a ); 50 // if ( initialized ) { 51 // //todo:switch controller and post 52 // } 53 //} 37 /** server mode */ 38 public App( final ChessMIDlet midlet, 39 final Game game, 40 final GameCanvas canvas, 41 final IConnection conn, 42 final Player player ) { 43 this.midlet = midlet; 44 this.game = game; 45 this.player = player; 46 master = player != null; 47 canvas.init( game ); 48 if( master ) { 49 remote = new MasterController( this, canvas, game, conn, player ); 50 } else { 51 remote = new ClientController( this, canvas, game, conn ); 52 } 53 } 54 54 55 private void initialized() {56 initialized = true;57 }58 55 59 60 61 56 public BaseController getRemote() { 57 return remote; 58 } 62 59 63 64 65 60 public boolean isMaster() { 61 return master; 62 } 66 63 67 68 69 64 public Player getPlayer() { 65 return player; 66 } 70 67 71 72 73 68 public Game getGame() { 69 return game; 70 } 74 71 75 76 77 72 public GameCanvas getCanvas() { 73 return canvas; 74 } 78 75 79 public MyMIDlet getMidlet() {80 81 76 public ChessMIDlet getMidlet() { 77 return midlet; 78 } 82 79 } -
chess/src/chess/game/Game.java
r4 r7 1 1 package chess.game; 2 2 3 import java.util.Vector; 4 3 import java.util.*; 4 5 import chess.Util; 5 6 import chess.game.units.*; 6 7 … … 12 13 * Time: 15:41:04<br/> 13 14 */ 14 public class Game implements IGame { 15 private static final int SIZE = 8; 16 17 18 private IGameListener listener = null; 19 20 private final Vector actionsLog = new Vector( 64 ); 21 22 private final Vector[] activeUnits = new Vector[2]; 23 private final Vector[] deadUnits = new Vector[2]; 24 25 private final AUnit[] desk = new AUnit[SIZE * SIZE]; 26 27 public Game() { 28 activeUnits[0] = new Vector( 16 ); 29 activeUnits[1] = new Vector( 16 ); 30 31 deadUnits[0] = new Vector( 16 ); 32 deadUnits[1] = new Vector( 16 ); 33 34 for ( int i = 0; i < SIZE; i++ ) { 35 put( new Pawn( Player.WHITE, this, new Location( i, 1 ) ) ); 36 put( new Pawn( Player.BLACK, this, new Location( i, SIZE - 2 ) ) ); 37 } 38 39 final int yWhite = 0; 40 final int yBlack = SIZE - 1; 41 put( new Castle( Player.WHITE, this, new Location( 0, yWhite ) ) ); 42 put( new Castle( Player.WHITE, this, new Location( SIZE - 1, yWhite ) ) ); 43 put( new Castle( Player.BLACK, this, new Location( 0, yBlack ) ) ); 44 put( new Castle( Player.BLACK, this, new Location( SIZE - 1, yBlack ) ) ); 45 46 put( new Bishop( Player.WHITE, this, new Location( 1, yWhite ) ) ); 47 put( new Bishop( Player.WHITE, this, new Location( SIZE - 2, yWhite ) ) ); 48 put( new Bishop( Player.BLACK, this, new Location( 1, yBlack ) ) ); 49 put( new Bishop( Player.BLACK, this, new Location( SIZE - 2, yBlack ) ) ); 50 51 put( new Knight( Player.WHITE, this, new Location( 2, yWhite ) ) ); 52 put( new Knight( Player.WHITE, this, new Location( SIZE - 3, yWhite ) ) ); 53 put( new Knight( Player.BLACK, this, new Location( 2, yBlack ) ) ); 54 put( new Knight( Player.BLACK, this, new Location( SIZE - 3, yBlack ) ) ); 55 56 put( new Queen( Player.WHITE, this, new Location( 3, yWhite ) ) ); 57 put( new Queen( Player.BLACK, this, new Location( 4, yBlack ) ) ); 58 59 put( new King( Player.WHITE, this, new Location( 4, yWhite ) ) ); 60 put( new King( Player.BLACK, this, new Location( 3, yBlack ) ) ); 61 } 62 63 public int getWidth() { 64 return SIZE; 65 } 66 67 public int getHeight() { 68 return SIZE; 69 } 70 71 public boolean isFree( final Location l ) { 72 return isFree( l.x, l.y ); 73 } 74 75 public boolean isFree( final int x, 76 final int y ) { 77 return isFree( toIndex( x, y ) ); 78 } 79 80 public boolean isFree( final byte index ) { 81 return desk[index] == null; 82 } 83 84 public boolean isValid( final Location l ) { 85 return isValid( l.x, l.y ); 86 } 87 88 public boolean isValid( final int x, 89 final int y ) { 90 return ( x >= 0 && x < 8 ) && ( y >= 0 && y < 8 ); 91 } 92 93 public boolean isValid( final byte index ) { 94 return index >= 0 && index < 64; 95 } 96 97 public Vector getActiveUnits( final Player p ) { 98 return activeUnits[p.ordinal()]; 99 } 100 101 public Vector getDeadUnits( final Player p ) { 102 return deadUnits[p.ordinal()]; 103 } 104 105 public IUnit getUnit( final Location l ) { 106 return getUnit( l.x, l.y ); 107 } 108 109 public IUnit getUnit( final int x, 110 final int y ) { 111 if ( !isValid( x, y ) ) { 112 throw new IllegalArgumentException( "Invalid location:" + x + ", " + y ); 113 } 114 return getUnit( toIndex( x, y ) ); 115 } 116 117 public IUnit getUnit( final byte index ) { 118 return desk[index]; 119 } 120 121 protected void put( final int x, 122 final int y, 123 final AUnit unit ) { 124 desk[toIndex( x, y )] = unit; 125 final int player = unit.getOwner().ordinal(); 126 if ( activeUnits[player].contains( unit ) ) { 127 throw new IllegalStateException( "Unit already here:" + unit ); 128 } 129 activeUnits[player].addElement( unit ); 130 } 131 132 protected void put( final AUnit unit ) { 133 final Location l = unit.getLocation(); 134 put( l.x, l.y, unit ); 135 } 136 137 private void kill( final IUnit victim ) { 138 final byte i = toIndex( victim.getLocation() ); 139 if ( desk[i] != victim ) { 140 throw new IllegalStateException( "Killing victim is not on it's place:" + victim ); 141 } 142 desk[i] = null; 143 144 final int pIndex = victim.getOwner().ordinal(); 145 activeUnits[pIndex].removeElement( victim ); 146 deadUnits[pIndex].addElement( victim ); 147 } 148 149 private void move( final AUnit u, 150 final Location from, 151 final Location to ) { 152 desk[toIndex( from )] = null; 153 desk[toIndex( to )] = u; 154 u.setLocation( to ); 155 } 156 157 public void doAction( final Action a ) { 158 //todo Action is mutable. a.clone()? 159 actionsLog.addElement( a ); 160 //todo make action 161 final Location from = a.from(); 162 final IUnit actor = getUnit( from ); 163 if ( actor == null ) { 164 throw new IllegalStateException( "Where is no unit at " + from ); 165 } 166 if ( actor.getOwner() != a.getPlayer() ) { 167 throw new IllegalStateException( "Unit " + actor + " is not owned by " + a.getPlayer() ); 168 } 169 170 final Location to = a.to(); 171 final IUnit victim = getUnit( to ); 172 if ( victim != null && victim.getOwner() == actor.getOwner() ) { 173 throw new IllegalStateException( "Can't take own unit:" + actor + " <> " + victim ); 174 } 175 176 if ( victim != null ) { 177 kill( victim ); 178 } 179 move( ( AUnit )actor, from, to ); 180 181 if ( listener != null ) { 182 listener.changed(); 183 } 184 } 185 186 public Vector getActionsLog() { 187 return actionsLog; 188 } 189 190 public void setListener( final IGameListener l ) { 191 this.listener = l; 192 } 193 194 public byte toIndex( final Location l ) { 195 return toIndex( l.x, l.y ); 196 } 197 198 public byte toIndex( final int x, final int y ) { 199 return ( byte )( x + y * SIZE ); 200 } 201 202 public Location toLocation( final byte index ) { 203 return new Location( 204 index % SIZE, 205 index / SIZE 206 ); 207 } 15 public final class Game implements IGame { 16 private static final int SIZE = 8; 17 18 19 private IGameListener listener = null; 20 21 private final Vector actionsLog = new Vector( 64 ); 22 23 private final Vector[] activeUnits = new Vector[2]; 24 private final Vector[] deadUnits = new Vector[2]; 25 26 private final AUnit[] desk = new AUnit[SIZE * SIZE]; 27 28 public Game() { 29 activeUnits[0] = new Vector( 16 ); 30 activeUnits[1] = new Vector( 16 ); 31 32 deadUnits[0] = new Vector( 16 ); 33 deadUnits[1] = new Vector( 16 ); 34 35 for( int i = 0; i < SIZE; i++ ) { 36 put( new Pawn( Player.WHITE, this, new Location( i, 1 ) ) ); 37 put( new Pawn( Player.BLACK, this, new Location( i, SIZE - 2 ) ) ); 38 } 39 40 final int yWhite = 0; 41 final int yBlack = SIZE - 1; 42 put( new Castle( Player.WHITE, this, new Location( 0, yWhite ) ) ); 43 put( new Castle( Player.WHITE, this, new Location( SIZE - 1, yWhite ) ) ); 44 put( new Castle( Player.BLACK, this, new Location( 0, yBlack ) ) ); 45 put( new Castle( Player.BLACK, this, new Location( SIZE - 1, yBlack ) ) ); 46 47 put( new Knight( Player.WHITE, this, new Location( 1, yWhite ) ) ); 48 put( new Knight( Player.WHITE, this, new Location( SIZE - 2, yWhite ) ) ); 49 put( new Knight( Player.BLACK, this, new Location( 1, yBlack ) ) ); 50 put( new Knight( Player.BLACK, this, new Location( SIZE - 2, yBlack ) ) ); 51 52 put( new Bishop( Player.WHITE, this, new Location( 2, yWhite ) ) ); 53 put( new Bishop( Player.WHITE, this, new Location( SIZE - 3, yWhite ) ) ); 54 put( new Bishop( Player.BLACK, this, new Location( 2, yBlack ) ) ); 55 put( new Bishop( Player.BLACK, this, new Location( SIZE - 3, yBlack ) ) ); 56 57 put( new Queen( Player.WHITE, this, new Location( 3, yWhite ) ) ); 58 put( new Queen( Player.BLACK, this, new Location( 3, yBlack ) ) ); 59 60 put( new King( Player.WHITE, this, new Location( 4, yWhite ) ) ); 61 put( new King( Player.BLACK, this, new Location( 4, yBlack ) ) ); 62 } 63 64 public int getWidth() { 65 return SIZE; 66 } 67 68 public int getHeight() { 69 return SIZE; 70 } 71 72 public boolean isFree( final Location l ) { 73 return isFree( l.x, l.y ); 74 } 75 76 public boolean isFree( final int x, 77 final int y ) { 78 return isFree( toIndex( x, y ) ); 79 } 80 81 public boolean isFree( final byte index ) { 82 return desk[index] == null; 83 } 84 85 public boolean isValid( final Location l ) { 86 return isValid( l.x, l.y ); 87 } 88 89 public boolean isValid( final int x, 90 final int y ) { 91 return ( x >= 0 && x < SIZE ) && ( y >= 0 && y < SIZE ); 92 } 93 94 public boolean isValid( final byte index ) { 95 return index >= 0 && index < SIZE * SIZE; 96 } 97 98 public Vector getActiveUnits( final Player p ) { 99 return activeUnits[p.ordinal()]; 100 } 101 102 public Vector getDeadUnits( final Player p ) { 103 return deadUnits[p.ordinal()]; 104 } 105 106 public IUnit getUnit( final Location l ) { 107 return getUnit( l.x, l.y ); 108 } 109 110 public IUnit getUnit( final int x, 111 final int y ) { 112 return getUnit( toIndex( x, y ) ); 113 } 114 115 public IUnit getUnit( final byte index ) { 116 return desk[index]; 117 } 118 119 protected void put( final int x, 120 final int y, 121 final AUnit unit ) { 122 desk[toIndex( x, y )] = unit; 123 final int player = unit.getOwner().ordinal(); 124 if( activeUnits[player].contains( unit ) ) { 125 throw new IllegalStateException( "Unit already here:" + unit ); 126 } 127 activeUnits[player].addElement( unit ); 128 } 129 130 protected void put( final AUnit unit ) { 131 final Location l = unit.getLocation(); 132 put( l.x, l.y, unit ); 133 } 134 135 private void kill( final IUnit victim ) { 136 final byte i = toIndex( victim.getLocation() ); 137 if( desk[i] != victim ) { 138 throw new IllegalStateException( "Killing victim is not on it's place:" + victim ); 139 } 140 desk[i] = null; 141 142 final int pIndex = victim.getOwner().ordinal(); 143 activeUnits[pIndex].removeElement( victim ); 144 deadUnits[pIndex].addElement( victim ); 145 } 146 147 private void move( final AUnit u, 148 final Location from, 149 final Location to ) { 150 desk[toIndex( from )] = null; 151 desk[toIndex( to )] = u; 152 u.setLocation( to ); 153 } 154 155 public void doAction( final Action a ) { 156 //todo Action is mutable. a.clone()? 157 actionsLog.addElement( a ); 158 //todo check, can we really make movement? 159 final Location from = a.from(); 160 final IUnit actor = getUnit( from ); 161 if( actor == null ) { 162 throw new IllegalStateException( "Where is no unit at " + from ); 163 } 164 if( actor.getOwner() != a.getPlayer() ) { 165 throw new IllegalStateException( "Unit " + actor + " is not owned by " + a.getPlayer() ); 166 } 167 168 final Location to = a.to(); 169 final IUnit victim = getUnit( to ); 170 if( victim != null && victim.getOwner() == actor.getOwner() ) { 171 throw new IllegalStateException( "Can't take own unit:" + actor + " <> " + victim ); 172 } 173 174 if( victim != null ) { 175 kill( victim ); 176 } 177 move( ( AUnit ) actor, from, to ); 178 179 if( listener != null ) { 180 listener.changed(); 181 } 182 } 183 184 public Vector getActionsLog() { 185 return actionsLog; 186 } 187 188 public void setListener( final IGameListener l ) { 189 this.listener = l; 190 } 191 192 public byte toIndex( final Location l ) { 193 return toIndex( l.x, l.y ); 194 } 195 196 public byte toIndex( final int x, 197 final int y ) { 198 check( x, y ); 199 return ( byte ) ( x + y * SIZE ); 200 } 201 202 public Location toLocation( final byte index ) { 203 return new Location( 204 index % SIZE, 205 index / SIZE 206 ); 207 } 208 209 private final void check( final int x, 210 final int y ) { 211 if( Util.STRICT && !isValid( x, y ) ) { 212 throw new IllegalArgumentException( "Invalid location:" + x + ", " + y + " is out of bounds!" ); 213 } 214 } 208 215 } -
chess/src/chess/game/Player.java
r5 r7 1 1 package chess.game; 2 3 import chess.MSG; 2 4 3 5 … … 9 11 */ 10 12 public final class Player { 11 12 13 public static Player WHITE = new Player( 0 ); 14 public static Player BLACK = new Player( 1 ); 13 15 14 16 private int ordinal; 15 17 16 17 18 18 private Player( final int ordinal ) { 19 this.ordinal = ordinal; 20 } 19 21 20 21 switch( type ) {22 23 24 25 26 27 28 22 public static Player byType( final int type ) { 23 switch( type ) { 24 case 0: 25 return WHITE; 26 case 1: 27 return BLACK; 28 } 29 throw new IllegalStateException( "Unknown player:" + type ); 30 } 29 31 30 31 32 32 public int ordinal() { 33 return ordinal; 34 } 33 35 34 public int getBaseY() {35 if( this == WHITE ) {36 return 0;37 38 return 8;//fixme: 39 40 36 public String name() { 37 if( this == WHITE ) { 38 return MSG.getMessage( "player.white" ); 39 } else { 40 return MSG.getMessage( "player.black" ); 41 } 42 } 41 43 42 public int getDirection() {43 if( this == WHITE ) {44 return 1;45 46 return -1;47 48 44 public int getBaseY( final IGame game ) { 45 if( this == WHITE ) { 46 return 0; 47 } else { 48 return game.getHeight(); 49 } 50 } 49 51 50 public Player opponent() {51 if( this == WHITE ) {52 return BLACK;53 54 return WHITE;55 56 52 public int getDirection() { 53 if( this == WHITE ) { 54 return 1; 55 } else { 56 return -1; 57 } 58 } 57 59 58 public boolean isWhite() { 59 return this == WHITE; 60 } 60 public Player opponent() { 61 if( this == WHITE ) { 62 return BLACK; 63 } else { 64 return WHITE; 65 } 66 } 61 67 62 public boolean isBlack() { 63 return this == BLACK; 64 } 68 public boolean isWhite() { 69 return this == WHITE; 70 } 71 72 public boolean isBlack() { 73 return this == BLACK; 74 } 65 75 } -
chess/src/chess/game/units/AUnit.java
r5 r7 1 1 package chess.game.units; 2 2 3 import java.util. Vector;3 import java.util.*; 4 4 import javax.microedition.lcdui.Graphics; 5 5 6 import chess.game.IGame; 7 import chess.game.IUnit; 8 import chess.game.Location; 9 import chess.game.Player; 6 import chess.game.*; 10 7 11 8 /** … … 15 12 */ 16 13 public abstract class AUnit implements IUnit { 17 18 19 14 protected final Player player; 15 protected final IGame game; 16 protected Location location; 20 17 21 /** 22 * Âðåìåííàÿ ïåðåìåííàÿ äëÿ ñáîðà ïîëîæåíèé â ìåòîäàõ canMoveTo/canAttack 23 */ 24 protected static final transient Vector _temp = new Vector( 16 ); 25 protected static final Location[] EMPTY = new Location[0]; 18 /** Âðåìåííàÿ ïåðåìåííàÿ äëÿ ñáîðà ïîëîæåíèé â ìåòîäàõ canMoveTo/canAttack */ 19 protected static final transient Vector _temp = new Vector( 16 ); 20 protected static final Location[] EMPTY = new Location[0]; 26 21 27 28 29 30 31 32 33 22 protected AUnit( final Player player, 23 final IGame game, 24 final Location location ) { 25 this.player = player; 26 this.game = game; 27 this.location = location; 28 } 34 29 35 publicIGame getGame() {36 37 30 public final IGame getGame() { 31 return game; 32 } 38 33 39 publicPlayer getOwner() {40 41 34 public final Player getOwner() { 35 return player; 36 } 42 37 43 publicLocation getLocation() {44 45 38 public final Location getLocation() { 39 return location; 40 } 46 41 47 48 49 50 51 52 final String name = getClass().getName();53 if ( player == Player.WHITE) {54 55 56 57 58 59 'C',60 61 y + height / 2,62 63 64 42 public void paint( final Graphics g, 43 final int x, 44 final int y, 45 final int width, 46 final int height ) { 47 final boolean white = player == Player.WHITE; 48 if( white ) { 49 g.setColor( 0xFFFFFF ); 50 } else { 51 g.setColor( 0 ); 52 } 53 g.drawChar( 54 getSymbol( white ), 55 x + width / 2, 56 y + height * 2 / 3, 57 Graphics.HCENTER | Graphics.BASELINE 58 ); 59 } 65 60 66 public void setLocation( final Location location ) { 67 this.location = location; 68 } 61 protected abstract char getSymbol( final boolean white ); 69 62 70 public boolean canTransform() {71 return false;72 63 public void setLocation( final Location location ) { 64 this.location = location; 65 } 73 66 74 public IUnit transform() {75 throw new IllegalArgumentException( "Queen can't transform" );76 67 public boolean canTransform() { 68 return false; 69 } 77 70 78 public final boolean isOpponent( final IUnit u) {79 return u.getOwner() != player;80 71 public IUnit transform() { 72 throw new IllegalArgumentException( "Queen can't transform" ); 73 } 81 74 82 protected static Location[] convertBuffer() { 83 if ( _temp.isEmpty() ) { 84 return EMPTY; 85 } 75 public final boolean isOpponent( final IUnit u ) { 76 return u.getOwner() != player; 77 } 86 78 87 final Location[] res = new Location[_temp.size()]; 88 _temp.copyInto( res ); 89 _temp.removeAllElements(); 90 return res; 91 } 79 protected static Location[] convertBuffer() { 80 if( _temp.isEmpty() ) { 81 return EMPTY; 82 } 92 83 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 } 84 final Location[] res = new Location[_temp.size()]; 85 _temp.copyInto( res ); 86 _temp.removeAllElements(); 87 return res; 88 } 89 90 protected static boolean checkAndAdd( final AUnit unit, 91 final IGame game, 92 final int x, 93 final int y ) { 94 if( !game.isValid( x, y ) ) { 95 return false; 96 } 97 final IUnit u = game.getUnit( x, y ); 98 if( u != null ) { 99 if( unit.isOpponent( u ) ) { 100 _temp.addElement( new Location( x, y ) ); 101 } 102 return false; 103 } else { 104 _temp.addElement( new Location( x, y ) ); 105 } 106 return true; 107 } 111 108 } -
chess/src/chess/game/units/Bishop.java
r5 r7 2 2 3 3 import chess.game.IGame; 4 import chess.game.IUnit;5 4 import chess.game.Location; 6 5 import chess.game.Player; … … 14 13 */ 15 14 public class Bishop extends AUnit { 16 17 18 19 20 15 public Bishop( final Player player, 16 final IGame game, 17 final Location location ) { 18 super( player, game, location ); 19 } 21 20 22 23 24 21 public Location[] canMoveTo() { 22 _temp.removeAllElements(); 23 final Location l = location; 25 24 26 27 28 for( int i = 1; i < width; i++ ) {29 30 31 if( !checkAndAdd( this, game, x, y ) ) {32 33 34 35 for( int i = 1; i < width; i++ ) {36 37 38 if( !checkAndAdd( this, game, x, y ) ) {39 40 41 42 for( int i = 1; i < width; i++ ) {43 44 45 if( !checkAndAdd( this, game, x, y ) ) {46 47 48 49 for( int i = 1; i < width; i++ ) {50 51 52 if( !checkAndAdd( this, game, x, y ) ) {53 54 55 56 57 25 final int width = game.getWidth(); 26 final int height = game.getHeight(); 27 for( int i = 1; i < width; i++ ) { 28 final int x = l.x + i; 29 final int y = l.y + i; 30 if( !checkAndAdd( this, game, x, y ) ) { 31 break; 32 } 33 } 34 for( int i = 1; i < width; i++ ) { 35 final int x = l.x - i; 36 final int y = l.y - i; 37 if( !checkAndAdd( this, game, x, y ) ) { 38 break; 39 } 40 } 41 for( int i = 1; i < width; i++ ) { 42 final int x = l.x + i; 43 final int y = l.y - i; 44 if( !checkAndAdd( this, game, x, y ) ) { 45 break; 46 } 47 } 48 for( int i = 1; i < width; i++ ) { 49 final int x = l.x - i; 50 final int y = l.y + i; 51 if( !checkAndAdd( this, game, x, y ) ) { 52 break; 53 } 54 } 55 return convertBuffer(); 56 } 58 57 59 public Location[] canAttack() { 60 throw new IllegalStateException( "Not implemented yet" ); 61 } 58 public Location[] canAttack() { 59 throw new IllegalStateException( "Not implemented yet" ); 60 } 61 62 63 protected char getSymbol( final boolean white ) { 64 if( white ) { 65 return '?'; 66 } else { 67 return '?'; 68 } 69 } 62 70 } -
chess/src/chess/game/units/Castle.java
r5 r7 103 103 return res; 104 104 } 105 106 107 protected char getSymbol(final boolean white) { 108 if( white ) { 109 return '?'; 110 } else { 111 return '?'; 112 } 113 } 105 114 } -
chess/src/chess/game/units/King.java
r5 r7 14 14 */ 15 15 public class King extends AUnit { 16 16 private boolean virgin = true; 17 17 18 19 20 21 22 18 public King( final Player player, 19 final IGame game, 20 final Location location ) { 21 super( player, game, location ); 22 } 23 23 24 25 26 27 28 29 30 24 public King( final Player player, 25 final IGame game, 26 final Location location, 27 final boolean virgin ) { 28 super( player, game, location ); 29 this.virgin = virgin; 30 } 31 31 32 33 34 35 32 public void setLocation( final Location location ) { 33 super.setLocation( location ); 34 virgin = false; 35 } 36 36 37 38 37 public Location[] canMoveTo() { 38 _temp.removeAllElements(); 39 39 40 40 final Location l = location; 41 41 42 for( int i = -1; i <= 1; i++ ) {43 for( int j = -1; j <= 1; j++ ) {44 if( i == 0 && j == 0 ) {45 46 47 48 49 50 51 52 53 54 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(); 54 } 55 55 56 public Location[] canAttack() { 57 throw new IllegalStateException( "Not implemented yet" ); 58 } 56 public Location[] canAttack() { 57 throw new IllegalStateException( "Not implemented yet" ); 58 } 59 60 61 protected char getSymbol( final boolean white ) { 62 if( white ) { 63 return '?'; 64 } else { 65 return '?'; 66 } 67 } 68 59 69 } -
chess/src/chess/game/units/Knight.java
r5 r7 1 1 package chess.game.units; 2 2 3 import chess.game.IGame; 4 import chess.game.IUnit; 5 import chess.game.Location; 6 import chess.game.Player; 3 import chess.game.*; 7 4 8 5 … … 15 12 public class Knight extends AUnit { 16 13 17 18 19 20 21 14 public Knight( final Player player, 15 final IGame game, 16 final Location location ) { 17 super( player, game, location ); 18 } 22 19 23 24 20 private static int[] xBuff = new int[8]; 21 private static int[] yBuff = new int[8]; 25 22 26 27 28 23 public Location[] canMoveTo() { 24 final Location l = location; 25 fillupBuffer( l ); 29 26 30 31 for( int i = 0; i < 8; i++ ) {32 33 34 if( game.isValid( x, y ) ) {35 if( game.isFree( x, y )36 37 38 39 40 41 42 27 _temp.removeAllElements(); 28 for( int i = 0; i < 8; i++ ) { 29 final int x = xBuff[i]; 30 final int y = yBuff[i]; 31 if( game.isValid( x, y ) ) { 32 if( game.isFree( x, y ) 33 || isOpponent( game.getUnit( x, y ) ) ) { 34 _temp.addElement( new Location( x, y ) ); 35 } 36 } 37 } 38 return convertBuffer(); 39 } 43 40 44 45 46 41 private void fillupBuffer( final Location l ) { 42 final int x = l.x; 43 final int y = l.y; 47 44 48 49 50 51 52 53 54 55 45 xBuff[0] = x + 1; 46 xBuff[1] = x - 1; 47 xBuff[2] = x + 1; 48 xBuff[3] = x - 1; 49 xBuff[4] = x + 2; 50 xBuff[5] = x + 2; 51 xBuff[6] = x - 2; 52 xBuff[7] = x - 2; 56 53 57 58 59 60 61 62 63 64 65 54 yBuff[0] = y + 2; 55 yBuff[1] = y + 2; 56 yBuff[2] = y - 2; 57 yBuff[3] = y - 2; 58 yBuff[4] = y + 1; 59 yBuff[5] = y - 1; 60 yBuff[6] = y + 1; 61 yBuff[7] = y - 1; 62 } 66 63 67 68 64 public Location[] canAttack() { 65 fillupBuffer( location ); 69 66 70 _temp.removeAllElements(); 71 for ( int i = 0; i < 8; i++ ) { 72 final int x = xBuff[i]; 73 final int y = yBuff[i]; 74 if ( game.isValid( x, y ) ) { 75 final IUnit u = game.getUnit( x, y ); 76 if ( u != null && u.getOwner() != player ) { 77 _temp.addElement( new Location( x, y ) ); 78 } 79 } 80 } 81 return convertBuffer(); 82 } 67 _temp.removeAllElements(); 68 for( int i = 0; i < 8; i++ ) { 69 final int x = xBuff[i]; 70 final int y = yBuff[i]; 71 if( game.isValid( x, y ) ) { 72 final IUnit u = game.getUnit( x, y ); 73 if( u != null && u.getOwner() != player ) { 74 _temp.addElement( new Location( x, y ) ); 75 } 76 } 77 } 78 return convertBuffer(); 79 } 80 81 82 protected char getSymbol( final boolean white ) { 83 if( white ) { 84 return '?'; 85 } else { 86 return '?'; 87 } 88 } 83 89 } -
chess/src/chess/game/units/Pawn.java
r5 r7 110 110 return new Queen( player, game, location ); 111 111 } 112 113 114 protected char getSymbol( final boolean white ) { 115 if( white ) { 116 return '?'; 117 } else { 118 return '?'; 119 } 120 } 112 121 } -
chess/src/chess/game/units/Queen.java
r5 r7 2 2 3 3 import chess.game.IGame; 4 import chess.game.IUnit;5 4 import chess.game.Location; 6 5 import chess.game.Player; … … 13 12 */ 14 13 public class Queen extends AUnit { 15 16 17 18 19 14 public Queen( final Player player, 15 final IGame game, 16 final Location location ) { 17 super( player, game, location ); 18 } 20 19 21 22 20 public Location[] canMoveTo() { 21 _temp.removeAllElements(); 23 22 24 25 26 27 for( int x = l.x + 1; x < width; x++ ) {28 if( !checkAndAdd( this, game, x, l.y ) ) {29 30 31 32 for( int x = l.x - 1; x >= 0; x-- ) {33 if( !checkAndAdd( this, game, x, l.y ) ) {34 35 36 23 final Location l = location; 24 final int width = game.getWidth(); 25 final int height = game.getHeight(); 26 for( int x = l.x + 1; x < width; x++ ) { 27 if( !checkAndAdd( this, game, x, l.y ) ) { 28 break; 29 } 30 } 31 for( int x = l.x - 1; x >= 0; x-- ) { 32 if( !checkAndAdd( this, game, x, l.y ) ) { 33 break; 34 } 35 } 37 36 38 for( int y = l.y + 1; y < height; y++ ) {39 if( !checkAndAdd( this, game, l.x, y ) ) {40 41 42 43 for( int y = l.y - 1; y >= 0; y-- ) {44 if( !checkAndAdd( this, game, l.x, y ) ) {45 46 47 37 for( int y = l.y + 1; y < height; y++ ) { 38 if( !checkAndAdd( this, game, l.x, y ) ) { 39 break; 40 } 41 } 42 for( int y = l.y - 1; y >= 0; y-- ) { 43 if( !checkAndAdd( this, game, l.x, y ) ) { 44 break; 45 } 46 } 48 47 49 for( int i = 1; i < width; i++ ) {50 51 52 if( !checkAndAdd( this, game, x, y ) ) {53 54 55 56 for( int i = 1; i < width; i++ ) {57 58 59 if( !checkAndAdd( this, game, x, y ) ) {60 61 62 63 for( int i = 1; i < width; i++ ) {64 65 66 if( !checkAndAdd( this, game, x, y ) ) {67 68 69 70 for( int i = 1; i < width; i++ ) {71 72 73 if( !checkAndAdd( this, game, x, y ) ) {74 75 76 77 78 48 for( int i = 1; i < width; i++ ) { 49 final int x = l.x + i; 50 final int y = l.y + i; 51 if( !checkAndAdd( this, game, x, y ) ) { 52 break; 53 } 54 } 55 for( int i = 1; i < width; i++ ) { 56 final int x = l.x - i; 57 final int y = l.y - i; 58 if( !checkAndAdd( this, game, x, y ) ) { 59 break; 60 } 61 } 62 for( int i = 1; i < width; i++ ) { 63 final int x = l.x + i; 64 final int y = l.y - i; 65 if( !checkAndAdd( this, game, x, y ) ) { 66 break; 67 } 68 } 69 for( int i = 1; i < width; i++ ) { 70 final int x = l.x - i; 71 final int y = l.y + i; 72 if( !checkAndAdd( this, game, x, y ) ) { 73 break; 74 } 75 } 76 return convertBuffer(); 77 } 79 78 80 public Location[] canAttack() { 81 throw new IllegalStateException( "Not implemented yet" ); 82 } 79 public Location[] canAttack() { 80 throw new IllegalStateException( "Not implemented yet" ); 81 } 82 83 protected char getSymbol( final boolean white ) { 84 if( white ) { 85 return '?'; 86 } else { 87 return '?'; 88 } 89 } 83 90 } -
chess/src/chess/remote/impl/RemoteManager.java
r5 r7 2 2 3 3 import java.io.IOException; 4 import java.util. Vector;4 import java.util.*; 5 5 import javax.bluetooth.*; 6 6 import javax.microedition.io.Connector; … … 8 8 import javax.microedition.io.StreamConnectionNotifier; 9 9 10 import chess.remote.IClientListener; 11 import chess.remote.IRemoteManager; 12 import chess.remote.IRemoteNode; 13 import chess.remote.IServerListener; 10 import chess.remote.*; 11 import chess.Util; 14 12 15 13 … … 21 19 */ 22 20 public class RemoteManager implements IRemoteManager { 23 24 21 private static final String MY_SERVICE_NUMBER = "3B9FA89520078C303355AAA694238F08;name=ChessGameServer"; 22 private final LocalDevice localDevice; 25 23 26 27 24 private final Vector devices = new Vector(); 25 private static final IRemoteNode[] EMPTY = new IRemoteNode[0]; 28 26 29 30 31 32 27 private RemoteManager() throws Exception { 28 localDevice = LocalDevice.getLocalDevice(); 29 fillupCache(); 30 } 33 31 34 35 36 37 if( preknownDevices != null ) {38 for( int i = 0; i < preknownDevices.length; i++ ) {39 40 41 42 43 44 if( cachedDevices != null ) {45 for( int i = 0; i < cachedDevices.length; i++ ) {46 47 48 49 50 32 private void fillupCache() { 33 final DiscoveryAgent agent = localDevice.getDiscoveryAgent(); 34 final RemoteDevice[] preknownDevices = agent.retrieveDevices( DiscoveryAgent.PREKNOWN ); 35 if( preknownDevices != null ) { 36 for( int i = 0; i < preknownDevices.length; i++ ) { 37 final RemoteDevice device = preknownDevices[i]; 38 devices.addElement( device ); 39 } 40 } 41 final RemoteDevice[] cachedDevices = agent.retrieveDevices( DiscoveryAgent.CACHED ); 42 if( cachedDevices != null ) { 43 for( int i = 0; i < cachedDevices.length; i++ ) { 44 final RemoteDevice device = cachedDevices[i]; 45 devices.addElement( device ); 46 } 47 } 48 } 51 49 52 53 54 55 56 } catch( IOException ex ) {57 58 59 50 public void waitForIncoming( final IServerListener l ) { 51 try { 52 localDevice.setDiscoverable( DiscoveryAgent.GIAC ); 53 new WaitForIncomingThread( l ); 54 } catch( IOException ex ) { 55 ex.printStackTrace(); 56 } 57 } 60 58 61 62 63 if( refresh ) {64 65 66 67 68 69 70 71 59 public void getAvailable( final boolean refresh, 60 final IClientListener l ) { 61 if( refresh ) { 62 //todo 63 try { 64 final DiscoveryAgent agent = localDevice.getDiscoveryAgent(); 65 agent.startInquiry( 66 DiscoveryAgent.GIAC, 67 new DiscoveryListener() { 68 public void deviceDiscovered( final RemoteDevice btDevice, 69 final DeviceClass cod ) { 72 70 73 71 } 74 72 75 76 73 public void servicesDiscovered( final int transID, 74 final ServiceRecord[] servRecord ) { 77 75 78 76 } 79 77 80 81 78 public void serviceSearchCompleted( final int transID, 79 final int respCode ) { 82 80 83 81 } 84 82 85 86 87 88 89 90 91 92 } catch( BluetoothStateException e ) {93 94 95 96 97 98 99 100 83 public void inquiryCompleted( final int discType ) { 84 devices.removeAllElements(); 85 fillupCache(); 86 l.finished( getNodes() ); 87 } 88 } 89 ); 90 } catch( BluetoothStateException e ) { 91 e.printStackTrace(); 92 l.finished( null ); 93 } 94 } else { 95 final IRemoteNode[] nodes = getNodes(); 96 l.finished( nodes ); 97 } 98 } 101 99 102 103 if( devices.isEmpty() ) {104 105 106 107 for( int i = 0; i < devices.size(); i++ ) {108 109 final RemoteDevice device = ( RemoteDevice )devices.elementAt( i );110 111 112 } catch( IOException e ) {113 114 115 116 117 100 private IRemoteNode[] getNodes() { 101 if( devices.isEmpty() ) { 102 return EMPTY; 103 } 104 final IRemoteNode[] nodes = new IRemoteNode[devices.size()]; 105 for( int i = 0; i < devices.size(); i++ ) { 106 try { 107 final RemoteDevice device = ( RemoteDevice ) devices.elementAt( i ); 108 final RemoteNode node = new RemoteNode( device ); 109 nodes[i] = node; 110 } catch( IOException e ) { 111 e.printStackTrace(); 112 } 113 } 114 return nodes; 115 } 118 116 119 120 return new RemoteManager();121 //return new DummyRemoteManager();122 117 public static IRemoteManager create() throws Exception { 118 // return new RemoteManager(); 119 return new DummyRemoteManager(); 120 } 123 121 124 125 122 private static class WaitForIncomingThread extends Thread { 123 private final IServerListener listener; 126 124 127 128 129 130 125 public WaitForIncomingThread( final IServerListener listener ) { 126 this.listener = listener; 127 start(); 128 } 131 129 132 133 134 while( true ) {135 136 final StreamConnectionNotifier server = ( StreamConnectionNotifier )Connector.open(137 138 139 140 141 System.out.println( "creating server and waiting for incoming" );142 143 while( true ) {144 145 146 System.out.println( "accepted incoming" + conn );147 148 if( listener.incoming( client ) ) {149 150 System.out.println( "client accepted -> close server" );151 152 153 154 155 } catch( IOException ex ) {156 157 158 159 160 } catch( IOException e ) {161 162 163 164 } catch( IOException ex ) {165 166 167 168 169 170 171 System.out.println( "server stopped" );172 173 174 130 public void run() { 131 try { 132 while( true ) { 133 try { 134 final StreamConnectionNotifier server = ( StreamConnectionNotifier ) Connector.open( 135 "btspp://localhost:" + MY_SERVICE_NUMBER, 136 Connector.READ_WRITE, 137 true 138 ); 139 Util.log.println( "creating server and waiting for incoming" ); 140 try { 141 while( true ) { 142 // Îæèäàåì âõîäÿùåå ïîòîêîâîå ñîåäèíåíèå 143 final StreamConnection conn = server.acceptAndOpen(); 144 Util.log.println( "accepted incoming" + conn ); 145 final RemoteNode client = new RemoteNode( conn ); 146 if( listener.incoming( client ) ) { 147 //åñëè êëèåíò ïðèíÿò -- âûõîäèì (èãðà äîïóñêàåò òîëüêî îäíîãî ñîïåðíèêà) 148 Util.log.println( "client accepted -> close server" ); 149 return; 150 } 151 client.close(); 152 } 153 } catch( IOException ex ) { 154 ex.printStackTrace(); 155 } finally { 156 try { 157 server.close(); 158 } catch( IOException e ) { 159 e.printStackTrace(); 160 } 161 } 162 } catch( IOException ex ) { 163 ex.printStackTrace(); 164 return; 165 } 166 } 167 } finally { 168 listener.stopped(); 169 Util.log.println( "server stopped" ); 170 } 171 } 172 } 175 173 } -
chess/src/chess/ui/GameCanvas.java
r5 r7 15 15 */ 16 16 public class GameCanvas extends Canvas implements CommandListener, IGameListener { 17 public static final int ACTIVE = 0; 18 public static final int WAITING_FOR_NETWORK = 1; 19 public static final int WAITING_FOR_OPPONENT = 2; 20 public static final int DISCONNECTED = 3; 21 22 protected final MyMIDlet midlet; 23 24 public final int fireKey; 25 public final int leftKey; 26 public final int rightKey; 27 public final int upKey; 28 public final int downKey; 29 30 protected Player player; 31 protected IGame game; 32 33 private int status = ACTIVE; 34 35 private Location focusedCell; 36 private Location selectedCell; 37 38 //bitwise masks -- 1 bit per cell 39 private final BitSet64 highlightedCells = new BitSet64(); 40 private final BitSet64 attackedCells = new BitSet64(); 41 42 private IUIListener listener = null; 43 private transient int vMargin; 44 private transient int hMargin; 45 private transient int cellSize; 46 47 public GameCanvas( final MyMIDlet midlet ) { 48 this.midlet = midlet; 49 fireKey = getKeyCode( FIRE ); 50 leftKey = getKeyCode( LEFT ); 51 rightKey = getKeyCode( RIGHT ); 52 upKey = getKeyCode( UP ); 53 downKey = getKeyCode( DOWN ); 54 55 addCommand( midlet.menuCommand ); 56 addCommand( midlet.helpCommand ); 57 setCommandListener( this ); 58 } 59 60 public void init( final IGame game ) { 61 this.game = game; 62 //this.player = player; 63 game.setListener( this ); 64 } 65 66 public void setUIListener( final IUIListener listener ) { 67 this.listener = listener; 68 } 69 70 public Location getFocusedCell() { 71 return focusedCell; 72 } 73 74 public void setFocusedCell( final Location l ) { 75 if ( focusedCell == null && l == null ) { 76 //nothing 77 } else if ( focusedCell == null ) { 78 focusedCell = l; 79 repaintCell( l.x, l.y ); 80 } else if ( l == null ) { 81 repaintCell( focusedCell.x, focusedCell.y ); 82 focusedCell = l; 83 } else { 84 final Location old = focusedCell; 85 focusedCell = l; 86 repaintCell( old.x, old.y ); 87 repaintCell( l.x, l.y ); 88 } 89 } 90 91 public Location getSelectedCell() { 92 return selectedCell; 93 } 94 95 public void setSelectedCell( final Location l ) { 96 if ( selectedCell == null && l == null ) { 97 //nothing 98 } else if ( selectedCell == null ) { 99 selectedCell = l; 100 repaintCell( l.x, l.y ); 101 } else if ( l == null ) { 102 repaintCell( selectedCell.x, selectedCell.y ); 103 selectedCell = l; 104 } else { 105 final Location old = selectedCell; 106 selectedCell = l; 107 repaintCell( old.x, old.y ); 108 repaintCell( l.x, l.y ); 109 } 110 } 111 112 public void addHighlightedCell( final Location l, 113 final boolean attack ) { 114 addHighlightedCell( l.x, l.y, attack ); 115 } 116 117 public void addHighlightedCell( final int x, 118 final int y, 119 final boolean attack ) { 120 final byte index = game.toIndex( x, y ); 121 if ( attack ) { 122 attackedCells.set( index ); 123 } else { 124 highlightedCells.set( index ); 125 } 126 repaintCell( x, y ); 127 } 128 129 public void clearHighlightedCells() { 130 highlightedCells.clear(); 131 attackedCells.clear(); 132 repaint(); 133 } 134 135 protected void paint( final Graphics g ) { 136 final int canvasWidth = getWidth(); 137 final int canvasHeight = getHeight(); 138 //clear 139 g.setColor( 0xFFFFFF ); 140 g.fillRect( 0, 0, canvasWidth, canvasHeight ); 141 142 //paint desk 143 if ( game != null ) { 144 final int width = game.getWidth(); 145 final int height = game.getHeight(); 146 147 final int deskWidth = canvasWidth - 2; 148 final int deskHeight = canvasHeight - 2; 149 150 cellSize = Math.min( deskWidth / width, deskHeight / height ); 151 152 if ( cellSize < 2 ) { 153 throw new IllegalStateException( "Too small canvas" ); 154 } 155 156 final int _deskWidth = cellSize * width; 157 final int _deskHeight = cellSize * height; 158 159 vMargin = ( canvasHeight - _deskHeight ) / 2; 160 hMargin = ( canvasWidth - _deskWidth ) / 2; 161 162 for ( int j = 0; j < height; j++ ) { 163 for ( int i = 0; i < width; i++ ) { 164 final int x = i * cellSize + hMargin; 165 final int y = j * cellSize + vMargin; 166 167 final int index = game.toIndex( i, j ); 168 final boolean highlight = highlightedCells.is( index ); 169 final boolean attack = attackedCells.is( index ); 170 if ( ( i % 2 == 0 ) ^ ( j % 2 == 0 ) ) { 171 if ( attack ) { 172 g.setColor( 150, 100, 100 ); 173 } else if ( highlight ) { 174 g.setColor( 100, 150, 100 ); 175 } else { 176 g.setColor( 100, 100, 100 ); 177 } 178 } else { 179 if ( attack ) { 180 g.setColor( 250, 200, 200 ); 181 } else if ( highlight ) { 182 g.setColor( 200, 250, 200 ); 183 } else { 184 g.setColor( 200, 200, 200 ); 185 } 186 } 187 188 g.fillRect( x, y, cellSize, cellSize ); 189 190 final IUnit unit = game.getUnit( i, j ); 191 if ( unit != null ) { 192 unit.paint( g, x, y, cellSize, cellSize ); 193 } 194 } 195 } 196 if ( focusedCell != null ) { 197 g.setColor( 0, 150, 0 ); 198 final int x = focusedCell.x * cellSize + hMargin; 199 final int y = focusedCell.y * cellSize + vMargin; 200 g.drawRect( x + 1, y + 1, cellSize - 2, cellSize - 2 ); 201 g.drawRect( x, y, cellSize, cellSize ); 202 } 203 if ( selectedCell != null ) { 204 g.setColor( 0, 250, 0 ); 205 final int x = selectedCell.x * cellSize + hMargin; 206 final int y = selectedCell.y * cellSize + vMargin; 207 g.drawRect( x + 1, y + 1, cellSize - 2, cellSize - 2 ); 208 g.drawRect( x, y, cellSize, cellSize ); 209 } 210 211 g.setColor( 0 ); 212 g.drawRect( hMargin, vMargin, _deskWidth, _deskHeight ); 213 } 214 } 215 216 private void repaintCell( final int x, 217 final int y ) { 218 final int _x = x * cellSize + hMargin; 219 final int _y = y * cellSize + vMargin; 220 repaint( 221 _x - 1, _y - 1, 222 cellSize + 2, cellSize + 2 223 ); 224 //System.out.println( "queried repaint:[" + x + "," + y + "]" ); 225 //repaint(); 226 } 227 228 protected void keyPressed( final int keyCode ) { 229 super.keyPressed( keyCode ); 230 if ( listener != null ) { 231 listener.keyPressed( keyCode ); 232 } 233 } 234 235 public void commandAction( final Command command, 236 final Displayable displayable ) { 237 if ( command == midlet.helpCommand ) { 238 midlet.showHelp(); 239 } else if ( command == midlet.menuCommand ) { 240 midlet.showStartScreen(); 241 } else if ( listener != null ) { 242 listener.commandAction( command, displayable ); 243 } 244 } 245 246 public void changed() { 247 if ( isShown() ) { 248 repaint(); 249 } 250 } 251 252 private final Screen waitScreen = new WaitScreen( MSG.getMessage( "wait-for-opponent.title" ) ); 253 254 public void setStatus( final int status ) { 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 } 262 } 263 264 public int getStatus() { 265 return status; 266 } 267 268 //public void repaint(){ 269 // this.repaint(); 270 //} 17 public static final int ACTIVE = 0; 18 public static final int WAITING_FOR_NETWORK = 1; 19 public static final int WAITING_FOR_OPPONENT = 2; 20 public static final int DISCONNECTED = 3; 21 22 private static final char[] COLUMNS = new char[] { 23 'A', 'B', 'C', 'D', 'E', 'F', 'J', 'H' 24 }; 25 private static final char[] ROWS = new char[] { 26 '1', '2', '3', '4', '5', '6', '7', '8' 27 }; 28 29 protected final ChessMIDlet midlet; 30 31 public final int fireKey; 32 public final int leftKey; 33 public final int rightKey; 34 public final int upKey; 35 public final int downKey; 36 37 protected Player player; 38 protected IGame game; 39 40 private int status = ACTIVE; 41 42 private Location focusedCell; 43 private Location selectedCell; 44 45 //bitwise masks -- 1 bit per cell 46 private final BitSet64 highlightedCells = new BitSet64(); 47 private final BitSet64 attackedCells = new BitSet64(); 48 49 private IUIListener listener = null; 50 private transient int vMargin; 51 private transient int hMargin; 52 private transient int cellSize; 53 54 55 private static final int MARGIN = 2; 56 //colors 57 private static final int BG_COLOR = 0xFFFFFF; 58 private static final int LABELS_COLOR = 0; 59 60 public GameCanvas( final ChessMIDlet midlet ) { 61 this.midlet = midlet; 62 fireKey = getKeyCode( FIRE ); 63 leftKey = getKeyCode( LEFT ); 64 rightKey = getKeyCode( RIGHT ); 65 upKey = getKeyCode( UP ); 66 downKey = getKeyCode( DOWN ); 67 68 addCommand( midlet.closeCommand ); 69 addCommand( midlet.helpCommand ); 70 setCommandListener( this ); 71 } 72 73 public void init( final IGame game ) { 74 this.game = game; 75 game.setListener( this ); 76 } 77 78 public void setUIListener( final IUIListener listener ) { 79 this.listener = listener; 80 } 81 82 public Location getFocusedCell() { 83 return focusedCell; 84 } 85 86 public void setFocusedCell( final Location l ) { 87 if( focusedCell == null && l == null ) { 88 //nothing 89 } else if( focusedCell == null ) { 90 focusedCell = l; 91 repaintCell( l.x, l.y ); 92 } else if( l == null ) { 93 repaintCell( focusedCell.x, focusedCell.y ); 94 focusedCell = l; 95 } else { 96 final Location old = focusedCell; 97 focusedCell = l; 98 repaintCell( old.x, old.y ); 99 repaintCell( l.x, l.y ); 100 } 101 } 102 103 public Location getSelectedCell() { 104 return selectedCell; 105 } 106 107 public void setSelectedCell( final Location l ) { 108 if( selectedCell == null && l == null ) { 109 //nothing 110 } else if( selectedCell == null ) { 111 selectedCell = l; 112 repaintCell( l.x, l.y ); 113 } else if( l == null ) { 114 repaintCell( selectedCell.x, selectedCell.y ); 115 selectedCell = l; 116 } else { 117 final Location old = selectedCell; 118 selectedCell = l; 119 repaintCell( old.x, old.y ); 120 repaintCell( l.x, l.y ); 121 } 122 } 123 124 public void addHighlightedCell( final Location l, 125 final boolean attack ) { 126 addHighlightedCell( l.x, l.y, attack ); 127 } 128 129 public void addHighlightedCell( final int x, 130 final int y, 131 final boolean attack ) { 132 final byte index = game.toIndex( x, y ); 133 if( attack ) { 134 attackedCells.set( index ); 135 } else { 136 highlightedCells.set( index ); 137 } 138 repaintCell( x, y ); 139 } 140 141 public void clearHighlightedCells() { 142 highlightedCells.clear(); 143 attackedCells.clear(); 144 repaint(); 145 } 146 147 protected void paint( final Graphics g ) { 148 final int canvasWidth = getWidth(); 149 final int canvasHeight = getHeight(); 150 //clear 151 g.setColor( BG_COLOR ); 152 g.fillRect( 0, 0, canvasWidth, canvasHeight ); 153 154 //paint desk 155 if( game != null ) { 156 final int width = game.getWidth(); 157 final int height = game.getHeight(); 158 159 final int deskWidth = canvasWidth - MARGIN; 160 final int deskHeight = canvasHeight - MARGIN; 161 162 //ðåçåðâèðóåì ñòîëáöû/ñòðîêè äëÿ ïîîäïèñåé 163 cellSize = Math.min( 164 deskWidth / ( width + 2 ), 165 deskHeight / ( height + 2 ) 166 ); 167 168 if( cellSize < 2 ) { 169 throw new IllegalStateException( "Too small canvas" ); 170 } 171 172 final int _deskWidth = cellSize * ( width + 2 ); 173 final int _deskHeight = cellSize * ( height + 2 ); 174 175 vMargin = ( canvasHeight - _deskHeight ) / 2 + cellSize; 176 hMargin = ( canvasWidth - _deskWidth ) / 2 + cellSize; 177 178 //draw labels 179 { 180 g.setColor( LABELS_COLOR ); 181 final int x1 = toScreenX( -1 ); 182 final int x2 = toScreenX( width + 1 ); 183 for( int i = 0; i < height; i++ ) { 184 final int y = toScreenY( i ); 185 186 final int baseline = y + cellSize * 3 / 4; 187 g.drawChar( 188 ROWS[i], 189 x1 + cellSize / 2, 190 baseline, 191 Graphics.HCENTER | Graphics.BASELINE 192 ); 193 g.drawChar( 194 ROWS[i], 195 x2 + cellSize / 2, 196 baseline, 197 Graphics.HCENTER | Graphics.BASELINE 198 ); 199 } 200 final int y1 = toScreenY( -1 ); 201 final int y2 = toScreenY( height + 1); 202 for( int i = 0; i < width; i++ ) { 203 final int x = toScreenX( i ); 204 205 final int baseline1 = y1 + cellSize * 3 / 4; 206 final int baseline2 = y2 + cellSize * 3 / 4; 207 g.drawChar( 208 COLUMNS[i], 209 x + cellSize / 2, 210 baseline1, 211 Graphics.HCENTER | Graphics.BASELINE 212 ); 213 g.drawChar( 214 COLUMNS[i], 215 x + cellSize / 2, 216 baseline2, 217 Graphics.HCENTER | Graphics.BASELINE 218 ); 219 } 220 } 221 222 //draw desk and units 223 for( int _y = 0; _y < height; _y++ ) { 224 for( int _x = 0; _x < width; _x++ ) { 225 final int x = toScreenX( _x ); 226 final int y = toScreenY( _y ); 227 228 final int index = game.toIndex( _x, _y ); 229 //A1 -- ÷åðíîå 230 final boolean black = ( _x % 2 == 0 ) ^ ( _y % 2 != 0 ); 231 final boolean highlight = highlightedCells.is( index ); 232 final boolean attack = attackedCells.is( index ); 233 if( black ) { 234 if( attack ) { 235 g.setColor( 150, 100, 100 ); 236 } else if( highlight ) { 237 g.setColor( 100, 150, 100 ); 238 } else { 239 g.setColor( 100, 100, 100 ); 240 } 241 } else { 242 if( attack ) { 243 g.setColor( 250, 200, 200 ); 244 } else if( highlight ) { 245 g.setColor( 200, 250, 200 ); 246 } else { 247 g.setColor( 200, 200, 200 ); 248 } 249 } 250 251 g.fillRect( x, y, cellSize, cellSize ); 252 253 final IUnit unit = game.getUnit( _x, _y ); 254 if( unit != null ) { 255 unit.paint( g, x, y, cellSize, cellSize ); 256 } 257 } 258 } 259 if( focusedCell != null ) { 260 g.setColor( 0, 150, 0 ); 261 final int x = toScreenX( focusedCell.x ); 262 final int y = toScreenY( focusedCell.y ); 263 g.drawRect( x + 1, y + 1, cellSize - 2, cellSize - 2 ); 264 g.drawRect( x, y, cellSize, cellSize ); 265 } 266 if( selectedCell != null ) { 267 g.setColor( 0, 250, 0 ); 268 final int x = toScreenX( selectedCell.x ); 269 final int y = toScreenY( selectedCell.y ); 270 g.drawRect( x + 1, y + 1, cellSize - 2, cellSize - 2 ); 271 g.drawRect( x, y, cellSize, cellSize ); 272 } 273 274 g.setColor( 0 ); 275 g.drawRect( 276 hMargin - cellSize, 277 vMargin - cellSize, 278 _deskWidth, 279 _deskHeight 280 ); 281 g.drawRect( 282 hMargin, 283 vMargin, 284 _deskWidth - cellSize * 2, 285 _deskHeight - cellSize * 2 286 ); 287 } 288 } 289 290 private int toScreenX( final int x ) { 291 return x * cellSize + hMargin; 292 } 293 294 private int toScreenY( final int y ) { 295 return ( game.getHeight() - 1 - y ) * cellSize + hMargin; 296 } 297 298 private void repaintCell( final int x, 299 final int y ) { 300 final int _x = x * cellSize + hMargin; 301 final int _y = y * cellSize + vMargin; 302 repaint( 303 _x - 1, _y - 1, 304 cellSize + 2, cellSize + 2 305 ); 306 //System.out.println( "queried repaint:[" + x + "," + y + "]" ); 307 //repaint(); 308 } 309 310 protected void keyPressed( final int keyCode ) { 311 super.keyPressed( keyCode ); 312 if( listener != null ) { 313 listener.keyPressed( keyCode ); 314 } 315 } 316 317 public void commandAction( final Command command, 318 final Displayable displayable ) { 319 if( command == midlet.helpCommand ) { 320 midlet.showHelp(); 321 } else if( command == midlet.closeCommand ) { 322 midlet.showStartScreen(); 323 } else if( listener != null ) { 324 listener.commandAction( command, displayable ); 325 } 326 } 327 328 public void changed() { 329 if( isShown() ) { 330 repaint(); 331 } 332 } 333 334 private final Screen waitScreen = new WaitScreen( MSG.getMessage( "wait-for-opponent.title" ) ); 335 336 public void setStatus( final int status ) { 337 this.status = status; 338 //todo move it to ChessMIDlet? 339 if( status != ACTIVE ) { 340 midlet.display.setCurrent( waitScreen ); 341 } else { 342 midlet.display.setCurrent( this ); 343 } 344 } 345 346 public int getStatus() { 347 return status; 348 } 349 350 //public void repaint(){ 351 // this.repaint(); 352 //} 271 353 } -
chess/src/chess/ui/HelpScreen.java
r4 r7 1 1 package chess.ui; 2 2 3 import javax.microedition.lcdui.Command; 4 import javax.microedition.lcdui.CommandListener; 5 import javax.microedition.lcdui.Displayable; 6 import javax.microedition.lcdui.Form; 3 import javax.microedition.lcdui.*; 4 5 import chess.MSG; 7 6 8 7 /** … … 12 11 * Time: 15:24:01<br/> 13 12 */ 14 public class HelpScreen extends Formimplements CommandListener {15 private final MyMIDlet midlet;13 public class HelpScreen extends Alert implements CommandListener { 14 private final ChessMIDlet midlet; 16 15 17 public HelpScreen( final MyMIDlet midlet ) { 18 super( "Help" ); 19 this.midlet = midlet; 16 public HelpScreen( final ChessMIDlet midlet ) { 17 super( 18 MSG.getMessage( "help.title" ), 19 MSG.getMessage( "help.content" ), 20 null, 21 AlertType.INFO 22 ); 23 this.midlet = midlet; 20 24 21 addCommand( midlet.okCommand ); 22 setCommandListener( this ); 25 setTimeout( FOREVER ); 26 // addCommand( midlet.okCommand ); 27 // setCommandListener( this ); 28 } 23 29 24 append( "this is a short info about game" ); 25 } 26 27 public void commandAction( final Command command, 28 final Displayable displayable ) { 29 midlet.showStartScreen(); 30 } 30 public void commandAction( final Command command, 31 final Displayable displayable ) { 32 // midlet.showStartScreen(); 33 } 31 34 } -
chess/src/chess/ui/JoinGameScreen.java
r5 r7 14 14 */ 15 15 public class JoinGameScreen extends List implements CommandListener, IClientListener { 16 private final MyMIDlet midlet;16 private final ChessMIDlet midlet; 17 17 18 18 private IRemoteNode[] nodes; … … 33 33 ); 34 34 35 public JoinGameScreen( final MyMIDlet midlet ) {35 public JoinGameScreen( final ChessMIDlet midlet ) { 36 36 super( MSG.getMessage( "join-game.title" ), Choice.EXCLUSIVE ); 37 37 this.midlet = midlet; … … 45 45 46 46 setSelectCommand( midlet.okCommand ); 47 48 47 } 49 48 -
chess/src/chess/ui/StartScreen.java
r4 r7 12 12 */ 13 13 public class StartScreen extends List implements CommandListener { 14 private final MyMIDlet midlet;14 private final ChessMIDlet midlet; 15 15 16 public StartScreen( final MyMIDlet midlet ) {17 18 16 public StartScreen( final ChessMIDlet midlet ) { 17 super( MSG.getMessage( "start-menu.title" ), Choice.IMPLICIT ); 18 this.midlet = midlet; 19 19 20 21 22 20 addCommand( midlet.okCommand ); 21 addCommand( midlet.exitCommand ); 22 setSelectCommand( midlet.okCommand ); 23 23 24 25 26 24 append( MSG.getMessage( "start-menu.new-game" ), null ); 25 append( MSG.getMessage( "start-menu.join-game" ), null ); 26 append( MSG.getMessage( "start-menu.help" ), null ); 27 27 28 29 28 setCommandListener( this ); 29 } 30 30 31 32 33 34 switch( index ) {35 36 midlet.newGame();37 38 39 40 41 42 43 44 45 31 public void commandAction( final Command command, 32 final Displayable displayable ) { 33 final int index = getSelectedIndex(); 34 switch( index ) { 35 case 0: 36 midlet.choosePlayer(); 37 return; 38 case 1: 39 midlet.searchGame(); 40 return; 41 case 2: 42 midlet.showHelp(); 43 return; 44 } 45 } 46 46 } -
chess/src/chess/ui/WaitScreen.java
r4 r7 1 1 package chess.ui; 2 2 3 import javax.microedition.lcdui. Form;3 import javax.microedition.lcdui.Alert; 4 4 5 5 … … 10 10 * Time: 11:13:19<br/> 11 11 */ 12 public class WaitScreen extends Form{12 public class WaitScreen extends Alert { 13 13 14 14 public WaitScreen( final String message ) { 15 15 super( message ); 16 17 append( message ); 16 setTimeout( Alert.FOREVER ); 18 17 } 19 18
Note: See TracChangeset
for help on using the changeset viewer.