C# 如何为猜谜游戏存储分数?
我是新来的,对编写c#程序还不熟悉。有人能帮我在c#控制台上玩猜谜游戏吗?我需要将玩家的猜测限制在10次尝试,分数减去10次猜测。当玩家赢了,如果他们想再玩,回答“否”,游戏结束时会显示所有玩家的得分和平均得分。如何开始计分代码?有人能给我一些想法或代码示例,我可以用它来创建一个模式吗?多谢各位 这是我的密码:C# 如何为猜谜游戏存储分数?,c#,C#,我是新来的,对编写c#程序还不熟悉。有人能帮我在c#控制台上玩猜谜游戏吗?我需要将玩家的猜测限制在10次尝试,分数减去10次猜测。当玩家赢了,如果他们想再玩,回答“否”,游戏结束时会显示所有玩家的得分和平均得分。如何开始计分代码?有人能给我一些想法或代码示例,我可以用它来创建一个模式吗?多谢各位 这是我的密码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using S
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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间中期
{
班级计划
{
int答案;
智力猜测;
静态void Main(字符串[]参数)
{
程序p=新程序();
p、 开口();
p、 随机化();
while(true)
{
p、 用户数据();
p、 显示();
}
}
//介绍方法
私人开放
{
WriteLine(“欢迎来到猜谜游戏!请按任意键继续!”);
Console.ReadLine();
}
//获取用户输入的方法
私有void userData()
{
控制台。WriteLine(“输入您的猜测编号”);
guess=int.Parse(Console.ReadLine());
}
//显示方法
专用void Display()
{
如果(猜测=答案)
{
Console.WriteLine(“恭喜你赢了!”);
控制台。WriteLine(“分数:”);
再次播放();
}
else if(猜测>回答)
{
WriteLine(“你的猜测很高”);
}
else if(猜测<回答)
{
Console.WriteLine(“你的猜测很低”);
}
}
个人无效分数()
{
整数[]分数=新整数[100];
}
私有无效平均分数()
{
}
//再玩问题的方法
二等兵
{
Console.WriteLine(“您想再次播放吗?是或否”);
字符串respond=Console.ReadLine().ToLower();
如果(回答=“是”)
{
开口();
随机化();
while(true)
{
用户数据();
显示();
}
}
否则如果(响应=“否”)
{
EndProgram();
}
其他的
{
WriteLine(“您输入了错误的响应。”);
Console.WriteLine(“将自动退出您”);
EndProgram();
}
}
//随机数发生器的设计方法
私有void随机化()
{
Random rand=新的Random();
答案=兰特。下一个(1500);
}
//终端程序设计方法
私有程序()
{
Console.WriteLine(“\n”);
Console.WriteLine(“按任意键退出”);
Console.ReadKey();
}
}
}
就在类级别add的int guess下方
智力得分=0;
然后,当用户得到正确答案时,添加
分数++
希望这有帮助 向userData()方法添加计数器。每次猜测后递增计数器 在Display()方法中使用计数器计算分数 考虑从Display()中调用userData()
我为透明度编写了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();
}
}
谢谢你的回复,我会试试你的想法。我还有一个问题,如何使用数组跟踪玩家玩的游戏的分数?你能给我另一个示例代码吗?我建议你不要对每场比赛的分数使用数组,而是使用
列表。这样,您只需不断添加新分数,列表就会自动变长,然后只需使用LINQ.Average()
方法即可获得平均值。
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;
}
}
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();
}
}