CatchCrazyCat源码

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
//Dot.java
package com.example.catchcrazycat;

public class Dot {//落点

int x,y;//坐标
int status;//当前棋子状态

public static final int STATUS_ON=1;//路障
public static final int STATUS_OFF=0;//空位
public static final int STATUS_IN=9;//猫

public Dot(int x, int y) {//有参构造初始化坐标和状态
this.x = x;
this.y = y;
status=STATUS_OFF;
}
//getter,setter
public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public void setXY(int x,int y) {
this.x=x;
this.y = y;
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//MainActivity.java
package com.example.catchcrazycat;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(new Playground(this));
// TextView tv_one =findViewById(R.id.tv_one);
// tv_one.setText("红色点表示神经猫,白色点表示空位,黄色点表示路障"+
// "游戏目的:阻止红点逃脱地图" +
// "游戏玩法:每回合玩家可以选择一个空位放置路障,神经猫则会移动一位");
}
}
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//Playground.java
package com.example.catchcrazycat;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.media.Image;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;

import java.util.HashMap;
import java.util.Vector;

public class Playground extends SurfaceView implements View.OnTouchListener {//游戏背景棋盘


private static int WIDTH=100;

private static final int ROW=15;//定义行列数
private static final int COL=15;
private static final int Blocks=25;//路障数量
boolean firstStep;

private Dot matrix[][];//创建二维矩阵棋盘
private Dot Cat;//猫


public Playground(Context context) {//背景创建初始化
super(context);
getHolder().addCallback(callback);
matrix=new Dot[ROW][COL];
for (int i=0;i<ROW;i++){
for (int j=0;j<COL;j++){
matrix[i][j]=new Dot(j,i);
}
}
setOnTouchListener(this);
initGame();
}

private Dot getDot(int x,int y){
return matrix[y][x];
}

private boolean isAtEdge(Dot d){//判断是否到边界
if(d.getX()*d.getY()==0||d.getX()+1==COL||d.getY()+1==ROW)
return true;
return false;
}

private Dot getNeighbor(Dot one,int dir){//移动到相邻点
switch(dir){
case 1:
return getDot(one.getX()-1,one.getY());
case 2:
if(one.getY()%2==0){
return getDot(one.getX()-1,one.getY()-1);
}
else{
return getDot(one.getX(),one.getY()-1);
}
case 3:
if(one.getY()%2==0){
return getDot(one.getX(),one.getY()-1);
}
else{
return getDot(one.getX()+1,one.getY()-1);
}
case 4:
return getDot(one.getX()+1,one.getY());
case 5:
if(one.getY()%2==0){
return getDot(one.getX(),one.getY()+1);
}
else{
return getDot(one.getX()+1,one.getY()+1);
}
case 6:
if(one.getY()%2==0){
return getDot(one.getX()-1,one.getY()+1);
}
else{
return getDot(one.getX(),one.getY()+1);
}
default:
break;
}


return null;
}

private int getDistance (Dot one ,int dir){
int distance =1;
if(isAtEdge(one))return distance;
// if(one.getStatus()!=Dot.STATUS_IN)return -1;
Dot ori =one,next;
while(true){//BFS搜索距离(后面有时间的话再用A*优化)
// distance++;
next=getNeighbor(ori,dir);
if(next.getStatus()==Dot.STATUS_ON){
return distance*-1;
}
if(isAtEdge(next)){
distance++;
return distance;
}
distance++;
ori=next;
}

}
//移动操作
private void MoveTo(Dot one){
one.setStatus(Dot.STATUS_IN);
getDot(Cat.getX(),Cat.getY()).setStatus(Dot.STATUS_OFF);
Cat.setXY(one.getX(),one.getY());
}
//逃脱算法
private void move(){
if(isAtEdge(Cat)){
lose();
return ;
}
Vector <Dot> avaliable=new Vector<>();
Vector <Dot> positive=new Vector<>();
HashMap<Dot,Integer> al =new HashMap<>();

for (int i = 1; i < 7; i++) {
Dot n=getNeighbor(Cat,i);
if(n.getStatus()==Dot.STATUS_OFF){
avaliable.add(n);
int Dis=getDistance(n,i);
al.put(n,Dis);

// System.out.println("x:"+n.getX()+" Y:"+n.getY()+" 方向:"+i+" 距离:"+Dis);
if(Dis>0)
positive.add(n);
}
}
if(avaliable.size()==0){
win();
}
else if(avaliable.size()==1){
MoveTo((avaliable.get(0)));
}
else{
Dot best=null;
if(positive.size()!=0){//存在直接逃脱的方向
int min=0x3f3f3f3f;
for (int i = 0; i < positive.size(); i++) {
int a=al.get(positive.get(i));
if(a<min){
min=a;
best=positive.get(i);
}
}
}
else{//所有方向都有路障
int max=0;
for (int i = 0; i < avaliable.size(); i++) {
int k=al.get(avaliable.get(i));
if(k<max){
max=k;
best=avaliable.get(i);
}
}
}
MoveTo(best);
}
return ;
}

private void lose(){
Toast toast=Toast.makeText(getContext(),"You Lose",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();


}


private void win(){
Toast toast=Toast.makeText(getContext(),"You Win!",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);

toast.show();
}


private void redraw(){
Canvas c=getHolder().lockCanvas();
c.drawColor(Color.LTGRAY);//绘制背景为灰色
Toast.makeText(getContext(),"红色点表示神经猫,白色点表示空位,黄色点表示路障"+
"游戏目的:阻止红点逃脱地图" +
"游戏玩法:每回合玩家可以选择一个空位放置路障,神经猫则会移动一位",Toast.LENGTH_SHORT).show();
Paint paint =new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
for (int i = 0; i < ROW; i++) {//绘制所有点
for (int j = 0; j <COL ; j++) {
int offset=0;
if(j%2!=0){
offset=WIDTH/2;
}
Dot one = getDot(i,j);
switch (one.getStatus()){
case Dot.STATUS_OFF:
paint.setColor(0xFFEEEEEE);
break;
case Dot.STATUS_ON:
paint.setColor(0xFFFFAA00);
break;
case Dot.STATUS_IN:
paint.setColor(0xFFFF0000);
break;
default:
break;

}
c.drawOval(new RectF(one.getX()*WIDTH+offset,one.getY()*WIDTH
,(one.getX()+1)*WIDTH+offset,(one.getY()+1)*WIDTH),paint);
}
}

getHolder().unlockCanvasAndPost(c);
}

SurfaceHolder.Callback callback =new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
redraw();
}

@Override
public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
WIDTH=i1/(COL+1);
redraw();
}

