❶ C語言井字游戲
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語言的,你參考看看。
❷ C++ 井字棋
好了 所有要求都有了
#include <iostream>
#include <string>
using namespace std;
const int MAX_MOVES=9;
void printBoard(char board[3][3]);
void getNextMove(char board[3][3]);
void win(char board[3][3]);
void whowin(char key);
int foot = 0;
int main()
{
int i;
int n = 0;
char board[3][3]={ '7','8','9', '4','5','6', '1','2','3' };
printBoard(board);
for(i=0;i<MAX_MOVES;i++)
{
getNextMove(board);
win(board);
n++;
}
if(n=MAX_MOVES)
cout<<"gameover";
system("PAUSE");
return 0;
}
void win(char a[3][3])
{
int i, j, k;
char key;
if((a[0][0] == a[1][1] && a[1][1] == a[2][2] && a[2][2] == a[0][0]) || (a[2][0] == a[1][1] && a[1][1] == a[0][2] && a[2][0] == a[0][2]))
{
key = a[0][0];
whowin(key);
}
else
{
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
if(a[i][j] == 15 || a[i][j] == 'X')
{
key = a[i][j];
for(k = 0; k < 3; k++)
{
if(a[i][k] != key)
break;
}
if(k == 3)
whowin(key);
for(k = 0; k < 3; k++)
{
if(a[k][j] != key)
break;
}
if(k == 3)
whowin(key);
}
}
}
void whowin(char key)
{
if(key == 15)
cout << "P1 Win!" << endl;
else
cout << "P2 Win!" << endl;
exit(0);
}
void printBoard(char board[3][3])
{
cout<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<endl;
cout<<"--+---+--"<<endl;
cout<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<endl;
cout<<"--+---+--"<<endl;
cout<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<endl;
}
void getNextMove(char board[3][3])
{
int i,j,s,t;
char m;
while(1)
{
s = -1;
t = -1;
cout<<"Make your move: ";
cin>>m;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(board[i][j]==m)
{
s=i;
t=j;
}
if(s == -1 && t == -1)
cout << "that point already has a pawn!" << endl;
else
break;
}
if(foot % 2 == 0)
board[s][t] = 15;
else
board[s][t] = 'X';
foot++;
system("cls");
printBoard(board);
}
❸ 用c語言寫井字棋游戲 求教一個問題
用char數組存棋盤,並記錄當前是玩家1還是玩家2。
如果用戶輸入0,首先判斷當前玩家是1還是2。如果是1,就board[0] = 'O';否則board[0] = 'X'。
偽代碼就類似於:
charboard[10]="";//初始為9個空格,並且多留一個位置放'