當前位置:首頁 » 編程語言 » c語言編寫雙人五子棋
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言編寫雙人五子棋

發布時間: 2023-03-21 06:09:20

『壹』 怎樣用c語言編一個簡單的五子棋游戲

#include <graphics.h>
#include <stdio.h>
#include <MATH.H>
IMAGE* IMG;
IMAGE* IMG2;
IMAGE* IMG3;
IMAGE* whole;
bool mark = false;
int x = 0, y = 0;
int flag[15][15];

void show()
{
outtextxy(550, 100, "白方:");
outtextxy(550, 150, " 箭頭移動");
outtextxy(550, 200, " 回車鍵落子");
outtextxy(550, 250, "黑方:");
outtextxy(550, 300, " ADWS移動");
outtextxy(550, 350, " 空格鍵落子");
}

int success1(int dir1, int dir2)
{
int number = 0;
int temp_x = x, temp_y = y;
while (((temp_x / 35 + dir1) >= 0 && (temp_x / 35 + dir1) < 15) && ((temp_y / 35 + dir2) >= 0 && (temp_y / 35 + dir2) < 15) && (flag[(temp_x / 35 + dir1)][(temp_y / 35 + dir2)] == 1))
{
temp_x = temp_x + dir1 * 35;
temp_y = temp_y + dir2 * 35;
++number;
}
return number;
}

int success2(int dir1, int dir2)
{
int number = 0;
int temp_x = x, temp_y = y;
while (((temp_x / 35 + dir1) >= 0 && (temp_x / 35 + dir1) < 15) && ((temp_y / 35 + dir2) >= 0 && (temp_y / 35 + dir2) < 15) && (flag[(temp_x / 35 + dir1)][(temp_y / 35 + dir2)] == 2))
{
temp_x = temp_x + dir1 * 35;
temp_y = temp_y + dir2 * 35;
++number;
}
return number;
}

int success1()
{
int number = 0;
number = success1(0, -1) + success1(0, 1);//上下
if (number < 4)
{
number = success1(-1, 0) + success1(1, 0);//左右
if (number < 4)
{
number = success1(-1, -1) + success1(1, 1);//左上右下
if (number < 4)
{
number = success1(-1, 1) + success1(1, -1);//左下右上
}
}
}
return number;
}
int success2()
{
int number = 0;
number = success2(0, -1) + success2(0, 1);//上下
if (number < 4)
{
number = success2(-1, 0) + success2(1, 0);//左右
if (number < 4)
{
number = success2(-1, -1) + success2(1, 1);//左上右下
if (number < 4)
{
number = success2(-1, 1) + success2(1, -1);//左下右上
}
}
}
return number;
}

void control()
{
char key = 0;

while (key != 27)
{
Sleep(10);
if (kbhit())
{
key = getch();
switch (key)
{
case VK_LEFT:
if (mark)
break;
if (x > 0)
x = x - 35;
break;
case 'a':
case 'A':
if (!mark)
break;
if (x > 0)
x = x - 35;
break;
case VK_RIGHT:
if (mark)
break;
if (x < 490)
x = x + 35;
break;
case 'd':
case 'D':
if (!mark)
break;
if (x < 490)
x = x + 35;
break;
case VK_UP:
if (mark)
break;
if (y > 0)
y = y - 35;
break;
case 'w':
case 'W':
if (!mark)
break;
if (y > 0)
y = y - 35;
break;
case VK_DOWN:
if (mark)
break;
if (y < 490)
y = y + 35;
break;
case 's':
case 'S':
if (!mark)
break;
if (y < 490)
y = y + 35;
break;
case VK_RETURN:
if (mark)
break;
if (flag[x / 35][y / 35] == 0)
{
putimage(whole, x + 6, y + 6, 31, 32, IMG2, 0, 0);
flag[x / 35][y / 35] = 1;
if (success1() >= 4)
{
outtextxy(600, 50, "黑方 勝!");
key = 27;
}
mark = true;
}
break;
case VK_SPACE:
if (!mark)
break;
if (flag[x / 35][y / 35] == 0)
{
putimage(whole, x + 6, y + 6, 31, 31, IMG3, 0, 0);
flag[x / 35][y / 35] = 2;
if (success2() >= 4)
{
outtextxy(600, 50, "白方 勝!");
key = 27;
}
mark = false;
}
break;
default:
break;
}
putimage(0, 0, whole);
putimage_transparent(NULL, IMG, x + 20, y + 20, 0x0, 0, 0, 20, 20);
}
}
}

void main()
{
setinitmode(0);
initgraph(800, 538);
SetWindowText(GetHWnd(), "五子棋20110327");
setcolor(0xffffff);
setfont(36, 0, "楷體_GB2312");

IMAGE* IMG1 = new IMAGE;
getimage(IMG1, "JPG", MAKEINTRESOURCE(102));//棋盤
putimage(0, 0, IMG1);
IMG2 = new IMAGE;
getimage(IMG2, "JPG", MAKEINTRESOURCE(103));//黑棋
IMG3 = new IMAGE;
getimage(IMG3, "JPG", MAKEINTRESOURCE(104));//白棋
IMG = new IMAGE;
getimage(IMG, "GIF", MAKEINTRESOURCE(101));//手形
whole = new IMAGE;
getimage(whole, 0, 0, 537, 537);
putimage_transparent(NULL, IMG, x + 20, y + 20, 0x0, 0, 0, 20, 20);

show();
control();

delete IMG1;
delete IMG2;
delete IMG3;
delete whole;
getch();
getch();
closegraph();
}

『貳』 c語言基礎,求五子棋詳細代碼

/*一個月就想做五子棋,有點難啊,希望你能看懂,這是代碼*/
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>


#defineMAXIMUS15//定義棋盤大小


intp[MAXIMUS][MAXIMUS];//存儲對局信息
charbuff[MAXIMUS*2+1][MAXIMUS*4+3];//輸出肆和緩沖器
intCx,Cy;//當前游標位置
intNow;//當前走子的玩家,1代表黑,2代表白
intwl,wp;//當前寫入緩沖器的列數和行數位置
char*showText;//在棋盤中央顯示的文字信息
intcount;//回合數


char*Copy(char*strDest,constchar*strSrc)//修改過的字元串復制函數,會忽略末端的
{
char*strDestCopy=strDest;
while(*strSrc!='')
{
*strDest++=*strSrc++;
}
陪襲returnstrDestCopy;
}

voidInitialize()//初始化一個對局函數
{
inti,j;//循環變數
showText="";//重置顯示信息
count=0;//回合數歸零
for(i=0;i<MAXIMUS;i++)//重置對局數據
{
for(j=0;j<MAXIMUS;j++)
{
p[i][j]=0;
}
}
Cx=Cy=MAXIMUS/2;//重置游標到中央
Now=1;//重置當前為黑方
}

char*getStyle(inti,intj)//獲得棋盤中指定坐標交點位置的字元,通過製表符拼成棋盤
{
if(p[i][j]==1)//1為黑子
return"●";
elseif(p[i][j]==2)//2為白子
return"○";
elseif(i==0&&j==0)//以下為邊緣棋盤樣式
return"┏";
elseif(i==MAXIMUS-1&&j==0)
return"┓";
elseif(i==MAXIMUS-1&&j==MAXIMUS-1)
return"┛";
elseif(i==0&&j==MAXIMUS-1)
return"┗";
elseif(i==0)
return"┠";
elseif(i==MAXIMUS-1)
return"┨";
elseif(j==0)
return"┯";
elseif(j==MAXIMUS-1)
return"┷";
return"┼";//中間的空位
}

char*getCurse(inti,intj)//獲得指定坐標交點位置左上格的樣式,通過製表符來模擬游標的顯示
{
if(i==Cx)
{
if(j==Cy)
return"┏";
elseif(j==Cy+1)
return"┗";
}
elseif(i==Cx+1)
{
if(j==Cy)
return"┓";
elseif(j==Cy+1)
return"┛";
}
return"";//如果不在游標附近則為空
}

voidwrite(char*c)//向緩沖器寫入字元串
{
Copy(buff[wl]+wp,c);
wp+=strlen(c);
}

voidln()//緩沖器寫入位置提行
{
wl+=1;
wp=0;
}

voidDisplay()//將緩沖器內容輸出到屏幕
{
inti,l=strlen(showText);//循環變數,中間文字信息的長度
intOffset=MAXIMUS*2+2-l/2;//算出中間文裂亂盯字信息居中顯示所在的橫坐標位置
if(Offset%2==1)//如果位置為奇數,則移動到偶數,避免混亂
{
Offset--;
}
Copy(buff[MAXIMUS]+Offset,showText);//講中間文字信息復制到緩沖器
if(l%2==1)//如果中間文字長度為半形奇數,則補上空格,避免混亂
{
*(buff[MAXIMUS]+Offset+l)=0x20;
}
system("cls");//清理屏幕,准備寫入
for(i=0;i<MAXIMUS*2+1;i++)//循環寫入每一行
{
printf("%s",buff[i]);
if(i<MAXIMUS*2)//寫入完每一行需要換行
printf(" ");
}
}

voidPrint()//將整個棋盤算出並儲存到緩沖器,然後調用Display函數顯示出來
{
inti,j;//循環變數
wl=0;
wp=0;
for(j=0;j<=MAXIMUS;j++)//寫入出交點左上角的字元,因為需要列印棋盤右下角,所以很以橫縱各多一次循環
{
for(i=0;i<=MAXIMUS;i++)
{
write(getCurse(i,j));//寫入左上角字元
if(j==0||j==MAXIMUS)//如果是棋上下盤邊緣則沒有連接的豎線,用空格填充位置
{
if(i!=MAXIMUS)
write("");
}
else//如果在棋盤中間則用豎線承接上下
{
if(i==0||i==MAXIMUS-1)//左右邊緣的豎線更粗
write("┃");
elseif(i!=MAXIMUS)//中間的豎線
write("│");
}
}
if(j==MAXIMUS)//如果是最後一次循環,則只需要處理邊側字元,交點要少一排
{
break;
}
ln();//提行開始列印交點內容
write("");//用空位補齊位置
for(i=0;i<MAXIMUS;i++)//按橫坐標循環正常的次數
{
write(getStyle(i,j));//寫入交點字元
if(i!=MAXIMUS-1)//如果不在最右側則補充一個橫線承接左右
{
if(j==0||j==MAXIMUS-1)
{
write("━");//上下邊緣的橫線更粗
}
else
{
write("—");//中間的橫線
}
}
}
ln();//寫完一行後提行
}
Display();//將緩沖器內容輸出到屏幕
}

intPut()//在當前游標位置走子,如果非空,則返回0表示失敗
{
if(p[Cx][Cy]==0)
{
p[Cx][Cy]=Now;//改變該位置數據
return1;//返回1表示成功
}
else
{
return0;
}
}

intCheck()//勝負檢查,即判斷當前走子位置有沒有造成五連珠的情況
{
intw=1,x=1,y=1,z=1,i;//累計橫豎正斜反邪四個方向的連續相同棋子數目
for(i=1;i<5;i++)//向下檢查
if(Cy+i<MAXIMUS&&p[Cx][Cy+i]==Now)
w++;
else
break;
for(i=1;i<5;i++)//向上檢查
if(Cy-i>0&&p[Cx][Cy-i]==Now)
w++;
else
break;
if(w>=5)//若果達到5個則判斷當前走子玩家為贏家
returnNow;
for(i=1;i<5;i++)//向右檢查
if(Cx+i<MAXIMUS&&p[Cx+i][Cy]==Now)
x++;
else
break;
for(i=1;i<5;i++)//向左檢查
if(Cx-i>0&&p[Cx-i][Cy]==Now)
x++;
else
break;
if(x>=5)//若果達到5個則判斷當前走子玩家為贏家
returnNow;
for(i=1;i<5;i++)//向右下檢查
if(Cx+i<MAXIMUS&&Cy+i<MAXIMUS&&p[Cx+i][Cy+i]==Now)
y++;
else
break;
for(i=1;i<5;i++)//向左上檢查
if(Cx-i>0&&Cy-i>0&&p[Cx-i][Cy-i]==Now)
y++;
else
break;
if(y>=5)//若果達到5個則判斷當前走子玩家為贏家
returnNow;
for(i=1;i<5;i++)//向右上檢查
if(Cx+i<MAXIMUS&&Cy-i>0&&p[Cx+i][Cy-i]==Now)
z++;
else
break;
for(i=1;i<5;i++)//向左下檢查
if(Cx-i>0&&Cy+i<MAXIMUS&&p[Cx-i][Cy+i]==Now)
z++;
else
break;
if(z>=5)//若果達到5個則判斷當前走子玩家為贏家
returnNow;
return0;//若沒有檢查到五連珠,則返回0表示還沒有玩家達成勝利
}

