We’re going to create a Scrabble-like game using the C# programming language. In this game, two players will type in words, and the program will calculate the scores for each word based on the Scrabble letter values.
Each letter in Scrabble has a specific point value. For example:
A is worth 1 point B is worth 3 points Z is worth 10 points The program will:
Ask each player to type a word. Calculate the score for each word by adding up the points for each letter. Compare the scores to determine which player has the higher score. Announce the winner, or declare a tie if both scores are equal. By the end of this tutorial, you’ll have a basic understanding of how to:
Use arrays to store data (letter scores). Write methods to perform tasks (calculate word scores). Take input from users (the players). Use conditionals (if-else statements) to decide the winner. This is a great project for practicing basic programming concepts like loops, arrays, methods, and user input/output. Plus, it’s fun because you’re building a simple game that you can play with your friends!
-
Visual Studio Code: This is the program where you’ll write your code.
- You can download it here.
-
.NET SDK: This is a tool that allows you to run and build C# programs.
- You can download it here.
-
Install Visual Studio Code by downloading and running the installer from the link above. Follow the instructions on the screen to finish the installation.
-
Install the .NET SDK by downloading it from the provided link and running the installer. Again, just follow the instructions.
-
After both tools are installed, open Visual Studio Code.
-
Now, you need to open the terminal inside Visual Studio Code:
- Click on
Terminalin the top menu. - Select
New Terminalfrom the dropdown.
- Click on
We need to create a new C# project where we’ll write our Scrabble game code. A project is just a folder with some setup files that help us organize and run our code.
-
In the terminal, type this command and press Enter:
dotnet new console -n ScrabbleGame
- This creates a new folder called
ScrabbleGamewith some basic setup files.
- This creates a new folder called
-
Now, type this command to move into the new folder:
cd ScrabbleGame- This tells your computer to switch to the
ScrabbleGamefolder.
- This tells your computer to switch to the
-
Finally, type this command to open the project in Visual Studio Code:
code .- This opens the current project folder in Visual Studio Code, so you can start writing code.
In C#, a class is like a container that holds your game’s code. Everything we write for the Scrabble game will go inside this class.
-
In the Explorer panel (on the left side of the screen), find and click on
Program.csto open the file. -
Delete everything in the file. We want to start from scratch.
-
Now, type this code:
Click here to reveal the code for the class
using System;
class ScrabbleGame
{
// We will add code here soon.
}Explanation:
using System;is a line that allows us to use basic C# commands likeConsole.WriteLineto print messages to the screen.class ScrabbleGamedefines a class calledScrabbleGame. All the code we write for the game will go inside this class (inside the curly braces{ }).
In Scrabble, each letter has a point value. For example, 'A' is worth 1 point, 'B' is worth 3 points, and 'Z' is worth 10 points. We’ll store these values in something called an array.
An array is like a list that holds multiple values. In this case, the array will hold the scores for each letter from A to Z.
- Inside the
ScrabbleGameclass (between the curly braces{ }), add the following code:
Click here to reveal the code for the letter scores
private int[] letterScores = {
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
};Explanation:
private int[] letterScorescreates an array calledletterScoresthat holds integers (whole numbers).- The numbers in the array represent the points for each letter:
- The first number,
1, is for 'A'. - The second number,
3, is for 'B'. - The last number,
10, is for 'Z'.
- The first number,
We now have an array that stores the points for all 26 letters of the alphabet.
Now, we’ll create a method that calculates the score of a word. A method is a block of code that performs a task. In this case, the method will go through each letter of a word, find its score in the letterScores array, and add up the total score.
- Inside the
ScrabbleGameclass, add the following method:
Click here to reveal the code for the ComputeScore method
public int ComputeScore(string word)
{
int totalScore = 0; // Start the score at 0
word = word.ToUpper(); // Convert the word to uppercase
// Loop through each letter in the word
for (int i = 0; i < word.Length; i++)
{
char letter = word[i]; // Get the current letter
// Check if the character is a letter
if (char.IsLetter(letter))
{
// Find the index of the letter (A=0, B=1, ..., Z=25)
int index = letter - 'A';
// Add the letter's score to totalScore
totalScore += letterScores[index];
}
}
return totalScore; // Return the total score of the word
}Explanation:
public int ComputeScore(string word)creates a method calledComputeScorethat takes in a word (string) and returns an integer (the total score).int totalScore = 0;initializes the score to 0.word = word.ToUpper();converts the word to all uppercase letters, so that 'cat' becomes 'CAT'. This ensures we don’t have to worry about whether the player used lowercase or uppercase letters.- The for loop goes through each letter in the word. It starts at the first letter (
i = 0) and stops when it reaches the end (i < word.Length). char.IsLetter(letter)checks if the character is actually a letter (just in case the player types something else by mistake).int index = letter - 'A';calculates the position of the letter in the alphabet (e.g., 'A' is 0, 'B' is 1, 'Z' is 25).totalScore += letterScores[index];adds the score of the letter (based on theletterScoresarray) to the total score.return totalScore;returns the total score for the word.
Now we need a method that actually starts the game. This method will ask the players for their words, calculate their scores, and decide who wins.
- Inside the
ScrabbleGameclass, add the following method:
Click here to reveal the code for starting the game
public void StartGame()
{
// Ask Player 1 for their word
Console.Write("Player 1, enter your word: ");
string player1Word = Console.ReadLine();
// Ask Player 2 for their word
Console.Write("Player 2, enter your word: ");
string player2Word = Console.ReadLine();
// Compute the scores for both words
int player1Score = ComputeScore(player1Word);
int player2Score = ComputeScore(player2Word);
// Display the scores
Console.WriteLine($"Player 1 Score: {player1Score}");
Console.WriteLine($"Player 2 Score: {player2Score}");
// Determine the winner
if (player1Score > player2Score)
{
Console.WriteLine("Player 1 wins! 🏆");
}
else if (player2Score > player1Score)
{
Console.WriteLine("Player 2 wins! 🏆");
}
else
{
Console.WriteLine("It's a tie! 🤝");
}
}Explanation:
Console.Write("Player 1, enter your word: ");prints a message asking Player 1 to enter their word.string player1Word = Console.ReadLine();captures the word that Player 1 types in.- We do the same for Player 2.
- We call
the ComputeScore method to calculate the score for both words.
ifandelsestatements compare the scores and decide who wins.
The Main method is the starting point of your program. It’s the first thing that runs when you start the program. Here, it will start the Scrabble game.
- Outside the
ScrabbleGameclass (below the closing brace}), add the following code:
Click here to reveal the Main method code
class Program
{
static void Main(string[] args)
{
// Create a new instance of the game
ScrabbleGame game = new ScrabbleGame();
// Start the game
game.StartGame();
}
}Explanation:
class Programdefines a new class calledProgram.- The Main method (
static void Main(string[] args)) is where the program starts. - We create a new instance of
ScrabbleGameand call theStartGame()method to begin the game.
Let’s see your game in action.
-
Save your work:
- Click
File>Save All, or pressCtrl + S on Windows (Cmd + S` on Mac).
- Click
-
Run the program:
-
In the terminal, type:
dotnet run
-
-
Play the game:
- Enter words for Player 1 and Player 2 when prompted.
- See the scores and find out who wins!
Example:
Player 1, enter your word: hello
Player 2, enter your word: world
Player 1 Score: 8
Player 2 Score: 9
Player 2 wins! 🏆
You’ve just built a simple Scrabble-like game in C#! 🎉
Want to take it further? Try these:
-
Input Validation:
- Ensure players only enter words containing letters.
-
Play Again Option:
- After the game ends, ask the players if they want to play again.
-
More Players:
- Modify the game to allow more than two players.
Great job! You've learned:
- Classes for organizing code.
- Methods for performing tasks.
- Arrays for storing data.
- Loops for repeating actions.
- User input and output in the console.
Keep practicing, and you’ll keep improving. Happy coding! 💻



