def run(state, move_num, isPlayerMove): #Allow the computer to make it's move if not isPlayerMove: state = computerTurn(state, move_num) move_num += 1 #Update the scoreboard black_score = VictoryStatus.countPieces(Constants.PIECE_BLACK, state) white_score = VictoryStatus.countPieces(Constants.PIECE_WHITE, state) ScreenWriter.writeScore(black_score, white_score) #Display valid moves valid_moves = SquareValidator.mainSquareValidator( Converter.toString(state), ListInterpret.whoseTurn(move_num)) TurtleMove.displayValidMoves(valid_moves) #Save variables to be used later FileHandler.saveVariable("State", Converter.toString(state)) FileHandler.saveVariable("Move", str(move_num)) FileHandler.saveVariable("Moving", "False") #Set up the window wn = Constants.WINDOW wn.onclick(placePiece) wn.onkey(saveGame, "s") wn.onkey(loadGame, "l") wn.onkey(quitGame, "space") wn.listen() wn.mainloop()
def loadGame(): #Load game info saved_state = FileHandler.loadVariable("State", "save.txt") saved_move_num = FileHandler.loadVariable("Move", "save.txt") #Write loaded variables to variables file FileHandler.saveVariable("State", saved_state) FileHandler.saveVariable("Move", saved_move_num) #Convert state from string to list saved_state = Converter.toList(saved_state) #Load board configuration ListInterpret.stringToPiece(saved_state) #Update the scoreboard black_score = VictoryStatus.countPieces(Constants.PIECE_BLACK, saved_state) white_score = VictoryStatus.countPieces(Constants.PIECE_WHITE, saved_state) ScreenWriter.writeScore(black_score, white_score) #Reset valid moves from TurtleMove TurtleMove.SHOWN_MOVES = [] #Display valid moves valid_moves = SquareValidator.mainSquareValidator( Converter.toString(saved_state), ListInterpret.whoseTurn(int(saved_move_num))) TurtleMove.displayValidMoves(valid_moves)
def stringToPiece(game_state): index = 0 for y in range(len(Constants.ROW_NUMBERS)): for x in range(len(Constants.COLUMN_LETTERS)): #Get location x_coord = Constants.COLUMN_LETTERS[x] y_coord = Constants.ROW_NUMBERS[y] #Check each piece at the index in the game state piece = game_state[index] if piece == "B": TurtleMove.placePiece(x_coord, y_coord, "Black") elif piece == "W": TurtleMove.placePiece(x_coord, y_coord, "White") #Increment index index += 1
def stringInterpret(game_state, NewMove, turn): column = NewMove[0].upper() row = int(NewMove[1]) column_IDX = (Constants.COLUMN_LETTERS.index(column)) row_IDX = (Constants.ROW_NUMBERS.index(row)) #Equation for converting a move coordinate to the index number to be changed # moveIDX = ((row_IDX * Constants.NUM_OF_ROWS) + column_IDX) moveIDX = moveToIndex(NewMove) turn_colour = whoseTurn(turn) new_state = game_state[:moveIDX] + turn_colour + game_state[moveIDX + 1:] # new_state = PieceChange.ChangePieces(game_state, NewMove, whoseTurn(turn)) if whoseTurn(turn) == "W": color = "White" else: color = "Black" TurtleMove.placePiece(column, row, color) # stringToPiece(new_state) if __name__ == "__main__": #TESTING TESTING TESTING ########################## print("Testing") print("turn ", turn) print(column_IDX, "column_IDX") # Column index number from letters print(row_IDX, "row_IDX") #Row index number from numbers print(column, "Column") #Prints the Column Letter print(row, "Row") #Prints the Row Number # print(game_state[moveIDX], ": Character at the move's index number.") #Prints the character at the index [moveIDX] print(moveIDX, "Index number of new move") #Takes the move and converts it to the index number for the string print(new_state) #Prints the new_state with the character at index.moveIDX in place print() ########################## return new_state #returns the new_state, The updated move.
def StringInterpret(token, NewMove, turn): column = NewMove[0].upper() row = int(NewMove[1]) ColumnIDX = (Constants.COLUMN_LETTERS.index(column)) RowIDX = (Constants.ROW_NUMBERS.index(row)) MoveToString = ( (RowIDX * Constants.NUM_OF_ROWS) + ColumnIDX ) #Equation for converting a move coordinate to the index number to be changed TurnColour = WhoseTurn(turn) NewToken = token[:MoveToString] + TurnColour + token[MoveToString + 1:] #StringToPiece(NewToken, 0) if WhoseTurn(turn) == "B": color = "White" else: color = "Black" TurtleMove.place_piece(column, row, color) if __name__ == "__main__": #TESTING TESTING TESTING ########################## print("Testing") print("turn ", turn) print(ColumnIDX, "Columnidx") # Column index number from letters print(RowIDX, "Rowidx") #Row index number from numbers print(column, "Column") #Prints the Column Letter print(row, "Row") #Prints the Row Number print(token[MoveToString], ": Character at the move's index number." ) #Prints the character at the index [MoveToString] print( MoveToString, "Index number of new move" ) #Takes the move and converts it to the index number for the string print( NewToken ) #Prints the NewToken with the character at index.MoveToString in place print() ########################## return NewToken #returns the NewToken, The updated move.
def stringInterpret(game_state, NewMove, turn): column = NewMove[0].upper() row = int(NewMove[1]) column_IDX = (Constants.COLUMN_LETTERS.index(column)) row_IDX = (Constants.ROW_NUMBERS.index(row)) turn_colour = whoseTurn(turn) new_state = game_state[:] new_state = ListUpdater.updateGameState(new_state, NewMove, turn_colour) if whoseTurn(turn) == Constants.PIECE_WHITE: color = "White" else: color = "Black" TurtleMove.placePiece(column, row, color) if __name__ == "__main__": #TESTING TESTING TESTING ########################## print("Testing") print("turn ", turn) print(column_IDX, "column_IDX") # Column index number from letters print(row_IDX, "row_IDX") #Row index number from numbers print(column, "Column") #Prints the Column Letter print(row, "Row") #Prints the Row Number print(game_state[move_to_string], ": Character at the move's index number." ) #Prints the character at the index [move_to_string] print( move_to_string, "Index number of new move" ) #Takes the move and converts it to the index number for the string print( new_state ) #Prints the new_state with the character at index.move_to_string in place print() ########################## return new_state #Return the updated game state
def updateGameState(game_state, move, turn_letter): #Get the x and y coordinates in the move, #Subtract 1 so they can be used as list indexes xy = convertMove(move) x = xy[0] - 1 y = xy[1] - 1 temp_game_state = game_state[:] #Update the column temp_game_state = updateColumn(game_state, x, y, turn_letter) temp_row = updateRow(temp_game_state[y], x, turn_letter) temp_game_state[y] = temp_row #Update the row #Because updateRow does not draw the pieces on the board, unlike the other update functions, we must draw them here counter = 0 for i in temp_row: conv_move = convertMoveType(counter, y + 1) Tx = conv_move[0] Ty = conv_move[1] if i != 'N': if i == "B": temp_piece = "Black" else: temp_piece = "White" TurtleMove.placePiece(Tx, Ty, temp_piece) counter = counter + 1 #Update the diagnals in both directions temp_game_state = updateDiagnalDU2(temp_game_state, x, y, turn_letter) temp_game_state = update_diagnalUD2(game_state, x, y, turn_letter) #Ensure that the move is updated in the token final_row = temp_game_state[y] final_row[x] = turn_letter temp_game_state[y] = final_row return temp_game_state
def updateColumn(game_state, x, y, turn_letter): #Create a temporary row of all of ther vertical letters for easy updating temp_row = [] for row in game_state: temp_row.append(row[x]) #Update the row temp_row = updateRow(temp_row, y, turn_letter) #Using the updated row, change the game_state new_game_state = game_state[:] loop_count = 0 #loop_count is what tracks where we are in the updated row, and acts as an index for row in new_game_state: if loop_count < len(temp_row): #Update the piece in the location in the game_state temp_piece = temp_row[loop_count] row[x] = temp_piece #We now need to update the board, so we need a coordinate in the A6 format conv_move = convertMoveType(x, loop_count) Tx = conv_move[0] Ty = conv_move[1] + 1 #Add 1 in order to start counting at 1 #Get the colour of the piece if temp_piece != 'N': if temp_piece == "B": temp_piece = "Black" else: temp_piece = "White" #Place the piece on the board in the appropriate spot TurtleMove.placePiece(Tx, Ty, temp_piece) loop_count = loop_count + 1 return new_game_state
def main(): #Declare the variable that is used to end the loop loopend = False #This loop will always loop back to the main menu, prompting the user for a choice until they press play, or exit. while loopend == False: #We declare inRules to be False, which will prevent the ""invalidChoice"" functioning from running when the user presses 'any key' in the rules. inRules = False #We get the user's choice UserChoice = MainMenu() if UserChoice == '1': #User wants to play! #Here we set up the board and prompt them for a move TurtleMove.setup() TurtleMove.prompt_move() #So we must end the loop loopend = True else: #Check if the user wannts to see the rules or quit if UserChoice == '2': show_rules() #update inRules to prevent the invalidChoice function from running in line inRules = True if UserChoice == '3': #The user sucks and wants to quit exit() #Here we check if the user made a valid choice and dsiplay the appropriate text if they did not. if UserChoice is not '1' or '2' or '3': #This if statement will prevent InvalidChoice from running when the user is viewing the rules if inRules == False: if loopend == False: InvalidChoice()
def validateMoveLocation(game_state, location): xy = TurtleMove.getColumnAndRow(location) x = convertColumnNumber( xy[0] ) #NOTE!!: This will be assigned 'invalid_range' if it is outside of range. This is done by the convert function y = xy[1] #Check if the location is on the board, if x == 'invalid_range': return False if 0 <= y > 8: return False #print("Move:", y, x) #Checks if the location is occupied already #return getChar(game_state, x + (8*(y -1))) == Constants.PIECE_NONE return game_state[y - 1][x - 1] == Constants.PIECE_NONE
def stringToPiece(token, i): for y in range(len(Constants.ROW_NUMBERS)): for x in range(len(Constants.COLUMN_LETTERS)): x_coord = Constants.COLUMN_LETTERS[x] y_coord = Constants.ROW_NUMBERS[y] if token[ i] == "N": #if index i on the string equals N place a green piece which is a non-piece. TurtleMove.placePiece(x_coord, y_coord, "Green") elif token[i] == "B": TurtleMove.placePiece(x_coord, y_coord, "Black") elif token[i] == "W": TurtleMove.placePiece(x_coord, y_coord, "White") i = i + 1
def stringToPiece(game_state): for y in range(len(game_state)): for x in range(len(game_state[y])): column = Constants.COLUMN_LETTERS[y] row = Constants.ROW_NUMBERS[x] piece = game_state[x][y] if piece == Constants.PIECE_BLACK: TurtleMove.placePiece(column, row, "Black") elif piece == Constants.PIECE_WHITE: TurtleMove.placePiece(column, row, "White") else: TurtleMove.resetSquare(column, row)
def validate_move_location(P_gridString, P_location): L_xy = TurtleMove.get_column_and_row(P_location) L_x = convert_column_number( L_xy[0] ) #NOTE!!: This will be assigned 'invalid_range' if it is outside of range. This is done by the convert function L_y = L_xy[1] #Check if the location is on the board, if L_x == 'invalid_range': return False if 0 <= L_y > 8: return False #Checks if the location is occupied already if 'N' != get_Char(P_gridString, L_x + (8 * (L_y - 1))): #All of that math simply takes the number of x + a certain number of rows return False else: #If the location is on the board return True. return True
def updateStringState(token, location, colour): #Get column and row from location xy = TurtleMove.get_column_and_row(location) #Extract column and row from above x = convertColumnNumber( xy[0] ) #NOTE!!: This will be assigned 'invalid_range' if it is outside of range. This is done by the convert function y = xy[1] - 1 #This needs to start at 0 in order to match the for loop #Convert column and row to an index update_location = (8 * y + x) letter_tracker = 0 new_token = '' #Iterate through gamesate and accumulate an updated board for letter in token: letter_tracker = letter_tracker + 1 if letter_tracker == update_location: new_token = new_token + colour else: new_token = new_token + letter return new_token
def update_string_state(P_gridString, P_location, colour): #Get column and row from location L_xy = TurtleMove.get_column_and_row(P_location) #Extract column and row from above L_x = convert_column_number( L_xy[0] ) #NOTE!!: This will be assigned 'invalid_range' if it is outside of range. This is done by the convert function L_y = L_xy[1] - 1 #This needs to start at 0 in order to match the for loop #Convert column and row to an index L_updateLocation = (8 * L_y + L_x) F_LetterTracker = 0 L_updatedString = '' #Iterate through gamesate and accumulate an updated board for letter in P_gridString: F_LetterTracker = F_LetterTracker + 1 if F_LetterTracker == L_updateLocation: L_updatedString = L_updatedString + colour else: L_updatedString = L_updatedString + letter return L_updatedString
def validateMoveLocation(token, location): xy = TurtleMove.getColumnAndRow(location) x = convertColumnNumber( xy[0] ) #NOTE!!: This will be assigned 'invalid_range' if it is outside of range. This is done by the convert function y = xy[1] #Check if the location is on the board, if x == 'invalid_range': return False if 0 <= y > 8: return False #Checks if the location is occupied already if 'N' != getChar(token, x + (8 * (y - 1))): #All of that math simply takes the number of x + a certain number of rows return False else: #If the location is on the board return True. return True
def placePiece(x, y): #Make sure a move is not being made if FileHandler.loadVariable("Moving") == "False": #Save variable FileHandler.saveVariable("Moving", "True") #Load the variables we need game_state = Converter.toList(FileHandler.loadVariable("State")) move_num = int(FileHandler.loadVariable("Move")) #Make sure the game is not over if VictoryStatus.endGameStatus(game_state) != True: #Check if the point is valid if isValidSquare(x, y): #Convert the x and y to the coordinate of a cell x = convertXToBoard(x) y = convertYToBoard(y) #Convert x and y to column and row letter = Constants.COLUMN_LETTERS[x] number = Constants.ROW_NUMBERS[Constants.NUM_OF_ROWS - (y + 1)] #The move being made move = letter + str(number) #Make sure a piece is not in this location and the move is valid if StringMove.validateMoveLocation( game_state, move) and MoveValidator.isValidMove( move, Converter.toString(game_state) ) and move in SquareValidator.mainSquareValidator( Converter.toString(game_state), ListInterpret.whoseTurn(move_num)): #Clear space at move TurtleMove.resetSquare(letter, number) #Update the game state game_state = ListInterpret.stringInterpret( game_state, letter + str(number), move_num) move_num += 1 #Allow the computer to place a piece if the game is not over if VictoryStatus.endGameStatus(game_state) != True: game_state = computerTurn(game_state, move_num) move_num += 1 #Save the variables FileHandler.saveVariable("State", Converter.toString(game_state)) FileHandler.saveVariable("Move", str(move_num)) #Update the scoreboard black_score = VictoryStatus.countPieces( Constants.PIECE_BLACK, game_state) white_score = VictoryStatus.countPieces( Constants.PIECE_WHITE, game_state) ScreenWriter.writeScore(black_score, white_score) #Display valid moves valid_moves = SquareValidator.mainSquareValidator( Converter.toString(game_state), ListInterpret.whoseTurn(move_num)) TurtleMove.displayValidMoves(valid_moves) else: #Print who won game_status = PlayerVictory.playerWon(game_state) ScreenWriter.writeMessage(game_status) #Wait for user Constants.WINDOW.exitonclick() #Reset state and move FileHandler.saveVariable("State", "") FileHandler.saveVariable("Move", "") #Save variable FileHandler.saveVariable("Moving", "False")
def CheckEast(game_state, stringID, turn_colour, isTesting=False): NofChanges = 0 #Counts the number of pieces to change. MoveID = 0 #This will make a copy of the index of the players move. NofPieces = 0 other_colour = OtherColour( turn_colour) #This Determines the colour of the other player's piece. MoveID = stringID #This will make a copy of the index of the players move. new_state = "" copy_state = game_state if stringID + Constants.EAST > len( game_state) - 1 or stringID + Constants.EAST < 0: return copy_state #While the next token to check is the other colour. #EAST = 1 #while game_state[stringID + Constants.EAST] != "N": while game_state[stringID + Constants.EAST] == other_colour: #Possible statements for catching invalid moves. if NofChanges > ((8 - (MoveID % 8)) - 1): print("Out of bounds.") return copy_state if game_state[stringID + Constants.EAST] == "N": print("N detected") return copy_state #Testing. if isTesting: print(NofChanges, "NofChanges Number of other colours") print(stringID + (Constants.EAST), "index of other_colour") #Move on to the Next Token stringID = stringID + Constants.EAST NofChanges = NofChanges + 1 if game_state[stringID + Constants.EAST] == turn_colour: NofPieces = NofChanges while NofChanges != 0: if turn_colour == "W": colour = "White" else: colour = "Black" ColumnRow = StringInterpret.pieceToString(stringID) column = ColumnRow[0] row = int(ColumnRow[1]) TurtleMove.placePiece(column, row, colour) stringID = stringID - Constants.EAST NofChanges = NofChanges - 1 if NofChanges == 0: new_state = game_state[:MoveID + 1] + ( turn_colour * NofPieces) + game_state[MoveID + NofPieces + 1:] return new_state return game_state
def CheckSouth(game_state, stringID, turn_colour, isTesting=False): NofChanges = 0 #Counts the number of pieces to change. PlaceHolder = "" #This will be a section of the new string. slice_new_state = "" #This will be a bigger section of the new game state string. new_state = "" #This going to be the new game stat. MoveID = 0 #This will make a copy of the index of the players move. MoveXID = 0 #This will be a marker for where the players piece is accross from their move. other_colour = OtherColour( turn_colour) #This Determines the colour of the other player's piece. MoveID = stringID #This will make a copy of the index of the players move. copy_state = game_state if stringID + Constants.SOUTH > len( game_state) or stringID + Constants.SOUTH < 0: print("Out of range") return copy_state if game_state[stringID + Constants.SOUTH] == "N": print("N detected") return copy_state #while game_state[stringID + Constants.SOUTH] != "N": #While the next token to check is the other colour. #SOUTH = 8 while game_state[stringID + Constants.SOUTH] == other_colour: #Check if move is invalid if NofChanges > (8 - (MoveID // 8)) - 1: print("Out of bounds") return copy_state if game_state[stringID + Constants.SOUTH] == "N": print("N detected") return copy_state #Move to the next piece. stringID = stringID + Constants.SOUTH #Counts one more for the number of pieces to change. NofChanges = NofChanges + 1 #If the next piece is the turn player's colour.... if game_state[stringID + Constants.SOUTH] == turn_colour: #The token across is the ID of the last piece to change plus SOUTH. MoveXID = stringID + Constants.SOUTH #While there is still a number of pieces left to change. while NofChanges != 0: #Create a block of string with the character changed at the beginning. PlaceHolder = turn_colour + game_state[stringID + 1:stringID + Constants.SOUTH] if turn_colour == "W": colour = "White" else: colour = "Black" ColumnRow = StringInterpret.pieceToString(stringID) column = ColumnRow[0] row = int(ColumnRow[1]) TurtleMove.placePiece(column, row, colour) #Create another block of string that was assembled from the blocks of Placeholder. slice_new_state = PlaceHolder + slice_new_state #Moving backwards through the index of the pieces that need to be changed. stringID = stringID - Constants.SOUTH #One less piece to change. NofChanges = NofChanges - 1 #Testing if isTesting: print(PlaceHolder, "PlaceHolder") print(slice_new_state, "slice_new_state") print(stringID, "Index of Change") print(NofChanges, "Number of Changes left") #If there are no pieces left to change, assemble the new string. if NofChanges == 0: new_state = game_state[:MoveID + Constants. SOUTH] + slice_new_state + game_state[ MoveXID:] #Testing. if isTesting: print(MoveID + Constants.SOUTH, " MoveID") print(MoveXID, " MoveXID") print( "012345678901234567890123456789012345678901234567890123" ) print(game_state) print(new_state) #Return the new string with all the pieces changed in the corresponding direction. return new_state return game_state
print(column, "Column") #Prints the Column Letter print(row, "Row") #Prints the Row Number # print(game_state[moveIDX], ": Character at the move's index number.") #Prints the character at the index [moveIDX] print(moveIDX, "Index number of new move") #Takes the move and converts it to the index number for the string print(new_state) #Prints the new_state with the character at index.moveIDX in place print() ########################## return new_state #returns the new_state, The updated move. #Sets up the board using the pieces and reading through the string. def setup2(): """ new_state = stringInterpretSetup(("NNNNNNNN" * 8), "D4", -2) new_state = stringInterpretSetup((new_state), "E4", -1) new_state = stringInterpretSetup((new_state), "D5", -1) new_state = stringInterpretSetup((new_state), "E5", -2) """ new_state = "NNNNNNNNNNNNNNNNNNNNNNNNNNNBWNNNNNNWBNNNNNNNNNNNNNNNNNNNNNNNNNNN" start_board = stringToPiece(new_state) for turn in range(64): new_state = stringInterpret(new_state, input("Move?"), turn + 1) if __name__ == "__main__": TurtleMove.setup() setup2() Constants.WINDOW.exitonclick()
def CheckWest(game_state, stringID, turn_colour, isTesting=False): NofChanges = 0 #Counts the number of pieces to change. PlaceHolder = "" #This will be a section of the new string. slice_new_state = "" #This will be a bigger section of the new game state string. new_state = "" #This going to be the new game stat. MoveID = 0 #This will make a copy of the index of the players move. MoveXID = 0 #This will be a marker for where the players piece is accross from their move. other_colour = OtherColour( turn_colour) #This Determines the colour of the other player's piece. MoveID = stringID #This will make a copy of the index of the players move. copy_state = game_state if stringID + Constants.WEST > len( game_state) or stringID + Constants.WEST < 0: return copy_state if game_state[stringID + Constants.WEST] == "N": print("N detected") return copy_state #While the next token to check is the other colour. #WEST = -1 while game_state[stringID + Constants.WEST] == other_colour: #Possible statements for catching invalid moves. if NofChanges > (MoveID % 8) - 1: print("out of bounds") return copy_state if game_state[stringID + Constants.WEST] == "N": print("N detected") return copy_state #Testing. if isTesting: print(NofChanges, "NofChanges Number of other colours") print(stringID + (Constants.WEST), "index of other_colour") #Move on to the Next Token stringID = stringID + Constants.WEST NofChanges = NofChanges + 1 if game_state[stringID + Constants.WEST] == turn_colour: NofPieces = NofChanges while NofChanges != 0: if turn_colour == "W": colour = "White" else: colour = "Black" ColumnRow = StringInterpret.pieceToString(stringID) column = ColumnRow[0] row = int(ColumnRow[1]) TurtleMove.placePiece(column, row, colour) stringID = stringID - Constants.WEST NofChanges = NofChanges - 1 if NofChanges == 0: new_state = game_state[:MoveID + 1] + ( turn_colour * NofPieces) + game_state[MoveID + NofPieces + 1:] return new_state return game_state
def update_diagnalUD2(game_state, x, y, turn_letter): #Get the diagnals in both directions left_row = get_diagnalSE(game_state, x, y) right_row = get_diagnalNW(game_state, x, y) #Form the the row that is to updated, and add in the turn_letter temp_row = left_row temp_row.append(turn_letter) temp_row = temp_row + right_row #This gives us the row where bottom is the left row. #find the location of the turn_letter relative to the diagnal, which is needed to update the row relative = len(left_row) #Update the row diag_row = updateRow(temp_row, relative - 1, turn_letter) #Apply the update to the old game_state edge_found = False xTracker = x yTracker = y iteration = relative - 1 for letter in diag_row: if 0 <= yTracker <= 7: temp_row = game_state[yTracker] if 0 <= xTracker <= 7: if 0 <= iteration <= len(diag_row) - 1: piece = diag_row[iteration] temp_row[xTracker] = piece game_state[yTracker] = temp_row #Place the piece on the board conv_move = convertMoveType(xTracker, yTracker) Tx = conv_move[0] Ty = conv_move[1] + 1 temp_piece = piece if temp_piece != 'N': if temp_piece == "B": temp_piece = "Black" else: temp_piece = "White" TurtleMove.placePiece(Tx, Ty, temp_piece) xTracker = xTracker - 1 yTracker = yTracker - 1 iteration = iteration + 1 #SE #Update the game_state and board in the SE direction xTracker = x yTracker = y iteration = relative - 1 for letter in diag_row: if 0 <= yTracker <= 7: temp_row = game_state[yTracker] if 0 <= xTracker <= 7: if 0 <= iteration <= len(diag_row) - 1: piece = diag_row[iteration] temp_row[xTracker] = piece game_state[yTracker] = temp_row #Place the piece on the board conv_move = convertMoveType(xTracker, yTracker) Tx = conv_move[0] Ty = conv_move[1] + 1 temp_piece = piece if temp_piece != 'N': if temp_piece == "B": temp_piece = "Black" else: temp_piece = "White" xTracker = xTracker + 1 yTracker = yTracker + 1 iteration = iteration - 1 return game_state
def updateDiagnalDU2(game_state, x, y, turn_letter): #Get the diagnals in both directions left_row = getDiagnalSW(game_state, x, y) right_row = getDiagnalNE(game_state, x, y) #Form the the row that is to updated, and add in the turn_letter temp_row = left_row temp_row.append(turn_letter) temp_row = temp_row + right_row #find the location of the turn_letter relative to the diagnal, which is needed to update the row relative = len(left_row) #Update the row diag_row = updateRow(temp_row, relative - 1, turn_letter) #NE Letter reinsertion edge_found = False xTracker = x yTracker = y iteration = relative - 1 #If the x and y trackers are indexes on the board, update the piece to match the diag_row index for letter in diag_row: if 0 <= yTracker <= 7: temp_row = game_state[yTracker] if 0 <= xTracker <= 7: if 0 <= iteration <= len(diag_row) - 1: #If all of the above if statements are true, then all indexes are within range #Update the piece based on the location in the diag_row #and the x and y trackers piece = diag_row[iteration] temp_row[xTracker] = piece game_state[yTracker] = temp_row #We need to place the piece on the board #convert the coords into the appropriate format conv_move = convertMoveType(xTracker, yTracker) Tx = conv_move[0] Ty = conv_move[1] + 1 #Add 1 to move out of list IDX format temp_piece = piece #Find the piece colour if temp_piece != 'N': if temp_piece == "B": temp_piece = "Black" else: temp_piece = "White" #Place the piece TurtleMove.placePiece(Tx, Ty, temp_piece) #Change the trackers in the desired directions xTracker = xTracker + 1 yTracker = yTracker - 1 iteration = iteration + 1 #We now repeat the same process just in a different direction edge_found = False xTracker = x yTracker = y iteration = relative - 1 for letter in diag_row: if 0 <= yTracker <= 7: temp_row = game_state[yTracker] if 0 <= xTracker <= 7: if 0 <= iteration <= len(diag_row) - 1: piece = diag_row[iteration] temp_row[xTracker] = piece game_state[yTracker] = temp_row conv_move = convertMoveType(xTracker, yTracker) Tx = conv_move[0] Ty = conv_move[1] + 1 temp_piece = piece if temp_piece != 'N': if temp_piece == "B": temp_piece = "Black" else: temp_piece = "White" TurtleMove.placePiece(Tx, Ty, temp_piece) xTracker = xTracker - 1 yTracker = yTracker + 1 iteration = iteration - 1 return game_state
def CheckNorthWest(game_state, stringID, turn_colour, isTesting=False): NofChanges = 0 #Counts the number of pieces to change. PlaceHolder = "" #This will be sections of string. slice_new_state = "" #This will make a bigger section of the game state string from PlaceHolders. new_state = "" #This is going to be the new string for the games state. MoveXID = 0 #This is going to be where the token across from other_colour = OtherColour(turn_colour) #Determines the other colour. MoveID = stringID #Makes a copy of the index of the current player's move. copy_state = game_state if stringID + Constants.NORTHWEST > len( game_state) or stringID + Constants.NORTHWEST < 0: return copy_state if game_state[stringID + Constants.NORTHWEST] == "N": print("N detected") return copy_state #While the next token to check is the other colour. #NORTHWEST = -9 while game_state[stringID + Constants.NORTHWEST] == other_colour: #Catch invalid moves if NofChanges > (MoveID // 8) - 1: print("Out of bounds!") return copy_state if game_state[stringID + Constants.NORTHWEST] == "N": print("N detected") return copy_state # if game_state[stringID + Constants.NORTHWEST] != other_colour and game_state[stringID + Constants.NORTHWEST] != turn_colour: # print("!= any colour") # return game_state #Move to the next piece. stringID = stringID + Constants.NORTHWEST #Counts one more for the number of pieces to change. NofChanges = NofChanges + 1 #If the next piece is the turn player's colour.... if game_state[stringID + Constants.NORTHWEST] == turn_colour: #The token across is the ID of the last piece to change plus NORTHWEST MoveXID = stringID + Constants.NORTHWEST #While there is still a number of pieces left to change. while NofChanges != 0: #Create a block of string with the character changed at the beginning PlaceHolder = turn_colour + game_state[stringID + 1:stringID - Constants.NORTHWEST] if turn_colour == "W": colour = "White" else: colour = "Black" ColumnRow = StringInterpret.pieceToString(stringID) column = ColumnRow[0] row = int(ColumnRow[1]) TurtleMove.placePiece(column, row, colour) #Create another block of string that was assembled from the blocks of Placeholder. slice_new_state = slice_new_state + PlaceHolder #Moveing backwards through the index of the pieces that need to be changed. stringID = stringID - Constants.NORTHWEST #One less piece to change. NofChanges = NofChanges - 1 #Testing if isTesting: print(PlaceHolder, "PlaceHolder") print(slice_new_state, "slice_new_state") print(stringID, "Index of Change") print(NofChanges, "Number of Changes left") #If there are no pieces left to change, assemble the new string. if NofChanges == 0: new_state = game_state[:MoveXID - Constants. NORTHWEST] + slice_new_state + game_state[ MoveID:] #Testing if isTesting: print("01234567890123456789012345678901234567890") print(game_state) print(new_state) #Return the new string with all the pieces changed in the corresponding direction. return new_state return game_state
def stringInterpret(game_state, NewMove, turn, setup=False): column = NewMove[0].upper() row = int(NewMove[1]) column_IDX = (Constants.COLUMN_LETTERS.index(column)) row_IDX = (Constants.ROW_NUMBERS.index(row)) turn_colour = whoseTurn(turn) new_state = game_state[:] if setup == True: new_state[row_IDX][column_IDX] = turn_colour file = open("testCode.txt", "r+") print("Before : ") file.write("Before updater : " + '\n') for i in game_state: print(i) file.write(str(i) + '\n') file.write(NewMove + " " + turn_colour + '\n') print(NewMove, " ", turn_colour) if setup == False: new_state = ListUpdater.updateToken(game_state, NewMove, turn_colour) stringToPiece(new_state) print("After : ") file.write("After updater : " + '\n') for i in new_state: print(i) #file = open("testCode.txt", "r+") file.write(str(i) + '\n') #file.close() file.close() if whoseTurn(turn) == Constants.PIECE_WHITE: color = "White" else: color = "Black" if setup == True: TurtleMove.placePiece(column, row, color) if __name__ == "__main__": #TESTING TESTING TESTING ########################## print("Testing") print("turn ", turn) print(column_IDX, "column_IDX") # Column index number from letters print(row_IDX, "row_IDX") #Row index number from numbers print(column, "Column") #Prints the Column Letter print(row, "Row") #Prints the Row Number print(game_state[move_to_string], ": Character at the move's index number." ) #Prints the character at the index [move_to_string] print( move_to_string, "Index number of new move" ) #Takes the move and converts it to the index number for the string print( new_state ) #Prints the new_state with the character at index.move_to_string in place print() ########################## return new_state #Return the updated game state