-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogic.java
More file actions
200 lines (183 loc) · 6.84 KB
/
Logic.java
File metadata and controls
200 lines (183 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package cc.windroid.flightchess;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
public class Logic implements Runnable {
private View chessView;
private static int step = 1;
public boolean over = false;
private String[] infoText = {"绿色玩家", "红色玩家", "黄色玩家", "我方"};
Logic(View v){
this.chessView = v;
}
@Override
public void run() {
Log.i("debug", "logic run");
playing(Player.B);
playing(Player.G);
playing(Player.R);
playing(Player.Y);
refreshButton();
}
private void playing(int color){
if(over) return;
refreshInfo(infoText[color]+"行动中");
int dise = Player.dise(color);
refreshDise(dise);
refreshTable(String.valueOf(step++)+"\t["+infoText[color]+"] 掷出了点数: "+String.valueOf(dise));
refreshView(color, dise);
boolean anotherChance = false;
switch (color){
case Player.G:
anotherChance = checkAnotherChance(Position.c_g[0]);
if(jump(Position.c_g[0])){
Position.c_g[0] += 4;
refreshTable(String.valueOf(step++)+"\t["+infoText[color]+"] 进行了一次飞行");
}else if(fly(Position.c_g[0])){
Position.c_g[0] += 12;
refreshTable(String.valueOf(step++)+"\t["+infoText[color]+"] 进行了一次超级飞行");
}
if(Position.c_g[0] >= Position.END_INDEX){
refreshTable(String.valueOf(step++)+"\t["+infoText[color]+"] 胜利");
refreshInfo("["+infoText[color]+"] 胜利");
gameOver();
}
break;
case Player.R:
anotherChance = checkAnotherChance(Position.c_r[0]);
if(jump(Position.c_r[0])){
Position.c_r[0] += 4;
refreshTable(String.valueOf(step++)+"\t["+infoText[color]+"] 进行了一次飞行");
}else if(fly(Position.c_r[0])){
Position.c_r[0] += 12;
refreshTable(String.valueOf(step++)+"\t["+infoText[color]+"] 进行了一次超级飞行");
}
if(Position.c_r[0] >= Position.END_INDEX){
refreshTable(String.valueOf(step++)+"\t["+infoText[color]+"] 胜利");
refreshInfo("["+infoText[color]+"] 胜利");
gameOver();
}
break;
case Player.Y:
anotherChance = checkAnotherChance(Position.c_y[0]);
if(jump(Position.c_y[0])){
Position.c_y[0] += 4;
refreshTable(String.valueOf(step++)+"\t["+infoText[color]+"] 进行了一次飞行");
}else if(fly(Position.c_y[0])){
Position.c_y[0] += 12;
refreshTable(String.valueOf(step++)+"\t["+infoText[color]+"] 进行了一次超级飞行");
}
if(Position.c_y[0] >= Position.END_INDEX){
refreshTable(String.valueOf(step++)+"\t["+infoText[color]+"] 胜利");
refreshInfo("["+infoText[color]+"] 胜利");
gameOver();
}
break;
case Player.B:
anotherChance = checkAnotherChance(Position.c_b[0]);
if(jump(Position.c_b[0])){
Position.c_b[0] += 4;
refreshTable(String.valueOf(step++)+"\t["+infoText[color]+"] 进行了一次飞行");
}else if(fly(Position.c_b[0])){
Position.c_b[0] += 12;
refreshTable(String.valueOf(step++)+"\t["+infoText[color]+"] 进行了一次超级飞行");
}
if(Position.c_b[0] >= Position.END_INDEX){
refreshTable(String.valueOf(step++)+"\t["+infoText[color]+"] 胜利");
refreshInfo("["+infoText[color]+"] 胜利");
gameOver();
}
break;
}
chessView.postInvalidate();
sleep(800);
if(anotherChance){
refreshTable(String.valueOf(step++)+"\t["+infoText[color]+"] 得到一次额外机会");
playing(color);
}
if(dise == 6){
refreshTable(String.valueOf(step++)+"\t["+infoText[color]+"] 得到一次额外机会");
playing(color);
}
}
private void gameOver(){
over = true;
Message msg = new Message();
Bundle b = new Bundle();
b.putString("game", "over");
msg.setData(b);
MainActivity.handler.sendMessage(msg);
}
private boolean checkAnotherChance(int pos){
int[] chance = {2, 6, 8, 15, 19, 21, 28, 32, 34};
for (int aChance : chance) {
if (pos == aChance) return true;
}
return false;
}
private boolean jump(int pos) {
return pos < Position.MID_INDEX - 1 && (pos - 1) % 4 == 0;
}
private boolean fly(int pos){
return pos == 17;
}
private void refreshView(int color, int dise){
for(int i=0; i<dise; i++){
switch (color){
case Player.G:
Position.c_g[0] += 1;
break;
case Player.R:
Position.c_r[0] += 1;
break;
case Player.Y:
Position.c_y[0] += 1;
break;
case Player.B:
Position.c_b[0] += 1;
break;
}
chessView.postInvalidate();
sleep(200);
}
}
private void refreshTable(String content){
Message msg = new Message();
Bundle b = new Bundle();
b.putString("table", content);
msg.setData(b);
MainActivity.handler.sendMessage(msg);
}
private void refreshInfo(String info){
Message msg = new Message();
Bundle b = new Bundle();
b.putString("info", info);
msg.setData(b);
MainActivity.handler.sendMessage(msg);
sleep(500);
}
private void refreshDise(int dise){
Message msg = new Message();
Bundle b = new Bundle();
b.putInt("dise", dise);
msg.setData(b);
MainActivity.handler.sendMessage(msg);
sleep(500);
}
private void refreshButton(){
if(over) return;
Message msg = new Message();
Bundle b = new Bundle();
b.putString("logic", "over");
msg.setData(b);
MainActivity.handler.sendMessage(msg);
}
private void sleep(int millis){
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}