1

I've been tasked with creating a bard game using c# at university.

There is to be 2-4 players, each player rolls a dice in turn. The objective is to get to the last square on the grid.

The only relevant rule to this question is that no more than one player can be on the same square at once.

So for example

Both players start at position 0.

Player (A) rolls a 1 = Player(A) at square 1. Player (B) rolls a 1 = Player(B) 'skips' over Player(A) and lands on square 2.

I've left out the dice roll method and the main as to my knowledge they're not relevant to the question.

private static void PlayerTurn(int playerNo)
        {
        playerPositions[playerNo] = playerPositions[playerNo] + RollDice();  
        // The selected player rolls the dice and moves x amount of squares 
        //(dependant on dice roll value)
        }

That is the method for moving each player.

What I'm struggling with is the following method.

static bool RocketInSquare(int squareNo)
       {
        //TODO: write a method that checks through the 
        //rocket positions and returns true if there is a rocket in the given square
       }

The method needs to check for collisions in the array. So if player (A) rolled a 1 on first roll and player (B) rolls a 1 on first roll i need to make player(B) 'leapfrog' player (A) to go to square 2.

AT the moment the game is just running in the console if that helps. Sorry about the format of this question, never asked on here before.

Many thanks

1 Answer 1

1

Well you need to simply check if both players share the same position, and if that is the case the "active player" is allowed to move one more

if(playerpositions[otherPlayer] == playerpositions[currentPlayer])
    playerpositions[currentPlayer]++;

So if you need to make a function for that it would be:

static bool RocketInSquare(int squareNo)
{
    return playerpositions[0] == squareNo ||
           playerpositions[1] == squareNo ||
           playerpositions[2] == squareNo ||
           playerpositions[3] == squareNo;
}

And then

int dice = RollDice();

if(RocketInSquare(playerPositions[playerNo] + dice))
{
    playerPositions[playerNo] += dice +1;
}
else
{
    playerPositions[playerNo] += dice;
}
3
  • Ok so i have int currentplayer = playerNo I need to make checks for up to 3 other players, Would i have to make a separate variable for each player? Cheers for the reply Nov 1, 2012 at 17:33
  • Ok that's brilliant. Cheers for your help mate Nov 1, 2012 at 17:52
  • Keep an eye on the RocketInSquare Function, I had a typo in the Playernumbers. And you are welcome.
    – CSharpie
    Nov 1, 2012 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.