當前位置:首頁 » 編程語言 » c語言黑白棋

c語言黑白棋

發布時間: 2022-12-15 17:58:26

1. c語言 人人對戰黑白棋 求邏輯過程~~~

你既然用二維數組,那麼為什麼要輸入2A這個東西給你自己添堵呢?可以直接輸入兩個數字啊!
把二維數組當成XY坐標,當輸入XY的時候把a[X][Y]=『w』或者『b』就行了,一個函數就可以搞定。對於a[x][y]是等於w呢還是y。可以利用自動機思想解決。
#define
W
1
#define
B
0
int
state;
state
=
B;
while(1)
{
if
(state
==
B)
{
a[x][y]
=
'b';
state
=
W;
}
else
if
(state
==
W)
{
a[x][y]='w';
state
=
B;
}
}
至於每次輸出不一樣,調用system(「cls」)清屏就OK了。在windows.h中
清楚屏幕,然後在輸出一下就行了
可以利用指針在兩個函數直接傳遞值。
其他的應該比較好解決。

2. 關於用C語言編寫的小游戲的游戲代碼,如黑白棋貪吃蛇等

我這兒有c語言的自寫俄羅斯方塊,請指教:#include
#include
#include
#include
#include
#include
#include

#define ESC 0x011b
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define SPACE 0x3920
#define Y 0x1579
#define N 0x316e
#define clearkbd(); while(bioskey(1)) bioskey(0); /*清空鍵盤緩沖隊列*/

void update();
void messagebox();
void process();
void initremove();
void initinfo();
void initbox();
void initposition();
void next_shape();

typedef struct shape /*形狀單一狀態的記錄*/
{ int attr;
int co[8];
}shape;

typedef struct RE_AB /*相對,絕對坐標記錄*/
{ int Rx,Ry;
int x1,x2,y1,y2;
}RE_AB;

RE_AB RA;

shape p[19]={ { RED,0,1,1,0,1,1,2,1 }, /*數組中保證y最大的在最後,以便initposition使用*/
{ RED,0,1,1,0,1,1,1,2 },
{ RED,0,0,1,0,2,0,1,1 },
{ RED,0,0,0,1,1,1,0,2 },
{ GREEN,0,0,1,0,2,0,3,0 },
{ GREEN,0,0,0,1,0,2,0,3 },
{ CYAN,0,0,0,1,1,0,1,1 },
{ BROWN,0,0,1,0,1,1,2,1 },
{ BROWN,1,0,0,1,1,1,0,2 },
{ BLUE,1,0,2,0,1,1,0,1 },
{ BLUE,0,0,0,1,1,1,1,2 },
{ MAGENTA,0,0,0,1,0,2,1,2 },
{ MAGENTA,2,0,0,1,1,1,2,1},
{ MAGENTA,0,0,1,0,1,1,1,2 },
{ MAGENTA,0,0,0,1,1,0,2,0 },
{ YELLOW,0,2,1,0,1,1,1,2 },
{ YELLOW,0,0,1,0,2,0,2,1 },
{ YELLOW,1,0,0,0,0,1,0,2},
{ YELLOW,0,0,0,1,1,1,2,1 },
};
int nback,nleft,nright,r_f[12][22],rs1,rs2,xcors,xcorb,ycors,ycorb;
/*檢查方快有沒有左,右,下接觸,游戲區內所有格子有無顏色記錄數組,rs1形狀記錄,rs2為提示框用,記錄小格子在游戲區中的位置,按鍵存儲*/

void interrupt (*oldint)(); /*系統定時中斷*/
int count_down=0,count_other=0; /*中斷記時*/

void interrupt newint() /*設置新的中斷程序*/
{ count_down++;
count_other++;
oldint();
}

void intenable() /*設置中斷向量表,啟動新的中斷程序*/
{ oldint=getvect(0x1c);
disable();
setvect(0x1c,newint);
enable();
}

void intrestore() /*恢復中斷向量*/
{ disable();
setvect(0x1c,oldint);
enable();
}

void HZ12(int x0,int y0,int w,int color,char *s) /*根據字模,在dos下顯示漢字*/
/*橫坐標,縱坐標,字間隔,漢字顏色,漢字字元串*/
{ FILE *fp;
register char buffer[24];
register char str[2];
unsigned long fpos;/*fpos為最終偏移動量*/
register int i,j,k;
fp=fopen(hzk12,r);/*打開12*12漢字苦*/
while(*s)/*一直到字元串結束為止*/
{
if(*s<0)/*漢字輸出*/
{ str[0]=(*s)-0xa0;
str[1]=*(s+1)-0xa0;
fpos=((str[0]-1)*94+(str[1]-1))*24L;/*計算漢字在hzk12的偏移量*/
fseek(fp,fpos,SEEK_SET);/*指針移動到當前位置*/
fread(buffer,24,1,fp);/*讀取一個漢字到數組中*/
for(i=0;i<12;i++)/*12行*/
for(j=0;j<2;j++)/*兩個位元組*/
for(k=0;k<8;k++)/*8位*/
if (((buffer[i*2+j]>>(7-k))&0x1)!=NULL)/*是一就畫點*/
putpixel(x0+8*j+k,y0+i,color);
s+=2;/*一個漢字占兩個位元組,現在將指針移動兩個位元組*/
x0+=w;/*顯示坐標也按照間隔移動*/
}
else/*顯示非漢字字元*/
{ settextstyle(0,0,1);
setcolor(color);
str[0]=*s;str[1]=0;
outtextxy(x0,y0+3,str);/*顯示單個字元*/
x0+=w-7;/*顯示單個字元後的x坐標變化*/
s++;/*指針移動到下一個位元組*/
}
}
fclose(fp);
}

void translation() /*把相對坐標解釋為絕對坐標*/
{ if(RA.Rx==1)
{ RA.x1=1; RA.x2=16; }
else
{ RA.x1=16*(RA.Rx-1); RA.x2=16*RA.Rx; }
if(RA.Ry==1)
{ RA.y1=1; RA.y2=16; }
else
{ RA.y1=16*(RA.Ry-1); RA.y2=16*RA.Ry; }
}

int check_b() /*檢查是否到達低部*/
{ int x,y,i,zf=0; /*zf為是否有顏色填充記錄*/
for(i=0;i<7;i++,i++)
{ x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>=6)
zf+=r_f[x-15][y-6+1];
}
if(zf==0)
return 1;
else
return 0;
}

int finish()
{ int tfull=0,i; /*判斷頂層空間是否有填充*/
for(i=1;i<11;i++)
tfull+=r_f[i][1];
if(tfull!=0)
return 1; /*告訴judge()可以結束了*/
}

int check_l() /*檢查形狀是否與左接觸*/
{ int x,y,i,zf=0;
for(i=0;i<7;i++,i++)
{ x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>6)
zf+=r_f[x-15-1][y-6];
if(y<=6&&x==16)
zf+=1;
}
if(zf==0)
return 1;
else
return 0;
}

int check_r() /*檢查形狀是否與右接觸*/
{ /*zf為是否有顏色填充記錄*/
int x,y,i,zf=0; /*zf為是否有顏色填充記錄*/
for(i=0;i<7;i++,i++)
{
x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>6)
zf+=r_f[x-15+1][y-6];
if(y<=6&&x==25)
zf+=1;
}
if(zf==0)
return 1;
else
return 0;
}

void check_touch()
{ nback=check_b();
nleft=check_l();
nright=check_r();
}

void draw(int cb) /*畫形狀,cb=1以填充色畫形狀,cb=2以背景色畫形狀,cb=3以白色畫形狀*/
{ int i,recordx=RA.Rx,recordy=RA.Ry;
for(i=0;i<7;i++,i++)
{ RA.Rx+=p[rs1].co[i];
RA.Ry+=p[rs1].co[i+1];
if(RA.Ry<=6)
{ RA.Rx=recordx;
RA.Ry=recordy;
continue;
}
translation();
if(cb==1)
setfillstyle(1,p[rs1].attr);
else
if(cb==2)
setfillstyle(1,BLACK);
else
if(cb==3)
{ setfillstyle(1,WHITE);
r_f[RA.Rx-15][RA.Ry-6]=1; /*置對應數組標記元素*/
}

bar(RA.x1+1,RA.y1+1,RA.x2-1,RA.y2-1);
RA.Rx=recordx;
RA.Ry=recordy;
}
}

void mov(int key) /*向下,左,右移動方塊*/
{ draw(2);
if(key==LEFT&&nleft)
RA.Rx--;
else
if(key==RIGHT&&nright)
RA.Rx++;
else
RA.Ry++;
nback=check_b();
if(nback) /*判斷形狀有沒有到達底部,有就將其顏色變為白色*/
draw(1);
else
draw(3);
}

void change() /*變換形狀*/
{ int status=rs1,buffer,i,x,y,zf=0;
if(p[rs1].attr==p[rs1+1].attr)
rs1++;
else
while(p[rs1].attr==p[rs1-1].attr)
rs1--;

for(i=0;i<7;i++,i++) /*檢查變化形狀後是否與已存形狀發生沖突*/
{ x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>6)
zf+=r_f[x-15][y-6];
}
if(zf!=0)
rs1=status;

buffer=rs1;
rs1=status;
status=buffer;
draw(2);

buffer=rs1;
rs1=status;
status=buffer;

nback=check_b(); /*判斷變化後的形狀是不是到達了低部,這個檢查是十分必要的*/
if(nback)
draw(1);
else
draw(3);
}

void accelerate()
{ if(count_down>=1)
{ check_touch(); /*消除上一步動作對方塊狀態的影響*/
count_down=0;
if(nback) /*0表示到達底部,1表示沒有到達*/
mov(DOWN);
}
}

void drawbox() /*畫方塊所在方框*/
{ int xcor,ycor;
for(xcor=xcors;xcor<=xcorb;xcor++)
for(ycor=ycors;ycor<=ycorb;ycor++)
{ if(xcor==xcors||xcor==xcorb||ycor==ycors||ycor==ycorb)
{ RA.Rx=xcor;
RA.Ry=ycor;
translation();
setfillstyle(1,DARKGRAY);
bar(RA.x1+1,RA.y1+1,RA.x2-1,RA.y2-1);
}
}
}

void erasure(int k)
{ int i,j,recordx=RA.Rx,recordy=RA.Ry;
{ j=k-1;
for(;j>0;j--)
{ for(i=1;i<11;i++)
{ r_f[i][j+1]=r_f[i][j];
RA.Rx=i+15;
RA.Ry=j+1+6;
translation();
if(r_f[i][j+1]==1)
setfillstyle(1,WHITE);
else
setfillstyle(1,BLACK);
bar(RA.x1+1,RA.y1+1,RA.x2-1,RA.y2-1);
RA.Rx=recordx;
RA.Ry=recordy;
}
}
}
}

void pause()
{ HZ12(450,400,15,BLACK,正常);
HZ12(450,400,15,GREEN,暫停);
for(;;)
if(bioskey(1)&&bioskey(0)==SPACE)
{ clearkbd();
HZ12(450,400,15,BLACK,暫停);
HZ12(450,400,15,RED,正常);
return;
}
}

void judge()
{ int i,j,full=0; /*full等於10說明某一行滿,該消除了*/
if(finish()) /*判斷游戲是否該結束了*/
messagebox(); /*win編程里有這個函數*/
for(j=1;j<21;j++) /*判斷某一行是否滿了*/
{ for(i=1;i<11;i++)
full+=r_f[i][j];
if(full==10)
erasure(j); /*消除這行*/
full=0;
}
}

void update() /*使程序可以重新運行*/
{ cleardevice();
setbkcolor(BLACK);
initinfo(); /*提示信息初始化*/
initbox(); /*游戲框架初始化*/
srand((unsigned)time(NULL)); /*隨機器函數的初始化*/
rs1=random(19);
rs2=random(19);
next_shape();
initposition(); /*方塊最開始的出現位置*/
initremove(); /*記錄每個方格有無顏色填充數組初始化*/
HZ12(450,400,15,RED,正常);
process();
}

void EXIT()
{ closegraph();
intrestore(); /*恢復中斷向量*/
exit(0);
}

void initremove()
{ int i,j;
for(i=0;i<12;i++)
for(j=0;j<22;j++)
if(i==0||i==11||j==0||j==21)
r_f[i][j]=1;
else
r_f[i][j]=0;
}

