用C语言编写一个五子棋的游戏程序

用C语言编写一个五子棋的游戏程序,第1张

// 程序“五子棋第三方版exe”运行时,把以前下棋的棋谱保存在“棋谱txt”文件中,

// 然后调用本程序。

// 本程序的作用是:首先读入以前的棋谱,然后调用函数“qxwz()”计算出计算机应该

// 下棋的位置,并保存在c中,然后把应下棋的位置写入文件“当前棋子txt”

#include <stdioh>

typedef struct

{

char x;

char y;

} QZ;

QZ qzwz(); //取得应下棋位置,返回值为QZ类型,其中的x,y两个成员代表应下棋位置

char qp[19][19]; //用于保存棋盘上每个点的状态,0--空,1--黑棋,2--白棋

void main()

{

FILE fp;

int tmp1,tmp2,tmp3;

QZ c;

/ 读入当前棋谱 /

if((fp=fopen("棋谱txt","r"))==NULL)

{

printf("[棋谱TXT]文件打开失败!\n");

return;

}

while(!feof(fp))

{

tmp1=tmp2=tmp3=-1;

fscanf(fp,"%d%d%d",&tmp1,&tmp2,&tmp3);

if(tmp3!=-1)

{

qp[tmp1][tmp2]=tmp3;

}

}

fclose(fp);

c=qzwz();

if((fp=fopen("当前棋子txt","w"))==NULL)

{

printf("[当前棋子TXT]文件打开失败!\n");

return;

}

fprintf(fp,"%d,%d,%d",cx,cy,2); //将应下棋位置传送到文件中

fclose(fp);

}

QZ qzwz()

{

(把这里面的程序补充完整就好了)

} 这个程序要求是:

1。只适用于人先下,机器后下的情况。有了“五子棋第三方版exe”来制作下棋界面,就只需要编写下棋算法,而不必考虑其它的问题了。

2.当运行“五子棋第三方版exe”程序时,该程序接受人下棋位置,然后调用“wzqexe”程序,由“wzqexe”程序计算出机器的应对并写入到“当前棋子txt”文件中(结束),机器的应对结果由“五子棋第三方版exe”负责读入,并等待人下一步的应对。

3.要做的工作就是,完成“wzqcpp” 中“qxwz()”函数的设计,使之能返回一个比较好的下棋位置。在设计过程中,可以增加全局变量以保存你认为有必要保存的数据,可以增加其它文件,用来保存你认为下次调用还有必要使用的数据,可以增加其它函数以方便下棋位置的计算过程。总之,所给出的“wzqcpp”程序只是一个框架,在不影响其功能的情况下,可以随意更改。

五子棋C语言代码如下:

#include <stdioh>

#include <biosh>

#include <ctypeh>

#include <conioh>

#include <dosh>

#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 <graphicsh>

#include <stdlibh>

#include <stdioh>

#include <conioh>

#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;

maxsum=-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(maxsum<w[i][j]sum)

{

maxsum=w[i][j]sum;

maxy=i;

maxx=j;

}

else if(maxsum==w[i][j]sum)

{

if(((maxy-zy)(maxy-zy)+(maxx-zx)(maxx-zx))>((i-zy)(i-zy)+(j-zx)(j-zx)))

maxsum=w[i][j]sum;

maxy=i;

maxx=j;

}

}

if(a[maxy][maxx]==0)

{

a[maxy][maxx]=-1;

zy=maxy;

zx=maxx;

}

}

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;

}

max1sum=-1;

max1y=0;

max1x=0;

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

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

{

if(max1sum<d[i][j])

{

max1sum=d[i][j];

max1y=i;

max1x=j;

w[i+y-1][j+x-1]sum+=max1sum;

}

else if(max1sum==d[i][j])

{

if(((i+y-1-zy)(i+y-1-zy)+(j+x-1-zx)(j+x-1-zx))>((max1y+y-1-zy)(max1y+y-1-zy)+(max1x+x-1-zx)(max1x+x-1-zx)))

{

max1sum=d[i][j];

max1y=i;

max1x=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=1;r<=5;r++)

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

}

else

{

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

{

setcolor(8);

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

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

}

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

{

setcolor(WHITE);

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

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

}

else

{

setcolor(B);

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

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

setcolor(RED); line(75+25zx-5,25+25zy,75+25x+5,25+25zy);

line(75+25zx,25+25zy-5,75+25zx,25+25zy+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;i<N;i++)

{

line(100,50+25i,75+N25,50+25i);

line(100+25i,50,100+25i,25+N25);

}

for(i=1;i<=N;i++)

for(j=1;j<=N;j++)

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

}

Option Explicit

'五子棋程序 人机对战版本

'需要2个Label控件 2个CommandButton控件

Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long

Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long

'Dim PlayStep() As String '记录棋谱的数组

'Dim Label2Cap As String

Private Const BoxL As Single = 50, BoxT As Single = 50, BoxW As Single = 25, BoxN As Integer = 18

Dim Table() As Long '棋盘(0-BoxN,0-BoxN) 0-空 1-黑子 2-白子

Dim PsCore() As Long '定义当前玩家桌面空格的分数

Dim CsCore() As Long '定义当前电脑桌面空格的分数

Dim pWin() As Boolean '定义玩家的获胜组合

Dim cWin() As Boolean '定义电脑的获胜组合

Dim pFlag() As Boolean '定义玩家的获胜组合标志

Dim cFlag() As Boolean '定义电脑的获胜组合标志

Dim ThePlayFlag As Boolean '定义游戏有效标志

Private Sub Command1_Click()

If Not ThePlayFlag Then Call InitPlayEnvironment: Exit Sub

If MsgBox("本局还没有下完,是否重新开始?(Y/N)", vbYesNo) = vbNo Then Exit Sub

Call InitPlayEnvironment

End Sub

Private Sub Command2_Click()

End

End Sub

Private Sub Form_Load()

Dim i As Long, lw As Long, lh As Long

'Label2Cap = "000 黑方 行 00 列 00"

MeWidth = 10815: MeHeight = 8040

' MeCaption = "五子棋 - 人机对战": MeShow

lw = MeWidth \ ScreenTwipsPerPixelX: lh = MeHeight \ ScreenTwipsPerPixelY

SetWindowRgn MehWnd, CreateRoundRectRgn(0, 0, lw, lh, 60, 60), True

With Label1

Alignment = vbCenter: FontSize = 12: FontBold = True

ForeColor = vbRed: BackStyle = 0: AutoSize = True: Move 8910, 510

End With

Label2AutoSize = True: Label2WordWrap = True

Label2BackStyle = 0: Label2Move 8040, 1050, 2280

Command1Move 8025, 7035, 1020, 435: Command1Caption = "再来一局"

Command2Move 9300, 7035, 1020, 435: Command2Caption = "不玩了"

Call DrawChessBoard: MeFillStyle = 0: Call InitPlayEnvironment

End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

End

End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

Dim iRow As Long, iCol As Long, i As Long, k As Long, t As String

If Not ThePlayFlag Then Exit Sub

If Button = vbLeftButton Then '左键下棋

iRow = -1: iCol = -1

For i = 0 To BoxN '鼠标必须落在交叉点 半径10以内 若是则给出行列号

If (Y + 10) > (BoxT + i BoxW) And (Y - 10) <= (BoxT + i BoxW) Then iRow = i

If (X + 10) > (BoxL + i BoxW) And (X - 10) <= (BoxL + i BoxW) Then iCol = i

Next

If (iRow = -1) Or (iCol = -1) Then Beep: Exit Sub

If Table(iCol, iRow) > 0 Then Exit Sub

Table(iCol, iRow) = 2: Label1Caption = "下一步 黑方"

MeFillColor = vbWhite: MeCircle (iCol BoxW + BoxT, iRow BoxW + BoxL), 8

For i = 0 To UBound(cWin, 3)

If cWin(iCol, iRow, i) = True Then cFlag(i) = False

Next

Call CheckWin: Call DianNao '检查当前玩家是否获胜 调用电脑算法

End If

End Sub

Public Sub InitPlayEnvironment()

'

' 模块名称: InitPlayEnvironment [初始化过程]

'

' 描述: 1 设置背景音乐。 2 设置游戏状态有效。

' 3 初始化游戏状态标签。 4 直接指定电脑的第一步走法。

' 5 初始化基本得分桌面。 6 电脑和玩家获胜标志初始化。

' 7 初始化所有获胜组合。 8 重新设定玩家的获胜标志。

'

Dim i As Long, j As Long, m As Long, n As Long

ThePlayFlag = True: Label1Caption = "下一步 白方": Label2Caption = ""

MeFillColor = vbBlack: MeFillStyle = 0: MeAutoRedraw = True

MeCls: MeCircle (9 BoxW + BoxL, 9 BoxW + BoxT), 8

ReDim Table(0 To BoxN, 0 To BoxN) As Long

ReDim pFlag(NumsWin(BoxN + 1) - 1) As Boolean

ReDim cFlag(UBound(pFlag)) As Boolean

ReDim PsCore(BoxN, BoxN) As Long, CsCore(BoxN, BoxN) As Long

ReDim pWin(BoxN, BoxN, UBound(pFlag)) As Boolean

ReDim cWin(BoxN, BoxN, UBound(pFlag)) As Boolean

For i = 0 To UBound(pFlag): pFlag(i) = True: cFlag(i) = True: Next

Table(9, 9) = 1 '假定电脑先手 并下了(9, 9)位 将其值设为1

' 初始化获胜组合

For i = 0 To BoxN: For j = 0 To BoxN - 4

For m = 0 To 4

pWin(j + m, i, n) = True: cWin(j + m, i, n) = True

Next

n = n + 1

Next: Next

For i = 0 To BoxN: For j = 0 To BoxN - 4

For m = 0 To 4

pWin(i, j + m, n) = True: cWin(i, j + m, n) = True

Next

n = n + 1

Next: Next

For i = 0 To BoxN - 4: For j = 0 To BoxN - 4

For m = 0 To 4

pWin(j + m, i + m, n) = True: cWin(j + m, i + m, n) = True

Next

n = n + 1

Next: Next

For i = 0 To BoxN - 4: For j = BoxN To 4 Step -1

For m = 0 To 4

pWin(j - m, i + m, n) = True: cWin(j - m, i + m, n) = True

Next

n = n + 1

Next: Next

' 初始化获胜组合结束

For i = 0 To UBound(pWin, 3) '由于电脑已下了(9, 9)位 所以需要重新设定玩家的获胜标志

If pWin(9, 9, i) = True Then pFlag(i) = False

Next

End Sub

Public Function DrawChessBoard() As Long

'容器的(BoxL, BoxT)为左上角坐标画一个 BoxNBoxN, 每格边长为 BoxW 象素的棋盘

Dim i As Long, j As Long, cx As Long, cy As Long

MeScaleMode = 3: MeFillStyle = 1: MeAutoRedraw = True: MeCls

For i = 0 To BoxN '画棋盘

MeLine (BoxL + i BoxW, BoxT)-(BoxL + i BoxW, BoxT + BoxN BoxW)

MeLine (BoxL, BoxT + i BoxW)-(BoxL + BoxN BoxW, BoxT + i BoxW)

MeCurrentX = BoxL + i BoxW - IIf(i > 9, 6, 2)

MeCurrentY = BoxT - 20: MePrint Format(i)

MeCurrentX = BoxL - IIf(i > 9, 23, 20)

MeCurrentY = BoxT + i BoxW - 6: MePrint Format(i)

Next

For i = 3 To 16 Step 6: For j = 3 To 16 Step 6 '画小标志

cx = BoxL + j BoxW - 3: cy = BoxT + i BoxW - 3

MeLine (cx, cy)-(cx + 6, cy + 6), , B

Next: Next

MeAutoRedraw = False: Set MePicture = MeImage

End Function

Public Sub CheckWin()

'

' 模块名称: CheckWin [获胜检查算法]

'

' 描述: 1 检查是否和棋。 2 检查电脑是否获胜。 3 检查玩家是否获胜。

'

Dim i As Long, j As Long, k As Long, m As Long, n As Long

Dim cA As Long, pA As Long, cN As Long

For i = 0 To UBound(cFlag): cN = IIf(cFlag(i) = False, cN + 1, cN): Next

If cN = UBound(cFlag) - 1 Then '设定和棋规则

Label1Caption = "双方和棋!": ThePlayFlag = False: Exit Sub

End If

For i = 0 To UBound(cFlag) '检查电脑是否获胜

If cFlag(i) = True Then

cA = 0: For j = 0 To BoxN: For k = 0 To BoxN

If Table(j, k) = 1 And cWin(j, k, i) = True Then cA = cA + 1

Next: Next

If cA = 5 Then Label1Caption = "电脑获胜!": ThePlayFlag = False: Exit Sub

End If

Next

For i = 0 To UBound(pFlag) '检查玩家是否获胜

If pFlag(i) = True Then

pA = 0: For j = 0 To BoxN: For k = 0 To BoxN

If Table(j, k) = 2 And pWin(j, k, i) = True Then pA = pA + 1

Next: Next

If pA = 5 Then Label1Caption = "玩家获胜!": ThePlayFlag = False: Exit Sub

End If

Next

End Sub

Public Sub DianNao()

'

' 模块名称: DianNao [电脑算法]

' 描述: 1 初始化赋值系统。 2 赋值加强算法。 3 计算电脑和玩家的最佳攻击位。

' 4 比较电脑和玩家的最佳攻击位并决定电脑的最佳策略。 5 执行检查获胜函数。

'

Dim i As Long, j As Long, k As Long, m As Long, n As Long

Dim Dc As Long, cAb As Long, pAb As Long

ReDim PsCore(BoxN, BoxN) As Long, CsCore(BoxN, BoxN) As Long '初始化赋值数组

' 电脑加强算法

For i = 0 To UBound(cFlag)

If cFlag(i) = True Then

cAb = 0

For j = 0 To BoxN: For k = 0 To BoxN

If Table(j, k) = 1 And cWin(j, k, i) = True Then cAb = cAb + 1

Next: Next

Select Case cAb

Case 3

For m = 0 To BoxN: For n = 0 To BoxN

If Table(m, n) = 0 And cWin(m, n, i) = True Then CsCore(m, n) = CsCore(m, n) + 5

Next: Next

Case 4

For m = 0 To BoxN: For n = 0 To BoxN

If Table(m, n) = 0 And cWin(m, n, i) = True Then

Table(m, n) = 1: Label1Caption = "下一步 白方"

MeFillColor = vbBlack: MeCircle (m BoxW + BoxL, n BoxW + BoxT), 8

For Dc = 0 To UBound(pWin, 3)

If pWin(m, n, Dc) = True Then pFlag(Dc) = False: Call CheckWin: Exit Sub

Next

End If

Next: Next

End Select

End If

Next

For i = 0 To UBound(pFlag)

If pFlag(i) = True Then

pAb = 0

For j = 0 To BoxN: For k = 0 To BoxN

If Table(j, k) = 2 And pWin(j, k, i) = True Then pAb = pAb + 1

Next: Next

Select Case pAb

Case 3

For m = 0 To BoxN: For n = 0 To BoxN

If Table(m, n) = 0 And pWin(m, n, i) = True Then PsCore(m, n) = PsCore(m, n) + 30

Next: Next

Case 4

For m = 0 To BoxN: For n = 0 To BoxN

If Table(m, n) = 0 And pWin(m, n, i) = True Then

Table(m, n) = 1: Label1Caption = "下一步 白方"

MeFillColor = vbBlack: MeCircle (m BoxW + BoxL, n BoxW + BoxT), 8

For Dc = 0 To UBound(pWin, 3)

If pWin(m, n, Dc) = True Then pFlag(Dc) = False: Call CheckWin: Exit Sub

Next

End If

Next: Next

End Select

End If

Next

' 电脑加强算法结束

' 赋值系统

For i = 0 To UBound(cFlag)

If cFlag(i) = True Then

For j = 0 To BoxN: For k = 0 To BoxN

If (Table(j, k) = 0) And cWin(j, k, i) Then

For m = 0 To BoxN: For n = 0 To BoxN

If (Table(m, n) = 1) And cWin(m, n, i) Then CsCore(j, k) = CsCore(j, k) + 1

Next: Next

End If

Next: Next

End If

Next

For i = 0 To UBound(pFlag)

If pFlag(i) = True Then

For j = 0 To BoxN: For k = 0 To BoxN

If (Table(j, k) = 0) And pWin(j, k, i) Then

For m = 0 To BoxN: For n = 0 To BoxN

If (Table(m, n) = 2) And pWin(m, n, i) Then PsCore(j, k) = PsCore(j, k) + 1

Next: Next

End If

Next: Next

End If

Next

' 赋值系统结束

' 分值比较算法

Dim a As Long, b As Long, c As Long, d As Long

Dim cS As Long, pS As Long

For i = 0 To BoxN: For j = 0 To BoxN

If CsCore(i, j) > cS Then cS = CsCore(i, j): a = i: b = j

Next: Next

For i = 0 To BoxN: For j = 0 To BoxN

If PsCore(i, j) > pS Then pS = PsCore(i, j): c = i: d = j

Next: Next

If cS > pS Then

Table(a, b) = 1: Label1Caption = "下一步 白方"

MeFillColor = vbBlack: MeCircle (a BoxW + BoxL, b BoxW + BoxT), 8

For i = 0 To UBound(pWin, 3)

If pWin(a, b, i) = True Then pFlag(i) = False

Next

Else

Table(c, d) = 1: Label1Caption = "下一步 白方"

MeFillColor = vbBlack: MeCircle (c BoxW + BoxL, d BoxW + BoxL), 8

For i = 0 To UBound(pWin, 3)

If pWin(c, d, i) = True Then pFlag(i) = False

Next

End If

' 分值比较算法结束

Call CheckWin

End Sub

Public Function NumsWin(ByVal n As Long) As Long

'根据输入的棋盘布局 nn 计算总共有多少种获胜组合

'假定棋盘为 10 10 相应的棋盘数组就是 Table(9, 9)

'水平方向 每一列获胜组合是6 共10列 610=60

'垂直方向 每一行获胜组合是6 共10行 810=60

'正对角线方向 6 + (5 + 4 + 3 + 2 + 1) 2 = 36

'反对角线方向 6 + (5 + 4 + 3 + 2 + 1) 2 = 36

'总的获胜组合数为 60 + 60 + 36 + 36 = 192

Dim i As Long, t As Long

For i = n - 5 To 1 Step -1: t = t + i: Next

NumsWin = 2 (2 t + n - 4) + 2 n (n - 4)

End Function

我手头上有一个不能人机对战的用java编写的界面,可以判定输赢的。

//界面代码

import javaawt;

import javaxswing;

import javaawteventWindowAdapter;

import javaawteventWindowEvent;

import javaawtevent;

import javaxswing;

class drawChessBoard extends Canvas

{

//Image image;

Image image_black,image_white,image_point;

ImageIcon imageIcon_black = new ImageIcon("E:/workspace/五子棋/res/Image/blackjpg");

ImageIcon imageIcon_white = new ImageIcon("E:/workspace/五子棋/res/Image/whitejpg");

ImageIcon imageIcon_point = new ImageIcon("E:/workspace/五子棋/res/pointjpg");

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_blackgetImage();

image_white = imageIcon_whitegetImage();

image_point = imageIcon_pointgetImage();

int temp_x = 0,temp_y = 0;

//画棋盘

for(row = 0;row < 16;row ++)

{

gdrawLine(0,40 + row 40, 640, 40 + row 40);

gdrawLine(40 + row 40, 0, 40 + row 40, 640);

// gdrawImage(image_point,317,317,imageIcon_pointgetIconWidth(),imageIcon_pointgetIconHeight(),imageIcon_pointgetImageObserver());

// gdrawImage(image_point,477,157,imageIcon_pointgetIconWidth(),imageIcon_pointgetIconHeight(),imageIcon_pointgetImageObserver());

// gdrawImage(image_point,157,157,imageIcon_pointgetIconWidth(),imageIcon_pointgetIconHeight(),imageIcon_pointgetImageObserver());

// gdrawImage(image_point,157,477,imageIcon_pointgetIconWidth(),imageIcon_pointgetIconHeight(),imageIcon_pointgetImageObserver());

// gdrawImage(image_point,477,477,imageIcon_pointgetIconWidth(),imageIcon_pointgetIconHeight(),imageIcon_pointgetImageObserver());

for(column = 0;column < 16;column ++)

{

if( chessboardchess_state[row][column] == 1)

{

gdrawImage(image_black,20 + row 40,20 + column 40,imageIcon_blackgetIconWidth(),imageIcon_blackgetIconHeight(),imageIcon_blackgetImageObserver());

}

else if(chessboardchess_state[row][column] == 2)

{

gdrawImage(image_white,20 + row 40,20 + column 40,imageIcon_whitegetIconWidth(),imageIcon_whitegetIconHeight(),imageIcon_whitegetImageObserver());

}

}

}

//画棋子

if(count != 0 && chessboardisStart)

{

if(chessboardcoordinate_x / 40 >= 0 && chessboardcoordinate_x / 40 < 16 && chessboardcoordinate_y / 40 >= 0 && chessboardcoordinate_y / 40 < 16)

{

if(chessboardisBlack && chessboardchess_state[chessboardcoordinate_x / 40][chessboardcoordinate_y / 40] == 0 )

{

gdrawImage(image_black,20 + chessBoardcoordinate_x,20 + chessBoardcoordinate_y,imageIcon_blackgetIconWidth(),imageIcon_blackgetIconHeight(),imageIcon_blackgetImageObserver());

chessboardisBlack = false;

chessboardisWhite = true;

chessboardchess_state[ chessboardcoordinate_x / 40 ][ chessboardcoordinate_y / 40 ] = 1;

// chessboardchess_state[chessboardcoordinate_x / 40 - 1][chessboardcoordinate_y / 40 - 1] = 2;

//判断黑子是否赢

str = winwinJudge( chessboardcoordinate_x / 40,chessboardcoordinate_y / 40 );

}

if(chessboardisWhite && chessboardchess_state[chessboardcoordinate_x / 40][chessboardcoordinate_y / 40] == 0 )

{

gdrawImage(image_white,20 + chessBoardcoordinate_x,20 + chessBoardcoordinate_y,imageIcon_whitegetIconWidth(),imageIcon_whitegetIconHeight(),imageIcon_whitegetImageObserver());

// gdrawImage(image_white,temp_x - 40,temp_y - 40,imageIcon_whitegetIconWidth(),imageIcon_whitegetIconHeight(),imageIcon_whitegetImageObserver());

chessboardisWhite = false;

chessboardisBlack = true;

chessboardchess_state[chessboardcoordinate_x / 40][chessboardcoordinate_y / 40] = 2;

//判断白子是否赢

str = winwinJudge(chessboardcoordinate_x / 40,chessboardcoordinate_y / 40 );

}

}

}

count ++;

}

public void update(Graphics g)

{

chessboard chessBoard = new chessboard();

//覆盖问题

if((chessboardisChange || chessboardchess_state[chessboardcoordinate_x / 40][chessboardcoordinate_y / 40] == 0) && chessboardisStart )

{

superupdate(g);

chessboardisChange = 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);

paneladd(button);

panelsetLayout(null);

result_label = new Label();//创建标签

black_radio = new JRadioButton("黑棋",false);

white_radio = new JRadioButton("白棋",false);

black_radiosetBounds(680,80,40,20);

black_radiosetBounds(720,80,40,20);

result_labelsetBounds(680,220,80,40);

buttonsetBounds(680,100,80,40);

button_exitsetBounds(680,160,80,40);

thisadd(button);

thisadd(button_exit);

thisadd(result_label);

thisadd(black_radio);

thisadd(white_radio);

p = new Panel();

psetLayout(null);

psetBackground(color);

// thisadd(drawchess);

thisadd(draw);

//

thissetBounds(300, 20, 800, 700);

drawaddMouseListener(this);//监听draw

drawaddMouseMotionListener(this);

buttonaddActionListener(this);

button_exitaddActionListener(this);

thisaddWindowListener(new MyWindowListener());

thissetVisible(true);

}

chessboard()

{

}

public void actionPerformed(ActionEvent e)

{

if((Button)egetSource() == button_exit)

{

Systemexit(0);

}

else if ((Button)egetSource() == button)

{

isStart = true;

result_labelsetText("游戏进行中");

for(i = 0;i < 17;i ++)

{

for(j = 0;j < 17;j ++)

{

chessboardchess_state[i][j] = 0;

}

}

drawrepaint();

}

}

public void mouseClicked(MouseEvent e)

{

}

public void mouseEntered(MouseEvent e)

{

}

public void mousePressed(MouseEvent e)

{

chessboard chess = new chessboard();

if(chesscoordinate_x <= 640 && chesscoordinate_y <= 640 && chesscoordinate_x >= 0 && chesscoordinate_y >= 0 )

{

drawrepaint();

}

}

public void mouseReleased(MouseEvent e)

{

if(isStart == false)

{

result_labelsetText(drawChessBoardstr);

}

//电脑落子调用mouseReleased

/

if(chessboardisComputer)

{

drawrepaint();

}

/

}

public void mouseExited(MouseEvent e)

{

}

public void mouseDragged(MouseEvent e)

{

// Systemexit(0);

}

public void mouseMoved(MouseEvent e){

Integer integ1,integ2 ;

chessboardisChange = true;

coordinate_x = egetX();

coordinate_y = egetY();

// integ1 = new Integer(coordinate_x / 40);

// integ2 = new Integer(coordinate_y / 40);

// result_labelsetText(integ1toString()+" "+integ2toString());

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)

{

Systemexit(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(chessboardchess_state[row][col] != 0)

{

//水平方向判断

for(index = 0;index < 5;index ++)

{

//向落子点右边扫描

while(isOneside)

{

if(n == 0) count = 1;

if(row + count <= 16 && chessboardchess_state[ row + count][ col ] == chessboardchess_state[ row ][ col ] )

{

flag ++;

n ++;

count ++;

}

else

{

isOneside = false;

n = 0;

break;

}

}

//向落子点左边扫面

while(!isOneside)

{

if(n == 0) count = 1;

if(row - count >= 0 && chessboardchess_state[ row - count ][ col ] == chessboardchess_state[ row ][ col ] && !isOneside && row > 1)

{

flag ++;

count ++;

n ++;

}

else

{

isOneside = true;

n = 0;

break;

}

}

//如果左边没有落子或者落了颜色不同的子

if(flag >= 5)

{

chessboardisStart = false;

if(chessboardchess_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 && chessboardchess_state[ row ][ col + count ] == chessboardchess_state[ row ][ col ] )

{

flag ++;

n ++;

count ++;

}

else

{

isOneside = false;

n = 0;

break;

}

}

//向落子点左边扫面

while(!isOneside)

{

if(n == 0) count = 1;

if(col - count >= 0 && chessboardchess_state[ row ][ col - count ] == chessboardchess_state[ row ][ col ] && !isOneside )

{

flag ++;

n ++;

count ++;

}

else

{

isOneside = true;

n = 0;

break;

}

}

//如果左边没有落子或者落了颜色不同的子

if(flag >= 5)

{

chessboardisStart = false;

if(chessboardchess_state[row][col] == 1)

{

return "黑子赢了";

}

else if(chessboardchess_state[row][col] == 2)

{

return "白子赢了";

}

}

if(flag < 5)

flag = 1;

}

//斜向上方向判断

for(index = 0;index < 5;index ++)

{

//向落子点右边扫描

while(isOneside)

{

if(n == 0) count = 1;

if( chessboardchess_state[ row + count][ col + count ] == chessboardchess_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 && chessboardchess_state[ row - count][ col - count ] == chessboardchess_state[ row ][ col ] && !isOneside)

{

flag ++;

count ++;

n ++;

}

else

{

isOneside = true;

n = 0;

break;

}

}

//如果左边没有落子或者落了颜色不同的子

if(flag >= 5)

{

chessboardisStart = false;

if(chessboardchess_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 && chessboardchess_state[ row - count][ col + count] == chessboardchess_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 && chessboardchess_state[ row + count][ col - count ] == chessboardchess_state[ row ][ col ] && !isOneside)

{

flag ++;

count ++;

n ++;

}

else

{

isOneside = true;

n = 0;

break;

}

}

//如果左边没有落子或者落了颜色不同的子

if(flag >= 5)

{

chessboardisStart = false;

if(chessboardchess_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();

//winjudgewinJudge(row, col);

}

}

以上就是关于用C语言编写一个五子棋的游戏程序全部的内容,包括:用C语言编写一个五子棋的游戏程序、C语言编写五子棋的程序能给我一个么、五子棋的程序设计.等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10047229.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存