Skip to content

Instantly share code, notes, and snippets.

@nhatminhbui
Last active September 13, 2022 08:38
Show Gist options
  • Save nhatminhbui/bf17b57dc289667100bbdf473731383c to your computer and use it in GitHub Desktop.
Save nhatminhbui/bf17b57dc289667100bbdf473731383c to your computer and use it in GitHub Desktop.
check.py
def check(A, r, c):
"""
ARGs:
- tick position
RETURN:
boolean
"""
global win_array
# row
cbw = c
cfw = c
neighbor = 1
while (neighbor <= 4
and cbw > 0 and cfw < len(A)-1):
if A[r][cbw-1] == 1: cbw -= 1
if A[r][cfw+1] == 1: cfw += 1
neighbor += 1
if distance(r, cbw, r, cfw) == 16:
for i in range(5): # construct array
win_array.append((r, cbw+i))
return True
# col
ruw = r
rdw = r
neighbor = 1
while (neighbor <= 4
and ruw > 0 and rdw < len(A)-1):
if A[ruw-1][c] == 1: ruw -= 1
if A[rdw+1][c] == 1: rdw += 1
neighbor += 1
if distance(ruw, c, rdw, c) == 16:
for i in range(5):
win_array.append((ruw+i, c))
return True
# diag
ulr, ulc = r, c
drr, drc = r, c
neighbor = 1
while (neighbor <= 4
and ulr > 0 and ulc > 0
and drr < len(A)-1 and drc < len(A)-1):
if A[ulr-1][ulc-1] == 1:
ulr -= 1; ulc -= 1
if A[drr+1][drc+1] == 1:
drr +=1; drc +=1
neighbor += 1
if distance(ulr, ulc, drr, drc) == 32:
for i in range(5):
win_array.append((ulr+i, ulc+i))
return True
# anti-diag
urr, urc = r, c
dlr, dlc = r, c
neighbor = 1
while (neighbor <= 4
and urr > 0 and urc < len(A)-1
and dlr < len(A)-1 and dlc > 0):
if A[urr-1][urc+1] == 1:
urr -= 1; urc += 1
if A[dlr+1][dlc-1] == 1:
dlr += 1; dlc -= 1
neighbor += 1
if distance(urr, urc, dlr, dlc) == 32:
for i in range(5):
win_array.append((urr+i, urc-i))
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment