我正在开发一个基于阵列的新工具和技术的刽子手游戏,我们还没有达到课程的这一部分工作,但是我试图抢先一步
到目前为止,我有这个代码(见下文)我尝试在零数组元素中显示每个字符的下划线(总共七个字符),然后如果猜测正确则显示字符位置,我'我只是在我的代码中寻找一些见解,也许是一篇文章甚至是一段代码片段,可能会对我的困境有所了解。
编辑:这是我的新代码,但在进入' a'对于亚利桑那州来说,它只能找到并打印出第一个' a'如何解决这个问题,找到并打印到控制台?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Array_Arizona_Test
{
class Program
{
static void Main(string[] args)
{
int count = 0;
string secretWord = "arizona";
char[] secret = new char[secretWord.Length];
Console.WriteLine("Welcome to Hangman!");
Console.WriteLine("\nFirst Word.....\n");
for (int i = 0; i < secret.Length; i++)
{
secret[i] = '_';
}
for (int i = 0; i < secret.Length; i++)
{
Console.Write(secret[i] + " ");
}
do
{
for (int i = 0; i < secretWord.Length; i++)
{
Console.Write("\n\nPlease enter your guess : ");
char input = (Console.ReadLine().ToCharArray()[0]);
if (secretWord[i] == input)
{
secret[i] = input;
for (int x = 0; x < secret.Length; x++)
{
Console.Write(secret[x] + " ");
}
}
else
{
for (int j = 0; j < secret.Length; j++)
{
Console.Write(secret[i] + " ");
}
}
}
count++;
} while (count < 5);
}
}
}
答案 0 :(得分:1)
将循环更改为以下内容:
char[] guess = underscores.ToCharArray();
for (int x = 0; x < characters.Length; x++)
{
if (userInput == characters[x])
{
guess[x] = userInput;
}
}
Console.Write(guess);
您还需要添加另一个循环,以允许用户在同一个单词上多次猜测。
答案 1 :(得分:1)
这是你应该做的事情
阅读此http://en.wikipedia.org/wiki/Hangman_(game)
这是我刚写的代码
static void Main(string[] args)
{
string secreteWord = "Arizona";
//have a dash array equal to the number of the letters of the secret word
char[] a = new char[secreteWord.Length];
for (int i = 0; i < a.Length; i++)
{
a[i] = '_';
}
// Tell the user the number of letters through the dashes
for (int i = 0; i <a.Length ; i++)
{
Console.Write(a[i]+ " ");
}
// ask the user to guess
Console.WriteLine();
int count = 0;
do
{
Console.WriteLine("Enter your guess letter");
char input = Console.ReadLine().ToCharArray()[0];
for (int i = 0; i < secreteWord.Length; i++)
{
//if the user guessed right, replace the correct dash and display to the user
if (secreteWord[i] == input)
{
count++; //update the count to check when to exit
a[i] = input; //here if the user guess correct, we replace the dash with the input
//now we display the dash array after it is modified
for (int j = 0; j < a.Length; j++)
{
Console.Write(a[j] + " ");
}
}
}
Console.WriteLine();
}
while (count < a.Length);
Console.WriteLine("You guessed it right");
Console.ReadLine();
}