求大神解答五子棋的题目
这个是C#的五子棋,怎么在里面再加一个东西,这个东西的作用是:随机时间在随机的位置,让这个位置上的棋子闪动三下,如果这个位置没有棋子,则自动产生一个棋子闪动三下然后消失。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplicationgobang
{
public partial class Form1 : Form
{
private static bool[,] isDraw = new bool;//二维数组用于检验当前坐标是否放置了棋子
private MouseEventArgs e;
private static int ncount=1;
//private static bool isBlackStoneWin=false;
//private static int N = 1;
private static bool isBlackStoneWin = false;
private static bool isWhiteStoneWin = false;
private static bool isBlueStoneWin = false;
//黑棋
private const int n = 32;
private bool isPlayBlackStone;
private static int nblackstone = 0;
private static int bcount = 0;
private BlackStone[] black=new BlackStone;
private static bool[,] isBlackStone = new bool;
//白棋
private bool isPlayWhiteStone;
private static int nwhitestone = 0;
private static int wcount = 0;
private WhiteStone[] white = new WhiteStone;
private static bool[,] isWhiteStone = new bool;
public Form1()
{
InitializeComponent();
InitBlackStone();
InitWhiteStone();
InitRedStone();
labelBlack.BackColor = Color.Red;
labelBlack.Text = "黑方下";
}
public void InitBlackStone()
{
isPlayBlackStone = false;
for (int i = 0; i < n; i++)
{
black = new BlackStone();
black.X = -10;
black.Y = -10;
}
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
isDraw = false;
isBlackStone = false;
}
}
}
public void InitWhiteStone()
{
isPlayWhiteStone = false;
for (int i = 0; i < n; i++)
{
white = new WhiteStone();
white.X = -10;
white.Y = -10;
}
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
isWhiteStone = false;
}
}
}
public void PlayBlackStone(MouseEventArgs e)
{
int x = panelGobang.ClientRectangle.X;
int y = panelGobang.ClientRectangle.Y;
int[] gobangX = new int;
int[] gobangY = new int;
for (int i = 0; i < 9; i++)
{
gobangX = x + (i + 1) * 40;
}
for (int i = 0; i < 9; i++)
{
gobangY = y + (i + 1) * 40;
}
for (int i = 0; i < 9; i++)
{
if ((e.X >= gobangX - 10) && (e.X <= gobangX + 10))
{
for (int j = 0; j < 9; j++)
{
if ((e.Y >= gobangY - 10) && (e.Y <= gobangY + 10))
{
black.X = gobangX;
black.Y = gobangY;
isPlayBlackStone = true;
break;
}
}
break;
}
}
}
public void PlayWhiteStone(MouseEventArgs e)
{
int x = panelGobang.ClientRectangle.X;
int y = panelGobang.ClientRectangle.Y;
int[] gobangX = new int;
int[] gobangY = new int;
for (int i = 0; i < 9; i++)
{
gobangX = x + (i + 1) * 40;
}
for (int i = 0; i < 9; i++)
{
gobangY = y + (i + 1) * 40;
}
for (int i = 0; i < 9; i++)
{
if ((e.X >= gobangX - 10) && (e.X <= gobangX + 10))
{
for (int j = 0; j < 9; j++)
{
if ((e.Y >= gobangY - 10) && (e.Y <= gobangY + 10))
{
white.X = gobangX;
white.Y = gobangY;
isPlayWhiteStone = true;
break;
}
}
break;
}
}
}
public bool IsBlackStoneWinSR()
{
int N = 1;
int x1 = black.X - 40;
int x2 = black.X + 40;
int y1 = black.Y + 40;
int y2 = black.Y - 40;
while (x1 >= 40 && y1 <= 360)
{
if (N == 5)
{
break;
}
if (isBlackStone != false)
{
N++;
x1 = x1 - 40;
y1 = y1 + 40;
}
else
{
break;
}
}
if (N < 5)
{
while (x2 <= 360 && y2 >= 40)
{
if (N == 5)
{
break;
}
if (isBlackStone)
{
N++;
x2 = x2 + 40;
y2 = y2 - 40;
}
else
{
break;
}
}
}
if (N == 5)
{
return true;
}
else
{
return false;
}
}
public bool IsBlackStoneWinSL()
{
int N = 1;
int x1 = black.X - 40;
int x2 = black.X + 40;
int y1 = black.Y - 40;
int y2 = black.Y + 40;
while (x1>=40&&y1 >= 40)
{
if (N == 5)
{
break;
}
if (isBlackStone != false)
{
N++;
x1 = x1 - 40;
y1 = y1 - 40;
}
else
{
break;
}
}
if (N < 5)
{
while (x2<=360&&y2 <= 360)
{
if (N == 5)
{
break;
}
if (isBlackStone)
{
N++;
x2 = x2 + 40;
y2 = y2 + 40;
}
else
{
break;
}
}
}
if (N == 5)
{
return true;
}
else
{
return IsBlackStoneWinSR();
}
}
public bool IsBlackStoneWinY()
{
int N = 1;
int y1 = black.Y - 40;
int y2 = black.Y + 40;
while (y1 >= 40)
{
if (N == 5)
{
break;
}
if (isBlackStone.X / 40 - 1, y1 / 40 - 1] != false)
{
N++;
y1 = y1 - 40;
}
else
{
break;
}
}
if (N < 5)
{
while (y2 <= 360)
{
if (N == 5)
{
break;
}
if (isBlackStone.X / 40 - 1, y2 / 40 - 1])
{
N++;
y2 = y2 + 40;
}
else
{
break;
}
}
}
if (N == 5)
{
return true;
}
else
{
return IsBlackStoneWinSL();
}
}
public bool IsBlackStoneWinX()
{
int N = 1;
int x1 = black.X - 40;
int x2 = black.X + 40;
while (x1 >= 40)
{
if (N == 5)
{
break;
}
if (isBlackStone.Y / 40 - 1] != false)
{
N++;
x1 = x1 - 40;
}
else
{
break;
}
}
if (N < 5)
{
while (x2 <= 360)
{
if (N == 5)
{
break;
}
if (isBlackStone.Y / 40 - 1])
{
N++;
x2 = x2 + 40;
}
else
{
break;
}
}
}
if (N == 5)
{
return true;
}
else
{
return IsBlackStoneWinY();
}
}
public bool IsWhiteStoneWinSR()
{
int N = 1;
int x1 =white.X - 40;
int x2 = white.X + 40;
int y1 = white.Y + 40;
int y2 = white.Y - 40;
while (x1 >= 40 && y1 <= 360)
{
if (N == 5)
{
break;
}
if (isWhiteStone != false)
{
N++;
x1 = x1 - 40;
y1 = y1 + 40;
}
else
{
break;
}
}
if (N < 5)
{
while (x2 <= 360 && y2 >= 40)
{
if (N == 5)
{
break;
}
if (isWhiteStone)
{
N++;
x2 = x2 + 40;
y2 = y2 - 40;
}
else
{
break;
}
}
}
if (N == 5)
{
return true;
}
else
{
return false;
}
}
public bool IsWhiteStoneWinSL()
{
int N = 1;
int x1 = white.X - 40;
int x2 = white.X + 40;
int y1 = white.Y - 40;
int y2 = white.Y + 40;
while (x1 >= 40 && y1 >= 40)
{
if (N == 5)
{
break;
}
if (isWhiteStone != false)
{
N++;
x1 = x1 - 40;
y1 = y1 - 40;
}
else
{
break;
}
}
if (N < 5)
{
while (x2 <= 360 && y2 <= 360)
{
if (N == 5)
{
break;
}
if (isWhiteStone)
{
N++;
x2 = x2 + 40;
y2 = y2 + 40;
}
else
{
break;
}
}
}
if (N == 5)
{
return true;
}
else
{
return IsWhiteStoneWinSR();
}
}
public bool IsWhiteStoneWinY()
{
int N = 1;
int y1 = white.Y - 40;
int y2 = white.Y + 40;
while (y1 >= 40)
{
if (N == 5)
{
break;
}
if (isWhiteStone.X / 40 - 1, y1 / 40 - 1] != false)
{
N++;
y1 = y1 - 40;
}
else
{
break;
}
}
if (N < 5)
{
while (y2 <= 360)
{
if (N == 5)
{
break;
}
if (isWhiteStone.X / 40 - 1, y2 / 40 - 1])
{
N++;
y2 = y2 + 40;
}
else
{
break;
}
}
}
if (N == 5)
{
return true;
}
else
{
return IsWhiteStoneWinSL();
}
}
public bool IsWhiteStoneWinX()
{
int N = 1;
int x1 = white.X - 40;
int x2 = white.X + 40;
while (x1 >= 40)
{
if (N == 5)
{
break;
}
if (isWhiteStone.Y / 40 - 1] != false)
{
N++;
x1 = x1 - 40;
}
else
{
break;
}
}
if (N < 5)
{
while (x2 <= 360)
{
if (N == 5)
{
break;
}
if (isWhiteStone.Y / 40 - 1])
{
N++;
x2 = x2 + 40;
}
else
{
break;
}
}
}
if (N == 5)
{
return true;
}
else
{
return IsWhiteStoneWinY();
}
}
public void RenewStone()
{
bcount = 0;
nblackstone = 0;
isPlayBlackStone = false;
for (int i = 0; i < n; i++)
{
//black = new BlackStone();
black.X = -10;
black.Y = -10;
//white = new WhiteStone();
white.X = -10;
white.Y = -10;
}
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
isDraw = false;
isBlackStone = false;
}
}
ncount = 1;
wcount = 0;
nwhitestone = 0;
isPlayWhiteStone = false;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
isWhiteStone = false;
}
}
}
private void panelGobang_Paint(object sender, PaintEventArgs e)
{
int x = panelGobang.ClientRectangle.X;
int y = panelGobang.ClientRectangle.Y;
for (int i = 0; i < 11; i++)
{
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), new Point(x, y + 40 * i), new Point(x + 400, y + 40 * i));
}
for (int i = 0; i < 11; i++)
{
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), new Point(x + 40 * i, y), new Point(x + 40 * i, y + 400));
}
for (int i = 0; i < nblackstone; i++)
{
black.DrawBlackStone(e.Graphics);
}
for (int i = 0; i < nwhitestone; i++)
{
white.DrawWhiteStone(e.Graphics);
}
if (ncount % 2 != 0)
{
if (isBlackStoneWin == true)
{
isBlackStoneWin = false;
labelBlack.Text = "黑方下";
}
if (isWhiteStoneWin == true)
{
isWhiteStoneWin = false;
labelBlack.Text="黑方下";
labelWhite.Text = "";
}
if (isPlayBlackStone)
{
if (!isDraw[(black.X) / 40 - 1, (black.Y) / 40 - 1])
{
black.DrawBlackStone(e.Graphics);
nblackstone = bcount + 1;
isPlayBlackStone = false;
isDraw[(black.X) / 40 - 1, (black.Y) / 40 - 1] = true;
isBlackStone[(black.X) / 40 - 1, (black.Y) / 40 - 1] = true;
if (IsBlackStoneWinX() == true)
{
MessageBox.Show("黑方获胜");
RenewStone();
isBlackStoneWin = true;
//labelBlack.Text = "";
//labelWhite.Text = "白方下";
panelGobang.Invalidate();
return;
}
bcount++;
ncount++;
labelWhite.BackColor = Color.Red;
labelWhite.Text = "白方下";
labelBlack.Text = "";
}
}
}
else
{
if (isPlayWhiteStone)
{
if (!isDraw[(white.X) / 40 - 1, (white.Y) / 40 - 1])
{
white.DrawWhiteStone(e.Graphics);
nwhitestone = wcount + 1;
isPlayWhiteStone = false;
isDraw[(white.X) / 40 - 1, (white.Y) / 40 - 1] = true;
isWhiteStone[(white.X) / 40 - 1, (white.Y) / 40 - 1] = true;
if (IsWhiteStoneWinX() == true)
{
MessageBox.Show("白方获胜");
RenewStone();
isWhiteStoneWin = true;
//labelBlack.Text = "";
//labelWhite.Text = "白方下";
panelGobang.Invalidate();
labelWhite.Text = "白方下";
return;
}
wcount++;
ncount++;
labelBlack.BackColor = Color.Red;
labelBlack.Text = "黑方下";
labelWhite.Text = "";
}
}
}
}
private void panelGobang_MouseClick(object sender, MouseEventArgs e)
{
if (ncount % 2 != 0)
{
if (bcount >= n)
{
if (wcount == n)
{
MessageBox.Show("您的棋子己用完!");
isPlayBlackStone = false;
return;
}
}
this.e = e;
PlayBlackStone(e);
panelGobang.Invalidate();
}
else
{
if (wcount >= n)
{
MessageBox.Show("您的棋子己用完!");
isPlayWhiteStone = false;
return;
}
this.e = e;
PlayWhiteStone(e);
panelGobang.Invalidate();
}
}
}
}
页:
[1]