加载中...

CS50 Lab2


Lab 2: Scrabble

Lab2 Page: https://cs50.harvard.edu/x/2023/labs/2/

CS50 Study Group:677535709(QQ Group)

Background 🧐

In the game of Scrabble, players create words to score points, and the number of points is the sum of the point values of each letter in the word.

Letter Points
A 1
B 3
C 3
D 2
E 1
F 4
G 2
H 4
I 1
J 8
K 5
L 1
M 3
N 1
O 1
P 3
Q 10
R 1
S 1
T 1
U 1
V 4
W 4
X 8
Y 4
Z 10

For example, if we wanted to score the word Code, we would note that in general Scrabble rules, the C is worth 3 points, the o is worth 1 point, the d is worth 2 points, and the e is worth 1 point. Summing these, we get that Code is worth 3 + 1 + 2 + 1 = 7 points.

Getting Started 🍉

Open VS Code.

Start by clicking inside your terminal window, then execute cd by itself. You should find that its “prompt” resembles the below.

$

Click inside of that terminal window and then execute

wget https://cdn.cs50.net/2022/fall/labs/2/scrabble.zip

followed by Enter in order to download a ZIP called scrabble.zip in your codespace. Take care not to overlook the space between wget and the following URL, or any other character for that matter!

Now execute

unzip scrabble.zip

to create a folder called scrabble. You no longer need the ZIP file, so you can execute

rm scrabble.zip

and respond with “y” followed by Enter at the prompt to remove the ZIP file you downloaded.

Now type

cd scrabble

followed by Enter to move yourself into (i.e., open) that directory. Your prompt should now resemble the below.

scrabble/ $

If all was successful, you should execute

ls

and you should see a file called scrabble.c. Open that file by executing the below:

code scrabble.c

If you run into any trouble, follow these same steps steps again and see if you can determine where you went wrong!

Walkthrough 🎥

YouTube: https://youtu.be/RtjxxxlN1gc

bilibili: https://www.bilibili.com/video/BV1Do4y1t7Dv?share_source=copy_web

Implementation Details ☘️

Complete the implementation of scrabble.c, such that it determines the winner of a short scrabble-like game, where two players each enter their word, and the higher scoring player wins.

  • Notice that we’ve stored the point values of each letter of the alphabet in an integer array named POINTS
    • For example, A or a is worth 1 point (represented by POINTS[0]), B or b is worth 3 points (represented by POINTS[1]), etc.
  • Notice that we’ve created a prototype for a helper function called compute_score() that takes a string as input and returns an int. Whenever we would like to assign point values to a particular word, we can call this function. Note that this prototype is required for C to know that compute_score() exists later in the program.
  • In main(), the program prompts the two players for their words using the get_string() function. These values are stored inside variables named word1 and word2.
  • In compute_score(), your program should compute, using the POINTS array, and return the score for the string argument. Characters that are not letters should be given zero points, and uppercase and lowercase letters should be given the same point values.
    • For example, ! is worth 0 points while A and a are both worth 1 point.
    • Though Scrabble rules normally require that a word be in the dictionary, no need to check for that in this problem!
  • In main(), your program should print, depending on the players’ scores, Player 1 wins!, Player 2 wins!, or Tie!.

How to Test Your Code 🖥

Your program should behave per the examples below.

$ ./scrabble
Player 1: Question?
Player 2: Question!
Tie!
$ ./scrabble
Player 1: Oh,
Player 2: hai!
Player 2 wins!
$ ./scrabble
Player 1: COMPUTER
Player 2: science
Player 1 wins!
$ ./scrabble
Player 1: Scrabble
Player 2: wiNNeR
Player 1 wins!

Execute the below to evaluate the correctness of your code using check50. But be sure to compile and test it yourself as well!

check50 cs50/labs/2023/x/scrabble

Execute the below to evaluate the style of your code using style50.

style50 scrabble.c

How to Submit 🚀

In your terminal, execute the below to submit your work.

submit50 cs50/labs/2023/x/scrabble

Solution 🧑🏻‍💻

Ricky’s GitHub Repository for CS50 Labs: https://github.com/Ricky2333/CS50-Labs

CS50 Study Group:677535709(QQ Group)


文章作者: Rickyの水果摊
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Rickyの水果摊 !
  目录