-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·119 lines (99 loc) · 2.8 KB
/
main.py
File metadata and controls
executable file
·119 lines (99 loc) · 2.8 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
#!/usr/bin/env python3
from defs import Board, slots, banner, Card
import copy
import pyautogui
import random
from scanner import prepare_samples, scan_board
from solver import Move, simulate_move, solve, get_moves
from time import sleep
from typing import Tuple
DELAY = 0.2
LIMIT = 30000
PITCH_Y = 31
DEBUG = False
def board_to_click(pos: Tuple[int, int]) -> Tuple[int, int]:
x, y = slots.rows[pos[0]].click
y += PITCH_Y * pos[1]
return x, y
def special_to_click(pos: int) -> Tuple[int, int]:
return slots.special[pos].click
def execute_move(move: Move) -> int:
if move.DRAGONMOVE:
sleep(DELAY)
for d in slots.dragons:
pyautogui.moveTo(d.click, duration=DELAY)
sleep(DELAY)
pyautogui.mouseDown()
sleep(0.3)
pyautogui.mouseUp()
return 0
if move.WINMOVE:
print("Congratulations!")
return 1
startx, starty = 0, 0
endx, endy = 0, 0
if move.src:
startx, starty = board_to_click(move.src)
if move.special_src is not None:
startx, starty = special_to_click(move.special_src)
if move.dst:
endx, endy = board_to_click(move.dst)
if move.special_dst is not None:
endx, endy = special_to_click(move.special_dst)
pyautogui.moveTo(startx, starty, duration=DELAY)
pyautogui.dragTo(endx, endy, duration=DELAY)
sleep(move.wait * 0.2)
return 0
def test_prediction(board, move):
bcopy = copy.deepcopy(board)
simulate_move(bcopy, move)
board = Board.new()
scan_board(board, slots)
if bcopy.get_hash() != board.get_hash():
print("Move failed")
print("Expected")
bcopy.show()
print("Got")
board.show()
exit(1)
#Do random moves interactivly to test the prediction
def debug_mode():
while 1:
board = Board.new()
scan_board(board, slots)
board.show()
moves = get_moves(board)
for m in moves:
print(m.show(board))
move = random.choice(moves)
print("Executing move")
execute_move(move)
test_prediction(board, move)
def start_new_game():
pyautogui.moveTo(1488, 940, duration=DELAY)
pyautogui.mouseDown()
sleep(0.3)
pyautogui.mouseUp()
sleep(5)
def do_game():
for _ in range(5):
board = Board.new()
scan_board(board, slots)
board.show()
solved = solve(board, LIMIT)
if not solved:
print("No solution found")
return
print("Soved with {} moves".format(len(solved.moves)))
board.show()
for m in solved.moves:
if execute_move(m):
return
if __name__ == "__main__":
print(banner)
prepare_samples()
sleep(5)
#while True:
#start_new_game()
do_game()
#sleep(5)