繁体   English   中英

如何为猜谜游戏存储分数?

[英]How to store score for a guessing game?

我是新来的,也是编码ac#程序的新手。 谁能在c#控制台中帮助我进行猜谜游戏? 我需要将玩家的猜测限制为10次尝试,并且分数是猜测数目的负10。 当玩家获胜时,如果他们想再次玩,则不回答,游戏将结束,显示所有玩家得分和他们的平均得分。 如何开始计分代码? 有人可以给我提供我可以用于模式的任何想法或代码示例吗? 谢谢

这是我的代码:

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();
        }

    }
}

将计数器添加到userData()方法。 每次猜测后增加计数器。

使用计数器来计算Display()方法中的分数。

考虑从Display()中调用userData()。

if (attempts < 10 && answer != guess) 
  Console.WriteLine("some feedback");
  userData();

这是主观的,但是如果我正在编写此程序,则可能会围绕评分系统构建反馈循环。

伪代码示例:

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;
  }
}

我为透明化编写了HLSL代码,然后将体内的一个图像和用于前景的另一个图像透明化:

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();
}
}

在您可以在班级进行智力预测的位置下方,添加智力得分= 0; 然后,当用户获得正确答案时,添加评分++;

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM