开发者

C# Dice rolling

开发者 https://www.devze.com 2023-01-20 11:15 出处:网络
im trying to get aC# programming on using 5 dice. Two player gets three throws. He has to get a 6, a 5 and a 4, in that开发者_如何学JAVA order, before he can score. As he throws each of these special

im trying to get a C# programming on using 5 dice. Two player gets three throws. He has to get a 6, a 5 and a 4, in that开发者_如何学JAVA order, before he can score. As he throws each of these special numbers, those dice are removed from play. The total of the two dice left over is his point. Computer has to beat that. The dice has to be built in order. That is, the player must throw the 6 before or at the same time as the 5 and the 5 before or at the same time as the 4.

How do i get it to recognise the dice of 6,5,4 is read and how do i minimise the number of dice after it has met its condtion?


Sounds like an excercise to learn about finite state machines.

The basic idea is that your "machine" starts in some state, let's call it "START".

You get to roll the dice three times in an attempt to achieve a score, so you'll also have to keep track of how many times the dice have been rolled.

After each roll, you examine the dice and, depending on the current state and what was rolled, your "machine" may transition to a new state.

You stated that you have to roll a 6, a 5 and a 4 in order (this hints to how states will transition) or during the same roll (telling you that states can be skipped).

So what are these other states? Since you have to roll a 6 then a 5 and then a 4, let's just call the states, "SIX", "FIVE" and "FOUR".

In psuedo-code:

int STATE_START = 6;
int STATE_SIX = 5;
int STATE_FIVE = 4;
int STATE_FOUR = 3;

int state = STATE_START;
int rolls = 0;
int[] dice
int score = 0;

do (while state != STATE_FOUR && rolls++ < 4) {
    dice = rollDice();

    do (while state != STATE_FOUR) {
        if (dice.Contains(state)) {
            state--;
        }
        else {
            break;
        }
    }
}

if (state == STATE_FOUR) {
    score = getScore(dice);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消