简体   繁体   中英

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. 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.

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.

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."

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. 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.)

Please help me! I've been tearing my hair out for 3 hours!

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. 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.

The winning condition was removed from inside the while, since once count exceeds 10 the loop ends. Then, the winner message is displayed.

Removed the clear instruction, since the two main variables are initialized at the start.

Also added the current count information when asking for the input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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