c语言的五子棋代码(博弈算法)

Python013

c语言的五子棋代码(博弈算法),第1张

#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=1i<=Ni++)

for(j=1j<=Nj++)

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=0i<=Ni++)

for(j=0j<+Nj++)

{

w[i][j].sum=0

w[i][j].x=i

w[i][j].y=j

}

for(i=1i<=N-4i++)

for(j=1j<=N-4j++)

{

k=zh5(i,j,a)

if(k==STOP) return (STOP)

}

for(i=1i<=Ni++)

for(j=1j<=Nj++)

{

if(max.sum<w[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=yi<=y+4i++)

for(j=xj<=x+4j++)

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=1i<=12i++)

{

switch(c[i])

{

case 5:biaoji=1return(STOP)

case -5:biaoji=-1return(STOP)

case -4:c[i]=100000break

case 4:c[i]=100000break

case -3:c[i]=150break

case 3:c[i]=150break

case -2:c[i]=120break

case 2:c[i]=100break

case -1:c[i]=1break

case 1:c[i]=1break

default: c[i]=0

}

}

for(i=1i<=12i++)

{

if(c[i]==150)

c[i]+=zzh5(b,i)

}

for(i=1i<=5i++)

for(j=1j<=5j++)

d[i][j]=0

for(i=1i<=5i++)

for(j=1j<=5j++)

{

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=1i<=5i++)

for(j=1j<=5j++)

{

if(b[i][j]!=0)

d[i][j]=-2

}

max1.sum=-1

max1.y=0

max1.x=0

for(i=1i<=5i++)

for(j=1j<=5j++)

{

if(max1.sum<d[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==0&&j==1&&k==1&&l==1&&m==0))

return (900)

if((i==0&&j==-1&&k==-1&&l==-1&&m==0))

return(1000)

if((i==0&&j==0&&k==1&&l==1&&m==1)||(i==1&&j==1&&k==1&&l==0&&m==0))

return(20)

if((i==0&&j==0&&k==-1&&l==-1&&m==-1)||(i==-1&&j==-1&&k==-1&&l==0&&m==0))

return(20)

if((i==-1&&j==1&&k==1&&l==1&&m==1)||(i==1&&j==-1&&k==1&&l==1&&m==1)||(i==1&&j==1&&k==-1&&l==1&&m==1)||(i==1&&j==1&&k==1&&l==-1&&m==1)||(i==1&&j==1&&k==1&&l==1&&m==-1))

return(-60)

if((i==1&&j==-1&&k==-1&&l==-1&&m==-1)||(i==-1&&j==1&&k==-1&&l==-1&&m==-1)||(i==-1&&j==1&&k==-1&&l==-1&&m==-1)||(i==-1&&j==-1&&k==-1&&l==1&&m==-1)||(i==-1&&j==-1&&k==-1&&l==-1&&m==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=key&0x00ff

hi=(key&0xff00)>>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=1r<=5r++)

circle(75+25*x,25+25*y,r)

}

else

{

if(a[zy][zx]==1)

{

setcolor(8)

for(r=1r<=5r++)

circle(75+25*x,25+25*y,r)

}

else if(a[zy][zx]==-1)

{

setcolor(WHITE)

for(r=1r<=5r++)

circle(75+25*x,25+25*y,r)

}

else

{

setcolor(B)

for(r=1r<=5r++)

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=1i<=ri++)

{

circle(x,y,i)

}

}

}

void map(int a[N+1][N+1])

{

int i,j

cleardevice()

setbkcolor(B)

setcolor(RED)

for(i=0i<Ni++)

{

line(100,50+25*i,75+N*25,50+25*i)

line(100+25*i,50,100+25*i,25+N*25)

}

for(i=1i<=Ni++)

for(j=1j<=Nj++)

cbar(a[i][j],75+25*j,25+25*i,10)

}

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)

}

}