E. Final Program with Comments

%FINAL SNAKES N LADDERS CODE  

 

clc 

clear 

close all 

 

%SDP Final Project 

fprintf(‘\n************************************************\n’) 

fprintf(‘*  Name: Ava Lacy, Evan Leist, David Katsman, Adriana Giordano   Date:  11/12/2020    *\n’) 

fprintf(‘*  Seat/Table:  k    File: SDP_Final_Project.m     *\n’) 

fprintf(‘*  Instructor:Jessica Thomas HH:MM                      *\n’)                       

fprintf(‘************************************************\n’) 

%Snakes and Ladders 

 

%welcoming to the game 

fprintf(‘This is a two player game. Grab a partner!\n’) 

 

%defining snakes and ladder spaces 

snakeSpaceone=16; 

snakeSpacetwo=47; 

snakeSpacethree=87; 

snakeSpacefour=56; 

snakeSpacefive=98; 

 

ladderSpaceone=4; 

ladderSpacetwo=28; 

ladderSpacethree=36; 

ladderSpacefour=9; 

ladderSpacefive=1; 

 

%defining turn values and confirming start of game 

lastTurn=input(‘Welcome to Snakes and Ladders! Enter 0 to start:’) 

roll = randi(6,1,2) 

rollTotal=roll(1)+roll(2) 

rollTurn=lastTurn+rollTotal 

 

%starting loop for player 1 and 2’s turns 

while rollTurn<100 

%player 1 turn 

lastTurn=input(‘Player 1: Enter rollTurn from last round. If this is your first turn, enter 0:’) 

 

 

%if player one lands on snake space, they go down a certain amount 

     

if rollTurn == snakeSpaceone 

    rollTurn-10 

    disp(‘You hit a snake! You slid down three spaces’) 

elseif rollTurn == snakeSpacetwo 

    rollTurn-21 

    disp(‘You hit a snake! You slid down three spaces’) 

elseif rollTurn == snakeSpacethree 

    rollTurn-63 

   disp(‘You hit a snake! You slid down three spaces’) 

elseif rollTurn == snakeSpacefour 

    rollTurn-3 

    disp(‘You hit a snake! You slid down three spaces’) 

elseif rollTurn == snakeSpacefive 

    rollTurn-20 

     disp(‘You hit a snake! You slid down three spaces’) 

 

%if player one lands on ladder space, they go up a certain amount 

elseif rollTurn == ladderSpaceone 

    rollTurn+10 

    disp(‘You hit a ladder! You climb three spaces’) 

elseif rollTurn == ladderSpacetwo 

    rollTurn+56 

    disp(‘You hit a ladder! You climb three spaces’) 

elseif rollTurn == ladderSpacethree 

    rollTurn+8 

    disp(‘You hit a ladder! You climb three spaces’) 

elseif rollTurn == ladderSpacefour 

    rollTurn+22 

    disp(‘You hit a ladder! You climb three spaces’) 

elseif rollTurn == ladderSpacefive 

    rollTurn+37 

    disp(‘You hit a ladder! You climb three spaces’) 

     

     

    %if you go greater than 100, you win 

elseif rollTurn >= 100 

    disp(‘You reached the end! Player 1 wins’) 

elseif rollTurn>100 

%     rollTurn=rollTotal-lastTurn 

else  

   rollTurn=rollTurn 

end 

 

%player 2 turn 

lastTurn=input(‘Player 2: Enter rollTurn from last round. If this is your first turn, enter 0:’) 

roll = randi(6,1,2) 

rollTotal=roll(1)+roll(2) 

rollTurn=lastTurn+rollTotal 

 

 

%if player two lands on snake space, they go down a certain amount 

if rollTurn == snakeSpaceone 

    rollTurn-10 

    disp(‘You hit a snake! You slid down three spaces’) 

elseif rollTurn == snakeSpacetwo 

    rollTurn-21 

    disp(‘You hit a snake! You slid down three spaces’) 

elseif rollTurn == snakeSpacethree 

    rollTurn-63 

   disp(‘You hit a snake! You slid down three spaces’) 

elseif rollTurn == snakeSpacefour 

    rollTurn-3 

    disp(‘You hit a snake! You slid down three spaces’) 

elseif rollTurn == snakeSpacefive 

    rollTurn-20 

    disp(‘You hit a snake! You slid down three spaces’) 

     

     

    %if player two lands on ladder space, they go up a certain amount 

elseif rollTurn == ladderSpaceone 

    rollTurn+10 

    disp(‘You hit a ladder! You climb three spaces’) 

elseif rollTurn == ladderSpacetwo 

    rollTurn+56 

    disp(‘You hit a ladder! You climb three spaces’) 

elseif rollTurn == ladderSpacethree 

    rollTurn+8 

    disp(‘You hit a ladder! You climb three spaces’) 

elseif rollTurn == ladderSpacefour 

    rollTurn+22 

    disp(‘You hit a ladder! You climb three spaces’) 

elseif rollTurn == ladderSpacefive 

    rollTurn+37 

    disp(‘You hit a ladder! You climb three spaces’) 

     

     

     

    %if player two hits 100 or greater they win 

elseif rollTurn >= 100 

    disp(‘You reached the end! Player 2 wins’) 

elseif rollTurn>100 

%     rollTurn=rollTotal-lastTurn 

else  

   rollTurn=rollTurn 

end 

end