所以我在C#上是一個完整的初學者,我一直在使用'csharpdemos'的Tic Tac Toe教程來嘗試和開發Connect 4遊戲。截至目前,除了一個問題之外,還可以玩遊戲。我有一個int movemade,跟蹤用戶所做的動作,因此它可以確定下一個是誰。但是,填充列時,儘管沒有設置值,但用戶單擊列時movemade仍會增加1。這意味着轉彎將不同步。我將在下面顯示我的代碼。C#Winforms連接四:如何檢查列是否已滿?
public class Board
{
public int movesMade = 0;
private Holder[,] holders = new Holder[5, 5];
public const int CT1 = 0;
public const int CT2 = 1;
public const int B = 2;
public void startBoard()
{
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 5; y++)
{
holders[x, y] = new Holder();
holders[x, y].setValue(B);
holders[x, y].setLocation(new Point(x, y));
}
}
}
public void detectHit(Point location)
{
int x = 0;
int y = 0;
if ((location.X < 100))
{
x = 0;
y = 4;
if (holders[0, 4].getValue() == CT1 || holders[0, 4].getValue() == CT2)
y = 3;
if (holders[0, 3].getValue() == CT1 || holders[0, 3].getValue() == CT2)
y = 2;
if (holders[0, 2].getValue() == CT1 || holders[0, 2].getValue() == CT2)
y = 1;
if (holders[0, 1].getValue() == CT1 || holders[0, 1].getValue() == CT2)
y = 0;
}
else if (location.X > 100 && location.X < 200)
{
x = 1;
y = 4;
if (holders[1, 4].getValue() == CT1 || holders[1, 4].getValue() == CT2)
y = 3;
if (holders[1, 3].getValue() == CT1 || holders[1, 3].getValue() == CT2)
y = 2;
if (holders[1, 2].getValue() == CT1 || holders[1, 2].getValue() == CT2)
y = 1;
if (holders[1, 1].getValue() == CT1 || holders[1, 1].getValue() == CT2)
y = 0;
}
else if (location.X > 200 && location.X < 300)
{
x = 2;
y = 4;
if (holders[2, 4].getValue() == CT1 || holders[2, 4].getValue() == CT2)
y = 3;
if (holders[2, 3].getValue() == CT1 || holders[2, 3].getValue() == CT2)
y = 2;
if (holders[2, 2].getValue() == CT1 || holders[2, 2].getValue() == CT2)
y = 1;
if (holders[2, 1].getValue() == CT1 || holders[2, 1].getValue() == CT2)
y = 0;
}
else if (location.X > 300 && location.X < 400)
{
x = 3;
y = 4;
if (holders[3, 4].getValue() == CT1 || holders[3, 4].getValue() == CT2)
y = 3;
if (holders[3, 3].getValue() == CT1 || holders[3, 3].getValue() == CT2)
y = 2;
if (holders[3, 2].getValue() == CT1 || holders[3, 2].getValue() == CT2)
y = 1;
if (holders[3, 1].getValue() == CT1 || holders[3, 1].getValue() == CT2)
y = 0;
}
else if (location.X > 400)
{
x = 4;
y = 4;
if (holders[4, 4].getValue() == CT1 || holders[4, 4].getValue() == CT2)
y = 3;
if (holders[4, 3].getValue() == CT1 || holders[4, 3].getValue() == CT2)
y = 2;
if (holders[4, 2].getValue() == CT1 || holders[4, 2].getValue() == CT2)
y = 1;
if (holders[4, 1].getValue() == CT1 || holders[4, 1].getValue() == CT2)
y = 0;
}
movesMade++;
if (holders[x, 0].getValue() == CT2)
{
movesMade = 1;
movesMade++;
}
if (holders[x, 0].getValue() == CT1)
{
movesMade = 0;
movesMade++;
}
if (movesMade % 2 == 0)
{
if (holders[x, 0].getValue() == 2)
{
Drawing.drawCt1(new Point(x, y));
holders[x, y].setValue(CT1);
Drawing.drawCt2(new Point(x, y));
holders[x, y].setValue(CT2);
}
if (holders[0,y].getValue() == 1 || holders[0,y].getValue() == 0)
if (detectColumn())
{
MessageBox.Show("WINNER!");
}
if (detectRow())
{
MessageBox.Show("WINNER!");
}
if (detectDiagonal())
{
MessageBox.Show("WINNER!");
}
}
else
{
if (holders[x, 0].getValue() == 2)
{
Drawing.drawCt2(new Point(x, y));
holders[x, y].setValue(CT2);
Drawing.drawCt1(new Point(x, y));
holders[x, y].setValue(CT1);
}
if (detectColumn())
{
MessageBox.Show("WINNER!");
}
if (detectRow())
{
MessageBox.Show("WINNER!");
}
if (detectDiagonal())
{
MessageBox.Show("WINNER!");
}
}
}
public bool detectColumn()
{
bool isWon = false;
for (int x = 0; x < 5; x++)
{
if ((holders[x, 0].getValue() == CT1 && holders[x, 1].getValue() == CT1 && holders[x, 2].getValue() == CT1 && holders[x, 3].getValue() == CT1) || (holders[x, 1].getValue() == CT1 && holders[x, 2].getValue() == CT1 && holders[x, 3].getValue() == CT1 && holders[x, 4].getValue() == CT1) || (holders[x, 0].getValue() == CT2 && holders[x, 1].getValue() == CT2 && holders[x, 2].getValue() == CT2 && holders[x, 3].getValue() == CT2) || (holders[x, 1].getValue() == CT2 && holders[x, 2].getValue() == CT2 && holders[x, 3].getValue() == CT2 && holders[x, 4].getValue() == CT2))
{
return true;
}
}
return isWon;
}
public bool detectRow()
{
bool isWon = false;
for (int y = 0; y < 5; y++)
{
switch (y)
{
case 0:
if ((holders[0, y].getValue() == CT1 && holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1) || (holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1 && holders[4, y].getValue() == CT1) || (holders[0, y].getValue() == CT2 && holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2) || (holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2 && holders[4, y].getValue() == CT2))
{
return true;
}
break;
case 1:
if ((holders[0, y].getValue() == CT1 && holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1) || (holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1 && holders[4, y].getValue() == CT1) || (holders[0, y].getValue() == CT2 && holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2) || (holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2 && holders[4, y].getValue() == CT2))
{
return true;
}
break;
case 2:
if ((holders[0, y].getValue() == CT1 && holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1) || (holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1 && holders[4, y].getValue() == CT1) || (holders[0, y].getValue() == CT2 && holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2) || (holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2 && holders[4, y].getValue() == CT2))
{
return true;
}
break;
case 3:
if ((holders[0, y].getValue() == CT1 && holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1) || (holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1 && holders[4, y].getValue() == CT1) || (holders[0, y].getValue() == CT2 && holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2) || (holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2 && holders[4, y].getValue() == CT2))
{
return true;
}
break;
case 4:
if ((holders[0, y].getValue() == CT1 && holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1) || (holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1 && holders[4, y].getValue() == CT1) || (holders[0, y].getValue() == CT2 && holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2) || (holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2 && holders[4, y].getValue() == CT2))
{
return true;
}
break;
}
}
return isWon;
}
public bool detectDiagonal()
{
bool isWon = false;
//If statements for checking if counters are in diagonal.There are only 8 total possible diagonal wins therefore 8 if statments
//Diagonal from [0,0] to [3,3] for each counter
if ((holders[0, 0].getValue() == CT1 && holders[1, 1].getValue() == CT1 && holders[2, 2].getValue() == CT1 && holders[3, 3].getValue() == CT1) || (holders[0, 0].getValue() == CT2 && holders[1, 1].getValue() == CT2 && holders[2, 2].getValue() == CT2 && holders[3, 3].getValue() == CT2))
{
isWon = true;
}
//Diagonal from [0,1] to [3,4] for each counter
if ((holders[0, 1].getValue() == CT1 && holders[1, 2].getValue() == CT1 && holders[2, 3].getValue() == CT1 && holders[3, 4].getValue() == CT1) || (holders[0, 1].getValue() == CT2 && holders[1, 2].getValue() == CT2 && holders[2, 3].getValue() == CT2 && holders[3, 4].getValue() == CT2))
{
isWon = true;
}
//Diagonal from [0, 3] to [3, 0] for each counter
else if ((holders[0, 3].getValue() == CT1 && holders[1, 2].getValue() == CT1 && holders[2, 1].getValue() == CT1 && holders[3, 0].getValue() == CT1) || (holders[0, 3].getValue() == CT2 && holders[1, 2].getValue() == CT2 && holders[2, 1].getValue() == CT2 && holders[3, 0].getValue() == CT2))
{
isWon = true;
}
//Diagonal from [0, 4] to [3, 1] for each counter
else if ((holders[0, 4].getValue() == CT1 && holders[1, 3].getValue() == CT1 && holders[2, 2].getValue() == CT1 && holders[3, 1].getValue() == CT1) || (holders[0, 4].getValue() == CT2 && holders[1, 3].getValue() == CT2 && holders[2, 2].getValue() == CT2 && holders[3, 1].getValue() == CT2))
{
isWon = true;
}
//Diagonal from [1,0] to [4,3] for each counter
if ((holders[1, 0].getValue() == CT1 && holders[2, 1].getValue() == CT1 && holders[3, 2].getValue() == CT1 && holders[4, 3].getValue() == CT1) || (holders[1, 0].getValue() == CT2 && holders[2, 1].getValue() == CT2 && holders[3, 2].getValue() == CT2 && holders[4, 3].getValue() == CT2))
{
isWon = true;
}
//Diagonal from [1,1] to [4,4] for each counter
else if ((holders[1, 1].getValue() == CT1 && holders[2, 2].getValue() == CT1 && holders[3, 3].getValue() == CT1 && holders[4, 4].getValue() == CT1) || (holders[1, 1].getValue() == CT2 && holders[2, 2].getValue() == CT2 && holders[3, 3].getValue() == CT2 && holders[4, 4].getValue() == CT2))
{
isWon = true;
}
//Diagonal from [1, 3] to [4, 0] for each counter
else if ((holders[1, 3].getValue() == CT1 && holders[2, 2].getValue() == CT1 && holders[3, 1].getValue() == CT1 && holders[4, 0].getValue() == CT1) || (holders[1, 3].getValue() == CT2 && holders[2, 2].getValue() == CT2 && holders[3, 1].getValue() == CT2 && holders[4, 0].getValue() == CT2))
{
isWon = true;
}
//Diagonal from [1, 4] to [4, 1] for each counter
else if ((holders[1, 4].getValue() == CT1 && holders[2, 3].getValue() == CT1 && holders[3, 2].getValue() == CT1 && holders[4, 1].getValue() == CT1) || (holders[1, 4].getValue() == CT2 && holders[2, 3].getValue() == CT2 && holders[3, 2].getValue() == CT2 && holders[4, 1].getValue() == CT2))
{
isWon = true;
}
return isWon;
}
}
class Holder
{
private Point location;
private int value = Board.B;
public void setLocation(Point p)
{
location = p;
}
public Point getLocation()
{
return location;
}
public int getValue()
{
return value;
}
public void setValue(int i)
{
value = i;
}
}
}
希望有人能幫助!
查看MVP模式。應該可以幫到你 – user853710
你可以爲每一列使用一個bool值。列滿時,將該列的布爾值設置爲false。然後,當用戶嘗試添加到該列時,檢查布爾值並相應地採取行動。 – ProgrammingDude
好的會做@ user853710,謝謝ProgrammingDude – IamHe