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 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 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 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 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 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 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 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
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 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 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
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