intRunGame()//進行整個對局,返回贏家信息(雖然有用上)
{
intinput;//輸入變數
intvictor;//贏家信息
Initialize();//初始化對局
while(1)//開始無限回合的死循環,直到出現勝利跳出
{
Print();//列印棋盤
input=getch();//等待鍵盤按下一個字元
if(input==27)//如果是ESC則退出程序
{
exit(0);
}
elseif(input==0x20)//如果是空格則開始走子
{
if(Put())//如果走子成功則判斷勝負
{
victor=Check();
Now=3-Now;//輪換當前走子玩家
count++;
if(victor==1)//如果黑方達到勝利,顯示提示文字並等待一次按鍵,返回勝利信息
{
showText="黑方獲得了勝利!";
Print();
if(getch()==0xE0)
{
getch();
}
returnNow;
}
elseif(victor==2)//如果白方達到勝利,顯示提示文字並等待一次按鍵,返回勝利信息
{
showText="白方獲得了勝利!";
Display();
if(getch()==0xE0)
{
getch();
}
returnNow;
}
elseif(count==MAXIMUS*MAXIMUS)//如果回合數達到了棋盤總量,即棋盤充滿,即為平局
{
showText="平局!";
Display();
if(getch()==0xE0)
{
getch();
}
return0;
}
}
}
elseif(input==0xE0)//如果按下的是方向鍵,會填充兩次輸入,第一次為0xE0表示按下的是控制鍵
{
input=getch();//獲得第二次輸入信息
switch(input)//判斷方向鍵方向並移動游標位置
{
case0x4B:
Cx--;
break;
case0x48:
Cy--;
break;
case0x4D:
Cx++;
break;
case0x50:
Cy++;
break;
}
if(Cx<0)//如果游標位置越界則移動到對側
Cx=MAXIMUS-1;
if(Cy<0)
Cy=MAXIMUS-1;
if(Cx>MAXIMUS-1)
Cx=0;
if(Cy>MAXIMUS-1)
Cy=0;
}
}
}

intmain()//主函數
{
system("title簡易五子棋——Etsnarl製作");//設置標題
system("modeconcols=63lines=32");//設置窗口大小
system("colorE0");//設置顏色
while(1)//循環執行游戲
{
RunGame();
}
return0;
}

『叄』 急求論文 c語言開發五子棋的應用

//但是這些代碼由於編譯器的原因,在TC下仍無法運行。請使用VC。
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>int ChessData[15][15] ={0};
int GuangbiaoData[2]={8,8};
int ChessStepData[255][2]={0};
void gotoxy(int x, int y) //gotoxy在TC中是在一個系統自帶的函數,但是在VC中沒有這個函數,所以在網上找到了這個函數以實現同樣的功能。
//只有這一個函數是網上找的,別的全部我自己寫的。
{
COORD c;
c.X=x-1;
c.Y=y-1;
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}void GotoChess(int x,int y)
{
x=3*x-2;y=2*y-1;
gotoxy(x,y);
}void Move(int MoveData) //輸入參數為用戶輸入的方向(1表示上,2表示下,3表示左,4表示右)
{
switch (MoveData)
{
case 1:GuangbiaoData[1]-=1;break;
case 2:GuangbiaoData[1]+=1;break;
case 3:GuangbiaoData[0]-=1;break;
case 4:GuangbiaoData[0]+=1;break;
default:printf("Move函數出錯");
}
GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
}int Get(int *data) //該函數的功能是用戶的按鍵,並轉化為01234567(0表示輸入錯誤,1表示上,2表示下,3表示左,4表示右,5表示落子,6表示悔棋,7表示退出。)
{ //並返回輸入的用戶號碼(共同鍵返回3),錯誤則返回0
int temp;B: temp=getch();
if (temp==224)
{
temp=getch();
switch (temp)
{
case 72:*data=1;break;
case 80:*data=2;break;
case 75:*data=3;break;
case 77:*data=4;break;
default:goto B;
}
return 2;
}
else
{
switch (temp)
{
case 'w':
case 'W':*data=1;return 1;break;
case 's':
case 'S':*data=2;return 1;break;
case 'a':
case 'A':*data=3;return 1;break;
case 'd':
case 'D':*data=4;return 1;break;
case 13 :*data=5;return 2;break;
case 32 :*data=5;return 1;break;
case 8 :*data=6;break;
case 27 :*data=7;break;
default:*data=0 ;return 0;break;
}
return 3;
}
}
void MoveToEnd()
{
gotoxy(1,30);
}
int LogicBeOut(int a,int b)
{
if (a==-1||a==15||b==-1||b==15) return 1;
else return 0;
}
int win(int v)
{
int i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]-i-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]+j-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]+i-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]-j-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]-i-1][b=GuangbiaoData[1]-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]+j-1][b=GuangbiaoData[1]-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;

return 0;
}void NewShow() //新棋局的開始
{
int i,j;
for (i=0;i<15;i++)
for (j=0;j<15;j++)
ChessData[i][j]=0;
system("cls");
for (i=1;i<=29;i++)
{
for (j=1;j<=43;j++)
if(i%2==1) printf("-");
else if (j%3==1) printf("|");
else printf(" ");
printf("\n");
}
GuangbiaoData[0]=8;GuangbiaoData[1]=8;
MoveToEnd();
printf("現在請用戶1下棋 \n");
printf("用戶1使用 w,s,a,d移動游標,空格鍵落子\n");
printf("用戶2使用各方向鍵移動游標,回車鍵落子\n");
printf("按下Backspace鍵悔棋,按下esc返回主菜單\n");
GotoChess(8,8);
}int BeOut(int data)
{
int Xiuzheng[2],New[2];
switch (data)
{
case 1:Xiuzheng[0]=0;Xiuzheng[1]=-1 ;break;
case 2:Xiuzheng[0]=0;Xiuzheng[1]=1;break;
case 3:Xiuzheng[0]=-1;Xiuzheng[1]=0;break;
case 4:Xiuzheng[0]=1;Xiuzheng[1]=0 ;break;
case 5:Xiuzheng[0]=0;Xiuzheng[1]=0 ;break;
default:printf("BeOut函數出錯");
}
New[0]=GuangbiaoData[0]+Xiuzheng[0];
New[1]=GuangbiaoData[1]+Xiuzheng[1];
if (New[0]>15||New[0]<1||New[1]>15||New[1]<1) return 1;
else return 0;
}void UserChoose(int * choice)
{
system("cls");
printf("________________________________________________________________________________");
printf("________________________________________________________________________________");
printf(" $1.單人游戲 ");
printf(" 2.雙人游戲 ");
printf(" 3.退出遊戲 ");
printf(" 4.游戲幫助 ");
printf("________________________________________________________________________________");
printf("________________________________________________________________________________");
printf(" ");
printf(" 開心五子棋 ");
printf(" ");
printf(" 出品人:張雲聰 ");
printf(" 學號:067108034 ");
printf(" 指導老師:邵艷玲 ");
printf(" ");
printf(" ");
printf(" 溫馨提示,游戲時請把窗口最大化,以達到最佳效果 ");
printf("________________________________________________________________________________");
int temp=0,i=0;
do
{
if ((temp=getch())==224)
{
temp=getch();
if (temp==72&&i!=0)
{
gotoxy(34,3+i);
printf(" ");
i--;
gotoxy(34,3+i);
printf("$");
gotoxy(0,0);
}
else if(temp==80&&i!=3)
{
gotoxy(34,3+i);
printf(" ");
i++;
gotoxy(34,3+i);
printf("$");
gotoxy(0,0);
}
} else if (temp==13) {*choice=i+1;return;}
else if (temp==27) {*choice=3;return;}
else if (temp=='1'||temp=='2'||temp=='3'||temp=='4') {*choice=temp-48;return;}}while(1);
}int CannotDo(int v1,int v2,int MoveData,int choice) //第一個輸入值為按鍵的用戶號,第二個是本應該按鍵的用戶號,第三個為按下鍵的對應值,第四個鍵代表游戲模式。
{
if (v1==3) return 0; //如果用戶輸入的為共用按鍵,則CannotDo為假else if (v1==0) return 1;//如果用戶輸入錯誤,則CannotDo為真
else if (v1!=v2&&choice==2) return 1; //如果不該此用戶輸入,而用戶進行了輸入,則CannotDo為真if (BeOut(MoveData)) return 1; //如果移動出邊界則CannotDo為真
return 0;
}int CannotLuozi() //判斷是否可以落子。
{
if (ChessData[GuangbiaoData[0]-1][GuangbiaoData[1]-1])
return 1;
else return 0;
}int luozi(int v) //玩家v落子。
{
ChessData[GuangbiaoData[0]-1][GuangbiaoData[1]-1]+=v*2;
if (v==1) printf("O");
else if (v==2) printf("X");
else printf("luozi函數出錯");
if (win(v)) {MoveToEnd();printf("玩家%d獲得了勝利! \n",v);for (int i=1;i<=240;i++) printf(" ");GotoChess(GuangbiaoData[0],GuangbiaoData[1]);getch();return 1;}
MoveToEnd();
printf("現在請用戶%d下棋 ",v%2+1);
GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
return 0;
}void HuiQi(int step) //輸入的是當前的要悔的棋是第幾步
{
GuangbiaoData[0]=ChessStepData[step-1][0];
GuangbiaoData[1]=ChessStepData[step-1][1];
ChessData[GuangbiaoData[0]-1][GuangbiaoData[1]-1]=0;

GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
printf("-");
MoveToEnd();
printf("現在請用戶%d下棋 ",(step+1)%2+1);
GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
}int DataGetAndChoose(int choice)
{
int MoveData=0,i=0,temp; //MoveData 0表示不可移動,1表示上,2表示下,3表示左,4表示右,5表示落子,6表示悔棋,7表示退出。
while(1)
{
loop: while (temp=Get(&MoveData),CannotDo(temp,i%2+1,MoveData,choice));
switch (MoveData)
{
case 1:
case 2:
case 3:
case 4:Move(MoveData);break;
case 7:return 0;
case 6:
if (i==0) {MoveToEnd();printf("現在無法悔棋 ");GotoChess(GuangbiaoData[0],GuangbiaoData[1]);}
else HuiQi(i--);
break;
case 5:
if (CannotLuozi()) goto loop;
if(luozi(i%2+1)) return 0;
ChessStepData[i][0]=GuangbiaoData[0];
ChessStepData[i][1]=GuangbiaoData[1];
i++;
break;
default:printf("DataGetAndChoose函數出錯");break;
}
}
return 1;
}void ShowHelp()
{
system("cls");
printf("********************************************************************************");
printf("********************************************************************************");
printf("****** 單人游戲供用戶一個人自己與自己下棋研究棋局之用 ******");
printf("****** 雙人游戲中,用戶1使用wsad控制方向,按空格落子 ******");
printf("****** 用戶2按方向鍵控制方向,回車鍵落子 ******");
printf("****** 游戲過程中按esc返回主菜單 ******");
printf("****** 游戲過程中退格鍵悔棋 ******");
printf("****** 雙人模式中某人下棋時,另一個用戶無法控制游標與落子 ******");
printf("****** ******");
printf("****** 幫助 ******");
printf("****** 按任意鍵返回 ******");
printf("********************************************************************************");
printf("********************************************************************************");
getch();
}int main()
{
int choice=0;
system("color 2b");
choose: UserChoose(&choice);
if (choice<1||choice>4) goto choose;
if (choice==3) {gotoxy(1,18);<br>printf("\n謝謝您的使用,再見 "); getch();return 0;}
if (choice==4) {ShowHelp(); goto choose;}
NewShow();
DataGetAndChoose(choice);
main();
return 0;
}

『肆』 五子棋C語言代碼

五子棋C語言代碼如下:
#include <stdio.h>
#include <bios.h>
#include <ctype.h>
#include <conio.h>
#include <dos.h>
#define CROSSRU 0xbf /*右上角點*/
#define CROSSLU 0xda /*左上角點*/
#define CROSSLD 0xc0 /*左下角點*/
#define CROSSRD 0xd9 /*右下角點*/
#define CROSSL 0xc3 /*左邊*/
#define CROSSR 0xb4 /*右邊*/
#define CROSSU 0xc2 /*上邊*/
#define CROSSD 0xc1 /*下邊*/
#define CROSS 0xc5 /*十字交叉點*/

/*定義棋盤左上角點在屏幕上的位置*/
#define MAPXOFT 5
#define MAPYOFT 2

/*定義1號玩家的操作鍵鍵碼*/
#define PLAY1UP 0x1157/*上移--'W'*/
#define PLAY1DOWN 0x1f53/*下移--'S'*/
#define PLAY1LEFT 0x1e41/*左移--'A'*/
#define PLAY1RIGHT 0x2044/*右移--'D'*/
#define PLAY1DO 0x3920/*落子--空格鍵*/

/*定義2號玩家的操作鍵鍵碼*/
#define PLAY2UP 0x4800/*上移--方向鍵up*/
#define PLAY2DOWN 0x5000/*下移--方向鍵down*/
#define PLAY2LEFT 0x4b00/*左移--方向鍵left*/
#define PLAY2RIGHT 0x4d00/*右移--方向鍵right*/
#define PLAY2DO 0x1c0d/*落子--回車鍵Enter*/

/*若想在游戲中途退出, 可按 Esc 鍵*/
#define ESCAPE 0x011b

/*定義棋盤上交叉點的狀態, 即該點有無棋子 */
/*若有棋子, 還應能指出是哪個玩家的棋子 */
#define CHESSNULL 0 /*沒有棋子*/
#define CHESS1 'O'/*一號玩家的棋子*/
#define CHESS2 'X'/*二號玩家的棋子*/

/*定義按鍵類別*/
#define KEYEX99v 0/*退出鍵*/
#define KEYFALLCHESS 1/*落子鍵*/
#define KEYMOVECURSOR 2/*游標移動鍵*/
#define KEYINVALID 3/*無效鍵*/

/*定義符號常量: 真, 假 --- 真為1, 假為0 */
#define TRUE 1
#define FALSE 0

/**********************************************************/
/* 定義數據結構 */

/*棋盤交叉點坐標的數據結構*/
struct point
{
int x,y;
};

或者下面這個:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define N 15
#define B 7
#define STOP -10000
#define OK 1
#define NO 0
#define UP 328
#define DOWN 336
#define LEFT 331
#define RIGHT 333

int a[N+1][N+1];
int zx,zy;
int write=1,biaoji=0;
struct zn{
long sum;

int y;

int x;

}w[N+1][N+1],max,max1;

void cbar(int i,int x,int y,int r);
void map(int a[][]);
int getkey();
int key();
void zuobiao(int x,int y,int i);
int tu(int a[][],int write);
int wtu(int a[][],int write);
int neng(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=neng(a);
if(n==STOP) goto end;
map(a);
}
end:
;
}

int neng(int a[N+1][N+1])

{
int i,j;
int k;
max.sum=-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(max.sum<w[i][j].sum)
{

max.sum=w[i][j].sum;
max.y=i;
max.x=j;
}

else if(max.sum==w[i][j].sum)
{

if(((max.y-zy)*(max.y-zy)+(max.x-zx)*(max.x-zx))>((i-zy)*(i-zy)+(j-zx)*(j-zx)))
max.sum=w[i][j].sum;
max.y=i;
max.x=j;
}
}
if(a[max.y][max.x]==0)

{
a[max.y][max.x]=-1;
zy=max.y;
zx=max.x;
}

}

int zh5(int y,int x,int a[N+1][N+1])
{

int i,j;
int b[6][6];
long c[13];

long d[6][6];
long temp;
for(i=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;

}

max1.sum=-1;

max1.y=0;

max1.x=0;

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

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

{

if(max1.sum<d[i][j])

{

max1.sum=d[i][j];

max1.y=i;

max1.x=j;

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

}

else if(max1.sum==d[i][j])

{

if(((i+y-1-zy)*(i+y-1-zy)+(j+x-1-zx)*(j+x-1-zx))>((max1.y+y-1-zy)*(max1.y+y-1-zy)+(max1.x+x-1-zx)*(max1.x+x-1-zx)))

{

max1.sum=d[i][j];

max1.y=i;

max1.x=j;

}

}

}

}

long zzh5(int b[6][6],int n)

{

int i,j,k,l,m;

switch(n)

{

case 1:i=b[1][1];j=b[1][2];k=b[1][3];l=b[1][4];m=b[1][5];break;

case 2:i=b[2][1];j=b[2][2];k=b[2][3];l=b[2][4];m=b[2][5];break;

case 3:i=b[3][1];j=b[3][2];k=b[3][3];l=b[3][4];m=b[3][5];break;

case 4:i=b[4][1];j=b[4][2];k=b[4][3];l=b[4][4];m=b[4][5];break;

case 5:i=b[5][1];j=b[5][2];k=b[5][3];l=b[5][4];m=b[5][5];break;

case 6:i=b[1][1];j=b[2][1];k=b[3][1];l=b[4][1];m=b[5][1];break;

case 7:i=b[1][2];j=b[2][2];k=b[3][2];l=b[4][2];m=b[5][2];break;

case 8:i=b[1][3];j=b[2][3];k=b[3][3];l=b[4][3];m=b[5][3];break;

case 9:i=b[1][4];j=b[2][4];k=b[3][4];l=b[4][4];m=b[5][4];break;

case 10:i=b[1][5];j=b[2][5];k=b[3][5];l=b[4][5];m=b[5][5];break;

case 11:i=b[1][1];j=b[2][2];k=b[3][3];l=b[4][4];m=b[5][5];break;

case 12:i=b[1][5];j=b[2][4];k=b[3][3];l=b[4][2];m=b[5][1];break;

}

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

return (900);

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

return(1000);

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

return(20);

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

return(20);

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

return(-60);

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

return(-60);

}

int wtu(int a[N+1][N+1],int write)

{

int i=1;

map(a);

zuobiao(zx,zy,1);

while(i)

{

int k;

k=tu(a,write);

if(k==OK) i=0;

if(k==STOP) return (STOP);

}

}

int getkey()

{

int key,lo,hi;

key=bioskey(0);

lo=key&0x00ff;

hi=(key&0xff00)>>8;

return((lo==0) ? hi+256:lo);

}

int key()

{

int k;

k=getkey();

switch(k)

{

case 27: return (STOP);

case 13:

case ' ': return (OK);

case 328: return (UP);

case 336: return (DOWN);

case 331: return (LEFT);

case 333: return (RIGHT);

default: return (NO);

}

}

void zuobiao(int x,int y,int i)

{

int r;

if(i!=0)

{

setcolor(GREEN);

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

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

}

else

{

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

{

setcolor(8);

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

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

}

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

{

setcolor(WHITE);

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

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

}

else

{

setcolor(B);

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

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

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

line(75+25*zx,25+25*zy-5,75+25*zx,25+25*zy+5);

}

}

}

int tu(int a[N+1][N+1],int write)

{

int k;

re:

k=key();

if(k==OK)

{

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

{

a[zy][zx]=write;

}

else

goto re;

}

if(k==STOP) return(STOP);

if(k==NO) goto re;

if(k==UP)

{

int i,j;

if(zy==1) j=zy;

else j=zy-1;

zuobiao(zx,zy,0);

zuobiao(zx,j,1);

zy=j;

goto re;

}

if(k==DOWN)

{

int i,j;

if(zy==N) j=zy;

else j=zy+1;

zuobiao(zx,zy,0);

zuobiao(zx,j,1);

zy=j;

goto re;

}

if(k==LEFT)

{

int i,j;

if(zx==1) i=zx;

else i=zx-1;

zuobiao(zx,zy,0);

zuobiao(i,zy,1);

zx=i;

goto re;

}

if(k==RIGHT)

{

int i,j;

if(zx==N) i=zx;

else i=zx+1;

zuobiao(zx,zy,0);

zuobiao(i,zy,1);

zx=i;

goto re;

}

}

void cbar(int i,int x,int y,int r)

{

if(i!=0)

{

if(i==1)

setcolor(8);

else if(i==-1)

setcolor(WHITE);

for(i=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+25*i,75+N*25,50+25*i);

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

}

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

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

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

}

『伍』 C語言/C++求五子棋的程序代碼和流程圖還有模塊設計,求求求,三者都要有~~~求大神幫忙。

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#define TEXTS 7
#define CURSOR 48
#define CHESSBOARD 352
#define WHITECHESS 103
#define SELECTEDWHITE 55
#define BLACKCHESS 96
#define SELECTEDBLACK 48
#define qx1_num 27
#define qx2_num 26
typedef struct node
{
int step;
int color;
} NODE;
typedef struct point
{
int x;
int y;
}_POINT;
typedef struct qixing
{
char qx[8];
int value;
}QIXING;
HANDLE hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
_POINT cursor;
int direction[8][2]={{0,-1},{0,1},{-1,0},{1,0},{-1,-1},{1,1},{-1,1},{1,-1}};
QIXING qx1[qx1_num]={{"x1111",200000},{"1x111",200000},{"11x11",200000},{"0x1110",6000},{"01x110",6000},{"101x101",6000},
{"0x111",1100},{"x0111",1100},{"0x1011",1100},{"0x1101",1100},{"01x11",1100},
{"011x1",1100},{"1x011",1100},{"10x11",1100},{"11x01",1100},{"1x101",1100},
{"x011102",250},{"0x110",250},{"01x10",250},{"0x01102",240},{"0x101",240},
{"0x112",20},{"01x12",10},{"011x2",20},{"1x12",10},{"0x10",20},{"0x010",5}};
QIXING qx2[qx2_num]={{"x1111",2000000},{"春巧1x111",2000000},{"11x11",2000000},{"0x1110"扒搏鍵,24000},{"01x110",24000},{"101x101"銀旁,24000},
{"0x111",2000},{"x0111",1900},{"0x1011",1900},{"0x1101",2000},{"01x11",2000},
{"011x1",2000},{"1x011",1900},{"10x11",2000},{"1x101",2000},{"x01112",2000},
{"0x110",850},{"01x10",850},{"0x0110",840},{"0x101",840},
{"0x112",125},{"01x12",125},{"011x2",115},{"1x12",115},{"0x10",125},{"0x010",110}};
//------------------------------------------------------------------------------------------------------
void textcolor(int color)
{
SetConsoleTextAttribute(hOutput, color);
}
void gotoxy(int x, int y)
{
COORD coordScreen={0,0};
coordScreen.X=x;
coordScreen.Y=y;
SetConsoleCursorPosition(hOutput,coordScreen);
}
void printnode(NODE chessboard[][15], int x, int y)
{
textcolor(CHESSBOARD);
if(chessboard[x][y].step==0)
{
if(x==cursor.x&&y==cursor.y)
textcolor(CURSOR);
switch(x)
{
case 0:
if(y==0)printf("┏");
else if(y==14)printf("┓");
else printf("┳");
break;
case 3:
if(y==0)printf("┣");
else if(y==3||y==11)printf("╬");
else if(y==14)printf("┫");
else printf("╋");
break;
case 7:
if(y==0)printf("┣");
else if(y==7)printf("╬");
else if(y==14)printf("┫");
else printf("╋");
break;
case 11:
if(y==0)printf("┣");
else if(y==3||y==11)printf("╬");
else if(y==14)printf("┫");
else printf("╋");
break;
case 14:
if(y==0)printf("┗");
else if(y==14)printf("┛");
else printf("┻");
break;
default:
if(y==0)printf("┣");
else if(y==14)printf("┫");
else printf("╋");
}
}
else if(chessboard[x][y].color==0)
{
if(x==cursor.x&&y==cursor.y)
textcolor(SELECTEDWHITE);
else textcolor(WHITECHESS);
printf("●");
}
else
{
if(x==cursor.x&&y==cursor.y)
textcolor(SELECTEDBLACK);
else textcolor(BLACKCHESS);
printf("●");
}
}
void printchessboard(NODE chessboard[][15])
{
int i,j;
char letter[]={" A B C D E F G H I J K L M N O\n"};
for(i=0;i<15;i++)
{
textcolor(TEXTS);
printf("%2d",15-i);
for(j=0;j<15;j++)
printnode(chessboard,i,j);
textcolor(TEXTS);
printf("\n");
}
textcolor(TEXTS);
printf("%s",letter);
printf("移動:方向鍵 下棋:ENTER 悔棋:U 退出:F12");
}
void renew(NODE chessboard[][15],int x,int y)
{
COORD coordScreen;
CONSOLE_SCREEN_BUFFER_INFO csbi;
if(x<0||x>14||y<0||y>14)
return;
if(!GetConsoleScreenBufferInfo(hOutput,&csbi))
return;
coordScreen=csbi.dwCursorPosition;
gotoxy((y-1)*2+4,x+1);
printnode(chessboard,x,y);
SetConsoleCursorPosition(hOutput,coordScreen);
}
void showmenu()
{
textcolor(TEXTS);
system("cls");
printf("1.人機對戰\n2.雙人對戰\n3.退出\n\n請選擇[1~3]:");
}
void showsubmenu()
{
textcolor(TEXTS);
system("cls");
printf("1.你先手\n2.電腦先手\n3.返回上級菜單\n\n請選擇[1~3]:");
}
int getchoose(int min, int max)
{
int choose;
do
{
choose=getch()-48;
}while(choose<min||choose>max);
printf("%d",choose);
return choose;
}
//-------------------------------------------------------------------------------------------------
bool quit;
bool regret;
bool getmove(NODE chessboard[][15])
{
char c;
for(;;)
{
c=getch();
if(c==-32)
switch(getch())
{
case 72:
cursor.x--;
if(cursor.x<0)cursor.x=0;
renew(chessboard,cursor.x+1,cursor.y);
renew(chessboard,cursor.x,cursor.y);
break;
case 80:
cursor.x++;
if(cursor.x>14)
cursor.x=14;
renew(chessboard,cursor.x-1,cursor.y);
renew(chessboard,cursor.x,cursor.y);
break;
case 75:
cursor.y--;
if(cursor.y<0)cursor.y=0;
renew(chessboard,cursor.x,cursor.y+1);
renew(chessboard,cursor.x,cursor.y);
break;
case 77:
cursor.y++;
if(cursor.y>14)cursor.y=14;
renew(chessboard,cursor.x,cursor.y-1);
renew(chessboard,cursor.x,cursor.y);
break;
case 134:
quit=true;
return true;
}
else if(c==13&&chessboard[cursor.x][cursor.y].step==0)
return true;
else if(c=='U'||c=='u')
{
regret=true;
return true;
}
}
}
void beback(NODE chessboard[][15], int step)
{
int i,j,tempx,tempy;
if(step==1)return;
if(step>2)
{
for(i=0;i<15;i++)
for(j=0;j<15;j++)
{
if(chessboard[i][j].step==step-1)
{
chessboard[i][j].step=0;
renew(chessboard,i,j);
}
else if(chessboard[i][j].step==step-2)
{
chessboard[i][j].step=0;
tempx=cursor.x;
tempy=cursor.y;
cursor.x=i;
cursor.y=j;
renew(chessboard,i,j);
renew(chessboard,tempx,tempy);
}
}
}
else if(step==2)
{
for(i=0;i<15;i++)
for(j=0;j<15;j++)
if(chessboard[i][j].step==step-1)
{
chessboard[i][j].step=0;
renew(chessboard,i,j);
}
tempx=cursor.x;
tempy=cursor.y;
cursor.x=7;
cursor.y=7;
renew(chessboard,i,j);
renew(chessboard,tempx,tempy);
}
}
//-----------------------------------------------------------------------------------------
bool inside(int x,int y)
{
if(x<0||x>14||y<0||y>14)return false;
return true;
}
int line(NODE chessboard[][15],int dirt,int x,int y,int color)
{
int i;
for(i=0;chessboard[x+direction[dirt][0]][y+direction[dirt][1]].step>0&&
chessboard[x+direction[dirt][0]][y+direction[dirt][1]].color==color;i++)
{
x=x+direction[dirt][0];
y=y+direction[dirt][1];
if(!inside(x,y))return i;
}
return i;
}
bool win(NODE chessboard[][15],int x,int y,int color)
{
if(line(chessboard,0,x,y,color)+line(chessboard,1,x,y,color)>3)
return true;
if(line(chessboard,2,x,y,color)+line(chessboard,3,x,y,color)>3)
return true;
if(line(chessboard,4,x,y,color)+line(chessboard,5,x,y,color)>3)
return true;
if(line(chessboard,6,x,y,color)+line(chessboard,7,x,y,color)>3)
return true;
return false;
}
//----------------------------------------------------------------------------------------------
int attacktrend,defenttrend;
bool macth1(NODE chessboard[][15],int x,int y,int dirt,int kind,int color)
{
int k;
char c;
char *p;
p=strchr(qx1[kind].qx,'x');
for(k=0;k<=p-qx1[kind].qx;k++)
{
x-=direction[dirt][0];
y-=direction[dirt][1];
}
for(k=0;(unsigned)k<strlen(qx1[kind].qx);k++)
{
x+=direction[dirt][0];
y+=direction[dirt][1];
if(!inside(x,y))return(false);
if(chessboard[x][y].step>0&&chessboard[x][y].color==color)c='2';
else if(chessboard[x][y].step>0)c='1';
else c='0';
if(c=='0'&&qx1[kind].qx[k]=='x')continue;
if(c!=qx1[kind].qx[k])return(false);
}
return true;
}
int value_qx1(NODE chessboard[][15],int x,int y,int dirt, int color)
{
int i;
for(i=0;i<qx1_num;i++)
if(macth1(chessboard,x,y,dirt,i,color))
return qx1[i].value;
return 0;
}
bool macth2(NODE chessboard[][15],int x,int y,int dirt, int kind,int color)
{
int k;
char c;
char *p;
p=strchr(qx2[kind].qx,'x');
for(k=0;k<=p-qx2[kind].qx;k++)
{
x-=direction[dirt][0];
y-=direction[dirt][1];
}
for(k=0;(unsigned)k<strlen(qx2[kind].qx);k++)
{
x+=direction[dirt][0];
y+=direction[dirt][1];
if(!inside(x,y))return false;
if(chessboard[x][y].step>0&&chessboard[x][y].color==color)c='2';
else if(chessboard[x][y].step>0)c='1';
else c='0';
if(c=='0'&&qx2[kind].qx[k]=='x')continue;
if(c!=qx2[kind].qx[k])return(false);
}
return true;
}
int value_qx2(NODE chessboard[][15],int x,int y,int dirt, int color)
{
int i;
for(i=0;i<qx2_num;i++)
if(macth2(chessboard,x,y,dirt,i,color))
return qx2[i].value;
return 0;
}
void AI(NODE chessboard[][15], int *x,int *y,int color)
{
int max=0;
int maxi,maxj;
int i,j,k;
int probability=1;
int value[15][15]={0};
int valueattack[15][15]={{0}};
int valuedefent[15][15]={{0}};
for(i=0;i<15;i++)
for(j=0;j<15;j++)
{
if(chessboard[i][j].step>0)continue;
for(k=0;k<8;k++)
valuedefent[i][j]+=value_qx1(chessboard,i,j,k,color);
if(maxi<valuedefent[i][j])
maxi=valuedefent[i][j];
}
for(i=0;i<15;i++)
for(j=0;j<15;j++)
{
if(chessboard[i][j].step>0)continue;
for(k=0;k<8;k++)
valueattack[i][j]+=value_qx2(chessboard,i,j,k,color);
if(maxj<valuedefent[i][j])
maxj=valuedefent[i][j];
}
if(rand()%(maxi+maxj+1)>maxi)
{
attacktrend=1;
defenttrend=1;
}
else
{
attacktrend=1;
defenttrend=2;
}
for(i=0;i<15;i++)
for(j=0;j<15;j++)
{
value[i][j]=valuedefent[i][j]*defenttrend+valueattack[i][j]*attacktrend;
if(max<value[i][j])
{
max=value[i][j];
maxi=i;
maxj=j;
probability=1;
}
else if(max==value[i][j])
{
if(rand()%(probability+1)<probability)
probability++;
else
{
probability=1;
max=value[i][j];
maxi=i;
maxj=j;
}
}
}
*x=maxi;
*y=maxj;
}
//-------------------------------------------------------------------------------------------------
bool vshuman;
void Vs(bool human)
{
int i,j;
int color=1;
int lastx,lasty;
int computer;
NODE chessboard[15][15]={{0,0}};
if(!human)
{
showsubmenu();
switch(getchoose(1,3))
{
case 1:
computer=0;
attacktrend=1;
defenttrend=1;
break;
case 2:
computer=1;
attacktrend=1;
defenttrend=2;
break;
case 3:return;
}
}
for(i=0;i<15;i++)
for(j=0;j<15;j++)
chessboard[i][j].step=0;
cursor.x=7;
cursor.y=7;
quit=false;
system("cls");
printf("\n");
printchessboard(chessboard);
for(i=1;i<=225;)
{
gotoxy(0,0);
textcolor(TEXTS);
printf(" 第%03d手, ",i);
if(color==1)printf("黑棋下");
else printf("白棋下");
regret=false;
if(i>1)
{
if(color!=computer||human)getmove(chessboard);
else
{
lastx=cursor.x;
lasty=cursor.y;
AI(chessboard,&cursor.x,&cursor.y,color);
renew(chessboard,lastx,lasty);
}
}
if(quit)return;
if(regret)
{
beback(chessboard,i);
if(i>2)i-=2;
else if(i==2)
{
i=1; color=(color+1)%2;
}
}
else
{
chessboard[cursor.x][cursor.y].step=i++;
chessboard[cursor.x][cursor.y].color=color;
renew(chessboard,cursor.x,cursor.y);
color=(color+1)%2;
}
if(win(chessboard,cursor.x,cursor.y,(color+1)%2)&&!regret)
{
textcolor(TEXTS);
gotoxy(0,0);
printf(" ");
gotoxy(0,0);
if(color==1)printf(" 白棋贏了!");
else printf(" 黑棋贏了!");
getch();
return;
}
}
gotoxy(0,0);
printf(" ");
gotoxy(0,0);
printf(" 平局!");
}
int main()
{
srand((unsigned)time(NULL));
for(;;)
{
showmenu();
switch(getchoose(1,3))
{
case 1:Vs(false);break;
case 2:Vs(true);break;
case 3:printf("\n");return 0;
}
}
return 0;
}

『陸』 求一個用C語言編寫五子棋游戲的全部代碼.

