中国象棋的源代码

中国象棋的源代码,第1张

中国象棋源代码blackleft(){ int x,y,n if(blackcurpos.y>0) {blackcurpos.y--x=position[blacktemppos.x][blacktemppos.y].x y=position[blacktemppos.x][blacktemppos.y].y if(board[blacktemppos.x][blacktemppos.y]==0)drawbmp(x,y,boardfile[blacktemppos.x][blacktemppos.y]) else if(!(blacktemppos.x==blackoldpos.x&&blacktemppos.y==blackoldpos.y&&blackstate==MOVE)) {n=board[blacktemppos.x][blacktemppos.y] drawbmp(x,y,chessfile[n]) } if(blacktemppos.x==blackoldpos.x&&blacktemppos.y==blackoldpos.y&&blackstate==MOVE)drawselecursor(blackoldpos) drawcursor(blackcurpos) blacktemppos.x=blackcurpos.x blacktemppos.y=blackcurpos.y }}blackright(){ int x,y,n if(blackcurpos.y<8) {blackcurpos.y++x=position[blacktemppos.x][blacktemppos.y].x y=position[blacktemppos.x][blacktemppos.y].y if(board[blacktemppos.x][blacktemppos.y]==0)drawbmp(x,y,boardfile[blacktemppos.x][blacktemppos.y]) else if(!(blacktemppos.x==blackoldpos.x&&blacktemppos.y==blackoldpos.y&&blackstate==MOVE)) {n=board[blacktemppos.x][blacktemppos.y] drawbmp(x,y,chessfile[n]) } if(blacktemppos.x==blackoldpos.x&&blacktemppos.y==blackoldpos.y&&blackstate==MOVE)drawselecursor(blackoldpos) drawcursor(blackcurpos) blacktemppos.x=blackcurpos.x blacktemppos.y=blackcurpos.y }}blackdo(){ int i,j,x,y,n if(blackstate==SELECT&&blackcanselect()) {if(board[blackcurpos.x][blackcurpos.y]<=BLACK&&board[blackcurpos.x][blackcurpos.y]>0){ blackstate=MOVE drawselecursor(blackcurpos) blackoldpos.x=blackcurpos.x blackoldpos.y=blackcurpos.y } } else if(blackstate==MOVE&&blackcanmove()) {x=position[blackoldpos.x][blackoldpos.y].x y=position[blackoldpos.x][blackoldpos.y].y drawbmp(x,y,boardfile[blackoldpos.x][blackoldpos.y]) x=position[blackcurpos.x][blackcurpos.y].x y=position[blackcurpos.x][blackcurpos.y].y n=board[blackoldpos.x][blackoldpos.y] drawbmp(x,y,chessfile[n]) if(board[blackcurpos.x][blackcurpos.y]==RED_JIANG){ winner=BLACK finish=1 return }board[blackcurpos.x][blackcurpos.y]=n board[blackoldpos.x][blackoldpos.y]=0 for(i=0i<=2i++) for(j=3j<=5j++)if(board[i][j]==BLACK_JIANG) {x=iy=j}for(i=x+1,j=y,n=0i<=9i++){ if(board[i][j]==RED_JIANG&&n==0) {winner=REDfinish=1break} else if(board[i][j]!=0) n++ } turn=RED redstate=SELECT drawcursor(redcurpos) drawbmp(30,438,"bmp\\rzq.wfb") /*转交控制权给红方*/ } }blackundo(){ int x,y,n if(blackstate==MOVE) {x=position[blackoldpos.x][blackoldpos.y].x y=position[blackoldpos.x][blackoldpos.y].y n=board[blackoldpos.x][blackoldpos.y] drawbmp(x,y,chessfile[n]) blackoldpos.x=blackcurpos.x blackoldpos.y=blackcurpos.y drawcursor(blackcurpos) blackstate=SELECT }}/*----------------------------------------------------*/start(){ drawcursor(blackcurpos) drawbmp(30,438,"bmp\\bzq.wfb") while(!finish) {key=getkey() switch(key){ case RED_UP: if(turn==RED) redup() break case RED_DOWN: if(turn==RED) reddown() break case RED_LEFT: if(turn==RED) redleft() break case RED_RIGHT: if(turn==RED) redright() break case RED_DO: if(turn==RED) reddo() break case RED_UNDO: if(turn==RED) redundo() breakcase BLACK_UP: if(turn==BLACK) blackup() break case BLACK_DOWN: if(turn==BLACK) blackdown() break case BLACK_LEFT: if(turn==BLACK) blackleft() break case BLACK_RIGHT: if(turn==BLACK) blackright() break case BLACK_DO:if(turn==BLACK) blackdo() break case BLACK_UNDO:if(turn==BLACK) blackundo() break case ESCAPE: finish=1break } }}main(){ init() initpos() initchessmap() drawbmp(0,0,"bmp\\board.wfb") initdrawchess() /*初始化光标位置*/ redcurpos.x=redoldpos.x=redtemppos.x=9 redcurpos.y=redoldpos.y=redtemppos.y=8 blackcurpos.x=blackoldpos.x=blacktemppos.x=0 blackcurpos.y=blackoldpos.y=blacktemppos.y=0/*开始*/ start() if(winner==RED)drawbmp(200,200,"bmp\\redwin.wfb") else if(winner==BLACK)drawbmp(200,200,"bmp\\blackwin.wfb") elsedrawbmp(200,200,"bmp\\exit.wfb") getch() end()}

你是要交作业吗?是要原创的吗?网上的可能很多人都有,但我自己写的还没有发表,哈哈。这里贴不了全的,我可以写一段给你看看,你觉得可以,就找我要吧,完全free的,能帮到你最好。

贴保存棋局和读取棋局部分的,你先看看:

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

#include #include #include #include #include

int x,y,i,j,k,p,q, num = 1, round //象棋游戏的全局变量

int place_x1 = 0,place_y1 = 0,place_x2 = 0,place_y2 = 0

int check_x,check_y,check_turn //基本参数

char ch, turn = 'O',turn1 = 'N',temp,temp1

char check_1[9][3] ={"车","马","象","士","将","炮","兵","+-"} //取棋子时只判断前8合法

char check_2[9][3] ={"车","马","相","仕","帅","炮","卒","+-"} //下棋时多一空位合法

char check[3]

void check_main1(char* temp,char* temp1,char* turn,char* turn1,int *num,int *if_return,char map[100][100])

{ //(象棋函数 判断 将方 下棋是否合法

check[0] = *temp check[1] = *temp1 check[2] = '\0' char a,b

for ( i = 0i <8i++)

{ if ( strcmp(check_2[i],check) == 0)

{ *temp = *turn *temp1 = *turn1 *turn = 'O' *turn1 = 'N'

if( i <7){ printf(" 帅方的%s被吃\n",check_2[i]) Sleep(500)} *num = *num + 1

for( k = 4k <= 8k = k + 2) //判断 帅 是否死亡

{ for(j = 15j <= 23j= j+ 4)

{ if (map[k][j] == check_2[4][0] &&map[k][j+1] == check_2[4][1])

{ place_x2 = k place_y2 = j break} }

if( j <= 23) break

}

if( k == 10)

{printf(" 帅 被将死 将方获得胜利\n")printf("按任意键返回菜单")

getch( ) *if_return = 1return

}

for( k = 18k <= 22k = k + 2) //判断 将 是否死亡

{for(j = 15j <= 23j= j+ 4)

{if(map[k][j] == check_1[4][0] &&map[k][j+1] == check_1[4][1])

{place_x1 = k place_y1 = j break} }

if( j <= 23) break

}

if ( k == 24)

{printf(" 将 被将死 帅方获得胜利\n")printf("按任意键返回菜单")

getch( ) *if_return = 1 return

}

if ( place_y1 == place_y2)

{for( k = place_x2 + 2k <= place_x1 - 2k = k +2) {if(map[k][place_y1] != '+') break}

if( k == place_x1)

{if(round == 1) printf(" 将方对将 帅方胜利")

else if( round == 2) printf(" 帅方对将 将方胜利")

printf("按任意键返回菜单") getch( ) *if_return = 1 return

}

}

break

}

} // for ( i = 0i <8i++)循环结束

if( i == 8) {printf("不合法的走法\n")Sleep(500)}

}

void check_main2(char* temp,char* temp1,char* turn,char* turn1,int *num,int *if_return,char map[100][100])

