Skip to content

Instantly share code, notes, and snippets.

@andrewrk
Last active August 29, 2015 13:56
Show Gist options
  • Save andrewrk/9203269 to your computer and use it in GitHub Desktop.
Save andrewrk/9203269 to your computer and use it in GitHub Desktop.
import random
def guess():
board = [[False] * 8 for y in range(8)]
left = 8
while left > 0:
x = random.randint(0, 7)
y = random.randint(0, 7)
if not board[y][x]:
board[y][x] = True
left -= 1
return board
def didItWork(board):
def inBounds(x, y):
return x >= 0 and x < 8 and y >= 0 and y < 8
def dirFail(x, y):
dirs = [(-1, -1), (-1, 1), (1, -1), (1, 1), (1, 0), (-1, 0), (0, -1), (0, 1)]
for direct in dirs:
xdir, ydir = direct
xit = x + xdir
yit = y + ydir
while inBounds(xit, yit):
if board[yit][xit]:
return True
xit += xdir
yit += ydir
return False
for y in range(8):
for x in range(8):
if board[y][x]:
if dirFail(x, y):
return False
return True
while True:
board = guess()
if didItWork(board):
print(board)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment