Arrays/Tables

I need some help setting up a table. I have a gameboard with 16 spaces and 16 game pieces. I want to move the game pieces on the board and to identify if the pieces are in the right location. To beat the game the pieces must be placed in specific locations. How do I do this. I am a newby and just need the initial the beginning code and I’m sure I can figure out the rest…hopefully:-)

Do I need to name my pieces specifically? How do I identify the location on the board? So many questions.

Michelle [import]uid: 72372 topic_id: 12599 reply_id: 312599[/import]

The simplest way – not the best way, mind you, but the simplest – would be to define a table of numbers or strings.

For instance, if your pieces were numbered 1-16, here’s a starting board:

local board = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16},
}

Now you can access any particular square by specifying it’s row (1 is the top, 4 is the bottom) and column (1 is the left, 4 is the right).

local topLeft = board[1][1] – 1
local topRight = board[1][4] – 4
local bottomLeft = board[4][1] – 13
local bottomRight = board[4][4] – 16

You’d check for winning after every move, and if the numbers were all in the correct place, bring up a “you win” screen.

Of course, if you don’t like numbers, you can use strings instead:

local board = {
{“rook”, “knight”, “king”, “bishop”},
{“pawn”, “pawn”, “pawn”, “pawn”},
{“Pawn”, “Pawn”, “Pawn”, “Pawn”},
{“Bishop”, “King”, “Knight”, “Rook”},
}

In that case, your comparisons would take a TINY bit longer. Numbers are faster than strings.

A neat lua trick: to swap two variables, you can use this syntax:

x,y = y,x

In your case, that would allow you to swap pieces with:

board[i][j],board[x][y] = board[x][y],board[i][j]

Good luck! [import]uid: 75168 topic_id: 12599 reply_id: 48291[/import]

Thank you! [import]uid: 72372 topic_id: 12599 reply_id: 48307[/import]