# Problem with checking a position in XNA game

• (2 Pages)
• 1
• 2

## 19 Replies - 2112 Views - Last Post: 01 June 2015 - 01:06 PM

### #1 Exploits

Reputation: 0
• Posts: 7
• Joined: 14-May 15

# Problem with checking a position in XNA game

Posted 14 May 2015 - 04:56 PM

I am making a board game similar to Ludo ,also this is first time i am developing a board game and using XNA.
I apologize in advance for my English.

U should know:
1. There are four moving pieces for every color.
2. Every color got moving path/array with 55 specific fields/positions.
3. 0,1,2,3 array elements or first 4 positions are home positions and last four 51,52,53,54 are goal positions in that color path.

Question:
I need to make method for checking if all pieces of same color are in goal or home positions,so i can allow that player to roll dice three times instead of one if he or she got none of pieces outside goal or home positions.
My problem is that,if pieces are set like this(picture 1) in goal positions i can allow to roll 3 times,but if pieces are set like on (picture 2) i cant allow cos it need to be like its on (picture 1)

Picture 1:

Picture 2:

Full code of Path class:
```class Putanje
{
static Rectangle[] prav = new Rectangle[80];
static Polje[] polja = new Polje[80];
static int[] zutaPutanja = new int[55];
static int[] crvenaPutanja = new int[55];
static int[] plavaPutanja = new int[55];
static int[] zelenaPutanja = new int[55];

static Putanje()
{
//zute startne pozicije
prav[0]=new Rectangle(100,100,50,50);
prav[1]=new Rectangle(150,100,50,50);
prav[2]=new Rectangle(100,150,50,50);
prav[3]=new Rectangle(150,150,50,50);

//crvene startne pozicije
prav[4]=new Rectangle(550,100,50,50);
prav[5]=new Rectangle(600,100,50,50);
prav[6]=new Rectangle(550,150,50,50);
prav[7]=new Rectangle(600,150,50,50);

//plave startne pozicije
prav[8]=new Rectangle(100,550,50,50);
prav[9]=new Rectangle(150,550,50,50);
prav[10]=new Rectangle(100,600,50,50);
prav[11]=new Rectangle(150,600,50,50);

//zelene startne pozicije
prav[12]=new Rectangle(550,550,50,50);
prav[13]=new Rectangle(600,550,50,50);
prav[14]=new Rectangle(550,600,50,50);
prav[15]=new Rectangle(600,600,50,50);

//zajednicka putanja
prav[16]=new Rectangle(50,250,50,50);
prav[17]=new Rectangle(100,250,50,50);
prav[18]=new Rectangle(150,250,50,50);
prav[19]=new Rectangle(200,250,50,50);
prav[20]=new Rectangle(250,250,50,50);
prav[21]=new Rectangle(250,200,50,50);
prav[22]=new Rectangle(250,150,50,50);
prav[23]=new Rectangle(250,100,50,50);
prav[24]=new Rectangle(250,50,50,50);
prav[25]=new Rectangle(300,50,50,50);
prav[26]=new Rectangle(350,50,50,50);
prav[27]=new Rectangle(400,50,50,50);
prav[28]=new Rectangle(450,50,50,50);
prav[29]=new Rectangle(450,100,50,50);
prav[30]=new Rectangle(450,150,50,50);
prav[31]=new Rectangle(450,200,50,50);
prav[32]=new Rectangle(450,250,50,50);
prav[33]=new Rectangle(500,250,50,50);
prav[34]=new Rectangle(550,250,50,50);
prav[35]=new Rectangle(600,250,50,50);
prav[36]=new Rectangle(650,250,50,50);
prav[37]=new Rectangle(650,300,50,50);
prav[38]=new Rectangle(650,350,50,50);
prav[39]=new Rectangle(650,400,50,50);
prav[40]=new Rectangle(650,450,50,50);
prav[41]=new Rectangle(600,450,50,50);
prav[42]=new Rectangle(550,450,50,50);
prav[43]=new Rectangle(500,450,50,50);
prav[44]=new Rectangle(450,450,50,50);
prav[45]=new Rectangle(450,500,50,50);
prav[46]=new Rectangle(450,550,50,50);
prav[47]=new Rectangle(450,600,50,50);
prav[48]=new Rectangle(450,650,50,50);
prav[49]=new Rectangle(400,650,50,50);
prav[50]=new Rectangle(350,650,50,50);
prav[51]=new Rectangle(300,650,50,50);
prav[52]=new Rectangle(250,650,50,50);
prav[53]=new Rectangle(250,600,50,50);
prav[54]=new Rectangle(250,550,50,50);
prav[55]=new Rectangle(250,500,50,50);
prav[56]=new Rectangle(250,450,50,50);
prav[57]=new Rectangle(200,450,50,50);
prav[58]=new Rectangle(150,450,50,50);
prav[59]=new Rectangle(100,450,50,50);
prav[60]=new Rectangle(50,450,50,50);
prav[61]=new Rectangle(50,400,50,50);
prav[62]=new Rectangle(50,350,50,50);
prav[63]=new Rectangle(50,300,50,50);

//zute kucice
prav[64]=new Rectangle(100,350,50,50);
prav[65]=new Rectangle(150,350,50,50);
prav[66]=new Rectangle(200,350,50,50);
prav[67]=new Rectangle(250,350,50,50);

//crvene kucice
prav[68]=new Rectangle(350,100,50,50);
prav[69]=new Rectangle(350,150,50,50);
prav[70]=new Rectangle(350,200,50,50);
prav[71]=new Rectangle(350,250,50,50);

//plave kucice
prav[72]=new Rectangle(350,600,50,50);
prav[73]=new Rectangle(350,550,50,50);
prav[74]=new Rectangle(350,500,50,50);
prav[75]=new Rectangle(350,450,50,50);

//zelene kucice
prav[76]=new Rectangle(600,350,50,50);
prav[77]=new Rectangle(550,350,50,50);
prav[78]=new Rectangle(500,350,50,50);
prav[79]=new Rectangle(450,350,50,50);

//pravi niz Polja
for (int i = 0; i < prav.Length; i++)
{
polja[i] = new Polje(prav[i]);
}

//zuta putanja
zutaPutanja[0]=0;
zutaPutanja[1]=1;
zutaPutanja[2]=2;
zutaPutanja[3]=3;
for (int i = 0; i < 47; i++)
{
zutaPutanja[i + 4] = i + 16;
}

zutaPutanja[51] = 64;
zutaPutanja[52] = 65;
zutaPutanja[53] = 66;
zutaPutanja[54] = 67;

//crvena putanja
crvenaPutanja[0] = 4;
crvenaPutanja[1] = 5;
crvenaPutanja[2] = 6;
crvenaPutanja[3] = 7;
for (int i = 0; i <36 ; i++)
{
crvenaPutanja[i + 4] = i + 28;
}

for (int i = 0; i < 11; i++)
{
crvenaPutanja[i + 40] = i + 16;
}

crvenaPutanja[51] = 68;
crvenaPutanja[52] = 69;
crvenaPutanja[53] = 70;
crvenaPutanja[54] = 71;

//plava putanja
plavaPutanja[0] = 8;
plavaPutanja[1] = 9;
plavaPutanja[2] = 10;
plavaPutanja[3] = 11;
for (int i = 0; i <12 ; i++)
{
plavaPutanja[i+4]=i+52;
}

for (int i = 0; i <35 ; i++)
{
plavaPutanja[i+16]=i+16;
}

plavaPutanja[51] = 72;
plavaPutanja[52] = 73;
plavaPutanja[53] = 74;
plavaPutanja[54] = 75;

//zelena putanja
zelenaPutanja[0] = 12;
zelenaPutanja[1] = 13;
zelenaPutanja[2] = 14;
zelenaPutanja[3] = 15;

for (int i = 0; i <24 ; i++)
{
zelenaPutanja[i+4]=i+40;
}

for (int i = 0; i < 23; i++)
{
zelenaPutanja[i + 28] = i + 16;
}

zelenaPutanja[51] = 76;
zelenaPutanja[52] = 77;
zelenaPutanja[53] = 78;
zelenaPutanja[54] = 79;

}
public static Polje[] getPolja()
{
return polja;
}

public static int[] getzutaPutanja()
{
return zutaPutanja;
}

public static int[] getcrvenaPutanja()
{
return crvenaPutanja;
}

public static int[] getplavaPutanja()
{
return plavaPutanja;
}

public static int[] getzelenaPutanja()
{
return zelenaPutanja;
}

}
```

