1 00:00:00,000 --> 00:00:00,120 2 00:00:00,120 --> 00:00:03,690 SPEAKER 1: Let's take a look at how you might have solved the Scrabble problem. 3 00:00:03,690 --> 00:00:05,670 Recall that in this problem your task was 4 00:00:05,670 --> 00:00:09,210 to write a program in C that could take two different words 5 00:00:09,210 --> 00:00:11,460 and figure out which one of them would have been worth 6 00:00:11,460 --> 00:00:14,280 more points in a game of Scrabble. 7 00:00:14,280 --> 00:00:16,370 Let's see how you might have done that. 8 00:00:16,370 --> 00:00:19,640 We gave you some distribution code in scrabble.c already. 9 00:00:19,640 --> 00:00:21,650 But the place you probably wanted to start 10 00:00:21,650 --> 00:00:24,470 was by writing this compute score function. 11 00:00:24,470 --> 00:00:26,870 This function that accepted a word as input 12 00:00:26,870 --> 00:00:30,870 and could tell you how many points that word was worth. 13 00:00:30,870 --> 00:00:33,470 So let's scroll down to the compute score function. 14 00:00:33,470 --> 00:00:36,890 Ultimately, in order to figure out how many points the word is worth, 15 00:00:36,890 --> 00:00:39,950 we want to go through each of the characters one by one 16 00:00:39,950 --> 00:00:42,530 and figure out how much each character is worth, 17 00:00:42,530 --> 00:00:44,750 adding up all of those results. 18 00:00:44,750 --> 00:00:46,880 As we add up all of those results, it's going 19 00:00:46,880 --> 00:00:50,510 to be helpful to have some variable that's keeping track of the score 20 00:00:50,510 --> 00:00:51,650 so far. 21 00:00:51,650 --> 00:00:55,430 And so that's the first thing that we do inside this compute score function. 22 00:00:55,430 --> 00:00:58,340 We have an integer called score equal to zero. 23 00:00:58,340 --> 00:01:03,080 And that variable is going to keep track of the score for the current word. 24 00:01:03,080 --> 00:01:07,100 Now what we need to do is loop over the entire word, 25 00:01:07,100 --> 00:01:09,980 looking at each character one at a time. 26 00:01:09,980 --> 00:01:13,880 To do so, we have a for loop, where we start with int i equals 0. 27 00:01:13,880 --> 00:01:17,270 i is going to represent the index of the character. 28 00:01:17,270 --> 00:01:22,140 And in order to complete this loop, we need to know how long the string is. 29 00:01:22,140 --> 00:01:25,190 So to do that, we can use the strlen function 30 00:01:25,190 --> 00:01:29,150 to access the length of the string, storing that inside of a variable 31 00:01:29,150 --> 00:01:30,590 called len. 32 00:01:30,590 --> 00:01:33,580 We're going to repeat this loop as long as i is less than len, 33 00:01:33,580 --> 00:01:38,330 increasing i by one every time this loop completes an iteration. 34 00:01:38,330 --> 00:01:40,550 Ultimately, this is going to have i start at zero, 35 00:01:40,550 --> 00:01:43,760 the first character in the string, then become 1, 2, 3, 36 00:01:43,760 --> 00:01:49,000 and so forth all the way until we get to the end of the string. 37 00:01:49,000 --> 00:01:51,570 Now there are two cases we need to consider here. 38 00:01:51,570 --> 00:01:54,230 This character might be an uppercase letter, in which case 39 00:01:54,230 --> 00:01:57,720 it's worth points, or it might be a lowercase letter, in which case 40 00:01:57,720 --> 00:02:00,180 it's also going to be worth some number of points. 41 00:02:00,180 --> 00:02:03,480 And for any other type of character, like a digit, or a punctuation, 42 00:02:03,480 --> 00:02:07,830 or a space, those are not going to be worth any points at all. 43 00:02:07,830 --> 00:02:11,090 So let's first deal with the uppercase situation. 44 00:02:11,090 --> 00:02:15,710 We can use the isupper function to check if any particular character is 45 00:02:15,710 --> 00:02:17,180 an uppercase letter. 46 00:02:17,180 --> 00:02:21,230 And so we can check for word square bracket i, meaning, 47 00:02:21,230 --> 00:02:23,780 take that string "word" and access character 48 00:02:23,780 --> 00:02:28,670 i for whatever i happens to be for this iteration of the loop. 49 00:02:28,670 --> 00:02:33,080 If this current character, word bracket i is an uppercase letter, 50 00:02:33,080 --> 00:02:35,300 well then we need to update the score. 51 00:02:35,300 --> 00:02:37,670 How will we know how to update the score? 52 00:02:37,670 --> 00:02:44,000 Well ultimately, we're going to use that points array at the top of our file. 53 00:02:44,000 --> 00:02:46,970 This array gives us the point value for each letter 54 00:02:46,970 --> 00:02:50,630 of the alphabet, where points square brackets 0 represents 55 00:02:50,630 --> 00:02:54,320 the score for the letter A. Points square bracket 1 represents 56 00:02:54,320 --> 00:02:57,090 the score for the letter B, and so on. 57 00:02:57,090 --> 00:03:00,860 But if you recall from our ASCII relationship between characters 58 00:03:00,860 --> 00:03:03,380 and what numbers represent those characters, 59 00:03:03,380 --> 00:03:07,520 capital letter A is not actually represented by the number 0. 60 00:03:07,520 --> 00:03:10,310 It's represented by the number 65. 61 00:03:10,310 --> 00:03:13,940 And the capital letter B is represented by the number 66. 62 00:03:13,940 --> 00:03:16,670 And capital C by 67. 63 00:03:16,670 --> 00:03:23,840 So we need to somehow translate those ASCII values 65, 66, 67 for A, B, and C 64 00:03:23,840 --> 00:03:27,560 into 0, 1, and 2, which will be the indexes we 65 00:03:27,560 --> 00:03:30,500 want to use into this points array. 66 00:03:30,500 --> 00:03:31,840 How do we do that? 67 00:03:31,840 --> 00:03:36,440 Well, ultimately, every letter's ASCII value for the uppercase letters 68 00:03:36,440 --> 00:03:40,880 is 65 more than whatever index value we actually want 69 00:03:40,880 --> 00:03:43,890 to use inside of this points array. 70 00:03:43,890 --> 00:03:48,680 So in order to translate that character into its corresponding index, 71 00:03:48,680 --> 00:03:53,820 I could say, words square bracket i minus 65, 72 00:03:53,820 --> 00:03:58,490 where capital A, which is 65 minus 65 would give us 0. 73 00:03:58,490 --> 00:04:01,670 So ultimately, we're accessing POINTS square brackets 74 00:04:01,670 --> 00:04:06,550 0 to get access to the number of points that corresponds to capital letter A. 75 00:04:06,550 --> 00:04:10,610 For capital letter B, it would have been 66 minus 65. 76 00:04:10,610 --> 00:04:12,710 And that would give us POINTS square bracket 77 00:04:12,710 --> 00:04:17,720 1 for accessing the number of points we would get for a capital B. 78 00:04:17,720 --> 00:04:22,490 But since 65 is really the same thing as capital A in terms of how they're 79 00:04:22,490 --> 00:04:25,610 represented, it's a little bit cleaner in my code 80 00:04:25,610 --> 00:04:29,300 to just say word i minus capital A. That way 81 00:04:29,300 --> 00:04:31,820 I don't need to know that 65 just so happens 82 00:04:31,820 --> 00:04:35,010 to correspond to the capital letter A. 83 00:04:35,010 --> 00:04:37,830 So ultimately what I'm doing here is saying word 84 00:04:37,830 --> 00:04:42,980 square bracket i minus capital A. That is going to give me an index value. 85 00:04:42,980 --> 00:04:47,180 If word square bracket i is capital A, then it's capital A minus capital A. 86 00:04:47,180 --> 00:04:48,350 That's zero. 87 00:04:48,350 --> 00:04:51,050 If word square bracket i is capital Z, then I'm 88 00:04:51,050 --> 00:04:55,610 going to get capital Z minus capital A, which is going to be 25. 89 00:04:55,610 --> 00:05:00,080 Those values correspond to an index in the points array. 90 00:05:00,080 --> 00:05:02,750 So by putting that in square brackets inside of POINTS, 91 00:05:02,750 --> 00:05:06,350 I can access how many points that particular character 92 00:05:06,350 --> 00:05:08,200 is going to be worth. 93 00:05:08,200 --> 00:05:10,630 Whatever number of points that character is worth, 94 00:05:10,630 --> 00:05:12,910 I'm going to then add that to the score. 95 00:05:12,910 --> 00:05:17,650 And that's going to update the value of score in response to this character. 96 00:05:17,650 --> 00:05:21,100 Then I need to do the same thing for lower case letters. 97 00:05:21,100 --> 00:05:25,480 If islower, this particular character, we're going to do the same logic, 98 00:05:25,480 --> 00:05:27,200 adding to the score as well. 99 00:05:27,200 --> 00:05:33,820 But this time, subtracting lowercase A, 97, instead of uppercase A, 65. 100 00:05:33,820 --> 00:05:38,460 To make sure that we're able to get the correct index inside of that points 101 00:05:38,460 --> 00:05:40,330 array. 102 00:05:40,330 --> 00:05:42,550 This condition has an if and an else if. 103 00:05:42,550 --> 00:05:44,680 We don't need an else condition. 104 00:05:44,680 --> 00:05:48,630 The else case would handle characters that are not letters at all. 105 00:05:48,630 --> 00:05:50,950 Numbers or punctuation, for example. 106 00:05:50,950 --> 00:05:53,510 But in those cases, since we don't need to do anything, 107 00:05:53,510 --> 00:05:57,610 the score is not going to change, we can just leave that case off entirely. 108 00:05:57,610 --> 00:06:00,640 We only consider uppercase and lowercase letters. 109 00:06:00,640 --> 00:06:03,940 And at the very end, once we have our final value for score, 110 00:06:03,940 --> 00:06:06,870 we can return the value of score. 111 00:06:06,870 --> 00:06:10,013 What is our main function that I need to do with that information? 112 00:06:10,013 --> 00:06:11,430 Well, we need to print the winner. 113 00:06:11,430 --> 00:06:13,170 So we can compare those scores. 114 00:06:13,170 --> 00:06:17,580 If score1 is greater than score2, we print "Player 1 wins!" 115 00:06:17,580 --> 00:06:21,850 Otherwise, if score1 is less than score2, we print "Player 2 wins!" 116 00:06:21,850 --> 00:06:25,080 And else, the only other possibility is that both players 117 00:06:25,080 --> 00:06:27,750 have the same score, in which case our program is going 118 00:06:27,750 --> 00:06:30,430 to print that there has been a tie. 119 00:06:30,430 --> 00:06:34,120 Once you compile and run that program, you should be able to type in two words 120 00:06:34,120 --> 00:06:38,620 and see which one of them would win in a game of Scrabble My name is Brian. 121 00:06:38,620 --> 00:06:40,980 And this was Scrabble. 122 00:06:40,980 --> 00:06:42,000