![](/img/trans.png)
[英]More then one mathematical operation on 2D array causes incorrect values inside 2D array
[英]How to see if more than one of the same values is used in a row inside a 2D array - c#
我是編碼新手,正在嘗試用C#制作Connect 4游戲。 我有一個6×6的2D數組,並將其初始化為全0。 當玩家1輸入他們的選擇時,數組中該列最低位置的0被替換為1。與玩家2相同-用2代替了0(僅)。
我似乎無法進行的工作是檢查每一列中是否有4個相同的值,檢查每一行中是否有4個相同的值,然后對角地檢查一行中的4個相同的值。 下面是我試圖使它沿着每一列在一行中搜索4個相同值的代碼。 能否請你幫忙。 謝謝
x = 0;
while (x <= 5)
{
for (row = 5; row >= 0; row--)
{
if (Grid[row, x] == 1)
{
Player1Score = Player1Score + 1;
}
else
{
Player1Score = 0;
}
if (Grid[row, x] == 2)
{
Player2Score = Player2Score + 1;
}
else
{
Player2Score = 0;
}
}
x = x + 1;
if (Player1Score == 4)
{
Console.Clear();
Console.WriteLine("Player1 wins");
}
else if (Player2Score == 4)
{
Console.Clear();
Console.WriteLine("Player2 wins");
}
}
怎么樣:擁有2個變量,player1Score,player2Score。
對於行:遍歷行中的每個元素,
如果是0
如果是1
當您完成該行的操作后,請檢查是否有任何玩家得分等於4,如果是,則贏得了游戲。 否則,重置分數並轉到另一行。
您對cols和對角線(在對角線中,col id等於行id)執行相同的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.