Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MATLAB中用骰子模拟一个有趣的游戏_Matlab - Fatal编程技术网

在MATLAB中用骰子模拟一个有趣的游戏

在MATLAB中用骰子模拟一个有趣的游戏,matlab,Matlab,我正在尝试编写模拟以下游戏的代码: 掷两个骰子,让x为它们的总和。如果x=7或x=11,则您获胜否则,再次掷两个骰子,直到你得到x(在这种情况下你赢了)或7或11(在这种情况下你输了) 假设你有一个两个骰子,如果在第一次掷骰子时你得到的是7或11的总和,你赢了,如果没有,那么总和可以是另一个数字,比如说9,那么你必须继续掷两个骰子,直到你再次得到9,如果在这个过程中你又得到7或11,那么你就输了:) 然后我编写了以下代码: d1=floor(6*rand)+1; d2=floor

我正在尝试编写模拟以下游戏的代码:

掷两个骰子,让x为它们的总和。如果x=7或x=11,则您获胜否则,再次掷两个骰子,直到你得到x(在这种情况下你赢了)或7或11(在这种情况下你输了)


假设你有一个两个骰子,如果在第一次掷骰子时你得到的是7或11的总和,你赢了,如果没有,那么总和可以是另一个数字,比如说9,那么你必须继续掷两个骰子,直到你再次得到9,如果在这个过程中你又得到7或11,那么你就输了:)

然后我编写了以下代码:

    d1=floor(6*rand)+1;
    d2=floor(6*rand)+1;
    x=d1+d2;

    while x~=7 & x~=11
    d1=floor(6*rand)+1;
        d2=floor(6*rand)+1;
        x=d1+d2;
        if x==x
            disp('You win')
        elseif x==7 | x==11
            disp('You loose')
        end
    end
但是它无限期地显示“youwin”这句话,并且无法阻止它,我知道这是因为条件“x==x”总是计算为true,但是我如何修复它以进行模拟呢

提前多谢

d1=floor(6*rand)+1;
d2=floor(6*rand)+1;
x=d1+d2;

kk=1;
while x~=7 && x~=11
    d1=floor(6*rand)+1;
    d2=floor(6*rand)+1;
    if kk ==1
        tmp = d1+d2;
    end
    x=d1+d2;
    if x==7 || x==11 && kk == 1
        disp('You win')
    elseif x==7 || x==11 && kk > 1
        disp('You loose')
    elseif x==tmp && kk>1
        disp('You win')
    else
        disp('You loose')  
    end
    kk = kk+1;
end
发生的事情是,我引入了
kk
,它可以被认为是迭代计数器。在获胜条件后,将其作为额外条件添加
if
语句然后包含您设置的所有条件,请注意,如果
kk==1
我将总和保存在
tmp
中,我将根据它检查第二个赢条件

试运行:

You loose
You loose
You loose
You win
发生的事情是,我引入了
kk
,它可以被认为是迭代计数器。在获胜条件后,将其作为额外条件添加
if
语句然后包含您设置的所有条件,请注意,如果
kk==1
我将总和保存在
tmp
中,我将根据它检查第二个赢条件

试运行:

You loose
You loose
You loose
You win

你想重新掷骰子直到你赢吗?在这种情况下,您希望在while循环中执行重新滚动。反复检查同一个数字是没有用的

x=0;
wins=[7 11];
while min(x~=wins)
    d1=floor(6*rand)+1;
    d2=floor(6*rand)+1;
    x=d1+d2
    if max(x==wins)
        disp('You win')
    else
        disp('You loose')
    end
end

你想重新掷骰子直到你赢吗?在这种情况下,您希望在while循环中执行重新滚动。反复检查同一个数字是没有用的

x=0;
wins=[7 11];
while min(x~=wins)
    d1=floor(6*rand)+1;
    d2=floor(6*rand)+1;
    x=d1+d2
    if max(x==wins)
        disp('You win')
    else
        disp('You loose')
    end
end
这应该起作用:

x=0;

while x~=7 | x~=11
    d1=floor(6*rand)+1;
    d2=floor(6*rand)+1;
    x=d1+d2;
    if x==7 | x==11
        disp(x)
        disp('You win')
        break
    else
        disp(x)
        disp('You loose')
        a = input('You want roll again (y/n)? ','s')
        if strcmpi(a,'y')
        else
          break
       end
    end
end
这应该起作用:

x=0;

while x~=7 | x~=11
    d1=floor(6*rand)+1;
    d2=floor(6*rand)+1;
    x=d1+d2;
    if x==7 | x==11
        disp(x)
        disp('You win')
        break
    else
        disp(x)
        disp('You loose')
        a = input('You want roll again (y/n)? ','s')
        if strcmpi(a,'y')
        else
          break
       end
    end
