what's going on already welcome to part
14 of the Python 3 basics series where
we have been building a game of
tic-tac-toe in order to learn most of
the basics in Python so where we left
off we were kind of bringing everything
together we've got a game sort of
functioning but we still have to connect
like the win we've got some copied code
here in the win that we'd like to fix up
players can play over each other this is
hard coded we got to fix that stuff like
that so let's go ahead and just get
started so the first thing I think we'll
solve is players playing over each other
so the first thing is basically right
you know with this input we can just
quickly check the game map and make sure
hey is anything in that position because
there's something's in that position we
don't want to let someone play there so
all we're gonna say is if game map and
then it is a row and then column if game
map row column basically if that doesn't
equal 0 that means somebody has played
there so so we just want to return this
position is ocupado choose another
choose another ok and then we'll also
return a false and then we can kind of
make this entire thing if it doesn't
return a game map let's make it return
false so that's also add return false
here and return false here if we don't
do that basically if you enter anything
invalid or hit an error you're you lose
your turn which we you know that might
be a feature not a bug
but for now we'll return false and we'll
just continue giving them an opportunity
to fix it all so I'm gonna remove this
definition a game that doesn't need to
be there
cool all right so now this returns false
for a variety of reasons so let's we
need to handle for that as well so we've
got current player and we can print the
current player
now what we want to do basically we'll
come down here and then we're going to
ask a question of played and then we're
gonna say that's equal false and then
we're gonna say while not played we're
going to attempt to run this block of
code but then if game is true
so if game we're gonna say played equals
true so this should break us out of of
our little loop here and switch players
basically so let's go ahead and test
that really quickly so let's just run
this 14 cool
we'll play at 1 1 okay we switch to
player 2 it's like 1 1 again positions
occupied how about 0 1 bool object is
not scriptable played game bool objects
play players play played equals false so
while it's the case that we've not
played
you know I guess part of the issue here
like if this did return false later if
you tried to input see we are probably
getting in trouble really quickly
actually because if that returns false
the game map has been redefined and we
can't really let that happen now can we
so why don't we will have to change this
ever so slightly to be a return game we
need to return game map and the boolean
statement so let me just will do this so
game map false and then here will return
true and then here game map false game
map false that way now we come down here
and when we play the game game we'll
also get a return now in this case we
don't care about the return so in Python
if you don't care about one of the
things that's going to be returned you
use an underscore and the underscore
just means it doesn't matter basically
but then down here game and then played
equals the following so now I guess I
could just do this and now let's save
that let's rerun that and see how we did
so and I think and that's why it says
bool is is it's not scriptable I think
what was happening is it you know
returns false we try to play another
location and we're attempting to choose
an index in a boolean which obviously it
doesn't exist so 1 1 ok current player 2
1 1 we can't do that spot how about 0 1
okay so now player 2 can play at
position 0 1
okay I think that works we don't have
the winds all set up yet so we also need
to account for the win and we also kind
of want to consolidate our wins or win
here mmm basically we're always doing
the exact same thing in the win and like
this line now what happens if we stop
using zero as like the space like maybe
we give it a space we start formatting
things we use X's and O's and spaces
well if we do that then we got to come
through and change all these but what's
the chance that you know we're on this
screen and we're thinking oh there's
only three things we change one two
three save exit and darn it we forgot
this one right we're gonna be in trouble
so you generally want to consolidate
where you can and again anytime you've
got copied things it should just be a
red flag to you that something is
probably wrong so the next thing I want
to do is create a quick little function
in here mmm
organ cause define all same all same and
we pass a list and we determine is this
thing all the same so let's just copy
any one of these so L is short for a
list that's gonna be our parameter so if
that is the case we return true it's
true with a capital T else return false
and then rather than being row we want
it to be L and L and L okay so this is
our nice little function to determine
all same so then we just need to replace
this if all same and in this case it's
row that we're using now well let me
replace all of these and then we'll
we'll talk about something for a moment
and let me just make sure I get this
right so if and then this one is rather
than row its diag dye eggs and then down
here it's the same thing so I'm just
going to copy paste and then
here it's the same thing but this time
it is a check okay cool
so we also have you know this little
statement like this statement here is
pretty repetitive so you could maybe
also include the print statement here
and then pass a parameter of like what
type of win it was maybe that might be a
good idea I don't I'm not really sure
there because it's a string or we are
like it's just going to inherently be
pretty hard coded but you might want to
do like you could probably do that and
then you to eliminate four lines so
anyway there is that now what we want to
do is basically this function also needs
to we need to know is there a winner and
if there is a winner we want to also
return things so this is like a you know
this is a you know a function within the
function but here this function actually
doesn't return anything this returns
stuff but only did this parent function
but here this function actually only
just print stuff it doesn't actually do
anything so if there is a win we would
like to return true all the more reason
why you might want to put this in here
and then say who the winner is and then
maybe pass the I'm still trying to
decide if I want to do it I think I'm
gonna hold off but homework you should
do it it would make more sense we should
just we probably want to do that
but anyways for now let me just continue
this way return true I'd like to wrap up
we could spend forever on tic-tac-toe
and I definitely want to wrap it up
and I want to get you guys out of the
basic so I want you guys to go and do
your own thing so I'm gonna just say
that you probably should also pull that
in but the more important thing for us
to have fixed is this getting this to be
an all in one spot anyway return true
return true
we want to return true here yeah we
could save eight lines now anyway
and then at the end of this function
let's return a false so how does this
work
so basically if there is a win the
function returns true and whenever a
function return something that's it it's
not going to run any further in that
function so like if someone won both
horizontally and diagonally like you
just started the game like that it's not
gonna get you know it's only gonna find
the horizontal winner it wouldn't even
recognize it would never get to the code
to run diagonal but if this is not the
case none of these returns will return
you'll get to the end of the function
boom returns false which means nobody
won so this makes sense it makes sense
and it maps to name the name of the
function pretty well because win is
either false or if someone won win is
true okay so now what we want to do is
if someone has won so hat here while not
game one we play and we just continue to
play until the play actually works great
so then inside of this not game one this
is where we need to determine if there's
been a winner and all that so first of
all if win and we want a pass game if
there is a win to the game first of all
let's go ahead and game one equals true
that will that would basically end
everything for us
I'm not really sure I want to do that
immediately but the next thing we should
do is ask the player hey do you want to
play again and so again is this going to
be equal to inputs the game is over
would you like to play again and then
we'll just put yes/no in parentheses at
a space here and then we'll just ask you
know we'll try to discern what what did
they want so if again and maybe the user
had caps entered or whatever so I always
like to whenever I'm getting text input
I like to kind of normalize it to these
extent that
we just do lower and all this does is if
there's any uppercase letters it just
lowercases them you can do the exact
same thing with upper so these are just
methods that we can run against a string
but I'm gonna go with lower so if again
dot lower is equal to basically yes
we'll just print restarting because play
where's my mouse play is equal to true
so so this loop here is just gonna run
forever okay so if they said yes we just
restart and we're gonna pop all the way
cuz now this let's see this loop is over
with right this loop is over with so
then we come back up here we start with
a fresh game and the cycle continues
but if again dot lower is equal to n
what would we what we'd like to do well
they don't want to play anymore so by E
right and then we can say play equals
false mmm and then we're done now one
thing to take note with is if this is
true it's gonna print restarting and
it's still gonna get to this and run
this statement so instead what we prize
should say is Elif so if something's the
case else if something else is the case
is what Elif kind of means now possibly
this doesn't handle for everything so
possibly the user you know maybe they
hit B okay so we want to handle for if
the user has fat fingers we want to say
not a valid answer so
see you later alligator
okay play equals false so so then you
know we actually could condense this
quite a bit we could have probably said
you know instead of that block we
actually probably could have said if if
again dot lower equals n let's see how
can I Lissy I guess really what we could
have said is why else buy or something
like that
anyways look I'll leave it here okay so
now we can exit the game and be done
with the game if we if we so choose so
let's test this code really quickly so
we've been writing a lot of junk so
let's do 0 0 1 2 0 1 and then 2 1 so
player 1 should win odd I screw
something up what did it what happened
to 1 I have no idea what just happened
0 we definitely want lets see columns in
this player 1 column 1 Row 2 whoa player
1 is the winner
vertically the game is over would you
like to play again
let's hit no I just want to test that
first by E ok great and then now let's
just do the same thing again 0 0 1 2 0 1
2 1 0 2 okay
player 1 1 let's play again just see
that it refreshes the game ok it does
player 1 continues 2 1 ok ok great so
that's looking good so what else do we
have now
the next thing would be I think for us
to look for some water next thing would
be for us to fix this okay I try think
do we have anything else or is that the
last thing I think that's the last thing
besides this we really should
consolidate that I'm probably not going
to do it in this video but we really
probably should I really think that's it
that's the last thing I want to do so
let's talk about that string so I've
left my testing grounds so let's talk
about the rule of this string so first
of all let me go get it cuz this is the
string we're gonna attempt to to do and
then let's just say game size equals
three okay so what is the rules of the
string well we started with one two
three spaces and then each number
increments by one and then we have one
two spaces between them so the first
thing that we could say is like s and
then s could equal let's say 1 2 3 cuz
we start with 3 spaces and then for I in
range of games sighs so this would be
another example where I is completely
let's see well actually I is important
in this one it's about to tell you you
can use it under score don't do that
okay for I in range game sighs we would
like to s plus equals and then it's the
string version of I plus 2 spaces so
space space right and then we could
print s and then we can see so the first
one gets printed out and then this is
the second one we calculated okay so
that makes sense
the other thing is you could look at it
another way where it's actually one
space and then two spaces lead before
because actually well we've just written
as three spaces and then what it's
actually printing out is this so it's
actually
not the same so if we led with one space
and then said plus two spaces and then
the number it's probably a little more
two spaces plus string I that's a little
more true to what is going on here now
the next thing is so this is make sure
yeah so that's a valid way we could
build that now if you want you can stick
with that otherwise you also could say
it could be like we could build this all
in like one line so so one option we
have with strings is to join and we can
join a list comprehension so I'm gonna
show you guys but just know a list
comprehension like in this case you know
it could be let me just show you because
I think we have actually done one list
comprehension here but s could be equal
to so one two three so three spaces dot
join or actually let's see here three
spaces let's make it let's do this first
join and then and then use a list
comprehension so let's just say string I
for I in range of game sighs sighs OH
we never printed s print s okay 0 1 2 so
then let's do because we know it's 2
spaces of padding to join okay so then
we have 0 1 2 those are spaced correctly
so now we just need to do and then three
spaces boom okay so that's another way
and that's a way we could do it all in
one beautiful line so again this is
going to be a case of does like who's on
your team and who's reading your code
and do they need to understand this line
because anybody who's like an
intermediate and above Python programmer
they're gonna understand what join does
and they're gonna understand list
comprehension if you're working with
people that are just getting started
they might not understand this this
might be really confusing so depending
on who you're working with you can use
one of the other methods or not so let's
come over here and now we use that oops
no wait we're gonna just print it so so
we'll just do print boom okay let's come
back up here let's run it let's make
sure oh game size not defined of course
for my in range
Len game map save that let's come back
over here break break break break alone
again
okay there's our beautiful game lovely
mm-hmm okay
so now in theory you could make a bigger
game now depending on now somebody
mentioned you could use numpy zeros
here's a problem you'll you could use
numpy zeros and if you're not familiar
with numpy zeros that's totally fine
numpy is a third party package which we
haven't really talked about installing
third party packages but if you want to
you can either use pip install you know
package name or you could do pi dash 3.7
for the specific version of python - m
pip install and then the package so
let's let's grab a numpy really quick
numpy is a number crunching library and
we'll see if this even works is we do
have 64-bit Python historically this was
always really hard to get 64-bit
packages like numpy that are like C you
know Python wrappers around C code but I
believe it that's all been fixed we're
about to find out
wait for it
okay we've got none pine-sol and that's
kind of why like people always say like
oh you should use anaconda or these
other packages that have like standalone
installation of Python I don't really
agree because it actually gets pretty
challenging sometimes with those but
anyways okay we grab dump I let's just
check let me show you what numpy does pi
- 3 / 7 here we can import numpy as NP +
numpy has all kinds of mathematical
operations for you so if you're into
like math and stats and stuff numpy is
your best friend but for now there is a
method in numpy called numpy zeroes so
it starts you off with an array with n
number of zeros so we could say 5 and
it's going to return 5 zeroes we could
also give it a shape so 5 it's just a 1d
array or 1 dimensional array
you could say in numpy zeroes and then
we could specify a tuple here so of 5 by
5 or let me show you a 3x3 cuz that's
really what I'm trying to get out
anyways so there's a 3x3 but this is an
array it's not pretty it's not gonna
look good on our screen when we show
it's gonna be hard to format we got a
reformat like all our numbers and stuff
so I don't really want to do it you
could convert it to a list so you could
you could do NP zeros dot to list and
then it looks like that and then you
could iterate but again it's zero point
zero so then you could go and remove the
decimal and blah-blah-blah-blah-blah
it would be freaking annoying so I don't
think numpy zeroes is the good idea but
I know if I don't say something about it
someone's army like ah but you could use
numpy zeroes okay but I can't all right
so glad we figured that one out so
coming back over coming back over here
okay we got our dynamics sighs and
honestly tic-tac-toe is done if you're
done with tic-tac-toe you are free to
leave at any point honestly you've
learned all the basics you need there is
only one more basic concept and
eventually we could probably cover it
but it's dictionaries and dictionaries
are just a way to have keys and values
so key 115 key to you know 32
dictionaries key one is how you would
reference 15 so if we printed that
really quick you'd see 15 you can also
set new values dictionaries you could
say hi there
equals 92 and then let's just print
dictionaries so you can see what I'm
yakking about boom it's been added okay
there you go dictionaries in like two
seconds
I was hoping we would find it use for a
dictionary
I just didn't so there's a lots of stuff
that we still have yet to cover we
didn't cover all the built-in functions
either but I do think the data type of
or the storage of dictionaries is not
pretty important so there you go
dictionaries okay so now we're really
done but I just kind of want to play
around now and make the game size
dynamic so again tutorial so ever you
want to get out of here go on get but
we're gonna make the game size dynamic
so let's say okay we'll keep game size
variable let's say our game is gonna be
an empty list then what we could say is
for I in range of the game size we're
gonna say row is an empty row for I in
range also game size we're just kind of
kind of assume the game is square row
dot append zero and then when we're all
done with that game dot append row but a
boom but a babe rank game okay we got a
three by three we want a five by five
boom done five by five
okay now while I still have you just for
the record you can again how man so I
got my computer I go to Python
programming net more than I go to
python.org so it always comes up but
don't forget you can always just search
like list comprehension and learn more
about list comprehension if you want to
learn more about how that works on how
this works but I'm gonna take this list
put it on
one line because it's important we get
this tic-tac-toe game done in under a
hundred lines and actually okay we kind
of did it already but I want to add the
dynamic game size and that's gonna push
us over a hundred lines it's really
important that we stay under a hundred
it matters
let's see define two things yeah there's
definitely gonna have to be one at least
one more tutorial cuz we're going to
have to talk about sublime packages and
stuff to forgot about that okay so where
was I
okay so we're gonna move this to be one
a one-liner for loop so first of all
this will just be it's like this here
would be let me just write it out okay
so it would be for I in range game size
would be one of the things so in this
case it won't be a you know normally
would be like I for I if you were gonna
write a thing but instead actually it's
this you know collection of zeros so
then it's actually this list for I in
range game size so how many lists do we
want of these zeros well we want as many
as four iron range game size so I'm
going to build this list and this one is
zero for I in range game size and now
it's just one big list comprehension so
boom boom we say game equals and I'm
gonna comment these out just to show
whoops too far
prints game okay we've done it now I'm
okay with pretty so it's like well have
a curious why it came up twice nothing
I'm not gonna science okay so now we
have ourselves a totally dynamic game so
then you could have you could say games
size size equals input
what size game of tic-tac-toe great okay
now they can do it so now we can come
over here
copy that come down to game here paste
it in tab this over are we good yeah 99
good good good
okay let's save that come over here Oh
quit and tutorial 14 what size game a
2x2 oops okay so our input we just needs
to be an int say if you guys said you
wanted longer tutorials assign to cool
so it's a zero zero and then let's do 0
1 for player 2 and then 1 1 player 1 is
the winner so not only is our game you
know it's self dynamic we also have the
game size all the winds are dynamic so
you could play any sized game
tic-tac-toe whatever your heart desires
that's pretty cool ok so we have gone
ahead and made the most absurd game of
dynamic tic-tac-toe I think that's it I
don't think I want to go any further
there's so much stuff that we really
could do though we could go and convert
this you know before we display it we
can convert zeroes as bases and ones to
X's and Tuesdays we could do colors like
we could use the Colorama package with
Python and make you know player 1 green
and player 2 I don't know purple or some
other color orange and then you know
have space it we can make it look really
good we could do a lot but I think I'm
going to stop it here with tic-tac-toe
if you want to continue improving it and
share it please do feel free to post it
in the comments post it
in the discord GG / Centex wherever you
want and I think now I've got one more
tutorial in store for you I'd like to
install a third-party package besides a
numpy that we installed real quickly
just to run a an example kind of show
you going through documentation and
stuff like that but everything all in
like one video I still haven't decided
really what I want to do if it's gonna
be like pianos with data analysis or
flask for a quick web dev example I
really don't know but yeah if you got
it's probably by the time you see this
video I'll have already decided so but
you could leave it below if you want it
but I'll probably have already decided
but yeah probably just one more video on
that and then honestly get out of here
go do stuff that's interesting to you
you can even probably leave right now
but if you want to check out that other
video the next video go for it otherwise
go to Python / gonna net get some ideas
for what you want to do even if you
don't want to follow these exact
tutorials there's just so much stuff
that you can do in Python that you just
shouldn't be in the basics anymore I
think you've got enough anytime you hit
an air google it you want to do
something in python google it it's
pretty simple
so anyways next video really just kind
of wrapping up the series but if you got
questions comments concerns whatever do
feel free to leave them below otherwise
I'll see you guys in the hopefully last
basics video you watch and basics
tutorial you do I'll see you next time