Some translation of array/properties names:

zutaPutanja - yellowPath
crvenaPutanja - redPath
plavaPutanja - bluePath
zelenaPutanja - greenPath
prav - rect is short of pravougaonik-rectangle
Polje - Field

Any idea how to make that method is appreciated.

Is This A Good Question/Topic? 0

## Replies To: Problem with checking a position in XNA game

### #2 _dontknow_

Reputation: 6
• Posts: 37
• Joined: 26-March 14

## Re: Problem with checking a position in XNA game

Posted 14 May 2015 - 11:34 PM

sry but not only this has pretty much nothing to do with xna but its also all written in some strange language...

also seems super easy to solve, i guess your code store somewhere the position for your 4 pieces, lets assume its an array

int[] YellowPieces = new int[4];
each corresponding to one of your hardcoded 80(or 55) positions of the board

your method should have some serie of ifs to check the positions against home and goal ones

```List<int> goalPositions= new List<int>();
List<int> homePositions= new List<int>();
List<int> piecesInGoal = new List<int>();
for (int i = 0; i < 4; i++)
{// this check if all yellow pieces are either in goal or home if not method return false
if (goalPositions.Contains(YellowPieces[i]))
else if (homePositions.Contains(YellowPieces[i]))
{
//do something dunno what
}
else
return false;
}
for (int i =3; i >=0; i--)
{//this checks if pieces in goal positions are actually ordered
if (piecesInGoal.Count > 0)
if (piecesInGoal.Contains(goalPositions[i]))
piecesInGoal.Remove(goalPositions[i]);
else
return false;
}
//if code arrive here you have the pieces like you wanted and can do your stuff!!!

```

