Guest User

Untitled

a guest
May 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.18 KB | None | 0 0
  1. # 2011/12/15 - Finished the final version of the Kalah Gameboard
  2. # - Started the commencing function
  3.  
  4. # 2011/12/16 - Finished commenced function at home
  5. # - Half completed houseEmpty and numSeedsInHouse function
  6.  
  7. # 2011/12/19
  8. # - Finished houseEmpty
  9. # - FinishednumSeedsInHouse functions
  10. # - Fixed up main program
  11.  
  12. # 2011/12/20
  13. # - Planning how to do sowSeeds function
  14.  
  15. # 2011/12/21
  16. # - Trying to complete sowSeeds function
  17.  
  18. # 2011/12/28
  19. # - Completed sowSeeds function and fixed up houseEmpty function
  20.  
  21. # 2011/12/29
  22. # - Error checking done on all finished work
  23.  
  24. # 2012/1/9
  25. # - Fixed up main program again to accomadate new work
  26. # - canPlay function started
  27. # - gameLoop function created and finished
  28.  
  29. # 2012/1/10
  30. # - CanPlay function almost finished
  31. # - anotherTurn function created and almost finished
  32.  
  33. # 2012/1/11
  34. # - canPlay and anotherTurn function finished
  35. # - Error checking done for all functions to run smoothly
  36. # - gameOver function created and finished.
  37.  
  38. # 2012/1/12
  39. # - canCapture function completed. Fixing errors and error checking
  40.  
  41. #Gameboard layout which shows the user the gameboard every turn.
  42. def drawGameboard(L):
  43. print()
  44. print(" _ _ _ _ _ _ _ _ _ _ _ _ ")
  45. print("| | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |")
  46. print("| || || || || || || || |")
  47. print("| || {:2d} || {:2d} || {:2d} || {:2d} || {:2d} || {:2d} || |".format(L[12], L[11], L[10], L[9], L[8], L[7]))
  48. print("| ||_ _ _ _||_ _ _ _||_ _ _ _||_ _ _ _||_ _ _ _||_ _ _ _|| |")
  49. print("| {:2d} | 13 12 11 10 9 8 | {:2d} |" .format(L[13], L[6]))
  50. print("| | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |")
  51. print("| || || || || || || || |")
  52. print("| || {:2d} || {:2d} || {:2d} || {:2d} || {:2d} || {:2d} || |".format(L[0], L[1], L[2], L[3], L[4], L[5]))
  53. print("| ||_ _ _ _||_ _ _ _||_ _ _ _||_ _ _ _||_ _ _ _||_ _ _ _|| |")
  54. print("|_ _ _ _ _ _| 1 2 3 4 5 6 |_ _ _ _ _ _|")
  55. print(" 14 7")
  56.  
  57. #Function which gets the number of seeds in each house from the user
  58. def Commence():
  59. print("--------------------------Game Commenced--------------------")
  60. print()
  61. print("--------------------Creating Beginning Gameboard--------------------")
  62. print()
  63. #Gets number of seeds from user
  64. num = int(input(" Enter how many stones are in each house from 3-6: "))
  65. while num < 3 or num >6:
  66. print("Invalid input.")
  67. print()
  68. num = int(input("Please enter again how many stones are in each house?: "))
  69. print()
  70. print()
  71. print()
  72. print(" ", num, "stones have been added to each house")
  73. #Puts the amount of seeds entered by the user into a list
  74. gameNums = [num,num,num,num,num,num,0,num,num,num,num,num,num,0]
  75. #Returns house number
  76. return gameNums
  77.  
  78. #Function which finds the amount of seeds in the house
  79. def numSeedsInHouse(L,houseNum):
  80. #Returns amount of seeds in the house
  81. return L[houseNum]
  82.  
  83. #Function which asks user which house to empty, depending on the player
  84. def houseEmpty(player):
  85. print()
  86. #Asks user which house to empty
  87. playerShouse = input("Enter which house you want to empty player {0}: ".format(player))
  88. if player == 1:
  89. #Uses a loop until user types in the right input
  90. while playerShouse.isdigit() == False or int(playerShouse) < 0 or int(playerShouse) >6:
  91. print()
  92. print("Invalid input.")
  93. print()
  94. playerShouse = input("Please enter again which house you want to empty player 1: ")
  95. #Returns house number that player 1 wants to empty
  96. return int(playerShouse)-1
  97. if player == 2:
  98. #Uses a loop until user types in the right input
  99. while playerShouse.isdigit() == False or int(playerShouse) < 8 or int(playerShouse) >13:
  100. print()
  101. print("Invalid input.")
  102. print()
  103. playerShouse = input("Please enter again which house you want to empty player 2: ")
  104. #Returns house number that player 2 wants to empty
  105. return int(playerShouse)-1
  106.  
  107.  
  108.  
  109. #Function which sees if the player has seeds in their houses to play
  110. def canPlay(L,player):
  111. if player == 1:
  112. #Checks houses 0-6
  113. for i in range(0,6):
  114. #Checks each index from 0-6, and if one of the numbers do not equal 0, returns True
  115. if L[i] != 0:
  116. return True
  117. #Returns false if all of them are 0
  118. return False
  119. if player == 2:
  120. #Checks houses 8-13
  121. for i in range(8,14):
  122. #Checks each index from 8-13, and if one of the numbers do not equal 0, returns True
  123. if L[i] != 0:
  124. return True
  125. #Returns false if all of them are 0
  126. return False
  127.  
  128. #Function which checks
  129. def canCapture(houseNum,numSeeds,L,player):
  130. #numSeeds = the number of seeds in the chosen house
  131. numSeeds = numSeedsInHouse(L,houseNum)
  132. # temp is equal to the list index
  133. temp = L[houseNum]
  134. # Changes the list index to zero
  135. L[houseNum] = 0
  136. if player == 1:
  137. #range of seeds is from 0 to 6
  138. rangeSeeds = range(0,7)
  139. #capList is the capture list. Every 2 numbers are paired, so house 0 and 12 are across from each other, house 1 and 11 are across from each other, etc.
  140. capList = [0,12,1,11,2,10,3,9,4,8,5,7]
  141. kalahNum = 6
  142. anotherTurn = 2
  143. Pnum = 13
  144. if player == 2:
  145. # Range of seeds from 8 to 14
  146. rangeSeeds = range(8,14)
  147. # Caplist for player 2
  148. capList = [7,5,8,4,9,3,10,2,11,1,12,0]
  149. #Kalah number
  150. kalahNum = 13
  151. anotherTurn = 1
  152. Pnum = 6
  153. #
  154. for y in range(numSeeds):
  155. if houseNum+numSeeds > 13:
  156. houseNum = -1 - (14-houseNum-1)
  157.  
  158. if L[houseNum+numSeeds] == 0 and houseNum+numSeeds+1 in rangeSeeds:
  159. print ("Captured!")
  160. for x in range(1,numSeeds):
  161. if x+houseNum > 13:
  162. housenum = -x
  163. L[houseNum+x] += 1
  164. L[kalahNum] += 1 + L[capList[capList.index(houseNum+numSeeds)+1]]
  165. L[houseNum] = 0
  166. L[capList[capList.index(houseNum+numSeeds)+1]] = 0
  167. print("You get another turn!")
  168. gameLoop(L,anotherTurn)
  169. else:
  170. for iterateHouse in range(1,temp+1):
  171. if houseNum+iterateHouse == Pnum:
  172. L[houseNum+iterateHouse] = L[houseNum+iterateHouse]
  173. else:
  174. L[houseNum+iterateHouse] += 1
  175. return True
  176. return False
  177.  
  178. def sowSeeds():
  179. while player == 1:
  180. temp = L[w]
  181. L[w] = 0
  182. numberofseeds = 0
  183. while temp!=0:
  184. L[w+temp] += 1
  185. temp-=1
  186. numberofseeds +=1
  187. return numberofseeds
  188. while player == 2:
  189. temp = L[w]
  190. L[w] = 0
  191. numberofseeds = 0
  192. while temp!=0:
  193. L[w+temp] += 1
  194. temp-=1
  195. numberofseeds +=1
  196. return numberofseeds
  197.  
  198. #Function which checks if player 1 or 2 can get another turn
  199. def anotherTurn(numberofseeds,houseNum,player,L):
  200. if player == 1:
  201. if numberofseeds + houseNum+1 == 7:
  202. print("You get another turn!")
  203. #Reruns gameLoop to get another turn
  204. gameLoop(L,2)
  205. if player == 2:
  206. if numberofseeds + houseNum+1 == 14:
  207. print("You get another turn!")
  208. #Reruns gameLoop to get another turn
  209. gameLoop(L,1)
  210.  
  211. #Function which checks if the game is over
  212. def gameOver(L):
  213. check=0
  214. for x in L[:6]:
  215. check+=x
  216. if check==0:
  217. print ("Game over. Player 1 has 0 seeds")
  218. for x in range(7,13):
  219. L[13]+=L[x]
  220. L[x]=0
  221.  
  222. print ("Player two gets remaining seeds. \nPlayer two now has {0} seeds in Kalah".format(L[13]))
  223. print ("Player one has {0} seeds in Kalah".format(L[6]))
  224. if L[6] > L[13]:
  225. print ("Player one wins game!")
  226. elif L[13] > L[6]:
  227. print ("Player two wins game!")
  228. else:
  229. print ("Draw game.")
  230. drawGameboard(L)
  231.  
  232.  
  233. check=0
  234. for x in L[7:13]:
  235. check+=x
  236. if check == 0:
  237. print ("Game over. Player 2 has 0 seeds")
  238. for x in range(0,6):
  239. L[6]+=L[x]
  240. L[x]=0
  241.  
  242. print ("Player one gets remaining seeds. \nPlayer one now has {0} seeds in Kalah".format(L[6]))
  243. print ("Player two has {0} seeds in Kalah".format(L[13]))
  244. if L[6] > L[13]:
  245. print ("Player one wins game!")
  246. elif L[13] > L[6]:
  247. print ("Player two wins game!")
  248. else:
  249. print ("Draw game.")
  250. drawGameboard(L)
  251. print ("Continue")
  252. return False
  253.  
  254. #Function which loops the game with the following functions to switch turns between players
  255. def gameLoop(L,player):
  256. while gameOver(L)!= True:
  257. if player == 1:
  258. player = 2
  259. else:
  260. player = 1
  261. drawGameboard(L)
  262. houseNum = houseEmpty(player)
  263. numseeds = numSeedsInHouse(L,houseNum)
  264. checkPlaying = canPlay(L,player)
  265. canCapture(houseNum,numseeds,L,player)
  266. anotherTurn(numseeds,houseNum,player,L)
  267.  
  268.  
  269.  
  270. def main():
  271. player = 2
  272. L = Commence()
  273. print(" ", "Kalah GameBoard has been created!")
  274. gameLoop(L,player)
  275. # Main Program
  276. main()
Add Comment
Please, Sign In to add comment