问题描述
我正在尝试创建自己的“舰队之战”版本,但目前停留在99.9%。这就是为什么。摧毁一艘船后,其颜色应变为红色,然后出现MessageBox
“您赢了”或“您输了”。因此,现在,如果AI摧毁了我的最后一艘船,那么一切都很好,所有的船都一一变成红色,然后出现“您输了”的信息。但是在相反的情况下-如果我赢了,则AI船会变成红色,但是在MessageBox
“您赢了”上单击“确定”后,最后一艘船的最后一个单元会变成红色。我在两种情况下都使用相同的算法,想知道我在做什么错。这是AI部分:
// AI hits a ship
if ((arrayOccupiedCellsBattlefield1[rowIndex,columnIndex] == "1") || (arrayOccupiedCellsBattlefield1[rowIndex,columnIndex] == "2") ||
(arrayOccupiedCellsBattlefield1[rowIndex,columnIndex] == "3") || (arrayOccupiedCellsBattlefield1[rowIndex,columnIndex] == "4"))
{
AIHuntingAShip = true;
dataGridView1.Rows[rowIndex].Cells[columnIndex].Value = " X";
arrayShipsHitbyaimap[rowIndex,columnIndex] = " X"; // AI marks the burning enemy ships
dataGridView1.Rows[rowIndex].Cells[columnIndex].Style.BackColor = Color.Red;
arrayAITakenShots[transformCoordinatesToCellNumber(rowIndex,columnIndex)] = transformCoordinatesToCellNumber(rowIndex,columnIndex);
storeRowIndex = rowIndex;
storeColumnIndex = columnIndex;
arrayShipsHitbyAIXY[detectedShipLength] = transformCoordinatesToCellNumber(rowIndex,columnIndex);
detectedShipLength++;
if (detectedShipLength == int.Parse(arrayOccupiedCellsBattlefield1[rowIndex,columnIndex])) // AI destroys a ship (the array stores the length of the ship in each of the ship's cells)
{
for (int i = 0; i < detectedShipLength; i++) // placing " o"s around the destroyed ship
{
restrictArea(int.Parse(tranformCellNumberToCoordinates(arrayShipsHitbyAIXY[i])[0].ToString()),int.Parse(tranformCellNumberToCoordinates(arrayShipsHitbyAIXY[i])[1].ToString()),dataGridView1,arrayShipsHitbyaimap,arrayAITakenShots);
}
detectedShipLength = 0;
AIHuntingAShip = false;
playerShips--;
textBox6.Text = playerShips.ToString();
if (playerShips == 0)
{
MessageBox.Show("You lost!","Try it again!",MessageBoxButtons.OK);
dataGridView2.Enabled = false;
}
}
这是我的职责所在
// player hits a ship
if ((arrayOccupiedCellsBattlefield2[e.RowIndex,e.ColumnIndex] != null) && (arrayOccupiedCellsBattlefield2[e.RowIndex,e.ColumnIndex] != " ."))
{
dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = " X";
arrayShipsHitbyPlayerMap[e.RowIndex,e.ColumnIndex] = " X";
dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Red;
arrayPlayerTakenShots[transformCoordinatesToCellNumber(e.RowIndex,e.ColumnIndex)] = transformCoordinatesToCellNumber(e.RowIndex,e.ColumnIndex);
int shipIdent = int.Parse(arrayOccupiedCellsBattlefield2[e.RowIndex,e.ColumnIndex][0].ToString()); // ship ID
int playeRSShipLength = int.Parse(arrayOccupiedCellsBattlefield2[e.RowIndex,e.ColumnIndex][1].ToString()); // ship length
arrayForPlayeRSShipsXY[shipIdent] += e.RowIndex.ToString() + e.ColumnIndex.ToString() + ";"; // save the coordinates of this particular ship
arrayForPlayerCounters[shipIdent]++; // increase this particular cell counter basing on the "shipIdent"
if (arrayForPlayerCounters[shipIdent] == playeRSShipLength) // ship destroyed,so mark the area with " o"
{
for (int i = 0; i < playeRSShipLength; i++)
{
string xy = extractXYForPlayeRSShip(arrayForPlayeRSShipsXY[shipIdent])[i].ToString();
int x = int.Parse(xy.ToString()[0].ToString());
int y = int.Parse(xy.ToString()[1].ToString());
restrictArea(x,y,dataGridView2,arrayShipsHitbyPlayerMap,arrayPlayerTakenShots);
}
AIShips--;
textBox7.Text = AIShips.ToString();
}
if (AIShips == 0)
{
dataGridView2.Enabled = false;
MessageBox.Show("You won!","Congrats!",MessageBoxButtons.OK);
}
}
有人有想法吗?谢谢您的帮助。
解决方法
算法看似相同,但事实并非如此。有很多重复的代码,很难确保它们都做“相同的事情”。尝试将这段代码分成较小的方法,以便您可以重用它们。
没有看到所有内容,我认为这是问题所在
AI
:
if (...) //Ship is destroyed
{
// ...
if (ships == 0)
{
MessageBox.Show(...);
}
}
Player
if (...) //ship is destroyed
{
// ...
}
if (ships == 0)
{
MessageBox.Show(...);
}
MessageBox.Show
在“船毁了” if
块的内部与外部被调用。
这个问题对于StackOverflow来说并不是最好的,因为代码相当混乱并且没有简化。查看this link,了解如何提出良好的问题。但是要坚持下去!
,正如您所说的,两种算法是相同的,但是在您单击“确定”按钮后,窗口会刷新您的表单。 因此,我建议您在显示msgbox之前先进行刷新。
if (AIShips == 0)
{
dataGridView2.Refresh(); // <-------- +
dataGridView2.Enabled = false;
MessageBox.Show("You won!","Congrats!",MessageBoxButtons.OK);
}
您的代码有点混乱,我不确定网格视图名称,如果您的问题与第一个视图有关,请尝试以下操作:dataGridView1.Refresh();