简体   繁体   English

如何将我的简单游戏减少到18行MATLab代码

[英]How do I reduce my simple game to 18 lines of MATLab code

I have recently been tasked with reducing a simple game written in MATLab code to a maximum of 18 lines of code. 我最近的任务是将用MATLab代码编写的简单游戏减少到最多18行代码。 This can be done through getting rid of redundancies but I am having trouble distinguishing them so far. 这可以通过消除冗余来完成,但是到目前为止,我很难区分它们。 My code currently runs to 24 lines. 我的代码当前运行到24行。

The requirements for the game are as follows: 游戏要求如下:

"In this exercise you are required to implement a simple two player counting game. The game starts by setting the count to 0. The two players should take alternative turns selecting between the numbers 1 and 2. In each turn the number the current player selects gets added to the count. The player who reaches a value 10 or larger wins the game. The program should check for input correctness. The image below has a sample run. “在本练习中,您需要实现一个简单的两人计数游戏。该游戏首先将计数设置为0。这两名玩家应轮流在数字1和2之间进行选择。在每一轮中,当前玩家选择的数字达到10或更大的值的玩家将赢得比赛,程序应检查输入的正确性,下图显示了示例。

Note: You must make the program a maximum of 18 lines of code but you're not allowed to achieve this goal by putting multiple statements/commands on the same line." 注意:您必须使程序最多包含18行代码,但不允许在同一行上放置多个语句/命令来实现此目标。”

One more thing: I have noticed that this part of my code... 还有一件事:我注意到我的代码的这一部分...

if count >= 10
    disp('Player x wins!');
end

... is positioned improperly because even if player 1 wins and the game exclaims it to be so, Player 2 will still be asked to input a number. ...位置不正确,因为即使玩家1获胜并且游戏大声疾呼如此,玩家2仍会被要求输入数字。 I'm not sure how to fix this either. 我也不知道该如何解决。 Sorry! 抱歉! (I'm sure that changing this code somehow will help in reducing the grand line total down to 18.) (我确信以某种方式更改此代码将有助于将总行数减少到18。)

Please help me! 请帮我! I've been tearing my hair out for 3 hours! 我已经把头发扯了3个小时了!

Thank you! 谢谢!

clear
clc
count = 0;
while count < 10;
    oneOrTwo = input('Player 1\nEnter 1 or 2: '); % Player 1 choose
    while oneOrTwo ~= 1 && oneOrTwo ~= 2
        oneOrTwo = input('Incorrect input. Try again: ');
    end
    count = count + oneOrTwo;
    if count >= 10
        disp('Player 1 wins!');
    end
    oneOrTwo = input('Player 2\nEnter 1 or 2: '); % Player 2 choose
    while oneOrTwo ~= 1 && oneOrTwo ~= 2
        oneOrTwo = input('Incorrect input. Try again: ');
    end
    count = count + oneOrTwo;
    if count >= 10
        disp('Player 2 wins!');
    end
end % This currently ends at line 24

I managed to get it down to 12 lines of code. 我设法将其简化为12行代码。 Try this: 尝试这个:

clc
count = 0;
player = 1;  % 0: player 1,  1: player 2
while count < 10;
    player = mod(player+1,2);
    oneOrTwo = input(['Count: ' num2str(count) '\nPlayer ' num2str(player+1) '\nEnter 1 or 2: ']);
    while oneOrTwo ~= 1 && oneOrTwo ~= 2
        oneOrTwo = input('Incorrect input. Try again: ');
    end
    count = count + oneOrTwo;
end
disp(['Player ' num2str(player+1) ' wins!']);

As you can see, the input process is compressed into one by using the player variable. 如您所见,输入过程通过使用player变量压缩为一个。

The winning condition was removed from inside the while, since once count exceeds 10 the loop ends. 从那一刻开始就取消了获胜条件,因为一旦count超过10,循环便结束了。 Then, the winner message is displayed. 然后,显示中奖者消息。

Removed the clear instruction, since the two main variables are initialized at the start. 删除了clear指令,因为两个主要变量均在开始时进行了初始化。

Also added the current count information when asking for the input. 要求输入时还添加了当前count信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM