C语言井字游戏
发布网友
发布时间:2022-04-19 14:24
我来回答
共2个回答
热心网友
时间:2023-07-06 05:20
http://www.pudn.com/downloads112/sourcecode/game/49636984jzGame.rar
还有这个
井字游戏:为双人对弈游戏,双方轮流放子,单任一行,列,斜线为三个相同的 子时即为胜利!
http://download.pudn.com/downloads63/sourcecode/game/57578890xtkdige.rar
还有
http://download.pudn.com/downloads76/sourcecode/game/55593407402922602.rar
还有这个
一个用c语言写的“井字游戏”,经过turbo c编译通过。
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<bios.h>
#include<alloc.h>
#include<stdlib.h>
#define x1 150
#define x2 250
#define x3 350
#define x4 450
#define y1 100
#define y2 200
#define y3 300
#define y4 400
/* 1表示O, 2表示X */
void *buf_yuan,*buf_cha,*buf;
FILE *fp;
int a[4][4];
int flag=0;
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void main(void)
{
int gd=DETECT,gm;
int END=0;
int i,j,h,kai=0,restart,key;
int heng=2,lie=2;
int temp=1;
void SystemInitial(void);/*初始化游戏界面*/
void SiJiao(int heng,int lie);/*显示将要走步的位格的边框*/
void hua(int heng,int lie,int type);/*走步,即画圆或画叉*/
void display(int type);/*显示谁赢了*/
void hz(int x,int y,int a,int COL,int b,char *s);/*显汉字,只可以显示中文*/
void computer(void);/*单人游戏时,电脑走步*/
void hanzi(int x,int y,char *p,int colour);/*也是显汉字,可以中文与英语混合显示*/
initgraph(&amt;gd,&amt;gm,"");
if ((fp=fopen("hzk16","rb"))==NULL)
{
printf("Can't open hzk16,Please add it");
getch();
closegraph();
exit(0);
}
cleardevice();
hz(220,100,40,2,YELLOW,"井字游戏");
setcolor(BLUE);
hanzi(400,460,"作者:04计科3班 邓永华 08号",BLUE);
setcolor(WHITE);
setfillstyle(SOLID_FILL,WHITE);
flag=0;
if(flag==0)/*单人游戏*/
{
circle(195,242,4);
floodfill(195,242,WHITE);
}
else/*双人游戏*/
{
circle(195,262,4);
floodfill(195,262,WHITE);
}
outtextxy(200,220,"Please select:");
setcolor(RED);
outtextxy(205,240,"Play with computer");
outtextxy(205,260,"Two people play");
hanzi(30,350,"游戏说明:双方轮流走步,先连成三个符号一直线(横、竖、斜)就Win",GREEN);
while(temp)
{
switch(bioskey(0))
{
case 0x1c0d:/*回车*/
{
temp=0;
break;
}
case 0x11b:/*Esc*/
exit(0);
case 0x4800:/*上*/
if(flag==0)
break;
else
{
flag=0;
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
circle(195,262,4);
floodfill(195,262,BLACK);
setcolor(WHITE);
setfillstyle(SOLID_FILL,WHITE);
circle(195,242,4);
floodfill(195,242,WHITE);
}
break;
case 0x5000:/*下*/
if(flag==1)
break;
else
{
flag=1;
/*清除*/
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
circle(195,242,4);
floodfill(195,242,BLACK);
setcolor(WHITE);
setfillstyle(SOLID_FILL,WHITE);
circle(195,262,4);
floodfill(195,262,WHITE);
break;
}
}
}
cleardevice();
/*begin to play*/
SystemInitial();
while(END!=1)
{
restart=0;
switch(bioskey(0))/*按键*/
{
case 0x11b:/*Esc退出*/
END=1;
break;
case 0x3920:/*space*/
if(kai==1)
break;
if(a[heng][lie]) break;
kai=1;
hua(heng,lie,1);
a[heng][lie]=1;
if((a[1][1]==1&amt;&amt;a[1][2]==1&amt;&amt;a[1][3]==1)/*判断是否赢了*/
||(a[2][1]==1&amt;&amt;a[2][2]==1&amt;&amt;a[2][3]==1)
||(a[3][1]==1&amt;&amt;a[3][2]==1&amt;&amt;a[3][3]==1)
||(a[1][1]==1&amt;&amt;a[2][1]==1&amt;&amt;a[3][1]==1)
||(a[1][2]==1&amt;&amt;a[2][2]==1&amt;&amt;a[3][2]==1)
||(a[1][3]==1&amt;&amt;a[2][3]==1&amt;&amt;a[3][3]==1)
||(a[1][1]==1&amt;&amt;a[2][2]==1&amt;&amt;a[3][3]==1)
||(a[1][3]==1&amt;&amt;a[2][2]==1&amt;&amt;a[3][1]==1))
{
display(1);
END=1;
}
if(!END)
{
h=0;
for(i=1;i<4;i++)
for(j=1;j<4;j++)
if(a[i][j])
h++;
if(h==9)
{
display(3);
END=1;
}
}
if(END==1)
{
outtextxy(260,450,"play again? Y/N ");
while(1)
{
key=bioskey(0);
if(key==0x1579||key==0x1559)/*y的大小写*/
{
END=0;
restart=1;
break;
}
else if((key==0x316e)||(key==0x314e))/*n的大小写*/
break;
else continue;
}
}
break;
case 0x5230:/*0*/
if(kai==2) break;
if(a[heng][lie]) break;
kai=2;
hua(heng,lie,2);
a[heng][lie]=20;
if((a[1][1]==20&amt;&amt;a[1][2]==20&amt;&amt;a[1][3]==20)/*判断是否赢了*/
||(a[2][1]==20&amt;&amt;a[2][2]==20&amt;&amt;a[2][3]==20)
||(a[3][1]==20&amt;&amt;a[3][2]==20&amt;&amt;a[3][3]==20)
||(a[1][1]==20&amt;&amt;a[2][1]==20&amt;&amt;a[3][1]==20)
||(a[1][2]==20&amt;&amt;a[2][2]==20&amt;&amt;a[3][2]==20)
||(a[1][3]==20&amt;&amt;a[2][3]==20&amt;&amt;a[3][3]==20)
||(a[1][1]==20&amt;&amt;a[2][2]==20&amt;&amt;a[3][3]==20)
||(a[1][3]==20&amt;&amt;a[2][2]==20&amt;&amt;a[3][1]==20))
{
display(2);
END=1;
}
if(!END)
{ h=0;
for(i=1;i<4;i++)
for(j=1;j<4;j++)
if(a[i][j])
h++;
if(h==9)
{
display(3);
END=1;
}
}
if(END==1)
{
outtextxy(260,450,"play again? Y/N ");
while(1)
{
key=bioskey(0);
if(key==0x1579||key==0x1559)
{
END=0;
restart=1;
break;
}
if(key==0x316e||key==0x314e)
break;
else continue;
}
}
break;
case 0x4800:
case 0x1177:
heng--;
if(heng<1)
heng=1;
SiJiao(heng,lie);
break;
case 0x5000:
case 0x1f73:
heng++;
if(heng>3)
heng=3;
SiJiao(heng,lie);
break;
case 0x4b00:
case 0x1e61:
lie--;
if(lie<1)
lie=1;
SiJiao(heng,lie);
break;
case 0x4d00:
case 0x2064:
lie++;
if(lie>3)
lie=3;
SiJiao(heng,lie);
break;
}
if(flag==0&amt;&amt;kai==1&amt;&amt;END!=1&amt;&amt;restart!=1)
{
computer();
kai=2;
if((a[1][1]==20&amt;&amt;a[1][2]==20&amt;&amt;a[1][3]==20)/*判断是否赢了*/
||(a[2][1]==20&amt;&amt;a[2][2]==20&amt;&amt;a[2][3]==20)
||(a[3][1]==20&amt;&amt;a[3][2]==20&amt;&amt;a[3][3]==20)
||(a[1][1]==20&amt;&amt;a[2][1]==20&amt;&amt;a[3][1]==20)
||(a[1][2]==20&amt;&amt;a[2][2]==20&amt;&amt;a[3][2]==20)
||(a[1][3]==20&amt;&amt;a[2][3]==20&amt;&amt;a[3][3]==20)
||(a[1][1]==20&amt;&amt;a[2][2]==20&amt;&amt;a[3][3]==20)
||(a[1][3]==20&amt;&amt;a[2][2]==20&amt;&amt;a[3][1]==20))
{
display(2);
END=1;
}
if(!END)
{ h=0;
for(i=1;i<4;i++)
for(j=1;j<4;j++)
if(a[i][j])
h++;
if(h==9)
{
display(3);
END=1;
}
}
if(END==1)
{
outtextxy(260,450,"play again? Y/N ");
while(1)
{
key=bioskey(0);
if(key==0x1579||key==0x1559)
{
END=0;
restart=1;
break;
}
if(key==0x316e||key==0x314e)
break;
else continue;
}
}
}
if(restart==1)
{
cleardevice();
SystemInitial();
kai=0;
heng=2;
lie=2;
}
}
free(buf_yuan);
free(buf_cha);
free(buf);
fclose(fp);
closegraph();
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void SystemInitial(void)
{
void hz(int x,int y,int a,int COL,int b,char *s);
void SiJiao(int heng,int lie);
int size,i,j;
for(i=1;i<4;i++)
for(j=1;j<4;j++)
a[i][j]=0;
SiJiao(2,2);
hz(240,30,40,2,YELLOW,"井字游戏");
setcolor(GREEN);
outtextxy(10,200,"1P");
setcolor(WHITE);
outtextxy(10,220,"up: w");
outtextxy(10,240,"down: s");
outtextxy(10,260,"left: a");
outtextxy(10,280,"right: d");
outtextxy(10,300,"fill: space");
outtextxy(10,320,"exit: Esc");
if(flag==1)
{
setcolor(GREEN);
outtextxy(520,200,"2P");
setcolor(WHITE);
outtextxy(520,220,"up:");
outtextxy(520,240,"down:");
outtextxy(520,260,"left: ");
outtextxy(520,280,"right: ");
outtextxy(520,300,"fill: 0");
outtextxy(520,320,"exit: Esc");
hz(585,220,25,1,WHITE,"↑");
hz(585,240,25,1,WHITE,"↓");
hz(585,260,25,1,WHITE,"←");
hz(585,280,25,1,WHITE,"→");
}
line(x1,y1,x1,y4);
line(x1,y1,x4,y1);
line(x4,y1,x4,y4);
line(x1,y4,x4,y4);
line(x2,y1,x2,y4); /*shu*/
line(x3,y1,x3,y4);
line(x1,y2,x4,y2); /*heng*/
line(x1,y3,x4,y3);
circle((x2+x3)/2,(y2+y3)/2,(y3-y2)/2-10);/*hua yuan*/
size=imagesize((x2+x3)/2-(y3-y2)/2+9,(y2+y3)/2-(y3-y2)/2+9,(x2+x3)/2+(y3-y2)/2-9,(y2+y3)/2+(y3-y2)/2-9);
buf_yuan=malloc(size);
if(!buf_yuan) exit(1);
getimage((x2+x3)/2-(y3-y2)/2+9,(y2+y3)/2-(y3-y2)/2+9,(x2+x3)/2+(y3-y2)/2-9,(y2+y3)/2+(y3-y2)/2-9,buf_yuan);
setcolor(BLACK);
circle((x2+x3)/2,(y2+y3)/2,(y3-y2)/2-10);
setcolor(WHITE); /*hua cha*/
line(x2+10,y2+10,x3-10,y3-10);
line(x2+10,y3-10,x3-10,y2+10);
buf_cha=malloc(size);
getimage((x2+x3)/2-(y3-y2)/2+9,(y2+y3)/2-(y3-y2)/2+9,(x2+x3)/2+(y3-y2)/2-9,(y2+y3)/2+(y3-y2)/2-9,buf_cha);
setcolor(BLACK);
line(x2+10,y2+10,x3-10,y3-10);
line(x2+10,y3-10,x3-10,y2+10);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void SiJiao(int heng,int lie)
{
int a1,b1,a2,b2;
void huahei(void);/*把原来用白色显示的将要走步的边框用黑色覆盖*/
huahei();
setcolor(WHITE);
switch(heng)
{
case 1:
b1=y1;
b2=y2;
break;
case 2:
b1=y2;
b2=y3;
break;
case 3:
b1=y3;
b2=y4;
break;
}
switch(lie)
{
case 1:
a1=x1;
a2=x2;
break;
case 2:
a1=x2;
a2=x3;
break;
case 3:
a1=x3;
a2=x4;
break;
}
line(a1+3,b1+3,a1+30,b1+3);
line(a1+3,b1+3,a1+3,b1+30);
line(a1+3,b2-3,a1+3,b2-30);
line(a1+3,b2-3,a1+30,b2-3);
line(a2-30,b1+3,a2-3,b1+3);
line(a2-3,b1+3,a2-3,b1+30);
line(a2-30,b2-3,a2-3,b2-3);
line(a2-3,b2-30,a2-3,b2-3);
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void huahei(void)
{
int i,j,a1,b1,a2,b2;
setcolor(BLACK);
for(i=1;i<4;i++)
for(j=1;j<4;j++)
{
switch(i)
{
case 1:
b1=y1;
b2=y2;
break;
case 2:
b1=y2;
b2=y3;
break;
case 3:
b1=y3;
b2=y4;
break;
}
switch(j)
{
case 1:
a1=x1;
a2=x2;
break;
case 2:
a1=x2;
a2=x3;
break;
case 3:
a1=x3;
a2=x4;
break;
}
line(a1+3,b1+3,a1+30,b1+3);
line(a1+3,b1+3,a1+3,b1+30);
line(a1+3,b2-3,a1+3,b2-30);
line(a1+3,b2-3,a1+30,b2-3);
line(a2-30,b1+3,a2-3,b1+3);
line(a2-3,b1+3,a2-3,b1+30);
line(a2-30,b2-3,a2-3,b2-3);
line(a2-3,b2-30,a2-3,b2-3);
}
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void hua(int heng,int lie,int type)/*type的值为1表示圆,2表示叉*/
{
int x,y;
setcolor(WHITE);
switch(lie)
{
case 1:
x=(x1+x2)/2-(x2-x1)/2+9;
break;
case 2:
x=(x2+x3)/2-(x3-x2)/2+9;
break;
case 3:
x=(x3+x4)/2-(x4-x3)/2+9;
break;
}
switch(heng)
{
case 1:
y=(y1+y2)/2-(y2-y1)/2+9;
break;
case 2:
y=(y2+y3)/2-(y3-y2)/2+9;
break;
case 3:
y=(y3+y4)/2-(y4-y3)/2+9;
break;
}
switch(type)
{
case 1:
buf=buf_yuan;
a[heng][lie]=1;
break;
case 2:
buf=buf_cha;
a[heng][lie]=20;
break;
}
putimage(x,y,buf,COPY_PUT);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void display(int type)
{
if(type==1)
outtextxy(270,430,"O Win");
if(type==2)
outtextxy(270,430,"X Win");
if(type==3)
outtextxy(270,430,"draw");
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void hz(int x,int y,int a,int COL,int b,char *s)/*x,y为显示的坐标,a为字与字之间的间隔,b为字的大小,s为指向为显示的汉字的指针*/
{
int ROW;
char buffer[32];
register m,n,i1,j1,k;
unsigned char qh,wh;
unsigned long offset;
ROW=COL;
while(*s)
{
qh=*(s)-0xa0;/*汉字区位码*/
wh=*(s+1)-0xa0;
offset=(94*(qh-1)+(wh-1))*32L;/*计算该汉字在字库中偏移量*/
fseek(fp,offset,SEEK_SET);
fread(buffer,32,1,fp);/*取出汉字32字节的点阵字模存入buffer中(一个汉字) */
for (i1=0;i1<16;i1++)/*将32位字节的点阵按位在屏幕上打印出来(1:打印,0:不打印),显示汉字 */
for(n=0;n<ROW;n++)
for(j1=0;j1<2;j1++)
for(k=0;k<8;k++)
for(m=0;m<COL;m++)
if (((buffer[i1*2+j1]>>(7-k))&amt;0x1)!=NULL)
putpixel(x+8*j1*COL+k*COL+m,y+i1*ROW+n,b);
s+=2;/*因为一个汉字内码占用两个字节,所以s必须加2*/
x+=a;
}
}
/**************************************************************************/
void computer(void)
{
int i,j;
for(i=1;i<4;i++)/*横向判断*/
if(a[i][1]+a[i][2]+a[i][3]==40)
for(j=1;j<4;j++)
if(a[i][j]==0)
{
hua(i,j,2);
return;
}
for(i=1;i<4;i++)/*横向判断*/
if(a[i][1]+a[i][2]+a[i][3]==2)
for(j=1;j<4;j++)
if(a[i][j]==0)
{
hua(i,j,2);
return;
}
for(i=1;i<4;i++)/*竖向判断*/
if(a[1][i]+a[2][i]+a[3][i]==40)
{
for(j=1;j<4;j++)
if(a[j][i]==0)
{
hua(j,i,2);
return;
}
}
for(i=1;i<4;i++)/*竖向判断*/
if(a[1][i]+a[2][i]+a[3][i]==2)
for(j=1;j<4;j++)
if(a[j][i]==0)
{
hua(j,i,2);
return;
}
if(a[1][1]+a[2][2]+a[3][3]==40)/*左上角到右下角判断*/
{
for(i=1;i<4;i++)
if(a[i][i]==0)
{
hua(i,i,2);
return;
}
}
else if(a[1][1]+a[2][2]+a[3][3]==2)
for(i=1;i<4;i++)
if(a[i][i]==0)
{
hua(i,i,2);
return;
}
if(a[3][1]+a[2][2]+a[1][3]==40)/*右上角到左下角判断*/
{
for(i=1;i<4;i++)
if(a[i][4-i]==0)
{
hua(i,4-i,2);
return;
}
}
else if(a[3][1]+a[2][2]+a[1][3]==2)
for(i=1;i<4;i++)
if(a[i][4-i]==0)
{
hua(i,4-i,2);
return;
}
for(i=1;i<4;i++)/*随便找到空位填上去*/
for(j=1;j<4;j++)
if(a[i][j]==0)
{
hua(i,j,2);
return;
}
}
void hanzi(int x,int y,char *p,int colour)
{
FILE *fp;
char buffer[32];
register i,j,k;
unsigned char qh,wh;
unsigned long location;
if((fp=fopen("hzk16","rb"))==NULL)
{
printf("Can't open hzk16!");
getch();
exit(0);
}
while(*p)
{
if(((unsigned char)*p>=0xa1&amt;&amt;(unsigned char)*p<=0xfe)&amt;&amt;((unsigned char)*(p+1)>=0xal&amt;&amt;(unsigned char)*(p+1)<=0xfe))
{
qh=*p-0xa0;
wh=*(p+1)-0xa0;
location=(94*(qh-1)+(wh-1))*32L;
fseek(fp,location,SEEK_SET);
fread(buffer,32,1,fp);
for(i=0;i<16;i++)
for(j=0;j<2;j++)
for(k=0;k<8;k++)
if(((buffer[i*2+j]>>(7-k))&amt;0x1)!=NULL)
putpixel(x+8*j+k,y+i,colour);
p+=2;
x+=18;
if(x>600)
{
x=15;y+=18;
}
}
else
{
char q[2];
moveto(x,y);
*q=*p;
*(q+1)='\0';
outtextxy(x,y+4,q);
x+=8+1;p++;
}
}
fclose(fp);
}
都是井字游戏,C语言的,你参考看看。
热心网友
时间:2023-07-06 05:20
把整个游戏贴出来啊
c语言井字游戏的问题求助!
错误集中在下面这段语句,即输入下棋位置的合理性判断。do { printf("Player %d,please enter where you want to put your %c:",player,(player==1)?'x':'o');scanf("%d",&choice);row=--choice/3;column=--choice%3; // 第一处错误 }while(i<1||i>9||board[row][column]>'...
C语言井字游戏
井字游戏:为双人对弈游戏,双方轮流放子,单任一行,列,斜线为三个相同的 子时即为胜利!http://download.pudn.com/downloads63/sourcecode/game/57578890xtkdige.rar还有http://download.pudn.com/downloads76/sourcecode/game/55593407402922602.rar还有这个 一个用c语言写的“井字游戏”,经过turbo c编译通过。 #inc...
dev c++中的 井字游戏 问题
.cpp的编译过程支持for(int i=0;i<n;i++)这种格式,而在.c的编译过程不支持。而且前者的生存期仅在自己的循环以内内部局部变量有效,跨循环就等于重新定义了局部变量了。
player=i%2+1怎么理解
1.i%2 是i/2的余数,所以i是偶数时,playe=1;i是奇数时,player=2;2.choice/3是choice除以3后的整数部分,choice%3是choice除以3后的余数部分,两者分别表示当前的行数与列数。例如:choic=5,表示1行2列 注意第1行之前是否有第0行 choice是否从0开始计数 00 01 02 0 1 2 10 11 12...