Skip to content

Commit 20e9098

Browse files
committed
add scrabble
1 parent e263771 commit 20e9098

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed

scrabble/.cs50.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
lab50:
2+
files:
3+
- !exclude "*"
4+
- !include scrabble.c
5+
6+
check50:
7+
files: &check50_files
8+
- !exclude "*"
9+
- !require scrabble.c
10+
11+
submit50:
12+
files: *check50_files

scrabble/README.md

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Scrabble
2+
3+
Determine which of two Scrabble words is worth more.
4+
5+
```
6+
$ ./scrabble
7+
Player 1: COMPUTER
8+
Player 2: science
9+
Player 1 wins!
10+
```
11+
12+
## Background
13+
14+
In the game of [Scrabble](https://scrabble.hasbro.com/en-us/rules), players create words to score points, and the number of points is the sum of the point values of each letter in the word.
15+
16+
|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|
17+
|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
18+
|1|3|3|2|1|4|2|4|1|8|5|1|3|1|1|3|10|1|1|1|1|4|4|8|4|10|
19+
20+
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.
21+
22+
## Implementation Details
23+
24+
Complete the implementation of `scrabble.c` at right, 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.
25+
26+
* Notice that we've stored the point values of each letter of the alphabet in an integer array named `POINTS`.
27+
* 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.
28+
* Notice that we've created a prototype for a helper function called `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 `score()` exists later in the program.
29+
* In `main()`, the program prompts the two players for their words using the `get_string()` function. These values are stored inside variables named `p1` and `p2`.
30+
* In `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.
31+
* For example, `!` is worth `0` points while `A` and `a` are both worth `1` point.
32+
* In `main()`, your program should print, depending on the players' scores, `"Player 1 Wins!"`, `"Player 2 Wins!"`, or `"Tie!"`.
33+
34+
### Hints
35+
36+
* You may find the functions `isupper()` and `islower()` to be helpful to you. These functions take in a character as the argument and return a boolean.
37+
38+
* To find the value at the `n`th index of an array called `arr`, we can write `arr[n]`. We can apply this to strings as well, as strings are arrays of characters.
39+
40+
* Recall that computers represent characters using [ASCII](http://asciitable.com/), a standard that represents each character as a number.
41+
42+
### How to Test Your Code
43+
44+
Your program should behave per the examples below.
45+
46+
```
47+
./scrabble
48+
Player 1: Question?
49+
Player 2: Question!
50+
Tie!
51+
```
52+
53+
```
54+
$ ./scrabble
55+
Player 1: Oh,
56+
Player 2: hai!
57+
Player 2 wins!
58+
```
59+
60+
```
61+
$ ./scrabble
62+
Player 1: COMPUTER
63+
Player 2: science
64+
Player 1 wins!
65+
```
66+
67+
```
68+
$ ./scrabble
69+
Player 1: Scrabble
70+
Player 2: wiNNeR
71+
Player 1 wins!
72+
```
73+
74+
{% next %}
75+
76+
## How to Submit
77+
78+
TODO

scrabble/__init__.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import check50
2+
import check50.c
3+
4+
@check50.check()
5+
def exists():
6+
"""scrabble.c exists"""
7+
check50.exists("scrabble.c")
8+
9+
@check50.check(exists)
10+
def compiles():
11+
"""scrabble.c compiles"""
12+
check50.c.compile("scrabble.c", lcs50=True)
13+
14+
@check50.check(compiles)
15+
def tie_letter_case():
16+
"""handles letter cases correctly"""
17+
check50.run("./scrabble").stdin("LETTERCASE").stdin("lettercase").stdout("[Tt]ie!?", "Tie!").exit(0)
18+
19+
@check50.check(compiles)
20+
def tie_punctuation():
21+
"""handles punctuation correctly"""
22+
check50.run("./scrabble").stdin("Punctuation!?!?").stdin("punctuation").stdout("[Tt]ie!?", "Tie!").exit(0)
23+
24+
@check50.check(compiles)
25+
def test1():
26+
"""correctly identifies 'Question?' and 'Question!' as a tie"""
27+
check50.run("./scrabble").stdin("Question?").stdin("Question!").stdout("[Tt]ie!?", "Tie!").exit(0)
28+
29+
@check50.check(compiles)
30+
def test2():
31+
"""correctly identifies 'drawing' and 'illustration' as a tie"""
32+
check50.run("./scrabble").stdin("drawing").stdin("illustration").stdout("[Tt]ie!?", "Tie!").exit(0)
33+
34+
@check50.check(compiles)
35+
def test3():
36+
"""correctly identifies 'hai!' as winner over 'Oh,'"""
37+
check50.run("./scrabble").stdin("Oh,").stdin("hai!").stdout("[Pp]layer 2 [Ww]ins!?", "Player 2 wins!").exit(0)
38+
39+
@check50.check(compiles)
40+
def test4():
41+
"""correctly identifies 'COMPUTER' as winner over 'science'"""
42+
check50.run("./scrabble").stdin("COMPUTER").stdin("science").stdout("[Pp]layer 1 [Ww]ins!?", "Player 1 wins!").exit(0)
43+
44+
@check50.check(compiles)
45+
def test5():
46+
"""correctly identifies 'Scrabble' as winner over 'wiNNeR'"""
47+
check50.run("./scrabble").stdin("Scrabble").stdin("wiNNeR").stdout("[Pp]layer 1 [Ww]ins!?", "Player 1 wins!").exit(0)
48+
49+
@check50.check(compiles)
50+
def test6():
51+
"""correctly identifies 'pig' as winner over 'dog'"""
52+
check50.run("./scrabble").stdin("pig").stdin("dog").stdout("[Pp]layer 1 [Ww]ins!?", "Player 1 wins!").exit(0)
53+
54+
@check50.check(compiles)
55+
def complex_case():
56+
"""correctly identifies 'Skating!' as winner over 'figure?'"""
57+
check50.run("./scrabble").stdin("figure?").stdin("Skating!").stdout("[Pp]layer 2 [Ww]ins!?", "Player 2 wins!").exit(0)
58+

scrabble/scrabble.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <cs50.h>
2+
#include <stdio.h>
3+
4+
// Points assigned to each letter of the alphabet
5+
int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
6+
7+
int compute_score(string word);
8+
9+
int main(void)
10+
{
11+
// Get input words from both players
12+
string word1 = get_string("Player 1: ");
13+
string word2 = get_string("Player 2: ");
14+
15+
// Score both words
16+
int score1 = compute_score(word1);
17+
int score2 = compute_score(word2);
18+
19+
// TODO: Print the winner
20+
}
21+
22+
int compute_score(string word)
23+
{
24+
// TODO: Compute and return score for string
25+
}

0 commit comments

Comments
 (0)