1
00:00:00,360 --> 00:00:03,270
In part three, the challenge was to write code that determines the winner.

2
00:00:05,340 --> 00:00:10,080
Right now, the app lets the player choose a spot on the board, but we need to write logic that stops

3
00:00:10,080 --> 00:00:14,490
the game as soon as one of the player gets three straight access or three straight O's.

4
00:00:16,520 --> 00:00:19,100
This is not easy to implement, so I'll go through it slowly.

5
00:00:25,670 --> 00:00:30,950
Here in TASC six, we can say with the usual public static, the function returns an integer.

6
00:00:33,440 --> 00:00:34,670
It's called Chegwidden.

7
00:00:36,440 --> 00:00:39,050
And it receives the tic tac toe board as a parameter.

8
00:00:46,560 --> 00:00:51,830
Now, inside the function, the first step is to make a count variable that starts at zero in count

9
00:00:51,840 --> 00:00:52,820
is equal to zero.

10
00:00:54,860 --> 00:00:58,280
And we can silence this error by returning the count right now.

11
00:01:01,080 --> 00:01:03,660
That was easy, but task sex is not over yet.

12
00:01:04,620 --> 00:01:07,440
If you scroll all the way up, I left you some instructions.

13
00:01:09,310 --> 00:01:11,590
So here I need to call the function check when.

14
00:01:19,090 --> 00:01:24,430
And I hope it's clear to you that every time the loop runs during every single turn, we're using check

15
00:01:24,430 --> 00:01:26,140
when to check if anybody one.

16
00:01:27,940 --> 00:01:32,680
Quinn returns an integer, and I'm going to store this return value in an integer called Count.

17
00:01:33,820 --> 00:01:39,550
All right, if that injury happens to be three, that means X got three in a row, so we need to print

18
00:01:39,550 --> 00:01:40,300
X wins.

19
00:01:40,720 --> 00:01:44,020
I'm going to manifest this in the code F count equals three.

20
00:01:53,930 --> 00:01:55,460
Print X wins.

21
00:01:59,790 --> 00:02:05,610
And break the loop if it's the opposite, if the energy return from calling the check, one function

22
00:02:05,610 --> 00:02:06,960
is equal to negative three.

23
00:02:14,920 --> 00:02:16,750
Then we can print Erwin's.

24
00:02:21,230 --> 00:02:22,340
And break the loop.

25
00:02:28,860 --> 00:02:31,530
In essence, we're checking for a winner after every single turn.

26
00:02:34,770 --> 00:02:37,830
So right now, our function doesn't do anything, it just return zero.

27
00:02:38,100 --> 00:02:40,030
So we need to start with task seven.

28
00:02:40,200 --> 00:02:43,460
We need to check every single row for straight Xs or straight oaks.

29
00:02:43,860 --> 00:02:47,460
Now, running through every character in a 2D array requires a nested loop.

30
00:02:48,060 --> 00:02:50,560
The outer loop is going to start with a row index of zero.

31
00:02:51,180 --> 00:02:55,650
It's going to keep running as long as the row index is less than the length of our board, which is

32
00:02:55,650 --> 00:02:57,030
three, the number of rows.

33
00:03:00,490 --> 00:03:05,050
And then as we hit the inner loop, Jay is going to start by indexing the first element in the current

34
00:03:05,050 --> 00:03:11,020
row, Jake will zero and the loop keeps running until Jay indexes every element in that row.

35
00:03:13,700 --> 00:03:14,810
J plus plus.

36
00:03:22,600 --> 00:03:28,210
And remember that the first bracket gets Dré, the road that we're interested in, we will use the Arrow

37
00:03:28,210 --> 00:03:32,890
Index to index each arrow and the second bracket gets the element in that row.

38
00:03:33,340 --> 00:03:36,990
We're going to use the counter j to index every element in the current row.

39
00:03:37,660 --> 00:03:44,170
So as the outer loop counterspy is fixed to a row, the inner loop countered J indexes every value in

40
00:03:44,170 --> 00:03:47,520
that row as a reminder of how nested loops work.