### #3 BBeck

• Here to help.

Reputation: 792
• Posts: 1,886
• Joined: 24-April 12

## Re: Problem with checking a position in XNA game

Posted 15 May 2015 - 12:57 PM

It looks like C# to me. Wouldn't that be XNA? I don't see anything XNA specific, but I don't see what it's not XNA. But then I jump around with languages so much I can't remember if I'm in C# or C++.

And a spoken language is only "strange" if you don't speak it. Granted, it would be a lot easier for me to understand the code if it were written in English since that's primarily what I speak, but just because I don't know the language does not make it strange in the sense of "unusual or surprising in a way that is unsettling or hard to understand". Although maybe you meant "not previously visited, seen, or encountered; unfamiliar or alien".

Anyway, I don't know the game Ludo. But looking at the board it appears similar to some games we played as a kid.

From the description, it appears that the pieces cannot go around the board more than once. If they can and they have to "earn" their way into the center rows then that would change things.

So, I would rethink the whole thing from the beginning and ask what the best way to represent the board is.

I would imagine from the description that there are 55 positions on the board for each piece and that each track is basically a straight line even though it's arranged in a circle. The "lines" overlap on the circle. I'm not sure if only one piece can occupy a space or not or what happens if another piece tries to occupy the same space.

But in this scenario, I might represent each color with four playing pieces such as RedOne, RedTwo, YellowThree, etc. Each piece might even be an object of a piece class. Anyway, I would either make each of those an integer to represent their spot in the line or I would make them objects with a position variable member to represent the position of the piece on the board.

So then the position of the piece would be as simple as asking what it's position is. If we were numbering starting at zero, I might ask if the piece's position is less than 4. If so, I would know it is home. If it's position is greater than 50 then I know it is home. I might write all sorts of if then logic to determine if it could move into a position, such as querying all of other objects to see if their position was already that of the position I'm trying to move the piece into.

Since I don't know the rules of the game, I don't know the difference between picture 1 and 2. However, if all pieces must be as far home as possible, I could check how many pieces of that color are in that lane and that none of the positions between them are open. How this works is really dependent on the rules of the game. Your code could say that you can have 3 rolls if and only if the number of yellow pieces in the yellow line is equal to the number of slots occupied at the end of the lane.

So since there are two pieces in the yellow lane, you could check if their positions are 54 and 53 and they don't get 3 rolls unless that is true.

But none of this really requires an array.

### #4 _dontknow_

Reputation: 6
• Posts: 37
• Joined: 26-March 14

## Re: Problem with checking a position in XNA game

Posted 15 May 2015 - 01:08 PM

it requires an array cause you save yourself a carpal tunnel syndrome of conditions youd have to write manually otherwise

### #5 BBeck

• Here to help.

Reputation: 792
• Posts: 1,886
• Joined: 24-April 12

## Re: Problem with checking a position in XNA game

Posted 16 May 2015 - 03:17 AM

I'll buy that. 2D's not really my thing or my strong suit. I've never tried to make a board game and haven't done a whole lot of 2D in general.

But would you use a List or an array? I'm seeing Lists in your code as opposed to static arrays.

This post has been edited by BBeck: 16 May 2015 - 03:21 AM

