C# 阵列夹带处理
我的游戏战舰有问题,更确切地说是进入治疗。如果我在阵列中选择坐标进行拍摄(X=第一个坐标,Y=第二个坐标),那么我可以在阵列中选择相同的坐标更多次,我想修复它。 我的意思是我只想选择一次特定的坐标(数组索引),而不是多次 谢谢你的帮助 有一个代码: 对不起,如果我在论坛上输入了错误的代码C# 阵列夹带处理,c#,arrays,C#,Arrays,我的游戏战舰有问题,更确切地说是进入治疗。如果我在阵列中选择坐标进行拍摄(X=第一个坐标,Y=第二个坐标),那么我可以在阵列中选择相同的坐标更多次,我想修复它。 我的意思是我只想选择一次特定的坐标(数组索引),而不是多次 谢谢你的帮助 有一个代码: 对不起,如果我在论坛上输入了错误的代码 namespace Battleship { class Program { static void Main(string[] args) {
namespace Battleship
{
class Program
{
static void Main(string[] args)
{
string continue = "yes";
while (continue == "yes")
{
Console.WriteLine();
Console.WriteLine("----Battleship-----");
Console.WriteLine();
Console.WriteLine("Your name");
string jmeno = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("Hit left 7x");
Console.WriteLine();
Hodnoty h = new Hodnoty();
while (h.Trefa < 7)
{
h.Zadej();
}
Console.Clear();
Console.WriteLine(name + " won!!!");
Console.WriteLine();
Console.WriteLine(name + " se trefil/a " + h.Trefa + "x");
Console.WriteLine(name + " miss " + h.Vedle + "x");
Console.WriteLine("Do you wanna play again? [yes/no]");
pokracovat = Console.ReadLine();
}
Console.Clear();
Console.WriteLine("Click on Enter");
Console.ReadLine();
}
class Hodnoty
{
public int Hit = 0;
public int left = 0;
int left = 0;
int x = 0;
int y = 0;
string cislo;
int hodnota;
//MAIN PROBLEM (I Guess)
//------------------------------------------------------------------------------
public void Zadej() /
{
var mapa = new char[5, 5];
using (var reader = new StreamReader("Lode.txt"))
{
Console.WriteLine(" ¦ 01234");
Console.WriteLine(" ¦----------");
for (var i = 0; i < 5; i++)
{
Console.Write((i).ToString() + " ¦ ");
var pole = reader.ReadLine();
for (var j = 0; j < 5; j++)
{
mapa[i, j] = char.Parse(pole[j].ToString());
}
Console.WriteLine();
}
}
Console.WriteLine();
Console.Write("Enter coordinates X = ");
cislo = Console.ReadLine();
if (int.TryParse(cislo, out hodnota))
{
x = hodnota;
}
else
{
Console.WriteLine("Enter number");
}
Console.Write("Enter bumber Y = ");
cislo = Console.ReadLine();
if (int.TryParse(cislo, out hodnota))
{
y = hodnota;
}
else
{
Console.WriteLine("Enter number");
}
if (mapa[x, y].Equals('X'))
{
Console.Clear();
Console.WriteLine("-----Battleship-----");
Console.WriteLine();
Trefa += 1;
Zbyva = 7 - Trefa;
Console.WriteLine("Hit " + Hit + "x ");
Console.WriteLine();
Console.WriteLine("hits left " + left + "x");
Console.WriteLine();
}
else
{
Console.Clear();
Console.WriteLine("-----Battleship-----");
Console.WriteLine();
Vedle += 1;
Console.WriteLine("Miss " + Miss + "x ");
Console.WriteLine();
Console.WriteLine("hits left " + left + "x");
Console.WriteLine();
}
}
}
}
}
名称空间战舰
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串continue=“yes”;
while(continue==“yes”)
{
Console.WriteLine();
控制台。写线(“----战舰------”);
Console.WriteLine();
Console.WriteLine(“您的名字”);
字符串jmeno=Console.ReadLine();
Console.WriteLine();
控制台写入线(“左击7x”);
Console.WriteLine();
Hodnoty h=新的Hodnoty();
而(h.Trefa<7)
{
h、 Zadej();
}
Console.Clear();
Console.WriteLine(名称+“赢得!!!”);
Console.WriteLine();
控制台写入线(名称+“se trefil/a”+h.Trefa+“x”);
控制台写入线(名称+小姐+h.韦德尔+x);
Console.WriteLine(“你想再玩一次吗?[是/否]”);
pokracovat=Console.ReadLine();
}
Console.Clear();
Console.WriteLine(“单击回车”);
Console.ReadLine();
}
霍德诺蒂级
{
公共int Hit=0;
公共int左=0;
int左=0;
int x=0;
int y=0;
弦西斯洛;
霍德诺塔国际酒店;
//主要问题(我猜)
//------------------------------------------------------------------------------
公开无效Zadej()/
{
var mapa=新字符[5,5];
使用(var reader=newstreamreader(“Lode.txt”))
{
控制台.写线(“△01234”);
Console.WriteLine(“”------------“”);
对于(变量i=0;i<5;i++)
{
Console.Write((i).ToString();
var pole=reader.ReadLine();
对于(var j=0;j<5;j++)
{
mapa[i,j]=char.Parse(pole[j].ToString());
}
Console.WriteLine();
}
}
Console.WriteLine();
Console.Write(“输入坐标X=”);
cislo=Console.ReadLine();
if(内锥虫(西斯洛,外霍德诺塔))
{
x=霍德诺塔;
}
其他的
{
Console.WriteLine(“输入编号”);
}
Console.Write(“输入bumber Y=”);
cislo=Console.ReadLine();
if(内锥虫(西斯洛,外霍德诺塔))
{
y=霍德诺塔;
}
其他的
{
Console.WriteLine(“输入编号”);
}
if(mapa[x,y].等于('x'))
{
Console.Clear();
控制台.WriteLine(“----战舰------”);
Console.WriteLine();
Trefa+=1;
Zbyva=7-Trefa;
控制台写入线(“点击”+点击+“x”);
Console.WriteLine();
控制台写入线(“点击左”+左+“x”);
Console.WriteLine();
}
其他的
{
Console.Clear();
控制台.WriteLine(“----战舰------”);
Console.WriteLine();
Vedle+=1;
控制台写入线(“未命中”+未命中+“x”);
Console.WriteLine();
控制台写入线(“点击左”+左+“x”);
Console.WriteLine();
}
}
}
}
}
问题:
我是说。。我只能输入同一个坐标一次,不能输入更多次。如果你不想在同一张地图上拍摄两次,你必须记住,那里发生了什么。您正在使用
char
状态标记。我建议使用枚举:
public enum MapStatus
{
Empty = 0,
Ship = 1,
HitShip = 2,
HitWater = 3
}
您的地图将如下所示:
MapStatus[,] mapTiles;
但是我们已经将int值设置为enum值。因此,我们可以将其转换为:
map[0,0] = (MapStatus)1; // This will set a ship to 1/1
现在你必须在每次拍摄时检查是否有水等
此外,您应该将逻辑移动到类“映射”或其他内容
嗯。。这种情况迅速升级:这里有一个完整的例子:
public enum MapStatus
{
Empty = 0,
Ship = 1,
HitShip = 2,
HitWater = 3
}
public class Map
{
MapStatus[,] mapTiles;
public Map(int[,] mapInt = null)
{
if (mapInt == null) // if there's no map in parameters we'll set a default map..
{
mapInt = new int[,]
{
{ 0, 0, 0, 0, 0 },
{ 0, 1, 1, 1, 1 }, // in this row there's a ship
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
};
}
// Initialize empty map
mapTiles = new MapStatus[mapInt.GetLength(0), mapInt.GetLength(1)];
// copy numbers into our map
for (int i = 0; i < mapTiles.GetLength(0); i++)
{
for (int j = 0; j < mapTiles.GetLength(1); j++)
{
mapTiles[i, j] = (MapStatus)mapInt[i, j];
}
}
}
/// <summary>
/// "Graphics-Engine" which enables us to print our map.
/// </summary>
public void PrintMap()
{
Console.Clear();
Console.Write(" ");
for (int j = 0; j < mapTiles.GetLength(1); j++)
{
Console.Write(j.ToString().PadLeft(8));
}
Console.WriteLine();
for (int i = 0; i < mapTiles.GetLength(0); i++)
{
Console.Write(i.ToString().PadLeft(8));
for (int j = 0; j < mapTiles.GetLength(1); j++)
{
Console.Write(mapTiles[i, j].ToString().PadLeft(8) + " ");
}
Console.WriteLine();
}
}
/// <summary>
/// Check if there are ships left. Have a look at Linq ;)
/// </summary>
public bool StillAlive()
{
return mapTiles.Cast<MapStatus>().Any(a => a == MapStatus.Ship);
}
public bool Shoot(int x, int y)
{
// Error-Checking if the values are valid..
if (x < 0 || x >= mapTiles.GetLength(0))
throw new ArgumentOutOfRangeException(string.Format("X-coordinate ({0}) is wrong (min: {1}, max: {2})!", x, 0, mapTiles.GetLength(0)));
if (y < 0 || y >= mapTiles.GetLength(1))
throw new ArgumentOutOfRangeException(string.Format("Y-coordinate ({0}) is wrong (min: {1}, max: {2})!", y, 0, mapTiles.GetLength(1)));
// Check if we shot here before..
if (mapTiles[x, y] == MapStatus.HitShip || mapTiles[x, y] == MapStatus.HitWater)
throw new ArgumentException(string.Format("You did already shoot the coordinates {0}/{1}", x, y));
// Shoot
if (mapTiles[x, y] == MapStatus.Empty)
{
mapTiles[x, y] = MapStatus.HitWater; // Change value
return false; // return the info that we didn't hit anything
}
else
{
mapTiles[x, y] = MapStatus.HitShip;
return true;
}
}
}
public static void Main(string[] args)
{
Map m = new Map(); // Initialize without map.
int x;
int y;
while (m.StillAlive()) // Loop untill all ships are sunk
{
m.PrintMap();
Console.Write("Enter X: ");
x = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Y: ");
y = Convert.ToInt32(Console.ReadLine());
try
{
bool hit = m.Shoot(x, y);
m.PrintMap();
if (hit)
Console.WriteLine("We hit a ship!");
else
Console.WriteLine("We hit only water!");
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
Console.WriteLine("(Press Enter to continue)");
Console.ReadLine();
}
Console.WriteLine("We won!");
Console.ReadLine();
}
公共枚举映射状态
{
空=0,
船舶=1,
HitShip=2,
HitWater=3
}
公共类地图
{
MapStatus[,]mapTiles;
公共映射(int[,]mapInt=null)
{
if(mapInt==null)//如果参数中没有映射,我们将设置默认映射。。
{
mapInt=newint[,]
{
{ 0, 0, 0, 0, 0 },
{0,1,1,1,1},//这一排有一艘船
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
};
}
//初始化空映射
mapTiles=新的MapStatus[mapInt.GetLength(0),mapInt.GetLength(1)];
//把数字复制到我们的地图上
for(int i=0;ido
{
int x, y;
Console.WriteLine("Enter Hit Coordinates (x,y):");
var input = Console.ReadLine();
var parts = input.Split(',');
if (parts.Length!=2)
{
Console.WriteLine("Please enter two values separated by a comma.");
break;
}
if (int.TryParse(parts[0].Trim(), out x) && int.TryParse(parts[1].Trim(), out y))
{
// use the x, y values below
Console.WriteLine($"User input was: ({x},{y})");
}
else
{
Console.WriteLine("Please enter numeric values.");
}
}
while (true);
var history = new List<(int x,int y)>();
var history = new List<(int x, int y)>();
do
{
Console.WriteLine("Enter Hit Coordinates (x,y):");
var input = Console.ReadLine();
var parts = input.Split(',');
if (parts.Length!=2)
{
Console.WriteLine("Please enter two values separated by a comma.");
break;
}
if (int.TryParse(parts[0].Trim(), out int x) && int.TryParse(parts[1].Trim(), out int y))
{
var coords = (x, y);
if (history.Contains(coords))
{
Console.WriteLine($"Coordinates ({x},{y}) Previously Played.");
}
else
{
// use the x, y values below
Console.WriteLine($"User input was: ({x},{y})");
history.Add(coords);
}
}
else
{
Console.WriteLine("Please enter numeric values.");
}
}
while (true);