# Loop back to beginning of list in monopoly style game

Hey all,

I am making a Monopoly clone and I am having a problem with going around the board. I am using tiles and storing their positions in a List which moves the player depending on the dice roll.

My problem is when I go around the board and get to the last tile I am not sure how to loop it back to the beginning of the list so the player will continue pass go and start moving around the board again.

My current code is:

``````    IEnumerator movePlayer(Token currentPlayerNumber, int rollValue)
{
isPlayerMoving = true;
//Get tile player is currently on
int currentTile = currentPlayerNumber.CurrentTileID;

//Move player according to dice value
for(int i = 0; i < rollValue; i++)
{
float t = 0;

//Makes the player move one tile every roll until it has reached the end position
Vector3 startPosition = tile[(currentTile + i)].transform.position + playerPosition[currentPlayerNumber.ID];
Vector3 endPosition = tile[(currentTile + i + 1)].transform.position + playerPosition[currentPlayerNumber.ID];

while (t < 1f)
{
t += Time.deltaTime * 4f;

currentPlayerNumber.transform.position = Vector3.Lerp(startPosition, endPosition, t);

yield return null;
}

yield return null;
}

//Reset current tile position
currentPlayerNumber.CurrentTileID = currentTile + rollValue;

isPlayerMoving = false;

StartCoroutine(tileActions());
}
``````

Any one have any ideas on how to achieve this?

Instead of using currentTile, you can use variable for the startTile and endTile index. Then you just have to check to see if the endTile value is greater than the last index of your tile array or not. This is untested code, but it should be enough to get the idea.

``````IEnumerator movePlayer(Token currentPlayerNumber, int rollValue) {

isPlayerMoving = true;
//Get tile player is currently on
int currentTile = currentPlayerNumber.CurrentTileID;
int sTile = currentTile;
int eTile;

for(int i = 0; i < rollValue; i++) {

float t = 0;
eTile++;

if(eTile > tile.length)
eTile = 0;

//Makes the player move one tile every roll until it has reached the end position
Vector3 startPosition = tile[sTile].transform.position + playerPosition[currentPlayerNumber.ID];
Vector3 endPosition = tile[eTile].transform.position + playerPosition[currentPlayerNumber.ID];

while (t < 1f) {
t += Time.deltaTime * 4f;
currentPlayerNumber.transform.position = Vector3.Lerp(startPosition, endPosition, t);
yield return null;
}

sTile = eTile;
yield return null;
}
//Reset current tile position
currentPlayerNumber.CurrentTileID = currentTile + rollValue;
isPlayerMoving = false;
StartCoroutine(tileActions());
}
``````

Hi @beefyt123 din you finish your work?? i also creating monopoly game with a twist do you have some sample project that i can use as a guide? thanks in advance