我目前正在尝试使用C#创建一个战舰骨骼模块,并且我已经完成了大部分代码,但是当我尝试在代码中标记命中和未命中时,我会陷入困境玩家猜对错或错误。
我试图做的是激活Case" H"和案例" M"当人们输入适当的网格坐标时。
代码在C#中。
应该发生什么:
基本战舰。这是我第一个使用C#的真实项目,隐藏船只对我来说并不重要。我正忙着拥有船只和#39;坐标被替换为在案例H和案例M中分配的适当的X和背景/前景色。
如果我遗漏了任何内容并且您需要任何其他信息,请告诉我们!
这是我到目前为止的代码:
class Program
{
static void Main(string[] args)
{
while (true)
{
//Basic function that allows the user to enter their 'guesses' for the ship locations.
printGrid(Grid);
Console.WriteLine("Type 'exit' to exit.");
Console.WriteLine("Enter your guess: ");
string cons = Console.ReadLine();
cons.Substring(0, 1);
cons.Substring(1, 1);
cons = cons.ToUpper();
char column = cons[0];
char row = cons[1];
int num = cons[0];
bool result = Int32.TryParse(cons, out num);
//Disallows the user from entering anything other than what's valid. Valid characters are, obviously, A - J.
string validChars = "ABCDEFGHIJ";
//This allows the user to exit the application by typing "Exit" instead of guessing.
if (cons == "EXIT")
{
return;
}
//Again, checks to see if the user inputs any numbers higher than what's allotted.
else if (!validChars.Contains(column) || (num >= 11))
{
Console.WriteLine("Please enter only A-J, and 1-10 e.g 'B5'");
}
Console.WriteLine("Column = {1}, Row = {0}", row, column);
if (result == false)
{
Console.WriteLine("Guess was a miss!");
}
else
{
Console.WriteLine("Guess was a hit!");
}
}
}
private static readonly char[,] Grid = new char[,]
{
{'.', '.', '.', '.', 'S', 'S', 'S', '.', '.', '.'},
{'P', 'P', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', 'P'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', 'P'},
{'.', '.', 'A', 'A', 'A', 'A', 'A', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', 'B', '.', '.'},
{'.', 'S', '.', '.', '.', '.', '.', 'B', '.', '.'},
{'.', 'S', '.', '.', '.', '.', '.', 'B', 'P', 'P'},
{'.', 'S', '.', '.', '.', '.', '.', 'B', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
};
public static void printGrid(Char[,] Grid)
{
//Let's make the layout pretty.
Console.WriteLine(" | A | B | C | D | E | F | G | H | I | J |");
Console.WriteLine("-----#-----#-----#-----#-----#-----#-----#-----#-----#-----#------#");
for (int i = 0; i < 10; i++)
{
if (i == 9)
{
Console.Write(" {0} ", i + 1);
}
else
Console.Write(" {0} ", i + 1);
for (int j = 0; j < 10; j++)
{
ShipColors(Grid[i, j]);
}
//This needs to match the header in order to format properly.
Console.Write(" |\r\n");
Console.WriteLine("-----#-----#-----#-----#-----#-----#-----#-----#-----#-----#------#");
}
}
//Now we're going to declare which colors each ship's cell is going to be.
public static void ShipColors(char useThis)
{
/*
* Legend for each ship name:
* P = patrol boat
* S = Submarine
* B = Battleship
* A = Aircraft Carrier
*/
switch(useThis)
//Going to use case statements here to automatically color any of the grid's cells with any special characters.
//Using cases allows the grid to be re-organized and still be colored properly.
{
case '.':
Console.Write(" | ");
//Blank space here needed to be added for formatting the cells properly.
Console.Write(" ");
break;
case 'A':
Console.Write(" | ");
Console.BackgroundColor = ConsoleColor.Blue;
Console.Write(" A ");
break;
case 'B':
Console.Write(" | ");
Console.BackgroundColor = ConsoleColor.Green;
Console.ForegroundColor = ConsoleColor.Black;
Console.Write(" B ");
break;
case 'P':
Console.Write(" | ");
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Black;
Console.Write(" P ");
break;
case 'S':
Console.Write(" | ");
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Black;
Console.Write(" S ");
break;
case 'H':
Console.Write(" | ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(" X ");
break;
case 'M':
Console.Write(" | ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" X ");
break;
}
Console.ResetColor();
}
}
答案 0 :(得分:0)
你试图在一种方法中做很多事情。理想情况下,您可以将其分解为多个方法调用 - 一个用于验证输入,一个用于检查命中,另一个用于重新绘制网格,其中包含一些他们猜测的点的指示。
但是,为了帮助解决这个问题,在您完成输入验证后,您需要执行以下操作:
1
开头显示行,但数组索引以0
开头,因此我们需要从输入中减去一行。A
的int值为65
,并且由于A
是第一行,我们从列中减去65
以获取索引。然后只需检查索引Grid
的{{1}}数组,看它是否包含一封信:
[newRow, newCol]
以下是代码的半清理版本:
if (char.IsLetter(Grid[rowNumber, colNumber]))
{
Console.WriteLine("HIT!");
}
else
{
Console.WriteLine("MISS.");
}