F:\java\knim-game\sources\ru\ifmo\knim\main\GamePlay.java
|
1 /**
2 * @(#)GamePlay.java
3 *
4 * Copyright Anthony Yakovlev <yakovlev@rain.ifmo.ru> and Michail Lukin <michail@users.msn.com>
5 */
6
7 package ru.ifmo.knim.main;
8
9 import ru.ifmo.knim.providers.GameEventProvider;
10
11 import com.evelopers.unimod.core.stateworks.Event;
12 import com.evelopers.unimod.runtime.ModelEngine;
13 import com.evelopers.unimod.runtime.context.Parameter;
14 import com.evelopers.unimod.runtime.context.StateMachineContextImpl;
15
16 /**
17 * Something that aggregates gameInfo and modelEngine.
18 * @author Anthony Yakovlev
19 */
20 public class GamePlay {
21 /**
22 * Singleton object
23 */
24 static GamePlay gamePlay = new GamePlay();
25
26 /**
27 * Game information
28 */
29 GameInfo gameInfo;
30
31 /**
32 * Signal model engine
33 */
34 ModelEngine modelEngine;
35
36 /**
37 * Default constructor
38 */
39 public GamePlay () {
40 int [] moves = {3, 4, 5};
41 gameInfo = new GameInfo(moves);
42 }
43
44 /**
45 * Initializes singleton object
46 * @param modelEngine
47 */
48 public void init(ModelEngine modelEngine) {
49 this.modelEngine = modelEngine;
50 }
51
52 /**
53 * Returns singleton object
54 * @return gamePlay object
55 */
56 public static GamePlay getGamePlay () {
57 return gamePlay;
58 }
59
60 /**
61 * Gets game information
62 * @return the game information
63 */
64 public GameInfo getGameInfo() {
65 return gameInfo;
66 }
67
68 /**
69 * Fires event to apropriate handler
70 * @param eventName - the event name
71 * @param parameter - the parameters
72 */
73 private void fireEvent(String eventName, Parameter parameter) {
74 if (modelEngine != null) {
75 if (parameter == null) {
76 modelEngine.getEventManager().handle(new Event(eventName), StateMachineContextImpl.create());
77 } else {
78 modelEngine.getEventManager().handle(new Event(eventName, new Parameter[]{parameter}), StateMachineContextImpl.create());
79 }
80 }
81 }
82
83 /**
84 * Restarts game
85 */
86 public void restart () {
87 gameInfo.restartGame();
88 }
89
90 /**
91 * Starts AI automata to process current situation
92 */
93 public void aiStartup () {
94 int xor = computeCurrentSumm();
95 fireEvent(GameEventProvider.E21, new Parameter("XORSumm", new Integer(xor)));
96 }
97
98 /**
99 * Computes current magic summ
100 * @return the magic summ
101 */
102 private int computeCurrentSumm() {
103 int xorValue = 0;
104 for(int i = 0; i < gameInfo.getLineAmount(); i++){
105 xorValue ^= gameInfo.getFishInLine(i);
106 }
107 return xorValue;
108 }
109
110 /**
111 * Computes and makes move using the safe strategy
112 */
113 public void sendSafeMove () {
114 // generate result
115 MoveResult result = new MoveResult();
116 for(int i = 0; i < gameInfo.getLineAmount(); i++){
117 if(gameInfo.getFishInLine(i) != 0){
118 result.take(i, 1);
119 break;
120 }
121 }
122
123 // send out
124 fireEvent(GameEventProvider.E22, new Parameter("AIMove", result));
125 }
126
127 /**
128 * Resends specifired move data
129 */
130 public void resendMove(MoveResult move) {
131 fireEvent(GameEventProvider.E20, new Parameter("AIMove", move));
132 }
133
134 /**
135 * Computes and makes move using the unsafe strategy
136 */
137 public void sendUnsafeMove () {
138 // generate result
139 if(gameInfo.getBadMoveLeft() > 0){
140 gameInfo.setBadMovesLeft(gameInfo.getBadMoveLeft() - 1);
141 sendSafeMove();
142 } else{
143 MoveResult result = new MoveResult();
144 int xorValue = computeCurrentSumm();
145 for(int i = 5; i >= 0; i--){
146 if(( xorValue & (1 << i) ) != 0){
147 for(int j = 0; j < gameInfo.getLineAmount(); j++){
148 int nStones = gameInfo.getFishInLine(j);
149 if(( nStones & (1 << i) ) != 0){//We found a line.
150 int amount = nStones ^ xorValue;
151 amount = nStones - amount;
152 result.take(j, amount);
153 break;
154 }
155 }
156 break;
157 }
158 }
159
160 // send out
161 fireEvent(GameEventProvider.E22, new Parameter("AIMove", result));
162 }
163
164 }
165 }
166