- Timestamp:
- 02/04/09 17:04:21 (13 years ago)
- Location:
- chess
- Files:
-
- 3 added
- 21 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
chess/chess.jad
r18 r19 1 MIDlet-1: Chess game, , chess.ui.ChessMIDlet 2 MIDlet-Jar-Size: 75780 1 MIDlet-Jar-Size: 855926 3 2 MIDlet-Jar-URL: chess.jar 4 3 MIDlet-Name: chess 5 4 MIDlet-Vendor: I am 6 5 MIDlet-Version: 1.0 7 MicroEdition-Configuration: CLDC-1. 08 MicroEdition-Profile: MIDP-2. 06 MicroEdition-Configuration: CLDC-1.1 7 MicroEdition-Profile: MIDP-2.1 -
chess/src/chess/Util.java
r9 r19 2 2 3 3 import java.io.PrintStream; 4 import java.io.OutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 8 import javax.microedition.rms.RecordStore; 9 10 import chess.remote.IRemote; 11 import chess.remote.impl.debug.DebugRemote; 12 import chess.remote.impl.Remote; 4 13 5 14 … … 9 18 */ 10 19 public final class Util { 11 private Util(){12 20 private Util() { 21 } 13 22 14 23 public static final PrintStream log = System.out; 15 24 16 17 18 19 20 25 /** 26 * âêëþ÷àåò äîïîëíèòåëüíûå ïðîâåðêè íà ñòàäèè âûïîëíåíèÿ, 27 * ïðè ýòîì îòêëþ÷àòü èõ íà ñòàäèè ïîäãîòîâêè production 28 */ 29 public static final boolean SAFE_MODE = true; 21 30 22 31 32 public static IRemote create() throws Exception { 33 return new Remote(); 34 //return new DebugRemote( 100, 100 ); 35 } 36 37 public static void writeInt( final OutputStream os, 38 final int value ) throws IOException { 39 os.write( value ); 40 os.write( value >> 8 ); 41 os.write( value >> 16 ); 42 os.write( value >> 24 ); 43 } 44 45 public static int readInt( final InputStream is ) throws IOException { 46 return is.read() | is.read() << 8 | is.read() << 16 | is.read() << 24; 47 } 48 49 public static int save( final String name, 50 final byte[] data ) throws Exception { 51 final RecordStore store = RecordStore.openRecordStore( name, true ); 52 try { 53 return store.addRecord( data, 0, data.length ); 54 } finally { 55 store.closeRecordStore(); 56 } 57 } 23 58 } -
chess/src/chess/control/BaseController.java
r17 r19 48 48 this.player = player; 49 49 this.node = node; 50 51 master = player != null; 50 game.setOwner( player ); 51 52 master = ( player != null ); 52 53 53 54 node.setListener( this ); 54 node. ensureConnected();55 node.start(); 55 56 56 57 ui.setUIListener( this ); … … 60 61 } else { 61 62 //wait for incoming actions log 62 synchronized ( this ) {63 while ( this.player == null ) {64 try {65 //ñòðàõóåìñÿ íà ñëó÷àé, åñëè ñîáûòèÿ óæå óñïåëè ïðèéòè66 wait( 150 );67 } catch ( InterruptedException e ) {68 e.printStackTrace();69 }70 }71 }63 //synchronized ( this ) { 64 // while ( this.player == null ) { 65 // try { 66 // //ñòðàõóåìñÿ íà ñëó÷àé, åñëè ñîáûòèÿ óæå óñïåëè ïðèéòè 67 // wait( 150 ); 68 // } catch ( InterruptedException e ) { 69 // e.printStackTrace(); 70 // } 71 // } 72 //} 72 73 } 73 74 lastTimeStamp = System.currentTimeMillis(); … … 121 122 if ( sa.type() == SystemAction.SET_PLAYER ) { 122 123 player = sa.getPlayer(); 124 game.setOwner( player ); 123 125 if ( isMaster() ) { 124 126 throw new IllegalStateException( "Master should not got SET_PLAYER commands" ); -
chess/src/chess/game/Action.java
r17 r19 6 6 7 7 import chess.remote.IDatagram; 8 import chess.Util; 8 9 9 10 … … 16 17 public class Action implements IDatagram { 17 18 private Player player; 18 //âðÿä ëè êòî-òî áóäåò äóìàòü áîëüøå ìèëëèîíà ñåêóíä 19 //âðÿä ëè êòî-òî áóäåò äóìàòü áîëüøå ìèëëèîíà ñåêóíä (~12 ñóòîê) 19 20 private int duration; 20 21 21 private Location from; 22 private Location to; 22 private int fromX; 23 private int fromY; 24 25 private int toX; 26 private int toY; 27 28 //private Location from; 29 //private Location to; 23 30 24 31 public Action( final InputStream is ) throws IOException { … … 30 37 final Location from, 31 38 final Location to ) { 39 this( player, duration, from.x, from.y, to.x, to.y ); 40 } 41 42 public Action( final Player player, 43 final long duration, 44 final int xFrom, 45 final int yFrom, 46 final int xTo, 47 final int yTo ) { 32 48 this.player = player; 33 49 if ( duration > Integer.MAX_VALUE ) { … … 35 51 } 36 52 this.duration = ( int )duration; 37 this.from = from; 38 this.to = to; 53 this.fromX = xFrom; 54 this.fromY = yFrom; 55 this.toX = xTo; 56 this.toY = yTo; 39 57 } 40 58 41 public Action( final Player player, 42 final int duration, 43 final int xFrom, 44 final int yFrom, 45 final int xTo, 46 final int yTo ) { 47 this( player, duration, 48 new Location( xFrom, yFrom ), 49 new Location( xTo, yTo ) 50 ); 59 public int fromX() { 60 return fromX; 51 61 } 52 62 53 public Location from() {54 return from ;63 public int fromY() { 64 return fromY; 55 65 } 56 66 57 public Location to() { 58 return to; 67 public int toX() { 68 return toX; 69 } 70 71 public int toY() { 72 return toY; 59 73 } 60 74 … … 70 84 os.write( player.ordinal() ); 71 85 72 os.write( duration ); 73 os.write( duration >> 8 ); 74 os.write( duration >> 16 ); 75 os.write( duration >> 24 ); 86 Util.writeInt( os, duration ); 76 87 77 os.write( from .x);78 os.write( from .y);79 os.write( to .x);80 os.write( to .y);88 os.write( fromX ); 89 os.write( fromY ); 90 os.write( toX ); 91 os.write( toY ); 81 92 } 82 93 83 94 public void read( final InputStream is ) throws IOException { 84 95 player = Player.byType( is.read() ); 85 duration = is.read() | is.read() << 8 | is.read() << 16 | is.read() << 24; 86 from = new Location( is.read(), is.read() ); 87 to = new Location( is.read(), is.read() ); 96 duration = Util.readInt( is ); 97 fromX = is.read(); 98 fromY = is.read(); 99 toX = is.read(); 100 toY = is.read(); 88 101 } 89 102 … … 97 110 } 98 111 99 final Action a ction= ( Action )o;112 final Action a = ( Action )o; 100 113 101 if ( duration != a ction.duration ) {114 if ( duration != a.duration ) { 102 115 return false; 103 116 } 104 if ( !from.equals( action.from ) ) { 105 return false; 106 } 107 if ( !player.equals( action.player ) ) { 108 return false; 109 } 110 if ( !to.equals( action.to ) ) { 117 if ( !player.equals( a.player ) ) { 111 118 return false; 112 119 } 113 120 114 return true; 121 122 return fromX == a.fromX && fromY == a.fromY 123 && toX == a.toX && toY == a.toY; 115 124 } 116 125 … … 119 128 result = player.hashCode(); 120 129 result = 31 * result + duration; 121 result = 31 * result + from.hashCode(); 122 result = 31 * result + to.hashCode(); 130 result = 31 * result + fromX; 131 result = 31 * result + fromY; 132 result = 31 * result + toX; 133 result = 31 * result + toY; 123 134 return result; 124 135 } 125 136 126 137 public String toString() { 127 return player + ": " + from + " -> " + to + "(" + duration + " ms)";138 return player + ": [" + fromX + ", " + fromY + "] -> [" + toX + ", " + toY + "] (" + duration + " ms)"; 128 139 } 129 140 } -
chess/src/chess/game/Game.java
r17 r19 2 2 3 3 import java.util.*; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.io.IOException; 4 7 5 8 import chess.Util; … … 16 19 private static final int SIZE = 8; 17 20 18 19 21 private IGameListener listener = null; 20 22 … … 28 30 private final long[] timings = new long[]{ 0, 0 }; 29 31 32 private Player turnOwner; 33 34 private Player owner; 35 30 36 public Game() { 37 //this.owner = owner; 31 38 activeUnits[0] = new Vector( 16 ); 32 39 activeUnits[1] = new Vector( 16 ); … … 62 69 put( new King( Player.WHITE, this, new Location( 4, yWhite ) ) ); 63 70 put( new King( Player.BLACK, this, new Location( 4, yBlack ) ) ); 71 72 setTurnOwner( Player.WHITE ); 73 } 74 75 //public Game( final InputStream is ) throws IOException { 76 // this(); 77 // load( is ); 78 //} 79 80 public Player getOwner() { 81 return owner; 82 } 83 84 public void setOwner( final Player owner ) { 85 this.owner = owner; 86 } 87 88 public Player getTurnOwner() { 89 return turnOwner; 90 } 91 92 public void setTurnOwner( final Player turnOwner ) { 93 this.turnOwner = turnOwner; 64 94 } 65 95 … … 148 178 149 179 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 ); 180 final int fromX, 181 final int fromY, 182 final int toX, 183 final int toY ) { 184 desk[toIndex( fromX, fromY )] = null; 185 desk[toIndex( toX, toY )] = u; 186 u.setLocation( toX, toY ); 155 187 } 156 188 157 189 public void doAction( final Action a ) { 158 190 //todo Action is mutable. a.clone()? 191 if ( a.getPlayer() != turnOwner ) { 192 throw new IllegalArgumentException( "Current turn is " + turnOwner + ", but " + a.getPlayer() + " trying to move!" ); 193 } 159 194 actionsLog.addElement( a ); 195 160 196 //todo check, can we really make movement? 161 final Location from = a.from(); 162 final IUnit actor = getUnit( from ); 197 final int fromX = a.fromX(); 198 final int fromY = a.fromY(); 199 final IUnit actor = getUnit( fromX, fromY ); 163 200 if ( actor == null ) { 164 throw new IllegalStateException( "Where is no unit at " + from);201 throw new IllegalStateException( "Where is no unit at [" + fromX + ", " + fromY + "]" ); 165 202 } 166 203 if ( actor.getOwner() != a.getPlayer() ) { … … 168 205 } 169 206 170 final Location to = a.to(); 171 final IUnit victim = getUnit( to ); 207 final int toX = a.toX(); 208 final int toY = a.toY(); 209 final IUnit victim = getUnit( toX, toY ); 172 210 if ( victim != null && victim.getOwner() == actor.getOwner() ) { 173 211 throw new IllegalStateException( "Can't take own unit:" + actor + " <> " + victim ); … … 177 215 kill( victim ); 178 216 } 179 move( ( AUnit )actor, from , to);217 move( ( AUnit )actor, fromX, fromY, toX, toY ); 180 218 181 219 timings[a.getPlayer().ordinal()] += a.duration(); 182 220 221 setTurnOwner( a.getPlayer().opponent() ); 183 222 if ( listener != null ) { 184 223 listener.changed(); … … 215 254 } 216 255 217 private finalvoid check( final int x,218 256 private void check( final int x, 257 final int y ) { 219 258 if ( Util.SAFE_MODE && !isValid( x, y ) ) { 220 259 throw new IllegalArgumentException( "Invalid location:" + x + ", " + y + " is out of bounds!" ); 221 260 } 222 261 } 262 263 public void save( final OutputStream os ) throws IOException { 264 final int size = actionsLog.size(); 265 Util.writeInt( os, size ); 266 for ( int i = 0; i < size; i++ ) { 267 final Action a = ( Action )actionsLog.elementAt( i ); 268 a.write( os ); 269 } 270 } 271 272 public void load( final InputStream is ) throws IOException { 273 final int size = Util.readInt( is ); 274 actionsLog.ensureCapacity( size ); 275 for ( int i = 0; i < size; i++ ) { 276 final Action a = new Action( is ); 277 doAction( a ); 278 } 279 } 223 280 } -
chess/src/chess/game/IGame.java
r17 r19 10 10 */ 11 11 public interface IGame { 12 public Player getTurnOwner(); 13 14 public Player getOwner(); 15 16 public void setOwner( final Player p ); 17 12 18 public int getWidth(); 13 19 -
chess/src/chess/game/units/AUnit.java
r17 r19 68 68 } 69 69 70 public void setLocation( final int x, 71 final int y ) { 72 setLocation( new Location( x, y ) ); 73 } 74 70 75 public boolean canTransform() { 71 76 return false; … … 108 113 } 109 114 return true; 110 115 } 111 116 } -
chess/src/chess/game/units/Bishop.java
r17 r19 57 57 58 58 protected char getSymbol( final boolean white ) { 59 if( white ) { 60 return '?'; 61 } else { 62 return '?'; 63 } 59 //if( white ) { 60 // return '?'; 61 //} else { 62 // return '?'; 63 //} 64 return 'B'; 64 65 } 65 66 } -
chess/src/chess/game/units/Castle.java
r17 r19 51 51 52 52 protected char getSymbol(final boolean white) { 53 if( white ) { 54 return '?'; 55 } else { 56 return '?'; 57 } 53 //if( white ) { 54 // return '?'; 55 //} else { 56 // return '?'; 57 //} 58 return 'C'; 58 59 } 59 60 } -
chess/src/chess/game/units/King.java
r17 r19 55 55 56 56 protected char getSymbol( final boolean white ) { 57 if( white ) { 58 return '?'; 59 } else { 60 return '?'; 61 } 57 //if( white ) { 58 // return '?'; 59 //} else { 60 // return '?'; 61 //} 62 return 'K'; 62 63 } 63 64 -
chess/src/chess/game/units/Knight.java
r17 r19 63 63 64 64 protected char getSymbol( final boolean white ) { 65 if( white ) { 66 return '?'; 67 } else { 68 return '?'; 69 } 65 //if( white ) { 66 // return '?'; 67 //} else { 68 // return '?'; 69 //} 70 return 'L'; 70 71 } 71 72 } -
chess/src/chess/game/units/Pawn.java
r17 r19 92 92 93 93 protected char getSymbol( final boolean white ) { 94 if( white ) { 95 return '?'; 96 } else { 97 return '?'; 98 } 94 //if( white ) { 95 // return '?'; 96 //} else { 97 // return '?'; 98 //} 99 return 'P'; 99 100 } 100 101 } -
chess/src/chess/game/units/Queen.java
r17 r19 12 12 */ 13 13 public class Queen extends AUnit { 14 15 16 17 18 14 public Queen( final Player player, 15 final IGame game, 16 final Location location ) { 17 super( player, game, location ); 18 } 19 19 20 21 20 public Location[] canMoveTo() { 21 _temp.removeAllElements(); 22 22 23 24 25 26 for( int x = l.x + 1; x < width; x++ ) {27 if( !checkAndAdd( this, game, x, l.y ) ) {28 29 30 31 for( int x = l.x - 1; x >= 0; x-- ) {32 if( !checkAndAdd( this, game, x, l.y ) ) {33 34 35 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 } 36 36 37 for( int y = l.y + 1; y < height; y++ ) {38 if( !checkAndAdd( this, game, l.x, y ) ) {39 40 41 42 for( int y = l.y - 1; y >= 0; y-- ) {43 if( !checkAndAdd( this, game, l.x, y ) ) {44 45 46 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 } 47 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 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 } 78 79 protected char getSymbol( final boolean white ) { 80 if( white ) { 81 return '?'; 82 } else { 83 return '?'; 84 } 85 } 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 } 78 79 protected char getSymbol( final boolean white ) { 80 //if( white ) { 81 // return '?'; 82 //} else { 83 // return '?'; 84 //} 85 return 'P'; 86 } 86 87 } -
chess/src/chess/remote/IRemoteNode.java
r9 r19 1 1 package chess.remote; 2 2 3 import java.io.IOException;4 3 5 4 /** … … 14 13 public void setListener( final IConnectionListener l ); 15 14 16 public void ensureConnected() throws IOException;15 public void start(); 17 16 18 17 public void send( final IDatagram data ); -
chess/src/chess/remote/impl/Remote.java
r17 r19 20 20 */ 21 21 public class Remote implements IRemote { 22 private static final String MY_SERVICE_NUMBER = "3B9FA89520078C303355AAA694238F08;name=ChessGameServer"; 22 public static final String BTSPP_SCHEME = "btspp://"; 23 public static final String MY_SERVICE_NUMBER = "3B9FA89520078C303355AAA694238F08;name=ChessGameServer"; 23 24 24 25 public static final IRemoteNode[] EMPTY = new IRemoteNode[0]; … … 27 28 private final Vector devices = new Vector(); 28 29 29 p rivateRemote() throws Exception {30 public Remote() throws Exception { 30 31 localDevice = LocalDevice.getLocalDevice(); 31 32 fillupCache(); … … 95 96 } 96 97 97 public static IRemote create() throws Exception {98 //return new Remote();99 // return new DummyRemote();100 return new DebugRemote( 100, 100 );101 }102 103 98 private static class WaitForIncomingThread extends BaseAsyncProcess { 104 99 private final LocalDevice localDevice; … … 118 113 try { 119 114 final StreamConnectionNotifier server = ( StreamConnectionNotifier )Connector.open( 120 "btspp://localhost:" + MY_SERVICE_NUMBER,115 BTSPP_SCHEME +"localhost:" + MY_SERVICE_NUMBER, 121 116 Connector.READ_WRITE, 122 117 true … … 127 122 // Îæèäàåì âõîäÿùåå ïîòîêîâîå ñîåäèíåíèå 128 123 final StreamConnection conn = server.acceptAndOpen(); 129 Util.log.println( "accepted incoming " + conn );124 Util.log.println( "accepted incoming:" + conn ); 130 125 final RemoteNode client = new RemoteNode( conn ); 131 126 if ( listener.incoming( client ) ) { -
chess/src/chess/remote/impl/RemoteNode.java
r9 r19 21 21 public class RemoteNode implements IRemoteNode, Runnable { 22 22 23 private String name; 24 25 private RemoteDevice device; 26 private IConnectionListener listener = null; 27 private StreamConnection connection = null; 28 29 private transient InputStream in; 30 private transient OutputStream out; 31 private transient Thread pump; 32 33 public RemoteNode( final RemoteDevice device ) throws IOException { 34 this.device = device; 35 name = device.getFriendlyName( false ); 36 } 37 38 public RemoteNode( final StreamConnection conn ) throws IOException { 39 this.connection = conn; 40 this.device = RemoteDevice.getRemoteDevice( connection ); 41 this.name = device.getFriendlyName( true ); 42 } 43 44 public RemoteNode( final String name, 45 final StreamConnection conn ) throws IOException { 46 this.name = name; 47 this.connection = conn; 48 this.device = RemoteDevice.getRemoteDevice( conn ); 49 } 50 51 public String getName() { 52 return name; 53 } 54 55 public void ensureConnected() throws IOException { 56 if( device == null ) { 57 throw new IllegalStateException( "Already closed node" ); 58 } else if( connection == null ) { 59 connection = ( StreamConnection ) Connector.open( device.getBluetoothAddress() ); 60 } 61 62 try { 63 in = connection.openInputStream(); 64 } catch( IOException ex ) { 65 ex.printStackTrace(); 66 throw new RuntimeException( "Bad:" + ex.getMessage() ); 67 } 68 try { 69 out = connection.openOutputStream(); 70 } catch( IOException ex ) { 71 ex.printStackTrace(); 72 throw new RuntimeException( "Bad:" + ex.getMessage() ); 73 } 74 if( pump != null && pump.isAlive() ) { 75 pump.interrupt(); 76 } 77 pump = new Thread( this, "network pump" ); 78 pump.start(); 79 // return this; 80 } 81 82 public void setListener( final IConnectionListener l ) { 83 this.listener = l; 84 } 85 86 public void send( final IDatagram data ) { 87 if( out != null ) { 88 try { 89 final GenericDatagram gdata = new GenericDatagram( data ); 90 gdata.write( out ); 91 } catch( IOException ex ) { 92 ex.printStackTrace(); 93 throw new RuntimeException( ex.getMessage() ); 94 } 95 if( listener != null ) { 96 listener.wasSent( data ); 97 } 98 } 99 } 100 101 public void close() { 102 if( in != null ) { 103 try { 104 in.close(); 105 } catch( IOException e ) { 106 e.printStackTrace(); 107 } finally { 108 in = null; 109 } 110 } 111 if( out != null ) { 112 try { 113 out.close(); 114 } catch( IOException e ) { 115 e.printStackTrace(); 116 } finally { 117 out = null; 118 } 119 } 120 if( connection != null ) { 121 try { 122 connection.close(); 123 } catch( IOException e ) { 124 e.printStackTrace(); 125 } finally { 126 connection = null; 127 } 128 } 129 if( listener != null ) { 130 listener.closed(); 131 } 132 if( pump != null && pump.isAlive() ) { 133 pump.interrupt(); 134 } 135 pump = null; 136 device = null; 137 } 138 139 140 public void run() { 141 try { 142 while( true ) { 143 synchronized( this ) { 144 if( in.available() > 0 ) { 145 final GenericDatagram data = new GenericDatagram( in ); 146 if( listener != null ) { 147 listener.received( data.wrapped ); 148 } 149 } 150 try { 151 wait( 100 ); 152 } catch( InterruptedException e ) { 153 } 154 } 155 } 156 } catch( IOException e ) { 157 e.printStackTrace(); 158 return; 159 } 160 } 161 162 163 public String toString() { 164 return getName(); 165 } 23 private String name; 24 25 private RemoteDevice device; 26 private IConnectionListener listener = null; 27 private StreamConnection connection = null; 28 29 private transient InputStream in; 30 private transient OutputStream out; 31 private transient Thread pump; 32 33 private final Object connectLock = new Object(); 34 35 public RemoteNode( final RemoteDevice device ) throws IOException { 36 this( device, null ); 37 } 38 39 public RemoteNode( final StreamConnection conn ) throws IOException { 40 this( null, null, conn ); 41 } 42 43 public RemoteNode( final RemoteDevice device, 44 final StreamConnection conn ) throws IOException { 45 this( null, device, conn ); 46 } 47 48 public RemoteNode( final String name, 49 final RemoteDevice device, 50 final StreamConnection conn ) throws IOException { 51 if ( device == null && conn == null ) { 52 throw new IllegalArgumentException( "No-no-no, David Blein! device =null && conn = null" ); 53 } 54 55 if ( device == null ) { 56 this.device = RemoteDevice.getRemoteDevice( conn ); 57 } else { 58 this.device = device; 59 } 60 if ( name == null ) { 61 this.name = this.device.getFriendlyName( true ); 62 } else { 63 this.name = name; 64 } 65 connection = conn; 66 } 67 68 private static String getBluetoothURL( final RemoteDevice device ) { 69 return Remote.BTSPP_SCHEME + device.getBluetoothAddress() + ":1"; 70 } 71 72 public RemoteNode( final String name, 73 final StreamConnection conn ) throws IOException { 74 this( name, null, conn ); 75 } 76 77 public String getName() { 78 return name; 79 } 80 81 public synchronized void start() { 82 checkStatus(); 83 if ( pump == null ) { 84 pump = new Thread( this, "network pump" ); 85 pump.start(); 86 } 87 } 88 89 private void checkStatus() { 90 if ( isClosed() ) { 91 throw new IllegalStateException( "Already closed node" ); 92 } 93 } 94 95 private boolean isClosed() { 96 return device == null; 97 } 98 99 public synchronized void setListener( final IConnectionListener l ) { 100 //start(); 101 this.listener = l; 102 } 103 104 public void send( final IDatagram data ) { 105 //start(); 106 checkStatus(); 107 synchronized ( connectLock ) { 108 while ( out == null ) { 109 try { 110 connectLock.wait(); 111 } catch ( InterruptedException e ) { 112 e.printStackTrace(); 113 } 114 } 115 } 116 synchronized ( this ) { 117 try { 118 final GenericDatagram gdata = new GenericDatagram( data ); 119 gdata.write( out ); 120 } catch ( IOException ex ) { 121 ex.printStackTrace(); 122 throw new RuntimeException( ex.getMessage() ); 123 } 124 if ( listener != null ) { 125 listener.wasSent( data ); 126 } 127 } 128 } 129 130 public synchronized void close() { 131 if ( isClosed() ) { 132 return; 133 } 134 if ( pump != null && pump.isAlive() ) { 135 pump.interrupt(); 136 if ( pump != Thread.currentThread() ) { 137 try { 138 pump.join(); 139 } catch ( InterruptedException e ) { 140 e.printStackTrace(); 141 } 142 } 143 } 144 if ( in != null ) { 145 synchronized ( in ) { 146 try { 147 in.close(); 148 } catch ( IOException e ) { 149 e.printStackTrace(); 150 } finally { 151 in = null; 152 } 153 } 154 } 155 if ( out != null ) { 156 synchronized ( out ) { 157 try { 158 out.close(); 159 } catch ( IOException e ) { 160 e.printStackTrace(); 161 } finally { 162 out = null; 163 } 164 } 165 } 166 if ( connection != null ) { 167 try { 168 connection.close(); 169 } catch ( IOException e ) { 170 e.printStackTrace(); 171 } finally { 172 connection = null; 173 } 174 } 175 if ( listener != null ) { 176 listener.closed(); 177 } 178 pump = null; 179 device = null; 180 } 181 182 183 public void run() { 184 try { 185 synchronized ( this ) { 186 if ( isClosed() ) { 187 return; 188 } 189 ensurePipelineOpened(); 190 } 191 synchronized ( connectLock ) { 192 connectLock.notifyAll(); 193 } 194 while ( !isClosed() ) { 195 synchronized ( this ) { 196 final int timeout; 197 if ( in.available() > 0 ) { 198 final GenericDatagram data = new GenericDatagram( in ); 199 if ( listener != null ) { 200 listener.received( data.wrapped ); 201 } 202 timeout = 50; 203 } else { 204 timeout = 250; 205 } 206 try { 207 wait( timeout ); 208 } catch ( InterruptedException e ) { 209 } 210 } 211 } 212 } catch ( Throwable e ) { 213 e.printStackTrace(); 214 return; 215 } finally { 216 pump = null; 217 close(); 218 } 219 } 220 221 private void ensurePipelineOpened() throws IOException { 222 if ( connection == null ) { 223 final String addr = getBluetoothURL( device ); 224 connection = ( StreamConnection )Connector.open( addr ); 225 } 226 if ( in == null ) { 227 try { 228 in = connection.openInputStream(); 229 } catch ( IOException ex ) { 230 ex.printStackTrace(); 231 throw new RuntimeException( "Can't open input stream:" + ex.getMessage() ); 232 } 233 } 234 if ( out == null ) { 235 try { 236 out = connection.openOutputStream(); 237 } catch ( IOException ex ) { 238 ex.printStackTrace(); 239 throw new RuntimeException( "Can't open output stream:" + ex.getMessage() ); 240 } 241 } 242 } 243 244 245 public String toString() { 246 return getName(); 247 } 166 248 } -
chess/src/chess/remote/impl/debug/DebugRemote.java
r17 r19 149 149 } 150 150 151 public void ensureConnected() throws IOException{151 public void start() { 152 152 153 153 } … … 225 225 } 226 226 227 public void ensureConnected() throws IOException{227 public void start() { 228 228 if ( listener == null ) { 229 throw new IllegalStateException( "listener can't be null in ensureConnected!" );229 throw new IllegalStateException( "listener can't be null in start!" ); 230 230 } 231 231 //ïîñûëàåì íà÷àëüíóþ ïîñëåäîâàòåëüíîñòü -- -
chess/src/chess/ui/ChessMIDlet.java
r15 r19 10 10 import chess.game.Player; 11 11 import chess.remote.*; 12 import chess.remote.impl.Remote;13 12 14 13 … … 40 39 41 40 public final Screen startForm = new StartMenuScreen( this ); 42 private final Screen choosePlayerForm = new ChoosePlayer Form( this );41 private final Screen choosePlayerForm = new ChoosePlayerScreen( this ); 43 42 private final GameCanvas gameCanvas = new GameCanvas( this ); 44 43 private final WaitScreen waitScreen = new WaitScreen( … … 59 58 public ChessMIDlet() throws Exception { 60 59 display = Display.getDisplay( this ); 61 remote = Remote.create();60 remote = Util.create(); 62 61 63 62 waitScreen.addCommand( cancelCommand ); … … 71 70 public void connectToServer( final IRemoteNode server ) { 72 71 try { 72 display.setCurrent( gameCanvas ); 73 73 app = new App( 74 74 ChessMIDlet.this, … … 77 77 server 78 78 ); 79 display.setCurrent( gameCanvas );80 79 } catch ( Exception e ) { 81 80 e.printStackTrace(); … … 188 187 189 188 protected void searchGame() { 189 display.setCurrent( searchForm ); 190 190 searchForm.refresh( false ); 191 display.setCurrent( searchForm );192 191 Util.log.println( "search server" ); 193 192 } -
chess/src/chess/ui/ChoosePlayerScreen.java
r18 r19 11 11 * created 31.01.2009 at 19:29:43 12 12 */ 13 public class ChoosePlayerForm extends Form implements CommandListener, ItemCommandListener { 14 private final ChessMIDlet midlet; 13 public class ChoosePlayerScreen extends BaseListScreen implements IChoiceListener { 14 public ChoosePlayerScreen( final ChessMIDlet midlet ) { 15 super( midlet, MSG.getMessage( "choose-player.title" ) ); 16 addItem( Player.WHITE.name() ); 17 addItem( Player.BLACK.name() ); 18 setChoiceListener( this ); 15 19 16 private final StringItem whiteItem = new StringItem(17 Player.WHITE.name(),18 "",19 StringItem.BUTTON20 );21 private final StringItem blackItem = new StringItem(22 Player.BLACK.name(),23 "",24 StringItem.BUTTON25 );26 27 public ChoosePlayerForm( final ChessMIDlet midlet ) {28 super( MSG.getMessage( "choose-player.title" ) );29 this.midlet = midlet;30 31 32 whiteItem.setDefaultCommand( midlet.okCommand );33 whiteItem.setItemCommandListener( this );34 whiteItem.setLayout( Item.LAYOUT_EXPAND | Item.LAYOUT_VEXPAND );35 36 blackItem.setDefaultCommand( midlet.okCommand );37 blackItem.setItemCommandListener( this );38 blackItem.setLayout( Item.LAYOUT_EXPAND | Item.LAYOUT_VEXPAND );39 40 append( whiteItem );41 append( blackItem );42 43 //addCommand( midlet.okCommand );44 20 addCommand( midlet.backCommand ); 45 46 setCommandListener( this );47 21 } 48 22 49 50 public void commandAction( final Command command, 51 final Displayable displayable ) { 52 /*if ( command == midlet.okCommand ) { 53 final int index = getSelectedIndex(); 54 if ( index >= 0 ) { 55 midlet.newGame( Player.byType( index ) ); 23 public void choosed( final Command c, 24 final int selectedIndex ) { 25 if ( c == midlet.okCommand ) { 26 switch ( selectedIndex ) { 27 case 0: 28 midlet.newGame( Player.WHITE ); 29 return; 30 case 1: 31 midlet.newGame( Player.BLACK ); 32 return; 56 33 } 57 } else */ 58 if ( command == midlet.backCommand ) { 34 } else if ( c == midlet.backCommand ) { 59 35 midlet.showStartScreen(); 60 36 } 61 37 } 62 63 public void commandAction( final Command command,64 final Item item ) {65 if ( command == midlet.okCommand ) {66 if ( item == whiteItem ) {67 midlet.newGame( Player.WHITE );68 } else if ( item == blackItem ) {69 midlet.newGame( Player.BLACK );70 }71 }72 }73 38 } -
chess/src/chess/ui/GameCanvas.java
r17 r19 37 37 public final int downKey; 38 38 39 protected Player player;39 //protected Player player; 40 40 protected IGame game; 41 41 … … 55 55 56 56 57 private static final int MARGIN = 2;57 private static final int MARGIN = 1; 58 58 //colors 59 59 private static final int BG_COLOR = 0xFFFFFF; … … 164 164 //paint desk 165 165 if ( game != null ) { 166 final int width = game.getWidth(); 167 final int height = game.getHeight(); 168 169 final int deskWidth = canvasWidth - MARGIN; 170 final int deskHeight = canvasHeight - MARGIN; 171 172 //ðåçåðâèðóåì ñòîëáöû/ñòðîêè äëÿ ïîîäïèñåé 166 final int nX = game.getWidth(); 167 final int nY = game.getHeight(); 168 169 final int availableWidth = canvasWidth - 2 * MARGIN; 170 final int availableHeight = canvasHeight - 2 * MARGIN; 171 172 final Font font = g.getFont(); 173 final int fontHeight = font.getHeight(); 174 175 //ðåçåðâèðóåì ìåñòî ïîä èíôîðìåðû: 2 * (âûñîòîé rows ñòðîê + îòñòóïû ) 176 //ñ÷èòàåì, ñêîëüêî íóæíî ñòðî÷åê, ÷òîáû íàïèñàòü "Áåëûå: 1235 ñåê <ñïèñîê âçÿòûõ ôèãóð>" 177 final int charWidth = font.charWidth( 'W' ); 178 final int chars = Math.max( 179 Player.WHITE.name().length(), 180 Player.BLACK.name().length() 181 ) + ": 99999 sec".length() + 16/*16 units per player*/; 182 183 final int rows = ( int )Math.floor( availableWidth / ( 1.0f * charWidth * chars ) ) + 1; 184 final int informerHeight = rows * fontHeight; 185 186 final int _availableHeight = availableHeight - 2 * ( informerHeight + MARGIN ); 187 188 //ðåçåðâèðóåì 2 ñòîëáöà è 2 ñòðîêè äëÿ ïîäïèñåé 173 189 cellSize = Math.min( 174 deskWidth / ( width+ 2 ),175 deskHeight / ( height+ 2 )190 availableWidth / ( nX + 2 ), 191 _availableHeight / ( nY + 2 ) 176 192 ); 177 193 … … 180 196 } 181 197 182 final int _deskWidth = cellSize * ( width+ 2 );183 final int _deskHeight = cellSize * ( height+ 2 );198 final int _deskWidth = cellSize * ( nX + 2 ); 199 final int _deskHeight = cellSize * ( nY + 2 ); 184 200 185 201 final int vDeskMargin = ( canvasHeight - _deskHeight ) / 2; … … 189 205 190 206 //draw labels 191 drawLabels( g, width, height);207 drawLabels( g, nX, nY ); 192 208 193 209 //draw desk and units 194 for ( int _y = 0; _y < height; _y++ ) {195 for ( int _x = 0; _x < width; _x++ ) {210 for ( int _y = 0; _y < nY; _y++ ) { 211 for ( int _x = 0; _x < nX; _x++ ) { 196 212 final int x = toScreenX( _x ); 197 213 final int y = toScreenY( _y ); … … 232 248 final int x = toScreenX( focusedCell.x ); 233 249 final int y = toScreenY( focusedCell.y ); 234 g.drawR ect( x + 1, y + 1, cellSize - 2, cellSize -2 );235 g.drawR ect( x, y, cellSize, cellSize);250 g.drawRoundRect( x + 1, y + 1, cellSize - 2, cellSize - 2, 2, 2 ); 251 g.drawRoundRect( x, y, cellSize, cellSize, 2, 2 ); 236 252 } 237 253 if ( selectedCell != null ) { … … 239 255 final int x = toScreenX( selectedCell.x ); 240 256 final int y = toScreenY( selectedCell.y ); 241 g.drawR ect( x + 1, y + 1, cellSize - 2, cellSize -2 );242 g.drawR ect( x, y, cellSize, cellSize);257 g.drawRoundRect( x + 1, y + 1, cellSize - 2, cellSize - 2, 2, 2 ); 258 g.drawRoundRect( x, y, cellSize, cellSize, 2, 2 ); 243 259 } 244 260 … … 257 273 ); 258 274 259 drawInformers( g, canvasHeight, _deskWidth, vDeskMargin, hDeskMargin ); 260 } 261 } 262 263 private void drawLabels( final Graphics g, final int width, final int height ) { 275 drawInformers( g, availableWidth, informerHeight ); 276 } 277 } 278 279 private void drawLabels( final Graphics g, 280 final int nX, 281 final int nY ) { 264 282 //draw labels 265 283 { 266 284 g.setColor( LABELS_COLOR ); 267 285 final int x1 = toScreenX( -1 ); 268 final int x2 = toScreenX( width);269 for ( int i = 0; i < height; i++ ) {286 final int x2 = toScreenX( nX ); 287 for ( int i = 0; i < nY; i++ ) { 270 288 final int y = toScreenY( i ); 271 289 … … 285 303 } 286 304 final int y1 = toScreenY( -1 ); 287 final int y2 = toScreenY( height);288 for ( int i = 0; i < width; i++ ) {305 final int y2 = toScreenY( nY ); 306 for ( int i = 0; i < nX; i++ ) { 289 307 final int x = toScreenX( i ); 290 308 … … 308 326 309 327 private void drawInformers( final Graphics g, 310 final int canvasHeight, 311 final int _deskWidth, 312 final int vDeskMargin, 313 final int hDeskMargin ) { 314 final int height = vDeskMargin - MARGIN - 2; 315 final int x = hDeskMargin; 316 final int width = _deskWidth; 328 final int width, 329 final int height ) { 330 final int x = MARGIN; 331 final int canvasHeight = getHeight(); 317 332 318 333 drawInformer( g, … … 323 338 324 339 drawInformer( g, 325 x, canvasHeight - vDeskMargin + 1,340 x, canvasHeight - height - 2 * MARGIN, 326 341 width, height, 327 342 Player.WHITE … … 336 351 final Player player ) { 337 352 g.drawRect( x, y, width, height ); 338 final String name = this.player== player ? player.name() + " (you)" : player.name();353 final String name = game.getOwner() == player ? player.name() + " (you)" : player.name(); 339 354 final int inset = 2; 340 355 g.drawString( name, … … 348 363 final long timing = game.getTiming( player ); 349 364 350 final String time = ": " + String.valueOf( timing / 1000 ) + " s";365 final String time = ": " + String.valueOf( timing / 1000 ) + " s"; 351 366 final int timeWidth = font.stringWidth( time ); 352 367 g.drawString( time, -
chess/src/chess/ui/JoinGameScreen.java
r17 r19 14 14 * Time: 15:30:43<br/> 15 15 */ 16 public class JoinGameScreen extends List implements CommandListener, IClientListener {17 private final ChessMIDlet midlet;16 public class JoinGameScreen extends BaseListScreen implements IChoiceListener, IClientListener { 17 //private final ChessMIDlet midlet; 18 18 19 19 private IRemoteNode[] nodes; … … 37 37 38 38 public JoinGameScreen( final ChessMIDlet midlet ) { 39 super( MSG.getMessage( "join-game.title" ), Choice.IMPLICIT);40 this.midlet = midlet;39 super( midlet, MSG.getMessage( "join-game.title" ) ); 40 //this.midlet = midlet; 41 41 42 42 scanningNetworkAlert.setTimeout( Alert.FOREVER ); … … 48 48 scanningNetworkAlert.setCommandListener( this ); 49 49 50 addCommand( midlet.okCommand );50 //addCommand( midlet.okCommand ); 51 51 addCommand( midlet.refreshCommand ); 52 52 addCommand( midlet.backCommand ); 53 53 54 setSelectCommand( midlet.okCommand );54 //setSelectCommand( midlet.okCommand ); 55 55 56 setCommandListener( this ); 56 //setCommandListener( this ); 57 setChoiceListener( this ); 57 58 } 58 59 … … 79 80 //todo check process.status() and display error message 80 81 81 deleteAll();82 removeAllItems(); 82 83 nodes = servers; 83 84 if ( nodes == null || nodes.length == 0 ) { … … 86 87 for ( int i = 0; i < nodes.length; i++ ) { 87 88 final IRemoteNode node = nodes[i]; 88 a ppend( node.getName(), null);89 addItem( node.getName() ); 89 90 } 90 91 midlet.display.setCurrent( this ); … … 101 102 } 102 103 } 103 } else if ( command == midlet.backCommand ) { 104 midlet.showStartScreen(); 105 } else if ( command == midlet.okCommand ) { 106 final int index = getSelectedIndex(); 107 if ( index >= 0 && index < nodes.length ) { 108 final IRemoteNode server = nodes[index]; 104 } 105 super.commandAction( command, displayable ); 106 } 107 108 public void choosed( final Command c, 109 final int selectedIndex ) { 110 if ( c == getDefaultCommand() ) { 111 if ( selectedIndex >= 0 && selectedIndex < nodes.length ) { 112 final IRemoteNode server = nodes[selectedIndex]; 109 113 midlet.connectToServer( server ); 110 114 } 111 } else if ( command == midlet.refreshCommand ) { 115 } else if ( c == midlet.backCommand ) { 116 midlet.showStartScreen(); 117 } else if ( c == midlet.refreshCommand ) { 112 118 refresh( true ); 113 119 } -
chess/src/chess/ui/StartMenuScreen.java
r15 r19 11 11 * Time: 15:20:49<br/> 12 12 */ 13 public class StartMenuScreen extends Form implements /*CommandListener, */ItemCommandListener { 14 private final ChessMIDlet midlet; 13 public class StartMenuScreen extends BaseListScreen implements IChoiceListener { 14 public StartMenuScreen( final ChessMIDlet midlet ) { 15 super( midlet, MSG.getMessage( "start-menu.title" ) ); 15 16 16 private final StringItem newGameItem = new StringItem( 17 MSG.getMessage( "start-menu.new-game" ), 18 "", 19 StringItem.BUTTON 20 ); 21 private final StringItem joinGameItem = new StringItem( 22 MSG.getMessage( "start-menu.join-game" ), 23 "", 24 StringItem.BUTTON 25 ); 26 private final StringItem helpItem = new StringItem( 27 MSG.getMessage( "start-menu.help" ), 28 "", 29 StringItem.BUTTON 30 ); 31 private final StringItem exitItem = new StringItem( 32 MSG.getMessage( "start-menu.exit" ), 33 "", 34 StringItem.BUTTON 35 ); 17 addItem( MSG.getMessage( "start-menu.new-game" ) ); 18 addItem( MSG.getMessage( "start-menu.join-game" ) ); 19 addItem( MSG.getMessage( "start-menu.help" ) ); 20 addItem( MSG.getMessage( "start-menu.exit" ) ); 36 21 37 public StartMenuScreen( final ChessMIDlet midlet ) { 38 super( MSG.getMessage( "start-menu.title" ) ); 39 this.midlet = midlet; 40 41 newGameItem.setDefaultCommand( midlet.okCommand ); 42 newGameItem.setItemCommandListener( this ); 43 newGameItem.setLayout( Item.LAYOUT_EXPAND | Item.LAYOUT_VEXPAND ); 44 45 joinGameItem.setDefaultCommand( midlet.okCommand ); 46 joinGameItem.setItemCommandListener( this ); 47 joinGameItem.setLayout( Item.LAYOUT_EXPAND | Item.LAYOUT_VEXPAND ); 48 49 helpItem.setDefaultCommand( midlet.okCommand ); 50 helpItem.setItemCommandListener( this ); 51 helpItem.setLayout( Item.LAYOUT_EXPAND | Item.LAYOUT_VEXPAND ); 52 53 exitItem.setDefaultCommand( midlet.okCommand ); 54 exitItem.setItemCommandListener( this ); 55 exitItem.setLayout( Item.LAYOUT_EXPAND | Item.LAYOUT_VEXPAND ); 56 57 append( newGameItem ); 58 append( joinGameItem ); 59 append( helpItem ); 60 append( exitItem ); 22 setChoiceListener( this ); 61 23 } 62 24 63 /*public void commandAction( final Command command, 64 final Displayable displayable ) { 65 if ( command == midlet.okCommand ) { 66 //final int index = getSelectedIndex(); 67 //switch ( index ) { 68 // case 0: 69 // midlet.choosePlayer(); 70 // return; 71 // case 1: 72 // midlet.searchGame(); 73 // return; 74 // case 2: 75 // midlet.showHelp(); 76 // return; 77 //} 78 } else { 79 midlet.exitGame(); 80 } 81 }*/ 82 83 public void commandAction( final Command command, 84 final Item item ) { 85 if ( command == midlet.okCommand ) { 86 if ( item == newGameItem ) { 87 midlet.choosePlayer(); 88 } else if ( item == joinGameItem ) { 89 midlet.searchGame(); 90 } else if ( item == helpItem ) { 91 midlet.showHelp(); 92 } else if ( item == exitItem ) { 93 midlet.exitGame(); 25 public void choosed( final Command c, 26 final int selectedIndex ) { 27 if ( c == midlet.okCommand ) { 28 switch ( selectedIndex ) { 29 case 0: 30 midlet.choosePlayer(); 31 return; 32 case 1: 33 midlet.searchGame(); 34 return; 35 case 2: 36 midlet.showHelp(); 37 return; 38 case 3: 39 midlet.exitGame(); 40 return; 94 41 } 95 42 }
Note: See TracChangeset
for help on using the changeset viewer.