Advertisement
Guest User

Untitled

a guest
May 29th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.84 KB | None | 0 0
  1. # Skeleton Program code for the AQA COMP1 Summer 2015 examination
  2. # this code should be used in conjunction with the Preliminary Material
  3. # written by the AQA COMP1 Programmer Team
  4. # developed in the Python 3.4 programming environment
  5.  
  6. BOARDDIMENSION = 8
  7.  
  8. def CreateBoard():
  9. Board = []
  10. for Count in range(BOARDDIMENSION + 1):
  11. Board.append([])
  12. for Count2 in range(BOARDDIMENSION + 1):
  13. Board[Count].append(" ")
  14. return Board
  15.  
  16. def DisplayWhoseTurnItIs(WhoseTurn):
  17. if WhoseTurn == "W":
  18. print("It is White's turn")
  19. else:
  20. print("It is Black's turn")
  21.  
  22. def GetTypeOfGame():
  23. TypeOfGame = input("What type of game do you want to play (S,N,L)") #we get to use this function. sample game/normal/load a game
  24. return TypeOfGame
  25.  
  26. def DisplayWinner(WhoseTurn):
  27. if WhoseTurn == "W":
  28. print("Black's Sarrum has been captured. White wins!")
  29. else:
  30. print("White's Sarrum has been captured. Black wins!")
  31.  
  32. def CheckIfGameWillBeWon(Board, FinishRank, FinishFile):
  33. if Board[FinishRank][FinishFile][1] == "S":
  34. return True
  35. else:
  36. return False
  37.  
  38. def DisplayBoard(Board):
  39. print()
  40. for RankNo in range(1, BOARDDIMENSION + 1):
  41. print(" _______________________")
  42. print(RankNo, end=" ")
  43. for FileNo in range(1, BOARDDIMENSION + 1):
  44. print("|" + Board[RankNo][FileNo], end="")
  45. print("|")
  46. print(" _______________________")
  47. print()
  48. print(" 1 2 3 4 5 6 7 8")
  49. print()
  50. print()
  51.  
  52. def CheckRedumMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile, ColourOfPiece):
  53. CheckRedumMoveIsLegal = False
  54. if ColourOfPiece == "W":
  55. if FinishRank == StartRank - 1:
  56. if FinishFile == StartFile and Board[FinishRank][FinishFile] == " ":
  57. CheckRedumMoveIsLegal = True
  58. elif abs(FinishFile - StartFile) == 1 and Board[FinishRank][FinishFile][0] == "B":
  59. CheckRedumMoveIsLegal = True
  60. elif FinishRank == StartRank + 1:
  61. if FinishFile == StartFile and Board[FinishRank][FinishFile] == " ":
  62. CheckRedumMoveIsLegal = True
  63. elif abs(FinishFile - StartFile) == 1 and Board[FinishRank][FinishFile][0] == "W":
  64. CheckRedumMoveIsLegal = True
  65. return CheckRedumMoveIsLegal
  66.  
  67. def CheckSarrumMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile):
  68. CheckSarrumMoveIsLegal = False
  69. if abs(FinishFile - StartFile) <= 1 and abs(FinishRank - StartRank) <= 1:
  70. CheckSarrumMoveIsLegal = True
  71. return CheckSarrumMoveIsLegal
  72.  
  73. def CheckGisgigirMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile):
  74. GisgigirMoveIsLegal = False
  75. RankDifference = FinishRank - StartRank
  76. FileDifference = FinishFile - StartFile
  77. if RankDifference == 0:
  78. if FileDifference >= 1:
  79. GisgigirMoveIsLegal = True
  80. for Count in range(1, FileDifference):
  81. if Board[StartRank][StartFile + Count] != " ":
  82. GisgigirMoveIsLegal = False
  83. elif FileDifference <= -1:
  84. GisgigirMoveIsLegal = True
  85. for Count in range(-1, FileDifference, -1):
  86. if Board[StartRank][StartFile + Count] != " ":
  87. GisgigirMoveIsLegal = False
  88. elif FileDifference == 0:
  89. if RankDifference >= 1:
  90. GisgigirMoveIsLegal = True
  91. for Count in range(1, RankDifference):
  92. if Board[StartRank + Count][StartFile] != " ":
  93. GisgigirMoveIsLegal = False
  94. elif RankDifference <= -1:
  95. GisgigirMoveIsLegal = True
  96. for Count in range(-1, RankDifference, -1):
  97. if Board[StartRank + Count][StartFile] != " ":
  98. GisgigirMoveIsLegal = False
  99. return GisgigirMoveIsLegal
  100.  
  101. def CheckNabuMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile):
  102. CheckNabuMoveIsLegal = False
  103. if abs(FinishFile - StartFile) == 1 and abs(FinishRank - StartRank) == 1:
  104. CheckNabuMoveIsLegal = True
  105. return CheckNabuMoveIsLegal
  106.  
  107. def CheckMarzazPaniMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile):
  108. CheckMarzazPaniMoveIsLegal = False
  109. if (abs(FinishFile - StartFile) == 1 and abs(FinishRank - StartRank) == 0) or (abs(FinishFile - StartFile) == 0 and abs(FinishRank - StartRank) ==1):
  110. CheckMarzazPaniMoveIsLegal = True
  111. return CheckMarzazPaniMoveIsLegal
  112.  
  113. def CheckEtluMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile):
  114. CheckEtluMoveIsLegal = False
  115. if (abs(FinishFile - StartFile) == 2 and abs(FinishRank - StartRank) == 0) or (abs(FinishFile - StartFile) == 0 and abs(FinishRank - StartRank) == 2):
  116. CheckEtluMoveIsLegal = True
  117. return CheckEtluMoveIsLegal
  118.  
  119. def CheckMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile, WhoseTurn):
  120. MoveIsLegal = True
  121. if (FinishFile == StartFile) and (FinishRank == StartRank):
  122. MoveIsLegal = False
  123. else:
  124. PieceType = Board[StartRank][StartFile][1]
  125. PieceColour = Board[StartRank][StartFile][0]
  126. if WhoseTurn == "W":
  127. if PieceColour != "W":
  128. MoveIsLegal = False
  129. if Board[FinishRank][FinishFile][0] == "W":
  130. MoveIsLegal = False
  131. else:
  132. if PieceColour != "B":
  133. MoveIsLegal = False
  134. if Board[FinishRank][FinishFile][0] == "B":
  135. MoveIsLegal = False
  136. if MoveIsLegal == True:
  137. if PieceType == "R":
  138. MoveIsLegal = CheckRedumMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile, PieceColour)
  139. elif PieceType == "S":
  140. MoveIsLegal = CheckSarrumMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile)
  141. elif PieceType == "M":
  142. MoveIsLegal = CheckMarzazPaniMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile)
  143. elif PieceType == "G":
  144. MoveIsLegal = CheckGisgigirMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile)
  145. elif PieceType == "N":
  146. MoveIsLegal = CheckNabuMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile)
  147. elif PieceType == "E":
  148. MoveIsLegal = CheckEtluMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile)
  149. return MoveIsLegal
  150.  
  151. def InitialiseBoard(Board, typegame):
  152. if typegame == "S": #if sample game
  153. for RankNo in range(1, BOARDDIMENSION + 1):
  154. for FileNo in range(1, BOARDDIMENSION + 1):
  155. Board[RankNo][FileNo] = " "
  156. Board[1][2] = "BG"
  157. Board[1][4] = "BS"
  158. Board[1][8] = "WG"
  159. Board[2][1] = "WR"
  160. Board[3][1] = "WS"
  161. Board[3][2] = "BE"
  162. Board[3][8] = "BE"
  163. Board[6][8] = "BR"
  164. elif typegame=="N": #if normal game
  165. for RankNo in range(1, BOARDDIMENSION + 1):
  166. for FileNo in range(1, BOARDDIMENSION + 1):
  167. if RankNo == 2:
  168. Board[RankNo][FileNo] = "BR"
  169. elif RankNo == 7:
  170. Board[RankNo][FileNo] = "WR"
  171. elif RankNo == 1 or RankNo == 8:
  172. if RankNo == 1:
  173. Board[RankNo][FileNo] = "B"
  174. if RankNo == 8:
  175. Board[RankNo][FileNo] = "W"
  176. if FileNo == 1 or FileNo == 8:
  177. Board[RankNo][FileNo] = Board[RankNo][FileNo] + "G"
  178. elif FileNo == 2 or FileNo == 7:
  179. Board[RankNo][FileNo] = Board[RankNo][FileNo] + "E"
  180. elif FileNo == 3 or FileNo == 6:
  181. Board[RankNo][FileNo] = Board[RankNo][FileNo] + "N"
  182. elif FileNo == 4:
  183. Board[RankNo][FileNo] = Board[RankNo][FileNo] + "M"
  184. elif FileNo == 5:
  185. Board[RankNo][FileNo] = Board[RankNo][FileNo] + "S"
  186. else:
  187. Board[RankNo][FileNo] = " "
  188. elif typegame=="L": #if they want to load a game
  189. LoadGame(Board)
  190.  
  191. def GetMove(StartSquare, FinishSquare):
  192. StartSquare = int(input("Enter coordinates of square containing piece to move (file first): "))
  193. FinishSquare = int(input("Enter coordinates of square to move piece to (file first): "))
  194. return StartSquare, FinishSquare
  195.  
  196. def MakeMove(Board, StartRank, StartFile, FinishRank, FinishFile, WhoseTurn):
  197. if WhoseTurn == "W" and FinishRank == 1 and Board[StartRank][StartFile][1] == "R":
  198. Board[FinishRank][FinishFile] = "WM"
  199. Board[StartRank][StartFile] = " "
  200. elif WhoseTurn == "B" and FinishRank == 8 and Board[StartRank][StartFile][1] == "R":
  201. Board[FinishRank][FinishFile] = "BM"
  202. Board[StartRank][StartFile] = " "
  203. else:
  204. Board[FinishRank][FinishFile] = Board[StartRank][StartFile]
  205. Board[StartRank][StartFile] = " "
  206. def SaveGame(Board,opp): #in this file we want to save the board and also whose turn it will be. if white just moved then it will be blacks turn so we arent loading whose turn but we are loading the opposite colours turn
  207. file="savegames.txt" #make/open a file
  208. f=open(file,"w") #this overwrites what ever was in the file before hand
  209. f.write("")
  210. f.close()
  211. for x in range(1,9):
  212. for y in range(1,9): #this checks every square
  213. if Board[x][y]!=" ": #if there is a piece on the square
  214. RankNo=str(x) #then we note down the rank
  215. FileNo=str(y) #and file ALSO YOU CAN ONLY WRITE STRINGS TO FILES NOT INTEGERS SO YOU HAVE TO CONVERT INTO STRING
  216. Piece=Board[x][y] #what piece we are using
  217. f=open(file,"a") #notice a for append
  218. f.write(RankNo+FileNo+Piece) #put the rank number, then file number, then the piece into one massive string
  219. f.write(opp) #put whose turn it will be at the very end
  220. f.close()
  221.  
  222.  
  223. def LoadGame(Board):
  224. file="savegames.txt" #open it
  225. f=open(file,"r") #read it
  226. string=f.readline() #the aim now is to seperate the massive string into each pieces and whose turn it will be
  227. lenstring=len(string)
  228. pieces=string[:lenstring-1] #first, lets get rid of whose turn it will be and just concentrate on pieces. so, get everything into a different string bar from the last letter
  229. WhoseTurn=string[lenstring-1] #the last letter we will call whoseturn
  230. length=len(pieces) #the length of the string/4 gives the amount of pieces
  231. amountofpieces=int(length/4)
  232. piecelist=[0 for i in range(amountofpieces)] #create a list which the info for each piece will go in
  233. step=0 #create a stepper so we can address each element of the list
  234. for x in range(0,length,4): #in order to get the massive string into the info of each piece, we need to seperate the string into 4 lettered bits. so, if we increment a stepper by 4 we get 0,4,8,12.
  235. i=x+4 #however, in order to do this we want the letter from 0 to 4, then the letter from 4 to 8, then the letters from 8 to 12. so, we make a new variable but add 4
  236. IndivPiece=pieces[x:i] #the info for each piece is equal to the string from 0-4, then 4-8, then 8-12, then 12-16 and so on until the length of the string is reached
  237. piecelist[step]=IndivPiece #put the info of each piece into each element of the list
  238. step=step+1 #increase the stepper
  239. for y in range(amountofpieces): #now lets put the pieces onto the board
  240. INFO=piecelist[y] #the info for each piece is equal to an element in Y
  241. RankNo=int(INFO[0]) #the rank number is the first letter of the info
  242. FileNo=int(INFO[1]) #file number is the second
  243. Piece=INFO[2:4] #and the piece number is the third and fourth letter (so 2nd up to 4th)
  244. Board[RankNo][FileNo]=Piece #put the pieces on the board
  245. return WhoseTurn #doing this will come in handy later when we want to see whose turn it is
  246.  
  247.  
  248.  
  249. if __name__ == "__main__":
  250. Board = CreateBoard() #0th index not used
  251. StartSquare = 0
  252. FinishSquare = 0
  253. PlayAgain = "Y"
  254. load=False #game doesnt come from being loaded, its comes from a sample/normal game
  255. count=0 #has program been run before
  256. while PlayAgain == "Y":
  257. WhoseTurn = "W"
  258. GameOver = False
  259. typegame = GetTypeOfGame() #get type of game implemented
  260. if typegame=="L": #so if they choose to load a game
  261. load=True #then they loaded a game
  262. turn=LoadGame(Board) #and whose turn it is is equal to the value we returned in the function (look up, we did return WhoseTurn)
  263. if ord(typegame) >= 97 and ord(typegame) <= 122:
  264. typegame = chr(ord(typegame) - 32)
  265. InitialiseBoard(Board, typegame)
  266. while not(GameOver):
  267. DisplayBoard(Board)
  268. if count==0 and load==True: #so if the program hasn't been run before (count=0) and they loaded a game (look up we defined this as well)
  269. WhoseTurn=turn #whose turn is the value of the variable we defined above
  270. count=count+1 #this indicates that we no longer need to define whose turn it is from the load game (ie second or more move)
  271. else:
  272. DisplayWhoseTurnItIs(WhoseTurn) #if its not the first move then just carry on as normal
  273. MoveIsLegal = False
  274. while not(MoveIsLegal):
  275. StartSquare, FinishSquare = GetMove(StartSquare, FinishSquare)
  276. StartRank = StartSquare % 10
  277. StartFile = StartSquare // 10
  278. FinishRank = FinishSquare % 10
  279. FinishFile = FinishSquare // 10
  280. MoveIsLegal = CheckMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile, WhoseTurn)
  281. if not(MoveIsLegal):
  282. print("That is not a legal move - please try again")
  283. GameOver = CheckIfGameWillBeWon(Board, FinishRank, FinishFile)
  284. MakeMove(Board, StartRank, StartFile, FinishRank, FinishFile, WhoseTurn)
  285. if GameOver:
  286. DisplayWinner(WhoseTurn)
  287. saveandleave=input('Do you want to save and leave:') #do you want to save and leave
  288. if WhoseTurn=="W":
  289. opp="B" #defining the opposition if its whites turn black is opp, and vice versa
  290. else:
  291. opp="W"
  292. if saveandleave=="Y": #if they do want to save and leave
  293. GameOver=True #game over is true, cancel the while loop we are packin da bags
  294. SaveGame(Board,opp) #put the board array and the opposition turn into the save game func
  295. if WhoseTurn == "W":
  296. WhoseTurn = "B"
  297. else:
  298. WhoseTurn = "W"
  299.  
  300.  
  301. PlayAgain = input("Do you want to play again (enter Y for Yes)? ")
  302. if ord(PlayAgain) >= 97 and ord(PlayAgain) <= 122:
  303. PlayAgain = chr(ord(PlayAgain) - 32)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement