-
Notifications
You must be signed in to change notification settings - Fork 0
/
lights.py
81 lines (70 loc) · 2.12 KB
/
lights.py
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
#!/usr/bin/env python
def playgame():
"""Asks user what size board they want to play on, creates a
board, and runs the game."""
size = int(raw_input("Enter a game size: "))
board = []
for i in range(size):
row = []
for j in range(size):
row.append(True)
board.append(row)
done = False
while not done:
printboard(board)
input = raw_input("Enter a row and column: ").split(' ')
row = int(input[0])-1
col = int(input[1])-1
board = move(board,row,col)
done = checkboard(board)
input = raw_input("You win! Would you like to play again: ")
if input.lower().strip() == "yes":
game()
else:
print "Thanks for playing!"
def checkboard(board):
"""Checks the board to see whether or not the game
is finished."""
for row in board:
for state in row:
if state:
return False
return True
def printboard(board):
"""Prints out the game board where an 'x' represents
that there is a light on at that location and a '.'
represents that there is a light off at that location."""
size = len(board)
print " ",
for i in range(size):
print (i+1),
print ''
for i,row in enumerate(board):
print (i+1),
for state in row:
if state:
print 'x',
else:
print '.',
print ''
def switch(board, row, col):
"""Toggles a light on or off at the given location."""
board[row][col] = not board[row][col]
return board
def move(board, row, col):
"""Toggles all thew lights that are affected by the
players movement to the given location."""
size = len(board)
board[row][col] = not board[row][col]
myboard = board
if row - 1 >= 0:
myboard = switch(myboard,row-1,col)
if row + 1 < size:
myboard = switch(myboard,row+1,col)
if col - 1 >= 0:
myboard = switch(myboard,row,col-1)
if col + 1 < size:
myboard = switch(myboard,row,col+1)
return myboard
if __name__ == "__main__":
playgame()