void initinfo()
{ char aStr[2];
setcolor(RED);
outtextxy(450,100,This game's writer is:);
HZ12(450,140,15,RED,該程序作者:NULL);
outtextxy(525,110,NULL);
outtextxy(450,180,FUNCTION FOR KEYS:);
outtextxy(450,200,UP:change the shape);
outtextxy(450,210,DOWN:accelerate);
outtextxy(450,220,LEFT:move left);
outtextxy(450,230,RIGHT:move right);
outtextxy(450,240,ESC:exit this game);
outtextxy(450,250,SPACE:pause);
HZ12(450,260,20,RED,上:);
HZ12(450,280,20,RED,下:);
HZ12(450,300,20,RED,左:);
HZ12(450,320,20,RED,右:);
HZ12(450,340,20,RED,ESC:退出);
HZ12(450,360,15,RED,空格: 暫停/開始);
HZ12(450,380,15,RED,目前狀態:);
HZ12(20,200,15,RED,下一個形狀);

aStr[0]=24;
aStr[1]=0;
aStr[6]=0;
HZ12(480,260,12,GREEN,aStr);
HZ12(500,260,12,GREEN,( 變形 ));
aStr[0]=25;
aStr[1]=0;
HZ12(480,280,12,GREEN,aStr);
HZ12(500,280,12,GREEN,( 加速 ));
aStr[0]=27;
aStr[1]=0;
HZ12(480,300,12,GREEN,aStr);
HZ12(500,300,12,GREEN,向左);
aStr[0]=26;
aStr[1]=0;
HZ12(480,320,12,GREEN,aStr);
HZ12(500,320,12,GREEN,向右);
}

void messagebox()
{ int key;
setcolor(GREEN);
setfillstyle(1,DARKGRAY);
rectangle(220,200,420,300);
bar(221,201,419,299);
HZ12(280,210,15,GREEN,GAME OVER);
HZ12(275,230,15,GREEN,重新游戲: Y);
HZ12(275,270,15,GREEN,退出遊戲: N);
HZ12(450,400,15,BLACK,正常);
HZ12(450,400,15,GREEN,GAME OVER);
for(;;)
if(bioskey(1))
{ key=bioskey(0);
if(key==Y)
{ clearkbd();
update();
}
else
if(key==N)
{ clearkbd();
EXIT();
}
else
clearkbd();
}
}

void initbox()
{ xcors=15; /*畫游戲框*/
xcorb=26;
ycors=6;
ycorb=27;
drawbox();

xcors=2; /*畫提示框*/
xcorb=7;
ycors=6;
ycorb=11;
drawbox();
}

void initposition()
{ RA.Rx=18;
RA.Ry=6-p[rs1].co[7];;
RA.x1=0;
RA.x2=0;
RA.y1=0;
RA.y2=0;
}

void next_shape() /*畫下一形狀提示框*/
{ int recordx=RA.Rx,recordy=RA.Ry,buffer;
RA.Rx=3;
RA.Ry=7;
draw(2);

buffer=rs1;
rs1=rs2;
rs2=buffer;

draw(1);
RA.Rx=recordx;
RA.Ry=recordy;

buffer=rs1;
rs1=rs2;
rs2=buffer;
}

void process() /*游戲過程*/
{ for(;;)
{ check_touch();
if(!nback)
{ rs1=rs2;
rs2=random(19); /*產生另一種方塊的碼數*/
initposition();
judge(); /*判斷某一行是否滿了和這個游戲是否可以結束了*/
draw(1);
next_shape();
}

if(count_other>=1)
{ count_other=0;
if(bioskey(1)) /*對按鍵的處理*/
{ int key=bioskey(0);
clearkbd(); /*清除鍵盤緩沖隊列*/
if(key==ESC)
EXIT();
if(key==LEFT&&nleft&&nback)
mov(LEFT);
if(key==RIGHT&&nright&&nback)
mov(RIGHT);
if(key==UP&&nback)
change();
if(key==SPACE)
pause();
if(key==DOWN)
accelerate();
}
}

if(count_down>=4)
{ check_touch(); /*消除上一步動作對方塊狀態的影響*/
count_down=0;
if(nback) /*0表示到達底部,1表示沒有到達*/
mov(DOWN);
}
}/*for*/
}

main()
{ int gdriver=DETECT,gmode=0;
initgraph(&gdriver,&gmode,d:turboc); /*啟動圖形與中斷部分*/
intenable();
update();
}

3. 解答關於「用C語言編寫黑白棋的演算法」的問題

#include"stdio.h"
#include"windows.h"
#defineempty0
#definewhite-1
#defineblack1
#definea8
intboard[a][a];
intplayer;
voidInitBoard()
{
inti,j;
for(j=0;j<a;j++)
for(i=0;i<a;i++)
board[i][j]=empty;
board[4][4]=white;
board[4][3]=black;
board[3][3]=white;
board[3][4]=black;

}

voidPrintBoard()
{
inti,j;
system("CLS");
printf("----------黑白棋人人對弈程序---------- ");
printf("012345678X ");
for(i=0;i<a;i++)
{
printf("%d",1+i);
for(j=0;j<a;j++)
{
if(board[j][i]==black)
{
printf("○");
}
elseif(board[j][i]==white)
{
printf("●");
}
else
{
if(i==0)
{
if(j==0)
printf("┌");
elseif(j==7)
printf("┐");
else
printf("┬");
}
elseif(i==7)
{
if(j==0)
printf("└");
elseif(j==7)
printf("┘");
else
printf("┴");
}
else
{
if(j==0)
printf("├");
elseif(j==7)
printf("┤");
else
printf("┼");
}
}
}
printf(" ");
}
puts("Y");
}

boolExecute(intx,inty,intside)
{
board[x][y]=side;
intdirection[8][2]={{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1}};
inttx,ty;
for(inti=0;i<8;i++)
{
tx=x-direction[i][0];
ty=y-direction[i][1];
boolmid=false;
while(ty<a&&ty>=0&&tx>=0&&tx<a&&board[tx][ty]==-side)
{
mid=true;
ty-=direction[i][1];
tx-=direction[i][0];
}
if(ty<a&&ty>=0&&tx>=0&&tx<a&&board[tx][ty]==side&&mid==true)
{
while(tx!=x||ty!=y){
board[tx][ty]=side;
tx+=direction[i][0];
ty+=direction[i][1];
}
}
}
returntrue;
}

boolJudge(intx,inty,intside)
{
if(x>=0&&x<a&&y>=0&&y<a&&board[x][y]==empty)
{
intdirection[8][2]={{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1}};
inttx,ty;
for(inti=0;i<8;i++)
{
tx=x-direction[i][0];
ty=y-direction[i][1];
boolmid=false;
while(ty<a&&ty>=0&&tx>=0&&tx<a&&board[tx][ty]==-side)
{
mid=true;
ty-=direction[i][1];
tx-=direction[i][0];
}
if(ty<a&&ty>=0&&tx>=0&&tx<a&&board[tx][ty]==side&&mid==true)
{
returntrue;
}
}
}
returnfalse;
}
boolexists(intside)
{
for(inth=0;h<a;h++)
{
for(intj=0;j<a;j++)
{
if(Judge(j,h,side)==true)
{
returntrue;
}
}
}
returnfalse;
}
intwin()
{
intc=0,w=0,b=0;
for(inti=0;i<a;i++)
{
for(intj=0;j<a;j++)
if(board[i][j]==white)
w++;
elseif(board[i][j]==black)
b++;
}
c=w+b;
if(exists(black)==false&&exists(white)==false||c>=a*a)
{
if(b>w)
return1;
elseif(w>b)
return-1;
else
return2;
}
return0;
}
voidInitGame()
{
InitBoard();
player=black;
}

intmain()
{
intx,y;
InitGame();
PrintBoard();
while(1)
{
if(!exists(player)&&exists(-player)){
printf(""%s"沒有有效棋子!",player==black?"黑方":"白方");
player=-player;
continue;
}
printf("請"%s"棋手輸入棋子落點坐標(X,Y):",player==black?"黑方":"白方");
scanf("%d,%d",&x,&y);
x--;y--;
getchar();
if(Judge(x,y,player)==false)
{
printf(" 非法招法! ");
continue;
}else{
Execute(x,y,player);
}
PrintBoard();
intwinresult=win();
if(winresult==2)
{
printf("平局");
printf("輸入任意鍵結束程序...");
getchar();
break;
}
elseif(winresult==white)
{
printf("白方獲勝");
printf("輸入任意鍵結束程序...");
getchar();
break;
}
elseif(winresult==black)
{
printf("黑方獲勝");
printf("輸入任意鍵結束程序...");
getchar();
break;
}
player=-player;
}
return0;
}

4. 使用c語言製作游戲,如:貪吃蛇、黑白棋、推箱子等

# include<stdio.h>
# include<string.h>
# include<stdlib.h>
# define SPA 0
# define MAN 1
# define COM 2 /* 空位置設為0 ,玩家下的位置設為1 ,電腦下的位置設為2 */
int qipan[15][15]; /* 15*15的棋盤 */
int a,b,c,d,x; /* a b為玩家下子坐標 ,c d為電腦下子坐標 x為剩餘空位置*/
void start(); /* 程序的主要控制函數 */
void draw(); /* 畫棋盤 */
int win(int p,int q); /* 判斷勝利 p q為判斷點坐標 */
void AI(int *p,int *q); /* 電腦下子 p q返回下子坐標 */
int value(int p,int q); /* 計算空點p q的價值 */
int qixing(int n,int p,int q); /* 返回空點p q在n方向上的棋型 n為1-8方向 從右順時針開始數 */
void yiwei(int n,int *i,int *j); /* 在n方向上對坐標 i j 移位 n為1-8方向 從右順時針開始數 */
void main()
{
char k;
do{
x=225;
start();
printf("還要再來一把嗎?輸入y或n:"); getchar(); scanf("%c",&k);
while(k!='y'&&k!='n'){
printf("輸入錯誤,請重新輸入\n"); scanf("%c",&k); }
system("cls"); }while(k=='y'); printf("謝謝使用!\n");
}
void start()
{
int i,j,a1,b1,c1,d1,choice; /* a1 b1儲存玩家上手坐標 c1 d1儲存電腦上手坐標 */
char ch;
printf("\t╔══════════════════════════════╗\n"); printf("\t║ ║\n"); printf("\t║ 歡迎使用五子棋對戰程序 祝您玩的愉快挑戰無極限 ║\n"); printf("\t║ ║\n"); printf("\t║ ._______________________. ║\n"); printf("\t║ | _____________________ | ║\n"); printf("\t║ | I I | ║\n"); printf("\t║ | I 五 子 棋 I | ║\n"); printf("\t║ | I I | ║\n"); printf("\t║ | I made by 曉之蓬 I | ║\n"); printf("\t║ | I___________________I | ║\n"); printf("\t║ !_______________________! ║\n"); printf("\t║ ._[__________]_. ║\n"); printf("\t║ .___|_______________|___. ║\n"); printf("\t║ |::: ____ | ║\n"); printf("\t║ | ~~~~ [CD-ROM] | ║\n"); printf("\t║ !_____________________! ║\n"); printf("\t║ ║\n"); printf("\t║ ║\n"); printf("\t║ 寒 星 溪 月 疏 星 首,花 殘 二 月 並 白 蓮。 ║\n"); printf("\t║ 雨 月 金 星 追 黑 玉,松 丘 新 宵 瑞 山 腥。 ║\n"); printf("\t║ 星 月 長 峽 恆 水 流,白 蓮 垂 俏 雲 浦 嵐。 ║\n"); printf("\t║ 黑 玉 銀 月 倚 明 星,斜 月 明 月 堪 稱 朋。 ║\n"); printf("\t║ 二 十 六 局 先 棄 二,直 指 游 星 斜 彗 星。 ║\n"); printf("\t║ ║\n"); printf("\t║ ║\n"); printf("\t║ 1.人機對戰 2.人人對戰 ║\n"); printf("\t║ ║\n"); printf("\t╚═══════════════════════════ ══╝\n"); printf("\t\t\t請輸入1或2:");
scanf("%d",&choice); /* 選擇模式:人機或人人 */
while(choice!=1&&choice!=2) {
printf("輸入錯誤,請重新輸入:"); scanf("%d",&choice); }
if(choice==1){ /* 人機模式 */
system("cls");
printf("歡迎使用五子棋人機對戰!下子請輸入坐標(如13 6)。悔棋請輸入15 1 5。\n\n\n");
for(j=0;j<15;j++)
for(i=0;i<15;i++)
qipan[j][i]=SPA; /* 置棋盤全為空 */
draw();
printf("先下請按1,後下請按2:"); scanf("%d",&i);
while(i!=1&&i!=2) { printf("輸入錯誤,請重新輸入:"); scanf("%d",&i); }
if(i==1) { /* 如果玩家先手下子 */
printf("請下子:"); scanf("%d%d",&a,&b);
while((a<0||a>14)||(b<0||b>14)) {
printf("坐標錯誤!請重新輸入:"); scanf("%d%d",&a,&b); }
a1=a; b1=b; x--; qipan[b][a]=MAN; system("cls"); draw();
}
while(x!=0){
if(x==225) {
c=7; d=7; qipan[d][c]=COM; x--; system("cls"); draw(); } /* 電腦先下就下在7 7 */
else { AI(&c,&d); qipan[d][c]=COM; x--; system("cls"); draw(); } /* 電腦下子 */
c1=c; d1=d; /* 儲存電腦上手棋型 */
if(win(c,d)){ /* 電腦贏 */
printf("要悔棋嗎?請輸入y或n:"); getchar(); scanf("%c",&ch);
while(ch!='y'&&ch!='n') { printf("輸入錯誤,請重新輸入:");
scanf("%c",&ch); }
if(ch=='n') {
printf("下不過電腦很正常,請不要灰心!!!\n"); return; }
else { x+=2; qipan[d][c]=SPA; qipan[b1][a1]=SPA;
system("cls"); draw(); } /* 悔棋 */
}
printf("電腦下在%d %d\n請輸入:",c,d);
scanf("%d%d",&a,&b); /* 玩家下子 */
if(a==15&&b==15) {
x+=2; qipan[d][c]=SPA; qipan[b1][a1]=SPA; system("cls"); draw();
printf("請輸入:"); scanf("%d%d",&a,&b); } /* 悔棋 */
while((a<0||a>14)||(b<0||b>14)||qipan[b][a]!=SPA) {
printf("坐標錯誤或該位置已有子!請重新輸入:");
scanf("%d%d",&a,&b); }
a1=a; b1=b; x--; qipan[b][a]=MAN; system("cls"); draw();
if(win(a,b)){ printf("電腦神馬的都是浮雲!!!\n");
return; } /* 玩家贏 */
}
printf("和局\n");
}
if(choice==2){
system("cls");
printf("歡迎使用五子棋人人對戰!下子請輸入坐標(如13 6)。悔棋請輸入15 15。 \n\n\n");
for(j=0;j<15;j++)
for(i=0;i<15;i++)
qipan[j][i]=SPA; /* 置棋盤全為空 */
draw();
while(x!=0){
printf("1P請輸入:"); scanf("%d%d",&a,&b);
if(a==15&&b==15) {
x+=2; qipan[d][c]=SPA; qipan[b1][a1]=SPA; system("cls");
draw(); printf("1P請輸入:"); scanf("%d%d",&a,&b); }
while((a<0||a>14)||(b<0||b>14)||qipan[b][a]!=SPA) {
printf("坐標錯誤或該位置已有子!請重新輸入:");
scanf("%d%d",&a,&b); }
a1=a; b1=b; x--; qipan[b][a]=MAN; system("cls"); draw();
printf("1P下在%d %d。\n",a,b);
if(win(a,b)){ printf("你真棒!!!\n"); return; } /* 玩家1贏 */
printf("2P請輸入:"); scanf("%d%d",&c,&d);
if(c==15&&d==15) {
x+=2; qipan[b][a]=SPA; qipan[d1][c1]=SPA; system("cls"); draw();
printf("2P請輸入:"); scanf("%d%d",&c,&d); }
while((c<0||c>14)||(d<0||d>14)||qipan[d][c]!=SPA) {
printf("坐標錯誤或該位置已有子!請重新輸入:"); scanf("%d%d",&c,&d);
}
c1=c; d1=d; x--; qipan[d][c]=COM; system("cls"); draw();
printf("2P下在%d %d。\n",c,d);
if(win(c,d)){ printf("你真棒!!!\n"); return; } /* 玩家2贏 */
}
printf("和局\n");
}
}
void draw() /* 畫棋盤 */
{
int i,j;
char p[15][15][4];
for(j=0;j<15;j++)
for(i=0;i<15;i++){
if(qipan[j][i]==SPA) strcpy(p[j][i]," \0");
if(qipan[j][i]==MAN) strcpy(p[j][i],"●\0");
if(qipan[j][i]==COM) strcpy(p[j][i],"◎\0"); }
printf(" 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 \n");
printf(" ┌—┬—┬—┬—┬—┬—┬—┬—┬—┬—┬—┬—┬—┬—┬—┐\n");
for(i=0,j=0;i<14;i++,j++){
printf(" %2d│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%d\n",j,p[i][0],p[i][1],p[i][2],p[i][3],p[i][4],p[i][5],p[i][6],p[i][7],p[i][8],p[i][9],p[i][10],p[i][11],p[i][12],p[i][13],p[i][14],j);
printf(" ├—┼—┼—┼—┼—┼—┼—┼—┼—┼—┼—┼—┼—┼—┼—┤\n"); }
printf(" 14│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│0\n",p[14][0],p[14][1],p[14][2],p[14][3],p[14][4],p[14][5],p[14][6],p[14][7],p[14][8],p[14][9],p[14][10],p[14][11],p[14][12],p[14][13],p[14][14]);
printf(" └—┴—┴—┴—┴—┴—┴—┴—┴—┴—┴—┴—┴—┴—┴—┘\n");
printf(" 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 \n");
}
int win(int p,int q) /* 判斷勝利 p q為判斷點坐標,勝利返回1,否則返回0 */
{
int k,n=1,m,P,Q; /* k儲存判斷點p q的狀態COM或MAN。P Q儲存判斷點坐標。n為判斷方向。m為個數。 */
P=p; Q=q; k=qipan[q][p];
while(n!=5){
m=0;
while(k==qipan[q][p]){
m++; if(m==5) return 1;
yiwei(n,&p,&q); if(p<0||p>14||q<0||q>14) break;
}
n+=4; m-=1; p=P; q=Q; /* 轉向判斷 */
while(k==qipan[q][p]){
m++;
if(m==5) return 1;
yiwei(n,&p,&q); if(p<0||p>14||q<0||q>14) break;
}
n-=3; p=P; q=Q; /* 不成功則判斷下一組方向 */
}
return 0;
}
void AI(int *p,int *q) /* 電腦下子 *p *q返回下子坐標 */
{
int i,j,k,max=0,I,J; /* I J為下點坐標 */
for(j=0;j<15;j++)
for(i=0;i<15;i++)
if(qipan[j][i]==SPA){ /* 歷遍棋盤,遇到空點則計算價值,取最大價值點下子。 */
k=value(i,j); if(k>=max) { I=i; J=j; max=k; }
}
*p=I; *q=J;
}
int value(int p,int q) /* 計算空點p q的價值 以k返回 */
{
int n=1,k=0,k1,k2,K1,K2,X1,Y1,Z1,X2,Y2,Z2,temp;
int a[2][4][4]={40,400,3000,10000,6,10,600,10000,20,120,200,0,6,10,500,0,30,300,2500,5000,2,8,300,8000,26,160,0,0,4,20,300,0}; /* 數組a中儲存己方和對方共32種棋型的值 己方0對方1 活0沖1空活2空沖3 子數0-3(0表示1個子,3表示4個子) */
while(n!=5){
k1=qixing(n,p,q); n+=4; /* k1,k2為2個反方向的棋型編號 */
k2=qixing(n,p,q); n-=3;
if(k1>k2) { temp=k1; k1=k2; k2=temp; } /* 使編號小的為k1,大的為k2 */
K1=k1; K2=k2; /* K1 K2儲存k1 k2的編號 */
Z1=k1%10; Z2=k2%10; k1/=10; k2/=10; Y1=k1%10; Y2=k2%10; k1/=10; k2/=10;
X1=k1%10; X2=k2%10; /* X Y Z分別表示 己方0對方1 活0沖1空活2空沖3 子數0-3(0表示1個子,3表示4個子) */
if(K1==-1) {
if(K2<0) { k+=0; continue; } else k+=a[X2][Y2][Z2]+5; continue; }; /* 空棋型and其他 */
if(K1==-2) { if(K2<0) { k+=0; continue; }
else k+=a[X2][Y2][Z2]/2; continue; }; /* 邊界沖棋型and其他 */
if(K1==-3) { if(K2<0) { k+=0; continue; }
else k+=a[X2][Y2][Z2]/3; continue; }; /* 邊界空沖棋型and其他 */
if(((K1>-1&&K1<4)&&((K2>-1&&K2<4)||(K2>9&&K2<14)))||((K1>99&&K1<104)&&((K2>99&&K2<104)||(K2>109&&K2<114)))){
/* 己活己活 己活己沖 對活對活 對活對沖 的棋型賦值*/
if(Z1+Z2>=2) { k+=a[X2][Y2][3]; continue; }
else { k+=a[X2][Y2][Z1+Z2+1]; continue; }
}
if(((K1>9&&K1<14)&&(K2>9&&K2<14))||((K1>109&&K1<114)&&(K2>109&&K2<114))){
/* 己沖己沖 對沖對沖 的棋型賦值*/
if(Z1+Z2>=2) { k+=10000; continue; }
else { k+=0; continue; }
}
if(((K1>-1&&K1<4)&&((K2>99&&K2<104)||(K2>109&&K2<114)))||((K1>9&&K1<14)&&((K2>99&&K2<104)||(K2>109&&K2<114)))){
/* 己活對活 己活對沖 己沖對活 己沖對沖 的棋型賦值*/
if(Z1==3||Z2==3) { k+=10000; continue; }
else { k+=a[X2][Y2][Z2]+a[X1][Y1][Z1]/4; continue; }
}
else
{ k+=a[X1][Y1][Z1]+a[X2][Y2][Z2]; continue; } /* 其他棋型的賦值 */
}
return k;
}
int qixing(int n,int p,int q) /* 返回空點p q在n方向上的棋型號 n為1-8方向 從右順時針開始數 */
{
int k,m=0; /* 棋型號註解: 己活000-003 己沖010-013 對活100-103 對沖110-113 己空活020-023 己空沖030-033 對空活120-123 對空沖130-133 空-1 邊界沖-2 邊界空沖-3*/
yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) k=-2; /* 邊界沖棋型 */
switch(qipan[q][p]){
case COM:{
m++; yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) { k=m+9; return k; }
while(qipan[q][p]==COM) {
m++; yiwei(n,&p,&q); if(p<0||p>14||q<0||q>14) { k=m+9; return k; }
}
if(qipan[q][p]==SPA) k=m-1; /* 己方活棋型 */
else k=m+9; /* 己方沖棋型 */
}break;
case MAN:{
m++; yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) { k=m+109; return k; }
while(qipan[q][p]==MAN) {
m++; yiwei(n,&p,&q); if(p<0||p>14||q<0||q>14) { k=m+109; return k; }
}
if(qipan[q][p]==SPA) k=m+99; /* 對方活棋型 */
else k=m+109; /* 對方沖棋型 */
}break;
case SPA:{
yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) { k=-3; return k; } /* 邊界空沖棋型 */
switch(qipan[q][p]){
case COM:{
m++; yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) { k=m+29; return k; }
while(qipan[q][p]==COM) {
m++; yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) { k=m+29; return k; }
}
if(qipan[q][p]==SPA) k=m+19; /* 己方空活棋型 */
else k=m+29; /* 己方空沖棋型 */
}break;
case MAN:{
m++; yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) { k=m+129; return k; }
while(qipan[q][p]==MAN) {
m++; yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) { k=m+129; return k; }
}
if(qipan[q][p]==SPA) k=m+119; /* 對方空活棋型 */
else k=m+129; /* 對方空沖棋型 */
}break;
case SPA: k=-1; break; /* 空棋型 */
}
}break;
}
return k;
}
void yiwei(int n,int *i,int *j) /* 在n方向上對坐標 i j 移位 n為1-8方向 從右順時針開始數 */
{
switch(n){
case 1: *i+=1; break;
case 2: *i+=1; *j+=1; break;
case 3: *j+=1; break;
case 4: *i-=1; *j+=1; break;
case 5: *i-=1; break;
case 6: *i-=1; *j-=1; break;
case 7: *j-=1; break;
case 8: *i+=1; *j-=1; break;
}
}

