How to resolve the algorithm Guess the number step by step in the C# programming language
How to resolve the algorithm Guess the number step by step in the C# programming language
Table of Contents
Problem Statement
Write a program where the program chooses a number between 1 and 10. A player is then prompted to enter a guess. If the player guesses wrong, then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a "Well guessed!" message, and the program exits. A conditional loop may be used to repeat the guessing until the user is correct.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Guess the number step by step in the C# programming language
The first code is a simple guessing game in C#. It generates a random number between 1 and 10 and asks the user to guess it. The user can keep guessing until they get the correct answer.
The second code is a bit more complex. It's a game where two players (a computer and a human) try to guess each other's secret numbers. The game uses a binary search algorithm to help the computer guess the human's number.
Here's a breakdown of the code:
Class: Program
Method: Main
Variables:
rand
: ARandom
object used to generate random numbers.t
: The current round number.h2
: The upper bound of the range of numbers to guess from.h1
: The lower bound of the range of numbers to guess from.f
: A flag indicating whether the game is finished.comp
: The computer's guess.human
: The human's guess.
Logic:
The while
loop runs until the flag f
is set to 1, indicating that the game is finished.
Inside the loop, the current round number t
, the computer's guess comp
, and the human's guess human
are printed to the console.
Then, the code checks if the computer's guess is less than the human's guess. If it is, the upper bound of the range of numbers to guess from is updated to the average of the current upper bound and the computer's guess.
If the computer's guess is greater than the human's guess, the lower bound of the range of numbers to guess from is updated to the average of the current lower bound and the computer's guess.
If the computer's guess is equal to the human's guess, the game is finished and the number of rounds it took to guess is printed to the console.
The loop continues until the game is finished or the maximum number of rounds is reached.
Source code in the csharp programming language
using System;
class GuessTheNumberGame
{
static void Main()
{
int randomNumber = new Random().Next(1, 11);
Console.WriteLine("I'm thinking of a number between 1 and 10. Can you guess it?");
while(true)
{
Console.Write("Guess: ");
if (int.Parse(Console.ReadLine()) == randomNumber)
break;
Console.WriteLine("That's not it. Guess again.");
}
Console.WriteLine("Congrats!! You guessed right!");
}
};
using System; using System.Text; //daMilliard.cs
namespace DANILIN
{ class Program
{ static void Main(string[] args)
{ Random rand = new Random();int t=0;
int h2=100000000; int h1=0; int f=0;
int comp = rand.Next(h2);
int human = rand.Next(h2);
while (f<1)
{ Console.WriteLine();Console.Write(t);
Console.Write(" ");Console.Write(comp);
Console.Write(" ");Console.Write(human);
if(comp < human)
{ Console.Write(" MORE");
int a=comp; comp=(comp+h2)/2; h1=a; }
else if(comp > human)
{ Console.Write(" less");
int a=comp; comp=(h1+comp)/2; h2=a;}
else {Console.Write(" win by ");
Console.Write(t); Console.Write(" steps");f=1;}
t++; }}}}
You may also check:How to resolve the algorithm Greatest common divisor step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the SNOBOL4 programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the R programming language
You may also check:How to resolve the algorithm JortSort step by step in the JavaScript programming language
You may also check:How to resolve the algorithm String append step by step in the MiniScript programming language