41
00:03:47,530 --> 00:03:49,090
Here's an animation that I made for you.

42
00:03:49,720 --> 00:03:53,680
If you'd like to skip it, then you can fast forward to the following timestamp.

43
00:04:43,450 --> 00:04:48,160
OK, as the inner loop runs through every character and a fixed arrow, we're going to check if that

44
00:04:48,160 --> 00:04:49,330
character is inducts.

45
00:04:56,890 --> 00:04:59,200
If so, we're going to add one to the counts.

46
00:05:05,210 --> 00:05:07,970
If the character is, oh, we're going to subtract one.

47
00:05:21,460 --> 00:05:25,810
So after the inner loop, we just went through an entire row of characters and we're going to check

48
00:05:25,810 --> 00:05:29,500
if the row count resulted in a value of three or negative three.

49
00:05:41,340 --> 00:05:45,330
If so, we're going to break the whole function prematurely by returning the counts.

50
00:05:47,830 --> 00:05:50,020
Otherwise, we have to reset the count.

51
00:05:50,530 --> 00:05:52,150
This is a really important.

52
00:05:54,190 --> 00:05:57,790
Because you want your next inner loop to start counting again from zero.

53
00:06:00,060 --> 00:06:05,060
And with that being said, our code is now able to check every row for a straight X or a straight O.

54
00:06:06,500 --> 00:06:07,490
That's around the code.

55
00:06:15,460 --> 00:06:17,650
I'm going to keep playing put x.

56
00:06:19,590 --> 00:06:20,100
Oh.

57
00:06:24,710 --> 00:06:30,890
Oh, and before I put my last text, let's make sure we understand what's really going on here, after

58
00:06:30,890 --> 00:06:34,160
I played the latest video, it printed the board, obviously.

59
00:06:36,290 --> 00:06:37,820
And then check when was called.

60
00:06:41,930 --> 00:06:46,910
First, the Roman Dukas fixed that zero in the inner loop, countered Jay's indexing every value in

61
00:06:46,910 --> 00:06:47,340
that road.

62
00:06:47,990 --> 00:06:50,120
There are two X's, so that counts to.

63
00:06:52,550 --> 00:06:54,650
Nothing happens, so the count resets to.

64
00:06:57,660 --> 00:07:02,970
Now we're indexing every value in a row, one, there are two O's, so that counts negative to.

65
00:07:05,690 --> 00:07:11,150
Again, nothing happens, it resets the count to zero, and now we're indexing every value in a row

66
00:07:11,150 --> 00:07:13,970
to there's nothing here, so the count is zero.

67
00:07:18,090 --> 00:07:20,280
So our function returns a count of zero.

68
00:07:24,900 --> 00:07:26,930
And that's not going to do anything for us.

69
00:07:29,600 --> 00:07:33,640
But in our next turn, if I put X X twins Nahar.

70
00:07:38,640 --> 00:07:41,580
Now, as we're checking the first rule, that counts three.

71
00:07:46,680 --> 00:07:50,490
The return key word breaks the function prematurely and returns the count.

72
00:07:54,610 --> 00:08:00,310
And since the return value is three X wins, breaks the loop and the game is done.

73
00:08:34,789 --> 00:08:35,179
Cool.

74
00:08:36,020 --> 00:08:38,870
Now, before moving on, we need to test for Stratos.

75
00:08:51,950 --> 00:08:54,080
I'll keep playing till I get all those.

76
00:09:06,640 --> 00:09:07,920
And perfect.

77
00:09:16,910 --> 00:09:21,560
This time, once the ROE index is fixed, that ROE one, it counts three values.

78
00:09:23,520 --> 00:09:25,560
Which results in a count of negative three.

79
00:09:27,760 --> 00:09:33,340
And after we're done checking that specific row, the return keyword breaks the function prematurely

80
00:09:33,340 --> 00:09:34,540
and returns the count.

81
00:09:38,140 --> 00:09:45,040
And negative three means a witness that is all for task six and seven in the next video, we're going

82
00:09:45,040 --> 00:09:46,370
to implement Task eight.

83
00:09:46,420 --> 00:09:47,140
See you there.

