fork(2) download
  1. enum GS {//game state
  2. Playing, Clear,
  3. }
  4.  
  5. Board board;
  6. GS gs;
  7.  
  8. void setup() {
  9. size(400, 400);
  10. colorMode(HSB, 360, 100, 100);
  11.  
  12. board = new Board();
  13. gs = GS.Playing;
  14. }
  15. void func_cleared() {
  16. int ts = 16;
  17. fill(0, 0, 100);
  18. textSize(ts);
  19. String[] mes = {//message
  20. //"complete",
  21. "click to restart",
  22. };
  23. final float m = 5;//margin
  24. final float by = height - mes.length*ts - m;//base y
  25. for (int i=0; i<mes.length; i++) {
  26. text(mes[i], m, by+ts*(i+1));
  27. }
  28. }
  29. void func_draw() {
  30. background(0, 0, 100);
  31.  
  32. switch(gs) {
  33. case Playing:
  34. board.draw();
  35. break;
  36.  
  37. case Clear:
  38. board.draw();
  39. func_cleared();
  40. break;
  41. }
  42. }
  43. void func_update() {
  44. switch(gs) {
  45. case Playing:
  46. board.update();
  47. if (board.isClear()) {
  48. gs = GS.Clear;
  49. }
  50. break;
  51.  
  52. case Clear:
  53. break;
  54. }
  55. }
  56. void draw() {
  57. func_draw();
  58. func_update();
  59. }
  60. void mousePressed() {
  61. switch(gs) {
  62. case Playing:
  63. board.mousePressed();
  64. break;
  65.  
  66. case Clear:
  67. board.reset();
  68. gs = GS.Playing;
  69. break;
  70. }
  71. }
  72.  
  73. import java.util.ArrayDeque;
  74.  
  75. class Board {
  76. final int div = 4;
  77. ArrayList<Cell> list;
  78. Cell[][] self;
  79. State state;
  80.  
  81. ArrayDeque<Cell> selected;
  82. final int size = 2;
  83.  
  84. boolean isShuffle;
  85. int count = 0;
  86. final int countMax = 4;//3;//16;//1;//5;//20;//何度シャッフルするか。
  87. boolean isClear;
  88.  
  89. Board() {
  90. list = new ArrayList<Cell>();
  91. self = new Cell[div][div];
  92. selected = new ArrayDeque<Cell>();
  93.  
  94. final int w = width/div;
  95. final int h = height/div;
  96.  
  97. PImage temp = loadImage("higanbana2.jpg");
  98. temp.resize(width, 0);
  99. int id = 0;
  100. for (int j=0; j<div; j++) {
  101. int y = j*h;
  102. for (int i=0; i<div; i++) {
  103. int x = i*w;
  104. Cell cell = new Cell(temp.get(x, y, w, h), x, y, w, h, id, i, j);
  105. list.add(cell);
  106. self[j][i] = cell;
  107. id++;
  108. }
  109. }
  110.  
  111. reset();
  112. }
  113. void reset() {
  114. state = State.Stop;
  115. isShuffle = true;
  116. isClear = false;
  117. count = 0;
  118.  
  119. for (Cell cell : list) {
  120. cell.reset();
  121. }
  122. }
  123. boolean isClear() {
  124. return isClear;
  125. }
  126. boolean isSolved() {
  127. if (isShuffle())return false;
  128.  
  129. int count = 0;
  130. for (int j=0; j<div; j++) {
  131. for (int i=0; i<div; i++) {
  132. if (self[j][i].id() != count)return false;
  133. count++;
  134. }
  135. }
  136. return true;
  137. }
  138.  
  139.  
  140. ////shuffling
  141. boolean isShuffle() {
  142. return isShuffle;
  143. }
  144. void shuffling() {
  145. while (true) {
  146. if (select(int(random(width)), int(random(height)))) {
  147. count++;
  148. break;
  149. }
  150. }
  151. }
  152.  
  153.  
  154. ////draw
  155. void drawInfo() {
  156. final int ts = 20;
  157. fill(0, 0, 100);
  158. textSize(ts);
  159. text(selected.size(), 5, 5+ts);
  160. }
  161. void draw() {
  162. for (Cell cell : list) {
  163. cell.draw();
  164. }
  165.  
  166. //drawInfo();//check, ok
  167. }
  168.  
  169.  
  170. ////update
  171. void moving() {
  172. for (Cell cell : selected) {
  173. cell.moving();
  174. }
  175. }
  176. boolean isStop() {
  177. for (Cell cell : selected) {
  178. if (cell.isStop()) {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. void finish() {
  185. for (Cell cell : selected) {
  186. cell.finish();
  187. self[cell.j()][cell.i()] = cell;
  188. }
  189. selected.clear();
  190. }
  191. void stops() {
  192. state = State.Stop;
  193. }
  194. void move() {
  195. state = State.Moving;
  196. }
  197. void update() {
  198. switch(state) {
  199. case Stop:
  200. if (isShuffle()) {
  201. shuffling();
  202. } else {
  203. if (isSolved()) {
  204. isClear = true;
  205. for (Cell cell : list) {
  206. cell.cleared();
  207. }
  208. }
  209. }
  210. break;
  211.  
  212. case Moving:
  213. if (isShuffle()) {
  214. moving();
  215. if (isStop()) {
  216. finish();
  217. stops();
  218. if (!(count < countMax)) {
  219. isShuffle = false;
  220. }
  221. break;
  222. }
  223. } else {
  224. moving();
  225. if (isStop()) {
  226. finish();
  227. stops();
  228. break;
  229. }
  230. }
  231. }
  232. }
  233.  
  234.  
  235. ////mousePressed
  236. boolean isAdjacent() {
  237. Cell a = selected.pollLast();
  238. Cell b = selected.pollLast();
  239.  
  240. selected.addFirst(a);
  241. selected.addFirst(b);
  242.  
  243. final int dx = abs(a.i()-b.i());
  244. final int dy = abs(a.j()-b.j());
  245.  
  246. if (dx==1 && dy==0)return true;
  247. if (dx==0 && dy==1)return true;
  248. return false;
  249. }
  250. void prepare() {
  251. Cell a = selected.pollLast();
  252. Cell b = selected.pollLast();
  253. selected.addFirst(a);
  254. selected.addFirst(b);
  255.  
  256. a.ni(b.i());
  257. a.nj(b.j());
  258.  
  259. b.ni(a.i());
  260. b.nj(a.j());
  261.  
  262. a.end(b.pos());
  263. b.end(a.pos());
  264.  
  265. final int dx = a.i() - b.i();
  266. final int dy = a.j() - b.j();
  267. a.dir(new Coord(-dx, -dy));
  268. b.dir(new Coord(dx, dy));
  269. a.velocity();
  270. b.velocity();
  271.  
  272. a.move();
  273. b.move();
  274. }
  275. boolean select(int mx, int my) {
  276. for (Cell cell : list) {
  277. if (cell.isPressed(mx, my)) {
  278. if (!selected.contains(cell)) {
  279. cell.select();
  280. selected.addFirst(cell);
  281.  
  282. if (selected.size() > size) {
  283. selected.pollLast().unselect();
  284. }
  285. if (selected.size() == size) {
  286. if (isAdjacent()) {
  287. move();
  288. prepare();
  289. return true;
  290. }
  291. }
  292. }
  293. break;
  294. }
  295. }
  296. return false;
  297. }
  298. void mousePressed() {
  299. switch(state) {
  300. case Stop:
  301. if (isShuffle()) {
  302. //
  303. } else {
  304. select(mouseX, mouseY);
  305. }
  306. break;
  307.  
  308. case Moving:
  309. break;
  310. }
  311. }
  312. }
  313.  
  314. class Cell {
  315. PImage img;
  316. PVector pos;//position
  317. PVector vel;//velocity
  318. final float v = 8.500;//velocity
  319. final float e = 0.930;
  320. final float eps = 1.0;//epsilon
  321. final float w;//width
  322. final float h;//height
  323. State state;
  324. final int id;
  325. int i;
  326. int j;
  327. boolean isSelected;
  328. PVector end;
  329. int ni;//next i
  330. int nj;//next j
  331. Coord dir;//direction
  332. boolean isClear;
  333.  
  334. Cell(PImage img, float x, float y, float w, float h, int id, int i, int j) {
  335. this.img = img;
  336. this.pos = new PVector(x, y);
  337. this.vel = new PVector(0, 0);
  338. this.w = w;
  339. this.h = h;
  340. this.state = State.Stop;
  341. this.id = id;
  342. this.isSelected = false;
  343. this.i = i;
  344. this.j = j;
  345. this.end = new PVector(0, 0);
  346. reset();
  347. }
  348. void drawFrame() {
  349. switch(state) {
  350. case Stop:
  351. if (isSelected()) {
  352. strokeWeight(3);
  353. stroke(0, 100, 100);
  354. } else {
  355. strokeWeight(1);
  356. stroke(0, 0, 100);
  357. }
  358. break;
  359.  
  360. case Moving:
  361. strokeWeight(3);
  362. stroke(120, 100, 100);
  363. break;
  364. }
  365.  
  366.  
  367. noFill();
  368. rect(x(), y(), w(), h());
  369. }
  370. void drawId(){
  371. final int ts = 20;
  372. fill(0, 0, 100);
  373. textSize(ts);
  374. text(id, x()+5, y()+ts+5);
  375. }
  376. void draw() {
  377. image(img, x(), y());
  378. if(!isClear){
  379. drawFrame();
  380. }
  381. //drawId();//check, ok
  382. }
  383. void update() {
  384. switch(state) {
  385. case Stop:
  386. //
  387. break;
  388.  
  389. case Moving:
  390. moving();
  391. break;
  392. }
  393. }
  394. void reset(){
  395. isClear = false;
  396. }
  397. void cleared(){
  398. isClear = true;
  399. }
  400. int id(){
  401. return id;
  402. }
  403. float x() {
  404. return pos.x;
  405. }
  406. float y() {
  407. return pos.y;
  408. }
  409. float w() {
  410. return w;
  411. }
  412. float h() {
  413. return h;
  414. }
  415. boolean isSelected() {
  416. return isSelected;
  417. }
  418. boolean isPressed(int mx, int my) {
  419. return
  420. x() <= mx && mx <= x()+w()
  421. &&
  422. y() <= my && my <= y()+h();
  423. }
  424. void select() {
  425. isSelected = true;
  426. }
  427. void unselect() {
  428. isSelected = false;
  429. }
  430. int i() {
  431. return i;
  432. }
  433. int j() {
  434. return j;
  435. }
  436. void i(int i) {
  437. this.i = i;
  438. }
  439. void j(int j) {
  440. this.j = j;
  441. }
  442. void ni(int ni) {
  443. this.ni = ni;
  444. }
  445. void nj(int nj) {
  446. this.nj = nj;
  447. }
  448. int ni() {
  449. return ni;
  450. }
  451. int nj() {
  452. return nj;
  453. }
  454. void end(PVector end) {
  455. this.end.x = end.x;
  456. this.end.y = end.y;
  457. }
  458. PVector end() {
  459. return end;
  460. }
  461. PVector pos(){
  462. return pos;
  463. }
  464. void move() {
  465. state = State.Moving;
  466. }
  467. void stops() {
  468. state = State.Stop;
  469. }
  470. void dir(Coord dir) {
  471. this.dir = dir;
  472. }
  473. void moving() {
  474. pos.add(vel);
  475. vel.mult(e);
  476. }
  477. boolean isStop() {
  478. return abs(pos.x-end.x) < eps && abs(pos.y-end.y) < eps;
  479. }
  480. void adjust() {
  481. pos.x = end.x;
  482. pos.y = end.y;
  483. }
  484. void velocity() {
  485. vel = new PVector(dir.x(), dir.y());
  486. vel.mult(v);
  487. }
  488. void finish() {
  489. adjust();
  490. stops();
  491. i(ni());
  492. j(nj());
  493. unselect();
  494. }
  495. }
  496.  
  497. class Coord {
  498. final int x, y;
  499. Coord(int x, int y) {
  500. this.x = x;
  501. this.y = y;
  502. }
  503. int x() {
  504. return x;
  505. }
  506. int y() {
  507. return y;
  508. }
  509. }
  510.  
  511. enum State {
  512. Stop, Moving,
  513. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:5: error: class, interface, or enum expected
Board board;
^
Main.java:6: error: class, interface, or enum expected
GS gs;
^
Main.java:8: error: class, interface, or enum expected
void setup() {
^
Main.java:10: error: class, interface, or enum expected
  colorMode(HSB, 360, 100, 100);
  ^
Main.java:12: error: class, interface, or enum expected
  board = new Board();
  ^
Main.java:13: error: class, interface, or enum expected
  gs = GS.Playing;
  ^
Main.java:14: error: class, interface, or enum expected
}
^
Main.java:17: error: class, interface, or enum expected
  fill(0, 0, 100);
  ^
Main.java:18: error: class, interface, or enum expected
  textSize(ts);
  ^
Main.java:19: error: class, interface, or enum expected
  String[] mes = {//message
  ^
Main.java:23: error: class, interface, or enum expected
  final float m = 5;//margin
        ^
Main.java:24: error: class, interface, or enum expected
  final float by = height - mes.length*ts - m;//base y
        ^
Main.java:25: error: class, interface, or enum expected
  for (int i=0; i<mes.length; i++) {
  ^
Main.java:25: error: class, interface, or enum expected
  for (int i=0; i<mes.length; i++) {
                ^
Main.java:25: error: class, interface, or enum expected
  for (int i=0; i<mes.length; i++) {
                              ^
Main.java:27: error: class, interface, or enum expected
  }
  ^
Main.java:32: error: class, interface, or enum expected
  switch(gs) {
  ^
Main.java:35: error: class, interface, or enum expected
    break;
    ^
Main.java:37: error: class, interface, or enum expected
  case Clear:
  ^
Main.java:39: error: class, interface, or enum expected
    func_cleared();
    ^
Main.java:40: error: class, interface, or enum expected
    break;
    ^
Main.java:41: error: class, interface, or enum expected
  }
  ^
Main.java:47: error: class, interface, or enum expected
    if (board.isClear()) {
    ^
Main.java:49: error: class, interface, or enum expected
    }
    ^
Main.java:52: error: class, interface, or enum expected
  case Clear:
  ^
Main.java:54: error: class, interface, or enum expected
  }
  ^
Main.java:58: error: class, interface, or enum expected
  func_update();
  ^
Main.java:59: error: class, interface, or enum expected
}
^
Main.java:64: error: class, interface, or enum expected
    break;
    ^
Main.java:66: error: class, interface, or enum expected
  case Clear:
  ^
Main.java:68: error: class, interface, or enum expected
    gs = GS.Playing;
    ^
Main.java:69: error: class, interface, or enum expected
    break;
    ^
Main.java:70: error: class, interface, or enum expected
  }
  ^
Main.java:152: error: '.class' expected
      if (select(int(random(width)), int(random(height)))) {
                    ^
Main.java:152: error: ';' expected
      if (select(int(random(width)), int(random(height)))) {
                                   ^
Main.java:152: error: not a statement
      if (select(int(random(width)), int(random(height)))) {
                                     ^
Main.java:152: error: ';' expected
      if (select(int(random(width)), int(random(height)))) {
                                        ^
Main.java:152: error: ';' expected
      if (select(int(random(width)), int(random(height)))) {
                                                       ^
38 errors
stdout
Standard output is empty