Photo AI

Question 16 is about a dice game played against a computer - AQA - GCSE Computer Science - Question 16 - 2023 - Paper 1

Question icon

Question 16

Question-16-is-about-a-dice-game-played-against-a-computer-AQA-GCSE Computer Science-Question 16-2023-Paper 1.png

Question 16 is about a dice game played against a computer. The aim of the game is to get as close to a score of 21 as you can, without going over 21. If your score... show full transcript

Worked Solution & Example Answer:Question 16 is about a dice game played against a computer - AQA - GCSE Computer Science - Question 16 - 2023 - Paper 1

Step 1

Write a C# program to simulate this game.

96%

114 rated

Answer

using System;

class DiceGame
{
    static void Main(string[] args)
    {
        Random rand = new Random();
        int score = 0;
        string userResponse;

        do
        {
            int die1 = rand.Next(1, 7);
            int die2 = rand.Next(1, 7);
            int totalRoll = die1 + die2;
            score += totalRoll;

            Console.WriteLine($"Roll 1: {die1}");
            Console.WriteLine($"Roll 2: {die2}");
            Console.WriteLine($"Current score: {score}");

            if (score < 21)
            {
                Console.WriteLine("Would you like to roll again? (yes/no)");
                userResponse = Console.ReadLine();
            }
            else
            {
                userResponse = "no";
            }

        } while (userResponse.ToLower() == "yes");

        if (score == 21)
        {
            Console.WriteLine("Congratulations! You have won!");
        }
        else if (score > 21)
        {
            Console.WriteLine("You lost!");
        }
        else
        {
            int randomScore = rand.Next(15, 22);
            if (randomScore > score)
            {
                Console.WriteLine("You lost!");
            }
            else
            {
                Console.WriteLine("Congratulations! You have won!");
            }
        }
    }
}

Join the GCSE students using SimpleStudy...

97% of Students

Report Improved Results

98% of Students

Recommend to friends

100,000+

Students Supported

1 Million+

Questions answered

;