Guest User

Game Code

a guest
Apr 11th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.05 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Mar 12 12:36:46 2020
  5.  
  6. @author: mcaggiano2022
  7. """
  8. import random
  9. import math
  10. from cards import Deck
  11. from cards import Card
  12.  
  13.  
  14. def setup():
  15. """
  16. paramaters: None (deck can be created within this function)
  17. returns:
  18. - a foundation (list of 4 empty lists)
  19. - cell (list of 4 empty lists)
  20. - a tableau (a list of 8 lists, the dealt cards)
  21. """
  22. global gametime
  23. gametime = 0
  24. d = Deck()
  25. d.shuffle()
  26. foundation = [[], [], [], []]#placeholder, needs replaced!!
  27. #placeholder, needs replaced!!
  28. tableau1 = [] #placeholder, needs replaced!!
  29. tableau2 = []
  30. tableau3 = []
  31. tableau4 = []
  32. tableau5 = []
  33. tableau6 = []
  34. tableau7 = []
  35. tableau8 = []
  36. global cell
  37. cell = [(),(),(),()]
  38. for i in range(0,7):
  39. tableau1.append(d.deal())
  40. for i in range(7,14):
  41. tableau2.append(d.deal())
  42. for i in range(14,21):
  43. tableau3.append(d.deal())
  44. for i in range(21,28):
  45. tableau4.append(d.deal())
  46. for i in range(28,34):
  47. tableau5.append(d.deal())
  48. for i in range(34,40):
  49. tableau6.append(d.deal())
  50. for i in range(40,46):
  51. tableau7.append(d.deal())
  52. for i in range(46,52):
  53. tableau8.append(d.deal())
  54. tableau = [tableau1, tableau2, tableau3, tableau4, tableau5, tableau6, tableau7, tableau8]
  55. return foundation, tableau, cell
  56.  
  57.  
  58.  
  59.  
  60. def move_to_foundation(tableau, foundation, t_col, f_col):
  61.  
  62. #Get rank and get suit graphs the rank and suit of the card and this method checks the rank and suit
  63.  
  64. one = tableau[t_col][-1]
  65. two = foundation[f_col]
  66.  
  67. '''
  68. Finds the rank and suit ^^^^^^^^^
  69. '''
  70.  
  71. if foundation[f_col] == []:
  72. if one.get_rank() == 1:
  73. foundation[f_col].append(one) #adds the card
  74. del tableau[t_col][-1] #del function removes card from tableau
  75. elif one.get_suit() == two.get_suit():
  76. if one.get_rank()== two.get_rank()+1:
  77. foundation[f_col].append(one)
  78. del tableau[t_col][-1]
  79.  
  80. '''
  81. parameters: a tableau, a foundation, column of tableau, column of foundation
  82. returns: Boolean (True if the move is valid, False otherwise)
  83. moves a card at the end of a column of tableau to a column of foundation
  84. This function can also be used to move a card from cell to foundation
  85. '''
  86.  
  87.  
  88. def move_to_cell(tableau, cell, t_col, c_col):
  89.  
  90. one = tableau[t_col][-1]
  91. two = cell[c_col]
  92.  
  93. if two == []:
  94. cell[c_col].append(one) #Appends tableau column card
  95. del tableau[t_col][-1] #del function removes card from tableau
  96.  
  97. '''
  98. parameters: a tableau, a cell, column of tableau, column of cell
  99. returns: Boolean (True if the move is valid, False otherwise)
  100. moves a card at the end of a column of tableau to a cell
  101. '''
  102.  
  103.  
  104. def move_to_tableau(tableau, foundation, t_col, f_col, c_col):
  105.  
  106. #s,c,d,h all refer to card colors and the code checks if colors are allowed
  107.  
  108. one = tableau[t_col][-1]
  109.  
  110. if cell[c_col] != []: #Does not equal empty slot for a card
  111. two = cell[c_col][0]
  112.  
  113. if one == two+1:
  114. if "d" in one or "h" in one: #runs the red cards first
  115. if "s" in two or "c" in two:
  116. tableau[t_col].append(two)
  117. del cell[c_col][-1]
  118.  
  119. elif "d" in two or "h" in two: #runs the red cards first
  120. if "s" in one or "c" in one:
  121. tableau[t_col].append(two)
  122. del cell[c_col][-1]
  123.  
  124.  
  125.  
  126. '''
  127. parameters: a tableau, a cell, column of tableau, a cell
  128. returns: Boolean (True if the move is valid, False otherwise)
  129. moves a card in the cell to a column of tableau
  130. remember to check validity of move
  131. '''
  132.  
  133.  
  134. def is_winner(foundation):
  135. gametime = 0
  136. while gametime == 0:
  137. if foundation[0] != []:
  138. if foundation[1] != []:
  139. if foundation[2] != []:
  140. if foundation[3] != []:
  141. print("You won!")
  142. gametime = 1
  143.  
  144. '''
  145. parameters: a foundation
  146. return: Boolean
  147. '''
  148.  
  149.  
  150. def move_in_tableau(tableau, t_col_source, t_col_dest):
  151.  
  152. #s,c,d,h all refer to card colors and the code checks if colors are allowed
  153.  
  154. one = tableau[t_col_source][-1]
  155. two = tableau[t_col_dest][-1]
  156.  
  157. if one == two+1:
  158. if "d" in one or "h" in one: #runs the red cards first
  159. if "s" in two or "c" in two:
  160. tableau[t_col_dest].append(one)
  161.  
  162. del cell[t_col_source][-1]
  163.  
  164.  
  165. elif "d" in two or "h" in two: #runs the red cards first
  166. if "s" in one or "c" in one:
  167. tableau[t_col_dest].append(one)
  168.  
  169. del cell[t_col_source][-1]
  170.  
  171.  
  172.  
  173. '''
  174. parameters: a tableau, the source tableau column and the destination tableau column
  175. returns: Boolean
  176. move card from one tableau column to another
  177. remember to check validity of move
  178. '''
  179. pass
  180.  
  181.  
  182. def print_game(foundation, tableau, cell):
  183. """
  184. parameters: a tableau, a foundation and a cell
  185. returns: Nothing
  186. prints the game, i.e, print all the info user can see.
  187. Includes:
  188. a) print tableau
  189. b) print foundation ( can print the top card only)
  190. c) print cells
  191.  
  192. """
  193. print()
  194. print(" Cells: Foundation:")
  195. # print cell and foundation labels in one line
  196. for i in range(4):
  197. print('{:8d}'.format(i + 1), end='')
  198. print(' ', end='')
  199. for i in range(4):
  200. print('{:8d}'.format(i + 1), end='')
  201. print() # carriage return at the end of the line
  202.  
  203. # print cell and foundation cards in one line; foundation is only top card
  204. for c in cell:
  205. # print if there is a card there; if not, exception prints spaces.
  206. try:
  207. print('{:8s}'.format(c[0].rank_and_suit()), end='')
  208. except IndexError:
  209. print('{:8s}'.format(''), end='')
  210.  
  211. print(' ', end='')
  212. for stack in foundation:
  213. # print if there is a card there; if not, exception prints spaces.
  214. try:
  215. print('{:8s}'.format(stack[-1].rank_and_suit()), end='')
  216. except IndexError:
  217. print('{:8s}'.format(''), end='')
  218.  
  219. print() # carriage return at the end of the line
  220. print('----------')
  221.  
  222. print("Tableau")
  223. for i in range(len(tableau)): # print tableau headers
  224. print('{:8d}'.format(i + 1), end='')
  225. print() # carriage return at the end of the line
  226.  
  227. # Find the length of the longest stack
  228. max_length = max([len(stack) for stack in tableau])
  229.  
  230. # print tableau stacks row by row
  231. for i in range(max_length): # for each row
  232. print(' ' * 7, end='') # indent each row
  233. for stack in tableau:
  234. # print if there is a card there; if not, exception prints spaces.
  235. try:
  236. print('{:8s}'.format(stack[i].rank_and_suit()), end='')
  237. except IndexError:
  238. print('{:8s}'.format(''), end='')
  239. print() # carriage return at the end of the line
  240. print('----------')
  241.  
  242.  
  243. def print_rules():
  244. '''
  245. parameters: none
  246. returns: nothing
  247. prints the rules
  248. '''
  249. print("Rules of FreeCell")
  250.  
  251. print("Goal")
  252. print("\tMove all the cards to the Foundations")
  253.  
  254. print("Foundation")
  255. print("\tBuilt up by rank and by suit from Ace to King")
  256.  
  257. print("Tableau")
  258. print("\tBuilt down by rank and by alternating color")
  259. print("\tThe bottom card of any column may be moved")
  260. print("\tAn empty spot may be filled with any card ")
  261.  
  262. print("Cell")
  263. print("\tCan only contain 1 card")
  264. print("\tThe card may be moved")
  265.  
  266.  
  267. def show_help():
  268. '''
  269. parameters: none
  270. returns: nothing
  271. prints the supported commands
  272. '''
  273. print("Responses are: ")
  274. print("\t t2f #T #F - move from Tableau to Foundation")
  275. print("\t t2t #T1 #T2 - move card from one Tableau column to another")
  276. print("\t t2c #T #C - move from Tableau to Cell")
  277. print("\t c2t #C #T - move from Cell to Tableau")
  278. print("\t c2f #C #F - move from Cell to Foundation")
  279. print("\t 'h' for help")
  280. print("\t 'q' to quit")
  281.  
  282.  
  283. def play():
  284. '''
  285. Main program. Does error checking on the user input.
  286. '''
  287. print_rules()
  288. foundation, tableau, cell = setup()
  289.  
  290. show_help()
  291. while True:
  292.  
  293. print_game(foundation, tableau, cell)
  294. response = input("Command (type 'h' for help): ")
  295. response = response.strip()
  296. response_list = response.split()
  297. if len(response_list) > 0:
  298. r = response_list[0]
  299.  
  300. f_col = response_list[2] #Defines f_col
  301. t_col = response_list[1] #Defines t_col
  302. c_col = response_list[2] #Defines c_col
  303.  
  304. '''
  305. t_col and f_col here ^^^^^^^^^^^^^^^^
  306. '''
  307.  
  308. if r == 't2f':
  309. move_to_foundation(tableau, foundation, t_col, f_col)
  310. elif r == 't2t':
  311. move_in_tableau(tableau, t_col_source, t_col_dest)
  312. elif r == 't2c':
  313. move_to_cell(tableau, cell, t_col, c_col)
  314. elif r == 'c2t':
  315. move_to_tableau(tableau, cell, t_col, c_col)
  316. elif r == 'c2f':
  317. move_to_foundation(tableau, foundation, t_col, f_col)
  318. elif r == 'q':
  319. break #Break quits the whole game
  320. elif r == 'h':
  321. show_help() #Calls for help method
  322. else:
  323. print('Unknown command:', r)
  324. else:
  325. print("Unknown Command:", response)
  326. print('Thanks for playing')
  327.  
  328.  
  329. play()
Add Comment
Please, Sign In to add comment