Multidimensional subscripts

While the most common subscripts are the ones that take a single parameter, subscripts are not limited to single parameters. They can take any number of input parameters, and these parameters can be of any type.

Let's look at how we could use a multidimensional subscript to implement a Tic-Tac-Toe board. A Tic-Tac-Toe board looks similar to the following figure:

The board can be represented by a two-dimensional array, where each dimension has three elements. Each player will then take a turn placing his/her pieces (typically x or o) onto the board until one player has three pieces in a row or the board is full.

Let's look at how we could implement a Tic-Tac-Toe board using a multidimensional array and multidimensional subscripts:

struct TicTacToe { 
  var board = [["","",""],["","",""],["","",""]] 
  subscript(x: Int, y: Int) -> String { 
    get { 
      return board[x][y] 
    } 
    set { 
      board[x][y] = newValue 
    } 
  } 
} 

We start the Tic-Tac-Toe structure by defining a 3×3 array, also known as a matrix, which will represent the game board. We then define a subscript that can be used to set and retrieve player pieces on the board. The subscript will accept two integer values. We define multiple parameters for our subscripts by putting the parameters between parentheses. In this example, we are defining the subscript with the parameters, (x: Int, y: Int). We can then use the x and y variable names within our subscripts to access the values that are passed in.

Let's look at how to use this subscript to set the user's pieces on the board:

var board = TicTacToe()  
board[1,1] = "x" 
board[0,0] = "o" 

If we run this code, we will see that we added the x piece to the center square and the o piece to the upper-left square, so our game board will look similar to the following:

We are not limited to using only one type for our multidimensional subscripts. For example, we could have a subscript of the (x: Int, y: Double, z: String).

We can also add external names for our multidimensional subscript types to help identify what values are used for and to distinguish between the subscripts that have the same types. Let's take a look at how to use multiple types and external names with subscripts, by creating a subscript that will return an array of string instances based on the values of the subscript:

struct SayHello { 
  subscript(messageText message: String, messageName name: String, number number: Int) -> [String]{ 
    var retArray: [String] = [] 
    for _ in 0..<number { 
      retArray.append("(message) (name)") 
    } 
    return retArray 
  } 
} 

In the SayHello structure, we define our subscript as follows:

subscript(messageText message: String,messageName name: String, number number: Int) -> [String] 

This defines a subscript with three elements. Each element has an external name (messageText, messageName, and number) and an internal name (message, name, and number). The first two elements are of the string type and the last one is an integer type. We use the first two elements to create a message for the user that will repeat the number of times defined by the last (number) element. We will use this subscript as follows:

var message = SayHello() 
var ret = message[messageText:"Bonjour",messageName:"Jon",number:5] 

If we run this code, we will see that the ret variable contains an array of five strings, where each string equals Bonjour Jon.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset