-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlightsout.py
More file actions
139 lines (115 loc) · 4.37 KB
/
Copy pathlightsout.py
File metadata and controls
139 lines (115 loc) · 4.37 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/python
# Head ends here
import copy
import random
def nextMove(player,board):
winning_move = win(board)
if winning_move != False:
print(str(winning_move["x"]) + " " + str(winning_move["y"]))
return None
losing_moves = opponentWin(board)
for x in xrange(0,8):
for y in xrange(0,8):
if x > 3 and len(losing_moves) == 0:
while(True):
randX = random.randint(0,7)
randY = random.randint(0,7)
if board[randX][randY] == "1":
print(str(randX) + " " + str(randY))
return None
elif board[x][y] == "1" and str(x)+str(y) not in losing_moves:
print(str(x) + " " + str(y))
return None
# if we've gotten here we're screwed, just hit the first
for i in xrange(0,8):
for j in xrange(0,8):
if board[i][j] == "1":
print(str(i) + " " + str(j))
return None
def win(board):
test_board = copy.copy(board)
for x in xrange(0,8):
for y in xrange(0,8):
if test_board[x][y] == "1":
# simulate a switch
test_board[x] = test_board[x][:y] + "0" + test_board[x][y+1:]
try:
if test_board[x+1][y] == "1":
test_board[x+1] = test_board[x+1][:y] + "0" + test_board[x+1][y+1:]
else:
test_board[x+1] = test_board[x+1][:y] + "1" + test_board[x+1][y+1:]
except:
pass
try:
if test_board[x][y+1] == "1":
test_board[x] = test_board[x][:y+1] + "0" + test_board[x][y+2:]
else:
test_board[x] = test_board[x][:y+1] + "1" + test_board[x][y+2:]
except:
pass
# check if the simulated state causes a win
if(checkWin(test_board) == True):
return {"x":x, "y":y}
else:
test_board = copy.copy(board)
return False
def opponentWin(board):
test_board = copy.copy(board)
losingMoves = []
for x in xrange(0,8):
for y in xrange(0,8):
if test_board[x][y] == "1":
# simulate a switch
test_board[x] = test_board[x][:y] + "0" + test_board[x][y+1:]
try:
if test_board[x+1][y] == "1":
test_board[x+1] = test_board[x+1][:y] + "0" + test_board[x+1][y+1:]
else:
test_board[x+1] = test_board[x+1][:y] + "1" + test_board[x+1][y+1:]
except:
pass
try:
if test_board[x][y+1] == "1":
test_board[x] = test_board[x][:y+1] + "0" + test_board[x][y+2:]
else:
test_board[x] = test_board[x][:y+1] + "1" + test_board[x][y+2:]
except:
pass
# check if the simulated state causes a win
winningMove = win(test_board)
if(winningMove != False):
losingMoves.append(str(x)+str(y))
elif(checkEndGame(test_board)):
losingMoves.append(str(x)+str(y))
test_board = copy.copy(board)
return losingMoves
def checkWin(board):
for x in xrange(0,8):
for y in xrange(0,8):
if board[x][y] == "1":
return False
return True
def checkEndGame(board):
on_count = 0
for x in xrange(0,8):
for y in xrange(0,8):
if board[x][y] == "1":
on_count = on_count + 1
if on_count < 5 and on_count > 0:
if(board[6][6] == "1" and board[6][7] == "1" and board[7][6] == "1" and board[7][7] == "1"):
return True
elif(board[6][6] == "0" and board[6][7] == "1" and board[7][6] == "1" and board[7][7] == "0"):
return True
else:
return False
else:
return False
# Tail starts here
#If player is 1, I'm the first player.
#If player is 2, I'm the second player.
player = raw_input()
#Read the board now. The board is a 8x8 array filled with 1 or 0.
board = []
for i in xrange(0, 8):
board.append(raw_input())
nextMove(player,board)