基于c语言的五子棋源码(c语言五子棋源代码)
本文目录一览:
c语言的五子棋代码(博弈算法)
#include stdio.h
#include bios.h
#include ctype.h
#include conio.h
#include dos.h
#define CROSSRU 0xbf /*右上角点*/
#define CROSSLU 0xda /*左上角点*/
#define CROSSLD 0xc0 /*左下角点*/
#define CROSSRD 0xd9 /*右下角点*/
#define CROSSL 0xc3 /*左边*/
#define CROSSR 0xb4 /*右边*/
#define CROSSU 0xc2 /*上边*/
#define CROSSD 0xc1 /*下边*/
#define CROSS 0xc5 /*十字交叉点*/
/*定义棋盘左上角点在屏幕上的位置*/
#define MAPXOFT 5
#define MAPYOFT 2
/*定义1号玩家的操作键键码*/
#define PLAY1UP 0x1157/*上移--'W'*/
#define PLAY1DOWN 0x1f53/*下移--'S'*/
#define PLAY1LEFT 0x1e41/*左移--'A'*/
#define PLAY1RIGHT 0x2044/*右移--'D'*/
#define PLAY1DO 0x3920/*落子--空格键*/
/*定义2号玩家的操作键键码*/
#define PLAY2UP 0x4800/*上移--方向键up*/
#define PLAY2DOWN 0x5000/*下移--方向键down*/
#define PLAY2LEFT 0x4b00/*左移--方向键left*/
#define PLAY2RIGHT 0x4d00/*右移--方向键right*/
#define PLAY2DO 0x1c0d/*落子--回车键Enter*/
/*若想在游戏中途退出, 可按 Esc 键*/
#define ESCAPE 0x011b
/*定义棋盘上交叉点的状态, 即该点有无棋子 */
/*若有棋子, 还应能指出是哪个玩家的棋子 */
#define CHESSNULL 0 /*没有棋子*/
#define CHESS1 'O'/*一号玩家的棋子*/
#define CHESS2 'X'/*二号玩家的棋子*/
/*定义按键类别*/
#define KEYEX99v 0/*退出键*/
#define KEYFALLCHESS 1/*落子键*/
#define KEYMOVECURSOR 2/*光标移动键*/
#define KEYINVALID 3/*无效键*/
/*定义符号常量: 真, 假 --- 真为1, 假为0 */
#define TRUE 1
#define FALSE 0
/* 定义数据结构 */
/*棋盘交叉点坐标的数据结构*/
struct point
int x,y;
或者下面这个:
#include graphics.h
#include stdlib.h
#include stdio.h
#include conio.h
#define N 15
#define B 7
#define STOP -10000
#define OK 1
#define NO 0
#define UP 328
#define DOWN 336
#define LEFT 331
#define RIGHT 333
int a[N+1][N+1];
int zx,zy;
int write=1,biaoji=0;
struct zn{
long sum;
int y;
int x;
}w[N+1][N+1],max,max1;
void cbar(int i,int x,int y,int r);
void map(int a[][]);
int getkey();
int key();
void zuobiao(int x,int y,int i);
int tu(int a[][],int write);
int wtu(int a[][],int write);
int zhineng(int a[][]);
int zh5(int y,int x,int a[][]);
long zzh5(int b[][],int i);
main()
int i,j;
int gdriver=DETECT;
int gmode;
initgraph(gdriver,gmode,"");
zx=(N+1)/2;
zy=(N+1)/2;
for(i=1;i=N;i++)
for(j=1;j=N;j++)
a[i][j]=0;
map(a);
i=1;
while(i)
int k,n;
k=wtu(a,write);
if(k==STOP) goto end;
map(a);
n=zhineng(a);
if(n==STOP) goto end;
map(a);
end:
int zhineng(int a[N+1][N+1])
int i,j;
int k;
max.sum=-1;
for(i=0;i=N;i++)
for(j=0;j+N;j++)
w[i][j].sum=0;
w[i][j].x=i;
w[i][j].y=j;
for(i=1;i=N-4;i++)
for(j=1;j=N-4;j++)
k=zh5(i,j,a);
if(k==STOP) return (STOP);
for(i=1;i=N;i++)
for(j=1;j=N;j++)
if(max.sumw[i][j].sum)
max.sum=w[i][j].sum;
max.y=i;
max.x=j;
else if(max.sum==w[i][j].sum)
if(((max.y-zy)*(max.y-zy)+(max.x-zx)*(max.x-zx))((i-zy)*(i-zy)+(j-zx)*(j-zx)))
max.sum=w[i][j].sum;
max.y=i;
max.x=j;
if(a[max.y][max.x]==0)
a[max.y][max.x]=-1;
zy=max.y;
zx=max.x;
int zh5(int y,int x,int a[N+1][N+1])
int i,j;
int b[6][6];
long c[13];
long d[6][6];
long temp;
for(i=y;i=y+4;i++)
for(j=x;j=x+4;j++)
b[i+1-y][j+1-x]=a[i][j];
c[1]=b[1][1]+b[1][2]+b[1][3]+b[1][4]+b[1][5];
c[2]=b[2][1]+b[2][2]+b[2][3]+b[2][4]+b[2][5];
c[3]=b[3][1]+b[3][2]+b[3][3]+b[3][4]+b[3][5];
c[4]=b[4][1]+b[4][2]+b[4][3]+b[4][4]+b[4][5];
c[5]=b[5][1]+b[5][2]+b[5][3]+b[5][4]+b[5][5];
c[6]=b[1][1]+b[2][1]+b[3][1]+b[4][1]+b[5][1];
c[7]=b[1][2]+b[2][2]+b[3][2]+b[4][2]+b[5][2];
c[8]=b[1][3]+b[2][3]+b[3][3]+b[4][3]+b[5][3];
c[9]=b[1][4]+b[2][4]+b[3][4]+b[4][4]+b[5][4];
c[10]=b[1][5]+b[2][5]+b[3][5]+b[4][5]+b[5][5];
c[11]=b[1][1]+b[2][2]+b[3][3]+b[4][4]+b[5][5];
c[12]=b[1][5]+b[2][4]+b[3][3]+b[4][2]+b[5][1];
for(i=1;i=12;i++)
switch(c[i])
case 5:biaoji=1;return(STOP);
case -5:biaoji=-1;return(STOP);
case -4:c[i]=100000;break;
case 4:c[i]=100000;break;
case -3:c[i]=150;break;
case 3:c[i]=150;break;
case -2:c[i]=120;break;
case 2:c[i]=100;break;
case -1:c[i]=1;break;
case 1:c[i]=1;break;
default: c[i]=0;
for(i=1;i=12;i++)
if(c[i]==150)
c[i]+=zzh5(b,i);
for(i=1;i=5;i++)
for(j=1;j=5;j++)
d[i][j]=0;
for(i=1;i=5;i++)
for(j=1;j=5;j++)
if(i==j) d[i][j]+=c[11];
if((i+j)==6) d[i][j]+=c[12];
d[i][j]+=c[i]+c[j+5];
for(i=1;i=5;i++)
for(j=1;j=5;j++)
if(b[i][j]!=0)
d[i][j]=-2;
max1.sum=-1;
max1.y=0;
max1.x=0;
for(i=1;i=5;i++)
for(j=1;j=5;j++)
if(max1.sumd[i][j])
max1.sum=d[i][j];
max1.y=i;
max1.x=j;
w[i+y-1][j+x-1].sum+=max1.sum;
else if(max1.sum==d[i][j])
if(((i+y-1-zy)*(i+y-1-zy)+(j+x-1-zx)*(j+x-1-zx))((max1.y+y-1-zy)*(max1.y+y-1-zy)+(max1.x+x-1-zx)*(max1.x+x-1-zx)))
max1.sum=d[i][j];
max1.y=i;
max1.x=j;
long zzh5(int b[6][6],int n)
int i,j,k,l,m;
switch(n)
case 1:i=b[1][1];j=b[1][2];k=b[1][3];l=b[1][4];m=b[1][5];break;
case 2:i=b[2][1];j=b[2][2];k=b[2][3];l=b[2][4];m=b[2][5];break;
case 3:i=b[3][1];j=b[3][2];k=b[3][3];l=b[3][4];m=b[3][5];break;
case 4:i=b[4][1];j=b[4][2];k=b[4][3];l=b[4][4];m=b[4][5];break;
case 5:i=b[5][1];j=b[5][2];k=b[5][3];l=b[5][4];m=b[5][5];break;
case 6:i=b[1][1];j=b[2][1];k=b[3][1];l=b[4][1];m=b[5][1];break;
case 7:i=b[1][2];j=b[2][2];k=b[3][2];l=b[4][2];m=b[5][2];break;
case 8:i=b[1][3];j=b[2][3];k=b[3][3];l=b[4][3];m=b[5][3];break;
case 9:i=b[1][4];j=b[2][4];k=b[3][4];l=b[4][4];m=b[5][4];break;
case 10:i=b[1][5];j=b[2][5];k=b[3][5];l=b[4][5];m=b[5][5];break;
case 11:i=b[1][1];j=b[2][2];k=b[3][3];l=b[4][4];m=b[5][5];break;
case 12:i=b[1][5];j=b[2][4];k=b[3][3];l=b[4][2];m=b[5][1];break;
if((i==0j==1k==1l==1m==0))
return (900);
if((i==0j==-1k==-1l==-1m==0))
return(1000);
if((i==0j==0k==1l==1m==1)||(i==1j==1k==1l==0m==0))
return(20);
if((i==0j==0k==-1l==-1m==-1)||(i==-1j==-1k==-1l==0m==0))
return(20);
if((i==-1j==1k==1l==1m==1)||(i==1j==-1k==1l==1m==1)||(i==1j==1k==-1l==1m==1)||(i==1j==1k==1l==-1m==1)||(i==1j==1k==1l==1m==-1))
return(-60);
if((i==1j==-1k==-1l==-1m==-1)||(i==-1j==1k==-1l==-1m==-1)||(i==-1j==1k==-1l==-1m==-1)||(i==-1j==-1k==-1l==1m==-1)||(i==-1j==-1k==-1l==-1m==1))
return(-60);
int wtu(int a[N+1][N+1],int write)
int i=1;
map(a);
zuobiao(zx,zy,1);
while(i)
int k;
k=tu(a,write);
if(k==OK) i=0;
if(k==STOP) return (STOP);
int getkey()
int key,lo,hi;
key=bioskey(0);
lo=key0x00ff;
hi=(key0xff00)8;
return((lo==0) ? hi+256:lo);
int key()
int k;
k=getkey();
switch(k)
case 27: return (STOP);
case 13:
case ' ': return (OK);
case 328: return (UP);
case 336: return (DOWN);
case 331: return (LEFT);
case 333: return (RIGHT);
default: return (NO);
void zuobiao(int x,int y,int i)
int r;
if(i!=0)
setcolor(GREEN);
for(r=1;r=5;r++)
circle(75+25*x,25+25*y,r);
else
if(a[zy][zx]==1)
setcolor(8);
for(r=1;r=5;r++)
circle(75+25*x,25+25*y,r);
else if(a[zy][zx]==-1)
setcolor(WHITE);
for(r=1;r=5;r++)
circle(75+25*x,25+25*y,r);
else
setcolor(B);
for(r=1;r=5;r++)
circle(75+25*x,25+25*y,r);
setcolor(RED); line(75+25*zx-5,25+25*zy,75+25*x+5,25+25*zy);
line(75+25*zx,25+25*zy-5,75+25*zx,25+25*zy+5);
int tu(int a[N+1][N+1],int write)
int k;
re:
k=key();
if(k==OK)
if(a[zy][zx]==0)
a[zy][zx]=write;
else
goto re;
if(k==STOP) return(STOP);
if(k==NO) goto re;
if(k==UP)
int i,j;
if(zy==1) j=zy;
else j=zy-1;
zuobiao(zx,zy,0);
zuobiao(zx,j,1);
zy=j;
goto re;
if(k==DOWN)
int i,j;
if(zy==N) j=zy;
else j=zy+1;
zuobiao(zx,zy,0);
zuobiao(zx,j,1);
zy=j;
goto re;
if(k==LEFT)
int i,j;
if(zx==1) i=zx;
else i=zx-1;
zuobiao(zx,zy,0);
zuobiao(i,zy,1);
zx=i;
goto re;
if(k==RIGHT)
int i,j;
if(zx==N) i=zx;
else i=zx+1;
zuobiao(zx,zy,0);
zuobiao(i,zy,1);
zx=i;
goto re;
void cbar(int i,int x,int y,int r)
if(i!=0)
if(i==1)
setcolor(8);
else if(i==-1)
setcolor(WHITE);
for(i=1;i=r;i++)
circle(x,y,i);
void map(int a[N+1][N+1])
int i,j;
cleardevice();
setbkcolor(B);
setcolor(RED);
for(i=0;iN;i++)
line(100,50+25*i,75+N*25,50+25*i);
line(100+25*i,50,100+25*i,25+N*25);
for(i=1;i=N;i++)
for(j=1;j=N;j++)
cbar(a[i][j],75+25*j,25+25*i,10);
c语言五子棋代码,
package day17.gobang;
import java.util.Arrays;
public class GoBangGame {
public static final char BLANK='*';
public static final char BLACK='@';
public static final char WHITE='O';
public static final int MAX = 16;
private static final int COUNT = 5;
//棋盘
private char[][] board;
public GoBangGame() {
//开始游戏
public void start() {
board = new char[MAX][MAX];
//把二维数组都填充‘*’
for(char[] ary: board){
Arrays.fill(ary, BLANK);
public char[][] getChessBoard(){
return board;
public void addBlack(int x, int y) throws ChessExistException{
//char blank = '*';
//System.out.println( x +"," + y + ":" + board[y][x] + "," + BLANK);
if(board[y][x] == BLANK){// x, y 位置上必须是空的才可以添棋子
board[y][x] = BLACK;
return;
throw new ChessExistException("已经有棋子了!");
public void addWhite(int x, int y)
throws ChessExistException{
if(board[y][x] == BLANK){// x, y 位置上必须是空的才可以添棋子
board[y][x] = WHITE;
return;
throw new ChessExistException("已经有棋子了!");
//chess 棋子:'@'/'O'
public boolean winOnY(char chess, int x, int y){
//先找到y方向第一个不是 blank的棋子
int top = y;
while(true){
if(y==0 || board[y-1][x]!=chess){
//如果y已经是棋盘的边缘, 或者的前一个不是chess
//就不再继续查找了
break;
y--;
top = y;
//向回统计所有chess的个数,如果是COUNT个就赢了
int count = 0;
y = top;
while(true){
if(y==MAX || board[y][x]!=chess){
//如果找到头 或者 下一个子不是chess 就不再继续统计了
break;
count++;
y++;
return count==COUNT;
//chess 棋子:'@'/'O'
public boolean winOnX(char chess, int x, int y){
//先找到x方向第一个不是 blank的棋子
int top = x;
while(true){
if(x==0 || board[y][x-1]!=chess){
//如果x已经是棋盘的边缘, 或者的前一个不是chess
//就不再继续查找了
break;
x--;
top = x;
//向回统计所有chess的个数,如果是COUNT个就赢了
int count = 0;
x = top;
while(true){
if(x==MAX || board[y][x]!=chess){
//如果找到头 或者 下一个子不是chess 就不再继续统计了
break;
count++;
x++;
return count==COUNT;
//chess 棋子:'@'/'O'
public boolean winOnXY(char chess, int x, int y){
//先找MAX向第一个不是 blank的棋子
int top = y;
int left = x;
while(true){
if(x==0 || y==0 || board[y-1][x-1]!=chess){
//如果x已经是棋盘的边缘, 或者的前一个不是chess
//就不再继续查找了
break;
x--;
y--;
top = y;
left=x;
//向回统计所有chess的个数,如果是COUNT个就赢了
int count = 0;
x = left;
y = top;
while(true){
if(x==MAX || y==MAX || board[y][x]!=chess){
//如果找到头 或者 下一个子不是chess 就不再继续统计了
break;
count++;
x++;
y++;
return count==COUNT;
//chess 棋子:'@'/'O'
public boolean winOnYX(char chess, int x, int y){
//先找到x方向第一个不是 blank的棋子
int top = y;
int left = x;
while(true){
if(x==MAX-1 || y==0 || board[y-1][x+1]!=chess){
//如果x已经是棋盘的边缘, 或者的前一个不是chess
//就不再继续查找了
break;
x++;
y--;
top = y;
left=x;
//向回统计所有chess的个数,如果是COUNT个就赢了
int count = 0;
x = left;
y = top;
while(true){
if(x==0 || y==MAX || board[y][x]!=chess){
//如果找到头 或者 下一个子不是chess 就不再继续统计了
break;
count++;
x--;
y++;
return count==COUNT;
public boolean whiteIsWin(int x, int y) {
//在任何一个方向上赢了,都算赢
return winOnY(WHITE, x, y) ||
winOnX(WHITE, x, y) ||
winOnXY(WHITE, x, y) ||
winOnYX(WHITE, x, y);
public boolean blackIsWin(int x, int y) {
return winOnY(BLACK, x, y) ||
winOnX(BLACK, x, y) ||
winOnXY(BLACK, x, y) ||
winOnYX(BLACK, x, y);
C语言设计出的鼠标操作的五子棋源代码
#include stdlib.h
#include stdio.h
#include conio.h
#include string.h
#define MAXIMUS 15 //定义棋盘大小
int p[MAXIMUS][MAXIMUS];//存储对局信息
char buff[MAXIMUS*2+1][MAXIMUS*4+3];//输出缓冲器
int Cx,Cy;//当前光标位置
int Now;//当前走子的玩家,1代表黑,2代表白
int wl,wp;//当前写入缓冲器的列数和行数位置
char* showText;//在棋盘中央显示的文字信息
int count;//回合数
char* Copy(char* strDest,const char* strSrc)//修改过的字符串复制函数,会忽略末端的\0
char* strDestCopy = strDest;
while (*strSrc!='\0')
*strDest++=*strSrc++;
return strDestCopy;
void Initialize()//初始化一个对局函数
int i,j;//循环变量
showText="";//重置显示信息
count=0;//回合数归零
for(i=0;iMAXIMUS;i++)//重置对局数据
for(j=0;jMAXIMUS;j++)
p[i][j]=0;
Cx=Cy=MAXIMUS/2;//重置光标到中央
Now=1;//重置当前为黑方
char* getStyle(int i,int j)//获得棋盘中指定坐标交点位置的字符,通过制表符拼成棋盘
if(p[i][j]==1)//1为黑子
return "●";
else if(p[i][j]==2)//2为白子
return "○";
else if(i==0j==0)//以下为边缘棋盘样式
return "┏";
else if(i==MAXIMUS-1j==0)
return "┓";
else if(i==MAXIMUS-1j==MAXIMUS-1)
return "┛";
else if(i==0j==MAXIMUS-1)
return "┗";
else if(i==0)
return "┠";
else if(i==MAXIMUS-1)
return "┨";
else if(j==0)
return "┯";
else if(j==MAXIMUS-1)
return "┷";
return "┼";//中间的空位
char* getCurse(int i,int j){//获得指定坐标交点位置左上格的样式,通过制表符来模拟光标的显示
if(i==Cx){
if(j==Cy)
return "┏";
else if (j==Cy+1)
return "┗";
else if(i==Cx+1)
if(j==Cy)
return "┓";
else if (j==Cy+1)
return "┛";
return " ";//如果不在光标附近则为空
void write(char* c)//向缓冲器写入字符串
Copy(buff[wl]+wp,c);
wp+=strlen(c);
void ln()//缓冲器写入位置提行
wl+=1;
wp=0;
void Display()//将缓冲器内容输出到屏幕
int i,l=strlen(showText);//循环变量,中间文字信息的长度
int Offset=MAXIMUS*2+2-l/2;//算出中间文字信息居中显示所在的横坐标位置
if(Offset%2==1)//如果位置为奇数,则移动到偶数,避免混乱
Offset--;
Copy(buff[MAXIMUS]+Offset,showText);//讲中间文字信息复制到缓冲器
if(l%2==1)//如果中间文字长度为半角奇数,则补上空格,避免混乱
*(buff[MAXIMUS]+Offset+l)=0x20;
system("cls");//清理屏幕,准备写入
for(i=0;iMAXIMUS*2+1;i++){//循环写入每一行
printf("%s",buff[i]);
if(iMAXIMUS*2)//写入完每一行需要换行
printf("\n");
void Print()//将整个棋盘算出并储存到缓冲器,然后调用Display函数显示出来
int i,j;//循环变量
wl=0;
wp=0;
for(j=0;j=MAXIMUS;j++)//写入出交点左上角的字符,因为需要打印棋盘右下角,所以很以横纵各多一次循环
for(i=0;i=MAXIMUS;i++)
write(getCurse(i,j));//写入左上角字符
if(j==0||j==MAXIMUS)//如果是棋上下盘边缘则没有连接的竖线,用空格填充位置
if(i!=MAXIMUS)
write(" ");
else//如果在棋盘中间则用竖线承接上下
if(i==0||i==MAXIMUS-1)//左右边缘的竖线更粗
write("┃");
else if(i!=MAXIMUS)//中间的竖线
write("│");
if(j==MAXIMUS)//如果是最后一次循环,则只需要处理边侧字符,交点要少一排
break;
ln();//提行开始打印交点内容
write(" ");//用空位补齐位置
for(i=0;iMAXIMUS;i++)//按横坐标循环正常的次数
write(getStyle(i,j));//写入交点字符
if(i!=MAXIMUS-1)//如果不在最右侧则补充一个横线承接左右
if(j==0||j==MAXIMUS-1)
write("━");//上下边缘的横线更粗
else
write("—");//中间的横线
ln();//写完一行后提行
Display();//将缓冲器内容输出到屏幕
int Put(){//在当前光标位置走子,如果非空,则返回0表示失败
if(p[Cx][Cy]==0)
p[Cx][Cy]=Now;//改变该位置数据
return 1;//返回1表示成功
else
return 0;
int Check()//胜负检查,即判断当前走子位置有没有造成五连珠的情况
int w=1,x=1,y=1,z=1,i;//累计横竖正斜反邪四个方向的连续相同棋子数目
for(i=1;i5;i++)if(Cy+iMAXIMUSp[Cx][Cy+i]==Now)w++;else break;//向下检查
for(i=1;i5;i++)if(Cy-i0p[Cx][Cy-i]==Now)w++;else break;//向上检查
if(w=5)return Now;//若果达到5个则判断当前走子玩家为赢家
for(i=1;i5;i++)if(Cx+iMAXIMUSp[Cx+i][Cy]==Now)x++;else break;//向右检查
for(i=1;i5;i++)if(Cx-i0p[Cx-i][Cy]==Now)x++;else break;//向左检查
if(x=5)return Now;//若果达到5个则判断当前走子玩家为赢家
for(i=1;i5;i++)if(Cx+iMAXIMUSCy+iMAXIMUSp[Cx+i][Cy+i]==Now)y++;else break;//向右下检查
for(i=1;i5;i++)if(Cx-i0Cy-i0p[Cx-i][Cy-i]==Now)y++;else break;//向左上检查
if(y=5)return Now;//若果达到5个则判断当前走子玩家为赢家
for(i=1;i5;i++)if(Cx+iMAXIMUSCy-i0p[Cx+i][Cy-i]==Now)z++;else break;//向右上检查
for(i=1;i5;i++)if(Cx-i0Cy+iMAXIMUSp[Cx-i][Cy+i]==Now)z++;else break;//向左下检查
if(z=5)return Now;//若果达到5个则判断当前走子玩家为赢家
return 0;//若没有检查到五连珠,则返回0表示还没有玩家达成胜利
int RunGame()//进行整个对局,返回赢家信息(虽然有用上)
int input;//输入变量
int victor;//赢家信息
Initialize();//初始化对局
while(1){//开始无限回合的死循环,直到出现胜利跳出
Print();//打印棋盘
input=getch();//等待键盘按下一个字符
if(input==27)//如果是ESC则退出程序
exit(0);
else if(input==0x20)//如果是空格则开始走子
if(Put())//如果走子成功则判断胜负
victor=Check();
Now=3-Now;//轮换当前走子玩家
count++;
if(victor==1)//如果黑方达到胜利,显示提示文字并等待一次按键,返回胜利信息
showText="黑方获得了胜利!";
Print();
if(getch()==0xE0)
getch();
return Now;
else if(victor==2)//如果白方达到胜利,显示提示文字并等待一次按键,返回胜利信息
showText="白方获得了胜利!";
Display();
if(getch()==0xE0)
getch();
return Now;
}else if(count==MAXIMUS*MAXIMUS)//如果回合数达到了棋盘总量,即棋盘充满,即为平局
showText="平局!";
Display();
if(getch()==0xE0)
getch();
return 0;
else if(input==0xE0)//如果按下的是方向键,会填充两次输入,第一次为0xE0表示按下的是控制键
input=getch();//获得第二次输入信息
switch(input)//判断方向键方向并移动光标位置
case 0x4B://
Cx--;
break;
case 0x48:
Cy--;
break;
case 0x4D:
Cx++;
break;
case 0x50:
Cy++;
break;
if(Cx0)Cx=MAXIMUS-1;//如果光标位置越界则移动到对侧
if(Cy0)Cy=MAXIMUS-1;
if(CxMAXIMUS-1)Cx=0;
if(CyMAXIMUS-1)Cy=0;
int main()//主函数
system("title 简易五子棋 ——Etsnarl制作");//设置标题
system("mode con cols=63 lines=32");//设置窗口大小
system("color E0");//设置颜色
while(1){//循环执行游戏
RunGame();
求一份C语言的五子棋游戏源代码 一定要正确能运行的 Turbo C和VC环境都可以
#includeiostream
using namespace std;
int Hsheng(char a[][15]);//判断o子是否获胜的函数
int Bsheng(char a[][15]);//判断x子是否获胜的函数
int he(char a[][15]);//判断是否平局(也就是棋盘下满了)的函数
void qipan(char a[15][15])//执行输出棋盘命令
for(int i=0;i15;i++) //打印棋盘
for(int j=0;j15;j++)
couta[i][j];
coutendl;
int main()
char a[15][15];
int x,y;
for(int i=0;i15;i++)
for(int j=0;j15;j++)
a[i][j]=' ';
qipan(a);
while(1)//用循环语句执行o,x交替下子,这些while语句看起来似乎是个死循环~实际上都会经过break结束
int a1=1;
while(1)
{ for(;a1;)
{ cout"请输入o子下的位置:"; //输入o子的位置
cinxy; if(a[x][y]=='o'||a[x][y]=='x')//判断是否已有子
{cout"已有子请重下"",";continue;}
else if(x=15||y=15){cout"输入错误请重输"",";continue;}
else { a[x][y]='o'; a1=0;}
break;}
qipan(a);//下好o子后将棋盘显示
if(Hsheng(a))//判断o子是否已经获胜
cout"o子获胜"endl;
break;
while(1)//下x子
cout"请输入x子下的位置:";
cinxy;
if(a[x][y]=='o'||a[x][y]=='x'||x=15||y=15)
for( ; a[x][y]=='o'||a[x][y]=='x'; )
cout"已有子请重下";
cout"请输入x子下的位置:";
cinxy;continue; }
for ( ; x=15||y=15||x; )
{ cout"输入错误请重输"","; //判断输入棋子位置是否正确
cout"请输入x子下的位置:";
cinxy;continue ;}
a[x][y]='x';break;
else
{ a[x][y]='x'; break;
qipan(a);//再一次输出棋盘
if(Bsheng(a))//判断x子是否已经获胜
cout"x子获胜"endl;
break;
if(he(a))//判断是否平局
cout"平局"endl;
break;
return 0;
int Hsheng(char a[][15])
int i,j;//判断横着的5个是否都相等
for(i=0;i15;i++)
for(j=0;j15;j++)
if(a[i][j]=='o'a[i][j+1]=='o'a[i][j+2]=='o'a[i][j+3]=='o'a[i][j+4]=='o')
return 1;
for(j=0;j15;j++)//判断竖着的5个是否都相等
for(i=0;i15;i++)
if(a[i][j]=='o'a[i+1][j]=='o'a[i+2][j]=='o'a[i+3][j]=='o'a[i+4][j]=='o')
return 1;
for(i=0;i15;i++)//判断左斜5个
for(j=0;j15;j++)
if(a[i][j]=='o'a[i+1][j+1]=='o'a[i+2][j+2]=='o'a[i+3][j+3]=='o'a[i+4][j+4]=='o')
return 1;
for(i=0;i15;i++)//右斜5个
for(j=14;j3;j--)
if(a[i][j]=='H'a[i+1][j-1]=='o'a[i+2][j-2]=='o'a[i+3][j-3]=='o'a[i+4][j-4]=='o')
return 1;
return 0;
int Bsheng(char a[][15])//同o,只是改字符
int i,j;
for(i=0;i15;i++)
for(j=0;j15;j++)
if(a[i][j]=='x'a[i][j+1]=='x'a[i][j+2]=='x'a[i][j+3]=='x'a[i][j+4]=='x')
return 1;
for(j=0;j15;j++)
for(i=0;i15;i++)
if(a[i][j]=='x'a[i+1][j]=='x'a[i+2][j]=='x'a[i+3][j]=='x'a[i+4][j]=='x')
return 1;
for(i=0;i15;i++)
for(j=0;j15;j++)
if(a[i][j]=='x'a[i+1][j+1]=='x'a[i+2][j+2]=='x'a[i+3][j+3]=='x'a[i+4][j+4]=='x')
return 1;
for(i=0;i15;i++)
for(j=14;j3;j--)
if(a[i][j]=='x'a[i+1][j-1]=='x'a[i+2][j-2]=='x'a[i+3][j-3]=='x'a[i+4][j-4]=='x')
return 1;
return 0;
int he(char a[][15])
for(int i=0;i15;i++)
for(int j=0;j15;j++)
if(a[i][j]==' ')//当棋盘全部子都不是' '时才能return 1,即棋盘已下满
return 0;
return 1;
五子棋C语言代码,在VC++6.0环境下运行谢谢了!
本程序设计为人与人对弈,双方有一方五子连成一线即为赢。设计一游戏变量(3到9之间),用来控制显示面板的大小,即用户可选择生成3×3到9×9的棋盘。
五子棋C语言代码如下:#include stdio.h#include bios.h#include ctype.h#include conio.h#include dos.h#define CROSSRU 0xbf /*右上角点*/#define CROSSLU 0xda /*左上角点*/#define CROSSLD 0xc0 /*左下角点*/#define CROSSRD 0xd9 /*右下角点*/#define CROSSL 0xc3 /*左边*/#define CROSSR 0xb4 /*右边*/#define CROSSU 0xc2 /*上边*/#define CROSSD 0xc1 /*下边*/#define CROSS 0xc5 /*十字交叉点*//*定义棋盘左上角点在屏幕上的位置*/#define MAPXOFT 5#define MAPYOFT 2/*定义1号玩家的操作键键码*/#define PLAY1UP 0x1157/*上移--'W'*/#define PLAY1DOWN 0x1f53/*下移--'S'*/#define PLAY1LEFT 0x1e41/*左移--'A'*/#define PLAY1RIGHT 0x2044/*右移--'D'*/#define PLAY1DO 0x3920/*落子--空格键*//*定义2号玩家的操作键键码*/#define PLAY2UP 0x4800/*上移--方向键up*/#define PLAY2DOWN 0x5000/*下移--方向键down*/#define PLAY2LEFT 0x4b00/*左移--方向键left*/#define PLAY2RIGHT 0x4d00/*右移--方向键right*/#define PLAY2DO 0x1c0d/*落子--回车键Enter*//*若想在游戏中途退出, 可按 Esc 键*/#define ESCAPE 0x011b/*定义棋盘上交叉点的状态, 即该点有无棋子 *//*若有棋子, 还应能指出是哪个玩家的棋子 */#define CHESSNULL 0 /*没有棋子*/#define CHESS1 'O'/*一号玩家的棋子*/#define CHESS2 'X'/*二号玩家的棋子*//*定义按键类别*/#define KEYEX99v 0/*退出键*/#define KEYFALLCHESS 1/*落子键*/#define KEYMOVECURSOR 2/*光标移动键*/#define KEYINVALID 3/*无效键*//*定义符号常量: 真, 假 --- 真为1, 假为0 */#define TRUE 1#define FALSE 0/**********************************************************//* 定义数据结构 *//*棋盘交叉点坐标的数据结构*/struct point{int x,y;};或者下面这个:#include graphics.h#include stdlib.h#include stdio.h#include conio.h#define N 15#define B 7#define STOP -10000#define OK 1#define NO 0#define UP 328#define DOWN 336#define LEFT 331#define RIGHT 333int a[N+1][N+1];int zx,zy;int write=1,biaoji=0;struct zn{long sum;int y;int x;}w[N+1][N+1],max,max1;void cbar(int i,int x,int y,int r);void map(int a[][]);int getkey();int key();void zuobiao(int x,int y,int i);int tu(int a[][],int write);int wtu(int a[][],int write);int zhineng(int a[][]);int zh5(int y,int x,int a[][]);long zzh5(int b[][],int i);main(){int i,j;int gdriver=DETECT;int gmode;initgraph(gdriver,gmode,"");zx=(N+1)/2;zy=(N+1)/2;for(i=1;i=N;i++)for(j=1;j=N;j++)a[i][j]=0;map(a);i=1;while(i){int k,n;k=wtu(a,write);if(k==STOP) goto end;map(a);n=zhineng(a);if(n==STOP) goto end;map(a);}end:;}int zhineng(int a[N+1][N+1]){int i,j;int k;max.sum=-1;for(i=0;i=N;i++)for(j=0;j+N;j++){w[i][j].sum=0;w[i][j].x=i;w[i][j].y=j;}for(i=1;i=N-4;i++)for(j=1;j=N-4;j++){k=zh5(i,j,a);if(k==STOP) return (STOP);}for(i=1;i=N;i++)for(j=1;j=N;j++){if(max.sumw[i][j].sum){max.sum=w[i][j].sum;max.y=i;max.x=j;}else if(max.sum==w[i][j].sum){if(((max.y-zy)*(max.y-zy)+(max.x-zx)*(max.x-zx))((i-zy)*(i-zy)+(j-zx)*(j-zx)))max.sum=w[i][j].sum;max.y=i;max.x=j;}}if(a[max.y][max.x]==0){a[max.y][max.x]=-1;zy=max.y;zx=max.x;}}int zh5(int y,int x,int a[N+1][N+1]){int i,j;int b[6][6];long c[13];long d[6][6];long temp;for(i=y;i=y+4;i++)for(j=x;j=x+4;j++)b[i+1-y][j+1-x]=a[i][j];c[1]=b[1][1]+b[1][2]+b[1][3]+b[1][4]+b[1][5];c[2]=b[2][1]+b[2][2]+b[2][3]+b[2][4]+b[2][5];c[3]=b[3][1]+b[3][2]+b[3][3]+b[3][4]+b[3][5];c[4]=b[4][1]+b[4][2]+b[4][3]+b[4][4]+b[4][5];c[5]=b[5][1]+b[5][2]+b[5][3]+b[5][4]+b[5][5];c[6]=b[1][1]+b[2][1]+b[3][1]+b[4][1]+b[5][1];c[7]=b[1][2]+b[2][2]+b[3][2]+b[4][2]+b[5][2];c[8]=b[1][3]+b[2][3]+b[3][3]+b[4][3]+b[5][3];c[9]=b[1][4]+b[2][4]+b[3][4]+b[4][4]+b[5][4];c[10]=b[1][5]+b[2][5]+b[3][5]+b[4][5]+b[5][5];c[11]=b[1][1]+b[2][2]+b[3][3]+b[4][4]+b[5][5];c[12]=b[1][5]+b[2][4]+b[3][3]+b[4][2]+b[5][1];for(i=1;i=12;i++){switch(c[i]){case 5:biaoji=1;return(STOP);case -5:biaoji=-1;return(STOP);case -4:c[i]=100000;break;case 4:c[i]=100000;break;case -3:c[i]=150;break;case 3:c[i]=150;break;case -2:c[i]=120;break;case 2:c[i]=100;break;case -1:c[i]=1;break;case 1:c[i]=1;break;default: c[i]=0;}}for(i=1;i=12;i++){if(c[i]==150)c[i]+=zzh5(b,i);}for(i=1;i=5;i++)for(j=1;j=5;j++)d[i][j]=0;for(i=1;i=5;i++)for(j=1;j=5;j++){if(i==j) d[i][j]+=c[11];if((i+j)==6) d[i][j]+=c[12];d[i][j]+=c[i]+c[j+5];}for(i=1;i=5;i++)for(j=1;j=5;j++){if(b[i][j]!=0)d[i][j]=-2;}max1.sum=-1;max1.y=0;max1.x=0;for(i=1;i=5;i++)for(j=1;j=5;j++){if(max1.sumd[i][j]){max1.sum=d[i][j];max1.y=i;max1.x=j;w[i+y-1][j+x-1].sum+=max1.sum;}else if(max1.sum==d[i][j]){if(((i+y-1-zy)*(i+y-1-zy)+(j+x-1-zx)*(j+x-1-zx))((max1.y+y-1-zy)*(max1.y+y-1-zy)+(max1.x+x-1-zx)*(max1.x+x-1-zx))){max1.sum=d[i][j];max1.y=i;max1.x=j;}}}}long zzh5(int b[6][6],int n){int i,j,k,l,m;switch(n){case 1:i=b[1][1];j=b[1][2];k=b[1][3];l=b[1][4];m=b[1][5];break;case 2:i=b[2][1];j=b[2][2];k=b[2][3];l=b[2][4];m=b[2][5];break;case 3:i=b[3][1];j=b[3][2];k=b[3][3];l=b[3][4];m=b[3][5];break;case 4:i=b[4][1];j=b[4][2];k=b[4][3];l=b[4][4];m=b[4][5];break;case 5:i=b[5][1];j=b[5][2];k=b[5][3];l=b[5][4];m=b[5][5];break;case 6:i=b[1][1];j=b[2][1];k=b[3][1];l=b[4][1];m=b[5][1];break;case 7:i=b[1][2];j=b[2][2];k=b[3][2];l=b[4][2];m=b[5][2];break;case 8:i=b[1][3];j=b[2][3];k=b[3][3];l=b[4][3];m=b[5][3];break;case 9:i=b[1][4];j=b[2][4];k=b[3][4];l=b[4][4];m=b[5][4];break;case 10:i=b[1][5];j=b[2][5];k=b[3][5];l=b[4][5];m=b[5][5];break;case 11:i=b[1][1];j=b[2][2];k=b[3][3];l=b[4][4];m=b[5][5];break;case 12:i=b[1][5];j=b[2][4];k=b[3][3];l=b[4][2];m=b[5][1];break;}if((i==0j==1k==1l==1m==0))return (900);if((i==0j==-1k==-1l==-1m==0))return(1000);if((i==0j==0k==1l==1m==1)||(i==1j==1k==1l==0m==0))return(20);if((i==0j==0k==-1l==-1m==-1)||(i==-1j==-1k==-1l==0m==0))return(20);if((i==-1j==1k==1l==1m==1)||(i==1j==-1k==1l==1m==1)||(i==1j==1k==-1l==1m==1)||(i==1j==1k==1l==-1m==1)||(i==1j==1k==1l==1m==-1))return(-60);if((i==1j==-1k==-1l==-1m==-1)||(i==-1j==1k==-1l==-1m==-1)||(i==-1j==1k==-1l==-1m==-1)||(i==-1j==-1k==-1l==1m==-1)||(i==-1j==-1k==-1l==-1m==1))return(-60);}int wtu(int a[N+1][N+1],int write){int i=1;map(a);zuobiao(zx,zy,1);while(i){int k;k=tu(a,write);if(k==OK) i=0;if(k==STOP) return (STOP);}}int getkey(){int key,lo,hi;key=bioskey(0);lo=key0x00ff;hi=(key0xff00)8;return((lo==0) ? hi+256:lo);}int key(){int k;k=getkey();switch(k){case 27: return (STOP);case 13:case ' ': return (OK);case 328: return (UP);case 336: return (DOWN);case 331: return (LEFT);case 333: return (RIGHT);default: return (NO);}}void zuobiao(int x,int y,int i){int r;if(i!=0){setcolor(GREEN);for(r=1;r=5;r++)circle(75+25*x,25+25*y,r);}else{if(a[zy][zx]==1){setcolor(8);for(r=1;r=5;r++)circle(75+25*x,25+25*y,r);}else if(a[zy][zx]==-1){setcolor(WHITE);for(r=1;r=5;r++)circle(75+25*x,25+25*y,r);}else{setcolor(B);for(r=1;r=5;r++)circle(75+25*x,25+25*y,r);setcolor(RED); line(75+25*zx-5,25+25*zy,75+25*x+5,25+25*zy);line(75+25*zx,25+25*zy-5,75+25*zx,25+25*zy+5);}}}int tu(int a[N+1][N+1],int write){int k;re:k=key();if(k==OK){if(a[zy][zx]==0){a[zy][zx]=write;}elsegoto re;}if(k==STOP) return(STOP);if(k==NO) goto re;if(k==UP){int i,j;if(zy==1) j=zy;else j=zy-1;zuobiao(zx,zy,0);zuobiao(zx,j,1);zy=j;goto re;}if(k==DOWN){int i,j;if(zy==N) j=zy;else j=zy+1;zuobiao(zx,zy,0);zuobiao(zx,j,1);zy=j;goto re;}if(k==LEFT){int i,j;if(zx==1) i=zx;else i=zx-1;zuobiao(zx,zy,0);zuobiao(i,zy,1);zx=i;goto re;}if(k==RIGHT){int i,j;if(zx==N) i=zx;else i=zx+1;zuobiao(zx,zy,0);zuobiao(i,zy,1);zx=i;goto re;}}void cbar(int i,int x,int y,int r){if(i!=0){if(i==1)setcolor(8);else if(i==-1)setcolor(WHITE);for(i=1;i=r;i++){circle(x,y,i);}}}void map(int a[N+1][N+1]){int i,j;cleardevice();setbkcolor(B);setcolor(RED);for(i=0;iN;i++){line(100,50+25*i,75+N*25,50+25*i);line(100+25*i,50,100+25*i,25+N*25);}for(i=1;i=N;i++)for(j=1;j=N;j++)cbar(a[i][j],75+25*j,25+25*i,10);}
如何用C语言做一个五子棋程序?要有源代码。
我手头上有一个不能人机对战的用java编写的界面基于c语言的五子棋源码,可以判定输赢的。
//界面代码
import java.awt.*;
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.*;
import javax.swing.*;
class drawChessBoard extends Canvas
//Image image;
Image image_black,image_white,image_point;
ImageIcon imageIcon_black = new ImageIcon("E:/workspace/五子棋/res/Image/black.jpg");
ImageIcon imageIcon_white = new ImageIcon("E:/workspace/五子棋/res/Image/white.jpg");
ImageIcon imageIcon_point = new ImageIcon("E:/workspace/五子棋/res/point.jpg");
isWin win = new isWin();
int locate =0;
int count =0;
static int drawCount = 0;
static String str;
public void paint(Graphics g)
int row = 0,column = 0;
chessboard chessBoard = new chessboard();
drawChessBoard draw = new drawChessBoard();
image_black = imageIcon_black.getImage();
image_white = imageIcon_white.getImage();
image_point = imageIcon_point.getImage();
int temp_x = 0,temp_y = 0;
//画棋盘
for(row = 0;row 16;row ++)
g.drawLine(0,40 + row * 40, 640, 40 + row * 40);
g.drawLine(40 + row * 40, 0, 40 + row * 40, 640);
// g.drawImage(image_point,317,317,imageIcon_point.getIconWidth(),imageIcon_point.getIconHeight(),imageIcon_point.getImageObserver());
// g.drawImage(image_point,477,157,imageIcon_point.getIconWidth(),imageIcon_point.getIconHeight(),imageIcon_point.getImageObserver());
// g.drawImage(image_point,157,157,imageIcon_point.getIconWidth(),imageIcon_point.getIconHeight(),imageIcon_point.getImageObserver());
// g.drawImage(image_point,157,477,imageIcon_point.getIconWidth(),imageIcon_point.getIconHeight(),imageIcon_point.getImageObserver());
// g.drawImage(image_point,477,477,imageIcon_point.getIconWidth(),imageIcon_point.getIconHeight(),imageIcon_point.getImageObserver());
for(column = 0;column 16;column ++)
if( chessboard.chess_state[row][column] == 1)
g.drawImage(image_black,20 + row * 40,20 + column * 40,imageIcon_black.getIconWidth(),imageIcon_black.getIconHeight(),imageIcon_black.getImageObserver());
else if(chessboard.chess_state[row][column] == 2)
g.drawImage(image_white,20 + row * 40,20 + column * 40,imageIcon_white.getIconWidth(),imageIcon_white.getIconHeight(),imageIcon_white.getImageObserver());
//画棋子
if(count != 0 chessboard.isStart)
if(chessboard.coordinate_x / 40 = 0 chessboard.coordinate_x / 40 16 chessboard.coordinate_y / 40 = 0 chessboard.coordinate_y / 40 16)
if(chessboard.isBlack chessboard.chess_state[chessboard.coordinate_x / 40][chessboard.coordinate_y / 40] == 0 )
g.drawImage(image_black,20 + chessBoard.coordinate_x,20 + chessBoard.coordinate_y,imageIcon_black.getIconWidth(),imageIcon_black.getIconHeight(),imageIcon_black.getImageObserver());
chessboard.isBlack = false;
chessboard.isWhite = true;
chessboard.chess_state[ chessboard.coordinate_x / 40 ][ chessboard.coordinate_y / 40 ] = 1;
// chessboard.chess_state[chessboard.coordinate_x / 40 - 1][chessboard.coordinate_y / 40 - 1] = 2;
//判断黑子是否赢
str = win.winJudge( chessboard.coordinate_x / 40,chessboard.coordinate_y / 40 );
if(chessboard.isWhite chessboard.chess_state[chessboard.coordinate_x / 40][chessboard.coordinate_y / 40] == 0 )
g.drawImage(image_white,20 + chessBoard.coordinate_x,20 + chessBoard.coordinate_y,imageIcon_white.getIconWidth(),imageIcon_white.getIconHeight(),imageIcon_white.getImageObserver());
// g.drawImage(image_white,temp_x - 40,temp_y - 40,imageIcon_white.getIconWidth(),imageIcon_white.getIconHeight(),imageIcon_white.getImageObserver());
chessboard.isWhite = false;
chessboard.isBlack = true;
chessboard.chess_state[chessboard.coordinate_x / 40][chessboard.coordinate_y / 40] = 2;
//判断白子是否赢
str = win.winJudge(chessboard.coordinate_x / 40,chessboard.coordinate_y / 40 );
count ++;
public void update(Graphics g)
chessboard chessBoard = new chessboard();
//覆盖问题
if((chessboard.isChange || chessboard.chess_state[chessboard.coordinate_x / 40][chessboard.coordinate_y / 40] == 0) chessboard.isStart )
super.update(g);
chessboard.isChange = false;//重写update方法
public class chessboard extends Frame implements MouseListener,ActionListener,MouseMotionListener
Button button = new Button("开始游戏");
Button button_exit = new Button("退出游戏");
static boolean isChange = false;
static boolean isStart = false;
drawChessBoard draw = new drawChessBoard();
int i ,j;
Panel panel = new Panel(),p;
static boolean isBlack = true,isWhite = false;
static boolean isPlayer = true,isComputer = false;
static int coordinate_x = 0,coordinate_y = 0;
static int chess_state[][] = new int[17][17];
JRadioButton black_radio,white_radio;
Label result_label;
chessboard(String name)
for(i = 0;i 17;i ++)
for(j = 0;j 17;j ++)
chess_state[i][j] = 0;
Color color = new Color(210,210,210);
panel.add(button);
panel.setLayout(null);
result_label = new Label();//创建标签
black_radio = new JRadioButton("黑棋",false);
white_radio = new JRadioButton("白棋",false);
black_radio.setBounds(680,80,40,20);
black_radio.setBounds(720,80,40,20);
result_label.setBounds(680,220,80,40);
button.setBounds(680,100,80,40);
button_exit.setBounds(680,160,80,40);
this.add(button);
this.add(button_exit);
this.add(result_label);
this.add(black_radio);
this.add(white_radio);
p = new Panel();
p.setLayout(null);
p.setBackground(color);
// this.add(drawchess);
this.add(draw);
this.setBounds(300, 20, 800, 700);
draw.addMouseListener(this);//监听draw
draw.addMouseMotionListener(this);
button.addActionListener(this);
button_exit.addActionListener(this);
this.addWindowListener(new MyWindowListener());
this.setVisible(true);
chessboard()
public void actionPerformed(ActionEvent e)
if((Button)e.getSource() == button_exit)
System.exit(0);
else if ((Button)e.getSource() == button)
isStart = true;
result_label.setText("游戏进行中");
for(i = 0;i 17;i ++)
for(j = 0;j 17;j ++)
chessboard.chess_state[i][j] = 0;
draw.repaint();
public void mouseClicked(MouseEvent e)
public void mouseEntered(MouseEvent e)
public void mousePressed(MouseEvent e)
chessboard chess = new chessboard();
if(chess.coordinate_x = 640 chess.coordinate_y = 640 chess.coordinate_x = 0 chess.coordinate_y = 0 )
draw.repaint();
public void mouseReleased(MouseEvent e)
if(isStart == false)
result_label.setText(drawChessBoard.str);
//电脑落子调用mouseReleased
* if(chessboard.isComputer)
draw.repaint();
public void mouseExited(MouseEvent e)
public void mouseDragged(MouseEvent e)
// System.exit(0);
public void mouseMoved(MouseEvent e){
Integer integ1,integ2 ;
chessboard.isChange = true;
coordinate_x = e.getX();
coordinate_y = e.getY();
// integ1 = new Integer(coordinate_x / 40);
// integ2 = new Integer(coordinate_y / 40);
// result_label.setText(integ1.toString()+" "+integ2.toString());
if(coordinate_x = 20)
coordinate_x = (coordinate_x / 40 + 1) * 40;
if(coordinate_y = 20)
coordinate_y = (coordinate_y / 40 + 1) * 40;
if( coordinate_x % 40 =20 )
coordinate_x = (coordinate_x / 40 -1) * 40;
else
coordinate_x = (coordinate_x / 40 ) * 40;
if( coordinate_y % 40 =20 )
coordinate_y = (coordinate_y / 40 - 1 ) * 40;
else
coordinate_y = (coordinate_y / 40 ) * 40;
public static void main(String args[])
chessboard cb = new chessboard("五子棋");
class MyWindowListener extends WindowAdapter
public void windowClosing(WindowEvent e)
System.exit(0);
//判断输赢
public class isWin {
//static int count = 0;
public String winJudge(int row,int col)
chessboard chessBoard = new chessboard();
int index,count = 1;
int n = 0 ,flag = 1;
boolean isOneside = true, isAnotherside = true;
if(chessboard.chess_state[row][col] != 0)
//水平方向判断
for(index = 0;index 5;index ++)
//向落子点右边扫描
while(isOneside)
if(n == 0) count = 1;
if(row + count = 16 chessboard.chess_state[ row + count][ col ] == chessboard.chess_state[ row ][ col ] )
flag ++;
n ++;
count ++;
else
isOneside = false;
n = 0;
break;
//向落子点左边扫面
while(!isOneside)
if(n == 0) count = 1;
if(row - count = 0 chessboard.chess_state[ row - count ][ col ] == chessboard.chess_state[ row ][ col ] !isOneside row 1)
flag ++;
count ++;
n ++;
else
isOneside = true;
n = 0;
break;
//如果左边没有落子或者落基于c语言的五子棋源码了颜色不同的子
if(flag = 5)
chessboard.isStart = false;
if(chessboard.chess_state[row][col] == 1)
return "黑子赢了";
else
return "白子赢了";
if(flag 5)
flag = 1;
//竖直方向判断
for(index = 0;index 5;index ++)
while(isOneside)
if( n == 0 )
count = 1;
if(col + count = 16 chessboard.chess_state[ row ][ col + count ] == chessboard.chess_state[ row ][ col ] )
flag ++;
n ++;
count ++;
else
isOneside = false;
n = 0;
break;
//向落子点左边扫面
while(!isOneside)
if(n == 0) count = 1;
if(col - count = 0 chessboard.chess_state[ row ][ col - count ] == chessboard.chess_state[ row ][ col ] !isOneside )
flag ++;
n ++;
count ++;
else
isOneside = true;
n = 0;
break;
//如果左边没有落子或者落了颜色不同的子
if(flag = 5)
chessboard.isStart = false;
if(chessboard.chess_state[row][col] == 1)
return "黑子赢了";
else if(chessboard.chess_state[row][col] == 2)
return "白子赢了";
if(flag 5)
flag = 1;
//斜向上方向判断
for(index = 0;index 5;index ++)
//向落子点右边扫描
while(isOneside)
if(n == 0) count = 1;
if( chessboard.chess_state[ row + count][ col + count ] == chessboard.chess_state[ row ][ col ] )
flag ++;
n ++;
count ++;
else
isOneside = false;
n = 0;
break;
//向落子点左边扫面
while(!isOneside)
if(n == 0) count = 1;
if(row - count = 0 col - count = 0 chessboard.chess_state[ row - count][ col - count ] == chessboard.chess_state[ row ][ col ] !isOneside)
flag ++;
count ++;
n ++;
else
isOneside = true;
n = 0;
break;
//如果左边没有落子或者落了颜色不同的子
if(flag = 5)
chessboard.isStart = false;
if(chessboard.chess_state[row][col] == 1)
return "黑子赢了";
else
return "白子赢了";
if(flag 5)
flag = 1;
//斜向下方向判断
for(index = 0;index 5;index ++)
//向落子点右边扫描
while(isOneside)
if(n == 0) count = 1;
if(row - count = 0 col + count = 16 chessboard.chess_state[ row - count][ col + count] == chessboard.chess_state[ row ][ col ] )
flag ++;
n ++;
count ++;
else
isOneside = false;
n = 0;
break;
//向落子点左边扫面
while(!isOneside)
if(n == 0) count = 1;
if(row + count = 16 col - count = 0 chessboard.chess_state[ row + count][ col - count ] == chessboard.chess_state[ row ][ col ] !isOneside)
flag ++;
count ++;
n ++;
else
isOneside = true;
n = 0;
break;
//如果左边没有落子或者落了颜色不同的子
if(flag = 5)
chessboard.isStart = false;
if(chessboard.chess_state[row][col] == 1)
return "黑子赢了";
else
return "白子赢了";
if(flag 5)
flag = 1;
}//if
return "0";
public static void main(String args[])
isWin winjudge = new isWin();
//winjudge.winJudge(row, col);