中國象棋源碼
㈠ 求VB編寫中國象棋的源代碼。急求。必有重謝
您提供了一些VB代碼片段,這些代碼片段用於中國象棋程序中讀取和保存棋局功能。我會逐一檢查這些代碼,並提出改進建議。
1. 確保文件路徑正確:
確保 `App.Path & "\cchess.che"` 路徑正確指向您的棋局文件。如果您的程序安裝在不同的目錄,需要相應地更改路徑。
2. 檢查文件存在性:
在讀取文件之前,檢查文件是否存在。如果不存在,應提示用戶保存棋局到其他位置或創建新文件。
3. 正確的字元串處理:
使用 `vbCrLf` 作為換行符可能會導致問題,因為它代表的是回車和換行兩個字元。通常在處理文本時,您應該只使用一個字元的換行符,例如 `Chr(10)`。
4. 字元串分割和棋盤大小驗證:
`t = Split(s, " ")` 應該基於棋盤的實際尺寸來分割字元串。89似乎不是一個正確的數字,中國象棋棋盤應該有9列和10行,共90個棋子。請確保按照正確的棋盤尺寸來分割字元串。
5. 錯誤處理:
當讀取或保存棋局時,如果發生錯誤,應該有適當的錯誤處理機制,比如 `On Error Resume Next` 或 try-catch 塊。
6. 代碼注釋:
代碼中的 `Rem` 表示這是一個注釋行,但注釋內容應該清晰地描述接下來的代碼功能。
7. 對話框消息:
使用 `MsgBox` 時,確保消息對用戶是有幫助的,讓用戶知道發生了什麼以及如何解決。
8. 代碼格式:
增加縮進和適當的空行可以提高代碼的可讀性。
下面是根據上述點評修改後的代碼段:
```vb
' 讀取棋局
Private Sub mnuRead_Click()
Dim s As String
Dim i As Integer
Dim pieces As Variant
Dim filePath As String
filePath = App.Path & "\cchess.che"
If Not FileExists(filePath) Then
MsgBox "沒有棋局文件可以讀入!請先保存棋局!", vbExclamation, "中國象棋"
Exit Sub
End If
Open filePath For Input As #1
Input #1, s
Close #1
s = Replace(s, vbCrLf, "")
s = Trim(s)
' 確保字元串長度正確
If s.Length > 90 Then
MsgBox "棋局文件已遭到破壞!請重新保存棋局!", vbExclamation, "中國象棋"
Exit Sub
End If
' 解析字元串為棋子數組
pieces = Split(s, " ")
' 將字元串轉換為棋子值並賦值給數組
For i = 0 To 89
a(i) = Val(pieces(i))
Next i
MsgBox "棋局文件已成功讀入!", vbInformation, "中國象棋"
End Sub
' 保存棋局
Private Sub mnuSave_Click()
Dim s As String
Dim i As Integer
Dim filePath As String
filePath = App.Path & "\cchess.che"
' 清空字元串
s = ""
' 遍歷棋盤並構建字元串
For i = 0 To 89
s = s & " " & a(i)
Next i
' 刪除字元串前後的空格
s = Trim(s)
Open filePath For Output As #1
Print #1, s
Close #1
MsgBox "當前棋局已成功保存!", vbInformation, "中國象棋"
End Sub
```
請注意,這里沒有包含錯誤處理和文件存在的檢查,您需要根據您的程序邏輯添加這些內容。此外,我假設 `a(i)` 是一個數組,用於存儲棋盤上的棋子信息,您需要確保它已經被正確初始化。
㈡ 誰給個中國象棋的易語言源碼給我電腦最好很厲害的
你能要到的源代碼都不厲害 厲害的你別想要到 這是保密的
㈢ 急求C++設計象棋打譜程序的源代碼
這是一個簡單的程序,會自動計算提子,但不會數目。其它的運行一次估計就差不多會用了。稍微寫了點注釋。
#include<stdio.h>
#include<stdlib.h>
char board[21][21];
char move[5][2]=,,,,};
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]=;
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]=;//記錄算過的棋子以節約效率
char Q[400][2]=;//某一片棋
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++程序設計 中國象棋源代碼
我提供兩個功能完善,而且最重要的,我認為演算法設計比較好的中國象棋源代碼,因為是源碼網的,所以可以學習參考下:
http://www.codefans.net/soft/1466.shtml
http://www.codefans.net/soft/1289.shtml
㈤ 求VB編寫中國象棋的源代碼。急求。必有重謝
貼保存棋局和讀取棋局部分的,你先看看:
Rem 讀取棋局
Private Sub mnuRead_Click()
Dim s As String, i As Integer, t
If Dir(App.Path & "\cchess.che") = "" Then MsgBox "沒有棋局文件可以讀入!請先保存棋局!", , "中國象棋": Exit Sub
Open App.Path & "\cchess.che" For Input As #1
Input #1, s
Close #1
s = Replace(s, vbCrLf, "")
s = Trim(s)
t = Split(s, " ")
If UBound(t) <> 89 Then MsgBox "棋局文件已遭到破壞!請重新保存棋局!", , "中國象棋": Exit Sub
For i = 0 To 89
a(i) = Val(t(i))
Next
MsgBox "棋局文件已成功讀入!", , "中國象棋"
End Sub
Rem 保存棋局
Private Sub mnuSave_Click()
Dim s As String, i As Integer
s = ""
For i = 0 To 89
s = s & " " & a(i)
Next
s = Trim(s)
Open App.Path & "\cchess.che" For Output As #1
Print #1, s;
Close #1
MsgBox "當前棋局已成功保存!", , "中國象棋"
End Sub