capstone.pyw 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. from graphics import *
  2. import time
  3. '''This program accepts a number n from the user, generates an nxn board,
  4. allows the user to place n queens, and checks to see if any of the queens
  5. can attack each other under the standard rules of chess: in other words,
  6. it determines if there is more than one queen in any given row, column, or
  7. diagonal. It then highlights any queens that are placed incorrectly. It uses a
  8. completely graphical interface - the command line need not be used.'''
  9. def smallcrown(point, win):
  10. '''draws a crown shape to represent a queen within a specified square
  11. on a board'''
  12. x = int(point.getX())
  13. y = int(point.getY())
  14. poly = Polygon(Point(x+.7, y+.3), Point(x+.3, y+.3), Point(x+.2, y+.6), Point(x+.35, y+.45), Point(x+.43, y+.6), Point(x+.5, y+.47), Point(x+.58, y+.6), Point(x+.65, y+.45), Point(x+.8, y+.6))
  15. poly.draw(win)
  16. def drawboard(win, n):
  17. '''draws a square grid of a specified size'''
  18. for i in range(n):
  19. line = Line(Point(i,0), Point(i, n))
  20. line.setWidth(1)
  21. line.draw(win)
  22. for i in range(n):
  23. line = Line(Point(0,i), Point(n,i))
  24. line.setWidth(1)
  25. line.draw(win)
  26. def placequeen(win, n):
  27. '''allows the user to place a specified number of 'queens' on a square
  28. board'''
  29. drawboard(win, n)
  30. pointX = []
  31. pointY = []
  32. for i in range(n):
  33. point = win.getMouse()
  34. pointX.append(int(point.getX()))
  35. pointY.append(int(point.getY()))
  36. smallcrown(point, win)
  37. return pointX, pointY
  38. def getMatrix(win, n):
  39. '''converts a board with n placed queens into a list of rows,
  40. with an empty space represented by 0 and a queen represented by 1'''
  41. X, Y = placequeen(win, n)
  42. Xrows = [[]]*n
  43. Yrows = [[]]*n
  44. for i in range(n):
  45. Xrows[i] = [0]*n
  46. Yrows[i] = [0]*n
  47. for i in range(n):
  48. Xrows[Y[i]][X[i]] = 1
  49. Yrows[X[i]][Y[i]] = 1
  50. return Xrows, Yrows
  51. def coord(n, x, y, qn):
  52. '''Takes row descriptions created by getMatrix and returns the following
  53. information for each queen: x, y, x+y, x-y'''
  54. coords = [[0,0]]*n
  55. coord = 0
  56. for row in range(n):
  57. for col in range(n):
  58. if x[row][col] == qn:
  59. coords[coord] = [row,col,row+col,row-col]
  60. coord += 1
  61. return coords
  62. def checkbit(n, coords, bp):
  63. '''uses a subset of the information generated by coord and uses it to
  64. check if there are multiple queens in any row, column, or diagonal. Makes
  65. a list of the (x,y) coordinates of those queens'''
  66. dct = {}
  67. lst = []
  68. for i in range(n):
  69. k = coords[i][bp]
  70. if dct.has_key(k):
  71. dct[k] = dct[k] + coords[i][0:2]
  72. else:
  73. dct[k] = coords[i][0:2]
  74. lst = dct.values()
  75. queens = []
  76. if len(lst) < n:
  77. for i in range(len(lst)):
  78. if len(lst[i]) <> 2:
  79. for b in range(0, len(lst[i]), 2):
  80. queens = queens + [lst[i][b:b+2]]
  81. return queens
  82. def checker(n, x, y, qn):
  83. '''Calls checkbit for each type of coordinate in turn'''
  84. coords = coord(n, x, y, qn)
  85. rows = checkbit(n, coords, 0)
  86. cols = checkbit(n, coords, 1)
  87. diag1 = checkbit(n, coords, 2)
  88. diag2 = checkbit(n, coords, 3)
  89. return rows, cols, diag1, diag2
  90. def main():
  91. '''This is the function to invoke to run the entire program'''
  92. win = GraphWin("Some Queens", 500, 500)
  93. f = Text(Point(250, 220), "Please enter a number for size of board")
  94. f.draw(win)
  95. e = Entry(Point(250, 250), 4)
  96. e.draw(win)
  97. b = Text(Point(250, 280), "Click to continue")
  98. b.draw(win)
  99. win.getMouse()
  100. n = int(e.getText())
  101. e.undraw()
  102. f.undraw()
  103. b.undraw()
  104. win.setCoords(0,0,n,n)
  105. x, y = getMatrix(win, n)
  106. rows, cols, diag1, diag2 = checker(n, x, y, 1)
  107. queens = rows + cols + diag1 + diag2
  108. time.sleep(0.5)
  109. if len(queens) == 0:
  110. t = Text(Point(n/2.0, (3*n)/4.0), "You're right!")
  111. t.setFill('blue')
  112. t.setSize(36)
  113. t.setStyle('bold')
  114. t.draw(win)
  115. else:
  116. for i in range(len(queens)):
  117. x = queens[i][0]
  118. y = queens[i][1]
  119. sqr = Rectangle(Point(y, x), Point(y+1, x+1))
  120. sqr.setFill('red')
  121. sqr.draw(win)
  122. smallcrown(Point(y, x), win)
  123. t = Text(Point(n/2.0, (3*n)/4.0), "Sorry, try again :(")
  124. t.setFill('blue')
  125. t.setSize(36)
  126. t.setStyle('bold')
  127. t.draw(win)
  128. clo = Text(Point(n/2.0, n/2.0), "Click to close")
  129. clo.setFill('blue')
  130. clo.setSize(20)
  131. clo.draw(win)
  132. win.getMouse()
  133. win.close()
  134. return
  135. main()