/043/chess/Canvas.py

https://github.com/engineer-man/youtube · Python · 323 lines · 272 code · 46 blank · 5 comment · 79 complexity · d01d4c7fc5923afaba269403d889708d MD5 · raw file

  1. """
  2. This module contains functions that display text to the screen, such as error
  3. messages and the game board.
  4. """
  5. import os
  6. import platform
  7. import random
  8. import board
  9. import globVar
  10. import sys
  11. import utils
  12. def drawBoard():
  13. clear()
  14. nowPlaying()
  15. numLabel = 8
  16. letterLabel = 'A'
  17. print("\n ", end = "")
  18. for i in range(8):
  19. print(letterLabel + " ", end = "")
  20. letterLabel = chr(ord(letterLabel) + 1)
  21. print("\n ",end="")
  22. for i in range(18):
  23. print("_",end="")
  24. print("\n", end = "")
  25. for i in range(8):
  26. print(' {} |'.format(numLabel), end = "")
  27. for j in range(8):
  28. print(board.Grid(i, j), end = "")
  29. print("|",end="")
  30. numLabel -= 1
  31. print("\n", end = "")
  32. print(" ",end="")
  33. for i in range(18):
  34. print("¯",end="")
  35. print("\n",end="")
  36. remaining()
  37. def nowPlaying():
  38. print(" ",end="")
  39. for i in range(23):
  40. print("-",end="")
  41. if( (globVar.w_check and globVar.player == "W") or
  42. (globVar.b_check and globVar.player == "b")):
  43. print("\n | CHECK! ", globVar.player, " |")
  44. elif globVar.checkmate:
  45. print("\n | CHECKMATE! |")
  46. else:
  47. print("\n | NOW PLAYING: ", globVar.player, " |")
  48. print(" ",end="")
  49. for i in range(23):
  50. print("-",end="")
  51. def pawn_to_new():
  52. drawBoard()
  53. while True:
  54. try:
  55. print(" 1. Rook 2. Knight")
  56. print(" 3. Bishop 4. Queen")
  57. choice = input("\n Choose a new piece: ")
  58. choices(choice)
  59. except ValueError:
  60. pawnError()
  61. continue
  62. if (choice == "" or len(choice) > 1 or not choice.isdigit()
  63. or int(choice) < 1 or int(choice) > 4):
  64. pawnError()
  65. continue
  66. else:
  67. break
  68. return int(choice)
  69. def remaining():
  70. w_pawn_count = utils.typeCounter("pawn", "W")
  71. w_rook_count = utils.typeCounter("rook", "W")
  72. w_knight_count = utils.typeCounter("knight", "W")
  73. w_bishop_count = utils.typeCounter("bishop", "W")
  74. w_queen_count = utils.typeCounter("queen", "W")
  75. w_king_count = utils.typeCounter("king", "W")
  76. b_pawn_count = utils.typeCounter("pawn", "b")
  77. b_rook_count = utils.typeCounter("rook", "b")
  78. b_knight_count = utils.typeCounter("knight", "b")
  79. b_bishop_count = utils.typeCounter("bishop", "b")
  80. b_queen_count = utils.typeCounter("queen", "b")
  81. b_king_count = utils.typeCounter("king", "b")
  82. print(" ",end="")
  83. print(" REMAINING:\n ", end="")
  84. for i in range(23):
  85. print("_",end="")
  86. print("\n White: | Black:")
  87. print(" {}P' {}R' | {}p. {}r.".format(w_pawn_count, w_rook_count, b_pawn_count, b_rook_count))
  88. print(" {}N' {}B' | {}n. {}b.".format(w_knight_count, w_bishop_count, b_knight_count, b_bishop_count))
  89. print(" {}Q' {}K' | {}q. {}k.".format(w_queen_count, w_king_count, b_queen_count, b_king_count))
  90. print(" ",end="")
  91. for i in range(23):
  92. print("¯",end="")
  93. print("\n")
  94. def startScreen():
  95. while True:
  96. try:
  97. clear()
  98. print("\n Welcome to Chess: Python Edition!\n\n")
  99. n = input(" How many players for this game?\n (0, 1, or 2): ")
  100. except ValueError:
  101. print("\n Please choose an option.")
  102. print("\n Press Enter to continue.")
  103. input("")
  104. continue
  105. if (not n.isdigit()) or (int(n) < 0) or (int(n) > 2):
  106. print("\n Please choose an option.")
  107. print("\n Press Enter to continue.")
  108. input("")
  109. continue
  110. else:
  111. break
  112. globVar.numPlayers = int(n)
  113. if globVar.numPlayers < 2:
  114. random.seed(a=None)
  115. if globVar.numPlayers == 0:
  116. globVar.noPlayers = True
  117. speedMenu()
  118. board.populate()
  119. return True
  120. def speedMenu():
  121. while True:
  122. try:
  123. clear()
  124. print("\n At what speed would you like the AI to play?")
  125. print("\n 1. Slow enough to watch the game")
  126. print(" 2. Full speed ahead")
  127. n = input("\n Option: ")
  128. choices(n)
  129. except ValueError:
  130. print("\n Please choose an option.")
  131. print("\n Press Enter to continue.")
  132. input("")
  133. continue
  134. if (not n.isdigit()) or (int(n) < 1) or (int(n) > 2):
  135. print("\n Please choose an option.")
  136. print("\n Press Enter to continue.")
  137. input("")
  138. continue
  139. else:
  140. break
  141. if int(n) == 1:
  142. globVar.slow_speed = True
  143. else:
  144. globVar.slow_speed = False
  145. def chooseAvailableMessage():
  146. errorSeparator()
  147. print("\n Please choose a piece with available moves.")
  148. pressEnter()
  149. def getouttacheckMessage():
  150. errorSeparator()
  151. print("\n Choose a move to get out of check.")
  152. pressEnter()
  153. def pickValidMoveMessage():
  154. errorSeparator()
  155. print("\n Please pick a valid move.")
  156. pressEnter()
  157. def pawnError():
  158. errorSeparator()
  159. print("\n Please pick a valid piece.")
  160. pressEnter()
  161. def pressEnter():
  162. print(" Press Enter to continue.")
  163. input("")
  164. drawBoard()
  165. def selectError():
  166. errorSeparator()
  167. print("\n Please choose a square with one of your pieces.")
  168. pressEnter()
  169. def colError():
  170. errorSeparator()
  171. print("\n Please choose a valid column.")
  172. pressEnter()
  173. def rowError():
  174. errorSeparator()
  175. print("\n Please choose a valid row.")
  176. pressEnter()
  177. def errorSeparator():
  178. print("\n ",end="")
  179. for i in range(43):
  180. print("-",end="")
  181. def clear():
  182. if platform.system() == "Linux":
  183. os.system("clear")
  184. if platform.system() == "Darwin":
  185. os.system("clear")
  186. elif platform.system() == "Windows":
  187. os.system("CLS")
  188. else:
  189. print("\033c")
  190. def chooseCol():
  191. while True:
  192. try:
  193. choice = input("\n Choose a column (letter): ")
  194. choices(choice)
  195. except ValueError:
  196. colError()
  197. continue
  198. if (choice == "" or len(choice) > 1 or
  199. ord(choice.upper()) < ord('A') or ord(choice.upper()) > ord('H')):
  200. colError()
  201. continue
  202. else:
  203. break
  204. return choice
  205. def chooseRow():
  206. while True:
  207. try:
  208. choice = input("\n Choose a row (number): ")
  209. choices(choice)
  210. except ValueError:
  211. rowError()
  212. continue
  213. if not choice.isdigit() or choice == "":
  214. rowError()
  215. continue
  216. elif int(choice) < 1 or int(choice) > 8:
  217. rowError()
  218. continue
  219. else:
  220. break
  221. return int(choice)
  222. def chooseMove(availMovesL):
  223. while True:
  224. try:
  225. choice = input("\n Choose a move (number): ")
  226. choices(choice)
  227. except ValueError:
  228. pickValidMoveMessage()
  229. continue
  230. if not choice.isdigit() or choice == "":
  231. pickValidMoveMessage()
  232. continue
  233. elif (int(choice) < 1) or (int(choice) > availMovesL):
  234. pickValidMoveMessage()
  235. continue
  236. else:
  237. break
  238. return int(choice)
  239. def choices(choice):
  240. if choice.upper() == "Q":
  241. quit()
  242. elif choice.upper() == "R":
  243. board.populate()
  244. clear()
  245. print("\n The board has been reset.")
  246. pressEnter()
  247. elif choice.upper() == "L":
  248. utils.readSave()
  249. clear()
  250. print("\n The last save has been loaded.")
  251. pressEnter()
  252. def quit():
  253. clear()
  254. print("\n Would you like to save your game? ", end="")
  255. y = yesNo()
  256. clear()
  257. if y:
  258. utils.writeSave()
  259. else:
  260. utils.delete_save()
  261. sys.exit(0)
  262. def yesNo():
  263. y = input("(y/n): ")
  264. if (y.upper() == "Y" or y.upper() == "YES"):
  265. return True
  266. else:
  267. return False
  268. def loadSave():
  269. clear()
  270. print("\n Save detected. Load previous game? ", end="")
  271. y = yesNo()
  272. if y:
  273. board.populate()
  274. utils.readSave()
  275. else:
  276. # board.populate()
  277. startScreen()