Drop links or images here to add them to the editor.

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

Your move

Make

Write a program that converts a grid reference, e.g. C5 to an array of two coordinates, e.g. [3, 5].

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called get_move that:

  1. Prompts the user to enter their move.
  2. Strips any additional whitespace.
  3. Converts the input to uppercase.
  4. Returns the string.

Complete the subprogram called get_indexes that:

  1. Takes a parameter move which is the string output from get_move.
  2. Returns the coordinates as an array of two indexes.

Complete the main program so that:

  1. It calls the get_move function.
  2. Outputs the coordinate array in the format [x, y].

Typical inputs and outputs from the program would be:

Enter your move: 
A1
[1, 1]
Enter your move: 
B3
[2, 3]
Enter your move: 
c5  
[3, 5]
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// Your move program

using System;

class Submission
{
---
    // -------------------------
    // Subprograms
    // -------------------------
    // Function to get move from the player
---
    static string get_move()
    {
---
        Console.WriteLine("Enter your move: ");
        string move = Console.ReadLine();
---
        // Strip spaces and convert to uppercase
        move = move.Trim();
        move = move.ToUpper();
---
        return move;
---
    }


---
    // Function to return an array of two numbers for the move
    // E.g. A3 is [1, 3] C5 is [3, 5]
---
    static int[] get_indexes(string move)
    {
---
        int[] index = {0, 0};
---
        // Convert to ascii is the most efficient way
        int ascii = ((int)(move[0])) - 64;
        index[0] = ascii;
---
        index[1] = Convert.ToInt32(move[1].ToString());
---
        return index;
---
    }


---
    // -------------------------
    // Main program
    // -------------------------
    public static void Main(string[] args)
    {
---
        string move = get_move();
---
        int[] indexes = get_indexes(move);
        Console.WriteLine($"[{indexes[0]}, {indexes[1]}]");
---
    }
---
}