Shay Subramanian
Shay Subramanian

Reputation: 29

Matlab 'count to 10' basic game exercise?

I received this question and am at a standstill. This is an intro course so we should really only be using basics and while/for/if-else loops. Here is the exercise:

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.

Here is the sample run given in the assignment, and what I have so far.

I have now gotten a little further as seen here:

However I don't understand how to make it alternate between Player 1 and 2, or how to display the winner. All it does is add the value and ask again at this point.

Upvotes: 0

Views: 750

Answers (1)

athingunique
athingunique

Reputation: 151

I have some working code, but if this is a homework assignment you really should try your best to solve it yourself. I will give you a hint though:
If you have a variable called player that is either 1 or 2, then

player = 2 / player;

will always give you the other number; if player = 1, after that, player = 2, then player = 1, ...etc.

I would also suggest that you check the input immediately, instead of how you're doing it:

while (~(choice == 1 | choice == 2))
    choice = input('Enter 1 or 2: ');
end

Once you have something working I will be happy to share my full code if you would like it.

Upvotes: 1

Related Questions