MessageBox.Show 显示速度太快

问题描述 投票:0回答:2

我正在尝试创建我自己的“舰队战”版本,但我卡在了 99.9%。这就是原因。一艘船被摧毁后,它的颜色应该变成红色,然后会出现

MessageBox
“你赢了”或“你输了”。所以现在,如果人工智能摧毁了我的最后一艘船,一切都会顺利,所有的船都会一艘一艘地变成红色,然后出现“你输了”的消息。但在相反的情况下 - 如果我赢了,人工智能飞船就会变成红色,但是当我在
MessageBox
“你赢了”处单击“确定”后,最后一艘飞船的最后一个单元格变成红色。我在这两种情况下都使用相同的算法,并想知道我做错了什么。这是人工智能部分:

// 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);
            }
        }

有人有想法吗?

c# messagebox
2个回答
0
投票

正如您所说,两种算法是相同的,但窗口会在您点击“确定”按钮后刷新您的表单。
所以我建议在显示msgbox之前自己刷新一下。

        if (AIShips == 0)
        {
            dataGridView2.Refresh(); // <-------- +
            dataGridView2.Enabled = false;                    
            MessageBox.Show("You won!", "Congrats!", MessageBoxButtons.OK);
        }

您的代码有点乱,我不确定网格视图名称,如果您的问题与第一个有关,请尝试以下操作:

dataGridView1.Refresh();


0
投票

这些算法可能看起来相同,但其实不然。有很多重复的代码,很难确保它们都做“相同的事情”。尝试将此代码的各个部分分解为较小的方法,以便您可以重用它们。

在没有看到一切的情况下,我认为这就是问题所在:

AI

if (...) //Ship is destroyed
{
    // ...
    if (ships == 0)
    {
        MessageBox.Show(...);
    }
}

Player

if (...) //ship is destroyed
{
    // ...
}

if (ships == 0)
{
    MessageBox.Show(...);
}

MessageBox.Show
称为“船舶被摧毁”
if
块内部与外部。

© www.soinside.com 2019 - 2024. All rights reserved.