1 00:00:00,540 --> 00:00:01,580 ‫Welcome back. 2 00:00:01,590 --> 00:00:07,110 ‫And this video, you're going to get a little challenge and you should create tic tac toe. 3 00:00:07,140 --> 00:00:13,230 ‫As you can see here, just a console game, not a forms game or anything like that, but just a console 4 00:00:13,230 --> 00:00:13,470 ‫game. 5 00:00:13,470 --> 00:00:20,850 ‫As you can see here, with nine values or nine fields and player, one can choose a field afterwards, 6 00:00:20,850 --> 00:00:23,250 ‫player two can choose a field and so forth. 7 00:00:23,250 --> 00:00:28,890 ‫And for example, there is the field one, I enter one and now it's an oh. 8 00:00:28,920 --> 00:00:34,650 ‫So this is not a zero, it's an oh then player two's turn is and I'm going to enter five. 9 00:00:34,650 --> 00:00:40,440 ‫So now you can see five is replaced by an x and only the numbers that you can see are still available. 10 00:00:40,440 --> 00:00:42,900 ‫So let's say I enter five again as player one. 11 00:00:42,990 --> 00:00:44,730 ‫It will tell me incorrect input. 12 00:00:44,730 --> 00:00:46,500 ‫Please use another field. 13 00:00:46,530 --> 00:00:48,240 ‫All right, so I'm going to use three. 14 00:00:48,240 --> 00:00:55,950 ‫So let's say player two doesn't understand that you should only enter numbers and he just enters a G 15 00:00:55,950 --> 00:00:57,030 ‫or whatever. 16 00:00:57,210 --> 00:00:59,490 ‫Then it says, Please enter a number. 17 00:00:59,490 --> 00:01:02,250 ‫Incorrect input, please use another field. 18 00:01:02,340 --> 00:01:02,700 ‫Right? 19 00:01:02,700 --> 00:01:04,020 ‫So it's still player to turn. 20 00:01:04,020 --> 00:01:05,490 ‫So what is he going to do? 21 00:01:05,520 --> 00:01:06,810 ‫He's going to try to win. 22 00:01:06,810 --> 00:01:09,330 ‫So this is going to be super close. 23 00:01:09,750 --> 00:01:10,920 ‫Player two. 24 00:01:11,780 --> 00:01:16,250 ‫Is going to win and we are play to you one. 25 00:01:16,250 --> 00:01:22,970 ‫So he has three crosses in a row and well, please press any key to reset the game. 26 00:01:22,970 --> 00:01:24,920 ‫I'm going to press any key. 27 00:01:24,920 --> 00:01:25,820 ‫And we are. 28 00:01:25,850 --> 00:01:27,410 ‫We have a new round. 29 00:01:27,410 --> 00:01:30,500 ‫So you should also create a reset functionality. 30 00:01:30,980 --> 00:01:32,540 ‫All right, so that's pretty much it. 31 00:01:32,540 --> 00:01:33,860 ‫Try to build that. 32 00:01:33,860 --> 00:01:41,690 ‫You will need to use two DX arrays, you'll need to use switch and case and methods and many different 33 00:01:41,690 --> 00:01:44,240 ‫things that we have touched in the last videos. 34 00:01:44,240 --> 00:01:49,790 ‫So this is going to take quite a while and your solution might look very different to mine. 35 00:01:49,910 --> 00:01:55,490 ‫And yeah, in general, if you don't get anywhere, if you if this is too hard for you, no worries. 36 00:01:55,490 --> 00:02:00,950 ‫You can simply follow along and try to understand all the steps that I'm going through and I'm going 37 00:02:00,950 --> 00:02:02,690 ‫to explain everything that I'm doing. 38 00:02:02,690 --> 00:02:08,960 ‫But it's best if you really try by yourself, because when you try it yourself, you will find solutions 39 00:02:08,960 --> 00:02:18,230 ‫which will catapult your your skill by a lot, because simply by doing you'll learn most and by just 40 00:02:18,230 --> 00:02:20,180 ‫watching, you will not learn as much. 41 00:02:20,180 --> 00:02:21,650 ‫So that's really important. 42 00:02:21,680 --> 00:02:27,350 ‫As long as you run into errors and you fix them yourself, that's when learning really kicks in. 43 00:02:27,350 --> 00:02:30,050 ‫And that's why it's really important that you try it yourself. 44 00:02:30,650 --> 00:02:31,340 ‫All right. 45 00:02:31,340 --> 00:02:37,850 ‫So I hope you pause the video now and you try it and this can take up quite some time. 46 00:02:37,850 --> 00:02:39,770 ‫So you shouldn't be done within 5 minutes. 47 00:02:39,770 --> 00:02:41,810 ‫Well, maybe you are correct and you can do it. 48 00:02:41,810 --> 00:02:47,660 ‫But otherwise this might take, well, more than an hour or even more than 2 hours based on the solution 49 00:02:47,660 --> 00:02:48,500 ‫that you choose. 50 00:02:48,500 --> 00:02:52,700 ‫And as I said, there are many different ways to solve this problem. 51 00:02:52,700 --> 00:02:59,750 ‫And I have just one which I thought was the one rather close to what we have learned so far. 52 00:02:59,750 --> 00:03:04,100 ‫So it's using the functionality that we have all learned so far. 53 00:03:04,100 --> 00:03:05,570 ‫So nothing too new. 54 00:03:05,570 --> 00:03:09,710 ‫And yeah, that's why I'm going to go ahead and build the app now. 55 00:03:09,860 --> 00:03:10,460 ‫All right. 56 00:03:10,670 --> 00:03:15,560 ‫So as I said, I hope you tried to create it on your own. 57 00:03:16,070 --> 00:03:21,020 ‫So I'm going to create a new project file new. 58 00:03:22,430 --> 00:03:23,300 ‫Project. 59 00:03:23,540 --> 00:03:30,230 ‫And I'm going to call that tic tac toe exercise. 60 00:03:30,500 --> 00:03:33,470 ‫And then it's going to be a console app once again. 61 00:03:33,470 --> 00:03:42,230 ‫And let's click, okay, now before I do any complex logic where I check who has one and all those kind 62 00:03:42,230 --> 00:03:44,180 ‫of things, I'm going to start off very simple. 63 00:03:44,180 --> 00:03:52,370 ‫I'm just going to create our play field first, and by that I mean even the console writing. 64 00:03:52,370 --> 00:04:00,670 ‫So I'm going to create a new method and I'm going to call that public static void set field, and I 65 00:04:00,680 --> 00:04:04,430 ‫make it static only so that I can use it within my main method. 66 00:04:04,430 --> 00:04:10,550 ‫Otherwise I would have to create an object object to exercise in order to use set field. 67 00:04:10,550 --> 00:04:12,950 ‫But making it static is totally fine. 68 00:04:13,250 --> 00:04:23,300 ‫So next within my set field method, I need to write the lot of console that right line and it's going 69 00:04:23,300 --> 00:04:25,160 ‫to look a little like this. 70 00:04:25,160 --> 00:04:33,230 ‫So quotation marks, plenty empty fields, then a pipe, some empty fields, and again, some empty 71 00:04:33,230 --> 00:04:33,950 ‫fields. 72 00:04:34,070 --> 00:04:36,650 ‫So this is the first line. 73 00:04:38,150 --> 00:04:41,660 ‫Then console that right line again. 74 00:04:41,960 --> 00:04:45,860 ‫You could also use see W double tap. 75 00:04:45,980 --> 00:04:55,820 ‫That's the quicker version and then two empty spots or empty fields and again a pipe. 76 00:04:57,650 --> 00:05:05,690 ‫And then here what I do is I prepare it so that we can display the numbers. 77 00:05:06,890 --> 00:05:09,320 ‫And what are the numbers that I want to display? 78 00:05:09,590 --> 00:05:14,870 ‫Well, that's something that we will have to use later on or create later on. 79 00:05:15,050 --> 00:05:19,220 ‫So for now, I'm just going to say one, two and three. 80 00:05:19,220 --> 00:05:21,380 ‫But later on, we will need to change that. 81 00:05:21,650 --> 00:05:32,810 ‫So I'm going to add that to my list to do replace numbers with variables. 82 00:05:34,430 --> 00:05:35,090 ‫Okay. 83 00:05:35,540 --> 00:05:40,940 ‫Now, I need to create a similar role to that one. 84 00:05:42,920 --> 00:05:53,420 ‫But here I'm going to have underscores and we can change that if we see later on that this is not enough 85 00:05:53,420 --> 00:05:56,780 ‫or it doesn't look as good, we can easily change that later on. 86 00:05:57,590 --> 00:05:58,010 ‫All right. 87 00:05:58,010 --> 00:05:58,730 ‫So. 88 00:06:00,400 --> 00:06:02,120 ‫This is pretty much it. 89 00:06:02,140 --> 00:06:04,030 ‫We need to do that multiple times now. 90 00:06:04,030 --> 00:06:08,500 ‫So I'm going to copy that, actually. 91 00:06:11,600 --> 00:06:13,220 ‫I'm going to copy all of that. 92 00:06:18,620 --> 00:06:19,550 ‫And there we are. 93 00:06:20,210 --> 00:06:23,600 ‫And finally, one more row of this. 94 00:06:25,250 --> 00:06:27,230 ‫And that should be pretty much it. 95 00:06:27,230 --> 00:06:28,130 ‫Let's have a look. 96 00:06:29,240 --> 00:06:35,900 ‫Let's call this method set field and let's see how it looks on the console. 97 00:06:37,250 --> 00:06:45,530 ‫Actually, we need to run console, but red line or red key is even better. 98 00:06:45,710 --> 00:06:46,700 ‫Let's run it. 99 00:06:47,210 --> 00:06:48,380 ‫And there we are. 100 00:06:48,620 --> 00:06:51,320 ‫It's not perfectly positioned yet, so let's have a look. 101 00:06:54,480 --> 00:06:54,810 ‫All right. 102 00:06:54,810 --> 00:06:57,690 ‫So one problem here is I need five. 103 00:06:59,290 --> 00:07:00,520 ‫Points distance. 104 00:07:00,700 --> 00:07:02,530 ‫So five empty spaces. 105 00:07:03,430 --> 00:07:04,390 ‫Same here. 106 00:07:05,260 --> 00:07:07,570 ‫One, two, three, four, five. 107 00:07:07,990 --> 00:07:09,930 ‫One, two, three, four, five. 108 00:07:10,270 --> 00:07:11,050 ‫That's good. 109 00:07:11,260 --> 00:07:16,110 ‫So now let's use this and replace the lines where we had that. 110 00:07:16,120 --> 00:07:21,910 ‫So here it's a little testing, you know, it's it's about making it look good and there's some testing 111 00:07:21,910 --> 00:07:22,750 ‫involved. 112 00:07:23,050 --> 00:07:32,530 ‫So I'm going to use the same approach here, and I'm going to copy those two lines and replace those 113 00:07:32,530 --> 00:07:33,100 ‫ones. 114 00:07:34,240 --> 00:07:39,010 ‫All right, now in here, we should have two in both directions. 115 00:07:39,040 --> 00:07:40,150 ‫Let's test it. 116 00:07:41,530 --> 00:07:42,680 ‫And there we are. 117 00:07:42,700 --> 00:07:44,620 ‫So that looks a lot better already. 118 00:07:44,650 --> 00:07:46,540 ‫One, two, three. 119 00:07:46,900 --> 00:07:49,420 ‫Those numbers should be replaced later on, as I said. 120 00:07:49,420 --> 00:07:51,800 ‫But that's already how we can set the field. 121 00:07:51,820 --> 00:07:57,580 ‫So you might imagine that this will already help us to reset the field later on. 122 00:07:58,420 --> 00:07:59,010 ‫All righty. 123 00:07:59,050 --> 00:08:04,510 ‫So next, now that we have that, we need to actually create the field. 124 00:08:04,510 --> 00:08:13,720 ‫And the play field should be, well, a field in terms of programming, create the play field. 125 00:08:13,930 --> 00:08:20,290 ‫And what it will do is static or actually not create the play field, but it's going to be the play 126 00:08:20,290 --> 00:08:23,170 ‫field static character. 127 00:08:23,170 --> 00:08:27,220 ‫So you could use, well, integers or strings in here as well. 128 00:08:27,220 --> 00:08:32,680 ‫But characters are the ones that are most resource efficient. 129 00:08:32,980 --> 00:08:39,970 ‫I'm going to call a play field and in here I need to have the different rows. 130 00:08:40,750 --> 00:08:47,380 ‫So one row with the one, two and three 131 00:08:49,960 --> 00:08:53,410 ‫and then the same thing with the next. 132 00:08:53,410 --> 00:08:57,190 ‫So four, five and six and so forth. 133 00:08:57,190 --> 00:09:01,780 ‫So I'm going to replace the numbers in a second. 134 00:09:01,780 --> 00:09:05,710 ‫So this one is the four, five, six, seven. 135 00:09:05,710 --> 00:09:10,750 ‫Actually, this is seven, eight and nine. 136 00:09:10,750 --> 00:09:11,430 ‫So there we are. 137 00:09:11,440 --> 00:09:12,850 ‫This is our play field. 138 00:09:13,810 --> 00:09:21,550 ‫Now, what we need to do is to actually replace those numbers here with the play field here. 139 00:09:21,550 --> 00:09:28,780 ‫So this is how generally our play field should look at the beginning, and we want to replace the numbers 140 00:09:29,140 --> 00:09:30,310 ‫in here later on. 141 00:09:31,720 --> 00:09:32,560 ‫By the user. 142 00:09:32,680 --> 00:09:33,100 ‫All right. 143 00:09:33,100 --> 00:09:39,370 ‫So what we use here instead of one is actually this play field at a specific position. 144 00:09:39,370 --> 00:09:42,850 ‫So actually, we get rid of that part here. 145 00:09:44,140 --> 00:09:46,180 ‫Now you need to see it a little better. 146 00:09:46,180 --> 00:09:53,890 ‫So this is the play field and I need to have the play field at the position zero, comma zero. 147 00:09:56,320 --> 00:09:56,950 ‫All right. 148 00:09:56,950 --> 00:09:59,880 ‫And now we do the same thing for the two. 149 00:09:59,890 --> 00:10:04,000 ‫So zero, comma one and zero comma two. 150 00:10:04,690 --> 00:10:15,370 ‫And then the second row, it's one zero, one, one and one, two. 151 00:10:15,910 --> 00:10:17,500 ‫If you're not sure what this means. 152 00:10:18,220 --> 00:10:19,930 ‫So this is row zero. 153 00:10:21,910 --> 00:10:23,410 ‫This is row one. 154 00:10:23,530 --> 00:10:26,650 ‫And that's row two. 155 00:10:28,510 --> 00:10:29,170 ‫So. 156 00:10:30,100 --> 00:10:31,450 ‫This is Rosie Rowe. 157 00:10:32,080 --> 00:10:35,320 ‫Here, then this is a real one. 158 00:10:35,980 --> 00:10:40,360 ‫And now we need to do the same thing with row two. 159 00:10:41,170 --> 00:10:43,880 ‫So this is going to be two zero. 160 00:10:44,080 --> 00:10:46,090 ‫This is going to be two one. 161 00:10:46,600 --> 00:10:50,990 ‫And finally, we have two, two, two, two. 162 00:10:51,040 --> 00:10:52,480 ‫So now let's start it again. 163 00:10:52,480 --> 00:10:54,690 ‫And we should still get the same result. 164 00:10:54,700 --> 00:10:55,450 ‫So there we are. 165 00:10:55,450 --> 00:10:57,550 ‫One, two, three, and so forth. 166 00:10:57,730 --> 00:10:58,340 ‫Great. 167 00:10:58,360 --> 00:11:03,610 ‫So now our playing field or playing field is prepared. 168 00:11:03,640 --> 00:11:06,910 ‫Now let's check out how we can get user input. 169 00:11:07,030 --> 00:11:10,810 ‫And what we need, therefore, is to create. 170 00:11:13,370 --> 00:11:17,540 ‫A variable which is going to be our player. 171 00:11:17,780 --> 00:11:23,240 ‫So player is going to be let's say we start with player one. 172 00:11:23,900 --> 00:11:28,700 ‫So we set it that player two and you will see later on why that makes sense to set it to two. 173 00:11:29,000 --> 00:11:37,670 ‫Then we need the user input and input is zero and I'm just going to set that to zero because none of 174 00:11:37,670 --> 00:11:39,950 ‫the fields here is zero. 175 00:11:39,950 --> 00:11:42,800 ‫So using it using zero is great here. 176 00:11:42,890 --> 00:11:49,850 ‫And finally, we need a boolean which checks if the input is correct, because if it's not, then we 177 00:11:49,850 --> 00:11:55,520 ‫want to well we want to inform the player and try to get the right input. 178 00:11:56,540 --> 00:11:57,290 ‫Alrighty. 179 00:11:57,290 --> 00:12:05,750 ‫So now that we have that we need to create a big or a very long do while so do. 180 00:12:06,050 --> 00:12:11,150 ‫And after many rows we have the wild statement. 181 00:12:11,150 --> 00:12:12,710 ‫And in here I'm just going to use. 182 00:12:12,710 --> 00:12:13,250 ‫True. 183 00:12:13,430 --> 00:12:15,710 ‫Why do they use do while with true. 184 00:12:15,740 --> 00:12:22,040 ‫Well, that's because I want the user to always be able to enter something in my game. 185 00:12:22,040 --> 00:12:27,380 ‫And in this case I do some code running in here which is not going to be automated. 186 00:12:27,380 --> 00:12:28,970 ‫It's dependent on user input. 187 00:12:28,970 --> 00:12:35,090 ‫So it's not an endless loop, but it's always going to be true as long as we play the game. 188 00:12:35,450 --> 00:12:37,460 ‫So that's why we need to do while. 189 00:12:37,460 --> 00:12:38,030 ‫True. 190 00:12:39,200 --> 00:12:39,440 ‫All right. 191 00:12:39,440 --> 00:12:47,180 ‫Next, what we need to check is, is player one the one who's who should play or is it player two So 192 00:12:47,180 --> 00:12:48,170 ‫whose turn is it? 193 00:12:48,410 --> 00:13:00,080 ‫If player is equal to two, then set player to one and else. 194 00:13:00,080 --> 00:13:04,850 ‫If actually we could just use els here else. 195 00:13:05,090 --> 00:13:15,110 ‫Player is equal to two, so you could either use els or else if in our case the player can only have 196 00:13:15,110 --> 00:13:15,650 ‫two values. 197 00:13:15,650 --> 00:13:21,920 ‫But if something goes wrong and for some reason player is increased to two, then it always will be 198 00:13:21,920 --> 00:13:22,520 ‫or increased. 199 00:13:22,520 --> 00:13:25,960 ‫And it'll always be this problem that it's always going to be the player two. 200 00:13:25,970 --> 00:13:33,170 ‫So that's why I'm just going to make sure that else if player is equal to one, then switch over to 201 00:13:33,170 --> 00:13:33,920 ‫player two. 202 00:13:34,880 --> 00:13:41,060 ‫So now, based on whose turn it is, I need to run a switch case statement. 203 00:13:41,300 --> 00:13:44,770 ‫So I'm just going to say switch player. 204 00:13:44,780 --> 00:13:48,380 ‫So based on whose turn it is, let's run some code. 205 00:13:49,670 --> 00:13:52,490 ‫Let's say the case is one. 206 00:13:52,490 --> 00:13:56,720 ‫So it means player ones turn. 207 00:13:59,410 --> 00:14:01,930 ‫And actually, let me add some code here. 208 00:14:02,140 --> 00:14:06,220 ‫Run code as long as. 209 00:14:08,080 --> 00:14:10,150 ‫The program runs. 210 00:14:11,650 --> 00:14:14,620 ‫So if it's player one stern, what do we want to do? 211 00:14:16,450 --> 00:14:18,820 ‫Well, we want to check. 212 00:14:19,240 --> 00:14:20,620 ‫What did he enter? 213 00:14:22,620 --> 00:14:24,430 ‫And assign the number. 214 00:14:24,450 --> 00:14:26,240 ‫Well, assign X to that number. 215 00:14:26,250 --> 00:14:28,350 ‫So we have here our play field. 216 00:14:28,350 --> 00:14:29,940 ‫So this is one, two, three. 217 00:14:29,940 --> 00:14:33,960 ‫And what I want now is if it's player one, then I want this to be replaced by an X. 218 00:14:33,960 --> 00:14:35,820 ‫So if he enters one, for example. 219 00:14:35,820 --> 00:14:48,060 ‫So in order to do that, I simply create another switch here, switch input because based on the input, 220 00:14:48,660 --> 00:14:51,030 ‫I want to check this case. 221 00:14:51,030 --> 00:15:01,290 ‫One So did the player enter one player or play field at the position zero zero should be replaced with 222 00:15:01,860 --> 00:15:04,710 ‫an x and then we want to break. 223 00:15:05,400 --> 00:15:06,540 ‫So then we are done. 224 00:15:07,740 --> 00:15:09,690 ‫By the way here it's still complaining. 225 00:15:09,690 --> 00:15:15,900 ‫That's because we don't break out of this switch loop here and now we do and now it's happy. 226 00:15:16,290 --> 00:15:23,940 ‫So I replace the play field with an x and here it's complaining because I used string and in order to 227 00:15:23,940 --> 00:15:27,060 ‫make sure that it works, I use CHA now. 228 00:15:27,060 --> 00:15:29,610 ‫So as you can see a past apostrophes. 229 00:15:29,610 --> 00:15:36,300 ‫They are there for characters and as you know, the play field has plenty of characters in there. 230 00:15:36,300 --> 00:15:38,460 ‫So it's a 2D array of characters. 231 00:15:38,460 --> 00:15:40,350 ‫That's why I need to use it that way. 232 00:15:40,380 --> 00:15:40,740 ‫All right. 233 00:15:40,740 --> 00:15:41,970 ‫So that's for case one. 234 00:15:41,970 --> 00:15:45,960 ‫And now I need to do the same thing for the different cases that we have. 235 00:15:45,960 --> 00:15:48,750 ‫So for case two, what do we need there? 236 00:15:48,750 --> 00:15:53,760 ‫We need to have zero one for case three one. 237 00:15:55,440 --> 00:15:57,960 ‫We're actually zero two for case four. 238 00:15:57,990 --> 00:16:00,030 ‫It should be one zero. 239 00:16:00,810 --> 00:16:01,700 ‫Case five. 240 00:16:01,710 --> 00:16:03,240 ‫It's one and one. 241 00:16:06,290 --> 00:16:10,850 ‫And if you are not sure what I do here, check again. 242 00:16:10,850 --> 00:16:11,930 ‫Roe zero. 243 00:16:12,350 --> 00:16:14,400 ‫So this is position zero zero. 244 00:16:14,420 --> 00:16:18,460 ‫This is position zero one, this position zero two and so forth. 245 00:16:18,470 --> 00:16:19,820 ‫So this is what I'm doing here. 246 00:16:20,180 --> 00:16:24,140 ‫So I'm replacing them with the correct values. 247 00:16:24,140 --> 00:16:38,450 ‫So the seven is at two and zero eight is at two and one and nine is at two and two. 248 00:16:39,620 --> 00:16:41,210 ‫So that's going to be. 249 00:16:42,160 --> 00:16:46,360 ‫What's happening if the player one enters a value. 250 00:16:47,050 --> 00:16:49,960 ‫And it's always going to replace it with an X. 251 00:16:52,580 --> 00:16:58,900 ‫Now what we can do now is we can copy this whole code and do the same thing for player two, right? 252 00:16:58,910 --> 00:17:03,280 ‫So we could just say, okay, let's copy this and then let's run it for player two. 253 00:17:03,290 --> 00:17:08,870 ‫Actually, I'm going to show you that just so you see why that is not the best approach. 254 00:17:09,420 --> 00:17:11,610 ‫So I'm going to add another case here. 255 00:17:12,840 --> 00:17:14,430 ‫Case, it's two. 256 00:17:15,390 --> 00:17:19,350 ‫And now we need to break out of it as well. 257 00:17:20,180 --> 00:17:23,150 ‫So actually this break here. 258 00:17:24,860 --> 00:17:29,030 ‫Should be in here and in here we break as well. 259 00:17:31,500 --> 00:17:40,110 ‫And we have another switch case so I could copy this code here actually and I can paste it in here. 260 00:17:40,830 --> 00:17:47,820 ‫But there is a nicer way we can create a method which simply checks. 261 00:17:47,820 --> 00:17:53,760 ‫Is it player one or is it player two are actually it's just going to return an X or an O. 262 00:17:54,970 --> 00:17:57,490 ‫So let's go ahead and create this method. 263 00:18:00,560 --> 00:18:08,390 ‫Public static void, enter x or O. 264 00:18:09,320 --> 00:18:10,850 ‫I'm just going to keep it simple. 265 00:18:11,420 --> 00:18:14,900 ‫And what I need here is I need an integer. 266 00:18:16,900 --> 00:18:20,710 ‫So I need an integer and I'm going to call that one player. 267 00:18:21,460 --> 00:18:25,270 ‫So what I do now is I cut this code out from here. 268 00:18:28,590 --> 00:18:37,390 ‫So switch statement, I'm going to cut it in here and I'm not going to need that one here anymore. 269 00:18:37,410 --> 00:18:39,300 ‫I will need the player still. 270 00:18:39,630 --> 00:18:44,670 ‫And what I'm going to do is I'm going to create a new character. 271 00:18:46,490 --> 00:18:58,970 ‫Player sign and I'm going to set that to an empty sign for now or an empty character, 272 00:19:02,030 --> 00:19:02,960 ‫which is not empty. 273 00:19:02,960 --> 00:19:05,240 ‫And it's in it's a space in here. 274 00:19:05,270 --> 00:19:16,950 ‫So now if player is equal to one, then actually we can do it in one row here. 275 00:19:16,970 --> 00:19:19,610 ‫Player sign is going to be. 276 00:19:22,230 --> 00:19:22,800 ‫X. 277 00:19:25,020 --> 00:19:39,900 ‫Else if a player is equal to two, player sign is going to be y or o in this case, actually. 278 00:19:40,770 --> 00:19:41,580 ‫And now. 279 00:19:42,780 --> 00:19:44,820 ‫We need the user input as well. 280 00:19:45,690 --> 00:19:47,580 ‫So I'm going to need the input. 281 00:19:48,690 --> 00:19:52,050 ‫And what kind of type was the input? 282 00:19:52,050 --> 00:19:53,730 ‫Input was an integer, right? 283 00:19:54,540 --> 00:20:02,670 ‫So I'm going to use input here int input and now I replace this X here with the player side. 284 00:20:06,090 --> 00:20:06,720 ‫All right. 285 00:20:07,020 --> 00:20:13,200 ‫So now what we can do is we can use this method instead of running this coast code here twice. 286 00:20:13,560 --> 00:20:19,560 ‫All right, now that we have this method, we actually don't even need the switch case anymore because 287 00:20:19,560 --> 00:20:21,650 ‫we already have the player here. 288 00:20:21,660 --> 00:20:26,490 ‫So we say player one and then we could simply run the code in here. 289 00:20:26,700 --> 00:20:28,410 ‫So let's do that. 290 00:20:29,280 --> 00:20:32,190 ‫Let's set the player here. 291 00:20:32,190 --> 00:20:40,950 ‫And because we have a little longer text here, we need to use the curly brackets or we have because 292 00:20:40,950 --> 00:20:45,030 ‫we have a multi line code that we want to execute. 293 00:20:45,030 --> 00:20:47,940 ‫That's why we need to enter this. 294 00:20:48,380 --> 00:20:48,640 ‫Okay. 295 00:20:48,660 --> 00:20:56,490 ‫So now I can go ahead and I can get rid of this whole switch case statement here and I only need to 296 00:20:56,490 --> 00:20:58,050 ‫call my method in here. 297 00:20:58,050 --> 00:21:06,150 ‫So the call method was called enter x or O, and here it's going to say enter X or O, first the player. 298 00:21:06,330 --> 00:21:12,390 ‫All right, it's going to be player and then the input and that's going to be the input. 299 00:21:12,900 --> 00:21:17,550 ‫So I just say the player and the input is what I want to get here. 300 00:21:17,820 --> 00:21:20,610 ‫So I do the same thing for the player, too. 301 00:21:20,670 --> 00:21:25,890 ‫And what that will do is it will simply set it either to X or O. 302 00:21:26,910 --> 00:21:30,000 ‫And it will run the code properly for me. 303 00:21:30,330 --> 00:21:36,250 ‫And by the way, we can get rid of this Tudou because we have replaced it already, so we don't need 304 00:21:36,250 --> 00:21:37,590 ‫that anymore. 305 00:21:41,910 --> 00:21:42,210 ‫All right. 306 00:21:42,210 --> 00:21:45,780 ‫Now let's check for the user input because we don't do that yet. 307 00:21:45,990 --> 00:21:51,900 ‫So in here I want to create another do while do while. 308 00:21:51,900 --> 00:21:53,840 ‫And what is the while? 309 00:21:53,850 --> 00:21:55,200 ‫So what is the condition? 310 00:21:55,320 --> 00:21:59,220 ‫It's that input correct is false. 311 00:21:59,220 --> 00:22:05,490 ‫So as long as the input is incorrect, I want to go ahead and try again. 312 00:22:05,760 --> 00:22:13,610 ‫So in here we're going to ask the player to enter his code and yeah, to go ahead. 313 00:22:13,620 --> 00:22:16,860 ‫So I'm just going to enter this here console. 314 00:22:17,100 --> 00:22:17,730 ‫Right. 315 00:22:18,060 --> 00:22:20,370 ‫And player at the position zero. 316 00:22:20,370 --> 00:22:26,850 ‫So which is the player the turn it is choose your field and now we need to get the input of the user. 317 00:22:26,850 --> 00:22:37,440 ‫So I'm going to use input convert to int 32 and I'm going to use console dot read line. 318 00:22:37,440 --> 00:22:46,650 ‫So I want to get the entry of the user and I want to convert it into our input. 319 00:22:46,650 --> 00:22:51,270 ‫So it's going to use that string and make an integer out of it and store it in input. 320 00:22:51,270 --> 00:22:57,180 ‫And now we can go ahead and use input, but the way we use input here already, but that's no problem 321 00:22:57,180 --> 00:22:58,680 ‫because it's a do while loop. 322 00:22:58,680 --> 00:23:03,360 ‫So it's going to get to this input and I'm going to change it to what we have entered. 323 00:23:03,360 --> 00:23:08,100 ‫So first it's going to be zero, then it's going to check again and then it's going to be what we have 324 00:23:08,100 --> 00:23:08,730 ‫entered. 325 00:23:09,030 --> 00:23:09,420 ‫All right. 326 00:23:09,420 --> 00:23:13,050 ‫So this is going to be our key here. 327 00:23:13,500 --> 00:23:15,390 ‫And now let's run it. 328 00:23:15,390 --> 00:23:16,680 ‫Let's see what happens. 329 00:23:17,160 --> 00:23:17,490 ‫All right. 330 00:23:17,490 --> 00:23:21,240 ‫So that we are and it doesn't show the text that we have just written. 331 00:23:21,240 --> 00:23:22,890 ‫So this whole line here. 332 00:23:22,890 --> 00:23:23,490 ‫Right. 333 00:23:23,490 --> 00:23:26,460 ‫But if I press enter, then it shows up. 334 00:23:26,460 --> 00:23:30,690 ‫And that's because we still have the read key in here. 335 00:23:30,690 --> 00:23:32,270 ‫So set field read key. 336 00:23:32,280 --> 00:23:38,820 ‫We had that earlier on so that we could still see what's in our program or in our game. 337 00:23:38,820 --> 00:23:40,940 ‫And if I get rid of it now, we are fine. 338 00:23:40,950 --> 00:23:41,340 ‫So. 339 00:23:41,340 --> 00:23:46,590 ‫Player one Choose your field and I'm just going to enter one for example, now it says Player to choose 340 00:23:46,590 --> 00:23:50,280 ‫your field and I'm going to use those different fields. 341 00:23:50,280 --> 00:23:54,870 ‫Now what doesn't happen yet is our text is not being updated yet. 342 00:23:54,870 --> 00:23:57,900 ‫So that's something that we have to take care of. 343 00:23:57,930 --> 00:24:03,210 ‫Now, in order to do that, we need to clear our console. 344 00:24:03,210 --> 00:24:11,880 ‫So console dot clear is going to happen here and now what we need to do is to set our field every time 345 00:24:11,880 --> 00:24:13,560 ‫that we are in this do loop. 346 00:24:13,560 --> 00:24:20,640 ‫So the set field should not be run within our main only once, but it should be run every single time 347 00:24:20,640 --> 00:24:23,310 ‫that we are within the do loop. 348 00:24:23,310 --> 00:24:28,920 ‫So it means we will simply set the field in here. 349 00:24:29,490 --> 00:24:30,960 ‫So now let's run it again. 350 00:24:30,990 --> 00:24:35,670 ‫The word please choose your field and I'm just going to use, let's say six. 351 00:24:36,390 --> 00:24:44,160 ‫Now it's place to turn, let's enter four and now we can see that six is suddenly entered. 352 00:24:44,160 --> 00:24:47,670 ‫Now four should be entered the next time that I enter something and there we are. 353 00:24:47,670 --> 00:24:53,610 ‫So we still have the problem that it's updated to late is not updated after the user has entered it, 354 00:24:53,610 --> 00:24:55,170 ‫but it's updated afterwards. 355 00:24:55,170 --> 00:24:57,540 ‫So let's check how we can solve that. 356 00:24:58,470 --> 00:25:02,880 ‫And that is because Set Field is before we check the player. 357 00:25:03,720 --> 00:25:04,980 ‫So let's do that. 358 00:25:04,980 --> 00:25:07,770 ‫Now we check the player and then we do the set field. 359 00:25:07,800 --> 00:25:09,150 ‫Now let's run it again. 360 00:25:09,690 --> 00:25:11,550 ‫So let's see, choose one field. 361 00:25:11,550 --> 00:25:16,560 ‫I'm going to choose field five and we see now it's zero or actually in oh now player two is going to 362 00:25:16,560 --> 00:25:19,050 ‫choose six, that's an X. 363 00:25:19,050 --> 00:25:22,680 ‫Now three is going to be an O, two is going to be an X and so forth. 364 00:25:22,680 --> 00:25:24,420 ‫So we have that functionality. 365 00:25:24,870 --> 00:25:32,010 ‫But what we are still lacking is the capability of checking if the user has entered something that is 366 00:25:32,010 --> 00:25:32,910 ‫allowed or not. 367 00:25:32,910 --> 00:25:36,090 ‫Because what I can still do is I can override. 368 00:25:36,090 --> 00:25:40,740 ‫So let's say player one chooses one player to choose this one again. 369 00:25:40,740 --> 00:25:43,500 ‫So as you can see now from an O, it ran to an X. 370 00:25:43,500 --> 00:25:49,200 ‫So player one is going to say, okay, now I want to have this field so it's going to enter it. 371 00:25:49,200 --> 00:25:54,990 ‫So what we need to do now is we need to check if one player has already entered something and if he 372 00:25:54,990 --> 00:26:01,020 ‫did, we don't want the other player to allow or be allowed to override that field. 373 00:26:01,020 --> 00:26:05,880 ‫So that's something we need to do in this do while loop because in here we check if the input is correct 374 00:26:05,880 --> 00:26:11,430 ‫and we can set the input incorrect if the user didn't enter a correct value. 375 00:26:11,430 --> 00:26:17,280 ‫So by the way, while we're at it, we can also fix a problem that we have here. 376 00:26:17,280 --> 00:26:21,960 ‫So if I enter, let's say a G, then my program crashes. 377 00:26:21,960 --> 00:26:30,150 ‫So I have this input which creates an error so we can get that with a try and catch, so we can solve 378 00:26:30,150 --> 00:26:32,220 ‫the problem with a try catch. 379 00:26:33,870 --> 00:26:36,180 ‫And this is going to be a very, very simple try catch. 380 00:26:36,180 --> 00:26:38,460 ‫So I'm not going to make it more complex than that. 381 00:26:38,460 --> 00:26:39,870 ‫And the only thing that I'm going to. 382 00:26:40,230 --> 00:26:44,100 ‫I'm gonna write onto the console right line. 383 00:26:45,240 --> 00:26:47,730 ‫Please enter a number. 384 00:26:52,040 --> 00:26:57,590 ‫So now if somebody does that again, so I'm going to enter anything. 385 00:26:59,480 --> 00:27:05,900 ‫It's not crashing yet, but it's also not writing onto the console because it's resetting afterwards. 386 00:27:07,670 --> 00:27:08,810 ‫Or is it not? 387 00:27:09,320 --> 00:27:14,210 ‫Well, yeah, it's resetting pretty much because we don't call for an exit statement here. 388 00:27:14,480 --> 00:27:19,070 ‫So what we need to do next, at least we fix that arrow. 389 00:27:19,160 --> 00:27:29,090 ‫Now we need to check if the input is one and the playfield. 390 00:27:32,430 --> 00:27:33,420 ‫At that position. 391 00:27:33,420 --> 00:27:42,030 ‫So at zero zero, which is the one position, is still a character of one and we need another bracket 392 00:27:42,030 --> 00:27:42,480 ‫here. 393 00:27:42,810 --> 00:27:44,220 ‫So that's the case. 394 00:27:45,090 --> 00:27:46,860 ‫Then set input. 395 00:27:48,120 --> 00:27:48,840 ‫Correct. 396 00:27:48,840 --> 00:27:49,740 ‫To true. 397 00:27:53,100 --> 00:27:56,910 ‫And now we need to do the same thing with every single field that we have. 398 00:27:56,910 --> 00:28:08,460 ‫So else, if input is equal to two and the play field is still open at that field at that position, 399 00:28:08,460 --> 00:28:10,920 ‫so it's going to be zero. 400 00:28:11,250 --> 00:28:15,300 ‫One is still a two. 401 00:28:16,110 --> 00:28:19,410 ‫Then please go ahead and set input. 402 00:28:19,410 --> 00:28:21,330 ‫Correct to true as well. 403 00:28:22,980 --> 00:28:26,940 ‫And here I'm missing another equal sign. 404 00:28:27,780 --> 00:28:34,380 ‫So this is what we need to copy and paste now multiple times so that we can catch each case. 405 00:28:34,380 --> 00:28:37,890 ‫So it's three, four, five, six. 406 00:28:39,800 --> 00:28:41,150 ‫And in here. 407 00:28:44,040 --> 00:28:45,590 ‫I ran into an error here. 408 00:28:45,900 --> 00:28:47,100 ‫Seven. 409 00:28:48,360 --> 00:28:49,170 ‫Eight. 410 00:28:51,890 --> 00:28:53,450 ‫And nine. 411 00:28:53,690 --> 00:29:01,460 ‫And now, of course, I need to replace those positions here as well, and I need to adjust those characters. 412 00:29:01,610 --> 00:29:04,190 ‫So seven, eight, nine. 413 00:29:04,580 --> 00:29:08,450 ‫Now we have the three was a02. 414 00:29:08,480 --> 00:29:10,970 ‫The four was at one zero. 415 00:29:13,170 --> 00:29:20,640 ‫Then five was at 1111 actually six was with one two. 416 00:29:21,480 --> 00:29:23,370 ‫That one was a20. 417 00:29:27,520 --> 00:29:30,590 ‫At two one and at two two. 418 00:29:30,640 --> 00:29:37,870 ‫So now we have all the correct cases when everything is good and now we need an LS here. 419 00:29:38,500 --> 00:29:40,900 ‫So else something is not correct. 420 00:29:40,900 --> 00:29:43,780 ‫So we want to inform the user about it. 421 00:29:43,780 --> 00:29:50,620 ‫So console dot right line a new line incorrect input. 422 00:29:51,790 --> 00:30:02,080 ‫Please use another field and we can also make sure that input correct is set to false. 423 00:30:04,790 --> 00:30:08,750 ‫So what we actually can do here is we can create a region. 424 00:30:09,440 --> 00:30:14,540 ‫So I'm going to call that region and it's going to end. 425 00:30:16,490 --> 00:30:19,880 ‫Just after this piece here and region. 426 00:30:22,170 --> 00:30:23,610 ‫So let me do that. 427 00:30:23,610 --> 00:30:28,080 ‫And now I also want to inform about what this region does. 428 00:30:28,080 --> 00:30:37,350 ‫And it's testing or test if a field is already taken, because that's the idea behind this whole region 429 00:30:37,350 --> 00:30:44,040 ‫or behind this whole do while code that we have here which checks okay, is this field taken or not? 430 00:30:44,040 --> 00:30:46,770 ‫If it's taken, then do something else. 431 00:30:46,770 --> 00:30:48,570 ‫If it's not taken, then go ahead. 432 00:30:49,530 --> 00:30:51,000 ‫So now let's run it again. 433 00:30:52,520 --> 00:30:53,120 ‫Let's see. 434 00:30:53,150 --> 00:30:54,590 ‫Player one uses one. 435 00:30:54,590 --> 00:30:58,670 ‫Player two tries to use one again and it says incorrect input. 436 00:30:58,670 --> 00:31:00,800 ‫Please use another field. 437 00:31:02,030 --> 00:31:03,380 ‫So he uses two. 438 00:31:03,380 --> 00:31:04,630 ‫Now let's use that. 439 00:31:04,680 --> 00:31:05,420 ‫Okay. 440 00:31:05,930 --> 00:31:11,630 ‫And as you can see, I can simply enter the different values and all seems to be good. 441 00:31:11,630 --> 00:31:17,450 ‫But as you also can see, player one has already won twice, so he has crossed here. 442 00:31:17,450 --> 00:31:20,270 ‫So that's something that we need to check next. 443 00:31:20,270 --> 00:31:23,120 ‫So we need to check if there is a winner. 444 00:31:23,540 --> 00:31:26,240 ‫And let's create a new region. 445 00:31:28,010 --> 00:31:31,460 ‫Just below set field where we check for the winner. 446 00:31:31,970 --> 00:31:33,980 ‫So here new region. 447 00:31:37,790 --> 00:31:43,250 ‫And I'm going to end the region directly and region. 448 00:31:43,580 --> 00:31:50,810 ‫And in here I have the whole code check winning condition. 449 00:31:53,540 --> 00:31:54,710 ‫And how do we do that? 450 00:31:54,740 --> 00:32:02,360 ‫Well, we need to check which characters do the different players have. 451 00:32:02,360 --> 00:32:05,540 ‫So we had that here player signed for the player. 452 00:32:05,540 --> 00:32:08,540 ‫One is an X for player two is an O. 453 00:32:08,750 --> 00:32:10,880 ‫So what we need to check now. 454 00:32:12,130 --> 00:32:14,110 ‫Or actually was it the right way? 455 00:32:14,260 --> 00:32:14,820 ‫Yeah. 456 00:32:14,830 --> 00:32:16,930 ‫If for play one, it's an X for player two is an O. 457 00:32:16,960 --> 00:32:18,580 ‫So these are the correct characters. 458 00:32:18,580 --> 00:32:24,250 ‫So what we need to check here or I'm going to use it with an array or make it with an array. 459 00:32:24,250 --> 00:32:34,480 ‫Player characters are an X and an O. 460 00:32:35,680 --> 00:32:48,790 ‫And now for each character that I have for each char player char in player chars, I want to check the 461 00:32:48,790 --> 00:32:49,480 ‫winning condition. 462 00:32:49,480 --> 00:32:53,710 ‫So what that will do is it will check if it's true for an X or if it's true for an O. 463 00:32:53,710 --> 00:32:58,090 ‫So in here we simply check, okay, is player one winning or is player two winning? 464 00:32:58,090 --> 00:33:11,890 ‫And we do that by checking if play field zero zero is equal to player char. 465 00:33:14,470 --> 00:33:17,920 ‫And Playfield 466 00:33:20,110 --> 00:33:22,630 ‫zero one. 467 00:33:24,980 --> 00:33:41,330 ‫Is equal to player char and also play field at zero two is equal to player char. 468 00:33:43,640 --> 00:33:48,710 ‫So if that's the case, then go ahead and run some code. 469 00:33:48,740 --> 00:33:58,070 ‫So in here I'm just going to write something like for now, see w we have a winner. 470 00:33:59,330 --> 00:34:04,370 ‫All right, so I just want to test that before we write the whole if statement, which is much, much 471 00:34:04,370 --> 00:34:04,960 ‫longer. 472 00:34:04,970 --> 00:34:10,880 ‫So player one is going to win the first row because this is the only row that we have entered here. 473 00:34:10,880 --> 00:34:18,830 ‫So in case the for all the three fields within that row, the, the character is the same. 474 00:34:18,830 --> 00:34:19,520 ‫We have a winner. 475 00:34:19,520 --> 00:34:22,550 ‫So that's pretty much the only thing that we have set in here. 476 00:34:22,880 --> 00:34:26,090 ‫So I'm going to say, okay, player one is going to be one. 477 00:34:26,090 --> 00:34:33,830 ‫Player two is going to be for player one is going to be two, five and three and we have a winner. 478 00:34:33,830 --> 00:34:39,680 ‫So you see the first row is full of zeros or of Oh's, so that means we have a winner. 479 00:34:42,400 --> 00:34:47,590 ‫And now what we need to do is we need to replace that with all the different conditions that we can 480 00:34:47,590 --> 00:34:48,210 ‫have. 481 00:34:48,220 --> 00:34:53,930 ‫So with all the different positions and if you look at the game, I'm just going to open it up again. 482 00:34:53,950 --> 00:34:58,290 ‫The possible winning conditions are one, two, three, four, five, six, seven, eight, nine. 483 00:34:58,300 --> 00:35:06,040 ‫Then we have 753159147258 and 369. 484 00:35:06,040 --> 00:35:07,440 ‫So we have one, two, three. 485 00:35:07,450 --> 00:35:08,410 ‫So six. 486 00:35:09,320 --> 00:35:10,320 ‫Plus two. 487 00:35:10,340 --> 00:35:15,950 ‫So we have eight different conditions in which we have a winner, and that's pretty much what we need 488 00:35:15,950 --> 00:35:16,550 ‫to do here. 489 00:35:16,550 --> 00:35:21,830 ‫So we need to create this eight times with the different options. 490 00:35:21,830 --> 00:35:24,530 ‫So I'm going to stop that here. 491 00:35:26,240 --> 00:35:31,940 ‫And each time we need to use an or sign because those are the or winning conditions. 492 00:35:32,180 --> 00:35:36,470 ‫So I'm going to do that six more times. 493 00:35:38,870 --> 00:35:43,040 ‫So we have one, two, three, four, five, six, seven, eight. 494 00:35:43,190 --> 00:35:43,940 ‫That's good. 495 00:35:43,940 --> 00:35:48,910 ‫And now, as I said, the different conditions are zero, zero and so forth. 496 00:35:48,920 --> 00:36:00,860 ‫Now the same thing with the one one, 011 and one two, and then with a third row 222021 and two two, 497 00:36:01,790 --> 00:36:06,770 ‫then we have the horizontal ones going from top to bottom. 498 00:36:06,770 --> 00:36:12,800 ‫So zero zero, then one zero and two zero. 499 00:36:14,000 --> 00:36:16,820 ‫Then we have. 500 00:36:18,900 --> 00:36:22,470 ‫One and one, actually. 501 00:36:22,470 --> 00:36:23,280 ‫Zero and one. 502 00:36:23,280 --> 00:36:25,950 ‫Sorry, zero one. 503 00:36:26,860 --> 00:36:34,470 ‫Ten one one and 1202, two, one and two, two. 504 00:36:34,480 --> 00:36:36,930 ‫And now we have the diagonal ones. 505 00:36:36,940 --> 00:36:38,770 ‫So it's zero zero. 506 00:36:38,800 --> 00:36:43,060 ‫Then let me check at zero zero. 507 00:36:44,980 --> 00:36:49,480 ‫Then one, one and two, two. 508 00:36:49,960 --> 00:36:52,690 ‫And finally, we have. 509 00:36:54,430 --> 00:36:57,850 ‫One, two, one, one. 510 00:36:57,850 --> 00:36:59,080 ‫And. 511 00:37:01,530 --> 00:37:03,810 ‫Two zero. 512 00:37:04,440 --> 00:37:07,280 ‫So those should be the eight conditions that we have. 513 00:37:07,290 --> 00:37:10,620 ‫And now we only need to check who has one. 514 00:37:10,980 --> 00:37:16,440 ‫So what we check here is with this long statement. 515 00:37:19,320 --> 00:37:19,950 ‫If. 516 00:37:21,330 --> 00:37:27,270 ‫Player, character or player char is equal to an x. 517 00:37:27,960 --> 00:37:32,970 ‫We know that player two has one. 518 00:37:35,280 --> 00:37:44,190 ‫So console red line in a new line player two has one. 519 00:37:45,690 --> 00:37:50,230 ‫And here I actually need to be very careful like that. 520 00:37:50,250 --> 00:37:50,820 ‫All right. 521 00:37:50,820 --> 00:37:52,650 ‫So that's the one case. 522 00:37:52,650 --> 00:37:56,820 ‫And then the other case is that the other player has one. 523 00:37:56,820 --> 00:38:01,680 ‫So I'm just going to use ls here and it's going to say player one has one. 524 00:38:02,310 --> 00:38:04,980 ‫And I also want to break out of the game here. 525 00:38:04,980 --> 00:38:06,960 ‫So break in here. 526 00:38:08,250 --> 00:38:13,320 ‫So let's run it and see if we can actually reproduce that. 527 00:38:13,320 --> 00:38:15,660 ‫So let's say we want to have the diagonal one. 528 00:38:15,660 --> 00:38:23,520 ‫So one, two, five, three, nine and we see player one has one. 529 00:38:24,360 --> 00:38:30,990 ‫So what we want now is we want to reset the game and we want to allow to create a new round. 530 00:38:30,990 --> 00:38:34,560 ‫So the player should be able to run again and play again. 531 00:38:34,560 --> 00:38:38,580 ‫And what we also haven't covered is a draw, so we need to still cover the draw. 532 00:38:38,820 --> 00:38:42,090 ‫But first, let's reset the field once we have a winner. 533 00:38:42,900 --> 00:38:49,410 ‫So what I want to do is before I break out, I want to reset the field. 534 00:38:49,410 --> 00:38:59,610 ‫So in here to do reset field and in order to set a field, we already have a function for that or a 535 00:38:59,610 --> 00:38:59,970 ‫method. 536 00:38:59,970 --> 00:39:03,270 ‫We created this set field method which does just that. 537 00:39:03,270 --> 00:39:04,920 ‫It sets the field once again. 538 00:39:04,920 --> 00:39:13,620 ‫But what it doesn't do yet, it doesn't reset our play field because our play field currently is set 539 00:39:13,620 --> 00:39:15,030 ‫to a specific value. 540 00:39:15,030 --> 00:39:18,450 ‫So this could be an X, this could be an O and so forth. 541 00:39:18,600 --> 00:39:23,790 ‫So in order to make sure that we can always reset, we need to copy that. 542 00:39:24,630 --> 00:39:31,110 ‫And it's standard version and I'm going to call it play field initial. 543 00:39:31,560 --> 00:39:36,900 ‫So this is how it's initially and this is the one that we constantly override. 544 00:39:37,440 --> 00:39:40,920 ‫So now we need another method which will do the resetting. 545 00:39:40,980 --> 00:39:50,040 ‫And the resetting method is going to be called public static void reset field. 546 00:39:52,080 --> 00:39:59,550 ‫And what I need to do here is, first of all, I need to set my play field to the play field initial. 547 00:40:00,420 --> 00:40:11,490 ‫So how it was initially and then I can set my field now let's reset the game once the player has one 548 00:40:11,490 --> 00:40:13,410 ‫and that's in here. 549 00:40:13,410 --> 00:40:15,990 ‫So player one has one or player two has one. 550 00:40:15,990 --> 00:40:19,530 ‫So let's reset just here, reset field. 551 00:40:22,650 --> 00:40:30,930 ‫And by the way, maybe we should also tell the player or tell the user that they can reset the game. 552 00:40:31,350 --> 00:40:41,010 ‫So console right line, please, and or press any key to reset the game. 553 00:40:42,480 --> 00:40:43,980 ‫And here we just say. 554 00:40:46,220 --> 00:40:54,410 ‫Console the red key and afterwards reset the game. 555 00:40:59,090 --> 00:40:59,230 ‫Okay. 556 00:40:59,390 --> 00:41:00,320 ‫Let's test it. 557 00:41:01,640 --> 00:41:02,540 ‫Choose your field. 558 00:41:02,540 --> 00:41:06,890 ‫1 to 5 three nine. 559 00:41:07,610 --> 00:41:08,780 ‫Player one has one. 560 00:41:08,780 --> 00:41:11,750 ‫Player, please press any key to reset the game. 561 00:41:12,560 --> 00:41:14,690 ‫I press any key and there they are. 562 00:41:14,720 --> 00:41:20,910 ‫We have reset the game and now it's players two turns player to turn. 563 00:41:20,930 --> 00:41:21,730 ‫As you can see here. 564 00:41:21,830 --> 00:41:23,000 ‫Choose your field. 565 00:41:23,030 --> 00:41:24,050 ‫Now let's try again. 566 00:41:24,050 --> 00:41:28,040 ‫As you can see, we have the different order now. 567 00:41:29,360 --> 00:41:32,570 ‫PLAYER two As one, please press any key to reset the game. 568 00:41:32,600 --> 00:41:33,320 ‫Great. 569 00:41:33,350 --> 00:41:38,930 ‫If I type anything incorrect and here I still get this error, but that's fine. 570 00:41:38,930 --> 00:41:40,190 ‫And that's exactly what I wanted. 571 00:41:40,190 --> 00:41:41,510 ‫So I catch it. 572 00:41:41,540 --> 00:41:43,130 ‫I catch the error with it. 573 00:41:43,130 --> 00:41:44,210 ‫Try catch that we had. 574 00:41:44,240 --> 00:41:48,140 ‫So now there is only one thing left and that's if there is a draw. 575 00:41:48,140 --> 00:41:50,390 ‫And there are many ways to solve this problem. 576 00:41:50,390 --> 00:41:56,390 ‫And the easiest one is to create a variable which is called turns, and this variable is just going 577 00:41:56,390 --> 00:42:02,310 ‫to hold the amount of turns that we have played already. 578 00:42:02,330 --> 00:42:05,300 ‫End turns is equal to zero. 579 00:42:05,300 --> 00:42:12,560 ‫So at the start we have zero turns and each time that we reset the field or set the field, we want 580 00:42:12,560 --> 00:42:14,300 ‫to increase turns by one. 581 00:42:16,170 --> 00:42:18,000 ‫Because this method. 582 00:42:18,000 --> 00:42:19,020 ‫When is it called? 583 00:42:19,050 --> 00:42:20,910 ‫Let's check set field. 584 00:42:21,630 --> 00:42:24,330 ‫It's called each time we go into this do while loop. 585 00:42:24,330 --> 00:42:29,250 ‫And that's only when there was a correct entry by the user. 586 00:42:29,940 --> 00:42:36,090 ‫Now if we have this turns field, we also need to be careful that we reset the turns field. 587 00:42:36,540 --> 00:42:42,330 ‫Once we reset our whole field by field, I mean like this is the field of our game. 588 00:42:42,330 --> 00:42:47,190 ‫But the turns is a field because it's a global variable, right? 589 00:42:47,310 --> 00:42:50,820 ‫So I want to reset turns once we reset the game. 590 00:42:50,820 --> 00:43:05,130 ‫So reset field here, we say turns is equal to zero or actually to be more precise because we increase 591 00:43:05,130 --> 00:43:08,640 ‫turns by one already, we need to do it just afterwards. 592 00:43:08,850 --> 00:43:13,360 ‫So now we only need to check if this condition is true. 593 00:43:13,380 --> 00:43:19,380 ‫So here we had the winning condition, if an else. 594 00:43:19,380 --> 00:43:21,570 ‫And here this if statement. 595 00:43:22,320 --> 00:43:24,480 ‫And here we have the WL statement. 596 00:43:24,480 --> 00:43:29,520 ‫So if we don't have a winner, we have a draw. 597 00:43:29,940 --> 00:43:38,580 ‫Or actually to be more precise else if turns is equal to ten, then we have a draw. 598 00:43:39,000 --> 00:43:43,950 ‫So what we say here is the same thing that we do here. 599 00:43:45,360 --> 00:43:52,710 ‫We also need to break out of the game and we also need to reset the game. 600 00:43:53,740 --> 00:43:58,210 ‫So reset field and. 601 00:43:59,560 --> 00:44:02,500 ‫We should inform the player that there was no winner. 602 00:44:03,880 --> 00:44:08,950 ‫So I'm just gonna enter draw as text here. 603 00:44:09,550 --> 00:44:14,200 ‫All right, now we solve one problem in the game, so let's, first of all, get a draw. 604 00:44:14,230 --> 00:44:15,610 ‫Let's check the draw first. 605 00:44:15,610 --> 00:44:18,880 ‫So, one, two, three, four. 606 00:44:20,970 --> 00:44:23,100 ‫Six five. 607 00:44:24,190 --> 00:44:29,410 ‫Eight, nine and seven. 608 00:44:29,410 --> 00:44:30,820 ‫And we see we get the draw. 609 00:44:30,850 --> 00:44:31,610 ‫That's fine. 610 00:44:31,630 --> 00:44:33,370 ‫Press any key to reset the game. 611 00:44:33,640 --> 00:44:34,600 ‫That works as well. 612 00:44:34,600 --> 00:44:36,070 ‫Now let's try it again. 613 00:44:36,790 --> 00:44:38,800 ‫Actually, not drawing, but winning. 614 00:44:39,220 --> 00:44:40,910 ‫And we see player two is one. 615 00:44:40,930 --> 00:44:49,300 ‫I press enter and our game does not reset and that has to do with our static variable here. 616 00:44:49,300 --> 00:44:56,440 ‫So this character playfield initial and what I need to do is I need to get rid of it here and I need 617 00:44:56,440 --> 00:45:06,430 ‫to make it and static actually to create a local variable which is going to be inside our reset field. 618 00:45:06,430 --> 00:45:13,000 ‫So I'm just going to create this play field initial and I'm going to set that into my play field. 619 00:45:13,750 --> 00:45:15,760 ‫So let's test that once again. 620 00:45:15,760 --> 00:45:20,950 ‫One, two, three, four, five, six, seven. 621 00:45:21,070 --> 00:45:22,120 ‫Play one has one. 622 00:45:22,140 --> 00:45:26,980 ‫Okay, now one, two, three, four, five, six, seven. 623 00:45:26,980 --> 00:45:30,220 ‫Play two is one under a key and you see it resets. 624 00:45:30,220 --> 00:45:36,790 ‫Now, something that I have figured out or tested and saw that there was still a little error is two, 625 00:45:37,210 --> 00:45:39,130 ‫one, three. 626 00:45:40,680 --> 00:45:45,270 ‫Four, then five, six and eight. 627 00:45:46,640 --> 00:45:53,630 ‫And as you can see, we have a condition where the player has one, but it's not caught up so or it's 628 00:45:53,630 --> 00:45:55,460 ‫not catched or caught here. 629 00:45:55,460 --> 00:45:57,200 ‫So you can see player two has one. 630 00:45:57,200 --> 00:46:02,090 ‫So even though player one has one with the 000 or the zero. 631 00:46:02,090 --> 00:46:07,520 ‫So there is still a little problem in my conditions here and you might have figured it out earlier on 632 00:46:07,610 --> 00:46:08,210 ‫already. 633 00:46:08,210 --> 00:46:11,120 ‫So I'm going to show you how to fix that. 634 00:46:11,690 --> 00:46:13,910 ‫And that should be this condition here. 635 00:46:13,910 --> 00:46:24,680 ‫So zero one, then it's one one and this one shouldn't be one two, but it should be two one. 636 00:46:25,930 --> 00:46:27,550 ‫Now let's test that again. 637 00:46:29,000 --> 00:46:31,200 ‫Let's see if we catch this condition. 638 00:46:31,220 --> 00:46:37,400 ‫So one, two, three, five, four, eight. 639 00:46:37,520 --> 00:46:39,170 ‫And we see player two as one. 640 00:46:39,320 --> 00:46:40,940 ‫All right, so we have that one. 641 00:46:40,940 --> 00:46:43,520 ‫And yeah, so that's pretty much it. 642 00:46:43,550 --> 00:46:46,270 ‫Our game runs and it does what it should. 643 00:46:46,280 --> 00:46:49,400 ‫There are some things that you could still improve, of course. 644 00:46:49,400 --> 00:46:52,970 ‫So for example, this character sign. 645 00:46:52,970 --> 00:47:00,080 ‫So we hand over an input and we check that input once again and we create a character sign and so forth. 646 00:47:00,080 --> 00:47:03,590 ‫But we could make it even easier. 647 00:47:03,590 --> 00:47:07,370 ‫So we don't take the player itself here. 648 00:47:07,370 --> 00:47:14,450 ‫But what we do take is a character which is the player char or player sign. 649 00:47:14,450 --> 00:47:16,880 ‫So it should be this could be the same thing. 650 00:47:17,060 --> 00:47:20,240 ‫So player sign makes more sense because we have used it here. 651 00:47:20,240 --> 00:47:23,750 ‫So then we wouldn't need to create all of that code here. 652 00:47:23,870 --> 00:47:25,760 ‫So let's get rid of that. 653 00:47:25,760 --> 00:47:32,750 ‫And instead when we call this method, which we do here, we hand over the character. 654 00:47:32,750 --> 00:47:40,400 ‫So for player one, we said it was the oh, I think and for player two it was the X. 655 00:47:40,910 --> 00:47:44,330 ‫So that will do the same thing. 656 00:47:45,680 --> 00:47:53,960 ‫And now we have decreased our code lines a little bit and you can even go ahead and improve the code 657 00:47:53,960 --> 00:47:54,590 ‫even further. 658 00:47:54,590 --> 00:48:00,410 ‫But that was just some code that I think that you should be able to write based on what you have learned 659 00:48:00,410 --> 00:48:01,970 ‫so far in this course. 660 00:48:03,790 --> 00:48:04,150 ‫All right. 661 00:48:04,150 --> 00:48:08,710 ‫So I hope you manage to do that, even though it looked very different in your case. 662 00:48:08,710 --> 00:48:11,110 ‫And please go ahead and share your solution. 663 00:48:11,110 --> 00:48:15,980 ‫Or if you took my solution that I have here and improved it, please go ahead and share that as well. 664 00:48:16,000 --> 00:48:18,150 ‫And yeah, so that's pretty much it. 665 00:48:18,160 --> 00:48:23,710 ‫A pretty long video, I know that, but I hope you learned a lot from it. 666 00:48:23,710 --> 00:48:28,240 ‫And even if you didn't do it yourself, you just followed it along and tried to build it. 667 00:48:28,360 --> 00:48:33,460 ‫So see you in the next video where we're going to look into a different topic. 668 00:48:33,460 --> 00:48:34,350 ‫So see you there.