質問編集履歴
2
ソースコード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -25,3 +25,657 @@
|
|
25
25
|
|
26
26
|
|
27
27
|
![AVDManger](63220a9607fda705c22d89fc52faa3c9.png)
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
### ソース
|
32
|
+
|
33
|
+
[Android プログラミングバイブル](http://www.socym.co.jp/book/1087) のサンプルコードです。
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
```java:PuzzleGame.java
|
38
|
+
|
39
|
+
package com.example.yuji3.puzzlegame;
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
import android.app.Activity;
|
44
|
+
|
45
|
+
import android.os.Bundle;
|
46
|
+
|
47
|
+
import android.view.Window;
|
48
|
+
|
49
|
+
import android.view.WindowManager;
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
public class PuzzleGame extends Activity {
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
@Override
|
58
|
+
|
59
|
+
protected void onCreate(Bundle savedInstanceState) {
|
60
|
+
|
61
|
+
super.onCreate(savedInstanceState);
|
62
|
+
|
63
|
+
Util.setActivity(this);
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
|
68
|
+
|
69
|
+
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
setContentView(new PuzzleView(this));
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
```
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
```java:PuzzleView.java
|
90
|
+
|
91
|
+
package com.example.yuji3.puzzlegame;
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
import android.app.Activity;
|
96
|
+
|
97
|
+
import android.graphics.Bitmap;
|
98
|
+
|
99
|
+
import android.graphics.Canvas;
|
100
|
+
|
101
|
+
import android.graphics.Color;
|
102
|
+
|
103
|
+
import android.graphics.Point;
|
104
|
+
|
105
|
+
import android.graphics.Rect;
|
106
|
+
|
107
|
+
import android.view.MotionEvent;
|
108
|
+
|
109
|
+
import android.view.View;
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
import lombok.EqualsAndHashCode;
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
/**
|
118
|
+
|
119
|
+
* Created by yuji3 on 2017/05/03.
|
120
|
+
|
121
|
+
*/
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
public class PuzzleView extends View {
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
public final static int SIZE = 4;
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
private final int EMPTY_NO = SIZE*SIZE-1;
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
public int W;
|
144
|
+
|
145
|
+
public int H;
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
private Scene scene = Scene.TITLE;
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
private int[][] data = new int[SIZE][SIZE];
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
private int shufle;
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
private Graphics g;
|
166
|
+
|
167
|
+
private Rect gSrc;
|
168
|
+
|
169
|
+
private Rect gDst;
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
private Bitmap imgBg;
|
174
|
+
|
175
|
+
private Bitmap imgFrame;
|
176
|
+
|
177
|
+
private Bitmap imgPic;
|
178
|
+
|
179
|
+
private Bitmap imgTitle;
|
180
|
+
|
181
|
+
private Bitmap imgTap;
|
182
|
+
|
183
|
+
private Bitmap imgClear;
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
public PuzzleView(Activity activity) {
|
190
|
+
|
191
|
+
super(activity);
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
imgBg = Util.res2bmp(R.drawable.bg);
|
196
|
+
|
197
|
+
imgFrame = Util.res2bmp(R.drawable.frame);
|
198
|
+
|
199
|
+
imgPic = Util.res2bmp(R.drawable.pic);
|
200
|
+
|
201
|
+
imgTitle = Util.res2bmp(R.drawable.title);
|
202
|
+
|
203
|
+
imgTap = Util.res2bmp(R.drawable.tap);
|
204
|
+
|
205
|
+
imgClear = Util.res2bmp(R.drawable.clear);
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
W = imgBg.getWidth();
|
210
|
+
|
211
|
+
H = imgBg.getHeight();
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
Point displaySize = Util.getDisplaySize();
|
216
|
+
|
217
|
+
int srcH = W * displaySize.y / displaySize.x;
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
int top = (H - srcH)/2;
|
222
|
+
|
223
|
+
gSrc = new Rect(0, top, W, top + srcH);
|
224
|
+
|
225
|
+
gDst = new Rect(0, 0, displaySize.x, displaySize.y);
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
g = new Graphics(W, H);
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
initScene(Scene.TITLE);
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
private void initScene(Scene scene) {
|
244
|
+
|
245
|
+
this.scene = scene;
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
switch (scene) {
|
250
|
+
|
251
|
+
case TITLE:
|
252
|
+
|
253
|
+
for (int i = 0; i < SIZE; i++) {
|
254
|
+
|
255
|
+
for (int j = 0; j < SIZE; j++) {
|
256
|
+
|
257
|
+
data[i][j] = i * SIZE + j;
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
break;
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
case PLAY:
|
268
|
+
|
269
|
+
shufle = 40;
|
270
|
+
|
271
|
+
while (shufle > 0) {
|
272
|
+
|
273
|
+
if (movePiece(new Cell(Util.rand(SIZE), Util.rand(SIZE)))) {
|
274
|
+
|
275
|
+
shufle--;
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
break;
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
default:
|
286
|
+
|
287
|
+
break;
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
invalidate();
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
@Override
|
306
|
+
|
307
|
+
protected void onDraw(Canvas canvas) {
|
308
|
+
|
309
|
+
int PIECE_PIXEL = imgPic.getWidth()/SIZE;
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
g.drawBitmap(imgBg, 0, 0);
|
314
|
+
|
315
|
+
g.drawBitmap(imgFrame, (W-imgFrame.getWidth())/2, (H-imgFrame.getHeight())/2);
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
int px = (W-imgPic.getWidth())/2;
|
320
|
+
|
321
|
+
int py = (H-imgPic.getHeight())/2;
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
// for (int )
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
for(int i=0; i< SIZE; i++) {
|
332
|
+
|
333
|
+
for (int j=0; j<SIZE; j++) {
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
if (scene != Scene.PLAY || data[i][j] != EMPTY_NO ) {
|
338
|
+
|
339
|
+
int dx = j;
|
340
|
+
|
341
|
+
int dy = i;
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
int sx = data[i][j] % SIZE;
|
346
|
+
|
347
|
+
int sy = data[i][j] / SIZE;
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
Rect src = new Rect(PIECE_PIXEL * sx, PIECE_PIXEL * sy, PIECE_PIXEL * sx + PIECE_PIXEL, PIECE_PIXEL * sy + PIECE_PIXEL);
|
352
|
+
|
353
|
+
int dstLeft = px + PIECE_PIXEL * dx;
|
354
|
+
|
355
|
+
int dstTop = py + PIECE_PIXEL * dy;
|
356
|
+
|
357
|
+
Rect dst = new Rect(dstLeft, dstTop, dstLeft + PIECE_PIXEL, dstTop + PIECE_PIXEL);
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
g.drawBitmap(imgPic, src, dst);
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
}
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
switch(scene) {
|
376
|
+
|
377
|
+
case TITLE:
|
378
|
+
|
379
|
+
g.drawBitmap(imgTitle, (W-imgTitle.getWidth())/2, 120);
|
380
|
+
|
381
|
+
g.drawBitmap(imgTap, (W-imgTap.getWidth())/2, 1100);
|
382
|
+
|
383
|
+
break;
|
384
|
+
|
385
|
+
case CLEAR:
|
386
|
+
|
387
|
+
g.drawBitmap(imgClear,(W-imgClear.getWidth())/2, 120);
|
388
|
+
|
389
|
+
break;
|
390
|
+
|
391
|
+
default:
|
392
|
+
|
393
|
+
break;
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
canvas.drawColor(Color.BLACK);
|
400
|
+
|
401
|
+
canvas.drawBitmap(g.getBitmap(), gSrc, gDst, null);
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
@Override
|
412
|
+
|
413
|
+
public boolean onTouchEvent(MotionEvent event) {
|
414
|
+
|
415
|
+
int PIECE_PIXEL = imgPic.getWidth()/SIZE;
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
int touchX = (int) (event.getX() * gSrc.width() / gDst.width());
|
420
|
+
|
421
|
+
int touchY = (int) (event.getY() * gSrc.height() / gDst.height());
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
if (event.getAction() == MotionEvent.ACTION_DOWN) {
|
426
|
+
|
427
|
+
switch(scene) {
|
428
|
+
|
429
|
+
case TITLE:
|
430
|
+
|
431
|
+
initScene(Scene.PLAY);
|
432
|
+
|
433
|
+
break;
|
434
|
+
|
435
|
+
|
436
|
+
|
437
|
+
case PLAY:
|
438
|
+
|
439
|
+
int px = (W-imgPic.getWidth())/2;
|
440
|
+
|
441
|
+
int py = (H - imgPic.getHeight())/2;
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
if (px < touchX && touchX < px + imgPic.getWidth() &&
|
446
|
+
|
447
|
+
py < touchY && touchY < py + imgPic.getHeight()) {
|
448
|
+
|
449
|
+
int tx = (touchX - px) / PIECE_PIXEL;
|
450
|
+
|
451
|
+
int ty = (touchY - py) / PIECE_PIXEL;
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
movePiece(new Cell(ty, tx));
|
456
|
+
|
457
|
+
|
458
|
+
|
459
|
+
clearCheck();
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
}
|
464
|
+
|
465
|
+
break;
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
case CLEAR:
|
470
|
+
|
471
|
+
initScene(Scene.TITLE);
|
472
|
+
|
473
|
+
break;
|
474
|
+
|
475
|
+
}
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
return true;
|
480
|
+
|
481
|
+
}
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
return super.onTouchEvent(event);
|
486
|
+
|
487
|
+
}
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
private boolean movePiece(Cell target) {
|
492
|
+
|
493
|
+
Cell empty = getEmptyCell();
|
494
|
+
|
495
|
+
if (target.equals(empty)) {
|
496
|
+
|
497
|
+
return false;
|
498
|
+
|
499
|
+
}
|
500
|
+
|
501
|
+
|
502
|
+
|
503
|
+
if ( (target.row == empty.row && Math.abs(target.col-empty.col) <= 1) ||
|
504
|
+
|
505
|
+
(target.col == empty.col && Math.abs(target.row - empty.row) <=1)) { //隣り合わせの場合
|
506
|
+
|
507
|
+
data[empty.row][empty.col] = data[target.row][target.col];
|
508
|
+
|
509
|
+
data[target.row][target.col] = EMPTY_NO;
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
|
514
|
+
|
515
|
+
return true;
|
516
|
+
|
517
|
+
} else {
|
518
|
+
|
519
|
+
return false;
|
520
|
+
|
521
|
+
}
|
522
|
+
|
523
|
+
}
|
524
|
+
|
525
|
+
|
526
|
+
|
527
|
+
private enum Scene {
|
528
|
+
|
529
|
+
TITLE,PLAY,CLEAR
|
530
|
+
|
531
|
+
}
|
532
|
+
|
533
|
+
|
534
|
+
|
535
|
+
private Cell getEmptyCell() {
|
536
|
+
|
537
|
+
for(int i=0; i< SIZE; i++) {
|
538
|
+
|
539
|
+
for (int j=0; j<SIZE; j++) {
|
540
|
+
|
541
|
+
if (data[i][j] == EMPTY_NO) {
|
542
|
+
|
543
|
+
return new Cell(i, j);
|
544
|
+
|
545
|
+
}
|
546
|
+
|
547
|
+
}
|
548
|
+
|
549
|
+
}
|
550
|
+
|
551
|
+
return null;
|
552
|
+
|
553
|
+
}
|
554
|
+
|
555
|
+
|
556
|
+
|
557
|
+
@EqualsAndHashCode
|
558
|
+
|
559
|
+
class Cell {
|
560
|
+
|
561
|
+
Cell(int row, int col) {
|
562
|
+
|
563
|
+
this.row = row;
|
564
|
+
|
565
|
+
this.col = col;
|
566
|
+
|
567
|
+
}
|
568
|
+
|
569
|
+
int row = 0;
|
570
|
+
|
571
|
+
int col = 0;
|
572
|
+
|
573
|
+
}
|
574
|
+
|
575
|
+
|
576
|
+
|
577
|
+
|
578
|
+
|
579
|
+
private boolean clearCheck() {
|
580
|
+
|
581
|
+
if (shufle > 0) {
|
582
|
+
|
583
|
+
return true;
|
584
|
+
|
585
|
+
}
|
586
|
+
|
587
|
+
|
588
|
+
|
589
|
+
int count = 0;
|
590
|
+
|
591
|
+
for(int i=0; i< SIZE; i++) {
|
592
|
+
|
593
|
+
for (int j=0; j<SIZE; j++) {
|
594
|
+
|
595
|
+
if (data[i][j] == i*SIZE + j) {
|
596
|
+
|
597
|
+
count++;
|
598
|
+
|
599
|
+
}
|
600
|
+
|
601
|
+
}
|
602
|
+
|
603
|
+
}
|
604
|
+
|
605
|
+
|
606
|
+
|
607
|
+
if (count == SIZE*SIZE ) {
|
608
|
+
|
609
|
+
scene = Scene.CLEAR;
|
610
|
+
|
611
|
+
invalidate();
|
612
|
+
|
613
|
+
return true;
|
614
|
+
|
615
|
+
} else {
|
616
|
+
|
617
|
+
invalidate();
|
618
|
+
|
619
|
+
return false;
|
620
|
+
|
621
|
+
}
|
622
|
+
|
623
|
+
|
624
|
+
|
625
|
+
}
|
626
|
+
|
627
|
+
}
|
628
|
+
|
629
|
+
```
|
630
|
+
|
631
|
+
|
632
|
+
|
633
|
+
```xml:AndroidManifest.xml
|
634
|
+
|
635
|
+
<?xml version="1.0" encoding="utf-8"?>
|
636
|
+
|
637
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
638
|
+
|
639
|
+
package="com.example.yuji3.puzzlegame">
|
640
|
+
|
641
|
+
|
642
|
+
|
643
|
+
<application
|
644
|
+
|
645
|
+
android:allowBackup="true"
|
646
|
+
|
647
|
+
android:icon="@mipmap/ic_launcher"
|
648
|
+
|
649
|
+
android:label="@string/app_name"
|
650
|
+
|
651
|
+
android:roundIcon="@mipmap/ic_launcher_round"
|
652
|
+
|
653
|
+
android:supportsRtl="true"
|
654
|
+
|
655
|
+
android:theme="@style/AppTheme">
|
656
|
+
|
657
|
+
<activity android:name=".PuzzleGame"
|
658
|
+
|
659
|
+
android:label="@string/app_name"
|
660
|
+
|
661
|
+
android:screenOrientation="portrait">
|
662
|
+
|
663
|
+
<intent-filter>
|
664
|
+
|
665
|
+
<action android:name="android.intent.action.MAIN" />
|
666
|
+
|
667
|
+
|
668
|
+
|
669
|
+
<category android:name="android.intent.category.LAUNCHER" />
|
670
|
+
|
671
|
+
</intent-filter>
|
672
|
+
|
673
|
+
</activity>
|
674
|
+
|
675
|
+
</application>
|
676
|
+
|
677
|
+
|
678
|
+
|
679
|
+
</manifest>
|
680
|
+
|
681
|
+
```
|
1
開発環境を追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,8 +8,20 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
-
![
|
11
|
+
![Emulator](70a5f6457e6ce6c2b76e3c55034fc595.png)
|
12
12
|
|
13
13
|
|
14
14
|
|
15
15
|
どこを修正すれば、正しく表示されるでしょうか?
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
### 開発環境
|
20
|
+
|
21
|
+
* Android Studio2.3.1
|
22
|
+
|
23
|
+
* AVD Manager Galaxy Nexus API 25
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
![AVDManger](63220a9607fda705c22d89fc52faa3c9.png)
|