當前位置:首頁 » 操作系統 » c圍棋源碼

c圍棋源碼

發布時間: 2023-01-08 01:29:41

① 用c語言. 編寫一個文本界面的圍棋打譜程序

這是一個簡單的程序,會自動計算提子,但不會數目。其它的運行一次估計就差不多會用了。稍微寫了點注釋。

#include<stdio.h>
#include<stdlib.h>

char board[21][21];
char move[5][2]={{-1,0},{1,0},{0,-1},{0,1},{0,0}};

void initBoard();//初始化棋盤
void showBoard();//輸出棋盤
char set(int x,int y,char color);//下子
void process(int xx,int yy);//計算提子

int main()
{
FILE * fptr=NULL;
char pufile[256]={0};
char op;
int s;
int x,y,r;
char color;
char win;
int cnt;

start:
s=8;
while(s!=1 && s!=2)
{
printf("選擇模式:\n1---下棋\n2---看棋譜\n0---退出\n");
printf("下棋模式下,下子請輸入s x y(x,y為位置),認輸輸入g,和棋輸入h\n選擇:");
scanf("%d",&s);
if(s==0) return 0;
//Egg1
if(s==10) printf("Programmer: swordlance :)\n");
//Egg1 end
}
getchar();
printf("輸入棋譜路徑:");
gets(pufile);

if(s==1) fptr=fopen(pufile,"w");
else fptr=fopen(pufile,"r");

if(!fptr)
{
printf("文件無法打開(創建)!\n");
system("PAUSE");
return -1;
}

initBoard();
cnt=0;
color='B';
while(op!='g')
{
system("CLS");
showBoard();
printf("(第%d手)",++cnt);
if(s==1)
{
printf("%c 方:",color);
scanf("%c",&op);
//printf("[%c]",op);
if(op=='s')
{
scanf("%d %d",&x,&y);
getchar();
if(set(x,y,color)!=0)
{
printf("該處不能落子!\n");
cnt--;
system("PAUSE");
}
else
{
process(x,y);
fprintf(fptr,"%d %d\n",x,y);
if(color=='B') color='W';
else color='B';
}
}
else if(op=='g')
{
printf("%c 方認輸。\n",color);
if(color=='B') fprintf(fptr,"0 1\n");
else fprintf(fptr,"0 -1\n");
fflush(fptr);
fclose(fptr);
system("PAUSE");
goto start;
}
else if(op=='h')
{
printf("和棋。\n");
fprintf(fptr,"0 0\n");
fflush(fptr);
fclose(fptr);
system("PAUSE");
goto start;
}
else
{
printf("參數錯誤,下子請輸入s x y(x,y為位置),認輸輸入 g,和棋輸入h");
cnt--;
system("PAUSE");
}
}
else
{
fscanf(fptr,"%d %d",&x,&y);
if(x==0)
{
if(y>0) printf("W 方勝!\n");
else if(y<0) printf("B 方勝!\n");
else printf("和棋!\n");
system("PAUSE");
goto start;
}
else
{
printf("%c 方落子(%d,%d)\n",color,x,y);
set(x,y,color);
process(x,y);
if(color=='B') color='W';
else color='B';
}
system("PAUSE");
}
}

system("PAUSE");
return 0;
}

void initBoard()
{
int i,j;
board[0][0]='O';
for(i=1;i<=19;i++) board[0][i]='-';
board[0][20]='O';
for(i=1;i<=19;i++)
{
board[i][0]='|';
for(j=1;j<=19;j++) board[i][j]='+';
board[i][20]='|';
}
board[20][0]='O';
for(i=1;i<=19;i++) board[20][i]='-';
board[20][20]='O';
board[4][4]=board[4][10]=board[4][16]=
board[10][4]=board[10][10]=board[10][16]=
board[16][4]=board[16][10]=board[16][16]='*';
}

void showBoard()
{
int i,j;
for(i=0;i<=20;i++)
{
for(j=0;j<=20;j++)
{
printf("%c",board[i][j]);
}
printf("\n");
}
}

char set(int x,int y,char color)
{
if(board[x][y]=='W' || board[x][y]=='B') return -1;//不能落子
else board[x][y]=color;
return 0;
}

//計算提子
void process(int xx,int yy)
{
char his[21][21]={0};//記錄算過的棋子以節約效率
char Q[400][2]={0};//某一片棋
int e;//Q的長度。
char mcolor;//這片棋的顏色
char ecolor;//另一種顏色
int QI=0;//氣數
int i,j,k,l,m;
int x,y;

for(m=0;m<5;m++)
{
i=xx+move[m][0];//為了能夠完成打劫,先算別人再算自己
j=yy+move[m][1];
if(his[i][j]==0 && (board[i][j]=='W' || board[i][j]=='B')) //該位置有子開始算氣
{
QI=0;
his[i][j]=1;
mcolor=board[i][j];
ecolor=(board[i][j]=='W'?'B':'W');
//printf("m=%c e=%c\n",mcolor,ecolor);
Q[0][0]=i;
Q[0][1]=j;
e=1;
for(k=0;k<e;k++)
{
for(l=0;l<4;l++)
{
x=Q[k][0]+move[l][0];
y=Q[k][1]+move[l][1];
//printf("x=%d y=%d\n",x,y);
//system("PAUSE");
if(x>0 && y>0 && x<20 && y<20 && his[x][y]==0)
{
if(board[x][y]==mcolor)//己方,長氣
{
Q[e][0]=x;
Q[e][1]=y;
e++;
his[x][y]=1;
}
else
{
if(board[x][y]=='+') QI++; //空地,加氣,忽略重復計算
}
}
}
}
//printf("QI=%d\n",QI);
//system("PAUSE");
if(!QI)//死棋,提子
{
for(k=0;k<e;k++)
{
board[Q[k][0]][Q[k][1]]='+';
his[Q[k][0]][Q[k][1]]=0;
}
}
}
}
}

② 用C語言編程 在屏幕上顯示圍棋棋盤

--
以怎麼不用 MS VC++6 ??
編C語言一般都用啊
還有\xCD是C語言
Ascii代碼 表示一個圖形

用循環語句啊!

等我一下 晚上寫出來 貼上哦

晚上10:00 上完課回來

呵呵 分給不給 隨便 你的問題讓我熟練了許多
分不重要了

我終於寫出來了
調試了30分鍾啊!

純粹的循環 + 判斷
-- 我就這點水平了 演算法可能不好

如果有更好的告訴我 上QQ 或發網路消息給我

網路的顯示有問題 最好
用源代碼格式化工具格式化一下 網路有搜

#include <stdio.h>
void main()
{
int n,m,x,y,temp,j=0,i=0 ;
printf("輸入棋盤的橫格數,豎格數:\n");
scanf("%d,%d",&x,&y);
printf("%d,%d\n",x,y);
n=2*x+1 ;
m=2*y+1 ;
printf("%d,%d\n",n,m);
while(j<m)
{
j++;
temp=j%2 ;
i=0 ;
if(j==1)
{
while(i<n)
{
i++;
if(i==1)
{
printf("\xC9");
continue ;
}
if(n==i)
{
printf("\xBB\n");
continue ;
}
if(i%2!=0)
{
printf("\xCB");
continue ;
}
if(i%2==0)
{
printf("\xCD\xCD\xCD");
continue ;
}

}
continue ;
}
if(m==j)
{
while(i<n)
{
i++;
if(i==1)
{
printf("\xC8");
continue ;
}
if(n==i)
{
printf("\xBC\n");
continue ;
}
if(i%2!=0)
{
printf("\xCA");
continue ;
}
if(i%2==0)
{
printf("\xCD\xCD\xCD");
continue ;
}

}
continue ;
}
if(temp==0)
{

while(i<n)
{
i++;
if(i%2!=0)
{
printf("\xBA");
if(n==i)printf("\n");
continue ;
}
if(i%2==0)
{
printf(" ");
continue ;
}

}
continue ;
}
if(temp!=0&&j!=1)
{
while(i<n)
{
i++;
if(i==1)
{
printf("\xCC");
continue ;
}
if(n==i)
{
printf("\xB9\n");
continue ;
}
if(i%2!=0)
{
printf("\xCE");
continue ;
}
if(i%2==0)
{
printf("\xCD\xCD\xCD");
continue ;
}

}
continue ;
}
}
}

100%編譯通過
可以畫出任意大小的棋盤
如 5*3 5*5
只要你的屏幕能夠顯示

以上是用圖像庫畫的棋盤

下面是用 __ 和 |
畫的棋盤 其實很簡單 替換一下就可以了

#include <stdio.h>
void main()
{
int n,m,x,y,temp,j=0,i=0 ;
printf("輸入棋盤的橫格數,豎格數:\n");
scanf("%d,%d",&x,&y);
printf("%d,%d\n",x,y);
n=2*x+1 ;
m=2*y+1 ;
printf("%d,%d\n",n,m);
while(j<m)
{
j++;
temp=j%2 ;
i=0 ;
if(j==1)
{
while(i<n)
{
i++;
if(i==1)
{
printf("__");
continue ;
}
if(n==i)
{
printf("__\n");
continue ;
}
if(i%2!=0)
{
printf("__");
continue ;
}
if(i%2==0)
{
printf("__");
continue ;
}

}
continue ;
}
if(m==j)
{
while(i<n)
{
i++;
if(i==1)
{
printf("__");
continue ;
}
if(n==i)
{
printf("__\n");
continue ;
}
if(i%2!=0)
{
printf("__");
continue ;
}
if(i%2==0)
{
printf("__");
continue ;
}

}
continue ;
}
if(temp==0)
{

while(i<n)
{
i++;
if(i%2!=0)
{
printf("|");
if(n==i)printf("\n");
continue ;
}
if(i%2==0)
{
printf(" ");
continue ;
}

}
continue ;
}
if(temp!=0&&j!=1)
{
while(i<n)
{
i++;
if(i==1)
{
printf("__");
continue ;
}
if(n==i)
{
printf("__\n");
continue ;
}
if(i%2!=0)
{
printf("__");
continue ;
}
if(i%2==0)
{
printf("__");
continue ;
}

}
continue ;
}
}
}

③ 用C語言編寫出圍棋游戲,要求如下。希望有位大神前來幫忙

看上去只有寥寥數語,但實際上包括很多功能,可以看看是否有相應的開源代碼,自己寫那沒有十天半個月是搞不定的。

④ 如何用c語言編寫一個求圍棋氣的程序

我寫的圍棋游戲程序中的數氣函數,註解不多.

int CMyView::pointqi(int x, int y, int bow, int e)
{

int i,j,n=1; // n is man number of the mass
int qi=0,kong=0; int pivalue,inpivalue;
int mass[2][100];
int countqi[19][19];//after be counted as qi,make this point 1.
int realeye=0;
for(i=0;i<=18;i++)
{
for(j=0;j<=18;j++)
{
countqi[i][j]=0;
}
}
eat=0;
mass[0][1]=x;
mass[1][1]=y;
if(bow==10)
{ pivalue=1000; inpivalue=1;}
else
{ pivalue=1; inpivalue=1000;}

for(i=1; i<=n; i++)
{
if(pigroup[mass[0][i]-1][mass[1][i]]==pivalue &&mass[0][i]-1>=0)
{
for(j=1;j<=n;j++) //confirm that the new man is not belong to mass.
{
if(mass[0][i]-1 != mass[0][j] || mass[1][i] != mass[1][j] )
{
if(j!= n)
continue;
else
{
n++;
mass[0][n]=mass[0][i]-1;
mass[1][n]=mass[1][i];
break;
}
}
else
break;
}

}
if(pigroup[mass[0][i]+1][mass[1][i]]==pivalue&&mass[0][i]+1<=18)
{
for(j=1;j<=n;j++) //confirm that the new man is not belong to mass.
{
if(mass[0][i]+1 != mass[0][j] || mass[1][i] != mass[1][j] )
{
if(j!= n)
continue;
else
{
n++;
mass[0][n]=mass[0][i]+1;
mass[1][n]=mass[1][i];
break;
}
}
else
break;
}
}
if(pigroup[mass[0][i]][mass[1][i]-1]==pivalue&&mass[1][i]-1>=0)
{
for(j=1;j<=n;j++) //confirm that the new man is not belong to mass.
{
if(mass[0][i] != mass[0][j] || mass[1][i]-1 != mass[1][j] )
{
if(j!= n)
continue;
else
{
n++;
mass[0][n]=mass[0][i];
mass[1][n]=mass[1][i]-1;
break;
}
}
else
break;
}
}
if(pigroup[mass[0][i]][mass[1][i]+1]==pivalue&&mass[1][i]+1<=18)
{
for(j=1;j<=n;j++) //confirm that the new man is not belong to mass.
{
if(mass[0][i] != mass[0][j] || mass[1][i]+1 != mass[1][j] )
{
if(j!= n)
continue;
else
{
n++;
mass[0][n]=mass[0][i];
mass[1][n]=mass[1][i]+1;
break;
}
}
else
break;
}
}
}
for(j=1;j<=n;j++) //check for all man of this mass whether qi exists.
{
if(pigroup[mass[0][j]-1][mass[1][j]]==0&&mass[0][j]-1>=0&&countqi[mass[0][j]-1][mass[1][j]]==0)
{ qi++;
countqi[mass[0][j]-1][mass[1][j]]=1;
if(judgerealeye(mass[0][j]-1,mass[1][j],bow)==1)
realeye=realeye+1;
}
if(pigroup[mass[0][j]+1][mass[1][j]]==0&&mass[0][j]+1<=18&&countqi[mass[0][j]+1][mass[1][j]]==0)
{ qi++;
countqi[mass[0][j]+1][mass[1][j]]=1;
if(judgerealeye(mass[0][j]+1,mass[1][j],bow)==1)
realeye=realeye+1;
}
if(pigroup[mass[0][j]][mass[1][j]-1]==0 &&mass[1][j]-1>=0&&countqi[mass[0][j]][mass[1][j]-1]==0)
{ qi++;
countqi[mass[0][j]][mass[1][j]-1]=1;
if(judgerealeye(mass[0][j],mass[1][j]-1,bow)==1)
realeye=realeye+1;
}
if(pigroup[mass[0][j]][mass[1][j]+1]==0&&mass[1][j]+1<=18&&countqi[mass[0][j]][mass[1][j]+1]==0)
{ qi++;
countqi[mass[0][j]][mass[1][j]+1]=1;
if(judgerealeye(mass[0][j],mass[1][j]+1,bow)==1)
realeye=realeye+1;
}
}

if(qi!=0)
{
eat=0;
if(e==2)
return realeye;
else
return qi;
} //qi is not 0.
if(qi==0) // if any man is eat, make the value of pigroup[][] 0.
{
eat=1;
eaten[k]=1;
if(e==1)
{
for(i=1;i<=n;i++)
{
pigroup[mass[0][i]][mass[1][i]]=0;
beeat[mass[0][i]][mass[1][i]]=1;
}
}

}
return 0;
}

⑤ 用C語言繪制圍棋棋盤

#include<stdio.h>

intmain()
{
intr,c;
for(r=0;r<19;++r){

for(c=0;c<19;++c){
if(r==0){
if(c==0)printf("┏");
elseif(c==18)printf("┓");
elseprintf("┯");
}elseif(r==18){
if(c==0)printf("┗");
elseif(c==18)printf("┛");
elseprintf("┷");
}else{
if(c==0)printf("┠");
elseif(c==18)printf("┨");
elseprintf("┼");
}
}
printf(" ");
}
return0;
}

⑥ 阿爾法狗 圍棋c語言

都沒人搭理你,我跟你說說吧。alphaGo裡面圖形處理用到的是MATLAB。具體怎麼實現的要是真有人能回答你就奇怪了。該項目耗費4億英鎊,豈是簡簡單單幾句話就能說明白的。

⑦ 求C語言 圍棋吃棋函數的思路!

你好!
建議你還是先找棋盤類的演算法看一看吧
如有疑問,請追問。

⑧ 求圍棋的VB源代碼~

來~加 我 的QQ405557154 我做了一個,還有一同學做的,還有一是網上的朋友做的呵呵

Private Sub Timer1_Timer()
Dim ms As Boolean
Dim Info, temp As String
Dim p, p1, p2, i As Integer
Dim Ch
' Begin of Time Show Process
If ModemState <> LOGIN And SocketState <> CONNECTED Then
'it is not a multiusers game
Exit Sub
Else
If (S_R = 1) And (TURN = BLACKP) Then
Black_Time = Black_Time + Time - Start_Time
TimeB.Caption = CDate(Black_Time / 200)
Else
If (S_R = 1) And (TURN = WHITEP) Then
White_Time = White_Time + Time - Start_Time
TimeW.Caption = CDate(White_Time / 200)
Else
If (S_R = 0) And (TURN = WHITEP) Then
Black_Time = Black_Time + Time - Start_Time
TimeB.Caption = CDate(Black_Time / 200)
Else
If (S_R = 0) And (TURN = BLACKP) Then
White_Time = White_Time + Time - Start_Time
TimeW.Caption = CDate(White_Time / 200)
End If
End If
End If
End If
End If
'End If
' End of Time Show Process

'Begin of winsockt process

If SocketState = CONNECTED And Begin_Flag = 1 Then
ms = Net.Message_Exist
If ms = False Then
Exit Sub
End If
Info = Net.WaitForValue(Chr$(26), 5)
If g_ErrorCode = 1 Then
'Some error such as Timeout occured
Exit Sub
End If
p1 = InStr(Info, "B")
p2 = InStr(Info, "E|;")
If p1 = 0 Or p2 = 0 Then
Exit Sub
End If
temp = Mid$(Info, p1 + 1, p2 - p1 - 1)
ParseLine (temp)
Msg(Msg_No).No = CInt(ParseArray(1))
Msg(Msg_No).Color = CInt(ParseArray(2))
If IsNumeric(ParseArray(3)) Then
Msg(Msg_No).X = CInt(ParseArray(3))
Msg(Msg_No).Y = CInt(ParseArray(4))
Else
Msg(Msg_No).X = ParseArray(3)
Msg(Msg_No).Y = ParseArray(4)
End If

If Msg(Msg_No).Color = GIVEUP Then
Beep
MsgBox ("對方已經認輸了")
Net.Winsock1.SendData ("R_O" + Chr$(26))
Pause 3
Call Begin_Click
Exit Sub
End If

If Side = BLACKP Then
p = Draw_Point(Msg(Msg_No).X, Msg(Msg_No).Y, WHITEP)
Record(Step).Color = WHITEP
step_show.Cls
step_show.Print Step
TURN = BLACKP
Else
p = Draw_Point(Msg(Msg_No).X, Msg(Msg_No).Y, BLACKP)
Record(Step).Color = BLACKP
step_show.Cls
step_show.Print Step
TURN = WHITEP
End If
Record(Step).X = Msg(Msg_No).X
Record(Step).Y = Msg(Msg_No).Y
Step = Step + 1
S_R = 1
R_R = 0
p = Count_All_Gas
If (Msg(Msg_No).X > 0 And Msg(Msg_No).Y > 0 _
And Msg(Msg_No).X < 20 And Msg(Msg_No).Y < 20) Then
Board(Msg(Msg_No).X, Msg(Msg_No).Y).Current = True
Refresh_Board
Board(Msg(Msg_No).X, Msg(Msg_No).Y).Current = False
End If
Msg_No = Msg_No + 1
Pause 1
Net.Winsock1.SendData ("R_O" + Chr$(26))
Exit Sub
End If
'End of process of winsocket

'Begin of modem process
If ModemState <> LOGIN Or R_R <> 1 Then
'It isn't a Inter_Modem Game
Exit Sub
End If

ms = Modem_F.Exist_Msg
If ms = False Then
'IO Port don't have any message
Exit Sub
End If

Info = Modem_F.WaitForValue(Chr$(26), 5)
'Wait a playing message
If g_ErrorCode = 1 Then
'Some error such as Timeout occured
Exit Sub
End If
p1 = InStr(Info, "B")
p2 = InStr(Info, "E|;")
If p1 = 0 Or p2 = 0 Then
Exit Sub
End If
temp = Mid$(Info, p1 + 1, p2 - p1 - 1)
ParseLine (temp)
Msg(Msg_No).No = CInt(ParseArray(1))
Msg(Msg_No).Color = CInt(ParseArray(2))
If IsNumeric(ParseArray(3)) Then
Msg(Msg_No).X = CInt(ParseArray(3))
Msg(Msg_No).Y = CInt(ParseArray(4))
Else
Msg(Msg_No).X = ParseArray(3)
Msg(Msg_No).Y = ParseArray(4)
End If

Modem_F.Comm1.InBufferCount = 0
'Clear Buffer

If Msg(Msg_No).Color = LOGOUT Then
Beep
MsgBox ("對方已經退出了")
Modem_F.Comm1.InBufferCount = 0
Modem_F.Comm1.Output = "R_O" + Chr$(26)
Cls
Step = 0
Start_Time = Time
Black_Time = 0
White_Time = 0
Exit Sub
End If

If Msg(Msg_No).Color = FINISHED Then
Beep
Ch = MsgBox("對方要求結束比賽,可以嗎?", vbYesNo)
Modem_F.Comm1.InBufferCount = 0
If Ch = 6 Then
Modem_F.Comm1.Output = "YESR_O" + Chr$(26)
Else
Modem_F.Comm1.Output = "NOR_O" + Chr$(26)
Exit Sub
End If
PlayState = FINISHED
Count_Area.Enabled = True
End If