5. 急求 c語言游戲黑白棋的設計思路

樓上說的有理
鄙人就說說簡單的雙人對戰吧
首先可以用二維數組表示棋盤(比方可以是int[][],元素為「1」表示玩家甲的棋子,「2」表示玩家乙...)
然後寫一個函數實現如下功能:
每下一子,就利用下標檢測此子周圍8個元素(邊上的沒有這么多,就要通過下標的限制了)有沒有相同的,有的話,累計(要考慮分4種情況累計,橫豎斜),並調用相應方向的函數檢測那些相同的元素,沒有就跳過繼續。
再寫四個函數(檢測橫豎斜4個方向的)
最後主函數
最最後。。。完善。。。!!!!!

說的不對的請指教

6. c語言小程序--黑白棋

你想做什麼?
人人對戰的代碼我有,但是人機對戰的AI寫不好
是個國際難題
//=====othello=====
#include<iostream>
#include<cstdio>

using namespace std;

int board[8][8],saveboard[60][8][8];
int cx,cy,col,pass,empty,black,white;

void init(){ //initialization
memset(board,-1,sizeof(board));
board[3][3]=0;
board[3][4]=1;
board[4][4]=0;
board[4][3]=1;
col=0;
pass=0;
empty=60;
black=2;
white=2;
}

