Skip to content

Commit

Permalink
Fixed critical out-of bounds bug
Browse files Browse the repository at this point in the history
  • Loading branch information
the-cybersapien committed Apr 26, 2017
1 parent e3d5be9 commit 5027644
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 64 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
back_dat/
cell_dat/
*.db
123.txt

.idea/
### Python template
Expand Down
32 changes: 18 additions & 14 deletions SciKit-Play-GUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,33 @@ def callmain(self):
driver_obj = driver.Driver(board_coords)
driver_obj.play()

# For 768p Screen
def combo_chosen_1(self):
global counter
counter = 0
global c1
global c2
global c3
global c4
c1 = int(688)
c2 = int(60)
c3 = int(1325)
c4 = int(622)
global c4
c1 = int(436)
c2 = int(54)
c3 = int(1011)
c4 = int(560)
print (c1, c2, c3, c4)

# For 1080p screen
def combo_chosen_2(self):
global counter
counter = 0
global c1
global c2
global c3
global c4
c1 = int(436)
c2 = int(55)
c3 = int(1010)
c4 = int(563)
global c4
c1 = int(688)
c2 = int(60)
c3 = int(1325)
c4 = int(622)
print (c1, c2, c3, c4)

def statistic(self):
url_1 = QtCore.QUrl('utilities\stat_1.pyw')
Expand Down Expand Up @@ -243,7 +247,7 @@ def setupUi(self, widget):
self.radioButton.setChecked(False)
self.radioButton.setObjectName(_fromUtf8("radioButton"))
##############################
self.radioButton.clicked.connect(self.combo_chosen_2)
self.radioButton.clicked.connect(self.combo_chosen_1)
###############################
self.verticalLayout.addWidget(self.radioButton)
self.radioButton_2 = QtGui.QRadioButton(self.horizontalLayoutWidget_2)
Expand All @@ -252,7 +256,7 @@ def setupUi(self, widget):
"font: 87 12pt \"Lato Black\";"))
self.radioButton_2.setObjectName(_fromUtf8("radioButton_2"))
##############################
self.radioButton_2.clicked.connect(self.combo_chosen_1)
self.radioButton_2.clicked.connect(self.combo_chosen_2)
###############################
self.verticalLayout.addWidget(self.radioButton_2)
self.radioButton_3 = QtGui.QRadioButton(self.horizontalLayoutWidget_2)
Expand Down Expand Up @@ -355,8 +359,8 @@ def retranslateUi(self, widget):
"<html><head/><body><p><span style=\" color:#ffffff;\">your coordinates</span></p></body></html>",
None))
self.label_3.setText(_translate("widget", "Resolution:", None))
self.radioButton.setText(_translate("widget", "1920 x 1080", None))
self.radioButton_2.setText(_translate("widget", "1366 x 768", None))
self.radioButton.setText(_translate("widget", "1366 x 768", None))
self.radioButton_2.setText(_translate("widget", "1920 x 1080", None))
self.radioButton_3.setText(_translate("widget", "Customized", None))
self.label_4.setToolTip(_translate("widget",
"<html><head/><body><p><span style=\" color:#ffffff;\">your coordinates</span></p></body></html>",
Expand Down
55 changes: 34 additions & 21 deletions driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,58 @@
from PIL import ImageGrab

import move_maker
from back_decoder import BackRecognizer
from cell_decoder import CellRecognizer
from utils import *

class Driver:
def __init__(self, board_coords):
self.ref_img = None
self.board_box = board_coords
print self.board_box
self.img_size = (board_coords[2] - board_coords[0], board_coords[3] - board_coords[1])
self.cell_size = (self.img_size[0] / 9, self.img_size[1] / 9)
self.game_board = np.zeros((board_size, board_size), dtype=np.int32)
self.cell_recognizer = CellRecognizer()
self.cell_recognizer.train()
self.background_img = Image.open('background.bmp')
self.background_img = self.background_img.resize(
(self.background_img.size[0] / 4, self.background_img.size[1] / 4), Image.NEAREST)

self.prev_board = None

self.back_recognizer = BackRecognizer()
self.back_recognizer.train()

self.mover = move_maker.MoveMaker()
self.img_end_game = Image.open('end_screen.bmp')
self.img_end_game = self.img_end_game.resize((self.img_end_game.size[0] / 4, self.img_end_game.size[1] / 4),
Image.NEAREST)


def close_script(self):
print 'wow'
sys.exit()

# It takes the screenshot of the board and then crops each cell using a nested loop
def play(self):
fx = open("flag.txt", "w+")
fx.write("0")
fx.close()
fx = open("prev.txt", "w+")
fx.write("[(0, 0), (0, 0)]")
fx.close()
while True:
if not self.board_is_moving():
board_img = self.grab_board()
board_state = self.back_recognizer.predict(board_img)
print 'working yo! '
print board_state
if board_state == scoreboard:
break
else:
move = self.mover.solve_board(self.game_board)
self.do_move(move)
time.sleep(0.4) # wait for the cells to finish moving


# It takes the screenshot of the board and then crops each cell using a nested loop
def grab_board(self):
global game_board
global temp_board
# global game_board
# global temp_board
img = ImageGrab.grab()
img = img.crop(self.board_box)
for y in range(0, 9):
Expand All @@ -50,17 +74,6 @@ def grab_board(self):
img = img.resize((img.size[0] / 4, img.size[1] / 4), Image.NEAREST)
return img

def play(self):
while True:
if not self.board_is_moving():
board_img = self.grab_board()
if self.compare_images(board_img, self.img_end_game, 10) < 3000:
break
move = self.mover.solve_board(self.game_board,)
#dbg.print_board(temp_board)
self.do_move(move)
time.sleep(0.4) # wait for the cells to finish moving

# checks if the board is in moving state (It is in moving state after the candies have been exploded)
def board_is_moving(self):
img = ImageGrab.grab()
Expand Down Expand Up @@ -121,4 +134,4 @@ def do_move(self, move):
win32api.SetCursorPos(end_w)
time.sleep(0.3) # for waiting the board to settle down after explosions
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, end_w[0], end_w[1], 0, 0)
win32api.SetCursorPos((1100, 1100))
win32api.SetCursorPos((self.board_box[0] - 50, self.board_box[1] - 50))
8 changes: 1 addition & 7 deletions driver_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,9 @@ def __init__(self, board_coords):
self.prev_board = None

def play(self):
moves = []
mover = [(0,0), (0,0)]
board_img = None
while True:
if not self.board_is_moving():
if not self.checkDiff(board_img):
moves.append(mover)
else:
moves = []
board_img = self.get_board()
board_state = self.back_recognizer.predict(board_img)
if board_state == curtain:
Expand All @@ -52,7 +46,7 @@ def play(self):
break
else:
self.grab_board()
score, mover = self.mover.solve_board(self.game_board, moves)
mover = self.mover.solve_board(self.game_board)
self.do_move(mover)
time.sleep(0.4) # wait for the cells to finish moving

Expand Down
44 changes: 22 additions & 22 deletions move_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ def solve_board(self, board):
if (flag == '0'):
max_score = 0
chosen_move = []
for i in range(0, 8):
for j in range(0, 8):
for i in xrange(0, 9):
for j in xrange(0, 9):
possible_directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for d in possible_directions:
score, move = self.check_direction((i, j), d)
Expand Down Expand Up @@ -225,12 +225,12 @@ def solve_board(self, board):
filex = open('prev.txt', 'r+')
previous_move = filex.read()
filex.close()

if (previous_move == str(chosen_move)):
filev = open('flag.txt', 'w+')
filev.write('1')
filev.close()

file = open('prev.txt', 'w+')
file.write(repr(chosen_move))
file.close()
Expand All @@ -245,31 +245,31 @@ def solve_board(self, board):

def aux_solve(board):
x = [(4, 5), (4, 6)]
for i in range(0, 8):
for j in range(0, 8):
if (j <= 5 and board[i][j] == board[i][j + 2] and board[i][j] == board[i][j + 3]):
for i in xrange(0, 9):
for j in xrange(0, 9):
if j <= 5 and board[i][j] == board[i][j + 2] and board[i][j] == board[i][j + 3]:
chosen_move = [(i, j), (i, j + 1)]
return chosen_move
if (j >= 3 and board[i][j] == board[i][j - 2] and board[i][j] == board[i][j - 3]):
if j >= 3 and board[i][j] == board[i][j - 2] and board[i][j] == board[i][j - 3]:
chosen_move = [(i, j - 1), (i, j)]
return chosen_move
if (i <= 5 and board[i][j] == board[i + 2][j] and board[i][j] == board[i + 3][j]):
if i <= 5 and board[i][j] == board[i + 2][j] and board[i][j] == board[i + 3][j]:
chosen_move = [(i, j), (i + 1, j)]
return chosen_move
if (i >= 3 and board[i][j] == board[i - 2][j] and board[i][j] == board[i - 3][j]):
if i >= 3 and board[i][j] == board[i - 2][j] and board[i][j] == board[i - 3][j]:
chosen_move = [(i - 1, j), (i, j)]
return chosen_move
if (i <= 7 and j <= 6 and board[i][j] == board[i+1][j+1] and board[i][j] == board[i+1][j+2]):
chosen_move = [(i, j), (i+1, j)]
return chosen_move
if (i <= 7 and j >= 1 and j <= 7 and board[i][j] == board[i+1][j-1] and board[i][j] == board[i+1][j+1]):
chosen_move = [(i, j), (i+1, j)]
return chosen_move
if ((i <= 6 and j >= 1 and board[i][j] == board[i+1][j-1] and board[i][j] == board[i+2][j-1]) or (i >= 2 and j >= 1 and board[i][j] == board[i-1][j-1] and board[i][j] == board[i-2][j-1])):
chosen_move = [(i, j-1), (i, j)]
return chosen_move
if ((i <= 6 and j <= 7 and board[i][j] == board[i+1][j+1] and board[i][j] == board[i+2][j+1]) or (i >= 2 and j <= 7 and board[i][j] == board[i-1][j+1] and board[i][j] == board[i-2][j+1])):
chosen_move = [(i, j), (i, j+1)]
return chosen_move
if i <= 7 and j <= 6 and board[i][j] == board[i+1][j+1] and board[i][j] == board[i+1][j+2]:
chosen_move = [(i, j), (i+1, j)]
return chosen_move
if 7 >= i >= 1 and j <= 7 and board[i][j] == board[i+1][j-1] and board[i][j] == board[i+1][j+1]:
chosen_move = [(i, j), (i+1, j)]
return chosen_move
if ((i <= 6 and j >= 1 and board[i][j] == board[i+1][j-1] and board[i][j] == board[i+2][j-1]) or (i >= 2 and j >= 1 and board[i][j] == board[i-1][j-1] and board[i][j] == board[i-2][j-1])):
chosen_move = [(i, j-1), (i, j)]
return chosen_move
if ((i <= 6 and j <= 7 and board[i][j] == board[i+1][j+1] and board[i][j] == board[i+2][j+1]) or (i >= 2 and j <= 7 and board[i][j] == board[i-1][j+1] and board[i][j] == board[i-2][j+1])):
chosen_move = [(i, j), (i, j+1)]
return chosen_move
return 1 # DONE: Complete this class with the evaluation functions as discussed.
# DONE: Create the explosions function to see the benefits from the special candies (if any)

0 comments on commit 5027644

Please sign in to comment.