| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.cb.jset.client; |
| 21 |
|
|
| 22 |
|
import org.apache.log4j.Logger; |
| 23 |
|
import org.cb.jset.*; |
| 24 |
|
import org.cb.jset.server.RemoteSetGame; |
| 25 |
|
|
| 26 |
|
import java.util.List; |
| 27 |
|
import java.util.ArrayList; |
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
0 |
public class SmartAutoPlayer implements SetGameListener |
| 36 |
|
{ |
| 37 |
|
private Logger _logger; |
| 38 |
|
private SetFinderThread _thread; |
| 39 |
|
private MatchingSetFinder _finder; |
| 40 |
|
private String _name; |
| 41 |
|
private NetworkGame _game; |
| 42 |
0 |
private BoardImpl _board = new BoardImpl(); |
| 43 |
|
|
| 44 |
0 |
private final GameController _gameController = new GameController(); |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
public SmartAutoPlayer(final String playerName) |
| 50 |
0 |
{ |
| 51 |
0 |
_logger = Logger.getLogger("SmartAutoPlayer-" + playerName); |
| 52 |
0 |
_name = playerName; |
| 53 |
0 |
_finder = new MatchingSetFinder(); |
| 54 |
0 |
_thread = new SetFinderThread(); |
| 55 |
0 |
} |
| 56 |
|
|
| 57 |
|
public String name() |
| 58 |
|
{ |
| 59 |
0 |
return _name; |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
private void printCards(final String msg, class="keyword">final CardProperties[] cards) |
| 63 |
|
{ |
| 64 |
0 |
if (_logger.isInfoEnabled()) |
| 65 |
|
{ |
| 66 |
0 |
final StringBuffer buf = new StringBuffer(msg); |
| 67 |
0 |
buf.append(CardProperties.toString(cards)); |
| 68 |
0 |
_logger.info(buf.toString()); |
| 69 |
|
} |
| 70 |
0 |
} |
| 71 |
|
|
| 72 |
|
void startPlayingGame(final RemoteSetGame game) |
| 73 |
|
{ |
| 74 |
0 |
final NetworkGame networkGame = new NetworkGame("SmartAutoPlayerUser"); |
| 75 |
0 |
networkGame.setRemoteGame(game); |
| 76 |
0 |
_game = networkGame; |
| 77 |
0 |
_game.addBoardListener(_gameController); |
| 78 |
0 |
_game.addGameListener(_gameController); |
| 79 |
0 |
_game.start(); |
| 80 |
0 |
_thread.start(); |
| 81 |
0 |
} |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
class GameController implements SetGameBoardListener, SetGameListener |
| 90 |
|
{ |
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
public void cardsAdded(final CardProperties[] cards) |
| 95 |
|
{ |
| 96 |
|
_board.addCards(cards); |
| 97 |
|
} |
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
public void setRemoved(final CardSet set) |
| 103 |
|
{ |
| 104 |
|
try |
| 105 |
|
{ |
| 106 |
|
_board.matchSet(set); |
| 107 |
|
} |
| 108 |
|
catch (Exception e) |
| 109 |
|
{ |
| 110 |
|
throw new IllegalStateException("BoardException should not happen here: " + e.getMessage()); |
| 111 |
|
} |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
public void gameChange(final GameEvent event) |
| 118 |
|
{ |
| 119 |
|
|
| 120 |
|
} |
| 121 |
|
} |
| 122 |
|
|
| 123 |
|
|
| 124 |
|
public synchronized void gameChange(final GameEvent event) |
| 125 |
|
{ |
| 126 |
0 |
if (event.getType() == GameEvent.END) |
| 127 |
|
{ |
| 128 |
0 |
notifyAll(); |
| 129 |
|
} |
| 130 |
0 |
} |
| 131 |
|
|
| 132 |
|
class SetFinderThread extends Thread |
| 133 |
|
{ |
| 134 |
|
public void run() |
| 135 |
|
{ |
| 136 |
|
play(); |
| 137 |
|
} |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
private void play() |
| 141 |
|
{ |
| 142 |
0 |
while (! _game.wasDisconnected()) |
| 143 |
|
{ |
| 144 |
0 |
tryToFindSet(); |
| 145 |
|
} |
| 146 |
0 |
} |
| 147 |
|
|
| 148 |
|
void tryToFindSet() |
| 149 |
|
{ |
| 150 |
0 |
if (_logger.isInfoEnabled()) |
| 151 |
|
{ |
| 152 |
0 |
_logger.info("-- cards added - starting to find --"); |
| 153 |
|
} |
| 154 |
0 |
int sleepTime = (class="keyword">int) (Math.random() * 6000); |
| 155 |
0 |
if (_logger.isInfoEnabled()) |
| 156 |
|
{ |
| 157 |
0 |
_logger.info("Sleep for " + sleepTime + "ms."); |
| 158 |
|
} |
| 159 |
|
try |
| 160 |
|
{ |
| 161 |
0 |
synchronized (SmartAutoPlayer.this) |
| 162 |
|
{ |
| 163 |
0 |
wait(sleepTime); |
| 164 |
0 |
} |
| 165 |
|
} |
| 166 |
0 |
catch(InterruptedException e) |
| 167 |
|
{ |
| 168 |
0 |
} |
| 169 |
|
|
| 170 |
|
if (true) |
| 171 |
|
{ |
| 172 |
0 |
sleepTime = 60000; |
| 173 |
|
try |
| 174 |
|
{ |
| 175 |
0 |
Thread. sleep(sleepTime); |
| 176 |
|
} |
| 177 |
0 |
catch (InterruptedException e) |
| 178 |
|
{ |
| 179 |
0 |
} |
| 180 |
|
} |
| 181 |
|
|
| 182 |
0 |
if (_logger.isInfoEnabled()) |
| 183 |
|
{ |
| 184 |
0 |
_logger.info("-- Awaken! -- "); |
| 185 |
|
} |
| 186 |
0 |
if (_game.wasDisconnected()) |
| 187 |
|
{ |
| 188 |
0 |
_logger.info("-- Connection was closed in the meantime -- "); |
| 189 |
0 |
return; |
| 190 |
|
} |
| 191 |
0 |
if (_logger.isInfoEnabled()) |
| 192 |
|
{ |
| 193 |
0 |
_logger.info("-- Searching for sets -- "); |
| 194 |
|
} |
| 195 |
0 |
printCards("-- Cards on BoardImpl:", _board.getCards()); |
| 196 |
|
|
| 197 |
0 |
final List elements = new ArrayList(); |
| 198 |
0 |
for (int i = 0; i < _board.getCards().length; i++) |
| 199 |
|
{ |
| 200 |
0 |
final CardProperties cardProperties = _board.getCards()[i]; |
| 201 |
0 |
elements.add(new JSetCard(cardProperties)); |
| 202 |
|
} |
| 203 |
0 |
final CardSet[] sets = _finder.findSets(elements); |
| 204 |
0 |
printMatchingSets(sets); |
| 205 |
0 |
if (sets.length > 0) |
| 206 |
|
{ |
| 207 |
0 |
final int randIdx = (class="keyword">int) (sets.length * Math.random()); |
| 208 |
0 |
if (_logger.isInfoEnabled()) |
| 209 |
|
{ |
| 210 |
0 |
_logger.info("Trying to remove set Idx" + randIdx + ": " + sets[randIdx]); |
| 211 |
|
} |
| 212 |
|
try |
| 213 |
|
{ |
| 214 |
0 |
_game.removeSet(sets[randIdx]); |
| 215 |
|
} |
| 216 |
0 |
catch (MatchingException e) |
| 217 |
|
{ |
| 218 |
0 |
if (_logger.isInfoEnabled()) |
| 219 |
|
{ |
| 220 |
0 |
_logger.info("Matching not accepted!", e); |
| 221 |
|
} |
| 222 |
0 |
} |
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
|
| 231 |
0 |
} |
| 232 |
|
|
| 233 |
|
void printMatchingSets(final CardSet[] foundSets) |
| 234 |
|
{ |
| 235 |
0 |
if (_logger.isInfoEnabled()) |
| 236 |
|
{ |
| 237 |
0 |
final int maxPrclass="keyword">inted = 5; |
| 238 |
0 |
_logger.info("Found " + foundSets.length + " sets."); |
| 239 |
|
int i; |
| 240 |
0 |
for (i = 0; i < foundSets.length && i < maxPrinted; i++) |
| 241 |
|
{ |
| 242 |
0 |
_logger.info("CardSet[" + i + "]:" + foundSets[i]); |
| 243 |
|
} |
| 244 |
0 |
if (i < foundSets.length) |
| 245 |
|
{ |
| 246 |
0 |
_logger.info("Skipping " + (foundSets.length - i) + " set(s)..."); |
| 247 |
|
} |
| 248 |
|
} |
| 249 |
0 |
} |
| 250 |
|
} |