If Msg(Msg_No).Color = GIVEUP Then
Beep
MsgBox ("對方已經認輸了")
Modem_F.Comm1.InBufferCount = 0
Modem_F.Comm1.Output = "R_O" + Chr$(26)
Call Begin_Click
Exit Sub
End If

If Msg(Msg_No).Color = TALK Then
Beep
MsgBox (Modem_F.His_Name.Text & "說: " & Msg(Msg_No).X)
Modem_F.Comm1.InBufferCount = 0
Modem_F.Comm1.Output = "R_O" + Chr$(26)
Exit Sub
End If

If Msg(Msg_No).Color = UNDO Then
Step = Step - 1
Beep
Modem_F.Comm1.InBufferCount = 0
Modem_F.Comm1.Output = "R_O" + Chr$(26)
Draw_Board
Ini_Board
For i = 1 To Step - 1
Board(Record(i).X, Record(i).Y).Current = False
p = Draw_Point(Record(i).X, Record(i).Y, Record(i).Color)
step_show.Cls
step_show.Print Step
p = Count_All_Gas
Next i
Board(Record(Step - 1).X, Record(Step - 1).Y).Current = True
Refresh_Board
Board(Record(Step - 1).X, Record(Step - 1).Y).Current = False
S_R = 1
R_R = 0
TURN = Side
Exit Sub
End If

Modem_F.Comm1.InBufferCount = 0
Modem_F.Comm1.Output = "R_O" + Chr$(26)
If Side = BLACKP Then
p = Draw_Point(Msg(Msg_No).X, Msg(Msg_No).Y, WHITEP)
Record(Step).Color = WHITEP
step_show.Cls
step_show.Print Step
Else
p = Draw_Point(Msg(Msg_No).X, Msg(Msg_No).Y, BLACKP)
Record(Step).Color = BLACKP
step_show.Cls
step_show.Print Step
End If
Record(Step).X = Msg(Msg_No).X
Record(Step).Y = Msg(Msg_No).Y
Step = Step + 1
S_R = 1
R_R = 0
p = Count_All_Gas
If (Msg(Msg_No).X > 0 And Msg(Msg_No).Y > 0 _
And Msg(Msg_No).X < 20 And Msg(Msg_No).Y < 20) Then
Board(Msg(Msg_No).X, Msg(Msg_No).Y).Current = True
Refresh_Board
Board(Msg(Msg_No).X, Msg(Msg_No).Y).Current = False
End If
Msg_No = Msg_No + 1
End Sub
Private Sub Timer2_Timer()
ShowS.Cls
ShowS.Print " :-):-):-):-):-) " + Show_String(C1) + " (:-(:-(:-(:-(:-"
C1 = C1 + 1
If C1 = 10 Then
C1 = 0
End If
Game_Time.Cls
Game_Time.Print " 現在時間: " & Time
End Sub
Private Sub Set_Hand(h As Integer)
'設置讓子(1-9)
Dim p As Integer
If h <= 1 Then
'Not a Handicap game
Exit Sub
End If
部分代碼。。

⑨ 【高分】求C程序,五子棋模擬演算法。完成有加分,非常感謝

到底是五子棋還是圍棋??!!貌似你想說的是五子棋,那怎麼又有提子呢??!!說清楚,這個我可以寫

⑩ 求五子棋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();
}



熱點內容
如何下載奧特曼高級化3安卓版 發布:2025-05-14 07:47:31 瀏覽:344
qml文件修改後編譯未生效 發布:2025-05-14 07:31:00 瀏覽:330
內到內演算法 發布:2025-05-14 07:29:11 瀏覽:33
文件夾名字不顯示 發布:2025-05-14 07:27:47 瀏覽:774
oracle的資料庫驅動jar 發布:2025-05-14 07:23:20 瀏覽:555
我的世界電腦版伺服器手機版能進嗎 發布:2025-05-14 07:22:01 瀏覽:678
達內培訓php多少錢 發布:2025-05-14 07:19:10 瀏覽:26
python位元組轉字元串 發布:2025-05-14 07:06:35 瀏覽:421
subplotpython 發布:2025-05-14 06:53:51 瀏覽:661
豎屏大屏導航工廠密碼一般是多少 發布:2025-05-14 06:49:29 瀏覽:806