https://cs50.harvard.edu/x/2023/labs/2/
Lab 2: Scrabble - CS50x 2023
Harvard University's introduction to the intellectual enterprises of computer science and the art of programming.
cs50.harvard.edu
ascii code 를 활용한 간단한 scrabble game.
아래는 제출한 답안 코드
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
// Points assigned to each letter of the alphabet
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};
int compute_score(string word);
int main(void)
{
// Get input words from both players
string word1 = get_string("Player 1: ");
string word2 = get_string("Player 2: ");
// Score both words
int score1 = compute_score(word1);
int score2 = compute_score(word2);
if (score1 > score2)
{
printf("Player 1 wins!\n");
}
else if (score1 < score2)
{
printf("Player 2 wins!\n");
}
else
{
printf("Tie!\n");
};
// TODO: Print the winner
}
int compute_score(string word)
{
int score = 0;
int length = strlen(word);
for (int i = 0; i < length; i++)
{
int ascii_code = word[i];
if (ascii_code > 64 && ascii_code < 91)
{
int letter_score = POINTS[ascii_code - 65];
score = score + letter_score;
}
else if (ascii_code > 96 && ascii_code < 123)
{
int topped_code = ascii_code - 32;
int letter_score = POINTS[topped_code - 65];
score = score + letter_score;
}
}
return score;
// TODO: Compute and return score for string
}