{ //象棋函数 判断 帅方 下棋是否合法

check[0] = *temp check[1] = *temp1 check[2] = '\0' char a,b

for ( i = 0i <8i++)

{if ( strcmp(check_1[i],check) == 0)

{ *temp = *turn *temp1 = *turn1 *turn = 'O' *turn1 = 'N'

if( i <7) {printf(" 将方的%s被吃",check_1[i]) Sleep(500)}*num = *num + 1

for( k = 4k <= 8k = k + 2) //判断 帅 是否死亡

{for(j = 15j <= 23j= j+ 4)

{if(map[k][j] == check_2[4][0] &&map[k][j+1] == check_2[4][1])

{place_x2 = k place_y2 = j break} }

if( j <= 23) break

}

if( k == 10)

{printf(" 帅 被将死 将方获得胜利\n")printf("按任意键返回菜单") getch( )

*if_return = 1 return

}

for( k = 18k <= 22k = k + 2) //判断 将 是否死亡

{for(j = 15j <= 23j= j+ 4)

{if(map[k][j] == check_1[4][0] &&map[k][j+1] == check_1[4][1])

{place_x1 = k place_y1 = j break} }

if( j <= 23) break

}

if( k == 24)

{printf(" 将 被将死 帅方获得胜利\n")printf("按任意键返回菜单") getch( )

*if_return = 1 return}

if( place_y1 == place_y2)

{for( k=place_x2 + 2k <= place_x1 - 2k=k +2) {if(map[k][place_y1] != '+') break}

if( k == place_x1)

{if(round==1)printf(" 将方对将 帅方胜利")else if(round==2)printf(" 帅方对将 将方胜利")

printf("按任意键返回菜单") getch( ) *if_return = 1 return }

}

break

}

} // for ( i = 0i <8i++)循环结束

if( i == 8) {printf("不合法的走法\n")Sleep(500)}

}

void xiangqi( ) //象棋主程序

