Come back again in this lesson I'm going to go to the advanced features of this development something
I want to mention to you is that don't rush into getting into this lesson and doing it simply because
there's going to be a lot of code change a lot of new features and all of new topics I might feel a
little bit overwhelming if you just come into it if you have a good understanding of Iowa's development
then you have a good background then you perhaps can just jump into this one.
If this is a new thing for you and you're just getting it on the corners of how to do ex-school and
swift and stuff like that maybe you want to spend a little bit more time on the court before you come
to this lesson and feel you know overwhelmed with it.
Now in saying that there are a few things that I will be doing in this particular lesson I will be cleaning
all of the code.
I will be repartitioning parts of the code I will be trying to.
Are merging the pieces of the code that are similar to each other are the feature that the user can
choose whether they want to play a text based game or on major base game.
And they try to merge both of them my label under my age classes that we made in the previous lessons.
And based on that we have to change our interaction with the items.
It doesn't seem like there is a lot of stuff to be done but unfortunately some of the things that we
have to do it will affect multiple different files and multiple different functions.
And as a result it ends up being so many different things that we have to change and we have to keep
an eye on them.
So for that reason I would suggest that if you chose to do this and do it in one setting.
Don't let it drive over a few days or a few hours to go through it once in one go and get it done.
So you know exactly where you are with the different parts of the development.
So with all of that in mind let's go ahead and to afford that and I'm going to duplicate my project
once again if you already know how to use version control then you probably want to start a new branch
but if you don't know version control just yet you might just want to duplicate your folder somewhere
duplicate the folder which is the images.
And I actually get to call it advanced at once.
And here is how I'm going to open that project and begin to get it started in here.
Changing the stuff that we need to change.
So the first thing that I wonder is I want to add a new feature to this game that lets users choose
whether they want to play text based game or on email based game.
And that is something that we have to do in the interface.
So I'm going to put a storyboard on up here that I have the reset button with and what I do is I'm going
to go ahead and say let's break the constraint that is the leading space or the horizontal space for
the reset button.
So there is going to be this one called leading space to whatever that object is.
And once I found it I'm when I delete it so my reset button now just squeezes itself to infer its size
from the reset text.
No they have that.
I'm going to go ahead and say this duplicate this game or place it in here and that's it.
Our segment that control play sit there.
And let's change these things.
So the game mode now becomes game type.
So that's the type of the game that I want to play.
And then I'm going to go ahead and say this one the segmented contour if you only have two types.
One of them is going to be called text.
And the other one is going to be image and this outlet already has a built in to it.
When you duplicate outlet's it breaks the constraints that they have against other objects but they
will maintain the kind of scenes they have against themselves.
So in this case the constraints are still there and I don't need it to be 240 maybe I could use a number
such as higher than a hundred fifty probably does.
So let's keep the hundred fifty and I'm going to add the other constraints for everybody else.
So if you're into it you're going to say the game time just like what we had done in one of our very
early sounds.
It has a space top.
It has this space to the horizontal spacing from this other segment of the control which devalue if
it is 20.
Just like everything else in this op it has a pilot which already has a height 29.
And that's all you have to do for the game type.
Then I'm going to come for this segment that controls again the same thing top a space and the horizontal
spacing.
And then it's kind of refresh itself because now it has the left anchor top anchor care vits and height
is already determined.
And finally my reset is going to have a horizontal spacing against the newly created segment control
and the value for it would be 20.
And the moment they do that I have the reset button out there as well.
Now something to keep in mind is that when you duplicate outlets that the connections they get duplicated
as well.
So now if I go to my view controller I go to connection's you see that game all the action is connected
to two different outlets.
One of them is the one that we want it to be four by four six by six and eight weight.
The other one is it takes an image which is the wrong thing.
So I'm going to have to disconnect that.
So I don't need not.
But they do need to write a new action for text or image so I'm going to weigh in here when I have my
Dimitroff that is called Game Mode.
I'm actually going to copy everything in here.
Go ahead and then your file called it View Controller with a lot of case old plus game type here I'm
going to paste everything even though I don't need all of it.
What they need is this game type action and I need to find the segmented control but they don't meet
any of these other things.
You can decide about this later on.
So we have a new action game type action and that's of the type Iby action.
So I can go back to a storyboard and say Give me the view controller and it says there is something
called game type action.
I'm going to derogate over this and say Give me a value changed value changed.
So now we have a new action that is the control of this segment control and that is here in my game
type extension which is also a game type action.
So here you have to ask ourselves what do we want to happen when the game time action is stopped on.
We certainly want to change the type of the game from text image or from email to text but we don't
have anything that controls that just yet.
So to do that I'm going to go to my view controller file.
I'm going to add a new valuable down in here that's going to be called there let's call it game type.
Now here's the problem.
We don't know what type of variable the game type has to become.
We could just we make it something like a string text and then whenever we wanted to switch it would
make it to become an image.
But that really is not a very good way of doing it.
A much better solution for this is using an enumeration or what we call an enum simply because arms
are designed for such situations situations where we have values of the same nature but they have to
be cross that fit within each other.
And he can choose whichever we want to use.
So now they know that I'm actually going to go ahead and make myself and you know I'm going to say you
know I'm I'm going to call it text or image in on.
I might take the mentioned you know we'll have to use a case that is text and another case that is image.
So might you know can be either text or email.
And I'm for now writing that you know within the body of this file that you control.
But soon enough I'm going to give it its own fine.
But they want to keep it here because they want to show you how we change it.
So I'm going to say Game time is going to be of that type text or image I is going to be text or imagined
text by default.
So when the game starts in the viewpoint of at the very beginning the assumption is that the game runs
on a text note.
Now do they have that I'm actually going to copy this or essentially cut it and put it inside its own
file.
So I'm going to say command and savefile this is text or image and the text or image is going to have
this anywhere 8:3 inside it and you don't need to import they I simply because we are not using anything
from the White kid.
But we do need to import the foundation because we are using something from foundation called enumeration
or you.
So now they have that and I have this value in the view controller what they can do is I can jump back
into my game type action which is here and say based on this value I want to change that.
So essentially I wanted the game type to become something else depending on what the index is on the
segment that control.
Now there are two ways of thinking about it.
One of them is we do the same thing as we did before.
I get the index from the index.
I switch it based on each case.
I do a different thing.
Let me write down the first which is not the way I want to use.
I'm going to say switch on the value when a switch is in the X and within the body chase zero then game
type becomes text or image that takes case 1 game type becomes so EMH does emerge.
And then I would have a default which use a break.
Now this is all well and works perfectly fine but this isn't really a very good practice.
I want to show you a better way of doing the same thing and that is why using the roll value of the
text or image.
That's how we can do that.
I'm going to go to my thanks so much say my enumeration actually has a type and that is integer and
then text is actually 0.
MH is actually one and I'm using zero on one specifically because the values of the segment that control
are zero and one takes the zero image is one because they are the indexes of a segmented control.
So I'm going to add that you're saying my you know has a type and it takes any major They have values
and then I'm going to go to game type If I got the index that index is a number between 0 or 1.
So I don't need any of these guys but they do need is this I need to say game type becomes text or email
each roll value of index.
So if the raw value is 0 text or image becomes text if the raw value is one text or you measure real
value becomes one that's giving an error or saying that this value is an optional one.
We can't be sure if the indexing here does produce a number and the corresponding numerator from here.
So I'm just going to go ahead and force on Rapid of course every time we do such a thing we obviously
have to reset our game as well.
We'll go ahead and say reset auction itself as well.
So he is so far choosing a different game types sitting at this value game type to be a different thing.
But that doesn't really change anything in our game simply because so far the only thing that is happening
is one of value is changing to a different thing.
And unfortunately there's a lot of code they have to write before I can actually compile it and show
it to you.
So you kind of have to bear with me.
So we managed to change the game time and now we have to see how does this game time actually affect
everything else.
Most notably the way we make our blocks and the Vedas we present them and to use the game type the most
important method that we have to worry about is this method to make tiles.
So currently the major Titans is actually going in here and making us toys of the type image just a
while back.
It was actually making us tiles of the type that could have fun.
Which was my label.
So now you have to have a system in place that every time that we make it right it can be either an
image or a text and that comes back to this object might imagine the class that we have made ourselves.
And now I'm going to have to make another class that is going to be a combination of both my image and
my label going on and that's what I'm going to say command and.
And this one I'm going to call it my time.
So this is the one that doesn't have an image or a label.
You know it just has can be whatever that we want it to be.
And since this is something that can be either of the two it has to be a subclass of you I.
So they have name Portie you I.
And then I'm going to say class that's going to be my title.
It's a subclass of UI view.
And here are all the things that we need to have for it and let me first my Stothard you for it.
And then I'll write the code for it.
My time is going to have a built in label as well as a built in you or I.
You made your view on this essentially helps me having most of them in one place so I can use whichever
I want.
So let's go ahead and do that.
I'm going to say I have a variable called leap in and that's going to be a new label.
So it can have a label if I want it to.
It has a valuable code.
I N G view and that is a you are you made your view that you can have in case I needed that.
And then of course I knew they'd turn on a number like the last one.
So it's going to be internal number that's going to be an integer and then beyond that in case I was
using a image of you I need an internal image so our internal image and that's going to be a message
again optional.
And then the other two values that they would need in year are the values we had for the my label which
are these two I'm going to copy them from the air which is the question mark and the smiley face.
And then the other two from the image which had these two.
Now since these names are a little bit similar to each other like the question and the smile I'm going
to rename these and call this one question the XTi a smile.
XTi.
This one becomes a question.
I am g and this one becomes tic I m g.
So my title has a builtin label built in image view.
Built an internal number image built in questions I'm built in a smile.
Or take images text or images.
Now the next thing I have to do is I have to somehow initialize this tile that actually includes these
values and also decides whether it is going to be a label or your view.
So in order to do that I'm actually going to go ahead and write a you know write my own version of an
initializer and then I go in here and say I have a new in it this new in it.
I'm not overwriting the existing ones.
I'm writing a new one I'm going to say this new one has an input frame which is a C Jurek that's something
we definitely need to make any kind of anything in in this type and it has a type which is a text or
image.
So depending on whether the type is text or image you have to add either the label or the major view
and the next thing I'm going to say is I'm going to go to this method and say first of all I need to
do a superb in it with frame input frame so the initialization of the superclass is happening and don't
worry about this error it says that you have to include the coder included there even though we are
not going to use it for anything but it has to be there because it's required.
And then in my custom I'm going to say lieben becomes a UI label with frame and the frame of it is going
to be said of that frame and self obviously refers to this view.
So in my custom anyway I'm going to say label becomes your label cell frame and then I'm going to have
to add just to set things that we had earlier on.
Those are actually here in the make tiles so I could actually copy them from here.
You want to change the font.
I'm going to change the text alignment so I'm going to go in here and say label copy it's on commentaries
on that font and that text alignment.
Let's reformat them properly.
So labor gets the right font.
Right text alignment and that's all we have to do for the label.
Then we have to do similar things for the AMG view.
I'm going to say I am you view becomes a UI.
You made your view which is again made by the same frame.
So it's going to be self that frame is going to be built there.
And then I am as you view Dadds EMH it has to become evaluable which is my tile dot on the value here
is called question.
I am g a question I am.
I think I forgot to add that for the label.
So the label LPL that text that has become this value has to become my title dot questions text.
So when we create a new label we don't want to show what's the value on them.
If we want to show them that it says question mark or a question e-mail.
So that's what we have to do in here.
Then the next important thing becomes this thing.
If you are initialising and you have a type.
Based on that either you have to show the label or you have to show the image.
And here's what I'm going to do when I say if type is a text or image dot text.
If you're trying to make a text kind of title then we have to say self that I had the self view Alvy
and otherwise we have to say self that I had the same view or AMG view.
So let's review this once again.
We make in your tile of the type.
My toilet and that tide has both a label and an image of you being built into it but depending on the
type that we are trying to make we either only copy the label tweet or the email is into it.
So for a lot of progress we managed to make our own version of a tile that can be or is there an image
view or a label.
And now we have to go to make tiles and use that.
And before I do that and this is going to make the code a little bit messy.
But before I do that I'm actually going to go ahead and delete most of these files my label and my image
if I don't have them in my project then those going to be a lot of errors.
The areas are bad but they are also good because they show me very I have to change the code so I'm
going in here.
Say both of these files delete them.
Now if I go to my make tiles file in here I will have a tile which is trying to build itself from my
image which doesn't really exist anymore.
So I have to go in here and say tile make it from my tile.
I might tile has two different initializers one that is the code that the other one that is our custom
one that's the one I want to use and I'm going to say the custom one and the rectangle for it will be
the same one as we had before.
And let me get myself a little bit of space either text or email.
Part the fifth would be game type.
So depending on the game type we make a tile which is either a label or an image.
You obviously don't need any of these anymore.
Then I'm going to come in here once we set these values the image name and we said they in turn our
number.
We said the internal image and then I'm going to go ahead and say I don't actually need any of you guys
simply because I don't need to say to a question mark any more because the moment they initialize it
it's already setting itself with a question mark.
And as creakier give you that.
So the end it automatically sets the question mark for the M.H. And the question text for the label.
So whether you are making a tile that is an image or a label automatically or getting the question why.
The only thing we need to feed them are these two internal numbers and the internal image.
So by these to our label or our image view they will both have exactly the same value that they need
to have.
Now we have successfully managed to add tiles that are of the type.
My tired but then again we have so many other objects that are referring to my label or my image class
types.
They don't exist in this project anymore.
And the next thing we have to do is we have to go kind of do the same practice as we did before and
that's going file by file and changing the types that we need.
So I'm going to go to my controller and if you can try I'm going to say everywhere that they see my
image all my label I have to turn them into my title.
So here this becomes my title I'm going to copy that.
The first time it has to be of the type my type.
I can tile the same Fountain's array the same thing should happen to them.
Then I'm going to go with the reset action which I don't think anything should change in here.
And then I'm going to go to the touches and you probably have to change quite a few things.
First of all there is an error saying that there is no one called my image so I'm going to have to change
that to my tile.
And then if the top Time is of the type my type.
Now when we test for the founds tile array we said all of these and depending on the compare.
Now we said the first time or not and then we go to this function reveal and compare flips to reveal.
Now I want to step outside of here for a few minutes and program something else and explain to you.
And that is revealing compeer which is dysfunction here is very similar to flip to a smile is very similar
to flip back and is very similar to flip to reveal.
It's kind of redundant to write the same code so many times even though every time something is different
in them it makes better sense if we wrote to one function that has so many different arguments and we
can call the arguments properly each time that we needed.
So a of all of these four different functions I'm actually going to go right to one a new function that
hopefully takes care of all of the things that we have to do.
And I'm going to take a little bit of a Riskin here and then to delete all of these for in advance before
I'm even going to do anything.
I'm going to say all these four different functions that they're kind of similar.
Go ahead and delete them.
I move them to trash and now there's going to be loads of things that they don't really know what they
are or nobody knows what is the name meaning of the reveal and compare.
But that's OK.
But I'm going to do this I'm going to say Give me a new file I'm going to call this fun view controller
plus flip this is the only function that we'll have for flipping something.
And again I'm going to say even posed to you I get as before this is an extension to extend ssion to
view controller.
And yet I will have one function that is called Flip and the function Philipe has to take so many arguments
and do some things for us before they write the arguments that you write the body of.
So it's going to be a view dog transition without a view and with the completions so duration options
animations and completion.
So this is going to be a little bit of a longer coding.
So I'm going to try to explain every bit of it as much as I can for you.
The first thing is this you have to have a view which in our case is our tiler.
So I'm going to say there's an argument called game title of the type my tied and that's the one I want
you to flip.
Then the duration I'm going to assume is a static value such as point five.
Then there is the UI view animation option and that matters because some fans want to flip to the right.
Some fans want to flip to the left and because of that I need an argument for that.
And that's what I'm going to call an option which is a I view toward animation options and that's the
one I'm going to use for here.
So every time that we call this function we also say what kind of animation we want to see.
Then you have the body of the animations and we have the body of the completion and the completion I'm
going to say there's one argument for it.
RAZ let me reformat everything properly.
Here are the other things that we have to do.
Every time we flip we have to change the background color so we obviously need an argument for the color.
So I'm going to say that it is going to become B.G. color of the type color.
So that's one argument that we need in and then sometimes when we flip We want to see the internal number.
So we need some sort of value that shows us that text.
So I'm going to say that's called input DX.
The wishes of the type is string.
Sometimes however we don't want to see the text we want to see the internal image.
So we have to actually input an image into it.
So I'm going to say Impe I am g.
That is an image.
And then sometimes we want to go ahead and compare.
Once the compilation happened I'm going to say there is another value called compare and that's going
to be a boolean value.
And I didn't that's all I would need.
I would need a time I would need the animation option the background the color or the color that they
want to show what text they want to show if that's the thing or what image I want to show if that's
what you're trying to show.
And of course whether they should compare or not.
Now let's go to the body of the view turns it on it's what is actually rather simple even though it
does a lot of things is actually very very simple.
The body effect is this in the body of the animations that are running while the transition are happening.
First of all input tiled the background color should become the color that we receive.
So whatever color you ask me to make it this function will make it like that.
Once you set the background color you have to go ahead and said a few other things.
One of them is that tile dot labeled dot text that has to become the value that you have input in your
input text and then input type dog or AMG view that image that has to become the input image value.
Now you might be wondering how come you're setting both the text and image irrelevant of the situation
of the game is relevant of the game type is email or text.
The thing is at any point we are only showing one of these to label or image of you so it doesn't matter
if you set fourths of them because only one of them is going to be shown.
Remember they're both being located in here.
We have both label and image of you if you want to separate them you could.
And in that case you had to test only for one of them.
But I don't really bother with that.
What they did is I said let's allocate and initialize most of them but only at one of them to my my
view.
So that's why here I don't need to do any tests.
If you wanted to put new ideas you could say you're a self-taught game type is thinks that you match
Daut text then I have to do the text then.
Otherwise I have to do image but I'm not going to bother with that.
The last thing I have to do is in the completion block and there's something I actually memorized because
I knew I'm going to forget and that was here in the touches and that we had a value there is that called
it tile is animating and that becomes you every time this process happens and we remember that we have
to make that for us at the completion of the flip.
So I'm going to go in here and say in the completion self DOD a title is animating becomes false.
Once it did become false.
Here is the situation.
Are we comparing.
If compare if that is what we are doing then the self starts comparing.
So with this one function now be essentially put the last four functions that we had in here into only
one place this one function can do.
Philip Farber can do that.
What it can change the colors to dark grey or to orange or green.
It can change that text into question mark or into a smiley face.
It can change the image into a question mark or a tick sign and it can compare or not.
So let's go ahead and use this newly created function.
I'm going to go to touch as and I mean the touches and then I'm actually going to write the else part
first.
If this is the first tile that we kept on so I don't have this function more.
They do have something called Flip and the flip.
I'm going to have to feed all of these arguments.
So let's go ahead and say if this is the first time we don't want to compare.
So that's taken care of that you made that they want to show is that tapped the toilet.
Daut internal image and that image is an optional one so I have to make sure that it is forced on that
then that text that I want to show is this format this format which is a game top tile.
DOD internal number.
So we convert that the background color has to become orange.
The animation option has to become flip from left and that's why it is obviously fast tight.
So that is flipping something.
If this is the first time we are clicking now let's call it a bit higher.
Let's go to this part where you have to flip for the second time.
I'm actually going to copy everything from here.
I think I forgot to add a Yes because that's also a value that is optional.
So I want to copy everything from here instead of the reveal and compare the keyboard being compared.
So I'm going to say instead of compare false compare is true.
And if we are flipping.
Second time this time again flip from left.
That's going to be the way that is top tie that internal number of internal image.
They both get passed through into our Flip function and that's all we have to do here.
Now we have some other things that we have to address and those are here.
If we did want to compare someone with the compare function and in the compare function we now have
these four different things that we just don't have them anymore and we have to rewrite them using the
new flip.
So what I'm going to say flip and let's do all of it in one go.
So if these two lines are the same it means we succeeded in finding what we want to do.
We want to flip the first tile using a UI animation view not left a gain the same or there.
We want to make the gun color become green.
We want to make sure the text on it shows this smiley face.
So that value becomes this like you and that's going to be called my tyed dot smile text and the image
for it.
If you're using the image it becomes my title not take image.
I am g.
And we don't have to compare anymore.
So that's for the first time I'm going to copy this entire piece of code pasted in here and say that
becomes for the second time.
So so far in the comp here if they were of the same nature if they had the same internal number we flip
most of them back and it's giving me again a warning saying that the take you make it something down
is optional you have to force on rapid and I'm going to say OK I'll do that and that's about that then
they have to go to the sport of it.
What if they were not the same.
And we have to flip them back to what they used to be.
So I'm going to paste that code in here again which is this one
and I'm going to say let's say for the first time first even though it doesn't really matter.
Flip it from right and then make the background color become dark gray and you know sort of the smiley
text.
Show me the question text and stuff that he made Show me the question image.
So if we compare it and they were not the same go back to the question make or the question text and
of course.
Copy this one more time and place it for the second time do the same thing in here if you want wanted
to push this.
You could actually make the Philipe function to accept an array of times.
I'll leave that to you guys to handle.
So I think that's all I had to do in here.
Let me quickly go through my code reformats some naming conventions just a little bit easier to read.
Let's see if that's all we had to do.
I'm going to get rid of the view control and replace it with divisi.
So that's what we see.
This is what we see plus reset said this is C plus touches and that this is a v c plus flip.
This is Visi Plus compare then I have received plus state VC game want me see game type see.
It's Hyams.
VC randomise VC time action on ABC.
You did.
Lord let me quickly look at all the code and this kind of final review for all of us to make sure everything
is working and then we can run the application.
So when the application loads we have the VC that we have all these different variables and it you know
reviewed it a lot.
We just reset it in the Reset.
We essentially go and set everything to their default values.
Game Time 0 nor found tiles array are game one is hidden first line and second third and then compare
Now as follows.
And so on and so forth.
And then we end up going to here which is important to make tiles in the make Times which is here now
we can actually see that we are making the tiles from Mitoy based on a certain frame and based on the
current game type.
And as we make them based on the current game time what happens is depending on the game time whether
it is text or image we add the text or that image view to our view.
Once we did that in the touches and we end up checking for whether we have tapped on one of these objects
from the type tile.
And if that's the case we are going to flip them one of them Phillips compares the other one to Phillips
and doesn't compare based on that we should they be able to see what we expected to see at the very
very beginning.
The other function that we added was the game type that switches the game type to a new value based
on the roll value and calls the reset action.
So everything else we haven't really done anything on them randomized stays the way it used to be timers
stays exactly the way it used to be working.
Let's go ahead and see if we have any errors and we already have an error or this value boolean value
is saying compare and this is probably because of the name that I have used so it has to be self.
Don't compare because I compare function is outside disclosure.
So cells that compare.
And then that's right again.
And I think we should be ok this time if there are arrows.
It's a very common thing that you might do a quote and you get a lot of arrows and that's nothing scary.
We just have to go through the code and find out what is wrong.
I'm very expecting for something to go wrong right now even though I have done this quote before.
So let's cross our fingers.
This is five and seven but nothing happened.
Five and five.
Everything is working.
I'm going to see six five six ten and six and 3 and 2 and 14 and then I'm going to shy only one more
time.
And I couldn't find anything.
Now I'm going to go to the e-mail type.
I can actually compare the dog and the satellite and the dog.
OK.
That was the wrong one.
The dog and the dog won and everything seems to be working the way we expected it.
A lot of things we went through in this particularly the final lesson.
I know it's a lot of different pieces of code you probably couldn't catch up at but sometimes because
there is a lot of stuff going on.
If you want it you can look at the code that I have.
I have you know for that you will read this lesson.
Watch this particular lesson a few times.
If you are not sure how things are happening and yeah I think that's about that for this development.
We have a game you can play in a four by four six five six or eight blades in two different ways.
We covered a lot of Kyon's.
We modify that of course.
And I hope you enjoyed it.
So good luck.