# C language: Scrabble game with a friend

In the second week of the Computer Science CS50 (Harvard) course, after completing all the individual tasks, we move on to the C lab, which must be completed in a group. It will be an interesting task – to write the code for the game “Scrabble” (Scrabble.c). The essence of the task: the players come up with words that score a different number of points depending on the letters in this word, while each letter is assigned a different number of points from 1 to 10 and then these points are added up (the sum of the points of the letters gives the total number of points in the word).

For this task, the following table of correspondences of letters and points is used:

 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 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

*Any character other than a letter scores zero points; uppercase and lowercase letters evaluate to the same number of characters.

Gameplay: each player enters a word, the program calculates the number of points for each word and the one with the most points wins.

Result of the game: after both players enter a word, the computer displays the number of the winning player (`"Player x wins!"`). If there is a tie, the message “`Tie!`” (draw).

Igroglaz solution:

```#include <stdio.h>
#include <ctype.h>

int scrabble (char *input, int *AZ);
int main()
{
int AZ[] = {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};
char input;
int p1_score, p2_score;

printf("Player 1: ");
p1_score = scrabble (input, AZ);

printf("Player 2: ");
p2_score = scrabble (input, AZ);

if (p1_score > p2_score)
printf("Player 1 wins!");
else if (p1_score < p2_score)
printf("Player 2 wins!");
else
printf("Tie!");

getchar();
getchar();
return 0;
}

int scrabble (char *input, int *AZ)
{
int i, x;
int score = 0;

for (i = 0; ; i++)
{
scanf ("%c", &input[i]);
if (input[i] == '\n')
break;    // end input when user press 'Enter'

// to count score - 1st make letter uppercase
if (input[i] >= 'a' && input[i] <= 'z')
input[i] = toupper(input[i]);

// count only letters (ignore numbers, punctuation marks etc)
if (input[i] >= 'A' && input[i] <= 'Z')
{
// get the ordinal index of letter
x = (int)input[i] - 65;
// get score from according index from score array
score += AZ[x];
}
}
return score;
}```

Shtukensia solution

For a long time I solved this problem, and for a start I made a program without additional functions, since it was easier for me to think that way. As a result, the first solution turned out to be not too elegant, long, but working.

```#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int compute_score(int c, int n, int values[]);

int main(void)
{
// Create array with the point values of each of 26 letters
int values = {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};

// Get users input of word:
printf("Player 1: ");
string player1 = get_string("");
printf("Player 2: ");
string player2 = get_string("");

// Turn characters from plaintxtx to ciphertext with function
char c; // character from user input
int n = 0; // value of letter
int sum1 = 0; // sum of letters values player 1
int sum2 = 0; // sum of letters values player 2
int textlen1 = strlen(player1);
int textlen2 = strlen(player2);

int i = 0;
int n1 = 0;
for (i = 0; i <= (textlen1 - 1); i++) // sum char by char values of letters player 1
{
c = player1[i];
n1 = compute_score(c, n, values);
sum1 += n1;
}

int j = 0;
int n2 = 0;
for (j = 0; j <= (textlen2 - 1); j++) // sum char by char values of letters player 2
{
c = player2[j];
n2 = compute_score(c, n, values);
sum2 += n2;
}

if (sum1 > sum2)
{
printf("Player 1 wins!\n");
}
else if (sum1 < sum2)
{
printf("Player 2 wins!\n");
}
else if (sum1 == sum2)
{
printf("Tie!\n");
}
return 0;
}

int compute_score(int c, int n, int values[])
{
// Count sum of values for letters from 1 player
int c_asci = 0; // ASCII number of player 1 char LETTER (A->65)
int num_letter = 0; // index of player 1 letter in alphabet

int check_c = isalpha(c); // check if char is letter

if (check_c != 0) // if char of player 1 is letter
{
int check_upper = isupper(c); // check if char is uppercase

if (check_upper > 0)
{
c_asci = (c); // convert char LETTER to int ASCII number (A->65)
num_letter = (c_asci - 65); // find numer of upper letter in alphabet (index)
}

else if (check_upper == 0)
{
c_asci = (c);
num_letter = (c_asci - 97); // convert char LETTER to int ASCII number (a->97)
}
n = values[num_letter]; // asign value for letter
}
return n;
}```

Let’s play a few times to check the correctness of the program code: What did you understand while doing this task?

This entry was posted in C language (en). Bookmark the permalink.