在不考虑资源的问题 只完成输赢判断
总是在这一步达不到要求
请各位达达修改求解。代码和附件我都放上来了。。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Drawing.Imaging;namespace 五子棋
{
public partial class Form1 : Form
{
bool b = true;
int[] xzb = new int[100];
int[] yzb = new int[100];
int[] wzbx = new int[100];
int[] wzby = new int[100];
int[] bzbx = new int[100];
int[] bzby = new int[100];
int [,] pp=new int[516,516];
public Form1()
{
InitializeComponent();
} private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
}//鼠标按下的指令
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
int xx = e.X;
int yy = e.Y;
if (xx >= 20 && xx <= 515 && yy >= 20 && yy <= 515)
{
int xxx = xzuobiao(xx);
int yyy = yzuobiao(yy); drawImage(xxx, yyy);
}
}
//绘图和胜利的判断
private void drawImage(int x, int y)
{
Graphics g = this.CreateGraphics();
SolidBrush ww = new SolidBrush(Color.White);
SolidBrush bb = new SolidBrush(Color.Black);
if (b == true)
{
if (pp[x, y] == -1)
{
b = false;
pp[x, y] = 1;
g.FillEllipse(bb, x - 15, y - 15, 30, 30);
}
}
else
{
if (pp[x, y] == -1)
{
b = true;
pp[x, y] = 2;
g.FillEllipse(ww, x - 15, y - 15, 30, 30);
}
}
bool haha = win(pp);
MessageBox.Show(haha.ToString());
if (haha == true)
{
MessageBox.Show("黑色胜利");
} } private void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.Text = string.Format("{0},{1}", e.X, e.Y);
}
//更正X坐标
private int xzuobiao(int x)
{
for (int i = 0; i < 15; i++)
{
if (xzb[i] - 18 < x && xzb[i] + 18 > x)
{
x = xzb[i]; }
}
return x;
}
//更正Y坐标
private int yzuobiao(int y)
{
for (int i = 0; i < 15; i++)
{
if (yzb[i] - 18 < y && yzb[i] + 18 > y)
{
y = yzb[i]; }
}
return y;
}
//初始化给予坐标集
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 20, j = 0; i <= 515; i = i + 35, j++)
{
xzb[j] = i;
yzb[j] = i;
} for (int x = 20; x < 516; x=x+35)
{
for (int n = 20; n < 516; n=n+35)
{
pp[x, n] = -1;
}
}
}
//输赢 错误就在这里。。
private bool win(int[,] pp)
{
int a = 0;
int p = 35;
for (int i = 20; i < 516; i = i + p)
{
for (int j =i; j < 516; j = j + p)
{
if (pp[i, j] == pp[i, j + p])
{
a++;
if (a == 4)
return true;
}
else
{
a = 0;
} }
} return false;
} private bool bxwinn()
{
bool uwin = false; int a = 1; //判断是否寻找5个连续一样的 int p = 35; //增量
for (int i = 20; i < 516; i = i + p) //2维数组的循环
{
a = 1;
for (int j = 20; j < 516; j = j + p)
{ if (j + p < 516 && pp[i, j] == 1 && pp[i, j + p] == 1)
{
a++;
} }
}
if (a == 6)
{
uwin = true; //返回TRUE.。
return uwin;
}
else
{
return false; //如果没有5个连续一起并值相等的 就返回假
} }
//private bool wxwinn()
//{
// int a = 0;
// int p = 35; // for (int i = 20; i < 516; i = i + p)
// {
// for (int j = 20; j < 516; j = j + p)
// {
// if (pp[i, j] == pp[i, j + p])
// {
// a++;
// if (a == 5)
// return true;
// }
// else
// {
// a = 0;
// } // }
// } // return false;
//}
//private bool bywinn()
//{
// bool win = false;
// int x=1;
// int xx = 35;
// Array.Sort<int>(bzby);
// for (int i = 0; i < bzbx.Length - 4; i++)
// {
// x = 1;
// for (int j = i + 1; j < bzbx.Length && x < 5; j++)
// {
// if (bzbx[j] == bzbx[i] + xx * x)
// {
// x++;
// }
// }
// }
// if (x == 5)
// { // return true;
// }
// else
// {
// return false;
// }
//}
}
}
总是在这一步达不到要求
请各位达达修改求解。代码和附件我都放上来了。。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Drawing.Imaging;namespace 五子棋
{
public partial class Form1 : Form
{
bool b = true;
int[] xzb = new int[100];
int[] yzb = new int[100];
int[] wzbx = new int[100];
int[] wzby = new int[100];
int[] bzbx = new int[100];
int[] bzby = new int[100];
int [,] pp=new int[516,516];
public Form1()
{
InitializeComponent();
} private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
}//鼠标按下的指令
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
int xx = e.X;
int yy = e.Y;
if (xx >= 20 && xx <= 515 && yy >= 20 && yy <= 515)
{
int xxx = xzuobiao(xx);
int yyy = yzuobiao(yy); drawImage(xxx, yyy);
}
}
//绘图和胜利的判断
private void drawImage(int x, int y)
{
Graphics g = this.CreateGraphics();
SolidBrush ww = new SolidBrush(Color.White);
SolidBrush bb = new SolidBrush(Color.Black);
if (b == true)
{
if (pp[x, y] == -1)
{
b = false;
pp[x, y] = 1;
g.FillEllipse(bb, x - 15, y - 15, 30, 30);
}
}
else
{
if (pp[x, y] == -1)
{
b = true;
pp[x, y] = 2;
g.FillEllipse(ww, x - 15, y - 15, 30, 30);
}
}
bool haha = win(pp);
MessageBox.Show(haha.ToString());
if (haha == true)
{
MessageBox.Show("黑色胜利");
} } private void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.Text = string.Format("{0},{1}", e.X, e.Y);
}
//更正X坐标
private int xzuobiao(int x)
{
for (int i = 0; i < 15; i++)
{
if (xzb[i] - 18 < x && xzb[i] + 18 > x)
{
x = xzb[i]; }
}
return x;
}
//更正Y坐标
private int yzuobiao(int y)
{
for (int i = 0; i < 15; i++)
{
if (yzb[i] - 18 < y && yzb[i] + 18 > y)
{
y = yzb[i]; }
}
return y;
}
//初始化给予坐标集
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 20, j = 0; i <= 515; i = i + 35, j++)
{
xzb[j] = i;
yzb[j] = i;
} for (int x = 20; x < 516; x=x+35)
{
for (int n = 20; n < 516; n=n+35)
{
pp[x, n] = -1;
}
}
}
//输赢 错误就在这里。。
private bool win(int[,] pp)
{
int a = 0;
int p = 35;
for (int i = 20; i < 516; i = i + p)
{
for (int j =i; j < 516; j = j + p)
{
if (pp[i, j] == pp[i, j + p])
{
a++;
if (a == 4)
return true;
}
else
{
a = 0;
} }
} return false;
} private bool bxwinn()
{
bool uwin = false; int a = 1; //判断是否寻找5个连续一样的 int p = 35; //增量
for (int i = 20; i < 516; i = i + p) //2维数组的循环
{
a = 1;
for (int j = 20; j < 516; j = j + p)
{ if (j + p < 516 && pp[i, j] == 1 && pp[i, j + p] == 1)
{
a++;
} }
}
if (a == 6)
{
uwin = true; //返回TRUE.。
return uwin;
}
else
{
return false; //如果没有5个连续一起并值相等的 就返回假
} }
//private bool wxwinn()
//{
// int a = 0;
// int p = 35; // for (int i = 20; i < 516; i = i + p)
// {
// for (int j = 20; j < 516; j = j + p)
// {
// if (pp[i, j] == pp[i, j + p])
// {
// a++;
// if (a == 5)
// return true;
// }
// else
// {
// a = 0;
// } // }
// } // return false;
//}
//private bool bywinn()
//{
// bool win = false;
// int x=1;
// int xx = 35;
// Array.Sort<int>(bzby);
// for (int i = 0; i < bzbx.Length - 4; i++)
// {
// x = 1;
// for (int j = i + 1; j < bzbx.Length && x < 5; j++)
// {
// if (bzbx[j] == bzbx[i] + xx * x)
// {
// x++;
// }
// }
// }
// if (x == 5)
// { // return true;
// }
// else
// {
// return false;
// }
//}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Drawing.Imaging;namespace 五子棋
{
public partial class Form1 : Form
{
bool b = true;
int[] xzb = new int[100];
int[] yzb = new int[100];
int[] wzbx = new int[100];
int[] wzby = new int[100];
int[] bzbx = new int[100];
int[] bzby = new int[100];
int [,] pp=new int[516,516];
public Form1()
{
InitializeComponent();
} private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
int xx = e.X;
int yy = e.Y;
if (xx >= 20 && xx <= 515 && yy >= 20 && yy <= 515)
{
int xxx = xzuobiao(xx);
int yyy = yzuobiao(yy); drawImage(xxx, yyy);
}
} private void drawImage(int x, int y)
{
Graphics g = this.CreateGraphics();
SolidBrush ww = new SolidBrush(Color.White);
SolidBrush bb = new SolidBrush(Color.Black);
if (b == true)
{
if (pp[x, y] == -1)
{
b = false;
pp[x, y] = 1;
g.FillEllipse(bb, x - 15, y - 15, 30, 30);
}
}
else
{
if (pp[x, y] == -1)
{
b = true;
pp[x, y] = 2;
g.FillEllipse(ww, x - 15, y - 15, 30, 30);
}
}
int xw = ywin(pp);
int yw = xwin(pp);
int zxw = zxwin(pp);
if (xw==1||yw==1||zxw==1)
{
MessageBox.Show("黑棋胜利");
}
else if (xw== 2||yw==2||zxw==2)
{
MessageBox.Show("白棋胜利");
}
} private void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.Text = string.Format("{0},{1}", e.X, e.Y);
}
private int xzuobiao(int x)
{
for (int i = 0; i < 15; i++)
{
if (xzb[i] - 18 < x && xzb[i] + 18 > x)
{
x = xzb[i]; }
}
return x;
}
private int yzuobiao(int y)
{
for (int i = 0; i < 15; i++)
{
if (yzb[i] - 18 < y && yzb[i] + 18 > y)
{
y = yzb[i]; }
}
return y;
} private void Form1_Load(object sender, EventArgs e)
{
for (int i = 20, j = 0; i <= 515; i = i + 35, j++)
{
xzb[j] = i;
yzb[j] = i;
} for (int x = 20; x < 516; x=x+35)
{
for (int n = 20; n < 516; n=n+35)
{
pp[x, n] = -1;
}
}
}
private int ywin(int[,] pp)
{
int a = 0;
int p = 35; for (int i = 20; i < 516; i = i + p)
{
for (int j = 20; j < 516; j = j + p)
{
if (pp[i, j] == -1)
{
a = 0;
continue;
}
else if(pp[i,j]==1)
{
if (pp[i, j] == pp[i, j + p])
{
a++;
if (a == 4)
return 1;
}
else
{
a = 0;
}
}
else if (pp[i, j] == 2)
{
if (pp[i, j] == pp[i, j + p])
{
a++;
if (a == 4)
return 2;
}
else
{
a = 0;
}
}
}
} return 0;
}
private int xwin(int[,] pp)
{
int a = 0;
int p = 35; for (int i = 20; i < 516; i = i + p)
{
for (int j = 20; j < 516; j = j + p)
{
if (pp[j, i] == -1)
{
a = 0;
continue;
}
else if (pp[j, i] == 1)
{
if (pp[j, i] == pp[j + p, i])
{
a++;
if (a == 4)
return 1;
}
else
{
a = 0;
}
}
else if (pp[j, i] == 2)
{
if (pp[j, i] == pp[j+p, i])
{
a++;
if (a == 4)
return 2;
}
else
{
a = 0;
}
}
}
} return 0;
}
private int zxwin(int[,] pp)
{
int a = 0;
int p = 35; for (int i = 20; i < 516; i = i + p)
{
for (int j = 20; j < 516; j = j + p)
{
if (pp[j, i] == -1)
{
a = 0;
continue;
}
else if (pp[j, i] == 1)
{
if (pp[j, i] == pp[j + p, i+p])
{
a++;
if (a == 4)
return 1;
}
else
{
a = 0;
}
}
else if (pp[j, i] == 2)
{
if (pp[j, i] == pp[j + p, i+p])
{
a++;
if (a == 4)
return 2;
}
else
{
a = 0;
}
}
}
} return 0;
}
}
}
这是新的 已经判断出了 横和竖方向的输赢判定现在对斜的 有点逻辑问题。
private bool win(int x, int y, int[,] pp)
{
//x,y为最新落子点的坐标,检测开始前,需要先将该点填充上棋子
//way=0和8,检测斜线1,way=1和7,检测横向,way=2和6检测斜线2,way=3和5,检测纵向
for(int i = 0;i < 4;i++)
if (countSameColor(x, y, pp, i) + countSameColor(x, y, pp, 8 - i) >= 4)
return true; return false;
} private int countSameColor(int x, int y, int[,] pp, int way)
{
//way为0-8,不包括4,检测方向
if (way == 4)
return 0; //棋盘大小和
int p = 35;
int lower = 20;
int upper = 516;
int xi;
int yi; xi = ((way / 3) - 1) * p;
yi = ((way % 3) - 1) * p; //获取当前落子的颜色
int color = pp[x, y];
int count = 0; while (x >= lower && y >= lower && x < upper && y < upper)
{
x += xi;
y += yi;
if (color == pp[x, y])
count++;
else
break;
} return count;
}