這個程序還需要兩個改滾如文件,告訴我你郵箱,我發給你
源程序前半部分
/*載入頭文件*/
#include<stdio.h>
#include<備好stdlib.h>
#include<graphics.h>
#include<bios.h>
#include<conio.h>
/*編譯預處理,定義按鍵碼*/
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
/*若想在游戲中途退出, 可按 Esc 鍵*/
#define ESC 0x011b
/*SPACE鍵表示落子*/
#define SPACE 0x3920
/*設置偏移量*/
#define OFFSET 20
#define OFFSET_x 4
#define OFFSET_y 3
/*定義數組大小*/
#define N 19
/*定義全局變數*/
int status[N][N]; /*定義的數組,保存狀態*/
int step_x,step_y;/核啟*行走的坐標*/
int key ; /*獲取按下的鍵盤的鍵*/
int flag; /*玩家標志*/
/*自定義函數原型*/
void DrawBoard();
void DrawCircle(int x,int y,int color);
void Alternation();
void JudgePlayer(int x,int y);
void Done();
int ResultCheck(int x,int y);
void WelcomeInfo();
void ShowMessage();
/*定義函數*/
/*顯示歡迎信息函數*/
void WelcomeInfo()
{
char ch ;
/*移動游標到指定位置*/
gotoxy(12,4);
/*顯示歡迎信息*/
printf("Welcome you to gobang word!");
gotoxy(12,6);
printf("1.You can use the up,down,left and right key to move the chessman,");
gotoxy(12,8);
printf(" and you can press Space key to enter after you move it !");
gotoxy(12,10);
printf("2.You can use Esc key to exit the game too !");
gotoxy(12,12);

『柒』 用C語言編寫一個五子棋的游戲程序

**********************************************************/
/* 程序中用到的庫函數所在頭文件應用 #include 命令包含進來 */

#include <stdio.h>
#include <bios.h>
#include <ctype.h>
#include <conio.h>
#include <dos.h>

/**********************************************************/
/* 定義符號常量 */

/*定義畫棋盤所需的製表符*/
#define CROSSRU 0xbf /*右上角點*/
#define CROSSLU 0xda /*左上角點*/
#define CROSSLD 0xc0 /*左下角點*/
#define CROSSRD 0xd9 /*右下角點*/
#define CROSSL 0xc3 /*左邊*/
#define CROSSR 0xb4 /*右邊*/
#define CROSSU 0xc2 /*上邊*/
#define CROSSD 0xc1 /*下邊*/
#define CROSS 0xc5 /*十字交叉點*/

/*定義棋盤左上角點在屏幕上的位置*/
#define MAPXOFT 5
#define MAPYOFT 2

/*定義1號玩家的操作鍵鍵碼*/
#define PLAY1UP 0x1157/*上移--'W'*/
#define PLAY1DOWN 0x1f53/*下移--'S'*/
#define PLAY1LEFT 0x1e41/*左移--'A'*/
#define PLAY1RIGHT 0x2044/*右移--'D'*/
#define PLAY1DO 0x3920/*落子--空格鍵*/

/*定義2號玩家的操作鍵鍵碼*/
#define PLAY2UP 0x4800/*上移--方向鍵up*/
#define PLAY2DOWN 0x5000/*下移--方向鍵down*/
#define PLAY2LEFT 0x4b00/*左移--方向鍵left*/
#define PLAY2RIGHT 0x4d00/*右移--方向鍵right*/
#define PLAY2DO 0x1c0d/*落子--回車鍵Enter*/

/*若想在游戲中途退出, 可按 Esc 鍵*/
#define ESCAPE 0x011b

/*定義棋盤上交叉點的狀態, 即該點有無棋子 */
/*若有棋子, 還應能指出是哪個玩家的棋子 */
#define CHESSNULL 0 //沒有棋子
#define CHESS1 'O'//一號玩家的棋子
#define CHESS2 'X'//二號玩家的棋子

/*定義按鍵類別*/
#define KEYEXIT 0/*退出鍵*/
#define KEYFALLCHESS 1/*落子鍵*/
#define KEYMOVECURSOR 2/*游標移動鍵*/
#define KEYINVALID 3/*無效鍵*/

/*定義符號常量: 真, 假 --- 真為1, 假為0 */
#define TRUE 1
#define FALSE 0

/**********************************************************/
/* 定義數據結構 */

/*棋盤交叉點坐標的數據結構*/
struct point
{
int x,y;
};

/**********************************************************/
/*自定義函數原型說明 */
void Init(void);
int GetKey(void);
int CheckKey(int press);
int ChangeOrder(void);
int ChessGo(int Order,struct point Cursor);
void DoError(void);
void DoOK(void);
void DoWin(int Order);
void MoveCursor(int Order,int press);
void DrawCross(int x,int y);
void DrawMap(void);
int JudgeWin(int Order,struct point Cursor);
int JudgeWinLine(int Order,struct point Cursor,int direction);
void ShowOrderMsg(int Order);
void EndGame(void);
/**********************************************************/

/**********************************************************/
/* 定義全局變數 */
int gPlayOrder; /*指示當前行棋方 */
struct point gCursor; /*游標在棋盤上的位置 */
char gChessBoard[19][19];/*用於記錄棋盤上各點的狀態*/
/**********************************************************/

/**********************************************************/
/*主函數*/
void main()
{
int press;
int bOutWhile=FALSE;/*退出循環標志*/

Init();/*初始化圖象,數據*/

while(1)
{
press=GetKey();/*獲取用戶的按鍵值*/
switch(CheckKey(press))/*判斷按鍵類別*/
{
/*是退出鍵*/
case KEYEXIT:
clrscr();/*清屏*/
bOutWhile = TRUE;
break;

/*是落子鍵*/
case KEYFALLCHESS:
if(ChessGo(gPlayOrder,gCursor)==FALSE)/*走棋*/
DoError();/*落子錯誤*/
else
{
DoOK();/*落子正確*/

/*如果當前行棋方贏棋*/
if(JudgeWin(gPlayOrder,gCursor)==TRUE)
{
DoWin(gPlayOrder);
bOutWhile = TRUE;/*退出循環標志置為真*/
}
/*否則*/
else
/*交換行棋方*/
ChangeOrder();
}
break;

/*是游標移動鍵*/
case KEYMOVECURSOR:
MoveCursor(gPlayOrder,press);
break;

/*是無效鍵*/
case KEYINVALID:
break;
}

if(bOutWhile==TRUE)
break;
}

/*游戲結束*/
EndGame();
}
/**********************************************************/

/*界面初始化,數據初始化*/
void Init(void)
{
int i,j;
char *Msg[]=
{
"Player1 key:",
" UP----w",
" DOWN--s",
" LEFT--a",
" RIGHT-d",
" DO----space",
"",
"Player2 key:",
" UP----up",
" DOWN--down",
" LEFT--left",
" RIGHT-right",
" DO----ENTER",
"",
"exit game:",
" ESC",
NULL,
};

/*先手方為1號玩家*/
gPlayOrder = CHESS1;
/*棋盤數據清零, 即棋盤上各點開始的時候都沒有棋子*/
for(i=0;i<19;i++)
for(j=0;j<19;j++)
gChessBoard[i][j]=CHESSNULL;
/*游標初始位置*/
gCursor.x=gCursor.y=0;

/*畫棋盤*/
textmode(C40);
DrawMap();

/*顯示操作鍵說明*/
i=0;
textcolor(BROWN);
while(Msg[i]!=NULL)
{
gotoxy(25,3+i);
cputs(Msg[i]);
i++;
}

/*顯示當前行棋方*/
ShowOrderMsg(gPlayOrder);
/*游標移至棋盤的左上角點處*/
gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);
}

/*畫棋盤*/
void DrawMap(void)
{
int i,j;

clrscr();

for(i=0;i<19;i++)
for(j=0;j<19;j++)
DrawCross(i,j);

}

/*畫棋盤上的交叉點*/
void DrawCross(int x,int y)
{
gotoxy(x+MAPXOFT,y+MAPYOFT);
/*交叉點上是一號玩家的棋子*/
if(gChessBoard[x][y]==CHESS1)
{
textcolor(LIGHTBLUE);
putch(CHESS1);
return;
}
/*交叉點上是二號玩家的棋子*/
if(gChessBoard[x][y]==CHESS2)
{
textcolor(LIGHTBLUE);
putch(CHESS2);
return;
}

textcolor(GREEN);

/*左上角交叉點*/
if(x==0&&y==0)
{
putch(CROSSLU);
return;
}

/*左下角交叉點*/
if(x==0&&y==18)
{
putch(CROSSLD);
return;
}

/*右上角交叉點*/
if(x==18&&y==0)
{
putch(CROSSRU);
return;
}

/*右下角交叉點*/
if(x==18&&y==18)
{
putch(CROSSRD);
return;
}

/*左邊界交叉點*/
if(x==0)
{
putch(CROSSL);
return;
}

/*右邊界交叉點*/
if(x==18)
{
putch(CROSSR);
return;
}

/*上邊界交叉點*/
if(y==0)
{
putch(CROSSU);
return;
}

/*下邊界交叉點*/
if(y==18)
{
putch(CROSSD);
return;
}

/*棋盤中間的交叉點*/
putch(CROSS);
}

/*交換行棋方*/
int ChangeOrder(void)
{
if(gPlayOrder==CHESS1)
gPlayOrder=CHESS2;
else
gPlayOrder=CHESS1;

return(gPlayOrder);
}

/*獲取按鍵值*/
int GetKey(void)
{
char lowbyte;
int press;

while (bioskey(1) == 0)
;/*如果用戶沒有按鍵,空循環*/

press=bioskey(0);
lowbyte=press&0xff;
press=press&0xff00 + toupper(lowbyte);
return(press);
}

/*落子錯誤處理*/
void DoError(void)
{
sound(1200);
delay(50);
nosound();
}

/*贏棋處理*/
void DoWin(int Order)
{
sound(1500);delay(100);
sound(0); delay(50);
sound(800); delay(100);
sound(0); delay(50);
sound(1500);delay(100);
sound(0); delay(50);
sound(800); delay(100);
sound(0); delay(50);
nosound();

textcolor(RED+BLINK);
gotoxy(25,20);
if(Order==CHESS1)
cputs("PLAYER1 WIN!");
else
cputs("PLAYER2 WIN!");
gotoxy(25,21);
cputs(" \\<^+^>/");
getch();
}

/*走棋*/
int ChessGo(int Order,struct point Cursor)
{
/*判斷交叉點上有無棋子*/
if(gChessBoard[Cursor.x][Cursor.y]==CHESSNULL)
{
/*若沒有棋子, 則可以落子*/
gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT);
textcolor(LIGHTBLUE);
putch(Order);
gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT);
gChessBoard[Cursor.x][Cursor.y]=Order;
return TRUE;
}
else
return FALSE;
}

/*判斷當前行棋方落子後是否贏棋*/
int JudgeWin(int Order,struct point Cursor)
{
int i;
for(i=0;i<4;i++)
/*判斷在指定方向上是否有連續5個行棋方的棋子*/
if(JudgeWinLine(Order,Cursor,i))
return TRUE;
return FALSE;
}

/*判斷在指定方向上是否有連續5個行棋方的棋子*/
int JudgeWinLine(int Order,struct point Cursor,int direction)
{
int i;
struct point pos,dpos;
const int testnum = 5;
int count;

switch(direction)
{
case 0:/*在水平方向*/
pos.x=Cursor.x-(testnum-1);
pos.y=Cursor.y;
dpos.x=1;
dpos.y=0;
break;
case 1:/*在垂直方向*/
pos.x=Cursor.x;
pos.y=Cursor.y-(testnum-1);
dpos.x=0;
dpos.y=1;
break;
case 2:/*在左下至右上的斜方向*/
pos.x=Cursor.x-(testnum-1);
pos.y=Cursor.y+(testnum-1);
dpos.x=1;
dpos.y=-1;
break;
case 3:/*在左上至右下的斜方向*/
pos.x=Cursor.x-(testnum-1);
pos.y=Cursor.y-(testnum-1);
dpos.x=1;
dpos.y=1;
break;
}

count=0;
for(i=0;i<testnum*2+1;i++)
{
if(pos.x>=0&&pos.x<=18&&pos.y>=0&&pos.y<=18)
{
if(gChessBoard[pos.x][pos.y]==Order)
{
count++;
if(count>=testnum)
return TRUE;
}
else
count=0;
}
pos.x+=dpos.x;
pos.y+=dpos.y;
}

return FALSE;
}

/*移動游標*/
void MoveCursor(int Order,int press)
{
switch(press)
{
case PLAY1UP:
if(Order==CHESS1&&gCursor.y>0)
gCursor.y--;
break;
case PLAY1DOWN:
if(Order==CHESS1&&gCursor.y<18)
gCursor.y++;
break;
case PLAY1LEFT:
if(Order==CHESS1&&gCursor.x>0)
gCursor.x--;
break;
case PLAY1RIGHT:
if(Order==CHESS1&&gCursor.x<18)
gCursor.x++;
break;

case PLAY2UP:
if(Order==CHESS2&&gCursor.y>0)
gCursor.y--;
break;
case PLAY2DOWN:
if(Order==CHESS2&&gCursor.y<18)
gCursor.y++;
break;
case PLAY2LEFT:
if(Order==CHESS2&&gCursor.x>0)
gCursor.x--;
break;
case PLAY2RIGHT:
if(Order==CHESS2&&gCursor.x<18)
gCursor.x++;
break;
}

gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);
}

/*游戲結束處理*/
void EndGame(void)
{
textmode(C80);
}

/*顯示當前行棋方*/
void ShowOrderMsg(int Order)
{
gotoxy(6,MAPYOFT+20);
textcolor(LIGHTRED);
if(Order==CHESS1)
cputs("Player1 go!");
else
cputs("Player2 go!");

gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);
}

/*落子正確處理*/
void DoOK(void)
{
sound(500);
delay(70);
sound(600);
delay(50);
sound(1000);
delay(100);
nosound();
}

/*檢查用戶的按鍵類別*/
int CheckKey(int press)
{
if(press==ESCAPE)
return KEYEXIT;/*是退出鍵*/

else
if
( ( press==PLAY1DO && gPlayOrder==CHESS1) ||
( press==PLAY2DO && gPlayOrder==CHESS2)
)
return KEYFALLCHESS;/*是落子鍵*/

else
if
( press==PLAY1UP || press==PLAY1DOWN ||
press==PLAY1LEFT || press==PLAY1RIGHT ||
press==PLAY2UP || press==PLAY2DOWN ||
press==PLAY2LEFT || press==PLAY2RIGHT
)
return KEYMOVECURSOR;/*是游標移動鍵*/

else
return KEYINVALID;/*按鍵無效*/
}

『捌』 請用所學的C語言實現一個命令行下的五子棋游戲。要求有棋盤界面,並實現人人,人機機人的三種對弈模式。

http://tieba..com/f?ct=335675392&tn=PostBrowser&sc=4335990148&z=430611008&pn=0&rn=30&lm=0&word=c%2B%2B#4335990148
網路貼吧裡面的,我試過可以運行,有人機對戰功能~
#include <iostream.h>
#include <process.h>
#include <conio.h>
class five
{
int x;
int y;
int m;
int n;
int num_xy;
int num_mn;
char qipan[20][20];
public:
five(int X = 0,int Y =0,int M = 0,int N = 0,int Num_xy = 0,int Num_mn = 0)
{
x = X;
y = Y;
m = M;
n = N;
num_xy = Num_xy;
num_mn = Num_mn;
for(int i = 0 ; i < 20 ; i ++)
{
for(int j = 0 ;j < 20 ; j ++)
qipan[i][j] = '.';
}

}
int getm()
{
return m;
}
int getn()
{
return n;
}
void getpointxy();
void getpointmn();
void machinemn();
void showqipan();
void print();
bool IsxyWin();
bool IsmnWin();
int dangerlevel(int a,int b);
void attack();
};
void five::getpointxy()
{
cout<<"請分別輸入第"<<num_xy+1<<"步白棋('o')的橫坐標和縱坐標:";
cin >> x >> y;
if(x <= 0 || y <= 0 || x > 20 || y > 20 )
{
cout << "出界,請重新輸入:"<<endl ;
getpointxy();
}
else if(qipan[y-1][x-1] != '.' )
{
cout << "請所下的位置已經有棋子,請重新輸入:"悶脊 <<endl;
getpointxy();
}
else if(num_xy+num_mn >= 400)
{
cout << "棋盤已滿,平局!" << endl;
exit(0);
}
else
{
qipan[y-1][x-1] = 'o';
num_xy++;
}

}
void five::getpointmn()
{
cout<<"請分別衡罩宴輸入第"<<num_mn+1<<"步黑棋('x')的橫坐標和縱坐標:";
cin >> m >> n;
if(m <= 0 || n <= 0 || m > 20 || n > 20 )
{
cout << "咐銀出界,請重新輸入:"<<endl ;
getpointmn();
}
else if(qipan[n-1][m-1]!='.')
{
cout << "請所下的位置已經有棋子,請重新輸入:" <<endl;
getpointmn();
}
else if(num_xy+num_mn >= 400)
{
cout << "棋盤已滿,平局!" << endl;
exit(0);
}
else
{
qipan[n-1][m-1] = 'x';
num_mn++;
}

}

int five::dangerlevel(int a, int b)
{
int level = 0;
for(int i = 1 ; a+i<=19; i ++)
{
if(qipan[a+i][b] == '.' && a + i <= 19)
{
i = i+1;
break;
}
if(qipan[a +i][b] == 'x' && a + i <= 19)
break;

}
level += (i-1);
for(i = 1 ; a-i>=0; i ++)
{
if(qipan[a-i][b] == '.' && (a-i) >= 0)
{
i = i+1;
break;
}
if(qipan[a-i][b] == 'x' && (a-i) >= 0)
break;
}
level += (i-1);
for(i = 1 ; b+i<=19 ; i ++)
{
if(qipan[a][b + i] == '.' && b + i <= 19)
{
i = i+1;
break;
}
if(qipan[a][b + i] == 'x' && b + i <= 19)
break;
}
level += (i-1);
for( i = 1 ; b-i>=0 ; i ++)
{
if(qipan[a][b - i] == '.' && (b-i) >= 0)
{
i = i+1;
break;
}
if(qipan[a][b - i] == 'x' && (b-i) >= 0)
break;
}
for( i = 1 ; a+i<=19&& b+i<=19; i ++)
{
if(qipan[a + i][b + i] == '.' && b+i <= 19 && a+i <= 19)
{
i = i+1;
break;
}
if(qipan[a + i][b + i] == 'x' && b+i <= 19 && a+i <= 19)
break;
}
level += (i-1);
for( i = 1 ; a-i>= 0 && b-i >= 0; i ++)
{
if(qipan[a - i][b - i] == '.' && a-i>= 0 && b-i >= 0)
{
i = i+1;
break;
}
if(qipan[a - i][b - i] == 'x' && a-i>= 0 && b-i >= 0)
break;
}
level += (i-1);
for( i = 1 ; a-i>= 0 && b+i <= 19 ; i ++)
{
if(qipan[a - i][b + i] == '.' && a-i>= 0 && b+i <= 19)
{
i = i+1;
break;
}
if(qipan[a - i][b + i] == 'x' && a-i>= 0 && b+i <= 19)
break;
}
level += (i-1);
for( i = 1 ; a + i<= 19 && b-i >= 0 ; i ++)
{
if(qipan[a + i][b - i] == '.' && a + i<= 19 && b-i >= 0)
{
i = i+1;
break;
}
if(qipan[a + i][b - i] == 'x' && a + i<= 19 && b-i >= 0)
break;
}
level += (i-1);
return level;
}

void five::attack()
{
int num1=1,num2=1,num3=1,num4=1;
int A = 20 , B = 20 ,max = 0;
for(int a = 0 ; a < 19 ; a ++)
{
for(int b = 0 ; b < 19 ; b ++)
{
if(qipan[a][b] == '.')
{
int level = 0;
for(int i = 1 ; a+i<=19; i ++)
{
if(qipan[a+i][b] == '.' && a + i <= 19)
{
i = i+1;
break;
}
if(qipan[a +i][b] == 'o' && a + i <= 19)
break;

}
level += (i-1);
for(i = 1 ; a-i>=0; i ++)
{
if(qipan[a-i][b] == '.' && (a-i) >= 0)
{
i = i+1;
break;
}
if(qipan[a-i][b] == 'o' && (a-i) >= 0)
break;
}
level += (i-1);
for(i = 1 ; b+i<=19 ; i ++)
{
if(qipan[a][b + i] == '.' && b + i <= 19)
{
i = i+1;
break;
}
if(qipan[a][b + i] == 'o' && b + i <= 19)
break;
}
level += (i-1);
for( i = 1 ; b-i>=0 ; i ++)
{
if(qipan[a][b - i] == '.' && (b-i) >= 0)
{
i = i+1;
break;
}
if(qipan[a][b - i] == 'o' && (b-i) >= 0)
break;
}
for( i = 1 ; a+i<=19&& b+i<=19; i ++)
{
if(qipan[a + i][b + i] == '.' && b+i <= 19 && a+i <= 19)
{
i = i+1;
break;
}
if(qipan[a + i][b + i] == 'o' && b+i <= 19 && a+i <= 19)
break;
}
level += (i-1);
for( i = 1 ; a-i>= 0 && b-i >= 0; i ++)
{
if(qipan[a - i][b - i] == '.' && a-i>= 0 && b-i >= 0)
{
i = i+1;
break;
}
if(qipan[a - i][b - i] == 'o' && a-i>= 0 && b-i >= 0)
break;
}
level += (i-1);
for( i = 1 ; a-i>= 0 && b+i <= 19 ; i ++)
{
if(qipan[a - i][b + i] == '.' && a-i>= 0 && b+i <= 19)
{
i = i+1;
break;
}
if(qipan[a - i][b + i] == 'o' && a-i>= 0 && b+i <= 19)
break;
}
level += (i-1);
for( i = 1 ; a + i<= 19 && b-i >= 0 ; i ++)
{
if(qipan[a + i][b - i] == '.' && a + i<= 19 && b-i >= 0)
{
i = i+1;
break;
}
if(qipan[a + i][b - i] == 'o' && a + i<= 19 && b-i >= 0)
break;
}
level += (i-1);
if(level >= max)
{
n = a + 1;
m = b + 1;
max = level;
}
}
else
continue;
}
}
qipan[n-1][m-1] = 'x';
}
void five::machinemn()
{

int Num1=1,Num2=1,Num3=1,Num4=1,A11 = 21,A12 = 21,A21 = 21,A22 = 21,A31 = 21,A32 = 21,A41 = 21,A42 = 21;
for(int i = x ; i < x+5&& x+5<20 ; i ++)
{
if(qipan[y-1][i] == '.')
{
A11=i;
break;
}
if(qipan[y-1][i] == 'x')
break;
if(qipan[y-1][i] == 'o')
Num1++;
}
for(i = x-2; i >= 0 ; i --)
{
if(qipan[y-1][i] == '.')
{
A12 = i;
break;
}
if(qipan[y-1][i] == 'x')
{
break;
}
if(qipan[y-1][i] == 'o')
Num1++;
}
////

for( i = y ; i < y+5 && y+5 < 20 ; i ++)
{
if(qipan[i][x-1] == '.')
{
A21 = i;
break;
}
if(qipan[i][x-1] == 'x')
{
break;
}
if(qipan[i][x-1] == 'o')
Num2++;
}
for(i = y-2; i >= 0 ; i --)
{
if(qipan[i][x-1] == '.')
{
A22 = i;
break;
}
if(qipan[i][x-1] == 'x')
{
break;
}
if(qipan[i][x-1] == 'o')
Num2++;
}
////
for(i = 0 ; i < 5 && x+i<20 && y+i<20; i++ )
{
if(qipan[y+i][x+i] == '.')
{
A31 = i;
break;
}
if(qipan[y+i][x+i] == 'x')
{
break;
}
if(qipan[y+i][x+i] =='o')
Num3++;
}
for(i = 0 ; x - i -2>=0 && y-i-2>=0 && i < 5;i++ )
{
if(qipan[y-i-2][x-i-2] == '.')
{
A32 = i;
break;
}
if(qipan[y-i-2][x-i-2] == 'x')
break;
if(qipan[y-i-2][x-i-2] == 'o')
Num3++;
}
////
for(i = 0 ; x + i < 20 && y - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[y-i-2][x+i] == '.')
{
A41 = i;
break;
}
if(qipan[y-i-2][x+i] == 'x')
{
break;
}
if(qipan[y-i-2][x+i] == 'o')
Num4++;
}
for(i = 0 ; y + i < 20 && x - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[y+i][x-i-2] == '.')
{
A42 = i;
break;
}
if(qipan[y+i][x-i-2] == 'x')
{
break;
}
if(qipan[y+i][x-i-2] == 'o')
Num4++;
}
////
if(num_xy == 0 && num_xy == 0)
{
m = n = 10;
qipan[m-1][n-1] = 'x';
}
else if(num_xy == 1)
attack();
else if(num_xy == 1)
{
m = x+1;
n = x+1;
qipan[m-1][n-1] = 'x';
}
else if(Num1 == 4 && A11 == 21 && A12 != 21) //一排四個右邊一頭堵住
{
n =1 + y-1;
m = 1 + A12;
qipan[y-1][A12] = 'x';
}
else if(Num1 == 4 && A12 == 21 && A11 != 21) //一排四個左邊邊一頭堵住
{
n =1 + y-1;
m = 1 + A11;
qipan[y-1][A11] = 'x';
}
else if(Num2 == 4 && A21 == 21 && A22 != 21) //一列四個下邊一頭堵住
{
n =1 + A22;
m = 1 + x-1;
qipan[A22][x-1] = 'x';
}
else if(Num2 == 4 && A22 == 21 && A21 != 21) //一列四個上邊一頭堵住
{
n =1 + A21;
m = 1 + x-1;
qipan[A21][x-1] = 'x';
}
else if(Num3 == 4 && A31 == 21 && A32 != 21) //一左上右下斜列四個右邊一頭堵住
{
n =1 + y - A32 - 2;
m = 1 + x - A32 - 2;
qipan[y-A32-2][x-A32-2] = 'x';
}
else if(Num3 == 4 && A32 == 21 && A31 != 21) //一左上右下斜列四個左邊一頭堵住
{
n =1 + y + A31;
m = 1 + x + A31;
qipan[y+A31][x+A31] = 'x';
}
else if(Num4 == 4 && A41 == 21 && A42 != 21) //一左下右上斜列四個右邊一頭堵住
{
n =1 + y + A42;
m = 1 + x - A42 - 2;
qipan[y+A42][x-A42-2] = 'x';
}
else if(Num4 == 4 && A42 == 21 && A41 != 21) //一左下右上斜列四個左邊一頭堵住
{
n =1 + y - A41 - 2;
m = 1 + x + A41;
qipan[y-A41-2][x+A41] = 'x';
}
else if(Num1==3 && A11 != 21 && A12 != 21) //一排3 個兩頭沒堵
{
n =1 + y-1;
m = 1 + A11;
if(dangerlevel(y-1,A11) >= dangerlevel(y-1,A12))
qipan[y-1][A11] = 'x';
else
qipan[y-1][A12] = 'x';

}
else if(Num2==3&& A21 != 21 && A22 != 21) //一列 3 個兩頭沒堵
{
n =1 + A21;
m = 1 + x-1;
if(dangerlevel(A21,x-1) >= dangerlevel(A22,x-1))
qipan[A21][x-1] = 'x';
else
qipan[A22][x-1] = 'x';
}
else if(Num3 == 3&& A31 != 21 && A32 != 21) //一左上右下斜列3 個兩頭沒堵
{
n =1 + y + A31;
m = 1 + x + A31;
if(dangerlevel(y+A31,x+A31) >= dangerlevel(y-A32 - 2,y - A32 -2))
qipan[y+A31][x+A31] = 'x';
else
qipan[y-A32-2][x-A32 -2] = 'x';

}
else if(Num4==3 && A41 != 21 && A42 != 21) //一左下右上斜列3 個兩頭沒堵
{
n =1 + y - A41 - 2;
m = 1 + x + A41;
if(dangerlevel(y-A41-2,x+A41) >= dangerlevel(y-A42-2,x+A42))
qipan[y-A41-2][x+A41] = 'x';
else
qipan[y-A42-2][x+A42] = 'x';
}
else if(Num1 == 2&& A11 != 21 && A12 != 21) //一排2個兩頭沒堵
{
n =1 + y-1;
m = 1 + A11;
qipan[y-1][A11] = 'x';
}