end

因为您在第一轮后更改了规则,所以您可以使用
if
语句指定第一轮的规则,并在
else
语句中指定游戏的其余部分。此外,您需要在代码中指定值
X
。这里我以5为例。如果第一轮后没有结果,您可以使用代码
d1=floor(6*rand)+1再次玩骰子;d2=楼层(6*rand)+1;x=d1+d2,然后确定您是赢还是输。如果仍然没有结果,您将使用while循环来保持此状态,直到出现赢或输的情况

d1=floor(6*rand)+1;
d2=floor(6*rand)+1;
x=d1+d2;
first_sum = x    % use another variable first_sum to hold the status of first roll

if x==7 || x==11
    disp('You win')
else
    d1=floor(6*rand)+1;
    d2=floor(6*rand)+1;
    x=d1+d2;
    if x== 7 || x==11
        disp('You lose')
    elseif x == first_sum
        disp('You win')
    else
        while x ~= first_sum && x~=11 && x~=7
            d1=floor(6*rand)+1;
            d2=floor(6*rand)+1;
            x=d1+d2;
            if x== 7 || x==11
                disp('You lose')
            elseif x == first_sum
                disp('You win')
            end
        end
    end
end
或者你可以用更简洁的方式写。由于else语句包含类似于
do while
循环的结构(意思是至少执行一次并在满足特定条件时继续执行),因此您也可以这样对其进行编码:

d1=floor(6*rand)+1;
d2=floor(6*rand)+1;
x=d1+d2;
first_sum = x

if x==7 || x==11
    disp('You win')
else
    flag = true
    while flag    % The while loop will keep going until flag is set to false.
        d1=floor(6*rand)+1;
        d2=floor(6*rand)+1;
        x=d1+d2;
        if x== 7 || x==11
            disp('You lose')
            flag = false   % If a result is reached, stop the while loop
        elseif x == first_sum
            disp('You win')
            flag = false
        end
    end
end

因为您在第一轮后更改了规则,所以您可以使用
if
语句指定第一轮的规则,并在
else
语句中指定游戏的其余部分。此外,您需要在代码中指定值
X
。这里我以5为例。如果第一轮后没有结果,您可以使用代码
d1=floor(6*rand)+1再次玩骰子;d2=楼层(6*rand)+1;x=d1+d2,然后确定您是赢还是输。如果仍然没有结果,您将使用while循环来保持此状态,直到出现赢或输的情况

d1=floor(6*rand)+1;
d2=floor(6*rand)+1;
x=d1+d2;
first_sum = x    % use another variable first_sum to hold the status of first roll

if x==7 || x==11
    disp('You win')
else
    d1=floor(6*rand)+1;
    d2=floor(6*rand)+1;
    x=d1+d2;
    if x== 7 || x==11
        disp('You lose')
    elseif x == first_sum
        disp('You win')
    else
        while x ~= first_sum && x~=11 && x~=7
            d1=floor(6*rand)+1;
            d2=floor(6*rand)+1;
            x=d1+d2;
            if x== 7 || x==11
                disp('You lose')
            elseif x == first_sum
                disp('You win')
            end
        end
    end
end
或者你可以用更简洁的方式写。由于else语句包含类似于
do while
循环的结构(意思是至少执行一次并在满足特定条件时继续执行),因此您也可以这样对其进行编码:

d1=floor(6*rand)+1;
d2=floor(6*rand)+1;
x=d1+d2;
first_sum = x

if x==7 || x==11
    disp('You win')
else
    flag = true
    while flag    % The while loop will keep going until flag is set to false.
        d1=floor(6*rand)+1;
        d2=floor(6*rand)+1;
        x=d1+d2;
        if x== 7 || x==11
            disp('You lose')
            flag = false   % If a result is reached, stop the while loop
        elseif x == first_sum
            disp('You win')
            flag = false
        end
    end
end

添加了更多的游戏氛围,也添加了评论,让你明白我在做什么

win1 = 7; %these values give you a win
win2 = 11;
lose1 = 0; %can't lose on first roll. this will be set later
lose2 = 0;

streak = 0;

