所以这是一个井字游戏,其中按钮网格的大小通过在 App.config 中设置而改变。这是我的CheckXWinner
功能,如果 X 获胜,它会突出显示获胜的行组合并将按钮变为红色。我想添加一个动态线条图,该线条图将从获胜模式的开始绘制到结束(通过获胜组合的线)。在 MSDN 上,它说要添加 Microsoft.VisualBasic.PowerPacks.VS 的引用,using Microsoft.VisualBasic.PowerPacks;
然后以下是他们绘制线条的代码示例:
ShapeContainer canvas = new ShapeContainer();
LineShape theLine = new LineShape();
// Set the form as the parent of the ShapeContainer.
canvas.Parent = this;
// Set the ShapeContainer as the parent of the LineShape.
theLine.Parent = canvas;
// Set the starting and ending coordinates for the line.
theLine.StartPoint = new System.Drawing.Point(0, 0);
theLine.EndPoint = new System.Drawing.Point(640, 480);
这是我的CheckXWinner
函数代码:
public void CheckXWinner(Button[] buttonArray)
{
int arrLength = buttonArray.Length;
int root = (int)Math.Sqrt(Convert.ToDouble(arrLength));
bool winner = false;//variable to keep the computer from going when Xwins
for (int i = 0; i < root; i++)
{
//Sets the counter for the winners back to zero
int d2Count = 0;
int d1Count = 0;
int hCount = 0;
int vCount = 0;
for(int j = 0; j < root; j++)
{
//increments the appropriate counter if the button contains an X
//Horizonal win
if (buttonArray[(i*root) + j].Text == "X")
{
hCount++;
if (hCount == root)
{
for (int z = (root - 1); z >= 0; z--)
{
buttonArray[(i*root) + z].BackColor = Color.IndianRed;
}
Xwins();
winner = true; //sets winner to true so computer does not take turn
}
}//end of Horizonal win
//Left to right diagonal
if (buttonArray[j + (j*root)].Text == "X")
{
d1Count++;
if (d1Count == root)
{
for (int z = (root - 1); z >= 0; z--)
{
buttonArray[z + (z * root)].BackColor = Color.IndianRed;
}
Xwins();
winner = true;
}
}//end of LTR win
//Right to left diagonal
if (buttonArray[(j*(root - 1)) + (root - 1)].Text == "X")
{
d2Count++;
if (d2Count == root)
{
for (int z = (root - 1); z >= 0; z--)
{
buttonArray[(z*(root - 1)) + (root - 1)].BackColor = Color.IndianRed;
}
Xwins();
winner = true;
}
}//end of RTL win
//Vertical win
if (buttonArray[i + (root*j)].Text == "X")
{
vCount++;
if (vCount == root)
{
for (int z = (root - 1); z >= 0; z--)
{
buttonArray[i + (root*z)].BackColor = Color.IndianRed;
}
Xwins();
winner = true;
}
}//end of vert win
}//end of for j loop
}//end of for loop
CheckDraw();
if (winner == false)
{
ComputerGoes(buttonArray);
};
}//end of CheckXWinner
如何获取对我的表格的引用以获取线条图的代码?之后,我将把这些点称为循环中的获胜线,以将获胜线变为红色。谢谢!