else if( Num2 == 2&& A21 != 21 && A22 != 21) //一列2個兩頭沒堵
{
n =1 + A21;
m = 1 + x-1;
qipan[A21][x-1] = 'x';
}
else if(Num3 == 2&& A31 != 21 && A32 != 21) //一左上右下斜列2 個兩頭沒堵
{
n =1 + y + A31;
m = 1 + x + A31;
qipan[y+A31][x+A31] = 'x';
}
else if(Num4==2 && A41 != 21 && A42 != 21) //一左下右上斜列2個兩頭沒堵
{
n =1 + y - A41 - 2;
m = 1 + x + A41;
qipan[y-A41-2][x+A41] = 'x';
}
else
attack();
}

void five::showqipan()
{
int p=0,q=0;
for(int i = 0 ; i <= 20 ; i++)
{

if(p<10)
{
cout << p <<' ';
}
else
cout << p ;
p++;
}
cout << endl;
for(i = 0 ; i < 20 ; i++)
{
if(i>=0)
{
q++;
if(q<10)
cout << q <<' ';
else
cout << q;
}
for(int j = 0 ; j < 20 ; j ++)
cout << qipan[i][j]<<' ' ;

cout << endl;
}
}

void five::print()
{
int p=0,q=0;
for(int i = 0 ; i <= 20 ; i++)
{

if(p<10)
{
cout << p <<' ';
}
else
cout << p ;
p++;
}
cout << endl;
for(i = 0 ; i < 20 ; i++)
{
if(i>=0)
{
q++;
if(q<10)
cout << q <<' ';
else
cout << q;
}
for(int j = 0 ; j < 20 ; j ++)
{
cout << qipan[i][j]<<' ' ;
}

cout << endl;
}
}

bool five::IsxyWin()
{
int num1=1,num2=1,num3=1,num4=1;
for(int i = x ; i < x+5&& x+5<20 ; i ++)
{
if(qipan[y-1][i] != 'o')
break;
if(qipan[y-1][i] == 'o')
num1++;
}
for(i = x-2; i >= 0 ; i --)
{
if(qipan[y-1][i] != 'o')
break;
if(qipan[y-1][i] == 'o')
num1++;
}
for( i = y ; i < y+5 ; i ++)
{
if(qipan[i][x-1] != 'o')
break;
if(qipan[i][x-1] == 'o')
num2++;
}
for(i = y-2; i >= 0 ; i --)
{
if(qipan[i][x-1] != 'o')
break;
if(qipan[i][x-1] == 'o')
num2++;
}
for(i = 0 ; i < 5 && x+i<20 && y+i<20; i++ )
{
if(qipan[y+i][x+i] != 'o')
break;
if(qipan[y+i][x+i] =='o')
num3++;
}
for(i = 0 ; x - i -2>=0 && y-i-2>=0 && i < 5;i++ )
{
if(qipan[y-i-2][x-i-2] != 'o')
break;
if(qipan[y-i-2][x-i-2] == 'o')
num3++;
}
for(i = 0 ; x + i < 20 && y - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[y-i-2][x+i] != 'o')
break;
if(qipan[y-i-2][x+i] == 'o')
num4++;
}
for(i = 0 ; y + i < 20 && x - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[y+i][x-i-2] != 'o')
break;
if(qipan[y+i][x-i-2] == 'o')
num4++;
}
if(num1>=5||num2>=5||num3>=5||num4>=5)
return 1;
else
return 0;

}
bool five::IsmnWin()
{
int num1=1,num2=1,num3=1,num4=1;
for(int i = m ; i < m+5&& m+5<20 ; i ++)
{
if(qipan[n-1][i] != 'x')
break;
if(qipan[n-1][i] == 'x')
num1++;
}
for(i = m-2; i >= 0 ; i --)
{
if(qipan[n-1][i] != 'x')
break;
if(qipan[n-1][i] == 'x')
num1++;
}
for( i = n ; i < n+5 ; i ++)
{
if(qipan[i][m-1] != 'x')
break;
if(qipan[i][m-1] == 'x')
num2++;
}
for(i = n-2; i >= 0 ; i --)
{
if(qipan[i][m-1] != 'x')
break;
if(qipan[i][m-1] == 'x')
num2++;
}
for(i = 0 ; i < 5 && m+i<20 && n+i<20; i++ )
{
if(qipan[n+i][m+i] != 'x')
break;
if(qipan[n+i][m+i] =='x')
num3++;
}
for(i = 0 ; m - i -2>=0 && n-i-2>=0 && i < 5;i++ )
{
if(qipan[n-i-2][m-i-2] != 'x')
break;
if(qipan[n-i-2][m-i-2] == 'x')
num3++;
}
for(i = 0 ; m + i < 20 && n - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[n-i-2][m+i] != 'x')
break;
if(qipan[n-i-2][m+i] == 'x')
num4++;
}
for(i = 0 ; n + i < 20 && m - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[n+i][m-i-2] != 'x')
break;
if(qipan[n+i][m-i-2] == 'x')
num4++;
}
if(num1>=5||num2>=5||num3>=5||num4>=5)
return 1;
else
return 0;

}

void main()
{
five a;
int temp;
cout << "歡迎體驗本游戲!這是本游戲的棋盤:" << endl;
a.showqipan();
cout << "雙人游戲請按1,人機游戲請按2,退出請按3:";
cin >> temp;
if(temp == 3)
exit(1);
if(temp == 1)
{
cout << "您選擇了雙人游戲,下面游戲開始:" << endl;
for(int i = 0 ; ; i ++)
{
a.getpointxy();
a.print();
if(a.IsxyWin())
{
cout << "白棋勝!" << endl;
break;
}
a.getpointmn();
a.print();
if(a.IsmnWin())
{
cout << "黑棋勝!" << endl;
break;
}
}
}
if(temp == 2)
{
int temp1=0;
cout << "您選擇了人機游戲,您是白棋('o'),電腦是黑棋('x'),下面游戲開始:" << endl;
cout << "您先下請按1,電腦先下請按2:";
cin >> temp1;
if(temp1 == 1)
{
for(int i = 0 ; ; i ++)
{
a.getpointxy();
a.print();
if(a.IsxyWin())
{
cout << "你贏了!恭喜!" << endl;
break;
}
a.machinemn();
a.print();
cout << "電腦走(" << a.getm() << ',' << a.getn() << ')' << endl;
if(a.IsmnWin())
{
cout << "哈哈,你輸了!" << endl;
break;
}
}
}
if(temp1 == 2)
{
for(int i = 0 ; ; i ++)
{
a.machinemn();
a.print();
if(a.IsmnWin())
{
cout << "哈哈,你輸了!" << endl;
break;
}
a.getpointxy();
a.print();
if(a.IsxyWin())
{
cout << "你贏了!恭喜!" << endl;
break;
}
}
}
}
cout << "Press any key to EXIT\n";
getch();
}

『玖』 求C語言編寫的五子棋程序。

#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<bios.h>
#include<conio.h>

#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define SPACE 0x3920

#define BILI 20
#define JZ 4
#define JS 3
#define N 19

int box[N][N];
int step_x,step_y ;
int key ;
int flag=1 ;

void draw_box();
void draw_cicle(int x,int y,int color);
void change();
void judgewho(int x,int y);
void judgekey();
int judgeresult(int x,int y);
void attentoin();

void attention()
{
char ch ;
window(1,1,80,25);
textbackground(LIGHTBLUE);
textcolor(YELLOW);
clrscr();
gotoxy(15,2);
printf("游戲操作規則:");
gotoxy(15,4);
printf("Play Rules:");
gotoxy(15,6);
printf("1、按左右上下方向鍵移動棋子");
gotoxy(15,8);
printf("1. Press Left,Right,Up,Down Key to move Piece");
gotoxy(15,10);
printf("2、按空格確定落棋子");
gotoxy(15,12);
printf("2. Press Space to place the Piece");
gotoxy(15,14);
printf("3、禁止在棋盤外按空格");
gotoxy(15,16);
printf("3. DO NOT press Space outside of the chessboard");
gotoxy(15,18);
printf("你是否接受上述的游戲規則(Y/N)");
gotoxy(15,20);
printf("Do you accept the above Playing Rules? [Y/N]:");
while(1)
{
gotoxy(60,20);
ch=getche();
if(ch=='Y'||ch=='y')
break ;
else if(ch=='N'||ch=='n')
{
window(1,1,80,25);
textbackground(BLACK);
textcolor(LIGHTGRAY);
clrscr();
exit(0);
}
gotoxy(51,12);
printf(" ");
}
}
void draw_box()
{
int x1,x2,y1,y2 ;
setbkcolor(LIGHTBLUE);
setcolor(YELLOW);
gotoxy(7,2);
printf("Left, Right, Up, Down KEY to move, Space to put, ESC-quit.");
for(x1=1,y1=1,y2=18;x1<=18;x1++)
line((x1+JZ)*BILI,(y1+JS)*BILI,(x1+JZ)*BILI,(y2+JS)*BILI);
for(x1=1,y1=1,x2=18;y1<=18;y1++)
line((x1+JZ)*BILI,(y1+JS)*BILI,(x2+JZ)*BILI,(y1+JS)*BILI);
for(x1=1;x1<=18;x1++)
for(y1=1;y1<=18;y1++)
box[x1][y1]=0 ;
}

void draw_circle(int x,int y,int color)
{
setcolor(color);
setlinestyle(SOLID_LINE,0,1);
x=(x+JZ)*BILI ;
y=(y+JS)*BILI ;
circle(x,y,8);
}

void judgekey()
{
int i ;
int j ;
switch(key)
{
case LEFT :

if(step_x-1<0)
break ;
else
{
for(i=step_x-1,j=step_y;i>=1;i--)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(i<1)break ;
step_x=i ;
judgewho(step_x,step_y);
break ;
}
case RIGHT :

if(step_x+1>18)
break ;
else
{
for(i=step_x+1,j=step_y;i<=18;i++)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(i>18)break ;
step_x=i ;
judgewho(step_x,step_y);
break ;
}
case DOWN :

if((step_y+1)>18)
break ;
else
{
for(i=step_x,j=step_y+1;j<=18;j++)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(j>18)break ;
step_y=j ;
judgewho(step_x,step_y);
break ;
}
case UP :

if((step_y-1)<0)
break ;
else
{
for(i=step_x,j=step_y-1;j>=1;j--)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(j<1)break ;
step_y=j ;
judgewho(step_x,step_y);
break ;
}
case ESC :
break ;

case SPACE :
if(step_x>=1&&step_x<=18&&step_y>=1&&step_y<=18)
{
if(box[step_x][step_y]==0)
{
box[step_x][step_y]=flag ;
if(judgeresult(step_x,step_y)==1)
{
sound(1000);
delay(1000);
nosound();
gotoxy(30,4);
if(flag==1)
{
setbkcolor(BLUE);
cleardevice();
setviewport(100,100,540,380,1);
/*定義一個圖形窗口*/
setfillstyle(1,2);
/*綠色以實填充*/
setcolor(YELLOW);
rectangle(0,0,439,279);
floodfill(50,50,14);
setcolor(12);
settextstyle(1,0,5);
/*三重筆劃字體, 水平放?5倍*/
outtextxy(20,20,"The White Win !");
setcolor(15);
settextstyle(3,0,5);
/*無襯筆劃字體, 水平放大5倍*/
outtextxy(120,120,"The White Win !");
setcolor(14);
settextstyle(2,0,8);
getch();
closegraph();
exit(0);
}
if(flag==2)
{
setbkcolor(BLUE);
cleardevice();
setviewport(100,100,540,380,1);
/*定義一個圖形窗口*/
setfillstyle(1,2);
/*綠色以實填充*/
setcolor(YELLOW);
rectangle(0,0,439,279);
floodfill(50,50,14);
setcolor(12);
settextstyle(1,0,8);
/*三重筆劃字體, 水平放大8倍*/
outtextxy(20,20,"The Red Win !");
setcolor(15);
settextstyle(3,0,5);
/*無襯筆劃字體, 水平放大5倍*/
outtextxy(120,120,"The Red Win !");
setcolor(14);
settextstyle(2,0,8);
getch();
closegraph();
exit(0);
}
}
change();
break ;
}
}
else
break ;
}
}