@Override
public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

}
};

private void initGame(){//游戏初始化
for (int i=0;i<ROW;i++){
for(int j=0;j<COL;j++){
matrix[i][j].setStatus(Dot.STATUS_OFF);
}
}
Cat =new Dot(7,8);
getDot(7,8).setStatus(Dot.STATUS_IN);
for (int i = 0; i < Blocks; ) {
int x=(int)((Math.random()*1000)%COL);
int y=(int)((Math.random()*1000)%ROW);
if(getDot(x,y).getStatus()==Dot.STATUS_OFF){
getDot(x,y).setStatus(Dot.STATUS_ON);
i++;
// System.out.println("Block"+i);
}
}
}


@Override
public boolean onTouch(View view, MotionEvent e) {//点击操作
if(e.getAction()==MotionEvent.ACTION_UP){//判断是否点击
// Toast.makeText(getContext(),e.getX()+":"+e.getY(),Toast.LENGTH_SHORT).show();
int x,y;
y=(int)(e.getY()/WIDTH);//根据点击奇偶行分别处理
if(y%2==0){
x=(int)(e.getX()/WIDTH);
}
else{
x=(int)((e.getX()-WIDTH/2)/WIDTH);
}

//判断点击是否再边界内
if(x+1>COL||y+1>ROW){//边界外则初始化
initGame();
}
else if(getDot(x,y).getStatus()==Dot.STATUS_OFF){//判断是否为空点
getDot(x,y).setStatus(Dot.STATUS_ON);
move();
}
redraw();
}
return true;
}
}