-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCodeLink 04.py
More file actions
140 lines (107 loc) · 3.71 KB
/
CodeLink 04.py
File metadata and controls
140 lines (107 loc) · 3.71 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
140
# THE COMPUTER ALWAYS WINS
# MIT PRESS
#
# CodeLink 4
# Eight Queens
# last revised 5/6/26
# Using backtracking, can you find the 92 solutions to Eight Queens?
# You will need to write your own recursive function to replace the
# below playRandomly() function, but you might want to reuse my isLegal()
# to evaluate whether a specific placement is permissible.
import random
# FUNCTIONS
# print the board nicely
def printBoard(board):
for row in range(0,8):
for col in range(0,8):
if board[row][col] == 0:
print(" _ ", end="")
if board[row][col] != 0:
print(" %d " % board[row][col], end="")
print("")
print("")
# return True if this space is legal
def isLegal (board, possRow, possCol):
# check this row
for counter in range(0,8):
if board[possRow][counter] >= 1:
return False
# check this column
for counter in range(0,8):
if board[counter][possCol] >= 1:
return False
# check the diagonals from here, up left
colCounter = possCol
for rowCounter in range(possRow, 0, -1):
if colCounter-1 >= 0:
if board[rowCounter-1][colCounter-1] >= 1:
return False
colCounter = colCounter - 1
# check the diagonal from here, up right
colCounter = possCol
for rowCounter in range(possRow, 0, -1):
if colCounter+1 <= 7:
if board[rowCounter-1][colCounter+1] >= 1:
return False
colCounter = colCounter + 1
# check the diagonal from here, down left
colCounter = possCol
for rowCounter in range(possRow, 7):
if colCounter-1 >= 0:
if board[rowCounter+1][colCounter-1] >= 1:
return False
colCounter = colCounter-1
# check the diagonal from here, down right
colCounter = possCol
for rowCounter in range(possRow, 7):
if colCounter+1 <= 7:
if board[rowCounter+1][colCounter+1] >= 1:
return False
colCounter = colCounter+1
return True
def playRandomly (board):
# randomly place eight queens, and evaluate the board
queensPlaced = 0
success = True
while queensPlaced < 8:
randomCol = random.randint(0,7)
randomRow = random.randint(0,7)
if board[randomRow][randomCol] == 0:
# found an empty space
# test to see if placing a queen here causes a conflict
if not isLegal(board, randomRow, randomCol):
success = False
# place the queen
queensPlaced = queensPlaced + 1
board[randomRow][randomCol] = queensPlaced
printBoard(board)
if success == True:
print ("By sheer luck, I found a winning board!")
else:
print ("My random placement did not work out.")
return
# main program
# ############
# Note that the board variable defined below has an extra row.
# I use positions board[0][0] through board[7][7],
# but I do not use board[8][0] through board [8][7].
# When you revise the code, feel free to use those extra spaces if you
# want to keep track of additional debug information about the game board.
# You also can just ignore them if you don't want to use them.
board[8].
board = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
print()
print("You will solve this problem using backtracking.")
print("Me? I am just trying random placements right now.")
print()
playRandomly(board)
print("")