void change()
{
if(flag==1)
flag=2 ;
else
flag=1 ;
}

void judgewho(int x,int y)
{
if(flag==1)
draw_circle(x,y,15);
if(flag==2)
draw_circle(x,y,4);
}

int judgeresult(int x,int y)
{
int j,k,n1,n2 ;
while(1)
{
n1=0 ;
n2=0 ;
/*水平向左數*/
for(j=x,k=y;j>=1;j--)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*水平向右數*/
for(j=x,k=y;j<=18;j++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1>=5)
{
return(1);
break ;
}

/*垂直向上數*/
n1=0 ;
n2=0 ;
for(j=x,k=y;k>=1;k--)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*垂直向下數*/
for(j=x,k=y;k<=18;k++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1>=5)
{
return(1);
break ;
}

/*向左上方數*/
n1=0 ;
n2=0 ;
for(j=x,k=y;j>=1,k>=1;j--,k--)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*向右下方數*/
for(j=x,k=y;j<=18,k<=18;j++,k++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1>=5)
{
return(1);
break ;
}

/*向右上方數*/
n1=0 ;
n2=0 ;
for(j=x,k=y;j<=18,k>=1;j++,k--)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*向左下方數*/
for(j=x,k=y;j>=1,k<=18;j--,k++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1>=5)
{
return(1);
break ;
}
return(0);
break ;
}
}

void main()
{
int gdriver=VGA,gmode=VGAHI;
clrscr();
attention();
initgraph(&gdriver,&gmode,"c:\\tc");
/* setwritemode(XOR_PUT);*/
flag=1 ;
draw_box();
do
{
step_x=0 ;
step_y=0 ;
/*draw_circle(step_x,step_y,8); */
judgewho(step_x-1,step_y-1);
do
{
while(bioskey(1)==0);
key=bioskey(0);
judgekey();
}
while(key!=SPACE&&key!=ESC);
}
while(key!=ESC);
closegraph();
}

『拾』 找五子棋源代碼c++

devc++運行通過,含注釋

#include<bits/stdc++.h>

#include<windows.h>

#include<conio.h>

#include<ctime>

using namespace std;

void gotoxy(int x,int y) {

COORD pos = {x,y};

HANDLE hOut =GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleCursorPosition(hOut,pos);

}//將游標移動到x,y點上

int mp[16][16]= {0},x1=0,x2=0;//地圖,用來搜索五子連成的

void print(int x) {

gotoxy(x,1);

cout<<"┬";

for(int i=2; i<=14; i++) {

gotoxy(x,i);

cout<<"┼";

}

gotoxy(x,15);

cout<<"┴";

}//輸出棋盤的中間部分

void gotoc() {

system("cls");

gotoxy(55,10);

cout<<"五 子 棋";

gotoxy(56,20);

cout<<"載入中...";

gotoxy(55,21);

cout<<"作者:北辰";

for(int j=0; j<100; j++) {

Sleep(17);

gotoxy(j+3,15);

cout<<" "<<j<<"%";

gotoxy(j,15);

cout<<逗鉛冊"■";

}

system("cls");

for(int i=0; i<100; i++) {

for(int j=0; j<40; j++) {

gotoxy(i,j);

cout<<"■";

//SetColor(rand()%10);

}

}

system("cls");

}//載入界山宏面函數

int main() {

gotoc();//載入

for(int i=2; i<=30; i+=2) {

gotoxy(i,0);

cout<<i/2;

}//橫坐標

for(int i=1; i<=15; i++) {

gotoxy(0,i);

cout<<i;

}//縱坐標

gotoxy(2,1);

cout<<"┌";

for(int i=2; i<=14; i++) {

gotoxy(2,i);

cout<<"├";

}

gotoxy(2,15);

cout<<"└";//輸出棋盤左側

for(int i=4; i<=28; i+=2) {

print(i);

}//用一個循環來輸出棋盤中間部分

gotoxy(30,1);

cout<<"┐";

for(int i=2; i<=14; i++) {

gotoxy(30,i);

cout<<"┤";

}

gotoxy(30,15);

cout<<"┘";//輸出棋盤右側

bool l=0;//沒什麼用的flag

long long m=2;//這個很重要,用來判斷是該白棋走還是黑棋走,每次走完++,每次判斷是偶數,該白棋,是奇數,該黑棋(一般用flag判斷,這是我個人喜好)

gotoxy(0,17);

cout<<"游戲說明:白棋先走,落子請輸入坐標,其他的不用我說了吧";//說明,一定要看

while(l=1) {

gotoxy(32,16);

int x,y;

cin>>x>>y;//讀入xy坐標

gotoxy(32,16);

cout<<" ";

if(mp[x][y]!=0) {

gotoxy(32,16);

cout<<"此位置已有落子!";

Sleep(1000);

gotoxy(32,16);

cout<<" ";

continue;

}//很重要,用來判斷此位置有沒有落子

if(x>15&&激州y<=15) {

gotoxy(32,16);

cout<<"x坐標超出棋盤范圍!";

Sleep(1000);

gotoxy(32,16);

cout<<" ";

continue;

}

if(y>15&&x<=15) {

gotoxy(32,16);

cout<<"y坐標超出棋盤范圍!";

Sleep(1000);

gotoxy(32,16);

cout<<" ";

continue;

}

if(y>15&&x>15) {

gotoxy(32,16);

cout<<"x和y坐標均超出棋盤范圍!";

Sleep(1000);

gotoxy(32,16);

cout<<" ";

continue;

}//以上三個if用來判斷有沒有超出棋盤大小

gotoxy(x*2,y);

if(m%2==0) {//是偶數,該白棋

cout<<"●";//輸出棋子

mp[x][y]=1;

//橫坐標搜索有沒有連成五個

if(mp[x+1][y]==1&&mp[x+2][y]==1&&mp[x+3][y]==1&&mp[x+4][y]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x-1][y]==1&&mp[x+1][y]==1&&mp[x+2][y]==1&&mp[x+3][y]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x-2][y]==1&&mp[x-1][y]==1&&mp[x+1][y]==1&&mp[x+2][y]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x-3][y]==1&&mp[x-2][y]==1&&mp[x-1][y]==1&&mp[x+1][y]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x-4][y]==1&&mp[x-3][y]==1&&mp[x-2][y]==1&&mp[x-1][y]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

//豎

if(mp[x][y+1]==1&&mp[x][y+2]==1&&mp[x][y+3]==1&&mp[x][y+4]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x][y-1]==1&&mp[x][y+1]==1&&mp[x][y+2]==1&&mp[x][y+3]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x][y-2]==1&&mp[x][y-1]==1&&mp[x][y+1]==1&&mp[x][y+2]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x][y-3]==1&&mp[x][y-2]==1&&mp[x][y-1]==1&&mp[x][y+1]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x][y-4]==1&&mp[x][y-3]==1&&mp[x][y-2]==1&&mp[x][y-1]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

//斜''

if(mp[x+1][y+1]==1&&mp[x+2][y+2]==1&&mp[x+3][y+3]==1&&mp[x+4][y+4]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x-1][y-1]==1&&mp[x+1][y+1]==1&&mp[x+2][y+2]==1&&mp[x+3][y+3]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x-2][y-2]==1&&mp[x-1][y-1]==1&&mp[x+1][y+1]==1&&mp[x+2][y+2]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x-3][y-3]==1&&mp[x-2][y-2]==1&&mp[x-1][y-1]==1&&mp[x+1][y+1]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x-4][y-4]==1&&mp[x-3][y-3]==1&&mp[x-2][y-2]==1&&mp[x-1][y-1]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

//斜'/'

if(mp[x-1][y+1]==1&&mp[x-2][y+2]==1&&mp[x-3][y+3]==1&&mp[x-4][y+4]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x+1][y-1]==1&&mp[x-1][y+1]==1&&mp[x-2][y+2]==1&&mp[x-3][y+3]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x+2][y-2]==1&&mp[x+1][y-1]==1&&mp[x-1][y+1]==1&&mp[x-2][y+2]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x+3][y-3]==1&&mp[x+2][y-2]==1&&mp[x+1][y-1]==1&&mp[x-1][y+1]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

if(mp[x+4][y-4]==1&&mp[x+3][y-3]==1&&mp[x+2][y-2]==1&&mp[x+1][y-1]==1) {

gotoxy(32,16);

cout<<"白棋獲勝!";

return 0;

}

} else if(m%2==1) {//為奇數,該黑棋

cout<<"○";

mp[x][y]=2;

//橫

if(mp[x+1][y]==2&&mp[x+2][y]==2&&mp[x+3][y]==2&&mp[x+4][y]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x-1][y]==2&&mp[x+1][y]==2&&mp[x+2][y]==2&&mp[x+3][y]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x-2][y]==2&&mp[x-1][y]==2&&mp[x+1][y]==2&&mp[x+2][y]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x-3][y]==2&&mp[x-2][y]==2&&mp[x-1][y]==2&&mp[x+1][y]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x-4][y]==2&&mp[x-3][y]==2&&mp[x-2][y]==2&&mp[x-1][y]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

//豎

if(mp[x][y+1]==2&&mp[x][y+2]==2&&mp[x][y+3]==2&&mp[x][y+4]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x][y-1]==2&&mp[x][y+1]==2&&mp[x][y+2]==2&&mp[x][y+3]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x][y-2]==2&&mp[x][y-1]==2&&mp[x][y+1]==2&&mp[x][y+2]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x][y-3]==2&&mp[x][y-2]==2&&mp[x][y-1]==2&&mp[x][y+1]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x][y-4]==2&&mp[x][y-3]==2&&mp[x][y-2]==2&&mp[x][y-1]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

//斜''

if(mp[x+1][y+1]==2&&mp[x+2][y+2]==2&&mp[x+3][y+3]==2&&mp[x+4][y+4]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x-1][y-1]==2&&mp[x+1][y+1]==2&&mp[x+2][y+2]==2&&mp[x+3][y+3]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x-2][y-2]==2&&mp[x-1][y-1]==2&&mp[x+1][y+1]==2&&mp[x+2][y+2]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x-3][y-3]==2&&mp[x-2][y-2]==2&&mp[x-1][y-1]==2&&mp[x+1][y+1]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x-4][y-4]==2&&mp[x-3][y-3]==2&&mp[x-2][y-2]==2&&mp[x-1][y-1]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

//斜'/'

if(mp[x-1][y+1]==2&&mp[x-2][y+2]==2&&mp[x-3][y+3]==2&&mp[x-4][y+4]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x+1][y-1]==2&&mp[x-1][y+1]==2&&mp[x-2][y+2]==2&&mp[x-3][y+3]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x+2][y-2]==2&&mp[x+1][y-1]==2&&mp[x-1][y+1]==2&&mp[x-2][y+2]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x+3][y-3]==2&&mp[x+2][y-2]==2&&mp[x+1][y-1]==2&&mp[x-1][y+1]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

if(mp[x+4][y-4]==2&&mp[x+3][y-3]==2&&mp[x+2][y-2]==2&&mp[x+1][y-1]==2) {

gotoxy(32,16);

cout<<"黑棋獲勝!";

return 0;

}

}

m++;//不要忘記++m

}

return 0;//這個沒什麼用了,不過比賽時不要忘記加哦,否則判0分

}