1 00:00:00,270 --> 00:00:04,980 Part two of the challenge was the code, the logic for taking turns, so in this video, I'm going to 2 00:00:04,980 --> 00:00:10,050 go through the solutions for tasks three to five, starting with task three, which tells you to run 3 00:00:10,050 --> 00:00:11,460 a loop that goes through each turn. 4 00:00:11,910 --> 00:00:14,220 There are nine turns in a game of tic tac toe. 5 00:00:14,220 --> 00:00:20,850 So I'm going to create a loop that runs nine times for enties equal to zero I smaller than nine I a 6 00:00:20,850 --> 00:00:21,600 plus plus. 7 00:00:24,530 --> 00:00:26,900 And for now, we'll print the counter during each run. 8 00:00:29,800 --> 00:00:33,480 I know we didn't write a lot of code, but it's always a good idea to keep testing. 9 00:00:33,700 --> 00:00:36,520 I want to get you in the habit of doing that, so I'll run. 10 00:00:43,710 --> 00:00:47,730 First, it prints the grid and then it prints a bunch of numbers nine times. 11 00:00:53,800 --> 00:00:58,180 We can now remove the print statement, the next step is to create an if statement that runs code if 12 00:00:58,180 --> 00:01:04,360 it's X's turn, as well as an else plot that runs code if it's ostern, I left you with a hint to learn 13 00:01:04,360 --> 00:01:09,280 the parts explains when the counter is even, whereas O plays on the counter is Ezzard. 14 00:01:12,560 --> 00:01:17,810 A number is even if dividing it by two returns, the remainder of zero zero, I'm going to check if 15 00:01:17,810 --> 00:01:20,950 the number modulus two is equal to zero. 16 00:01:25,590 --> 00:01:26,400 Then I'll print. 17 00:01:30,580 --> 00:01:33,250 Turn X otherwise. 18 00:01:39,900 --> 00:01:40,770 Turno. 19 00:01:41,870 --> 00:01:43,680 All right, and that's really it for TASC three. 20 00:01:43,700 --> 00:01:44,960 Feel free to test your code. 21 00:01:52,810 --> 00:01:55,270 And as expected, it keeps changing turns. 22 00:01:56,140 --> 00:02:00,660 Now, before we read any more logic inside the final statements, we need to create some functions. 23 00:02:00,850 --> 00:02:02,710 So moving on to task for. 24 00:02:07,420 --> 00:02:10,600 Our next task is to write a function that lets the user choose a spot. 25 00:02:12,910 --> 00:02:15,760 So we'll follow our comment public static. 26 00:02:20,170 --> 00:02:22,060 The function returns an entero. 27 00:02:27,170 --> 00:02:28,940 The function is called ask user. 28 00:02:31,860 --> 00:02:34,260 And it receives one parameter, the board. 29 00:02:40,900 --> 00:02:46,060 In order to satisfy this area, we need to create an array and return it, but I don't feel like creating 30 00:02:46,060 --> 00:02:46,490 an array. 31 00:02:46,600 --> 00:02:51,040 This is actually a great time to employ the array trick that I left you in the cheat sheet. 32 00:02:53,970 --> 00:03:00,140 I provided you with the syntax and what this does is it offers a cool way to return a raise on the fly, 33 00:03:00,900 --> 00:03:04,410 so I'm going to follow the syntax and return a new integer array. 34 00:03:07,950 --> 00:03:10,470 Were the random values of 011 for now. 35 00:03:12,040 --> 00:03:14,820 Now, you know me, I like to keep testing my code as I write it. 36 00:03:15,880 --> 00:03:19,570 And there is still a lot of code we need to write for this function, so as you're writing it, we're 37 00:03:19,570 --> 00:03:25,860 going to call it for each turne ask user pass in the board and call the function for Ostern as well. 38 00:03:32,390 --> 00:03:34,530 Inside the function, I left you some instructions. 39 00:03:34,550 --> 00:03:38,930 First, we're going to ask the user to pick a spot on the grid in the form of a row and column number. 40 00:03:39,980 --> 00:03:45,020 If you go back to learn the part, pay attention to the output, the user writes each valley beside 41 00:03:45,020 --> 00:03:45,620 the question. 42 00:03:49,330 --> 00:03:52,720 Accordingly, I'm going to print the message using print, not print line. 43 00:04:07,140 --> 00:04:12,630 And since we're asking the user to input two values, we need to call upon Skinner, I declared Skinner 44 00:04:12,630 --> 00:04:15,990 for you at the level of the class so we should be able to access it from anywhere. 45 00:04:17,930 --> 00:04:19,820 And here, I'm going to use it to pick up the RO. 46 00:04:28,310 --> 00:04:30,980 The element in that row, i.e., the column. 47 00:04:35,500 --> 00:04:40,510 OK, I think we have enough to start seeing some pretty cool output, run your code. 48 00:04:55,590 --> 00:04:58,980 And you know what, I'm going to add some white space right after the Colin. 49 00:05:04,140 --> 00:05:07,290 I'll use controls to stop the output we run the code. 50 00:05:13,670 --> 00:05:18,050 Turn exile to zero zero and inside the row, I'll choose the first inducts. 51 00:05:22,620 --> 00:05:27,810 Here, I'll do Rosero second index, and this loop runs exactly nine times. 52 00:05:29,140 --> 00:05:32,710 So players can keep picking spots until the game ends. 53 00:05:41,450 --> 00:05:42,440 All right, good. 54 00:05:42,470 --> 00:05:44,600 After nine turns, the loop breaks. 55 00:05:47,680 --> 00:05:52,750 Now, instead of returning to random numbers, the function is going to return the spot that the user 56 00:05:52,750 --> 00:05:53,440 chooses. 57 00:05:59,720 --> 00:06:04,550 All right, there's another step for task force, but we can't do it yet right now, we need to populate 58 00:06:04,550 --> 00:06:06,230 the board with X's and O's. 59 00:06:10,860 --> 00:06:12,240 And that would be task five. 60 00:06:14,620 --> 00:06:20,350 During each turn, the function call returns an array of integers that contains the user spot, so we 61 00:06:20,350 --> 00:06:22,110 need to store that array in a variable. 62 00:06:22,750 --> 00:06:25,000 So I'm going to create a variable int spot. 63 00:06:27,770 --> 00:06:33,050 And whether it's Axis Turner O's turn, we're going to set the spot variable equal to the return value. 64 00:06:36,050 --> 00:06:38,720 We're going to use the spot variable to update the board. 65 00:06:41,450 --> 00:06:43,580 The first bracket specifies the row. 66 00:06:45,330 --> 00:06:49,680 And we know the road that the user chooses is index zero of our spot array. 67 00:06:51,130 --> 00:06:57,250 So we'll use that value to index the row and the column, the element in that row would be in the second 68 00:06:57,250 --> 00:06:59,080 index of our spot array. 69 00:07:00,280 --> 00:07:06,810 And if we're inside the statement, we're going to update that spot on the board to X and if we're inside 70 00:07:06,820 --> 00:07:08,330 or else it's OK turn. 71 00:07:08,350 --> 00:07:11,830 So we're going to update the spot that they choose on the board of. 72 00:07:24,220 --> 00:07:28,810 After the player picks their spot and we update the border Ray, we need to print the board and thank 73 00:07:28,810 --> 00:07:31,270 goodness we can just call the function print board. 74 00:07:32,350 --> 00:07:33,670 OK, now run your code. 75 00:07:45,020 --> 00:07:47,060 I'm going to pick the spot zero zero. 76 00:07:51,820 --> 00:07:53,380 And check it at. 77 00:07:55,930 --> 00:07:59,470 The function excuser return to my spot is an array of two integers. 78 00:08:01,960 --> 00:08:06,910 And we're using each integer to index the board and update the first element at Rosero. 79 00:08:08,810 --> 00:08:11,090 After updating the array, we're reprinting it. 80 00:08:15,900 --> 00:08:21,570 Now I'm going to update the third element of Roe to euro to index two, and everything works perfectly. 81 00:08:23,940 --> 00:08:27,450 I'll choose the first element from one zero one zero. 82 00:08:28,850 --> 00:08:36,080 The third element in row zero zero two and everything works well, OK, we're looking good so far. 83 00:08:40,630 --> 00:08:42,730 And you know what, I'm going to rerun the code. 84 00:08:42,760 --> 00:08:48,010 There is one thing I forgot to address, and that is the fact that the user can override a previous 85 00:08:48,010 --> 00:08:48,440 spot. 86 00:08:48,910 --> 00:08:50,260 Let me actually demonstrate that. 87 00:09:02,550 --> 00:09:07,530 If I pick a spot to zero, players should not be allowed to take that spot. 88 00:09:10,770 --> 00:09:14,340 But it does happen and we need to come up with a strategy to prevent this. 89 00:09:18,730 --> 00:09:23,920 If we turn back the task for there was still one more thing we had to do inside the function, and that 90 00:09:23,920 --> 00:09:25,630 was to check if the spot is taken. 91 00:09:25,840 --> 00:09:27,550 If so, let the user choose again. 92 00:09:29,220 --> 00:09:34,800 Conveniently, the function receives the board as a parameter so we can use a while loop and I can keep 93 00:09:34,800 --> 00:09:38,370 asking the user to pick a new spot if the one that they choose is taken. 94 00:09:39,090 --> 00:09:41,790 So while the spot that they choose. 95 00:09:44,260 --> 00:09:45,910 Is already equal to X. 96 00:09:48,730 --> 00:09:49,150 Or. 97 00:09:50,940 --> 00:09:52,380 If it's already equal to oh. 98 00:10:01,320 --> 00:10:02,400 Then we're going to print. 99 00:10:06,890 --> 00:10:07,970 Spot taken. 100 00:10:08,220 --> 00:10:09,050 Try again. 101 00:10:13,130 --> 00:10:17,390 It's important that we use print, not print line, because once again, we need the user to enter his 102 00:10:17,390 --> 00:10:22,990 enter beside the prompts and first we pick up the row, row is equal to scan to an extent. 103 00:10:28,870 --> 00:10:30,460 And then we'll pick up the Cullom. 104 00:10:36,660 --> 00:10:38,880 And I believe that wraps up part two. 105 00:10:41,830 --> 00:10:44,980 Because we're officially done tasks three, four and five. 106 00:10:48,630 --> 00:10:49,620 Let's run the code. 107 00:10:59,250 --> 00:11:01,140 I'm going to pick up some random spots. 108 00:11:10,680 --> 00:11:11,310 OK, good. 109 00:11:11,490 --> 00:11:13,770 Now I'm going to pick a spot that's already taken. 110 00:11:16,640 --> 00:11:21,920 It stops me from doing so, and it's going to keep stopping me until I choose one that isn't taken. 111 00:11:32,280 --> 00:11:33,150 Purrfect. 112 00:11:46,500 --> 00:11:52,080 What's happening is once the user picks a spot that isn't taken, the while loop breaks, it returns 113 00:11:52,080 --> 00:11:57,540 the spot in the form of an array and we're using the spot to index the board and update it with the 114 00:11:57,540 --> 00:11:58,650 appropriate symbol. 115 00:12:00,080 --> 00:12:04,490 Now, I'd like to show you that there are always so many ways to write code that achieves the exact 116 00:12:04,490 --> 00:12:08,720 same output, we know that the underscore represents a free spot. 117 00:12:13,670 --> 00:12:19,790 So instead of this big logical comparison, I can just check if the spot the player chooses is not free. 118 00:12:24,210 --> 00:12:26,510 Then keep asking them to pick another one. 119 00:12:29,750 --> 00:12:32,330 This code should achieve the exact same output. 120 00:12:38,170 --> 00:12:39,910 Once again, I'll pick random spots. 121 00:12:43,930 --> 00:12:51,010 OK, good, now pick a spot that's already taken, it stops me from doing so and like before it's going 122 00:12:51,010 --> 00:12:54,010 to keep stopping me until I choose one that isn't taken. 123 00:12:58,180 --> 00:12:59,020 Beautiful. 124 00:12:59,260 --> 00:13:00,790 Our game is really coming together. 125 00:13:00,820 --> 00:13:02,320 I'll see you in part three.