-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
90 lines (81 loc) · 3.35 KB
/
Copy pathMain.java
File metadata and controls
90 lines (81 loc) · 3.35 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
import java.awt.*;
import java.awt.event.InputEvent;
import java.util.ArrayDeque;
public class Main {
private static Robot robot;
private static final int[] columns = {470,550,630,710,790,870};
private static final int[] columnGutters = {509,590,669,749,828};
private static final int[] rows = {215,291,358,449,525,615};
private static final int[] rowGutters = {250,330,409,488,570};
//colours
private static final int BACKGROUND_RED = 51;
private static final int LIGHT_BLOCK_RED = 200;
private static final int RED_BLOCK_GREEN = 100;
public static void main(String[] args) {
try {robot = new Robot();} catch (AWTException ignore) {}
robot.setAutoDelay(50);
robot.setAutoWaitForIdle(true);
//wait 'till game is full screen
while(robot.getPixelColor(5,5).getRed() != BACKGROUND_RED) {
//poll
}
//for i number of levels
for (int i = 0; i < 8; i++) {
Board board = new Board();
discoverBlocks(board, columns, rowGutters, true);
discoverBlocks(board, rows, columnGutters, false);
makeMoves(StateSearch.AStar(board));
clickStartNextLevel();
}
}
private static void discoverBlocks(Board board, int[] listA, int[] listB, boolean verticalSearch) {
Block block = null;
for (int i = 0; i < listA.length; i++) { //for each column/row
for (int j = 0; j < listB.length; j++) { //for each rowGutter/columnGutter
int pixelX = verticalSearch ? listA[i] : listB[j];
int pixelY = verticalSearch ? listB[j] : listA[i];
//if pixel at x,y is the colour of a wooden block
if (robot.getPixelColor(pixelX, pixelY).getRed() > LIGHT_BLOCK_RED) {
if (block == null) {
if (verticalSearch)
block = new VerticalBlock(board.blocks.size(), i, j);
else
block = new HorizontalBlock(board.blocks.size(), j, i);
board.blocks.put(block.id, block);
//if the color
if (robot.getPixelColor(pixelX, pixelY).getGreen() < RED_BLOCK_GREEN) {
board.redBlockKey = block.id;
}
} else {
block.length++;
}
} else {
block = null;
}
}
block = null;
}
}
private static void makeMoves(ArrayDeque<int[][]> moves) {
while (!moves.isEmpty()) {
int[][] move = moves.removeFirst();
robot.mouseMove(columns[move[0][0]], rows[move[0][1]]);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseMove(columns[move[1][0]], rows[move[1][1]]);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
}
public static void clickStartNextLevel() {
try {
Thread.sleep(500);
} catch (InterruptedException ignore) {
}
robot.mouseMove(826, 559);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
try {
Thread.sleep(500);
} catch (InterruptedException ignore) {
}
}
}