%while game is still going on..
while true
    newgame = input('Would you like to start a new game? Type YES or NO  --> ','s'); %user inputs string
    if strcmpi(newgame,'YES') %did user type YES?
        game = true;  % set games state to true to begin the game loop
        firstroll=true; %first roll coming up
    elseif strcmpi(newgame,'NO')
        break; % end program by exiting outer loop
    else %user didn't enter correctly so re-ask
        continue;
    end

    while game
        input('Press enter to roll dice'); %roll the dice
        d1 = floor(6*rand)+1;
        d2 = floor(6*rand)+1;

        disp('You rolled:'); %display the dice
        disp(d1);
        disp(d2);
        x=d1+d2;

        if x==win1 || x==win2  % check for a win
            streak=streak+1; %%add a win to your total
            disp('WINNER!')
        elseif x==lose1 || x==lose2
            disp('YOU LOST!')
            disp('streak:') %show how many games you won
            disp(streak)
            game = false; % end game state because you lost

            %reset winning and losing numbers
            win1=7;
            win2=11;
            lose1=0;
            lose2=0;
            streak = 0; %reset streak
        elseif firstroll   %ok, didn't win on the FIRST ROLL so lets set the NEW winning value and reroll
            win1 = x;  % we win if we roll the same value again
            win2 = x;
            lose1 = 7; % we lose if we roll a 7 or 11
            lose2 = 11;
            firstroll = false; % set firstroll false so it doesn't keep resetting the winning and losing values
            continue;
        end
    end
end
disp('Thanks for playing!')

添加了更多的游戏氛围,也添加了评论,让你明白我在做什么

win1 = 7; %these values give you a win
win2 = 11;
lose1 = 0; %can't lose on first roll. this will be set later
lose2 = 0;

streak = 0;

%while game is still going on..
while true
    newgame = input('Would you like to start a new game? Type YES or NO  --> ','s'); %user inputs string
    if strcmpi(newgame,'YES') %did user type YES?
        game = true;  % set games state to true to begin the game loop
        firstroll=true; %first roll coming up
    elseif strcmpi(newgame,'NO')
        break; % end program by exiting outer loop
    else %user didn't enter correctly so re-ask
        continue;
    end

    while game
        input('Press enter to roll dice'); %roll the dice
        d1 = floor(6*rand)+1;
        d2 = floor(6*rand)+1;

        disp('You rolled:'); %display the dice
        disp(d1);
        disp(d2);
        x=d1+d2;

        if x==win1 || x==win2  % check for a win
            streak=streak+1; %%add a win to your total
            disp('WINNER!')
        elseif x==lose1 || x==lose2
            disp('YOU LOST!')
            disp('streak:') %show how many games you won
            disp(streak)
            game = false; % end game state because you lost

            %reset winning and losing numbers
            win1=7;
            win2=11;
            lose1=0;
            lose2=0;
            streak = 0; %reset streak
        elseif firstroll   %ok, didn't win on the FIRST ROLL so lets set the NEW winning value and reroll
            win1 = x;  % we win if we roll the same value again
            win2 = x;
            lose1 = 7; % we lose if we roll a 7 or 11
            lose2 = 11;
            firstroll = false; % set firstroll false so it doesn't keep resetting the winning and losing values
            continue;
        end
    end
end
disp('Thanks for playing!')

在其他问题中,您需要在每个循环迭代中重新掷骰子。对,但我不知道如何做到:),还有什么其他问题?我编辑了我的帖子:)您认为如何?在其他问题中,您需要在每个循环迭代中重新掷骰子。对,但我不知道如何做到:),还有其他问题吗?我编辑了我的帖子:)你觉得怎么样?非常感谢@Adriaann,但问题是如果你第一次掷骰子的总数是7或11,你就赢了,如果不是,你就再掷一次直到你最终选择第一个总数,如果你在这个过程中又得到了7或11,你就输了:)我不明白。我可以实现7/11部分,但我不知道你的意思,直到你最终选择第一个骰子。假设你有两个骰子,那么如果在第一轮中你得到的是7或11的总和,你赢了,如果没有,那么总和可以是其他数字,比如说9,那么你必须继续滚动两个骰子,直到你得到9,如果在这个过程中你又得到了7或11个错误:)Adriaan我很抱歉这个问题,但是当我试图运行你的代码时,MATLAB告诉我我还没有定义tmp:)发现了同样的问题;初始化
kk=1
(发生的事情是它的
kk=0
没有正确定义)非常感谢@Adriaann,但问题是,如果你第一次掷骰子的总数是7或11,你就赢了,如果不是,你就再掷一次,直到你最终选择第一个总数,如果你在这个过程中又得了7分或11分,你就输了:)我不明白。我可以实现7/11部分,但我不知道你的意思,直到你最终选择第一个骰子。假设你有两个骰子,那么如果在第一轮中你得到的是7或11的总和,你赢了,如果没有,那么总和可以是其他数字,比如说9,那么你必须继续滚动两个骰子,直到你得到9,如果在这个过程中你又得到了7或11个错误:)Adriaan我很抱歉这个问题,但是当我试图运行你的代码时,MATLAB告诉我我还没有定义tmp:)发现了同样的问题;初始化
kk=1
(实际情况是它的
kk=0
定义不正确