Reputation: 31
I'm new here and new to coding a c# program. Can anyone help me on a guessing game in c# console? I need to limit the player's guess to 10 tries and the score is minus 10 the number of guesses. When the player win, and respond no if they want to play again, it will end the game displaying all the players score and their average scores. How can I start the scoring code ? Can someone give me any idea or a code sample that I can use for a pattern? thank you
here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Midterm
{
class Program
{
int answer;
int guess;
static void Main(string[] args)
{
Program p = new Program();
p.Opening();
p.randomize();
while (true)
{
p.userData();
p.Display();
}
}
//method for the introduction
private void Opening()
{
Console.WriteLine("Welcome to the Guessing Game! Pless any key to continue!");
Console.ReadLine();
}
//method for getting user input
private void userData()
{
Console.WriteLine("Enter your guess number.");
guess = int.Parse(Console.ReadLine());
}
//method for display
private void Display()
{
if (guess == answer)
{
Console.WriteLine("Congratulation. You won!");
Console.WriteLine("Score: ");
PlayAgain();
}
else if (guess > answer)
{
Console.WriteLine("Your guess is high");
}
else if (guess < answer)
{
Console.WriteLine("Your guess is low");
}
}
private void Score()
{
int[] score = new int[100];
}
private void AverageScore()
{
}
//method for playing again question
private void PlayAgain()
{
Console.WriteLine("Do you want to play again? Yes or No");
string respond = Console.ReadLine().ToLower();
if (respond == "yes")
{
Opening();
randomize();
while (true)
{
userData();
Display();
}
}
else if (respond == "no")
{
EndProgram();
}
else
{
Console.WriteLine("You enter a wrong respond.");
Console.WriteLine("Will automatic exit you");
EndProgram();
}
}
//method for the random number generator
private void randomize()
{
Random rand = new Random();
answer = rand.Next(1, 500);
}
//method for end program
private void EndProgram()
{
Console.WriteLine("\n");
Console.WriteLine("Press any key to Exit");
Console.ReadKey();
}
}
}
Upvotes: 2
Views: 2493
Reputation: 311
I wrote HLSL code for transparency and then I will transparent one image inside the body and another image used for my foreground :
sampler stream : register(s0);
sampler back : register(s1);
sampler character : register(s2);
float4 DepthToRGB(float2 texCoord : TEXCOORD0) : COLOR0
{
// We can't easily sample non-normalized data,
// Texture is a short packed stuffed into a BGRA4444 (4 bit per component normalized)
// It's not really BGRA4444 which is why we have to reverse the normalization here
float4 color = tex2D(stream, texCoord);
float4 backColor = tex2D(back, texCoord);
float4 characterColor = tex2D(character, texCoord);
// convert from [0,1] to [0,15]
color *= 15.01f;
// unpack the individual components into a single int
int4 iColor = (int4)color;
int depthV = iColor.w * 4096 + iColor.x * 256 + iColor.y * 16 + iColor.z;
// player mask is in the lower 8 bits
int playerId = depthV % 8;
if(playerId == 0)
{
//Not yet recognized
return backColor * float4(1.0, 1.0, 1.0, 1.0);
}
else
{
return backColor * characterColor;
}
}
technique KinectDepth
{
pass KinectDepth
{
PixelShader = compile ps_2_0 DepthToRGB();
}
}
Upvotes: 2
Reputation: 107
Add a counter to the userData() method. Increment the counter after each guess.
Use the counter to calculate score in the Display() method.
Consider calling userData() from within Display().
if (attempts < 10 && answer != guess)
Console.WriteLine("some feedback");
userData();
This is subjective, but if I were writing this program I would probably structure the feedback loop around the scoring system.
Example pseudocode:
private void start() {
display welcome message;
answer = new random number;
guess = getInput();
score = 10;
win = false;
while (score > 0 && win == false) {
if ( !evaluateGuess(answer, guess) ) {
guess = getInput();
score--;
}
else if ( evaluateGuess(answer, guess) ) {
display winning message and score;
win = true;
playAgain();
}
}
}
private void evaluateGuess(answer, guess) {
if (answer == guess) {
display feedback;
return true;
}
else {
display other feedback;
return false;
}
}
Upvotes: 2
Reputation: 263
just below where you have int guess at the class level add int score = 0; then when the user gets a correct answer add score++;
hope this helps!
Upvotes: -1