CS50 Video Player
    • 🧁

    • 🍮

    • 🍈

    • 🍿
    • 0:00:00SPEAKER 1: Let's take a look at how you might have solved the Scrabble problem.
    • 0:00:03Recall that in this problem your task was
    • 0:00:05to write a program in C that could take two different words
    • 0:00:09and figure out which one of them would have been worth
    • 0:00:11more points in a game of Scrabble.
    • 0:00:14Let's see how you might have done that.
    • 0:00:16We gave you some distribution code in scrabble.c already.
    • 0:00:19But the place you probably wanted to start
    • 0:00:21was by writing this compute score function.
    • 0:00:24This function that accepted a word as input
    • 0:00:26and could tell you how many points that word was worth.
    • 0:00:30So let's scroll down to the compute score function.
    • 0:00:33Ultimately, in order to figure out how many points the word is worth,
    • 0:00:36we want to go through each of the characters one by one
    • 0:00:39and figure out how much each character is worth,
    • 0:00:42adding up all of those results.
    • 0:00:44As we add up all of those results, it's going
    • 0:00:46to be helpful to have some variable that's keeping track of the score
    • 0:00:50so far.
    • 0:00:51And so that's the first thing that we do inside this compute score function.
    • 0:00:55We have an integer called score equal to zero.
    • 0:00:58And that variable is going to keep track of the score for the current word.
    • 0:01:03Now what we need to do is loop over the entire word,
    • 0:01:07looking at each character one at a time.
    • 0:01:09To do so, we have a for loop, where we start with int i equals 0.
    • 0:01:13i is going to represent the index of the character.
    • 0:01:17And in order to complete this loop, we need to know how long the string is.
    • 0:01:22So to do that, we can use the strlen function
    • 0:01:25to access the length of the string, storing that inside of a variable
    • 0:01:29called len.
    • 0:01:30We're going to repeat this loop as long as i is less than len,
    • 0:01:33increasing i by one every time this loop completes an iteration.
    • 0:01:38Ultimately, this is going to have i start at zero,
    • 0:01:40the first character in the string, then become 1, 2, 3,
    • 0:01:43and so forth all the way until we get to the end of the string.
    • 0:01:49Now there are two cases we need to consider here.
    • 0:01:51This character might be an uppercase letter, in which case
    • 0:01:54it's worth points, or it might be a lowercase letter, in which case
    • 0:01:57it's also going to be worth some number of points.
    • 0:02:00And for any other type of character, like a digit, or a punctuation,
    • 0:02:03or a space, those are not going to be worth any points at all.
    • 0:02:07So let's first deal with the uppercase situation.
    • 0:02:11We can use the isupper function to check if any particular character is
    • 0:02:15an uppercase letter.
    • 0:02:17And so we can check for word square bracket i, meaning,
    • 0:02:21take that string "word" and access character
    • 0:02:23i for whatever i happens to be for this iteration of the loop.
    • 0:02:28If this current character, word bracket i is an uppercase letter,
    • 0:02:33well then we need to update the score.
    • 0:02:35How will we know how to update the score?
    • 0:02:37Well ultimately, we're going to use that points array at the top of our file.
    • 0:02:44This array gives us the point value for each letter
    • 0:02:46of the alphabet, where points square brackets 0 represents
    • 0:02:50the score for the letter A. Points square bracket 1 represents
    • 0:02:54the score for the letter B, and so on.
    • 0:02:57But if you recall from our ASCII relationship between characters
    • 0:03:00and what numbers represent those characters,
    • 0:03:03capital letter A is not actually represented by the number 0.
    • 0:03:07It's represented by the number 65.
    • 0:03:10And the capital letter B is represented by the number 66.
    • 0:03:13And capital C by 67.
    • 0:03:16So we need to somehow translate those ASCII values 65, 66, 67 for A, B, and C
    • 0:03:23into 0, 1, and 2, which will be the indexes we
    • 0:03:27want to use into this points array.
    • 0:03:30How do we do that?
    • 0:03:31Well, ultimately, every letter's ASCII value for the uppercase letters
    • 0:03:36is 65 more than whatever index value we actually want
    • 0:03:40to use inside of this points array.
    • 0:03:43So in order to translate that character into its corresponding index,
    • 0:03:48I could say, words square bracket i minus 65,
    • 0:03:53where capital A, which is 65 minus 65 would give us 0.
    • 0:03:58So ultimately, we're accessing POINTS square brackets
    • 0:04:010 to get access to the number of points that corresponds to capital letter A.
    • 0:04:06For capital letter B, it would have been 66 minus 65.
    • 0:04:10And that would give us POINTS square bracket
    • 0:04:121 for accessing the number of points we would get for a capital B.
    • 0:04:17But since 65 is really the same thing as capital A in terms of how they're
    • 0:04:22represented, it's a little bit cleaner in my code
    • 0:04:25to just say word i minus capital A. That way
    • 0:04:29I don't need to know that 65 just so happens
    • 0:04:31to correspond to the capital letter A.
    • 0:04:35So ultimately what I'm doing here is saying word
    • 0:04:37square bracket i minus capital A. That is going to give me an index value.
    • 0:04:42If word square bracket i is capital A, then it's capital A minus capital A.
    • 0:04:47That's zero.
    • 0:04:48If word square bracket i is capital Z, then I'm
    • 0:04:51going to get capital Z minus capital A, which is going to be 25.
    • 0:04:55Those values correspond to an index in the points array.
    • 0:05:00So by putting that in square brackets inside of POINTS,
    • 0:05:02I can access how many points that particular character
    • 0:05:06is going to be worth.
    • 0:05:08Whatever number of points that character is worth,
    • 0:05:10I'm going to then add that to the score.
    • 0:05:12And that's going to update the value of score in response to this character.
    • 0:05:17Then I need to do the same thing for lower case letters.
    • 0:05:21If islower, this particular character, we're going to do the same logic,
    • 0:05:25adding to the score as well.
    • 0:05:27But this time, subtracting lowercase A, 97, instead of uppercase A, 65.
    • 0:05:33To make sure that we're able to get the correct index inside of that points
    • 0:05:38array.
    • 0:05:40This condition has an if and an else if.
    • 0:05:42We don't need an else condition.
    • 0:05:44The else case would handle characters that are not letters at all.
    • 0:05:48Numbers or punctuation, for example.
    • 0:05:50But in those cases, since we don't need to do anything,
    • 0:05:53the score is not going to change, we can just leave that case off entirely.
    • 0:05:57We only consider uppercase and lowercase letters.
    • 0:06:00And at the very end, once we have our final value for score,
    • 0:06:03we can return the value of score.
    • 0:06:06What is our main function that I need to do with that information?
    • 0:06:10Well, we need to print the winner.
    • 0:06:11So we can compare those scores.
    • 0:06:13If score1 is greater than score2, we print "Player 1 wins!"
    • 0:06:17Otherwise, if score1 is less than score2, we print "Player 2 wins!"
    • 0:06:21And else, the only other possibility is that both players
    • 0:06:25have the same score, in which case our program is going
    • 0:06:27to print that there has been a tie.
    • 0:06:30Once you compile and run that program, you should be able to type in two words
    • 0:06:34and see which one of them would win in a game of Scrabble My name is Brian.
    • 0:06:38And this was Scrabble.
  • CS50.ai
Shortcuts
Before using a shortcut, click at least once on the video itself (to give it "focus") after closing this window.
Play/Pause spacebar or k
Rewind 10 seconds left arrow or j
Fast forward 10 seconds right arrow or l
Previous frame (while paused) ,
Next frame (while paused) .
Decrease playback rate <
Increase playback rate >
Toggle captions on/off c
Toggle mute m
Toggle full screen f or double-click video