int input(){
char s[1000]="";
scanf("%s",&s);
if(s[0]>='a' && s[0]<='h')
cy=s[0]-'a';
else if(s[0]>='A' && s[0]<='H')
cy=s[0]-'A';
else return 0;
if(s[1]>='1' && s[1]<='8'){
cx=s[1]-'1';
return 1;
}
return 0;
}

int judge(int x,int y){
int i,j,temp;
temp=(col+1)%2;
//left && up
if(board[x-1][y-1]==temp){
for(i=x-1,j=y-1; i>=0 && j>=0; i--,j--){
if(board[i][j]<0) break;
if(col==board[i][j]) return 1;
}
}
//up
if(board[x-1][y]==temp){
for(i=x-1; i>=0; i--){
if(board[i][y]<0) break;
if(col==board[i][y]) return 1;
}
}
//right && up
if(board[x-1][y+1]==temp){
for(i=x-1,j=y+1; i>=0 && j<8; i--,j++){
if(board[i][j]<0) break;
if(col==board[i][j]) return 1;
}
}
//right
if(board[x][y+1]==temp){
for(j=y+1; j<8; j++){
if(board[x][j]<0) break;
if(col==board[x][j]) return 1;
}
}
//right && down
if(board[x+1][y+1]==temp){
for(i=x+1,j=y+1; i<8 && j<8; i++,j++){
if(board[i][j]<0) break;
if(col==board[i][j]) return 1;
}
}
//down
if(board[x+1][y]==temp){
for(i=x+1; i<8; i++){
if(board[i][y]<0) break;
if(col==board[i][y]) return 1;
}
}
//left && down
if(board[x+1][y-1]==temp){
for(i=x+1,j=y-1; i<8 && j>=0; i++,j--){
if(board[i][j]<0) break;
if(col==board[i][j]) return 1;
}
}
//left
if(board[x][y-1]==temp){
for(j=y-1; j>=0; j--){
if(board[x][j]<0) break;
if(col==board[x][j]) return 1;
}
}
return 0;
}

void move(int x,int y){
int i,j,temp,count;
temp=(col+1)%2;
count=0;
//left && up
if(board[x-1][y-1]==temp){
for(i=x-1,j=y-1; i>=0 && j>=0; i--,j--){
if(board[i][j]<0) break;
if(col==board[i][j]){
while(i!=x){
board[++i][++j]=col;
count++;
}
count--;
break;
}
}
}
//up
if(board[x-1][y]==temp){
for(i=x-1; i>=0; i--){
if(board[i][y]<0) break;
if(col==board[i][y]){
while(i!=x){
board[++i][y]=col;
count++;
}
count--;
break;
}
}
}
//right && up
if(board[x-1][y+1]==temp){
for(i=x-1,j=y+1; i>=0 && j<8; i--,j++){
if(board[i][j]<0) break;
if(col==board[i][j]){
while(i!=x){
board[++i][--j]=col;
count++;
}
count--;
break;
}
}
}
//right
if(board[x][y+1]==temp){
for(j=y+1; j<8; j++){
if(board[x][j]<0) break;
if(col==board[x][j]){
while(j!=y){
board[x][--j]=col;
count++;
}
count--;
break;
}
}
}
//right && down
if(board[x+1][y+1]==temp){
for(i=x+1,j=y+1; i<8 && j<8; i++,j++){
if(board[i][j]<0) break;
if(col==board[i][j]){
while(i!=x){
board[--i][--j]=col;
count++;
}
count--;
break;
}
}
}
//down
if(board[x+1][y]==temp){
for(i=x+1; i<8; i++){
if(board[i][y]<0) break;
if(col==board[i][y]){
while(i!=x){
board[--i][y]=col;
count++;
}
count--;
break;
}
}
}
//left && down
if(board[x+1][y-1]==temp){
for(i=x+1,j=y-1; i<8 && j>=0; i++,j--){
if(board[i][j]<0) break;
if(col==board[i][j]){
while(i!=x){
board[--i][++j]=col;
count++;
}
count--;
break;
}
}
}
//left
if(board[x][y-1]==temp){
for(j=y-1; j>=0; j--){
if(board[x][j]<0) break;
if(col==board[x][j]){
while(j!=y){
board[x][++j]=col;
count++;
}
count--;
break;
}
}
}
board[x][y]=col;
if(col){
black+=count;
white-=count;
black++;
}
else{
black-=count;
white+=count;
white++;
}
empty--;
}

void output(){
char c;
printf(" ");
for(int i=0; i<8; i++){
c='A'+i;
printf("%2c",c);
}
printf("\n");

for(int i=0; i<8; i++){
printf("%d",i+1);
for(int j=0; j<8; j++){
if(board[i][j]==-1)
c=' ';
else if(board[i][j]==0)
c='O';
else
c='X';
printf("%2c",c);
}
printf("\n");
}
printf("Black:%3d White:%3d\n",black,white);
}

int passjudge(){
int f=0;
for(int i=0; i<8; i++)
for(int j=0; j<8; j++)
if(board[i][j]<0)
f+=judge(i,j);
return f;
}

void startprint(){
printf("1、New game\n2、setboard\n0、Exit\n");
}

void pvp(){
while(empty && pass<2){
//black or white
col++;
col%=2;
output();
//input
if(!input()){
if(!passjudge()){
printf("Pass!\n");
pass++;
}
else {
col++;
printf("No pass!\nPlease input right stone!\n");
}
continue;
}
if(judge(cx,cy)){
move(cx,cy);
pass=0;
}
else{
col++;
printf("Miss stone\n");
}
}
output();
if(black>white)
printf("Black Win!\n");
else if(black<white)
printf("White Win!\n");
else
printf("Draw Game!\n");
startprint();
}

void setboard(){
char c;
for(int i=0; i<8; i++)
for(int j=0; scanf("%c",&c) && c!='\n'; j++){
if(j>=8) continue;
if(c=='o' && c=='O')
board[i][j]=0;
else if(c=='x' && c=='X')
board[i][j]=1;
}
printf("White start or Black start?\n(W/B)");
scanf("%c",&c);
if(c=='w' || c=='W')
col=1;
if(c=='b' || c=='B')
col=0;
}

int main(int argc, char* argv[]){
int n;
startprint();
while(scanf("%d",&n) && n){
init();
if(n==1)
pvp();
if(n==2){
setboard();
pvp();
}
}
return 0;
}

7. C語言黑白棋