{ char map[100][100]= { "[[===================================]]",

"[| ①将 【象棋】②帅 |]",

"[[===================================]]",

"[[-----------------------------------]]",

"[[ 车—-马—-相—-仕—-帅—-仕—-相—-马—-车]]",

"[[ | | | | \\ | / | | | | ]]",

"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",

"[[ | | | | / | \\ | | | | ]]",

"[[ +-—-炮—-+-—-+-—-+-—-+-—-+-—-炮—-+-]]",

"[[ | | | | | | | | | ]]",

"[[ 卒—-+-—-卒—-+-—-卒—-+-—-卒—-+-—-卒]]",

"[[ | | | | | | | | | ]]",

"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",

"[[===================================]]",

"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",

"[[ | | | | | | | | | ]]",

"[[ 兵—-+-—-兵—-+-—-兵—-+-—-兵—-+-—-兵]]",

"[[ | | | | | | | | | ]]",

"[[ +-—-炮—-+-—-+-—-+-—-+-—-+-—-炮—-+-]]",

"[[ | | | | \\ | / | | | | ]]",

"[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]",

"[[ | | | | / | \\ | | | | ]]",

"[[ 车—-马—-象—-士—-将—-士—-象—-马—-车]]",

"[[-----------------------------------]]",

"[[===================================]]"}

int if_return = 0

system("mode con cols=45 lines=32") //迷你界面

system("color 70")

printf("[[==================================]]\n")

printf("[[ -------------------------------- ]]\n")

printf("[[ | | ]]\n")

printf("[[ | 【<>】 | ]]\n")

printf("[[ | | ]]\n")

printf("[[ |------------------------------| ]]\n")

printf("[[ | 控制wasd双方轮流控制指针下棋| ]]\n")

printf("[[ |------------------------------| ]]\n")

printf("[[ | 键盘输入大小写 ' M '| ]]\n")

printf("[[ | 都视为确认下棋 | ]]\n")

printf("[[ |------------------------------| ]]\n")

printf("[[ | 为了方便区分棋子 | ]]\n")

printf("[[ | 后手方全设为繁体复杂字体| ]]\n")

printf("[[ |------------------------------| ]]\n")

printf("[[ |------------------------------| ]]\n")

printf("[[ | 我已阅读规则,按任意键继续 | ]]\n")

printf("[[ |------------------------------| ]]\n")

printf("[[==================================]]\n")

getch( ) system("mode con cols=45 lines=32") //迷你界面

system("color 70")

for ( i = 0i <27i++){ puts(map[i]) Sleep(100)}

x = 6, y = 19 temp = map[x][y] temp1 = map[x][y+1]

while(num)

{ if (num % 2 == 1 &&num / 2 % 2 == 0){ printf(" 现在是'将'的回合\n")round = 1}

else if( num %2 == 1){ printf(" 现在轮到'帅'的回合了\n")round = 2}

ch = getch( )

if ( ch == 's') //下移

{ if ( map[x+1][y]!= '-')

{map[x][y] =temp map[x][y+1] = temp1 x = x + 2

temp = map[x][y] temp1 = map[x][y+1] map[x][y] = turn map[x][y+1] = turn1}

}

else if ( ch == 'a') //左移

{ if (map[x][y-1]!=' ')

{map[x][y] =temp map[x][y+1] = temp1y = y - 4

temp = map[x][y] temp1 = map[x][y+1] map[x][y] = turn map[x][y+1] = turn1}

}

else if ( ch == 'w') //上移

{ if ( map[x-1][y]!= '-')

{map[x][y] =temp map[x][y+1] = temp1x = x - 2 temp = map[x][y]

temp1 = map[x][y+1]map[x][y] = turn map[x][y+1] = turn1 }

}

else if ( ch == 'd') //右移

{ if (map[x][y+2]!=']')

{map[x][y] =temp map[x][y+1] = temp1y = y + 4 temp = map[x][y]

temp1 = map[x][y+1]map[x][y] = turn map[x][y+1] = turn1 }

}

else if( ch == 'm' || ch =='M') //M确认要移动的棋子,或确认要移到的目的地

{ if (num % 2 == 1 &&temp != '+' &&temp1 != '-') //取棋

{check[0] = temp check[1] = temp1 check[2] = '\0'

if ( round == 1)

{ for ( i = 0i <7i++)//将方

{ if ( strcmp(check_1[i],check) == 0)

{turn = temp turn1 = temp1 temp = '+' temp1 = '-'

check_x = x check_y = y check_turn = 10 + i num++ break}

}

if( i == 7){ printf("这不是你的棋子\n")Sleep(500)}

}

else if( round == 2)

{for ( i = 0i <7i++) //帅方

{ if( strcmp(check_2[i],check) == 0)

{turn = temp turn1 = temp1 temp = '+' temp1 = '-'

check_x = x check_y = y check_turn = 20 + i num++ break }

}

if( i == 7){ printf("这不是你的棋子\n")Sleep(500)}

}

}

else if( num % 2 == 0) //放棋

{ char check_1[8][3] ={"车","马","象","士","将","炮","卒","+-"}

char check_2[8][3] ={"俥","马","相","仕","帅","軳","兵","+-"}

//中界 楚河上下坐标 12 15 往下2 往右4

if( check_turn <20) //将方

{if( check_turn == 10) //车的走法规范

{ if((x == check_x &&y == check_y))

{temp = turn temp1 = turn1 turn = 'O' turn1 = 'N' num--

printf("三思而后行\n")printf("还是你的回合")Sleep(500) }

else if( y == check_y )

{ if( x >check_x)

{ for(j = check_x + 2j <xj = j + 2)

{ if(map[j][y] == '+')else{printf("不合法的下法\n")Sleep(500)break} }

if( j >= x) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)

}

if( x check_x){ for(j = check_x - 2j >xj = j - 2)

{ if(map[j][y] == '+')else{printf("不合法的下法\n")Sleep(500)break}

}

if( j <= x)check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)

}

}

else if( x == check_x )

{if( y >check_y)

{for(j = check_y + 4j <yj = j + 4)

{if(map[x][j] == '+')else {printf("不合法的下法\n")Sleep(500) break}

}

if( j >= y) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)

}

if( y <check_y)

{for(j = check_y - 4j >yj = j - 4)

{ if(map[x][j] == '+')else { printf("不合法的下法\n")Sleep(500) break}

}

if( j <= y) check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)

}

}

else { printf("不合法的下法\n")Sleep(500)}

}

if( check_turn == 11) //马的走法规范

{if((x == check_x &&y == check_y))

{ temp = turn temp1 = turn1 turn = 'O' turn1 = 'N' num--

printf("三思而后行\n")printf("还是你的回合")Sleep(500) }

else if( (abs( x - check_x) == 2&&abs( y - check_y) == 8)&&map[check_x][(y+check_y)/2] =='+')

{check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)}

else if( (abs( x - check_x) == 4&&abs( y - check_y) == 4)&&map[(x + check_x)/2][check_y] == '+' )

{check_main1(&temp,&temp1,&turn,&turn1,&num,&if_return,map)}

else { printf("不合法的下法\n")Sleep(500)}

} //其余代码在后续


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6176928.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-17
下一篇 2023-03-17

发表评论

登录后才能评论

评论列表(0条)

保存