Skip to content

Instantly share code, notes, and snippets.

@tomtheisen
Created December 1, 2013 18:38
Show Gist options
  • Save tomtheisen/7739034 to your computer and use it in GitHub Desktop.
Save tomtheisen/7739034 to your computer and use it in GitHub Desktop.
Two ways to print a checkerboard
def printboard1(chars, squares):
def boardchar(row, col):
return " X"[(row^col) & 1]
print "-" * (chars * squares + 2)
for row in range(squares):
contents = "".join(boardchar(row, col) * chars for col in range(squares))
print ("|" + contents + "|\n") * chars,
print "-" * (chars * squares + 2)
def printboard2(chars, squares):
def boardchar(x, y):
if y in [-1, chars * squares]:
return "-"
elif x in [-1, chars * squares]:
return "|"
elif (x / chars + y / chars) % 2:
return "X"
else:
return " "
coordinatesrange = range(-1, chars * squares + 1)
for y in coordinatesrange:
print "".join(boardchar(x, y) for x in coordinatesrange)
print "Get ready to make your checkerboard!"
chars = input("Please enter characters per square: ")
squares = input("Please enter squares per side: ")
printboard1(chars, squares)
printboard2(chars, squares)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment