INTRO TO COMPUTERS FOR ENGINEERS FINAL PROJECT: MATLAB A Game of Sticks This pro
ID: 3832577 • Letter: I
Question
INTRO TO COMPUTERS FOR ENGINEERS FINAL PROJECT: MATLAB
A Game of Sticks
This project will require you to implement the Game of Sticks in MATLAB. The rules of the game are as follows. There are 2 players and 20 sticks. Players take turns sequentially. On each player’s turn, they must remove between 1 and 3 sticks. The object of the game is to not be the player to pick up the last stick. This project has the following deliverables: C: Your project implements a 2-player version of the game of sticks in the command window. B: You have a visualization of how many sticks there are, and an interface with buttons for players to interact with the game. A: Your game should have a “play the computer” option. The computer should follow all of the rules of the game, and implement some kind of viable strategy (not just make the same move every time).
My code so far is:
function [sticks]=prac(~)
promptmessage = sprintf('Would you like to play against a friend or the computer?');
button = questdlg('Would you like to play with a friend or the computer?','Game of Sticks','Friend','Computer','Friend');
sticks=20;
turn=0;
switch button
case 'Friend'
while sticks>=1
player=rem(turn,2)+1;
turn=turn+1;
promptMessage=sprintf('Sticks remaining: %d Player #%d Do you want to take 1, 2, or 3 sticks?',sticks,player);
button=questdlg(promptMessage,'Sticks','1','2','3','2');
if strcmpi(button,'1')
move=1;
elseif strcmpi(button,'2')
move=2;
elseif strcmpi(button,'3')
move=3;
end
fprintf('Player %d selected %d ',player,move);
sticks=sticks-move;
end
case 'Computer'
A = [1 2 3];
while A >= 0
option = randi(length(A));
fprintf('Computer selected %d sticks',option);
sticks = sticks-option;
break
end
end
if sticks<1
if rem(turn,2)>0
player=2;
else
player=1;
end
message=fprintf('Player %d wins! ',player);
fprintf('%s ',message);
end
With a separate random number generator:
function [option] = randomGen(~)
A = [1 2 3];
while A >= 0
option = randi(length(A));
break
end
disp(option)
And this is the separate code for the opening pop-up window:
promptmessage = sprintf('Would you like to play against a friend or the computer?');
button = questdlg('Would you like to play with a friend or the computer?','Game of Sticks','Friend','Computer','Friend');
function prac
function randomGen
switch button
case 'Friend'
prac
case 'Computer'
randomGen
end
end
End
Explanation / Answer
function [sticks]=prac(~)
promptmessage = sprintf('Would you like to play against a friend or the computer?');
button = questdlg('Would you like to play with a friend or the computer?','Game of Sticks','Friend','Computer','Friend');
sticks=20;
turn=0;
switch button
case 'Friend'
while sticks>=1
player=rem(turn,2)+1;
turn=turn+1;
promptMessage=sprintf('Sticks remaining: %d Player #%d Do you want to take 1, 2, or 3 sticks?',sticks,player);
button=questdlg(promptMessage,'Sticks','1','2','3','2');
if strcmpi(button,'1')
move=1;
elseif strcmpi(button,'2')
move=2;
elseif strcmpi(button,'3')
move=3;
end
fprintf('Player %d selected %d ',player,move);
sticks=sticks-move;
end
case 'Computer'
A = [1 2 3];
while A >= 0
option = randi(length(A));
fprintf('Computer selected %d sticks',option);
sticks = sticks-option;
break
end
end
if sticks<1
if rem(turn,2)>0
player=2;
else
player=1;
end
message=fprintf('Player %d wins! ',player);
fprintf('%s ',message);
end
function [option] = randomGen(~)
A = [1 2 3];
while A >= 0
option = randi(length(A));
break
end
disp(option)
promptmessage = sprintf('Would you like to play against a friend or the computer?');
button = questdlg('Would you like to play with a friend or the computer?','Game of Sticks','Friend','Computer','Friend');
function prac
function randomGen
switch button
case 'Friend'
prac
case 'Computer'
randomGen
end
end
End
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.