mod07 Snakes and Ladders   Not on the Board   Part 04

mod07 Snakes and Ladders Not on the Board Part 04


SNAKES AND LADDERS: NOT ON THE BOARD 04 Alright guys, in the previous video you would
have seen the method of show board the functionality I had mentioned here, I had written the code
board for functionality so that the board is shown to the player now what is pending
is the play functionality, let us see what is needed in play functionality ok I had commander
it back then because I want you to show that show board functionality works so now play
is require so I will remove the ash so that its now no longer a command and now we want
play functionality to find play here as I said in this game we having two players p
one and p two let us save them p one will have some name because we want to have it
as personalised interface, it should not just be like player one player two he had got a
sweet name, we need to refer to him by it by his name then he will be very very happy
so we need to refer to him by his name and lets input his name from him lets input his
name input is taken by this functionality raw input we are inputting a string so it
is taken by raw input, string is nothing but a set of characters names generally consists
if characters right so that is what technically we call it as string this is a string so we
use raw input functionality to input strings so this is the message that has to be displayed.
what should we be displaying? Maybe we shall say player one please enter your name looks
good alright the same has to be done to player two as well, instead of typing it again let
me copy and paste this and do the modifications wherever needed, it is player two’s name
and it is player two so the first would be inputting the player one’s name and then
inputting player two’s name so let me command that as well for completion sake input player
one name this is input player two name why do we have commands is? To help us understand
the code at later point of time. Maybe when you are doing this now maybe you will find
it later easier but once you have done sufficient number of exercises and when you want to look
back at what you had done previously that time there may be some confusions so to avoid
that we have commands where we write in English format that is understandable by us we can
look back to the commands and then get to know the syntax that has been used and all
that, it would be easier for our revision if needed so that is why we are using commands
alright, so initially both are starting at that is they don’t have their, they have
their dice placed on the floor not on the board so they will roll the dice based on
what the number appears they will place it on the snake and ladder board so ground let
me denote it by point zero player one has points zero this is nothing but I will command
this as well initial points of player one, player one has got point zero alright? Ok
the same as to be repeated for player two so let me copy and paste it this is for player
two so I will just give a summary of this four lines we input player ones name the same
we do it for player two then we have set the initial points of both the players to zero
and there will be one variable it will denote whose turn it is, let me initialise it with
zero, you can initialise it one as well but in computers generally the counting starts
from zero so I have done it with zero but it is not mistake even if you start from one
so the game will be played continuously so let me say while one this is nothing but an
infinite loop the game would be played continuously so I have put an infinite loop here I guess
most of you would have some confusion here that I said when you reach the end points
the game ends but here I am saying the game will be played continuously how is that possible?
You would have it is not contradicting this is a doubt you may have so it will become
clear as the code proceeds there is a check inside whether the end points has been reached
or not if end point has been reached then we will break this loop, break this loop is
nothing but we come out of the game that is what we call as break this loop this is a
infinite loop that will be broken that is the game would end when you reach the end
point the check would be perform at a later point alright so turn zero is for player one
so I would check turn zero is for player one turn one then I will increase the value of
turn by one and I will increment if by one so turn will become one turn one will be for
player two turn two will be again for player one turn three will be again for player two
so every turn is alternated so if you would observe the pattern here turn zero two four
six and so on for player one and turn one three five seven so on are for player two
this could be abstracted from this way that to take the value of turn you divide it by
two and take the reminder if that is zero that is the turn of player one otherwise it
is the turn of player two so to check the reminder we have the operator mod which is
denoted by percentage we will be using that now if turn mod two equals equal zero note
that single equal to is assignment it is nothing but you are assigning the value as zeros let
the value be anything I don’t care, I want it to be zero then you have to use a single
equal to here you have to check the equality I don’t want to change the value I want
to fetch that value I want to compare it with some standard value in that case you have
to use the double equal to symbol please note this point ok if that is the if this is the
turn then this is player ones turn player ones turn this is alright this has to be notified
to the player so I will print a message player ones name his name has to be mentioned I will
say your turn player one this is your turn a message has been put and I had said the
player may want to quit the game in the middle as well we have to give him this options till
you reach the points hundred I don’t want to I want to quit it even that option will
have to be given to him for that matlab this will give more personalised feeling that is
why we are giving this option as well that I will take it as his choice so c nothing
but ask players approval to continue asks players wish, players choice let me say that
as players choice to continue this is what will be doing next let me call this as c,
c for choice or whether he wants to continue you can take it anyway I will just call this
c or you can name it as choice as well that is up to you I am using c input see that was
the function I was used previously the raw input that was a specialised function to input
strings nothing but something like name or your address something like that to input
that you will use raw input here I will ask one or zero one will denote he wants to continue
zero denotes he want to quit the game so I would input his choice I will have to say
him press one to continue and zero to quit so I will get his input if his choice is zero
if the choice is zero I will have to the scores and I will have to quit the game so let me
show the scores the points is stored in pp one and pp two, pp one is the points of player
one, pp two is points of player two. First the player ones name has to be mentioned player
ones name quote here the points this has to be mentioned and the same thing has to be
done for player two as well so I am copying and pasting it player two points of player
two this is done, you have to mention the points and you have to say quitting the game
quitting the game thanks for playing something like that just a humble message, a message
has been displayed and now we have to come out of the loop that is this all these are
for the user to give a personalised experience to the user we are using all these print statements
but what is the instruction to be given to the computer to get out of the loop is this
statement break, break statement will come out of the loop and it will end the game basically
that’s what this will do here and otherwise if choice is zero this would be executed if
not it would just come here observe the indents, indentation is important here it is in the
same level of if so if c equals equals to zero this check would be made if the user
hasn’t pressed zero that is he doesn’t want to quit it would come directly to line
number forty here which is in the same indent level here you are suppose to roll a dice
let me when you roll a dice you would get some number that let me store it in a variable
called dice so to roll a dice to stimulate this what happens is what happens when you
roll a dice some random number from one to six something from one to six would come when
you roll a dice so I to stimulate this what should I give the instruction to the computer
is you pick a random number from one to six to give this instruction I have to use this
random package random library has to be imported I will say import random this is the library
that is needed since we needed it we imported it here now I will say random dot the functionality
is randint this is the functionality randint this requires few to give the end points that
is what should be the start value and what should be the end value, the start value is
one in our case and end value is six, this particular functionality would generate a
random number from one two three four five six this is what this functionality is doing,
you generate a random number this is basically stimulation of rolling a dice alright we have
generated a random number now you are suppose to display what was showed by the dice, print
dice showed dice showed the points that is captured by this variables dice so dice showed
this point is printed to the user and now once this is once the dice has been rolled
you have to add that many number of points to your already, already the points whatever
you have to that you must add this points so let us do that. P one this variable p p
one stores the points of player one this is nothing whatever is the pint take it you add
the value of dice so that will be your new points and when you are playing the snake
and ladder sometimes the it may happen that when you reach some position there may be
a snake or they may be a ladder so accordingly your points will increase or decrease the
change has to occur so to stimulate that let us say the points one is nothing but you check
if there is a ladder at this particular points if you think why do I pass the same parameter
here and here is this particular line what it does is it will add the points add the
points here I guess the name would clarifies so commands is not needed but why do we pass
this here and here I will explain, this is the place where you have landed after rolling
the dice now you are suppose to check if that particular position has got a ladder if that
has got a ladder your points this particular value of this particular value of pp one the
points is no longer valid some higher points you would get so that higher value of points
would be stored here so that is why we are using same variable pp one in both the cases
I hope it is clear in it will be clear when we deal deep into this particular functionality
check ladder this may be a ladder a positive thing which will increase your points or it
may happened that it is a snake check if it is a snake of the same the same way check
ladder and check snake are two functionalities which we will define later these are the things
that we have to do and sometimes it may happen that in some points you are all at say ninety
five you are at position ninety five and your dice showed six so when you add here ninety
five plus six is one not one but your dice has board has just hundred so what will you
do then you will stop at hundred and you would say that you have won so that particular check
has to be done here so this is I am sorry for this, this is the same players player
ones points has to be changed I am sorry for that and here as I had said you have to check
in case during this addition process have you gone beyond the end point so in that case
you have to come back to the end point so that check let us make, if the points, points
of player one has exceeded end, end point then you are suppose to say you bring it to
end point that is ninety five he was present, six came up so he is supposed to go to one
not one since the board ended at hundred you stop at hundred that is what we are stimulating
here, this is to the functionality here is to check if the player goes beyond the board
beyond the board this is being check here alright? Now you are suppose to print him
his score as of now you say his name player one name your score your score is this value
pp one that is storing the points of player one alright, if he has reached the end points
as I have said earlier this is an infinite loop you may wonder that when you have reach
the end point you have to quit the game but here we have given an infinite loop now to
break this loop we are performing a particular check if you have reached the end, if you
have reached the end with this current points that is whether this particular current points
is the end point or not here checking if the person has reached the end I am sorry I have
used the extra bracket its seems sorry if it’s just a space if this if this particular
player has reached the end you are suppose to print the message player one name his name
you are supposed to give and won player one this person has won the game you are suppose
to display it and once you had displayed it you come out of the loop you break that this
particular place we are using break because the user wants to quit the game here we are
breaking that is here we are quitting the game because one person has reached the end
point this is the check for player one this is a player ones turn anything maybe possible
right player one may reach the end point player two may reach the end point so we will perform
all this checks in player two as well alright let get back to this if turn then else this
all this whatever we had done are for player one let us the same as to be repeated for
player two so let us copy paste everything copy ok let me paste it this is for player
two player two you will modify everything to player two player two sorry this is player
one I am sorry for this because here the player wants to quit and we are suppose to display
both their points this is player one and player two and now here it is player two is player
so his points are added so layer two player two alright player two player two player two
player two player two player two layer two player two alright so we are given rough overview
of the game let us save this up to here I will give you the summary of what all just
happened till now we have inputted the name of two players we have initially set their
points to zero and we have some variable called turn which will actually keep track of who
has to play now and the turns zero two four six and so on are for player one, one three
five seven and so on for player two to keep track of that we are using this particular
operator modulo denoted by percentage which will return the reminder after the divisions
so we are dividing it by two if the reminder turns out to be zero it is player ones turn
otherwise it is player two turn so one thing I guess I skipped here is I skipper here is
I should have should have incremented the turn, yeah I should increment the turn right?
Once one player has done I should give the chance to the next person so for that I need
to increment the turn that I have to do I have done that alright so what in each players
turn what are the checks that are being performed let me tell you that, first we say that it
is his turn we are asking if he wants to continue or quit if he is quitting we specify the status
till now as to how many each person has scored and we are quitting the game, in case he wants
to continue we are rolling the dice, dice would give you a number from one to six some
random number that number is added to his points and then it is possible that, that
particular position there may be a snake or a ladder we will be performing a check there
if that is a snake or ladder and appropriately the points would be changed and in case during
the process of rolling a dice the points exceeded the end point as I had given an example earlier
if the position was ninety five and the dice showed have six addition would give one not
one where as your board just as hundred so end point exceeded so in that case we should
bring back to end point so this check has been performed here and after that and after
performing all the check after performing the normalisation part we are displaying his
score and we are checking if that particular position is the end point or not if the person
has reached the end point is declared that this person has won and here we quit the game,
the quitting here is due to the users choice quitting here is due to reaching of end point
that is the difference here the same thing is done for player two as well alright so
the higher level overview of what play does is given to you and check snake check ladder
reached end these are the three functionalities we have defined here that is we have we have
just given the names here what this particular functionality is suppose to do we will define
later we will be defining in next few minutes let’s see to it. I hope you have understood
what all happened till now you may pause for sometime look back to what has happened, you
proceed once you got all the pieces till now clear.

Leave a Reply

Your email address will not be published. Required fields are marked *