#include "graphics.h" /*圖形系統頭文件*/
#define LEFT 0x4b00 /*游標左鍵值*/
#define RIGHT 0x4d00 /*游標右鍵值*/
#define DOWN 0x5000 /*游標下鍵值*/
#define UP 0x4800 /*游標上鍵值*/
#define ESC 0x011b /* ESC鍵值*/
#define ENTER 0x1c0d /* 回車鍵值*/
int a[8][8]={0},key,score1,score2;/*具體分數以及按鍵與存放棋子的變數*/
char playone[3],playtwo[3];/*兩個人的得分轉換成字元串輸出*/
void playtoplay(void);/*人人對戰函數*/
void DrawQp(void);/*畫棋盤函數*/
void SetPlayColor(int x);/*設置棋子第一次的顏色*/
void MoveColor(int x,int y);/*恢復原來棋盤狀態*/
int QpChange(int x,int y,int z);/*判斷棋盤的變化*/
void DoScore(void);/*處理分數*/
void PrintScore(int n);/*輸出成績*/
void playWin(void);/*輸出勝利者信息*/
/******主函數*********/
void main(void)
{
int gd=DETECT,gr;
initgraph(&gd,&gr,"c:\\tc"); /*初始化圖形系統*/
DrawQp();/*畫棋盤*/
playtoplay();/*人人對戰*/
getch();
closegraph();/*關閉圖形系統*/
}
void DrawQp()/*畫棋盤*/
{
int i,j;
score1=score2=0;/*棋手一開始得分都為0*/
setbkcolor(BLUE);
for(i=100;i<=420;i+=40)
{
line(100,i,420,i);/*畫水平線*/
line(i,100,i,420); /*畫垂直線*/
}
setcolor(0);/*取消圓周圍的一圈東西*/
setfillstyle(SOLID_FILL,15);/*白色實體填充模式*/
fillellipse(500,200,15,15); /*在顯示得分的位置畫棋*/
setfillstyle(SOLID_FILL,8); /*黑色實體填充模式*/
fillellipse(500,300,15,15);
a[3][3]=a[4][4]=1;/*初始兩個黑棋*/
a[3][4]=a[4][3]=2;/*初始兩個白棋*/
setfillstyle(SOLID_FILL,WHITE);
fillellipse(120+3*40,120+3*40,15,15);
fillellipse(120+4*40,120+4*40,15,15);
setfillstyle(SOLID_FILL,8);
fillellipse(120+3*40,120+4*40,15,15);
fillellipse(120+4*40,120+3*40,15,15);
score1=score2=2; /*有棋後改變分數*/
DoScore();/*輸出開始分數*/
}
void playtoplay()/*人人對戰*/
{
int x,y,t=1,i,j,cc=0;
while(1)/*換棋手走棋*/
{
x=120,y=80;/*每次棋子一開始出來的坐標,x為行坐標,y為列坐標*/
while(1) /*具體一個棋手走棋的過程*/
{
PrintScore(1);/*輸出棋手1的成績*/
PrintScore(2);/*輸出棋手2的成績*/
SetPlayColor(t);/*t變數是用來判斷棋手所執棋子的顏色*/
fillellipse(x,y,15,15);
key=bioskey(0);/*接收按鍵*/
if(key==ESC)/*跳出遊戲*/
break;
else
if(key==ENTER)/*如果按鍵確定就可以跳出循環*/
{
if(y!=80&&a[(x-120)/40][(y-120)/40]!=1
&&a[(x-120)/40][(y-120)/40]!=2)/*如果落子位置沒有棋子*/
{
if(t%2==1)/*如果是棋手1移動*/
a[(x-120)/40][(y-120)/40]=1;
else/*否則棋手2移動*/
a[(x-120)/40][(y-120)/40]=2;
if(!QpChange(x,y,t))/*落子後判斷棋盤的變化*/
{
a[(x-120)/40][(y-120)/40]=0;/*恢復空格狀態*/
cc++;/*開始統計嘗試次數*/
if(cc>=64-score1-score2) /*如果嘗試超過空格數則停步*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
break;
}
else
continue;/*如果按鍵無效*/
}
DoScore();/*分數的改變*/
break;/*棋盤變化了,則輪對方走棋*/
}
else/*已經有棋子就繼續按鍵*/
continue;
}
else /*四個方向按鍵的判斷*/
if(key==LEFT&&x>120)/*左方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x-=40;
fillellipse(x,y,15,15);
}
else
if(key==RIGHT&&x<400&&y>80)/*右方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x+=40;
fillellipse(x,y,15,15);
}
else
if(key==UP&&y>120)/*上方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y-=40;
fillellipse(x,y,15,15);
}
else
if(key==DOWN&&y<400)/*下方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y+=40;
fillellipse(x,y,15,15);
}
}
if(key==ESC)/*結束游戲*/
break;
if((score1+score2)==64||score1==0||score2==0)/*格子已經占滿或一方棋子為0判斷勝負*/
{
playWin();/*輸出最後結果*/
break;
}
t=t%2+1; /*一方走後,改變棋子顏色即輪對方走*/
cc=0; /*計數值恢復為0*/
} /*endwhile*/
}
void SetPlayColor(int t)/*設置棋子顏色*/
{
if(t%2==1)
setfillstyle(SOLID_FILL,15);/*白色*/
else
setfillstyle(SOLID_FILL,8);/*灰色*/
}
void MoveColor(int x,int y)/*走了一步後恢復原來格子的狀態*/
{
if(y<100)/*如果是從起點出發就恢復藍色*/
setfillstyle(SOLID_FILL,BLUE);
else/*其他情況如果是1就恢復白色棋子,2恢復黑色棋子,或恢復藍色棋盤*/
switch(a[(x-120)/40][(y-120)/40])
{
case 1:
setfillstyle(SOLID_FILL,15);break; /*白色*/
case 2:
setfillstyle(SOLID_FILL,8);break; /*黑色*/
default:
setfillstyle(SOLID_FILL,BLUE); /*藍色*/
}
}
int QpChange(int x,int y,int t)/*判斷棋盤的變化*/
{
int i,j,k,kk,ii,jj,yes;
yes=0;
i=(x-120)/40; /*計算數組元素的行下標*/
j=(y-120)/40; /*計算數組元素的列下標*/
SetPlayColor(t);/*設置棋子變化的顏色*/
/*開始往8個方向判斷變化*/
if(j<6)/*往右邊*/
{
for(k=j+1;k<8;k++)
if(a[i][k]==a[i][j]||a[i][k]==0)/*遇到自己的棋子或空格結束*/
break;
if(a[i][k]!=0&&k<8)
{
for(kk=j+1;kk<k&&k<8;kk++)/*判斷右邊*/
{
a[i][kk]=a[i][j]; /*改變棋子顏色*/
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j+1) /*條件成立則有棋子改變過顏色*/
yes=1;
}
}
if(j>1)/*判斷左邊*/
{
for(k=j-1;k>=0;k--)
if(a[i][k]==a[i][j]||!a[i][k])
break;
if(a[i][k]!=0&&k>=0)
{
for(kk=j-1;kk>k&&k>=0;kk--)
{
a[i][kk]=a[i][j];
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j-1)
yes=1;
}
}
if(i<6)/*判斷下邊*/
{
for(k=i+1;k<8;k++)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0&&k<8)
{
for(kk=i+1;kk<k&&k<8;kk++)
{
a[kk][j]=a[i][j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i+1)
yes=1;
}
}
if(i>1)/*判斷上邊*/
{
for(k=i-1;k>=0;k--)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0&&k>=0)
{
for(kk=i-1;kk>k&&k>=0;kk--)
{
a[kk][j]=a[i][j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i-1)
yes=1;
}
}
if(i>1&&j<6)/*右上*/
{
for(k=i-1,kk=j+1;k>=0&&kk<8;k--,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]&&k>=0&&kk<8)

{
for(ii=i-1,jj=j+1;ii>k&&k>=0;ii--,jj++)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i<6&&j>1)/*左下*/
{
for(k=i+1,kk=j-1;k<8&&kk>=0;k++,kk--)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k<8&&kk>=0)
{
for(ii=i+1,jj=j-1;ii<k&&k<8;ii++,jj--)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
if(i>1&&j>1)/*左上*/
{
for(k=i-1,kk=j-1;k>=0&&kk>=0;k--,kk--)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k>=0&&kk>=0)
{
for(ii=i-1,jj=j-1;ii>k&&k>=0;ii--,jj--)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i<6&&j<6)/* 右下*/
{
for(k=i+1,kk=j+1;kk<8&&kk<8;k++,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&kk<8&&k<8)
{
for(ii=i+1,jj=j+1;ii<k&&k<8;ii++,jj++)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
return yes;/*返回是否改變過棋子顏色的標記*/
}
void DoScore()/*處理分數*/
{
int i,j;
score1=score2=0;/*重新開始計分數*/
for(i=0;i<8;i++)
for(j=0;j<8;j++)
if(a[i][j]==1)/*分別統計兩個人的分數*/
score1++;
else
if(a[i][j]==2)
score2++;
}
void PrintScore(int playnum)/*輸出成績*/
{
if(playnum==1)/*清除以前的成績*/
{
setfillstyle(SOLID_FILL,BLUE);
bar(550,100,640,400);
}
setcolor(RED);
settextstyle(0,0,4);/*設置文本輸出樣式*/
if(playnum==1)/*判斷輸出哪個棋手的分,在不同的位置輸出*/
{
sprintf(playone,"%d",score1);
outtextxy(550,200,playone);
}
else
{
sprintf(playtwo,"%d",score2);
outtextxy(550,300,playtwo);
}
setcolor(0);
}
void playWin()/*輸出最後的勝利者結果*/
{
settextstyle(0,0,4);
setcolor(12);
if(score2>score1)/*開始判斷最後的結果*/
outtextxy(100,50,"black win!");
else
if(score2<score1)
outtextxy(100,50,"white win!");
else
outtextxy(60,50,"you all win!");
}

8. C語言編制黑白棋游戲:

我寫的人機對戰的:
#include <stdio.h>
#include <ctype.h>

#define SIZE 8

void display(char board[][SIZE]);
int valid_moves(char board[][SIZE],int moves[][SIZE],char player);
void make_move(char board[][SIZE],int row,int col,char player);
void computer_move(char board[][SIZE],int moves[][SIZE],char player);
int get_score(char board[][SIZE],char player);
int best_move(char board[][SIZE],int moves[][SIZE],char player);

void main()
{
char board[SIZE][SIZE]={0};
int moves[SIZE][SIZE]={0};
int row=0;
int col=0;
int no_of_games=0;
int no_of_moves=0;
int invalid_moves=0;
int comp_score=0;
int user_score=0;
char y=0;
char x=0;
char again=0;
int player=0;

printf("\nREVERSI\n\n");
printf("You can go first on the first game,then we will take truns.\n");
printf(" You will be white - (0)\n I will be black - (@).\n");
printf("Select a square for your move by typing a digit for the row\n"
"and a letter for the column with no spaces between.\n");
printf("\nGood luck! press Enter to start.\n");
scanf("%c",&again);

do
{
player=++no_of_games%2;
no_of_moves=4;

for(row=0;row<SIZE;row++)
for(col=0;col<SIZE;col++)
board[row][col]=' ';

board[SIZE/2-1][SIZE/2-1]=board[SIZE/2][SIZE/2]='0';
board[SIZE/2-1][SIZE/2]=board[SIZE/2][SIZE/2-1]='@';

do
{
display(board);
if(player++%2)
{
if(valid_moves(board,moves,'0'))
{
for(;;)
{
fflush(stdin);
printf("Please enter your move (row column): ");
scanf("%d%c",&x,&y);
y=tolower(y)-'a';
x--;
if(x>=0&&y>=0&&x<SIZE&&y<SIZE&&moves[x][y])
{
make_move(board,x,y,'0');
no_of_moves++;
break;
}
else
printf("Not a valid move,try again.\n");
}
}
else
if(++invalid_moves<2)
{
fflush(stdin);
printf("\nYou have to pass,press return");
scanf("%c",&again);
}
else
printf("\nNeither of us can go, so the game is over.\n");
}
else
{
if(valid_moves(board,moves,'@'))
{
invalid_moves=0;
computer_move(board,moves,'@');
no_of_moves++;
}
else
{
if(++invalid_moves<2)
printf("\nI have to pass, your go\n");
else
printf("\nNeither of us can go, so the game is over.\n");
}
}
}while(no_of_moves<SIZE*SIZE&&invalid_moves<2);

display(board);

comp_score=user_score=0;
for(row=0;row<SIZE;row++)
for(col=0;col<SIZE;col++)
{
comp_score+=board[row][col]=='@';
user_score+=board[row][col]=='0';
}
printf("The final score is:\n");
printf("Computer %d\n User %d\n\n",comp_score,user_score);

fflush(stdin);
printf("Do you want to play again (y/n): ");
scanf("%c",&again);
}while(tolower(again)=='y');

printf("\nGoodbye\n");
}

void display(char board[][SIZE])
{
int row=0;
int col=0;
char col_label='a';

printf("\n ");
for(col=0;col<SIZE;col++)
printf(" %c",col_label+col);
printf("\n");

for(row=0;row<SIZE;row++)
{
printf(" +");
for(col=0;col<SIZE;col++)
printf("---+");
printf("\n%2d|",row+1);

for(col=0;col<SIZE;col++)
printf(" %c |",board[row][col]);
printf("\n");
}

printf(" +");
for(col=0;col<SIZE;col++)
printf("---+");
printf("\n");
}

int valid_moves(char board[][SIZE],int moves[][SIZE],char player)
{
int rowdelta=0;
int coldelta=0;
int row=0;
int col=0;
int x=0;
int y=0;
int no_of_moves=0;

char opponent=(player=='0')?'@':'0';

for(row=0;row<SIZE;row++)
for(col=0;col<SIZE;col++)
moves[row][col]=0;

for(row=0;row<SIZE;row++)
for(col=0;col<SIZE;col++)
{
if(board[row][col]!=' ')
continue;
for(rowdelta=-1;rowdelta<=1;rowdelta++)
for(coldelta=-1;coldelta<=1;coldelta++)
{
if(row+rowdelta<0||row+rowdelta>=SIZE||
col+coldelta<0||col+coldelta>=SIZE||
(rowdelta==0&&coldelta==0))
continue;
if(board[row+rowdelta][col+coldelta]==opponent)
{
x=row+rowdelta;
y=col+coldelta;

for(;;)
{
x+=rowdelta;
y+=coldelta;

if(x<0||x>=SIZE||y<0||y>=SIZE)
break;

if(board[x][y]==' ')
break;
if(board[x][y]==player)
{
moves[row][col]=1;
no_of_moves++;
break;
}
}
}
}
}
return no_of_moves;
}

void make_move(char board[][SIZE],int row,int col,char player)
{
int rowdelta=0;
int coldelta=0;
int x=0;
int y=0;
char opponent=(player=='0')?'@':'0';

board[row][col]=player;

for(rowdelta=-1;rowdelta<=1;rowdelta++)
for(coldelta=-1;coldelta<=1;coldelta++)
{
if(row+rowdelta<0||row+rowdelta>=SIZE||
col+coldelta<0||col+coldelta>=SIZE||
(rowdelta==0&&coldelta==0))
continue;

if(board[row+rowdelta][col+coldelta]==opponent)
{
x=row+rowdelta;
y=col+coldelta;

for(;;)
{
x+=rowdelta;
y+=coldelta;

if(x<0||x>=SIZE||y<0||y>=SIZE)
break;
if(board[x][y]==' ')
break;
if(board[x][y]==player)
{
while(board[x-=rowdelta][y-=coldelta]==opponent)
board[x][y]=player;
break;
}
}
}
}
}

int get_score(char board[][SIZE],char player)
{
int score=0;
int row=0;
int col=0;
char opponent=player=='0'?'@':'0';

for(row=0;row<SIZE;row++)
for(col=0;col<SIZE;col++)
{
score-=board[row][col]==opponent;
score+=board[row][col]==player;
}
return score;
}

int best_move(char board[][SIZE],int moves[][SIZE],char player)
{
int row=0;
int col=0;
int i=0;
int j=0;

char opponent=player=='0'?'@':'0';

char new_board[SIZE][SIZE]={0};
int score=0;
int new_score=0;

for(row=0;row<SIZE;row++)
for(col=0;col<SIZE;col++)
{
if(!moves[row][col])
continue;

for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
new_board[i][j]=board[i][j];

make_move(new_board,row,col,player);

new_score=get_score(new_board,player);

if(score<new_score)
score=new_score;
}
return score;
}

void computer_move(char board[][SIZE],int moves[][SIZE],char player)
{
int row=0;
int col=0;
int best_row=0;
int best_col=0;
int i=0;
int j=0;
int new_score=0;
int score=100;
char temp_board[SIZE][SIZE];
int temp_moves[SIZE][SIZE];
char opponent=player=='0'?'@':'0';

for(row=0;row<SIZE;row++)
for(col=0;col<SIZE;col++)
{
if(moves[row][col]==0)
continue;

for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
temp_board[i][j]=board[i][j];

make_move(temp_board,row,col,player);
valid_moves(temp_board,temp_moves,opponent);
new_score=best_move(temp_board,temp_moves,opponent);

if(new_score<score)
{
score=new_score;
best_row=row;
best_col=col;
}
}
make_move(board,best_row,best_col,player);
}
我已經用英文說明了如何操作了......看不懂的我在這個說一下.
先按回車開始游戲,然後輸入行號和列號就可以了...一局結束之後,
會提示是否再來一盤輸入大寫的Y繼續大寫的N結束程序!!!
我已經對操作說的很明確了!~~~~~你控制的是白棋(0),首先按回車開始游戲,然後輸入行號(數字),列號(字元)就行了.......如果還是不行的話,那就是你更本不懂黑白棋的規則....

9. 求一個C語言的黑白棋程序

#include "graphics.h" /*圖形系統頭文件*/
#define LEFT 0x4b00 /*游標左鍵值*/
#define RIGHT 0x4d00 /*游標右鍵值*/
#define DOWN 0x5000 /*游標下鍵值*/
#define UP 0x4800 /*游標上鍵值*/
#define ESC 0x011b /* ESC鍵值*/
#define ENTER 0x1c0d /* 回車鍵值*/

int a[8][8]={0},key,score1,score2;/*具體分數以及按鍵與存放棋子的變數*/
char playone[3],playtwo[3];/*兩個人的得分轉換成字元串輸出*/
void playtoplay(void);/*人人對戰函數*/
void DrawQp(void);/*畫棋盤函數*/
void SetPlayColor(int x);/*設置棋子第一次的顏色*/
void MoveColor(int x,int y);/*恢復原來棋盤狀態*/
int QpChange(int x,int y,int z);/*判斷棋盤的變化*/
void DoScore(void);/*處理分數*/
void PrintScore(int n);/*輸出成績*/
void playWin(void);/*輸出勝利者信息*/

/******主函數*********/
void main(void)
{
int gd=DETECT,gr;
initgraph(&gd,&gr,"c:\\tc"); /*初始化圖形系統*/
DrawQp();/*畫棋盤*/
playtoplay();/*人人對戰*/
getch();
closegraph();/*關閉圖形系統*/
}

void DrawQp()/*畫棋盤*/
{
int i,j;
score1=score2=0;/*棋手一開始得分都為0*/
setbkcolor(BLUE);
for(i=100;i<=420;i+=40)
{
line(100,i,420,i);/*畫水平線*/
line(i,100,i,420); /*畫垂直線*/
}
setcolor(0);/*取消圓周圍的一圈東西*/
setfillstyle(SOLID_FILL,15);/*白色實體填充模式*/
fillellipse(500,200,15,15); /*在顯示得分的位置畫棋*/
setfillstyle(SOLID_FILL,8); /*黑色實體填充模式*/
fillellipse(500,300,15,15);
a[3][3]=a[4][4]=1;/*初始兩個黑棋*/
a[3][4]=a[4][3]=2;/*初始兩個白棋*/
setfillstyle(SOLID_FILL,WHITE);
fillellipse(120+3*40,120+3*40,15,15);
fillellipse(120+4*40,120+4*40,15,15);
setfillstyle(SOLID_FILL,8);
fillellipse(120+3*40,120+4*40,15,15);
fillellipse(120+4*40,120+3*40,15,15);
score1=score2=2; /*有棋後改變分數*/
DoScore();/*輸出開始分數*/
}

void playtoplay()/*人人對戰*/
{
int x,y,t=1,i,j,cc=0;
while(1)/*換棋手走棋*/
{
x=120,y=80;/*每次棋子一開始出來的坐標,x為行坐標,y為列坐標*/
while(1) /*具體一個棋手走棋的過程*/
{
PrintScore(1);/*輸出棋手1的成績*/
PrintScore(2);/*輸出棋手2的成績*/
SetPlayColor(t);/*t變數是用來判斷棋手所執棋子的顏色*/
fillellipse(x,y,15,15);
key=bioskey(0);/*接收按鍵*/
if(key==ESC)/*跳出遊戲*/
break;
else
if(key==ENTER)/*如果按鍵確定就可以跳出循環*/
{
if(y!=80&&a[(x-120)/40][(y-120)/40]!=1
&&a[(x-120)/40][(y-120)/40]!=2)/*如果落子位置沒有棋子*/
{
if(t%2==1)/*如果是棋手1移動*/
a[(x-120)/40][(y-120)/40]=1;
else/*否則棋手2移動*/
a[(x-120)/40][(y-120)/40]=2;
if(!QpChange(x,y,t))/*落子後判斷棋盤的變化*/
{
a[(x-120)/40][(y-120)/40]=0;/*恢復空格狀態*/
cc++;/*開始統計嘗試次數*/
if(cc>=64-score1-score2) /*如果嘗試超過空格數則停步*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
break;
}
else
continue;/*如果按鍵無效*/
}
DoScore();/*分數的改變*/
break;/*棋盤變化了,則輪對方走棋*/
}
else/*已經有棋子就繼續按鍵*/
continue;
}
else /*四個方向按鍵的判斷*/
if(key==LEFT&&x>120)/*左方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x-=40;
fillellipse(x,y,15,15);
}
else
if(key==RIGHT&&x<400&&y>80)/*右方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x+=40;
fillellipse(x,y,15,15);
}
else
if(key==UP&&y>120)/*上方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y-=40;
fillellipse(x,y,15,15);
}
else
if(key==DOWN&&y<400)/*下方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y+=40;
fillellipse(x,y,15,15);
}
}
if(key==ESC)/*結束游戲*/
break;
if((score1+score2)==64||score1==0||score2==0)/*格子已經占滿或一方棋子為0判斷勝負*/
{
playWin();/*輸出最後結果*/
break;
}
t=t%2+1; /*一方走後,改變棋子顏色即輪對方走*/
cc=0; /*計數值恢復為0*/
} /*endwhile*/
}

void SetPlayColor(int t)/*設置棋子顏色*/
{
if(t%2==1)
setfillstyle(SOLID_FILL,15);/*白色*/
else
setfillstyle(SOLID_FILL,8);/*灰色*/
}

void MoveColor(int x,int y)/*走了一步後恢復原來格子的狀態*/
{
if(y<100)/*如果是從起點出發就恢復藍色*/
setfillstyle(SOLID_FILL,BLUE);
else/*其他情況如果是1就恢復白色棋子,2恢復黑色棋子,或恢復藍色棋盤*/
switch(a[(x-120)/40][(y-120)/40])
{
case 1:
setfillstyle(SOLID_FILL,15);break; /*白色*/
case 2:
setfillstyle(SOLID_FILL,8);break; /*黑色*/
default:
setfillstyle(SOLID_FILL,BLUE); /*藍色*/
}
}

int QpChange(int x,int y,int t)/*判斷棋盤的變化*/
{
int i,j,k,kk,ii,jj,yes;
yes=0;
i=(x-120)/40; /*計算數組元素的行下標*/
j=(y-120)/40; /*計算數組元素的列下標*/
SetPlayColor(t);/*設置棋子變化的顏色*/
/*開始往8個方向判斷變化*/
if(j<6)/*往右邊*/
{
for(k=j+1;k<8;k++)
if(a[k]==a[j]||a[k]==0)/*遇到自己的棋子或空格結束*/
break;
if(a[k]!=0&&k<8)
{
for(kk=j+1;kk<k&&k<8;kk++)/*判斷右邊*/
{
a[kk]=a[j]; /*改變棋子顏色*/
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j+1) /*條件成立則有棋子改變過顏色*/
yes=1;
}
}
if(j>1)/*判斷左邊*/
{
for(k=j-1;k>=0;k--)
if(a[k]==a[j]||!a[k])
break;
if(a[k]!=0&&k>=0)
{
for(kk=j-1;kk>k&&k>=0;kk--)
{
a[kk]=a[j];
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j-1)
yes=1;
}
}
if(i<6)/*判斷下邊*/
{
for(k=i+1;k<8;k++)
if(a[k][j]==a[j]||!a[k][j])
break;
if(a[k][j]!=0&&k<8)
{
for(kk=i+1;kk<k&&k<8;kk++)
{
a[kk][j]=a[j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i+1)
yes=1;
}
}
if(i>1)/*判斷上邊*/
{
for(k=i-1;k>=0;k--)
if(a[k][j]==a[j]||!a[k][j])
break;
if(a[k][j]!=0&&k>=0)
{
for(kk=i-1;kk>k&&k>=0;kk--)
{
a[kk][j]=a[j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i-1)
yes=1;
}
}
if(i>1&&j<6)/*右上*/
{
for(k=i-1,kk=j+1;k>=0&&kk<8;k--,kk++)
if(a[k][kk]==a[j]||!a[k][kk])
break;
if(a[k][kk]&&k>=0&&kk<8)
{
for(ii=i-1,jj=j+1;ii>k&&k>=0;ii--,jj++)
{
a[ii][jj]=a[j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i<6&&j>1)/*左下*/
{
for(k=i+1,kk=j-1;k<8&&kk>=0;k++,kk--)
if(a[k][kk]==a[j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k<8&&kk>=0)
{
for(ii=i+1,jj=j-1;ii<k&&k<8;ii++,jj--)
{
a[ii][jj]=a[j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
if(i>1&&j>1)/*左上*/
{
for(k=i-1,kk=j-1;k>=0&&kk>=0;k--,kk--)
if(a[k][kk]==a[j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k>=0&&kk>=0)
{
for(ii=i-1,jj=j-1;ii>k&&k>=0;ii--,jj--)
{
a[ii][jj]=a[j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i<6&&j<6)/* 右下*/
{
for(k=i+1,kk=j+1;kk<8&&kk<8;k++,kk++)
if(a[k][kk]==a[j]||!a[k][kk])
break;
if(a[k][kk]!=0&&kk<8&&k<8)
{
for(ii=i+1,jj=j+1;ii<k&&k<8;ii++,jj++)
{
a[ii][jj]=a[j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
return yes;/*返回是否改變過棋子顏色的標記*/
}

void DoScore()/*處理分數*/
{
int i,j;
score1=score2=0;/*重新開始計分數*/
for(i=0;i<8;i++)
for(j=0;j<8;j++)
if(a[j]==1)/*分別統計兩個人的分數*/
score1++;
else
if(a[j]==2)
score2++;
}

void PrintScore(int playnum)/*輸出成績*/
{
if(playnum==1)/*清除以前的成績*/
{
setfillstyle(SOLID_FILL,BLUE);
bar(550,100,640,400);
}
setcolor(RED);
settextstyle(0,0,4);/*設置文本輸出樣式*/
if(playnum==1)/*判斷輸出哪個棋手的分,在不同的位置輸出*/
{
sprintf(playone,"%d",score1);
outtextxy(550,200,playone);
}
else
{
sprintf(playtwo,"%d",score2);
outtextxy(550,300,playtwo);
}
setcolor(0);
}

void playWin()/*輸出最後的勝利者結果*/
{
settextstyle(0,0,4);
setcolor(12);
if(score2>score1)/*開始判斷最後的結果*/
outtextxy(100,50,"black win!");
else
if(score2<score1)
outtextxy(100,50,"white win!");
else
outtextxy(60,50,"you all win!");
}

10. 急:黑白棋求助,望各位高手幫忙!!!

補充上邊的問題:
for(i=0; i<8; i++)
{
getchar();
for(t=0; t<8; t++)
{
ch = getchar();
if(ch=='1') chesscolor[i][t]=1;
else if(ch=='2') chesscolor[i][t]=2;
else if(ch=='0') chesscolor[i][t]=0;
}
}
int probability[8][8]={10,9,-1,0,0,-1,9,10,9,-10,0,0,0,-10,9,-1,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,-1,9,-10,0,0,0,-10,9,10,9,-1,0,0,-1,9,10,};
for(k=0;k<8;k++)
for(l=0;l<8;l++)
{
for(x=0;x<8;x++)
for(y=0;y<8;y++)
{
if(rules(x,y,chesscolor[x][y],chesscolor)==0)
{
xx[m]=x;
yy[n]=y;
m++;
n++;
}
}
for(m=0;m<8;m++)
for(n=0;n<8;n++)
{
if(probability[xx[m]][yy[n]]==10)
{
flag=1;
chesscolor[xx[m]][yy[n]]=rules(xx[m],yy[n],chesscolor[xx[m]][yy[n]],chesscolor);
}
else if(probability[xx[m]][yy[n]]==9)
{
if(probability[xx[m]--][yy[n]]==-1 || probability[xx[m]][yy[n]--]==-1)
{
flag=1;
chesscolor[xx[m]][yy[n]]=rules(xx[m],yy[n],chesscolor[xx[m]][yy[n]],chesscolor);
}
}
else if(probability[xx[m]][yy[n]]==-10)
{
flag=0;
continue;
}
}
if(flag=0)
{
r=rand()%7;
p=rand()%7;
chesscolor[xx[r]][yy[p]]=rules(xx[r],yy[p],chesscolor[r][p],chesscolor);
}
}
printf("讀入數據:\n");
for(i=0; i<8; i++)
{
for(t=0; t<8; t++)
{
printf("%d", chesscolor[i][t]);
}
printf("\n");
}
}
上述代碼可以通過編譯,但運行時出錯,DEBUG顯示xx[m]=x;這行出錯,原因不知,求解,急!!!!語言C語言

熱點內容
sql分離 發布:2024-05-08 16:09:12 瀏覽:889
怎麼把安卓手機視頻播放器 發布:2024-05-08 16:08:17 瀏覽:565
昆侖通態腳本改變按鈕顏色 發布:2024-05-08 15:51:44 瀏覽:3
dlcache文件夾 發布:2024-05-08 15:46:49 瀏覽:897
大眾大屏科士達密碼是多少 發布:2024-05-08 15:25:47 瀏覽:514
flutter不重新編譯調試 發布:2024-05-08 15:22:42 瀏覽:713
mysql資料庫編碼修改 發布:2024-05-08 15:03:27 瀏覽:979
高斯混合模型的em演算法 發布:2024-05-08 14:56:55 瀏覽:342
您訪問過快 發布:2024-05-08 14:31:32 瀏覽:145
android廣播生命周期 發布:2024-05-08 14:28:58 瀏覽:60