### #6 _dontknow_

Reputation: 6
• Posts: 37
• Joined: 26-March 14

## Re: Problem with checking a position in XNA game

Posted 16 May 2015 - 05:57 AM

array for board pieces, i dont know the game but if i understood correctly you can only have 4 pieces so it would make sense to use array there

i used list for flexiblity and reusability, so you can maybe pass other infos to the method, like a list of goal/home tiles so you can check for different players(but yeah probably if the game is well defined a list wasnt necessary, i just thought it "might" come handy if something in the game requires some flexibility)

### #7 Exploits

Reputation: 0
• Posts: 7
• Joined: 14-May 15

## Re: Problem with checking a position in XNA game

Posted 17 May 2015 - 01:38 PM

Thank u for help guys ,i am busy these days so i don't have time to try finish my method.I think that i can use that ordering check from _dontknow somehow ...that's only problem i got i made check code for goal and home positions ,now only need to check if pieces are ordered inside goal positions.

And language is Serbian,
also this game is similar to "Don't get mad man",
think that we all played it at least once in our lives

yea its xna game just i did not copy/paste "using" part from that page where class is
also i am using c#,visual studio 2010 express

### #8 _dontknow_

Reputation: 6
• Posts: 37
• Joined: 26-March 14

## Re: Problem with checking a position in XNA game

Posted 17 May 2015 - 02:37 PM

the idea behind my code was to actually check order too

you see i copy all yellow pieces that are in goal tiles into piecesInGoal

then i remove in order from the last meaning if they arent the last n of the 4 goal tiles youll get false as result

(assuming my code was correct)

### #9 BBeck

• Here to help.

Reputation: 792
• Posts: 1,886
• Joined: 24-April 12

## Re: Problem with checking a position in XNA game

Posted 18 May 2015 - 08:54 AM

I think we call it Trouble here.

http://img0.etsystat...N.264470424.jpg

My grandparents called it "peg". There was a wooden board with holes drilled in it and you used colored golf tees as the pieces called pegs. It was shaped more square similar to your version where Trouble is more of a circle. And I think there may have been more than 4 pegs but I was a small kid the last time I played it.

We don't see enough board games in game programming these days. Or maybe I just don't know where to look.

I was playing a lot of Monopoly against the computer a couple of months ago and I'm thinking about how old this game is and why you don't see digital boards games. Monopoly is just an awesome game. It's amazing that more people haven't come up with a whole slew of board games. Although, I've gotten where I can beat the computer at Monopoly almost every time, it's a nice blend of skill and luck.

At least in theory anyway, you can beat the most expert player if the rolls just happen to go your way. But it's also heavily based on the skill of property trading. Decisions about which properties to buy and when and your ability to trade properties with other players (I find the computer to be too easy or too hard on this and generally too easy) are the primary determination of who wins. Although some decisions are countered with a certain amount of luck. For example, if you manage to get a couple Monopolies and put hotels on them, there's a Chance card that makes you pay taxes on them which can be pretty bad if you leveraged yourself to get those hotels and are broke.

But anyway, I've thought about trying to make a "new" digital board game.

This post has been edited by BBeck: 18 May 2015 - 09:06 AM

### #10 _dontknow_

Reputation: 6
• Posts: 37
• Joined: 26-March 14

## Re: Problem with checking a position in XNA game

Posted 18 May 2015 - 10:53 AM

sry to have to correct you but you sort of missed a tons of super board games, monopoly and risk were a very old generation (mine too btw ) but in the recent years(from around 20 years ago till these days) so many came out and much much better both for entertaining and for more "competitive" play

and also... yeah they have been ported to online platforms too, you can check www.brettspielwelt.de which is the best afaik and has tons of board games ported (too bad its in crappy java but nobody is perfect i guess )

some has been ported to apple store too if i remember correctly

also there have been a few games developed for computer by indie guys that were basically board games or with a very high flavor of them but cant recall names atm

anyway you are very welcome to try, im a nostalgic too and always enjoy a good old fashioned style

### #11 BBeck

• Here to help.

Reputation: 792
• Posts: 1,886
• Joined: 24-April 12

## Re: Problem with checking a position in XNA game

Posted 18 May 2015 - 11:53 AM

I'll have to check that site out.

### #12 Exploits

Reputation: 0
• Posts: 7
• Joined: 14-May 15

## Re: Problem with checking a position in XNA game

Posted 25 May 2015 - 06:24 PM

