Skip to content

Latest commit

 

History

History
171 lines (150 loc) · 4.77 KB

README.md

File metadata and controls

171 lines (150 loc) · 4.77 KB

🎯 C# Decisions & Loops Project

Lesson Snapshot

Build a smart number guessing game that makes decisions and repeats actions automatically! (Ages 8-18)

Folder & File Map

DecisionsAndLoops/
├── .git/
├── .gitignore
├── DecisionsAndLoops.csproj
└── Program.cs

Step-by-Step Build Guide

1. Fork and Clone the Repository ✅

  • What to do: Go to https://github.com/PhillyCoderDojo/session3-decisions → Click "Fork" → Open GitHub Desktop → Clone your fork
  • File(s) touched: Creating project folder
  • Code snippet: None
  • Why it matters: Get your own copy to experiment with
  • Git command: git clone https://github.com/YOUR-USERNAME/CSharp-Lesson-Three.git

2. Open Project in Rider ✅

  • What to do: Open Rider → File → Open → Select your cloned folder → Click OK
  • File(s) touched: Loading project
  • Code snippet: None
  • Why it matters: Ready to start coding!
  • Git command: None

3. Create Your First Decision 🤔

  • What to do: Open Program.cs and add this if-statement code
  • File(s) touched: Program.cs
  • Code snippet:
Console.Write("Enter a number: ");
int userNumber = int.Parse(Console.ReadLine());

if (userNumber > 10)
{
    Console.WriteLine("That's a big number!");
}
else
{
    Console.WriteLine("That's a small number!");
}
  • Why it matters: Programs can make choices based on data
  • Git command: git add . && git commit -m "Add first if-else decision"

4. Add Multiple Conditions 🎯

  • What to do: Replace your code with this expanded version
  • File(s) touched: Program.cs
  • Code snippet:
Console.Write("Guess my number (1-20): ");
int guess = int.Parse(Console.ReadLine());
int secret = 15;

if (guess == secret)
{
    Console.WriteLine("Perfect! You got it!");
}
else if (guess > secret)
{
    Console.WriteLine("Too high! Try again.");
}
else
{
    Console.WriteLine("Too low! Try again.");
}
  • Why it matters: Multiple conditions create smarter programs
  • Git command: git add . && git commit -m "Add multiple conditions with else-if"

5. Add a Counting Loop 🔄

  • What to do: Add this for-loop to count attempts
  • File(s) touched: Program.cs (add before existing code)
  • Code snippet:
int secret = 15;

for (int attempt = 1; attempt <= 3; attempt++)
{
    Console.Write($"Attempt {attempt} - Guess my number (1-20): ");
    int guess = int.Parse(Console.ReadLine());
    
    if (guess == secret)
    {
        Console.WriteLine("Perfect! You won!");
        break;
    }
    else if (guess > secret)
    {
        Console.WriteLine("Too high!");
    }
    else
    {
        Console.WriteLine("Too low!");
    }
}
  • Why it matters: Loops repeat actions without copying code
  • Git command: git add . && git commit -m "Add for-loop for multiple attempts"

6. Create Complete Guessing Game 🎮

  • What to do: Replace all code with this full game
  • File(s) touched: Program.cs
  • Code snippet:
Random random = new Random();
int secret = random.Next(1, 21);
bool won = false;

Console.WriteLine("=== Number Guessing Game ===");
Console.WriteLine("I'm thinking of a number between 1 and 20!");

for (int attempt = 1; attempt <= 5 && !won; attempt++)
{
    Console.Write($"Attempt {attempt}/5 - Your guess: ");
    int guess = int.Parse(Console.ReadLine());
    
    if (guess == secret)
    {
        Console.WriteLine($"🎉 Amazing! You guessed {secret} in {attempt} tries!");
        won = true;
    }
    else if (guess > secret)
    {
        Console.WriteLine("📉 Too high!");
    }
    else
    {
        Console.WriteLine("📈 Too low!");
    }
}

if (!won)
{
    Console.WriteLine($"💀 Game over! The number was {secret}.");
}
  • Why it matters: Combines decisions and loops into a real game
  • Git command: git add . && git commit -m "Create complete guessing game"

7. Push Your Code 🚀

  • What to do: In GitHub Desktop → Write commit message → Commit → Push origin
  • File(s) touched: Uploading to GitHub
  • Code snippet: None
  • Why it matters: Saves your work online
  • Git command: git push origin main

Run & Test ▶️

  • Press Ctrl+F5 or click green ▶️ button in Rider
  • Expected output: Game asks for guesses, gives "too high/low" hints, celebrates wins
  • Common error: "Input string was not in a correct format" when typing letters
  • Fix: Only type numbers when the game asks for guesses

Bonus Challenge 🔥

Add a play-again feature using a while-loop!

Hint:

string playAgain = "yes";
while (playAgain == "yes")
{
    // Put your entire game code here
    Console.Write("Play again? (yes/no): ");
    playAgain = Console.ReadLine().ToLower();
}