Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Skeleton Program code for the AQA COMP1 Summer 2015 examination
- # this code should be used in conjunction with the Preliminary Material
- # written by the AQA COMP1 Programmer Team
- # developed in the Python 3.4 programming environment
- BOARDDIMENSION = 8
- def CreateBoard():
- Board = []
- for Count in range(BOARDDIMENSION + 1):
- Board.append([])
- for Count2 in range(BOARDDIMENSION + 1):
- Board[Count].append(" ")
- return Board
- def DisplayWhoseTurnItIs(WhoseTurn):
- if WhoseTurn == "W":
- print("It is White's turn")
- else:
- print("It is Black's turn")
- def GetTypeOfGame():
- 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
- return TypeOfGame
- def DisplayWinner(WhoseTurn):
- if WhoseTurn == "W":
- print("Black's Sarrum has been captured. White wins!")
- else:
- print("White's Sarrum has been captured. Black wins!")
- def CheckIfGameWillBeWon(Board, FinishRank, FinishFile):
- if Board[FinishRank][FinishFile][1] == "S":
- return True
- else:
- return False
- def DisplayBoard(Board):
- print()
- for RankNo in range(1, BOARDDIMENSION + 1):
- print(" _______________________")
- print(RankNo, end=" ")
- for FileNo in range(1, BOARDDIMENSION + 1):
- print("|" + Board[RankNo][FileNo], end="")
- print("|")
- print(" _______________________")
- print()
- print(" 1 2 3 4 5 6 7 8")
- print()
- print()
- def CheckRedumMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile, ColourOfPiece):
- CheckRedumMoveIsLegal = False
- if ColourOfPiece == "W":
- if FinishRank == StartRank - 1:
- if FinishFile == StartFile and Board[FinishRank][FinishFile] == " ":
- CheckRedumMoveIsLegal = True
- elif abs(FinishFile - StartFile) == 1 and Board[FinishRank][FinishFile][0] == "B":
- CheckRedumMoveIsLegal = True
- elif FinishRank == StartRank + 1:
- if FinishFile == StartFile and Board[FinishRank][FinishFile] == " ":
- CheckRedumMoveIsLegal = True
- elif abs(FinishFile - StartFile) == 1 and Board[FinishRank][FinishFile][0] == "W":
- CheckRedumMoveIsLegal = True
- return CheckRedumMoveIsLegal
- def CheckSarrumMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile):
- CheckSarrumMoveIsLegal = False
- if abs(FinishFile - StartFile) <= 1 and abs(FinishRank - StartRank) <= 1:
- CheckSarrumMoveIsLegal = True
- return CheckSarrumMoveIsLegal
- def CheckGisgigirMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile):
- GisgigirMoveIsLegal = False
- RankDifference = FinishRank - StartRank
- FileDifference = FinishFile - StartFile
- if RankDifference == 0:
- if FileDifference >= 1:
- GisgigirMoveIsLegal = True
- for Count in range(1, FileDifference):
- if Board[StartRank][StartFile + Count] != " ":
- GisgigirMoveIsLegal = False
- elif FileDifference <= -1:
- GisgigirMoveIsLegal = True
- for Count in range(-1, FileDifference, -1):
- if Board[StartRank][StartFile + Count] != " ":
- GisgigirMoveIsLegal = False
- elif FileDifference == 0:
- if RankDifference >= 1:
- GisgigirMoveIsLegal = True
- for Count in range(1, RankDifference):
- if Board[StartRank + Count][StartFile] != " ":
- GisgigirMoveIsLegal = False
- elif RankDifference <= -1:
- GisgigirMoveIsLegal = True
- for Count in range(-1, RankDifference, -1):
- if Board[StartRank + Count][StartFile] != " ":
- GisgigirMoveIsLegal = False
- return GisgigirMoveIsLegal
- def CheckNabuMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile):
- CheckNabuMoveIsLegal = False
- if abs(FinishFile - StartFile) == 1 and abs(FinishRank - StartRank) == 1:
- CheckNabuMoveIsLegal = True
- return CheckNabuMoveIsLegal
- def CheckMarzazPaniMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile):
- CheckMarzazPaniMoveIsLegal = False
- if (abs(FinishFile - StartFile) == 1 and abs(FinishRank - StartRank) == 0) or (abs(FinishFile - StartFile) == 0 and abs(FinishRank - StartRank) ==1):
- CheckMarzazPaniMoveIsLegal = True
- return CheckMarzazPaniMoveIsLegal
- def CheckEtluMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile):
- CheckEtluMoveIsLegal = False
- if (abs(FinishFile - StartFile) == 2 and abs(FinishRank - StartRank) == 0) or (abs(FinishFile - StartFile) == 0 and abs(FinishRank - StartRank) == 2):
- CheckEtluMoveIsLegal = True
- return CheckEtluMoveIsLegal
- def CheckMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile, WhoseTurn):
- MoveIsLegal = True
- if (FinishFile == StartFile) and (FinishRank == StartRank):
- MoveIsLegal = False
- else:
- PieceType = Board[StartRank][StartFile][1]
- PieceColour = Board[StartRank][StartFile][0]
- if WhoseTurn == "W":
- if PieceColour != "W":
- MoveIsLegal = False
- if Board[FinishRank][FinishFile][0] == "W":
- MoveIsLegal = False
- else:
- if PieceColour != "B":
- MoveIsLegal = False
- if Board[FinishRank][FinishFile][0] == "B":
- MoveIsLegal = False
- if MoveIsLegal == True:
- if PieceType == "R":
- MoveIsLegal = CheckRedumMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile, PieceColour)
- elif PieceType == "S":
- MoveIsLegal = CheckSarrumMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile)
- elif PieceType == "M":
- MoveIsLegal = CheckMarzazPaniMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile)
- elif PieceType == "G":
- MoveIsLegal = CheckGisgigirMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile)
- elif PieceType == "N":
- MoveIsLegal = CheckNabuMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile)
- elif PieceType == "E":
- MoveIsLegal = CheckEtluMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile)
- return MoveIsLegal
- def InitialiseBoard(Board, typegame):
- if typegame == "S": #if sample game
- for RankNo in range(1, BOARDDIMENSION + 1):
- for FileNo in range(1, BOARDDIMENSION + 1):
- Board[RankNo][FileNo] = " "
- Board[1][2] = "BG"
- Board[1][4] = "BS"
- Board[1][8] = "WG"
- Board[2][1] = "WR"
- Board[3][1] = "WS"
- Board[3][2] = "BE"
- Board[3][8] = "BE"
- Board[6][8] = "BR"
- elif typegame=="N": #if normal game
- for RankNo in range(1, BOARDDIMENSION + 1):
- for FileNo in range(1, BOARDDIMENSION + 1):
- if RankNo == 2:
- Board[RankNo][FileNo] = "BR"
- elif RankNo == 7:
- Board[RankNo][FileNo] = "WR"
- elif RankNo == 1 or RankNo == 8:
- if RankNo == 1:
- Board[RankNo][FileNo] = "B"
- if RankNo == 8:
- Board[RankNo][FileNo] = "W"
- if FileNo == 1 or FileNo == 8:
- Board[RankNo][FileNo] = Board[RankNo][FileNo] + "G"
- elif FileNo == 2 or FileNo == 7:
- Board[RankNo][FileNo] = Board[RankNo][FileNo] + "E"
- elif FileNo == 3 or FileNo == 6:
- Board[RankNo][FileNo] = Board[RankNo][FileNo] + "N"
- elif FileNo == 4:
- Board[RankNo][FileNo] = Board[RankNo][FileNo] + "M"
- elif FileNo == 5:
- Board[RankNo][FileNo] = Board[RankNo][FileNo] + "S"
- else:
- Board[RankNo][FileNo] = " "
- elif typegame=="L": #if they want to load a game
- LoadGame(Board)
- def GetMove(StartSquare, FinishSquare):
- StartSquare = int(input("Enter coordinates of square containing piece to move (file first): "))
- FinishSquare = int(input("Enter coordinates of square to move piece to (file first): "))
- return StartSquare, FinishSquare
- def MakeMove(Board, StartRank, StartFile, FinishRank, FinishFile, WhoseTurn):
- if WhoseTurn == "W" and FinishRank == 1 and Board[StartRank][StartFile][1] == "R":
- Board[FinishRank][FinishFile] = "WM"
- Board[StartRank][StartFile] = " "
- elif WhoseTurn == "B" and FinishRank == 8 and Board[StartRank][StartFile][1] == "R":
- Board[FinishRank][FinishFile] = "BM"
- Board[StartRank][StartFile] = " "
- else:
- Board[FinishRank][FinishFile] = Board[StartRank][StartFile]
- Board[StartRank][StartFile] = " "
- 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
- file="savegames.txt" #make/open a file
- f=open(file,"w") #this overwrites what ever was in the file before hand
- f.write("")
- f.close()
- for x in range(1,9):
- for y in range(1,9): #this checks every square
- if Board[x][y]!=" ": #if there is a piece on the square
- RankNo=str(x) #then we note down the rank
- FileNo=str(y) #and file ALSO YOU CAN ONLY WRITE STRINGS TO FILES NOT INTEGERS SO YOU HAVE TO CONVERT INTO STRING
- Piece=Board[x][y] #what piece we are using
- f=open(file,"a") #notice a for append
- f.write(RankNo+FileNo+Piece) #put the rank number, then file number, then the piece into one massive string
- f.write(opp) #put whose turn it will be at the very end
- f.close()
- def LoadGame(Board):
- file="savegames.txt" #open it
- f=open(file,"r") #read it
- string=f.readline() #the aim now is to seperate the massive string into each pieces and whose turn it will be
- lenstring=len(string)
- 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
- WhoseTurn=string[lenstring-1] #the last letter we will call whoseturn
- length=len(pieces) #the length of the string/4 gives the amount of pieces
- amountofpieces=int(length/4)
- piecelist=[0 for i in range(amountofpieces)] #create a list which the info for each piece will go in
- step=0 #create a stepper so we can address each element of the list
- 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.
- 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
- 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
- piecelist[step]=IndivPiece #put the info of each piece into each element of the list
- step=step+1 #increase the stepper
- for y in range(amountofpieces): #now lets put the pieces onto the board
- INFO=piecelist[y] #the info for each piece is equal to an element in Y
- RankNo=int(INFO[0]) #the rank number is the first letter of the info
- FileNo=int(INFO[1]) #file number is the second
- Piece=INFO[2:4] #and the piece number is the third and fourth letter (so 2nd up to 4th)
- Board[RankNo][FileNo]=Piece #put the pieces on the board
- return WhoseTurn #doing this will come in handy later when we want to see whose turn it is
- if __name__ == "__main__":
- Board = CreateBoard() #0th index not used
- StartSquare = 0
- FinishSquare = 0
- PlayAgain = "Y"
- load=False #game doesnt come from being loaded, its comes from a sample/normal game
- count=0 #has program been run before
- while PlayAgain == "Y":
- WhoseTurn = "W"
- GameOver = False
- typegame = GetTypeOfGame() #get type of game implemented
- if typegame=="L": #so if they choose to load a game
- load=True #then they loaded a game
- turn=LoadGame(Board) #and whose turn it is is equal to the value we returned in the function (look up, we did return WhoseTurn)
- if ord(typegame) >= 97 and ord(typegame) <= 122:
- typegame = chr(ord(typegame) - 32)
- InitialiseBoard(Board, typegame)
- while not(GameOver):
- DisplayBoard(Board)
- 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)
- WhoseTurn=turn #whose turn is the value of the variable we defined above
- 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)
- else:
- DisplayWhoseTurnItIs(WhoseTurn) #if its not the first move then just carry on as normal
- MoveIsLegal = False
- while not(MoveIsLegal):
- StartSquare, FinishSquare = GetMove(StartSquare, FinishSquare)
- StartRank = StartSquare % 10
- StartFile = StartSquare // 10
- FinishRank = FinishSquare % 10
- FinishFile = FinishSquare // 10
- MoveIsLegal = CheckMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile, WhoseTurn)
- if not(MoveIsLegal):
- print("That is not a legal move - please try again")
- GameOver = CheckIfGameWillBeWon(Board, FinishRank, FinishFile)
- MakeMove(Board, StartRank, StartFile, FinishRank, FinishFile, WhoseTurn)
- if GameOver:
- DisplayWinner(WhoseTurn)
- saveandleave=input('Do you want to save and leave:') #do you want to save and leave
- if WhoseTurn=="W":
- opp="B" #defining the opposition if its whites turn black is opp, and vice versa
- else:
- opp="W"
- if saveandleave=="Y": #if they do want to save and leave
- GameOver=True #game over is true, cancel the while loop we are packin da bags
- SaveGame(Board,opp) #put the board array and the opposition turn into the save game func
- if WhoseTurn == "W":
- WhoseTurn = "B"
- else:
- WhoseTurn = "W"
- PlayAgain = input("Do you want to play again (enter Y for Yes)? ")
- if ord(PlayAgain) >= 97 and ord(PlayAgain) <= 122:
- PlayAgain = chr(ord(PlayAgain) - 32)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement