Processing grid references to indexes.
★★☆Popular games like chess and battleships are played on a grid of squares that can be referred to with coordinates such as A1, B3, C5 etc. The user would input the coordinate to make a move with a piece on that square. However, for processing it is easier to work with just numbers instead of a combination of letters and numbers. This program takes an input and creates a list of the numerical grid reference. E.g. A1 is [1, 1], B3 is [2, 3] and C5 is [3, 5].
Write a program that converts a grid reference, e.g. C5 to a list of two coordinates, e.g. [3, 5].
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
get_move that:get_indexes that:move which is the string output from get_move.get_move function.Enter your move: A1
[1, 1]
Enter your move: B3
[2, 3]
Enter your move: c5
[3, 5]
# Your move program
# -------------------------
# Subprograms
# -------------------------
# Function to get move from the player
---
def get_move():
---
move = input("Enter your move: ")
---
# Strip spaces and convert to uppercase
move = move.strip()
move = move.upper()
---
return move
---
# Function to return an array of two numbers for the move
# E.g. A3 is [1, 3] C5 is [3, 5]
---
def get_indexes(move):
---
index = [0, 0]
---
# Convert to ascii is the most efficient way
ascii = ord(move[0]) - 64
---
index[0] = ascii
index[1] = int(move[1])
---
return index
---
# -------------------------
# Main program
# -------------------------
---
move = get_move()
---
print(get_indexes(move))