```class Player
{
protected PieceSet[] pieces;
Color color;
int numberOfThrows;
Dice dice;

public enum Color
{
Yellow, Green, Blue, Red
}

public Player() {}

public Player(Color color)
{
int[] path = new int[55];
this.color = color;
dice = new Dice();
numberOfThrows = 3;
switch (color)
{
case Color.Yellow:
path = BoardHelper.getYellowPath();
break;
case Color.Green:
path = BoardHelper.getGreenPath();
break;
case Color.Blue:
path = BoardHelper.getBluePath();
break;
case Color.Red:
path = BoardHelper.getRedPath();
break;
}
pieces = new PieceSet[4];
pieces[0] = new PieceSet(path, 0);
pieces[1] = new PieceSet(path, 1);
pieces[2] = new PieceSet(path, 2);
pieces[3] = new PieceSet(path, 3);
}

[b]public bool isAllPiecesInBaseOrGoal()[/b]
{
if ((pieces[0].getPosition() < 4 || pieces[0].getPosition() == 54) &&
(pieces[1].getPosition() < 4 || pieces[1].getPosition() == 54) &&
(pieces[2].getPosition() < 4 || pieces[2].getPosition() == 54) &&
(pieces[3].getPosition() < 4 || pieces[3].getPosition() == 54))
return true;
else
return false;
}
```

This is part of class from source code i use to help me make that game,but that game got only one goal position, need to change that isAllPiecesInBaseOrGoal method into one with 4 goal positions and order check like i asked before.

Now i am badly confused, i don't know how to use your code example inside of it because i must getPosition() before any check and u used only one color pieces i need to make one method usable by all 4 colors.
Is it possible or i need to start from scratch, find another way to deal with it and hope that i will finish it in one month cos it for my last exam.
That method is only problem in my whole game.

### #13 Exploits

Reputation: 0
• Posts: 7
• Joined: 14-May 15

## Re: Problem with checking a position in XNA game

Posted 25 May 2015 - 06:33 PM

I was thinking to start like this to make 3 lists pieces list golpos list and basepos list and than do the check but dont know how to put values of pieces[0].getPosition(),pieces[1].getPosition() etc. into one list ?

```public bool isAllPiecesInBaseOrGoalOrdered()
{
List<int> goalPositions = new List<int>();

List<int> basePositions = new List<int>();

}
```

### #14 _dontknow_

Reputation: 6
• Posts: 37
• Joined: 26-March 14

## Re: Problem with checking a position in XNA game

Posted 25 May 2015 - 11:25 PM

i guess you can pass the data you want to be checked to the method

public bool isAllPiecesInBaseOrGoalOrdered(List<int> goalPositions,List<int> homePositions, List<int> playerPieces)
{

}

or just check for each player, its your choice
or you could just pass the color to the method and the method itself retrieve the corresponding goal and base position for that color

### #15 Exploits

Reputation: 0
• Posts: 7
• Joined: 14-May 15

## Re: Problem with checking a position in XNA game

Posted 26 May 2015 - 05:54 AM

is this correct way to do it so method retuuns true or false after check all ?

```pieces = new Pieces[4];
pieces[0]=new Pieces(path,0);
pieces[1]=new Pieces(path,1);
pieces[2]=new Pieces(path,2);
pieces[3]=new Pieces(path,3);

List<int> goalPositions = new List<int>();

List<int> basePositions = new List<int>();

int piecePosition1 = pieces[0].getPosition();
int piecePosition2 = pieces[1].getPosition();
int piecePosition3 = pieces[2].getPosition();
int piecePosition4 = pieces[3].getPosition();

List<int> piecePositions = new List<int>();

}

public bool isAllPiecesInBaseOrGoalOrdered(List<int> goalPositions,List<int> basePositions,List<int> piecePositions )
{
List<int> piecesOnGoal =new List<int>();
List<int> piecesOnBase = new List<int>();
bool value = false;

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

if (goalPositions.Contains(piecePositions[i]))
{

if (basePositions.Contains(piecePositions[i]))
{
}
}
else
value = false;

if (piecesOnGoal.Count+piecesOnBase.Count==4)
{
value = true;
}
}
for (int i = 3; i >= 0; i++)
{
if (piecesOnGoal.Count > 0)
if (piecesOnGoal.Contains(goalPositions[i]))
piecesOnGoal.Remove(goalPositions[i]);
else
value=false;

}

return value;

}
```

i saw that i need to put this outside of for statement

```if (piecesOnGoal.Count+piecesOnBase.Count==4)
{

value = true;

}

```