XFS_算法中国象棋算法研究分析代码
中国象棋python代码
中国象棋python代码下面是一个简单的中国象棋的Python 代码示例:# 定义棋盘# 绘制棋盘def draw_board():for row in chessboard:for piece in row:print(piece, end=' ')print()# 判断是否在棋盘内def is_valid_move(x, y):return 0 <= x < 9 and 0 <= y < 10# 判断移动是否合法def is_valid_move(x1, y1, x2, y2):piece = chessboard[y1][x1]target = chessboard[y2][x2]if piece == ' ':return Falseif piece == '車' or piece == '車':if x1 != x2 and y1 != y2:return Falseif x1 == x2:min_y, max_y = (y1, y2) if y1 < y2 else (y2, y1)for y in range(min_y + 1, max_y):if chessboard[y][x1] != ' ':return Falseif y1 == y2:min_x, max_x = (x1, x2) if x1 < x2 else (x2, x1)for x in range(min_x + 1, max_x):if chessboard[y1][x] != ' ':return Falseif piece == '馬' or piece == '馬':dx = abs(x2 - x1)dy = abs(y2 - y1)if (dx == 1 and dy == 2) or (dx == 2 and dy == 1): return Truereturn Falseif piece == '象' or piece == '相':if y2 > 4 and (y2 - y1) % 2 != 0:return Falseif abs(x2 - x1) != 2 or abs(y2 - y1) != 2:return Falseif chessboard[(y1 + y2) // 2][(x1 + x2) // 2] != ' ': return Falseif piece == '士' or piece == '士':if x2 < 3 or x2 > 5:return Falseif y2 < 7 or y2 > 9:return Falseif abs(x2 - x1) != 1 or abs(y2 - y1) != 1: return Falseif piece == '帥' or piece == '将':if x2 < 3 or x2 > 5:return Falseif y2 < 0 or y2 > 2:return Falseif abs(x2 - x1) + abs(y2 - y1) != 1:return Falseif piece == '兵':if y1 < 5 and y2 != y1 - 1:return Falseif y1 >= 5 and (x1 != x2 or y2 != y1 - 1):return Falseif piece == '卒':if y1 > 4 and y2 != y1 + 1:return Falseif y1 <= 4 and (x1 != x2 or y2 != y1 + 1):return Falseif target == '帥' or target == '将':if piece == '卒' or piece == '兵':if y2 > 2:return Falseif piece == '兵' or piece == '卒':if y2 < 7:return Falsereturn True# 移动棋子def move_piece(x1, y1, x2, y2):if is_valid_move(x1, y1, x2, y2):piece = chessboard[y1][x1]chessboard[y2][x2] = piecechessboard[y1][x1] = ' 'else:print("Invalid move!")# 游戏循环def game_loop():while True:draw_board()player_input = input("请输入移动的起始位置和目标位置,以逗号分隔(例如:2,1,2,3):")positions = player_input.split(',')if len(positions) != 4:print("请输入正确的起始位置和目标位置!")continuex1, y1, x2, y2 = map(int, positions)if not is_valid_move(x1, y1) or not is_valid_move(x2, y2):print("请输入正确的起始位置和目标位置!")continuemove_piece(x1, y1, x2, y2)# 启动游戏game_loop()这是一个简单的中国象棋游戏代码示例,包括棋盘的绘制、棋子移动的判断和执行等功能。
“理治棋壮”中国象棋计算机博弈引擎关键算法分析与设计
“理治棋壮”中国象棋计算机博弈引擎关键算法分析与设计“理治棋壮”中国象棋计算机博弈引擎开发小组在《程序架构设计与主要算法概述》一文中,我们阐述了中国象棋计算机博弈涉及的主要算法。
下面就其中的关键算法进行深入分析,并说明其设计实现方法。
一、核心搜索算法1、Principal Variable Search搜索算法是计算机博弈程序的核心算法。
如何选择适合的搜索算法,配以合理的剪枝条件,是决定搜索效率的关键所在。
博弈树不同于一般的搜索树,它是由对弈双方共同产生的一种“变性”搜索树。
应对这类问题,香农(Claude Shannon)教授早在1950年提出了极大-极小算法(Minimax Algorithm)。
这种算法常以一种形式上的改进——负极大值算法出现:记我方节点值为正,对方节点值为负,双方在每一节点分别选择其子节点中绝对值最大的一个。
递归深度优先遍历有限层次的树,找到使起始节点绝对值最大的一个叶子节点,然后回溯找到形成这一叶子节点的第一层子节点,作为最优解。
可以在博弈树深度优先搜索过程中记录2个附加值,α:我方搜索到的最好值,任何比它更小的值就没用了;β:对于对手来说最坏的值。
在搜索算法中,如果某个节点的结果小于或等于α,那么它就可以抛弃;如果某个着法的结果大于或等于β,那么整个结点就作废了,因为对手不希望走到这个局面。
如果某个节点值大于α但小于β,那么这个节点就是可以考虑走。
这便是所谓的α-β剪枝搜索。
由α和β可以形成一个节点预选窗口。
如何能够快速得到一个尽可能小而又尽可能准确的窗口?有不少窗口搜索算法被设计出来解决这个问题,如:Aspiration Search、Principal Variable Search、Tolerance Search等。
目前大多数国际象棋与中国象棋的算法核心青睐速度快而不会出现错误剪枝的Principal Variable Search,它的原理是第一个分枝以完整窗口搜索,产生一个解v,后继分枝则以一个极小窗口(v,v+1)搜索之,旨在建立高效的、极小的搜索树。
中国象棋博弈算法研究
本科毕业论文(科研训练、毕业设计)题目:中国象棋博弈算法研究******学院:软件学院系:软件工程专业:软件工程年级:2004级学号:********指导教师:史亮职称:副教授2008年6月5日摘要计算机博弈是人工智能研究的一个重要分支,被专家门称为人工智能界的果蝇,意思是说人类对计算机博弈的研究衍生了大量的研究成果,这些成果在人工智能领域产生了重要影响。
国际象棋计算机博弈研究已经有了五十多年的历史,IBM公司在1997年开发出了超级计算机“深蓝”战胜了当时世界国际象棋大师卡斯帕罗夫,标志其水平已达到国际象棋世界冠军水平。
而中国象棋的历史更为悠久,虽然中国象棋计算机博弈研究起步晚于国际象棋,但起点高,国际象棋计算机博弈研究的成果为我们提供了很多的借鉴技术。
近年来随着研究的不断深入,中国象棋计算机博弈越来越成为继国际象棋后计算机博弈研究的热点之一。
本文在对目前主流的计算机博弈技术进行全面的综述后,对构成计算机博弈系统的四个组成部分进行了优化和改进,特别是针对静态估值算法不能应对局势变化的固有缺点,提出了动态局势再评估算法。
在此之上实现了一个中国象棋计算机博弈系统,论文主要研究了以下3方面的问题:第一、对计算机博弈系统的四个组成部分及基础技术进行了研究,包括数据结构,着法生成,搜索算法,估值算法。
第二、研究了建立在Alpha-Beta搜索算法基础之上的各种优化技术。
主要讨论了窗口探测,静寂搜索,历史启发,深层迭代,Null Move5个方面的优化方法,并根据实验结果结合置换表技术提出了具体的组合方案。
第三、论文针对目前广泛使用的静态估值算法不能应对局势变化的固有缺点,提出了动态局势再评估算法。
通过引入“局势因子”,使得估值算法根据当前局面形势做出攻防策略。
关键词:人工智能;中国象棋;博弈算法;动态局势再评估;局势因子AbstractComputer game is an important branch of artificial intelligence research. It is described as a fruit fly of the artificial intelligence by experts. That’s to say human’s research to the computer game has achieved massive research results. These achievements have played an important influence on a more widespread domain. Throug overseas researchers’ exploration of chess gambling system for more than 50 years, IBM Corporation developed super computer ”DarkBlue” in1997,and has defeated world chess master Ksparov; while the Chinese chess history is more glorious. The research of Chinese chess computer game is later than the research of chess computer game, but it based on the computer g ame’s research results. With the deeper study of research, Chinese chess computer game becomes one of the most active parts of computer game research area recently.After summarizing related researches on Chinese chess computer game. Some key problems are studied and discussed in this dissertation. Based on above work,an integrated Chinese chess computer game system are designed and developed. The whole work mainly focuses on the following three aspects:1. Introduce the key component parts of a Chinese chess computer game system which involve date structure, generate legal moves, search algorithms and evaluate algorithms.2. Make a study on the optimization of search algorithm based on the Alpha-Beta algorithm which included Principal Variation Search, quiescence search, history heuristic, interative deepening, null-move pruning and so on.3. This paper provides an algorithm called “dynamic situation evaluate algorithm” which avoids the drawback of static evaluate. As the introduction of “decision factor”, computer can make decisions by situation.Key words: artificial intelligence; Chinese chess; game algorithm; dynamic situation evaluate algorithm; decision factor目录第一章绪论 (8)1.1 选题背景和研究意义 (8)1.2 中国象棋计算机博弈的发展历程 (8)1.3 国内外研究现状 (10)1.4 本文的主要工作和论文结构 (11)第二章背景知识 (13)2.1 数据结构 (13)2.1.1 棋盘表示 (13)2.1.2 置换表 (14)2.2 着法生成 (15)2.3 搜索算法 (16)2.3.1 博弈树的基本概念 (16)2.3.2极大极小算法 (17)2.3.3 负极大值法 (19)2.3.4 Alpha-Beta搜索算法 (20)2.4 估值算法 (22)2.5 本章小结 (23)第三章搜索算法的优化 (24)3.1 窗口探测 (24)3.1.1渴望搜索 (24)3.1.2 极小窗口算法 (25)3.2 静寂搜索 (26)3.3 历史启发 (26)3.4 深层迭代 (27)3.5 Null Move (29)3.6 内存优化 (29)3.7 本章小结 (30)第四章动态局势再评估算法 (31)4.1 静态评估算法详述 (31)4.1.1 对子力和攻击性的评估 (31)4.1.2 对棋子位置附加值的评估 (31)4.1.3 对灵活性的评估 (32)4.1.4 对棋子的协调性和保护性的评估 (32)4.1.5 静态估值函数 (33)4.2 静态估值函数的缺陷 (33)4.3 局势因子及动态局势再评估函数 (33)4.4 动态局势再评估算法的步骤 (36)4.5 本章小结 (36)第五章中国象棋计算机博弈系统——出棋制胜的设计与实现 (38)5.1 系统设计 (38)5.1.1中国象棋通用引擎协议层(UCCI) (38)5.1.2 “出棋制胜”软件系统结构图 (39)5.2 详细设计 (39)5.2.1 棋盘棋子表示 (39)5.2.2 着法生成 (40)5.2.3 搜索算法 (43)5.2.4 评估算法 (45)5.2.5 置换表 (45)5.3 实验结果和相关问题的讨论 (46)5.4 本章小结 (47)第六章总结 (48)致谢 (49)参考文献 (50)ContentsChapter 1 Introduction (8)1.1 Research Topics’ Background and Significance (8)1.2 Chinese Chess Computer Game’s Developing Process (8)1.3 The Status Quo at Home and Abroad (10)1.4 The Main Work and Structure of this thesis (11)Chapter 2 Background Knowledge (13)2.1 Date Structure (13)2.1.1 Chess Board Expression (13)2.1.2 Transposing Table (14)2.2 Moves Generation (15)2.3 Search Algorithms (16)2.3.1 game tree’s concept (16)2.3.2 Minimax Algorithm (17)2.3.3 Negamax Algorithm (19)2.3.4 Alpha-Beta Algorithm (20)2.4 Evaluate Algorithm (22)2.5 Chapter Summary (23)Chapter 3 The Optimization of Search Algorithm (24)3.1 Window Detection (24)3.1.1 Eager Search (24)3.1.2 Principal Variation Search (25)3.2 Quiescence Search (26)3.3 History Heuristic (26)3.4 Interative Deepening (27)3.5 Null Move (29)3.6 Memory Optimization (29)3.7 Chapter Summary (30)Chaper 4 Dynamic Situation Evaluate Algorithm (31)4.1 Static Evaluate Algorithm (31)4.1.1 The Evaluation of The C hessman’s Value (31)4.1.2 The Evaluation of The C hessman’s Position (31)4.1.3 The Evaluation of The C hessman’s Movability (32)4.1.4 The Evaluation of The C hessman’s Compatibility (32)4.1.5 Static Evaluate Method (33)4.2 Static Evaluate Method’s Disadvantage (33)4.3 Decision Factor and Dynamic Situation Evaluate Algorithm (33)4.4 The Steps of Dynamic Situation Evaluate Algorithm (36)4.5 Chapter Summary (36)Chaper 5 Chinese Chess Computer Game System (38)5.1 System Design (38)5.1.1 Universal Chinese Chess Protocol (UCCI) (38)5.1.2 The Structure of The System (39)5.2 Detailed Design (39)5.2.1 Chess Board Expression (39)5.2.2 Moves Generation (40)5.2.3 Search Algorithms (43)5.2.4 Evaluate Algorithm (45)5.2.5 Transposing Table (45)5.3 The Experimental Results and Discuss Related Issues (46)5.4 Chapter Summary (47)Chapter 6 Summary (48)Acknowledgement (49)References (50)第一章绪论1.1 选题背景和研究意义计算机博弈是人工智能研究的一个重要分支,被专家门称为人工智能界的果蝇,意思是说人类对计算机博弈的研究衍生了大量的研究成果,这些成果在人工智能领域产生了重要影响。
C语言程序源代码---中国象棋
#include<graphics.h>#include<conio.h>#include<string.h>#include<bios.h>#include<stdlib.h>#include"c:\tc\LIB\1.c"#define W 119#define S 115#define A 97#define D 100#define space 32#define UP 72#define DOWN 80#define LEFT 75#define RIGHT 77#define ENTER 13void qipan();void jiemian(int);void guangbiao1(int,int);void guangbiao2(int,int);void xuanzhong(int,int);void gaizi(int,int);char array(int,int);void xiazi(int,int,int,int);/*int panding(char,int,int,int,int);*/main(){int gdriver,gmode,i=0,c=0,x=190,y=190,m,n; char p;FILE *fp;gdriver=DETECT;gmode=0;if((fp=fopen("file.txt","at")) == NULL) {printf("Cannot open file!");system("pause");exit(0);}printf("%d,%d",gdriver,gmode); registerbgidriver(EGAVGA_driver);initgraph(&gdriver,&gmode,"c:\\tc"); cleardevice();while(c!=27){c=getch();clrscr();jiemian(i);if(c==80){fputs("down ",fp);i++;if(i==4){i=0;}}if(i==1){if(c==13){fputs("enter ",fp);qipan();c=getch();while(c!=27){c=getch();if(c==115){fputs("S ",fp);y=y+40;guangbiao1(x,y);guangbiao2(x,y-40);}if(c==119){fputs("W ",fp);y=y-40;guangbiao1(x,y);guangbiao2(x,y+40);}if(c==97){ fputs("A\n",fp);x=x-40;guangbiao1(x,y);guangbiao2(x+40,y);}if(c==100){ fputs("D\n",fp);x=x+40;guangbiao1(x,y);guangbiao2(x-40,y);}if(c==13){fputs("enter\n",fp);xuanzhong(x,y);m=x;n=y;}if(c==32){fputs("space\n",fp);xiazi(m,n,x,y);fputs("gaizi\n",fp);gaizi(m,n);}if(x>350||y>390||x<30||y<30){x=190;y=30;}}}}}getch();closegraph();fclose(fp);restorecrtmode();return 0;}void qipan(){int i,j;setbkcolor(GREEN);cleardevice();setlinestyle(0,0,3);setcolor(1);rectangle(10,10,370,410);rectangle(30,30,350,390);for(i=1;i<8;i++){setlinestyle(0,0,3);line(i*40+30,30,i*40+30,190);line(i*40+30,230,i*40+30,390);}for(j=1;j<9;j++){setlinestyle(0,0,3);line(30,j*40+30,350,j*40+30);}setlinestyle(3,0,3);line(150,30,230,110);line(230,30,150,110);line(150,310,230,390);line(230,310,150,390); setusercharsize(4,1,2,1); settextstyle(1,0,4);outtextxy(70,195,"chinese chess"); red_shuai(190,30);red_shi(150,30);red_shi(230,30);red_xiang(110,30);red_xiang(270,30);red_ma(70,30);red_ma(310,30);red_ju(30,30);red_ju(350,30);red_pao(70,110);red_pao(310,110);red_bing(30,150);red_bing(110,150);red_bing(190,150);red_bing(270,150);red_bing(350,150);black_jiang(190,390);black_shi(150,390);black_shi(230,390);black_xiang(110,390);black_xiang(270,390);black_ma(70,390);black_ma(310,390);black_ju(30,390);black_ju(350,390);black_pao(70,310);black_pao(310,310);black_zu(30,270);black_zu(110,270);black_zu(190,270);black_zu(270,270);black_zu(350,270);setcolor(BLUE);rectangle(400,30,600,320);setcolor(4);settextstyle(1,0,2);outtextxy(420,50,"A->shuai B->shi"); outtextxy(420,80,"C->xiang D->ma"); outtextxy(420,110,"E->ju F->pao"); outtextxy(420,140,"G->bing"); setcolor(8);outtextxy(420,200,"H->jiang I->shi"); outtextxy(420,230,"J->xiang K->ma"); outtextxy(420,260,"L->ju M->pao"); outtextxy(420,290,"N->zu");}void jiemian(int i){setbkcolor(GREEN); cleardevice();settextstyle(1,0,8);setcolor(BLUE);outtextxy(50,70,"chinese chess"); settextstyle(0,0,3);setcolor(RED);outtextxy(260,215,"start"); outtextxy(260,255,"again"); outtextxy(260,295,"undo"); outtextxy(260,335,"exit"); rectangle(250,210+i*40,390,240+i*40); }void guangbiao1(int x,int y){setcolor(WHITE);setlinestyle(0,0,3);line(x-17,y-7,x-17,y-17);line(x-7,y-17,x-17,y-17);line(x+7,y-17,x+17,y-17);line(x+17,y-7,x+17,y-17);line(x-7,y+17,x-17,y+17);line(x-17,y+7,x-17,y+17);line(x+17,y+7,x+17,y+17);line(x+7,y+17,x+17,y+17);}void guangbiao2(int x,int y){setcolor(GREEN);setlinestyle(0,0,3);line(x-17,y-7,x-17,y-17);line(x-7,y-17,x-17,y-17);line(x+7,y-17,x+17,y-17);line(x+17,y-7,x+17,y-17);line(x-7,y+17,x-17,y+17);line(x-17,y+7,x-17,y+17);line(x+17,y+7,x+17,y+17);line(x+7,y+17,x+17,y+17);}void xuanzhong(int x,int y){setcolor(CYAN);setlinestyle(0,0,3);circle(x,y,15);}void gaizi(int x1,int y1){setlinestyle(0,0,3);setcolor(GREEN);circle(x1,y1,15);setfillstyle(0,3);floodfill(x1,y1,GREEN);setcolor(1);setlinestyle(0,0,3);if((30<x1<350)&&((y1==30)||(y1==230))) {line(x1-18,y1,x1+18,y1);line(x1,y1,x1,y1+18);if((30<x1<350)&&(y1==390||y1==190)) {line(x1-18,y1,x1+18,y1);line(x1,y1-18,x1,y1);}if((30<y1<390)&&x1==30){line(x1,y1,x1+18,y1);line(x1,y1-18,x1,y1+18);}if((30<y1<390)&&(x1==350)){line(x1-18,y1,x1,y1);line(x1,y1-18,x1,y1+18);}if((x1==30)&&(y1==30)){line(x1,y1,x1+18,y1);line(x1,y1,x1,y1+18);}if((x1==350)&&(y1==30)){line(x1-18,y1,x1,y1);line(x1,y1,x1,y1+18);}if((x1==30)&&(y1==390)){line(x1,y1,x1+18,y1);line(x1,y1,x1,y1-18);}if((x1==350)&&(y1==390)){line(x1,y1,x1-18,y1);line(x1,y1,x1,y1-18);}else{line(x1-18,y1,x1+18,y1);line(x1,y1-18,x1,y1+18);}}char array(int i,int j)char a[13][13];int c,b;c=i;b=j;for(c=1;c<10;c++){for(b=1;b<11;b++){a[c][b]='Z';}}a[1][5]='A';a[1][4]='B';a[1][6]='B';a[1][3]='C';a[1][7]='C';a[1][2]='D';a[1][8]='D';a[1][1]='E';a[1][9]='E';a[3][2]='F';a[3][8]='F';a[4][1]=a[4][3]=a[4][5]=a[4][7]=a[4][9]='G';a[10][5]='H';a[10][4]='I';a[10][6]='I';a[10][3]='J';a[10][7]='J';a[10][2]='K';a[10][8]='K';a[10][1]='L';a[10][ 9]='L';a[2][3]='M';a[8][3]='M';a[7][1]=a[7][3]=a[7][5]=a[7][7]=a[7][9]='N';return a[i][j];}void xiazi(int x6,int y6,int x7,int y7){switch(array(y6/40+1,x6/40+1)){case 'A':red_shuai(x7,y7);break;case 'B':red_shi(x6,y7);break;case 'C':red_xiang(x7,y7);break;case 'D':red_ma(x7,y7);break;case 'E':red_ju(x7,y7);break;case 'F':red_pao(x7,y7);break;case 'G':red_bing(x7,y7);break;case 'H':black_jiang(x7,y7);break;case 'I':black_shi(x7,y7);break;case 'J':black_xiang(x7,y7);break;case 'K':black_ma(x7,y7);break;case 'L':black_ju(x7,y7);break;case 'M':black_pao(x7,y7);break;case 'N':black_zu(x7,y7);break;case 'Z':gaizi(x6,x6);break;}}/*int panding(char q,int x,int y,int a,int b){switch(q){case 'A':if(y>110||x>230||x<150||(a-x)>40||(x-a)>40||(y-b)>40||(b-y)>40)return 0;elsereturn 1;break;case'B':if(((x-a)==40&&(y-b)==40)&&y<=110&&230<x<150||((a-x)==40&&(b-y)==40)&&y<=110& &230>x>150)return 1;elsereturn 0;break;case'C':if((((x-a)==80&&(y-b)==80)&&y<=190)&&(array((y+b)/2/40+1,(x+a)/2/40+1,)=='Z')))||(((a-x)==80&&(b-y)==80)&&y<=190)&&(array((y+b)/2/40+1,(x+a)/2/40+1)=='Z'))))return 1;elsereturn 0;break;case'D':if((((x-a)==80&&(y-b)==40&&(array(y/40+1,(x-40)/40+1)=='Z'))||(((a-x)==80&&(b-y)==40)&&(array(y/40+1,(x+40)/40+1)=='Z'))||(((x-a)==40&&(y-b)==80)(array((y-40)/40+1,x/40+1)==' Z'))||(((a-x)==40&&(b-y)==80)&&(array((y+40)/40+1,x/40+1)=='z'))))return 1;elsereturn 0;break;case 'E':return 1;break;case 'F':return 1;break;case 'G':if(y<190){if(y>b||x!=a){return 0;}elsereturn 1;}else{if((b-y)>40||(a-x)>40||(x-a)>40||y>b){return 0;}elsereturn 1;}break;case 'H':if(y<310||x>230||x<150||(a-x)>40||(x-a)>40||(y-b)>40||(b-y)>40)return 0;elsereturn 1;break;case'I':if(((x-a)==40&&(y-b)==40)&&y>=310&&230<x<150||((a-x)==40&&(b-y)==40)&&y>310&& 230>x>150)return 1;elsereturn 0;break;case可编辑'J':if(((((x-a)==80&&(y-b)==80)&&y>=230)&&array(((y+b)/2/40+1,(x+a)/2/40+1)=='Z')))||(((a-x )==80&&(b-y)==80)&&y>=230)&&(array((y+b)/2/40+1,(x+a)/2/40+1)=='Z'))))return 1;elsereturn 0;break;case'K':if((((x-a)==80&&(y-b)==40&&(array(y/40+1,(x-40)/40+1)=='Z'))||(((a-x)==80&&(b-y)==40) &&(array(y/40+1,(x+40)/40+1)=='Z'))||(((x-a)==40&&(y-b)==80)(array((y-40)/40+1,x/40+1)==' Z'))||(((a-x)==40&&(b-y)==80)&&(array((y+40)/40+1,x/40+1)=='Z'))return 1;elsereturn 0;break;case 'L':return 1;break;case 'M':return 1;break;case 'N':if(y>230){if(y<b||x!=a){return 0;}elsereturn 1;}else{if(y-b>40||(a-x)>40||(x-a)>40||y<b){return 0;}elsereturn 1;}default:return 0;}}*/.。
C语言知识学习程序源代码-中国象棋
#include<graphics.h> #include<conio.h>#include<string.h>#include<bios.h>#include<stdlib.h>#include"c:\tc\LIB\1.c"#define W 119#define S 115#define A 97#define D 100#define space 32#define UP 72#define DOWN 80#define LEFT 75#define RIGHT 77#define ENTER 13void qipan();void jiemian(int);void guangbiao1(int,int); void guangbiao2(int,int);void xuanzhong(int,int);void gaizi(int,int);char array(int,int);void xiazi(int,int,int,int);/*int panding(char,int,int,int,int);*/main(){int gdriver,gmode,i=0,c=0,x=190,y=190,m,n; char p;FILE *fp;gdriver=DETECT;gmode=0;if((fp=fopen("file.txt","at")) == NULL) {printf("Cannot open file!");system("pause");exit(0);}printf("%d,%d",gdriver,gmode); registerbgidriver(EGAVGA_driver);initgraph(&gdriver,&gmode,"c:\\tc"); cleardevice();while(c!=27){c=getch();clrscr();jiemian(i);if(c==80){fputs("down ",fp);i++;if(i==4){i=0;}}if(i==1){if(c==13){fputs("enter ",fp);qipan();c=getch();while(c!=27){c=getch();if(c==115){fputs("S ",fp);y=y+40; guangbiao1(x,y); guangbiao2(x,y-40);}if(c==119){fputs("W ",fp);y=y-40;guangbiao1(x,y);guangbiao2(x,y+40); }if(c==97){ fputs("A\n",fp);x=x-40;guangbiao1(x,y);guangbiao2(x+40,y); }if(c==100){ fputs("D\n",fp);x=x+40;guangbiao1(x,y);guangbiao2(x-40,y); }if(c==13){fputs("enter\n",fp);xuanzhong(x,y);m=x;n=y;}if(c==32){fputs("space\n",fp);xiazi(m,n,x,y);fputs("gaizi\n",fp);gaizi(m,n);}if(x>350||y>390||x<30||y<30){x=190;y=30;}}}}}getch();closegraph();fclose(fp);restorecrtmode();return 0;}void qipan(){int i,j;setbkcolor(GREEN);cleardevice();setlinestyle(0,0,3);setcolor(1);rectangle(10,10,370,410); rectangle(30,30,350,390);for(i=1;i<8;i++){setlinestyle(0,0,3);line(i*40+30,30,i*40+30,190); line(i*40+30,230,i*40+30,390); }for(j=1;j<9;j++){setlinestyle(0,0,3);line(30,j*40+30,350,j*40+30); }setlinestyle(3,0,3);line(150,30,230,110);line(230,30,150,110);line(150,310,230,390);line(230,310,150,390); setusercharsize(4,1,2,1);settextstyle(1,0,4);outtextxy(70,195,"chinese chess"); red_shuai(190,30);red_shi(150,30);red_shi(230,30);red_xiang(110,30);red_xiang(270,30);red_ma(70,30);red_ma(310,30);red_ju(30,30);red_ju(350,30);red_pao(70,110);red_pao(310,110);red_bing(30,150);red_bing(110,150);red_bing(190,150);red_bing(270,150);red_bing(350,150);black_jiang(190,390);black_shi(150,390);black_shi(230,390);black_xiang(110,390);black_xiang(270,390);black_ma(70,390);black_ma(310,390);black_ju(30,390);black_ju(350,390);black_pao(70,310);black_pao(310,310);black_zu(30,270);black_zu(110,270);black_zu(190,270);black_zu(270,270);black_zu(350,270);setcolor(BLUE);rectangle(400,30,600,320);setcolor(4);settextstyle(1,0,2);outtextxy(420,50,"A->shuai B->shi"); outtextxy(420,80,"C->xiang D->ma"); outtextxy(420,110,"E->ju F->pao"); outtextxy(420,140,"G->bing"); setcolor(8);outtextxy(420,200,"H->jiang I->shi");outtextxy(420,230,"J->xiang K->ma"); outtextxy(420,260,"L->ju M->pao"); outtextxy(420,290,"N->zu");}void jiemian(int i){setbkcolor(GREEN);cleardevice();settextstyle(1,0,8);setcolor(BLUE);outtextxy(50,70,"chinese chess"); settextstyle(0,0,3);setcolor(RED);outtextxy(260,215,"start");outtextxy(260,255,"again"); outtextxy(260,295,"undo"); outtextxy(260,335,"exit");rectangle(250,210+i*40,390,240+i*40); }void guangbiao1(int x,int y){setcolor(WHITE); setlinestyle(0,0,3);line(x-17,y-7,x-17,y-17); line(x-7,y-17,x-17,y-17); line(x+7,y-17,x+17,y-17); line(x+17,y-7,x+17,y-17); line(x-7,y+17,x-17,y+17); line(x-17,y+7,x-17,y+17); line(x+17,y+7,x+17,y+17); line(x+7,y+17,x+17,y+17); }void guangbiao2(int x,int y) {setcolor(GREEN); setlinestyle(0,0,3);line(x-17,y-7,x-17,y-17); line(x-7,y-17,x-17,y-17); line(x+7,y-17,x+17,y-17); line(x+17,y-7,x+17,y-17); line(x-7,y+17,x-17,y+17); line(x-17,y+7,x-17,y+17); line(x+17,y+7,x+17,y+17);line(x+7,y+17,x+17,y+17);}void xuanzhong(int x,int y){setcolor(CYAN);setlinestyle(0,0,3);circle(x,y,15);}void gaizi(int x1,int y1){setlinestyle(0,0,3);setcolor(GREEN);circle(x1,y1,15);setfillstyle(0,3);floodfill(x1,y1,GREEN);setcolor(1);setlinestyle(0,0,3);if((30<x1<350)&&((y1==30)||(y1==230))) {line(x1-18,y1,x1+18,y1);line(x1,y1,x1,y1+18);}if((30<x1<350)&&(y1==390||y1==190)) {line(x1-18,y1,x1+18,y1);line(x1,y1-18,x1,y1);}if((30<y1<390)&&x1==30){line(x1,y1,x1+18,y1);line(x1,y1-18,x1,y1+18);}if((30<y1<390)&&(x1==350)){line(x1-18,y1,x1,y1);line(x1,y1-18,x1,y1+18);}if((x1==30)&&(y1==30)){line(x1,y1,x1+18,y1);line(x1,y1,x1,y1+18);}if((x1==350)&&(y1==30)) {line(x1-18,y1,x1,y1);line(x1,y1,x1,y1+18);}if((x1==30)&&(y1==390)) {line(x1,y1,x1+18,y1);line(x1,y1,x1,y1-18);}if((x1==350)&&(y1==390)) {line(x1,y1,x1-18,y1);line(x1,y1,x1,y1-18);}else{line(x1-18,y1,x1+18,y1); line(x1,y1-18,x1,y1+18); }}char array(int i,int j)char a[13][13];int c,b;c=i;b=j;for(c=1;c<10;c++){for(b=1;b<11;b++){a[c][b]='Z';}}a[1][5]='A';a[1][4]='B';a[1][6]='B';a[1][3]='C';a[1][7]='C';a[1][2]='D';a[1][8]='D';a[1][1 ]='E';a[1][9]='E';a[3][2]='F';a[3][8]='F';a[4][1]=a[4][3]=a[4][5]=a[4][7]=a[4][9]='G';a[10][5]='H';a[10][4]='I';a[10][6]='I';a[10][3]='J';a[10][7]='J';a[10][2]='K';a[10][8]='K'; a[10][1]='L';a[10][9]='L';a[2][3]='M';a[8][3]='M';a[7][1]=a[7][3]=a[7][5]=a[7][7]=a[7][9]='N';return a[i][j];void xiazi(int x6,int y6,int x7,int y7) {switch(array(y6/40+1,x6/40+1)) {case 'A':red_shuai(x7,y7);break;case 'B':red_shi(x6,y7);break;case 'C':red_xiang(x7,y7);break;case 'D':red_ma(x7,y7);break;case 'E':red_ju(x7,y7);break;case 'F':red_pao(x7,y7);break;case 'G':red_bing(x7,y7);break;case 'H':black_jiang(x7,y7);break;case 'I':black_shi(x7,y7);break;case 'J':black_xiang(x7,y7);break;case 'K':black_ma(x7,y7);break;case 'L':black_ju(x7,y7);break;case 'M':black_pao(x7,y7);break;case 'N':black_zu(x7,y7);break;case 'Z':gaizi(x6,x6);break;}}/*int panding(char q,int x,int y,int a,int b) {switch(q){case 'A':if(y>110||x>230||x<150||(a-x)>40||(x-a)>40||(y-b)>40||(b-y)>40)return 0;elsereturn 1;break;case'B':if(((x-a)==40&&(y-b)==40)&&y<=110&&230<x<150||((a-x)==40&&(b-y)==4 0)&&y<=110&&230>x>150)return 1;elsereturn 0;break;case'C':if((((x-a)==80&&(y-b)==80)&&y<=190)&&(array((y+b)/2/40+1,(x+a)/2/40+1,) =='Z')))||(((a-x)==80&&(b-y)==80)&&y<=190)&&(array((y+b)/2/40+1,(x+a)/2/40 +1)=='Z'))))return 1;elsereturn 0;break;case'D':if((((x-a)==80&&(y-b)==40&&(array(y/40+1,(x-40)/40+1)=='Z'))||(((a-x)==80& &(b-y)==40)&&(array(y/40+1,(x+40)/40+1)=='Z'))||(((x-a)==40&&(y-b)==80)(arra y((y-40)/40+1,x/40+1)=='Z'))||(((a-x)==40&&(b-y)==80)&&(array((y+40)/40+1,x/4 0+1)=='z'))))return 1;elsereturn 0;break;case 'E':return 1;break;case 'F':return 1;break;case 'G':if(y<190){if(y>b||x!=a){return 0;}elsereturn 1;}else{if((b-y)>40||(a-x)>40||(x-a)>40||y>b){return 0;}elsereturn 1;}break;case 'H':if(y<310||x>230||x<150||(a-x)>40||(x-a)>40||(y-b)>40||(b-y)>40) return 0;elsereturn 1;break;case'I':if(((x-a)==40&&(y-b)==40)&&y>=310&&230<x<150||((a-x)==40&&(b-y)==40 )&&y>310&&230>x>150)return 1;elsebreak;case'J':if(((((x-a)==80&&(y-b)==80)&&y>=230)&&array(((y+b)/2/40+1,(x+a)/2/40+1) =='Z')))||(((a-x)==80&&(b-y)==80)&&y>=230)&&(array((y+b)/2/40+1,(x+a)/2/40 +1)=='Z'))))return 1;elsereturn 0;break;case'K':if((((x-a)==80&&(y-b)==40&&(array(y/40+1,(x-40)/40+1)=='Z'))||(((a-x)==80& &(b-y)==40)&&(array(y/40+1,(x+40)/40+1)=='Z'))||(((x-a)==40&&(y-b)==80)(arra y((y-40)/40+1,x/40+1)=='Z'))||(((a-x)==40&&(b-y)==80)&&(array((y+40)/40+1,x/4 0+1)=='Z'))return 1;elsereturn 0;break;case 'L':return 1;break;break;case 'N':if(y>230){if(y<b||x!=a){return 0;}elsereturn 1;}else{if(y-b>40||(a-x)>40||(x-a)>40||y<b){return 0;}elsereturn 1;}default:return 0;}}*/。
中国象棋C语言源代码
*--------------------chess.c----------------------*/ #include "dos.h"#include "stdio.h"/*----------------------------------------------------*/ #define RED 7#define BLACK 14#define true 1#define false 0#define SELECT 0#define MOVE 1#define RED_UP 0x1100#define RED_DOWN 0x1f00#define RED_LEFT 0x1e00#define RED_RIGHT 0x2000#define RED_DO 0x3900#define RED_UNDO 0x1000#define BLACK_UP 0x4800#define BLACK_DOWN 0x5000#define BLACK_LEFT 0x4b00#define BLACK_RIGHT 0x4d00#define BLACK_DO 0x1c00#define BLACK_UNDO 0x2b00#define ESCAPE 0x0100#define RED_JU 1#define RED_MA 2#define RED_XIANG 3#define RED_SHI 4#define RED_JIANG 5#define RED_PAO 6#define RED_BIN 7#define BLACK_JU 8#define BLACK_MA 9#define BLACK_XIANG 10#define BLACK_SHI 11#define BLACK_JIANG 12#define BLACK_PAO 13#define BLACK_BIN 14/*----------------------------------------------------*/ int firsttime=1;int savemode;char page_new=0,page_old=0;int finish=false,turn=BLACK,winner=0;int key;int redstate=SELECT,blackstate=SELECT;int board[10][9];/*----------------------------------------------------*/char *chessfile[15]={"","bmp\\rju.wfb", "bmp\\rma.wfb", "bmp\\rxiang.wfb","bmp\\rshi.wfb","bmp\\rjiang.wfb","bmp\\rpao.wfb","bmp\\rbin.wfb","bmp\\bju.wfb", "bmp\\bma.wfb", "bmp\\bxiang.wfb","bmp\\bshi.wfb","bmp\\bjiang.wfb","bmp\\bpao.wfb","bmp\\bbin.wfb"};char *boardfile[10][9]={{"bmp\\11.wfb","bmp\\1t.wfb","bmp\\1t.wfb","bmp\\14.wfb","bmp\\15.wfb","bmp\\16.wfb"," bmp\\1t.wfb","bmp\\1t.wfb","bmp\\19.wfb"},{"bmp\\21.wfb","bmp\\2c.wfb","bmp\\2c.wfb","bmp\\24.wfb","bmp\\25.wfb","bmp\\26.wfb"," bmp\\2c.wfb","bmp\\2c.wfb","bmp\\29.wfb"},{"bmp\\21.wfb","bmp\\3a.wfb","bmp\\3t.wfb","bmp\\34.wfb","bmp\\3t.wfb","bmp\\36.wfb"," bmp\\3t.wfb","bmp\\3a.wfb","bmp\\29.wfb"},{"bmp\\41.wfb","bmp\\4t.wfb","bmp\\4a.wfb","bmp\\4t.wfb","bmp\\4a.wfb","bmp\\4t.wfb","b mp\\4a.wfb","bmp\\4t.wfb","bmp\\49.wfb"},{"bmp\\51.wfb","bmp\\52.wfb","bmp\\5t.wfb","bmp\\54.wfb","bmp\\5t.wfb","bmp\\56.wfb"," bmp\\5t.wfb","bmp\\58.wfb","bmp\\59.wfb"},{"bmp\\61.wfb","bmp\\62.wfb","bmp\\6t.wfb","bmp\\64.wfb","bmp\\6t.wfb","bmp\\66.wfb"," bmp\\6t.wfb","bmp\\68.wfb","bmp\\69.wfb"},{"bmp\\71.wfb","bmp\\7t.wfb","bmp\\7a.wfb","bmp\\7t.wfb","bmp\\7a.wfb","bmp\\7t.wfb","b mp\\7a.wfb","bmp\\7t.wfb","bmp\\79.wfb"},{"bmp\\81.wfb","bmp\\8a.wfb","bmp\\8t.wfb","bmp\\84.wfb","bmp\\85.wfb","bmp\\86.wfb"," bmp\\8t.wfb","bmp\\8a.wfb","bmp\\89.wfb"},{"bmp\\91.wfb","bmp\\9t.wfb","bmp\\9t.wfb","bmp\\9t.wfb","bmp\\95.wfb","bmp\\9t.wfb","b mp\\9t.wfb","bmp\\9t.wfb","bmp\\99.wfb"},{"bmp\\101.wfb","bmp\\102.wfb","bmp\\102.wfb","bmp\\104.wfb","bmp\\105.wfb","bmp\\10 6.wfb","bmp\\108.wfb","bmp\\108.wfb","bmp\\109.wfb"}};char cursor[14][14]={0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,255,255,255,255,255,255,255,0,0,1,1,1,1,0,255,255,255,255,255,255,0,0,1,1,1,1,1,0,255,255,255,255,255,255,0,0,1,1,1,1,1,0,255,255,255,255,255,255,255,0,0,1,1,1,1,0,255,255,255,255,255,255,255,255,0,0,1,1,1,0,255,255,255,255,255,255,255,255,255,0,0,1,1,0,255,255,0,255,255,255,255,255,255,255,0,0,1,0,255,0,1,1,0,255,255,255,255,255,255,255,0,0,0,1,1,1,1,0,255,255,255,255,255,0,1,0,1,1,1,1,1,1,0,255,255,255,0,1,1,1,1,1,1,1,1,1,1,0,255,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1};struct pos{int x;int y;}position[10][9],redcurpos,redtemppos,redoldpos,blackcurpos,blacktemppos,blackoldpos; /*----------------------------------------------------*/selectpage(register char page) /*换页函数*/{union REGS r;r.x.ax=0x4f05;r.x.bx=0;r.x.dx=page; /*选择页面*/int86(0x10,&r,&r);}unsigned char set_SVGA_mode(int vmode) /*设置SVGA屏幕模式*/{union REGS r;r.x.ax=0x4f02;r.x.bx=vmode;int86(0x10,&r,&r);return(r.h.ah);}unsigned int get_SVGA_mode() /*获取当前SVGA屏幕模式*/{union REGS r;r.x.ax=0x4f03;int86(0x10,&r,&r);return(r.x.bx);}drawbmp(int start_x,int start_y,char filename[]){char buffer[640];int i,j,k,n,r,g,b,width,length;long position;FILE *fp;if((fp=fopen(filename,"rb"))==NULL){printf("Error! Can't open file!");getch();return;}fseek(fp,28,SEEK_SET);fread(&i,2,1,fp);if(i!=8) /*检查是否为256色位图*/{puts("Error!Can't find bitmap!");fclose(fp);getch();exit(0);}fseek(fp,18,SEEK_SET);fread(&width,4,1,fp);fread(&length,4,1,fp);if(firsttime){fseek(fp,54,SEEK_SET);for(i=0;i<256;i++) /*按照该图片的DAC色表设置色彩寄存器*/{b=fgetc(fp);g=fgetc(fp);r=fgetc(fp); /*获取R、G、B分量*/outportb(0x3c8,i);outportb(0x3c9,r>>2); /*右移是要转化为VGA的6位寄存器形式*/ outportb(0x3c9,g>>2);outportb(0x3c9,b>>2);fgetc(fp);}}elsefseek(fp,300,SEEK_SET);k=(width%4)?(4-width%4):0; /*宽度修正值*/for(j=length-1+start_x;j>=start_x;j--){fread(buffer,width,1,fp);for(i=start_y,n=0;i<width+start_y;i++,n++){position=j*640l+i; /*计算要显示点的显存位置*/page_new=position/65536; /*计算显示页*/if(page_new!=page_old) /*当显示页不同时更换页面,提高一定的输出速度*/{selectpage(page_new);page_old=page_new;}pokeb(0xa000,position%65536,buffer[n]); /*写到显存位置*/}fseek(fp,k,SEEK_CUR); /*每行绘制完后修正宽度*/}fclose(fp);}init(){savemode=get_SVGA_mode(); /*先保存原来的屏幕模式*/set_SVGA_mode(0x101); /*硬件无关性初始化屏幕为640*480 256色模式*/ }end(){set_SVGA_mode(savemode); /*恢复屏幕*/}/*----------------------------------------------------*/initpos(){int i,j;for(i=0;i<10;i++)for (j=0;j<9;j++){position[i][j].x=35+i*39;position[i][j].y=43+j*40;}}initchessmap(){board[0][0]=BLACK_JU;board[0][1]=BLACK_MA;board[0][2]=BLACK_XIANG;board[0][3]=BLACK_SHI;board[0][4]=BLACK_JIANG;board[0][5]=BLACK_SHI;board[0][6]=BLACK_XIANG;board[0][7]=BLACK_MA;board[0][8]=BLACK_JU;board[2][1]=BLACK_PAO;board[2][7]=BLACK_PAO;board[3][0]=BLACK_BIN;board[3][2]=BLACK_BIN;board[3][4]=BLACK_BIN;board[3][6]=BLACK_BIN;board[3][8]=BLACK_BIN;board[9][0]=RED_JU;board[9][1]=RED_MA;board[9][2]=RED_XIANG;board[9][3]=RED_SHI;board[9][4]=RED_JIANG;board[9][5]=RED_SHI;board[9][6]=RED_XIANG;board[9][7]=RED_MA;board[9][8]=RED_JU;board[7][1]=RED_PAO;board[7][7]=RED_PAO;board[6][0]=RED_BIN;board[6][2]=RED_BIN;board[6][4]=RED_BIN;board[6][6]=RED_BIN;board[6][8]=RED_BIN;}initdrawchess(){int i,j;;for(i=0;i<10;i++)for(j=0;j<9;j++){if(board[i][j])drawbmp(position[i][j].x,position[i][j].y,chessfile[board[i][j]]); }}drawcursor(struct pos p){int i,j,n,m,x,y;long thisposition;x=position[p.x][p.y].x+20;y=position[p.x][p.y].y+25;for(j=13-1+x,m=13;j>=x;j--,m--){for(i=y,n=0;i<13+y;i++,n++){thisposition=j*640l+i; /*计算要显示点的显存位置*/page_new=thisposition/65536; /*计算显示页*/if(page_new!=page_old) /*当显示页不同时更换页面,提高一定的输出速度*/ {selectpage(page_new);page_old=page_new;}if(cursor[m][n]!=1)if(cursor[m][n]==0)pokeb(0xa000,thisposition%65536,0);elseif(turn==RED)pokeb(0xa000,thisposition%65536,153);elsepokeb(0xa000,thisposition%65536,255);}}}drawselecursor(struct pos p){int i,j,n,m,x,y;long thisposition;x=position[p.x][p.y].x+20;y=position[p.x][p.y].y+25;for(j=13-1+x,m=13;j>=x;j--,m--){for(i=y,n=0;i<13+y;i++,n++){thisposition=j*640l+i; /*计算要显示点的显存位置*/page_new=thisposition/65536; /*计算显示页*/if(page_new!=page_old) /*当显示页不同时更换页面,提高一定的输出速度*/ {selectpage(page_new);page_old=page_new;}if(cursor[m][n]!=1)pokeb(0xa000,thisposition%65536,0);}}}/*----------------------------------------------------*/int getkey(){int press;while(bioskey(1) == 0);press=bioskey(0);press=press&0xff00;return(press);}/*--------------------红方操作--------------------*/int redcanselect(){int x,y;x=redcurpos.x;y=redcurpos.y;if(board[x][y]>=RED_JU&&board[x][y]<=RED_BIN)return 1;elsereturn 0;}int redcanmove(){int i,j,min,max,oldx,oldy,x,y;oldx=redoldpos.x;oldy=redoldpos.y;x=redcurpos.x;y=redcurpos.y;/*case1 目标位置是否是自己人*/if(board[x][y]>=RED_JU&&board[x][y]<=RED_BIN)return 0;/* 军、马、炮、相、士、将、卒的走法正确性的判断*/ switch(board[oldx][oldy]){case RED_BIN: /*完成*/if(oldx>=5){ if(y!=oldy||(oldx-x)!=1) return 0;}else{ if(x==(oldx-1)&&y==oldy) return 1;elseif(x==oldx&&y==(oldy+1)) return 1;elseif(x==oldx&&y==(oldy-1)) return 1;elsereturn 0;}break;case RED_JIANG: /*完成*/if(x!=oldx&&y!=oldy) return 0;if(x!=oldx)if((x-oldx)>1||(oldx-x)>1) return 0;else if(x<7) return 0;else if(y!=oldy)if((y-oldy)>1||(oldy-y)>1) return 0;else if(y<3||y>5) return 0;break;case RED_JU: /*完成*/if(x!=oldx&&y!=oldy) return 0;else if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0) return 0;}break;case RED_MA: /*完成*/if((x-oldx)==2&&((y-oldy)==1||(oldy-y)==1)) {if(board[oldx+1][oldy]!=0) return 0;}elseif((oldx-x)==2&&((y-oldy)==1||(oldy-y)==1)) {if(board[oldx-1][oldy]!=0) return 0;}elseif((y-oldy)==2&&((x-oldx)==1||(oldx-x)==1)) {if(board[oldx][oldy+1]!=0) return 0;}elseif((oldy-y)==2&&((x-oldx)==1||(oldx-x)==1)) {if(board[oldx][oldy-1]!=0) return 0;}elsereturn 0;break;case RED_PAO: /*完成*/if(x!=oldx&&y!=oldy) return 0;if(board[x][y]==0){if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0) return 0; }else if(y!=oldy){ min=(y>oldy)?oldy:y; max=(y>oldy)?y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0) return 0; }}else{if(x!=oldx){ min=(x>oldx)?oldx:x; max=(x>oldx)?x:oldx;for(i=min+1,j=0;i<max;i++) if(board[i][y]!=0) j++;if(j!=1) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y; max=(y>oldy)?y:oldy;for(i=min+1,j=0;i<max;i++) if(board[x][i]!=0) j++;if(j!=1) return 0;}}break;case RED_SHI: /*完成*/if(oldx==9||oldx==7){if(x!=8||y!=4) return 0;} else if(oldx==8){if(x==9&&y==3) return 1; elseif(x==9&&y==5) return 1; elseif(x==7&&y==3) return 1; elseif(x==7&&y==5) return 1; else return 0;}else return 0;break;case RED_XIANG: /*完成*/ if(x<5) return 0;if(x!=oldx&&y!=oldy){if((x-oldx)==2&&(y-oldy)==2){i=oldx+1;j=oldy+1;}else if((x-oldx)==2&&(oldy-y)==2) {i=oldx+1;j=oldy-1;}else if((oldx-x)==2&&(y-oldy)==2) {i=oldx-1;j=oldy+1;}else if((oldx-x)==2&&(oldy-y)==2) {i=oldx-1;j=oldy-1;}else return 0;if(board[i][j]!=0) return 0;}else return 0;break;}return 1;}。
象棋引擎原理
象棋引擎原理详解引言象棋引擎是一种能够独立思考并下棋的计算机程序。
它通过搜索和评估棋局来选择最优的走法,并且可以与人类玩家进行对弈。
本文将详细介绍象棋引擎的基本原理,包括搜索算法、评估函数、剪枝技术以及其他一些优化策略。
搜索算法搜索算法是象棋引擎中最核心的部分,它通过遍历可能的走法来找到最佳的下一步。
常用的搜索算法有极小化极大(Minimax)和Alpha-Beta剪枝。
极小化极大算法在极小化极大算法中,引擎会递归地遍历所有可能的走法,并为每个走法分配一个分数。
对于电脑来说,它会选择能够使自己得分最高的走法;对于人类玩家来说,它会选择能够使电脑得分最低的走法。
具体实现时,可以使用深度优先搜索(DFS)或广度优先搜索(BFS)来遍历所有可能的走法。
DFS更常用,因为它可以通过设定搜索深度来控制计算时间。
Alpha-Beta剪枝Alpha-Beta剪枝是一种优化搜索算法的技术。
它通过排除一些不必要的搜索分支来减少搜索空间,从而提高搜索效率。
在Alpha-Beta剪枝中,引擎会维护两个值:alpha和beta。
alpha表示当前最好的走法对电脑来说的最佳分数,beta表示当前最好的走法对人类玩家来说的最佳分数。
在搜索过程中,如果某个节点的评估结果比alpha更好(即更高),则更新alpha;如果某个节点的评估结果比beta更差(即更低),则剪去该节点及其子节点。
这样可以排除一些不可能产生最优解的分支,从而加速搜索过程。
评估函数评估函数是象棋引擎用于评估当前棋局优劣的重要组成部分。
它会根据棋局特征给出一个分数,用于指导搜索算法选择下一步。
评估函数可以基于各种指标进行设计,常见的包括棋子价值、位置价值、攻击威胁等。
具体实现时,可以为每个棋子分配一个固定的价值,并为每个位置分配一个权重。
然后根据棋子和位置的组合情况来计算总分。
评估函数的设计需要考虑多个因素,如棋子价值的权重、位置价值的权重以及不同棋子之间的相互影响等。
象棋游戏代码
#include<stdio.h>#include<math.h>int x[11][12];void main(){int i,j;int x1,y1,x2,y2,rg,gg;int rgo(int,int,int,int);int ggo(int,int,int,int);/*初始化棋子(开局)*/for(i=1;i<=5;i++){ x[i][1]=x[10-i][1]=i+10;x[i][10]=x[10-i][10]=i+20;if(i%2==1){ x[i][4]=x[10-i][4]=17;x[i][7]=x[10-i][7]=27;}}x[2][3]=x[8][3]=16;x[2][8]=x[8][8]=26;for(i=0;i<=9;i++)x[i][0]=i;for(i=1;i<=10;i++)x[0][i]=i;printf("S27\n=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=\n"); for(j=10;j>=0;j--){ for(i=0;i<=9;i++){if(i==0){printf("%2d |",x[i][j]);continue;}if(j==0){printf(" 0%d ",x[i][j]);continue;}switch(x[i][j]){case 0 : printf(" ");break;case 11 : printf(" 车");break;case 12 : printf(" 马");break;case 13 : printf(" 相");break;case 14 : printf(" 士");break;case 15 : printf(" 帅");break;case 16 : printf(" 炮");break;case 17 : printf(" 兵");break;case 21 : printf(" JU ");break;case 22 : printf(" MA ");break;case 23 : printf(" XN ");break;case 24 : printf(" SH ");break;case 25 : printf(" JN ");break;case 26 : printf(" PO ");break;case 27 : printf(" ZU ");break;}}if(j==1)printf("|\n---+-------------------------------------\n");else if(j==0)printf("\n");elseprintf("|\n | |\n");}/*走子*/for(;;){/*红子走子*/for(rg=0;rg==0;){for(x1=0,y1=0,x2=0,y2=0;x[x1][y1]==0||x[x1][y1]>=20||x2<1||y2<1||x2>9||y2>10||x[x2][y2] <20&&x[x2][y2]>10||x[x2][y2]==x[x1][y1];){printf("请输入红子坐标(x1,y1,x2,y2):");scanf("%d,%d,%d,%d",&x1,&y1,&x2,&y2);}if(x[x1][y1]>20){continue;}rg=rgo(x1,y1,x2,y2);if(rg==0)continue;else{if(x[x2][y2]==25){x[x2][y2]=x[x1][y1],x[x1][y1]=0;printf("lu qi shu le\n");}elsex[x2][y2]=x[x1][y1],x[x1][y1]=0;}}printf("S27 制作\n=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=\n"); for(j=10;j>=0;j--){for(i=0;i<=9;i++){if(i==0){printf("%2d |",x[i][j]);continue;}if(j==0){printf(" 0%d ",x[i][j]);continue;}switch(x[i][j]){case 0 : printf(" ");break;case 11 : printf(" 车");break;case 12 : printf(" 马");break;case 13 : printf(" 相");break;case 14 : printf(" 士");break;case 15 : printf(" 帅");break;case 16 : printf(" 炮");break;case 17 : printf(" 兵");break;case 21 : printf(" JU ");break;case 22 : printf(" MA ");break;case 23 : printf(" XN ");break;case 24 : printf(" SH ");break;case 25 : printf(" JN ");break;case 26 : printf(" PO ");break;case 27 : printf(" ZU ");break;}}if(j==1)printf("|\n---+-------------------------------------\n");else if(j==0)printf("\n");printf("|\n | |\n");}/*绿子走子*/for(gg=0;gg==0;){for(x1=0,y1=0,x2=0,y2=0;x[x1][y1]<10||x2<1||y2<1||x2>9||y2>10||x[x2][y2]>20||x[x2][y2]= =x[x1][y1];){printf("请输入绿子坐标(x1,y1,x2,y2):");scanf("%d,%d,%d,%d",&x1,&y1,&x2,&y2);}if(x[x1][y1]<20){continue;}gg=ggo(x1,y1,x2,y2);if(gg==0)continue;else{if(x[x2][y2]==25){x[x2][y2]=x[x1][y1],x[x1][y1]=0;printf("hong qi shu le\n");}elsex[x2][y2]=x[x1][y1],x[x1][y1]=0;}}printf("S27 制作\n=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=\n");for(j=10;j>=0;j--){for(i=0;i<=9;i++){if(i==0){printf("%2d |",x[i][j]);continue;}if(j==0){printf(" 0%d ",x[i][j]);continue;switch(x[i][j]){case 0 : printf(" ");break;case 11 : printf(" 车");break;case 12 : printf(" 马");break;case 13 : printf(" 相");break;case 14 : printf(" 士");break;case 15 : printf(" 帅");break;case 16 : printf(" 炮");break;case 17 : printf(" 兵");break;case 21 : printf(" JU ");break;case 22 : printf(" MA ");break;case 23 : printf(" XN ");break;case 24 : printf(" SH ");break;case 25 : printf(" JN ");break;case 26 : printf(" PO ");break;case 27 : printf(" ZU ");break;}}if(j==1)printf("|\n---+-------------------------------------\n");else if(j==0)printf("\n");elseprintf("|\n | |\n"); }}}/*判定红棋是否有错*/int rgo(int xa,int ya,int xb,int yb){ if(x[xa][ya]==11) /*车*/{int t;if(xb-xa==0){if(yb<ya)t=yb,yb=ya,ya=t;for(t=1+ya;t<yb;t++)if(x[xa][t]!=0&&x[xa][t]<20)return 0;}else if(yb-ya==0){ if(xb<xa)t=xb,xb=xa,xa=t;for(t=1+xa;t<xb;t++)if(x[t][ya]!=0&&x[xa][t]<20)return 0;}else return 0;return 1;}if(x[xa][ya]==12) /*马*/ { if(fabs(yb-ya)==2&&fabs(xb-xa)==1) {if(yb-ya>0&&x[xa][ya+1]!=0)return 0;if(yb-ya<0&&x[xa][ya-1]!=0)return 0;if(x[xb][yb]!=0&&x[xb][yb]<20)return 0;}else if(fabs(xb-xa)==2&&fabs(yb-ya)==1) {if(xb-xa>0&&x[xa+1][ya]!=0)return 0;if(xb-xa<0&&x[xa-1][ya]!=0)return 0;if(x[xb][yb]!=0&&x[xb][yb]<20)return 0;}else return 0;return 1;}if(x[xa][ya]==13) /*相*/ {if(yb>5)return 0;if(fabs(yb-ya)==2&&fabs(xb-xa)==2) {if(yb-ya>0){if(xb-xa>0&&x[xa+1][ya+1]!=0)return 0;else if(xb-xa<0&&x[xa-1][ya+1]!=0)return 0;}else{if(xb-xa>0&&x[xa+1][ya-1]!=0)return 0;else if(xb-xa<0&&x[xa-1][ya-1]!=0)return 0;}if(x[xb][yb]!=0&&x[xb][yb]<20)return 0;}else return 0;return 1;}if(x[xa][ya]==14) /*士*/{if(xb<4||xb>6||yb>3)return 0;if(x[xb][yb]!=0&&x[xb][yb]<20)return 0;if(fabs(yb-ya)==fabs(xb-xa)==1)return 1;else return 0;}if(x[xa][ya]==15) /*帅*/{if(xb<4||xb>6||yb>3)return 0;if((fabs(yb-ya)==1&&xb-xa==0)||(fabs(xb-xa)==1&&yb-ya==0)) return 1;else return 0;}if(x[xa][ya]==16) /*炮*/{int t,k;if(yb-ya==0){if(xb<xa)t=xb,xb=xa,xa=t;for(t=0,k=1;xb>xa+k;k++)if(x[xa+k][ya]!=0)t++;if(t>1)return 0;else if(t==0){if(x[xb][yb]==0||x[xa][ya]==0)return 1;else return 0;}else if(t==1){if(x[xb][yb]>20)return 1;else return 0;}}else if(xb-xa==0){if(yb<ya)t=yb,yb=ya,ya=t;for(t=0,k=1;yb>ya+k;k++)if(x[xa][ya+k]!=0)t++;if(t>1)return 0;else if(t==0){if(x[xb][yb]==0||x[xa][ya]==0)return 1;else return 0;}else if(t==1){if(x[xb][yb]>20)return 1;else return 0;}}else return 0;}if(x[xa][ya]==17) /*兵*/{if(yb==ya&&fabs(xb-xa)==1||yb-ya==1&&xb==xa) {if((ya==4||ya==5)&&xb!=xa)return 0;if(x[xb][yb]!=0&&x[xb][yb]<20)return 0;}else return 0;}}/*判定绿棋是否有错*/int ggo(int xa,int ya,int xb,int yb){if(x[xa][ya]==21) /*车*/ {int t;if(xb-xa==0){if(yb<ya)t=yb,yb=ya,ya=t;for(t=1+ya;t<yb;t++)if(x[xa][t]!=0&&x[xa][t]>20)return 0;}else if(yb-ya==0){if(xb<xa)t=xb,xb=xa,xa=t;for(t=1+xa;t<xb;t++)if(x[t][ya]!=0&&x[t][ya]>20)return 0;}else return 0;return 1;}if(x[xa][ya]==22) /*马*/ {if(fabs(yb-ya)==2&&fabs(xb-xa)==1) {if(yb-ya>0&&x[xa][ya+1]!=0)return 0;if(yb-ya<0&&x[xa][ya-1]!=0)return 0;if(x[xb][yb]!=0&&x[xb][yb]>20)return 0;}else if(fabs(xb-xa)==2&&fabs(yb-ya)==1) {if(xb-xa>0&&x[xa+1][ya]!=0)return 0;if(xb-xa<0&&x[xa-1][ya]!=0)if(x[xb][yb]!=0&&x[xb][yb]>20)return 0;}else return 0;return 1;}if(x[xa][ya]==23) /*相*/ {if(yb<6)return 0;if(fabs(yb-ya)==2&&fabs(xb-xa)==2) {if(yb-ya>0){if(xb-xa>0&&x[xa+1][ya+1]!=0)return 0;else if(xb-xa<0&&x[xa-1][ya+1]!=0)return 0;}else{if(xb-xa>0&&x[xa+1][ya-1]!=0)return 0;else if(xb-xa<0&&x[xa-1][ya-1]!=0)return 0;}if(x[xb][yb]!=0&&x[xb][yb]>20)return 0;}else return 0;return 1;}if(x[xa][ya]==24) /*士*/ {if(xb<4||xb>6||yb<8)return 0;if(x[xb][yb]!=0&&x[xb][yb]>20)return 0;if(fabs(yb-ya)==fabs(xb-xa)==1) return 1;else return 0;}if(x[xa][ya]==25) /*帅*/ {return 0;if((fabs(yb-ya)==1&&xb-xa==0)||(fabs(xb-xa)==1&&yb-ya==0)) return 1;else return 0;}if(x[xa][ya]==26) /*炮*/{ int t,k;if(yb-ya==0){ if(xb<xa)t=xb,xb=xa,xa=t;for(t=0,k=1;xb>xa+k;k++)if(x[xa+k][ya]!=0)t++;if(t>1)return 0;else if(t==0){ if(x[xb][yb]==0||x[xa][ya]==0)return 1;else return 0;}else if(t==1){ if(x[xb][yb]<20)return 1;else return 0;}}else if(xb-xa==0){if(yb<ya)t=yb,yb=ya,ya=t;for(t=0,k=1;yb>ya+k;k++)if(x[xa][ya+k]!=0)t++;if(t>1)return 0;else if(t==0){if(x[xb][yb]==0||x[xa][ya]==0)return 1;else return 0;}else if(t==1){return 1;else return 0;}}else return 0;}if(x[xa][ya]==27) /*兵*/{if(yb==ya&&fabs(xb-xa)==1||ya-yb==1&&xb==xa) {if((ya==6||ya==7)&&xb!=xa)return 0;if(x[xb][yb]!=0&&x[xb][yb]>20)return 0;}else return 0;return 1;}}。
中国象棋博弈算法研究(XFS算法)毕业论文
毕业论文声明本人郑重声明:1.此毕业论文是本人在指导教师指导下独立进行研究取得的成果。
除了特别加以标注地方外,本文不包含他人或其它机构已经发表或撰写过的研究成果。
对本文研究做出重要贡献的个人与集体均已在文中作了明确标明。
本人完全意识到本声明的法律结果由本人承担。
2.本人完全了解学校、学院有关保留、使用学位论文的规定,同意学校与学院保留并向国家有关部门或机构送交此论文的复印件和电子版,允许此文被查阅和借阅。
本人授权大学学院可以将此文的全部或部分内容编入有关数据库进行检索,可以采用影印、缩印或扫描等复制手段保存和汇编本文。
3.若在大学学院毕业论文审查小组复审中,发现本文有抄袭,一切后果均由本人承担,与毕业论文指导老师无关。
4.本人所呈交的毕业论文,是在指导老师的指导下独立进行研究所取得的成果。
论文中凡引用他人已经发布或未发表的成果、数据、观点等,均已明确注明出处。
论文中已经注明引用的内容外,不包含任何其他个人或集体已经发表或撰写过的研究成果。
对本文的研究成果做出重要贡献的个人和集体,均已在论文中已明确的方式标明。
学位论文作者(签名):年月关于毕业论文使用授权的声明本人在指导老师的指导下所完成的论文及相关的资料(包括图纸、实验记录、原始数据、实物照片、图片、录音带、设计手稿等),知识产权归属华北电力大学。
本人完全了解大学有关保存,使用毕业论文的规定。
同意学校保存或向国家有关部门或机构送交论文的纸质版或电子版,允许论文被查阅或借阅。
本人授权大学可以将本毕业论文的全部或部分内容编入有关数据库进行检索,可以采用任何复制手段保存或编汇本毕业论文。
如果发表相关成果,一定征得指导教师同意,且第一署名单位为大学。
本人毕业后使用毕业论文或与该论文直接相关的学术论文或成果时,第一署名单位仍然为大学。
本人完全了解大学关于收集、保存、使用学位论文的规定,同意如下各项内容:按照学校要求提交学位论文的印刷本和电子版本;学校有权保存学位论文的印刷本和电子版,并采用影印、缩印、扫描、数字化或其它手段保存或汇编本学位论文;学校有权提供目录检索以及提供本学位论文全文或者部分的阅览服务;学校有权按有关规定向国家有关部门或者机构送交论文的复印件和电子版,允许论文被查阅和借阅。
中国象棋C代码
中国象棋#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>#include<windows.h>int x, y, i, j, k, p, q, num = 1, round; //象棋游戏的全局变量int px1 = 0, py1 = 0, px2 = 0, py2 = 0;int ck_x, ck_y, ck_t; //基本参数char ch, tn = 'O', tn1 = 'N', tp, tp1;char ck_1[9][3] ={"車","馬","相","仕","帥","砲","兵","+-"}; //取棋子时只判断前8合法char ck_2[9][3] ={"车","马","象","士","将","炮","卒","+-"};//下棋子时判断多一个空位合法char check[3];void ckm1(char* tp,char* tp1,char* tn,char* tn1,int *num,int *if_ov,char map[100][100]) {//象棋函数判断将方下棋是否合法check[0] = *tp; check[1] = *tp1; check[2] = '\0';char a,b;for ( i = 0; i < 8; i++){ if ( strcmp(ck_2[i],check) == 0){ *tp = *tn; *tp1 = *tn1; *tn = 'O'; *tn1 = 'N';if( i < 7){ printf(" 将方的%s被吃",ck_2[i]); Sleep(500); }*num = *num + 1;for( k = 4; k <= 8; k = k + 2)//判断将是否死亡{for(j = 15; j <= 23; j= j+ 4){ if (map[k][j] == ck_2[4][0] && map[k][j+1] == ck_2[4][1]){ px2 = k; py2 = j; break; }}if( j <= 23) break;}if( k == 10){printf(" 将被将死帥方获得胜利\n"); printf("按任意键返回菜单");getch( ); *if_ov = 1; return;}for( k = 18; k <= 22; k = k + 2) //判断帥是否死亡{for(j = 15; j <= 23; j= j+ 4){if(map[k][j] == ck_1[4][0] && map[k][j+1] == ck_1[4][1]){px1 = k; py1 = j; break; }}if( j <= 23) break;}if ( k == 24){printf(" 帥被将死将方获得胜利\n"); printf("按任意键返回菜单");getch( ); *if_ov = 1; return;}if ( py1 == py2){for( k = px2 + 2; k <= px1 - 2; k = k +2) {if(map[k][py1] != '+') break;}if( k == px1){if(round == 1) printf(" 帥方对将将方胜利");else if( round == 2) printf(" 将方对将帥方胜利");printf("按任意键返回菜单"); getch( ); *if_ov = 1; return;}}break;}} // for ( i = 0; i < 8; i++)循环结束if( i == 8) {printf("不合法的走法\n"); Sleep(500); }}void ckm2(char* tp,char* tp1,char* tn,char* tn1,int *num,int *if_ov,char map[100][100]) {//象棋函数判断帥方下棋是否合法check[0] = *tp; check[1] = *tp1; check[2] = '\0';char a,b;for ( i = 0; i < 8; i++){if ( strcmp(ck_1[i],check) == 0){ *tp = *tn; *tp1 = *tn1; *tn = 'O'; *tn1 = 'N';if( i < 7) {printf(" 帥方的%s被吃",ck_1[i]); Sleep(500); }*num = *num + 1;for( k = 4; k <= 8; k = k + 2) //判断将是否死亡{for(j = 15; j <= 23; j= j+ 4){if(map[k][j] == ck_2[4][0] && map[k][j+1] == ck_2[4][1]){px2 = k; py2 = j; break; }}if( j <= 23) break;}if( k == 10){printf(" 将被将死帥方获得胜利\n");printf("按任意键返回菜单"); getch( );*if_ov = 1; return;}for( k = 18; k <= 22; k = k + 2) //判断帥是否死亡{for(j = 15; j <= 23; j= j+ 4){if(map[k][j] == ck_1[4][0] && map[k][j+1] == ck_1[4][1]){px1 = k; py1 = j; break; }}if( j <= 23) break;}if( k == 24){printf(" 帥被将死将方获得胜利\n");printf("按任意键返回菜单"); getch( );*if_ov = 1; return;}if( py1 == py2){for( k = px2 + 2; k <= px1 - 2; k = k +2) {if(map[k][py1] != '+') break; }if( k == px1){if(round == 1) printf(" 帥方对将将方胜利");else if( round == 2) printf(" 将方对将帥方胜利");printf("按任意键返回菜单"); getch( ); *if_ov = 1; return;}}break;}} // for ( i = 0; i < 8; i++)循环结束if( i == 8) {printf("不合法的走法\n"); Sleep(500); }}void xiangqi( )//象棋主程序{char map[100][100]= { "[[===================================]]","[| ①帥【象棋】②将|]","[[===================================]]","[[-----------------------------------]]","[[ 车—-马—-象—-士—-将—-士—-象—-马—-车]]","[[ | | | | \\ | / | | | | ]]","[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]","[[ | | | | / | \\ | | | | ]]","[[ +-—-炮—-+-—-+-—-+-—-+-—-+-—-炮—-+-]]","[[ | | | | | | | | | ]]","[[ 卒—-+-—-卒—-+-—-卒—-+-—-卒—-+-—-卒]]","[[ | | | | | | | | | ]]","[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]","[[===================================]]","[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]","[[ | | | | | | | | | ]]","[[ 兵—-+-—-兵—-+-—-兵—-+-—-兵—-+-—-兵]]","[[ | | | | | | | | | ]]","[[ +-—-砲—-+-—-+-—-+-—-+-—-+-—-砲—-+-]]","[[ | | | | \\ | / | | | | ]]","[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]","[[ | | | | / | \\ | | | | ]]","[[ 車—-馬—-相—-仕—-帥—-仕—-相—-馬—-車]]","[[-----------------------------------]]","[[===================================]]"};int if_ov = 0;system("mode con cols=42 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=42 lines=32"); //迷你界面system("color 70");for ( i = 0; i < 27; i++){ puts(map[i]); Sleep(100); }x = 6, y = 19; tp = map[x][y]; tp1 = 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] =tp; map[x][y+1] = tp1;x = x + 2; tp = map[x][y]; tp1 = map[x][y+1];map[x][y] = tn; map[x][y+1] = tn1;}}else if ( ch == 'a') //左移{ if (map[x][y-1]!=' '){map[x][y] =tp; map[x][y+1] = tp1;y = y - 4; tp = map[x][y]; tp1 = map[x][y+1];map[x][y] = tn; map[x][y+1] = tn1;}}else if ( ch == 'w') //上移{ if ( map[x-1][y]!= '-'){map[x][y] =tp; map[x][y+1] = tp1;x = x - 2; tp = map[x][y]; tp1 = map[x][y+1];map[x][y] = tn; map[x][y+1] = tn1;}}else if ( ch == 'd') //右移{ if (map[x][y+2]!=']'){map[x][y] =tp; map[x][y+1] = tp1;y = y + 4; tp = map[x][y]; tp1 = map[x][y+1];map[x][y] = tn; map[x][y+1] = tn1;}}else if( ch == 'm' || ch =='M')//M确认要移动的棋子,或确认要移到的目的地{ if (num % 2 == 1 && tp != '+' && tp1 != '-') //取子{check[0] = tp; check[1] = tp1; check[2] = '\0';if ( round == 1){ for ( i = 0; i < 7; i++) //将方{ if ( strcmp(ck_1[i],check) == 0){tn = tp; tn1 = tp1; tp = '+'; tp1 = '-';ck_x = x; ck_y = y; ck_t = 10 + i;num++; break;}}if( i == 7){ printf("这不是你的棋子\n"); Sleep(500); }}else if( round == 2){for ( i = 0; i < 7; i++) //帅方{ if( strcmp(ck_2[i],check) == 0){tn = tp; tn1 = tp1; tp = '+'; tp1 = '-';ck_x = x; ck_y = y; ck_t = 20 + i;num++; break;}}if( i == 7){ printf("这不是你的棋子\n"); Sleep(500); }}}else if( num % 2 == 0) //放子{ char ck_1[8][3] ={"车","马","象","士","将","炮","卒","+-"};char ck_2[8][3] ={"俥","馬","相","仕","帥","軳","兵","+-"};//中界楚河上下坐标12 15 往下2 往右4if ( ck_t < 20){if( ck_t == 10) //车的走法规范(将方){ if((x == ck_x && y == ck_y)){tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( y == ck_y ){ if( x > ck_x){ for(j = ck_x + 2; j < x;j = j + 2){ if(map[j][y] == '+'); else{printf("不合法的下法\n"); Sleep(500); break; }}if( j >= x) ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}if( x < ck_x){ for(j = ck_x - 2; j > x;j = j - 2){ if(map[j][y] == '+'); else{printf("不合法的下法\n"); Sleep(500); break; }}if( j <= x) ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}}else if( x == ck_x ){if( y > ck_y){for(j = ck_y + 4; j < y;j = j + 4){if(map[x][j] == '+'); else {printf("不合法的下法\n"); Sleep(500); break; }}if( j >= y) ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}if( y < ck_y){for(j = ck_y - 4; j > y;j = j - 4){ if(map[x][j] == '+'); else { printf("不合法的下法\n"); Sleep(500); break; }}if( j <= y) ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}}else { printf("不合法的下法\n"); Sleep(500); }}if( ck_t == 11) //马的走法规范{if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( (abs( x - ck_x) == 2&& abs( y - ck_y) == 8)&& map[ck_x][(y+ck_y)/2] =='+') {ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else if( (abs( x - ck_x) == 4&& abs( y - ck_y) == 4)&& map[(x + ck_x)/2][ck_y] == '+' ) {ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else { printf("不合法的下法\n");Sleep(500); }}if( ck_t == 12) //相的走法规范{ if((x == ck_x && y == ck_y)){tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( x >= 15 &&(abs(y - ck_y) == 8 && abs(x - ck_x) == 4)){if((x == 22 && (y == 11 || y == 27))||(x == 18 &&( y == 3 || y == 19 || y == 35)) ||(x == 14 && (y == 11|| y ==27))){ if( map[(x+ck_x)/2][(y+ck_y)/2] == '+')ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else {printf("棋子卡住,不可执行"); Sleep(500); }}else {printf("不合法的下法\n");Sleep(500); }}else {printf("不合法的下法\n"); Sleep(500); }}if( ck_t == 13) //士的走法规范{ if((x == ck_x && y == ck_y)){tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( abs(x - ck_x)== 2 && abs( y - ck_y) == 4 &&((x==22 && (y == 15 || y == 23)) || ( x == 20 && y == 19) || ( x == 18 && ( y == 15 || y == 23)))){ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else { printf("不合法的下法\n"); Sleep(500); }}if( ck_t == 14) //将的走法规范{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( ((abs(x - ck_x)== 2 && abs( y - ck_y) == 0 )|| (abs(x - ck_x)== 0 && abs( y - ck_y) == 4)) && x >= 18 && x <= 22 && y >= 15 && y <= 23 ){ ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else { printf("不合法的下法\n"); Sleep(500); }}if( ck_t == 15) //炮的走法规范{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( y == ck_y ){ int check_pao = 0;if( x > ck_x){ for(j = ck_x + 2; j<= x ;j = j+ 2){ if(map[j][y] == '+' ); else check_pao++;}if(check_pao == 1&& tp == '+') // 直线行走但不可吃棋子ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2 && tp != '+') //跳跃吃棋ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n"); Sleep(500); }}else { for(j = ck_x - 2; j>= x;j = j - 2){ if(map[j][y] == '+' ); else { check_pao++;} }if(check_pao == 1&& tp == '+') //直线行走但不可吃棋子ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2 && tp != '+') //跳跃吃棋ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n"); Sleep(500); }}}else if( x == ck_x ){ int check_pao = 0;if( y > ck_y){ for(j = ck_y + 4; j<= y ;j = j+4){ if(map[x][j] == '+' ); else check_pao++;}if(check_pao == 1&& tp == '+') //直线行走但不可吃棋子ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2 && tp != '+') //跳跃吃棋ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n"); Sleep(500); }}else {for(j = ck_y - 4; j>= y;j = j - 4){if(map[x][j] == '+' ); else check_pao++;}if(check_pao == 1&& tp == '+') //直线行走但不可吃棋子ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2 && tp != '+') //跳跃吃棋ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n"); Sleep(500); }}}else { printf("不合法的下法\n");Sleep(500); }}if( ck_t == 16) //卒的走法规范{ if ( x >= 14){ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( x == ck_x - 2 && y == ck_y)ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n"); Sleep(500); }}else{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if((x - ck_x == 0 && abs(y-ck_y) ==4) ||( x - ck_x == -2 && abs(y-ck_y) == 0)) ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n"); Sleep(500); }}}}else { if( ck_t == 20) //车的走法规范(帅方){ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( y == ck_y ){ if( x > ck_x){ for(j = ck_x + 2; j < x;j = j + 2){ if(map[j][y] == '+'); else {printf("不合法的下法\n"); Sleep(500); break; } }if( j >= x) ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}if( x < ck_x){ for(j = ck_x - 2; j > x;j = j - 2){ if(map[j][y] == '+'); else { printf("不合法的下法\n"); Sleep(500); break; } }if( j <= x) ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}}else if( x == ck_x ){ if( y > ck_y){ for(j = ck_y + 4; j < y;j = j + 4){ if(map[x][j] == '+'); else { printf("不合法的下法\n"); Sleep(500); break; } }if( j >= y) ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}if( y < ck_y){ for(j = ck_y - 4; j > y;j = j - 4){ if(map[x][j] == '+'); else { printf("不合法的下法\n");Sleep(500); break; } }if( j <= y) ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}}else { printf("不合法的下法\n"); Sleep(500); }}if( ck_t == 21) //马的走法规范{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n");printf("还是你的回合"); Sleep(500);}else if( (abs( x - ck_x) == 2&& abs( y - ck_y) == 8)&&map[ck_x][(y+ck_y)/2] =='+'){ ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else if( (abs( x - ck_x) == 4&& abs( y - ck_y) == 4)&&map[(x + ck_x)/2][ck_y] == '+' ){ ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else { printf("不合法的下法\n");Sleep(500); }}if( ck_t == 22) //相的走法规范{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n");printf("还是你的回合"); Sleep(500);}else if( x <= 12 && (abs(y - ck_y) == 8 && abs(x - ck_x) == 4)){ if((x == 4 && (y == 11 || y == 27))||(x == 8 && ( y == 3 || y == 19 || y == 35)) ||(x == 12 && (y == 11|| y ==27))){ if( map[(x+ck_x)/2][(y+ck_y)/2] == '+')ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("棋子卡住,不可执行");Sleep(500); }}else {printf("不合法的下法\n");Sleep(500); }}else { printf("不合法的下法\n");Sleep(500); }}if( ck_t == 23) //士的走法规范{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n");printf("还是你的回合"); Sleep(500);}else if( abs(x - ck_x)== 2 && abs( y - ck_y) == 4 &&((x==4 &&(y == 15 || y == 23)) || ( x == 6 && y == 19) || ( x == 8 && ( y == 15 || y == 23)))) { ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else { printf("不合法的下法\n");Sleep(500); }}if( ck_t == 24) //将的走法规范{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n");printf("还是你的回合"); Sleep(500);}else if( ((abs(x - ck_x)== 2 && abs( y - ck_y) == 0 )|| (abs(x - ck_x)== 0 && abs( y - ck_y) == 4)) && x >= 4 && x <= 8 && y >= 15 && y <= 23 ){ ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else {printf("不合法的下法\n");Sleep(500); }}if( ck_t == 25) //炮的走法规范{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n");printf("还是你的回合"); Sleep(500);}else if( y == ck_y ){ int check_pao = 0;if( x > ck_x){ for(j = ck_x + 2; j<= x ;j = j+ 2){ if(map[j][y] == '+' ); else check_pao++;}if(check_pao == 1&& tp == '+') //直线行走但不可吃棋子ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2 && tp != '+') //跳跃吃棋ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n");Sleep(500); }}else { for(j = ck_x - 2; j>= x;j = j - 2){ if(map[j][y] == '+' ); else { check_pao++;} }if(check_pao == 1&& tp== '+') //直线行走但不可吃棋子ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2 && tp != '+') //跳跃吃棋ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n");Sleep(500); }}}else if( x == ck_x ){ int check_pao = 0;if( y > ck_y){ for(j = ck_y + 4; j<= y ;j = j+4){ if(map[x][j] == '+' ); else check_pao++;}if(check_pao == 1&& tp == '+') //直线行走但不可吃棋子ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2 && tp != '+') //跳跃吃棋ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n");Sleep(500); }}else{ for(j = ck_y - 4 ; j>= y;j = j - 4){ if(map[x][j] == '+' ); else check_pao++;}if(check_pao ==1&& tp == '+') //直线行走但不可吃棋子ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2&& tp != '+') //跳跃吃棋ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n");Sleep(500); }}}else { printf("不合法的下法\n");Sleep(500); }}if( ck_t == 26) //卒的走法规范{ if( x <= 12){ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( x == ck_x + 2 && y == ck_y)ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n");Sleep(500); }}else{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n");printf("还是你的回合"); Sleep(500);}else if((x - ck_x == 0 && abs(y-ck_y) ==4) ||( x - ck_x == 2&& abs(y-ck_y) == 0))ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n");Sleep(500); }}}}}}system("cls");if( if_ov) return;for(i = 0; i < 27; i++)puts(map[i]);}Sleep(5000);}int main( ){while(1){xiangqi( );printf("\n 重来,请按键.\n");getch( );}return 0;}。
从“中国象棋数字编码方案”谈思维
从“中国象棋数字编码方案”谈思维日期: 2009-10-1 23:39:00数字象棋编码我从小酷爱中国象棋,它对我一生有很大的帮助.而且我编的第1个程序就是象棋程序.在80年代初,国家公布了汉字编码方案.当时我没料到网络能普及到今天的地步.当时想如果两地棋手下棋可能通过卫星等无线传送棋谱数据,如果用ASCII或汉字传送棋谱时.传送的数据要大些且不能反映棋谱的动态性,故想像能否进行对中国象棋的数字编码来解决.当时有人做过类似国际象棋立体形状的中国象棋.由于中国象棋是很早发明的,列从右起始1~9,行为1~10.下盲棋时一般认为行从0~9.棋子共有车,马,相(象),仕(士),帅(将),兵(卒),炮等7个兵种.行棋有平,前(进) ,后(退)等3类.这样正好满足了数字的0~9.真是老天有眼呀---祖宗有灵气,发明的如此"精密".如果用棋子名在列线的位置号做为棋子号的话,就会产生以下编码:1-车 2-马 3-相(象) 4-仕(士) 5-帅(将) 7-兵(卒) 8-炮那么就剩下数字0,6,9了....天哪那个6上的"小尖尖"不是向上指的吗不就是前或进的意思吗同例9下的"小尖尖"不是向下指的吗不就是后或退的意思吗留下的0不说也知道了---那是不进不退只有横着走了,难道不就是"平"吗哈哈~~~原来什么事情分析出来后就是如此的简单,明了~~~“中国象棋数字编码方案”就被我几下倒塌地思维出来了:1-车 2-马 3-相(象) 4-仕(士) 5-帅(将) 7-兵(卒) 8-炮0-平 6-前(进) 9-后(退)棋谱举例:1.炮二平五马8进7 1. 8205 28672.马二进三车9平8 2. 2263 1908 。
11.前兵进一后炮退3 11. 6761 989312.前车平三后卒平4 12. 6103 9704 。
即炮二平五表示炮(8)二(2)平(0)五(5) 对应的数字编码:8205. 倒塌了,原来思维就是如此的简单~~~。
C语言程序源代码---中国象棋
#include<graphics.h>#include<conio.h>#include<string.h>#include<bios.h>#include<stdlib.h>#include"c:\tc\LIB\1.c"#define W 119#define S 115#define A 97#define D 100#define space 32#define UP 72#define DOWN 80#define LEFT 75#define RIGHT 77#define ENTER 13void qipan();void jiemian(int);void guangbiao1(int,int);void guangbiao2(int,int);void xuanzhong(int,int);void gaizi(int,int);char array(int,int);void xiazi(int,int,int,int);/*int panding(char,int,int,int,int);*/main(){int gdriver,gmode,i=0,c=0,x=190,y=190,m,n; char p;FILE *fp;gdriver=DETECT;gmode=0;if((fp=fopen("file.txt","at")) == NULL) {printf("Cannot open file!");system("pause");exit(0);}printf("%d,%d",gdriver,gmode);registerbgidriver(EGAVGA_driver);initgraph(&gdriver,&gmode,"c:\\tc"); cleardevice();while(c!=27){c=getch();clrscr();jiemian(i);if(c==80){fputs("down ",fp);i++;if(i==4){i=0;}}if(i==1){if(c==13){fputs("enter ",fp);qipan();c=getch();while(c!=27){c=getch();if(c==115){fputs("S ",fp);y=y+40;guangbiao1(x,y);guangbiao2(x,y-40);}if(c==119){fputs("W ",fp);y=y-40;guangbiao1(x,y);guangbiao2(x,y+40);}if(c==97){ fputs("A\n",fp);x=x-40;guangbiao1(x,y);guangbiao2(x+40,y);}if(c==100){ fputs("D\n",fp);x=x+40;guangbiao1(x,y);guangbiao2(x-40,y);}if(c==13){fputs("enter\n",fp);xuanzhong(x,y);m=x;n=y;}if(c==32){fputs("space\n",fp);xiazi(m,n,x,y);fputs("gaizi\n",fp);gaizi(m,n);}if(x>350||y>390||x<30||y<30) {x=190;y=30;}}}}}getch();closegraph();fclose(fp);restorecrtmode();return 0;}void qipan(){int i,j;setbkcolor(GREEN);cleardevice();setlinestyle(0,0,3);setcolor(1);rectangle(10,10,370,410);rectangle(30,30,350,390);for(i=1;i<8;i++){setlinestyle(0,0,3);line(i*40+30,30,i*40+30,190);line(i*40+30,230,i*40+30,390);}for(j=1;j<9;j++){setlinestyle(0,0,3);line(30,j*40+30,350,j*40+30);}setlinestyle(3,0,3);line(150,30,230,110);line(230,30,150,110);line(150,310,230,390);line(230,310,150,390);setusercharsize(4,1,2,1);settextstyle(1,0,4);outtextxy(70,195,"chinese chess"); red_shuai(190,30);red_shi(150,30);red_shi(230,30);red_xiang(110,30);red_xiang(270,30);red_ma(70,30);red_ma(310,30);red_ju(30,30);red_ju(350,30);red_pao(70,110);red_pao(310,110);red_bing(30,150);red_bing(110,150);red_bing(190,150);red_bing(270,150);red_bing(350,150);black_jiang(190,390);black_shi(150,390);black_shi(230,390);black_xiang(110,390);black_xiang(270,390);black_ma(70,390);black_ma(310,390);black_ju(30,390);black_ju(350,390);black_pao(70,310);black_pao(310,310);black_zu(30,270);black_zu(110,270);black_zu(190,270);black_zu(270,270);black_zu(350,270);setcolor(BLUE);rectangle(400,30,600,320);setcolor(4);settextstyle(1,0,2);outtextxy(420,50,"A->shuai B->shi"); outtextxy(420,80,"C->xiang D->ma"); outtextxy(420,110,"E->ju F->pao");outtextxy(420,140,"G->bing");setcolor(8);outtextxy(420,200,"H->jiang I->shi"); outtextxy(420,230,"J->xiang K->ma"); outtextxy(420,260,"L->ju M->pao");outtextxy(420,290,"N->zu");}void jiemian(int i){setbkcolor(GREEN);cleardevice();settextstyle(1,0,8);setcolor(BLUE);outtextxy(50,70,"chinese chess");settextstyle(0,0,3);setcolor(RED);outtextxy(260,215,"start");outtextxy(260,255,"again");outtextxy(260,295,"undo");outtextxy(260,335,"exit");rectangle(250,210+i*40,390,240+i*40); }void guangbiao1(int x,int y){setcolor(WHITE);setlinestyle(0,0,3);line(x-17,y-7,x-17,y-17);line(x-7,y-17,x-17,y-17);line(x+7,y-17,x+17,y-17);line(x+17,y-7,x+17,y-17);line(x-7,y+17,x-17,y+17);line(x-17,y+7,x-17,y+17);line(x+17,y+7,x+17,y+17);line(x+7,y+17,x+17,y+17);}void guangbiao2(int x,int y){setcolor(GREEN);setlinestyle(0,0,3);line(x-17,y-7,x-17,y-17);line(x-7,y-17,x-17,y-17);line(x+7,y-17,x+17,y-17);line(x+17,y-7,x+17,y-17);line(x-7,y+17,x-17,y+17);line(x-17,y+7,x-17,y+17);line(x+17,y+7,x+17,y+17);line(x+7,y+17,x+17,y+17);}void xuanzhong(int x,int y){setcolor(CYAN);setlinestyle(0,0,3);circle(x,y,15);}void gaizi(int x1,int y1){setlinestyle(0,0,3);setcolor(GREEN);circle(x1,y1,15);setfillstyle(0,3);floodfill(x1,y1,GREEN);setcolor(1);setlinestyle(0,0,3);if((30<x1<350)&&((y1==30)||(y1==230))) {line(x1-18,y1,x1+18,y1);line(x1,y1,x1,y1+18);if((30<x1<350)&&(y1==390||y1==190)) {line(x1-18,y1,x1+18,y1);line(x1,y1-18,x1,y1);}if((30<y1<390)&&x1==30){line(x1,y1,x1+18,y1);line(x1,y1-18,x1,y1+18);}if((30<y1<390)&&(x1==350)){line(x1-18,y1,x1,y1);line(x1,y1-18,x1,y1+18);}if((x1==30)&&(y1==30)){line(x1,y1,x1+18,y1);line(x1,y1,x1,y1+18);}if((x1==350)&&(y1==30)){line(x1-18,y1,x1,y1);line(x1,y1,x1,y1+18);}if((x1==30)&&(y1==390)){line(x1,y1,x1+18,y1);line(x1,y1,x1,y1-18);}if((x1==350)&&(y1==390)){line(x1,y1,x1-18,y1);line(x1,y1,x1,y1-18);}else{line(x1-18,y1,x1+18,y1);line(x1,y1-18,x1,y1+18);}}char array(int i,int j)char a[13][13];int c,b;c=i;b=j;for(c=1;c<10;c++){for(b=1;b<11;b++){a[c][b]='Z';}}a[1][5]='A';a[1][4]='B';a[1][6]='B';a[1][3]='C';a[1][7]='C';a[1][2]='D';a[1][8] ='D';a[1][1]='E';a[1][9]='E';a[3][2]='F';a[3][8]='F';a[4][1]=a[4][3]=a[4][5]=a[4][7]=a[4][9]='G';a[10][5]='H';a[10][4]='I';a[10][6]='I';a[10][3]='J';a[10][7]='J';a[10][2]='K';a [10][8]='K';a[10][1]='L';a[10][9]='L';a[2][3]='M';a[8][3]='M';a[7][1]=a[7][3]=a[7][5]=a[7][7]=a[7][9]='N';return a[i][j];}void xiazi(int x6,int y6,int x7,int y7){switch(array(y6/40+1,x6/40+1)){case 'A':red_shuai(x7,y7);break;case 'B':red_shi(x6,y7);break;case 'C':red_xiang(x7,y7);break;case 'D':red_ma(x7,y7);break;case 'E':red_ju(x7,y7);break;case 'F':red_pao(x7,y7);break;case 'G':red_bing(x7,y7);break;case 'H':black_jiang(x7,y7);break;case 'I':black_shi(x7,y7);break;case 'J':black_xiang(x7,y7);break;case 'K':black_ma(x7,y7);break;case 'L':black_ju(x7,y7);break;case 'M':black_pao(x7,y7);break;case 'N':black_zu(x7,y7);break;case 'Z':gaizi(x6,x6);break;}}/*int panding(char q,int x,int y,int a,int b){switch(q){case 'A':if(y>110||x>230||x<150||(a-x)>40||(x-a)>40||(y-b)>40||(b-y)>40)return 0;elsereturn 1;break;case'B':if(((x-a)==40&&(y-b)==40)&&y<=110&&230<x<150||((a-x)==40&&(b-y)==40)&&y<=11 0&&230>x>150)return 1;elsereturn 0;break;case'C':if((((x-a)==80&&(y-b)==80)&&y<=190)&&(array((y+b)/2/40+1,(x+a)/2/40+1,)=='Z ')))||(((a-x)==80&&(b-y)==80)&&y<=190)&&(array((y+b)/2/40+1,(x+a)/2/40+1)=='Z') )))return 1;elsereturn 0;break;case'D':if((((x-a)==80&&(y-b)==40&&(array(y/40+1,(x-40)/40+1)=='Z'))||(((a-x)==80&&(b-y)==40)&&(array(y/40+1,(x+40)/40+1)=='Z'))||(((x-a)==40&&(y-b)==80)(array((y -40)/40+1,x/40+1)=='Z'))||(((a-x)==40&&(b-y)==80)&&(array((y+40)/40+1,x/40+1)== 'z'))) )return 1;elsereturn 0;break;case 'E':return 1;break;case 'F':return 1;break;case 'G':if(y<190){if(y>b||x!=a){return 0;}elsereturn 1;}else{if((b-y)>40||(a-x)>40||(x-a)>40||y>b){return 0;}elsereturn 1;}break;case 'H':if(y<310||x>230||x<150||(a-x)>40||(x-a)>40||(y-b)>40||(b-y)>40) return 0;elsereturn 1;break;case'I':if(((x-a)==40&&(y-b)==40)&&y>=310&&230<x<150||((a-x)==40&&(b-y)==40)&&y>310 &&230>x>150)return 1;elsereturn 0;break;case'J':if(((((x-a)==80&&(y-b)==80)&&y>=230)&&array(((y+b)/2/40+1,(x+a)/2/40+1)=='Z ')))||(((a-x)==80&&(b-y)==80)&&y>=230)&&(array((y+b)/2/40+1,(x+a)/2/40+1)=='Z'))))return 1;elsereturn 0;break;case'K':if((((x-a)==80&&(y-b)==40&&(array(y/40+1,(x-40)/40+1)=='Z'))||(((a-x)==80&& (b-y)==40)&&(array(y/40+1,(x+40)/40+1)=='Z'))||(((x-a)==40&&(y-b)==80)(array((y -40)/40+1,x/40+1)=='Z'))||(((a-x)==40&&(b-y)==80)&&(array((y+40)/40+1,x/40+1)== 'Z'))return 1;elsereturn 0;break;case 'L':return 1;break;case 'M':return 1;break;case 'N':if(y>230){if(y<b||x!=a){return 0;}elsereturn 1;}else{if(y-b>40||(a-x)>40||(x-a)>40||y<b){return 0;}elsereturn 1;}default:return 0;}}*/。
C语言编写象棋程序代码
/*--------------------chess.c----------------------*/#include "dos.h"#include "stdio.h"/*----------------------------------------------------*/ #define RED 7#define BLACK 14#define true 1#define false 0#define SELECT 0#define MOVE 1#define RED_UP 0x1100#define RED_DOWN 0x1f00#define RED_LEFT 0x1e00#define RED_RIGHT 0x2000#define RED_DO 0x3900#define RED_UNDO 0x1000#define BLACK_UP 0x4800#define BLACK_DOWN 0x5000#define BLACK_LEFT 0x4b00#define BLACK_RIGHT 0x4d00#define BLACK_DO 0x1c00#define BLACK_UNDO 0x2b00#define ESCAPE 0x0100#define RED_JU 1#define RED_MA 2#define RED_XIANG 3#define RED_SHI 4#define RED_JIANG 5#define RED_PAO 6#define RED_BIN 7#define BLACK_JU 8#define BLACK_MA 9#define BLACK_XIANG 10#define BLACK_SHI 11#define BLACK_JIANG 12#define BLACK_PAO 13#define BLACK_BIN 14/*----------------------------------------------------*/ int firsttime=1;int savemode;char page_new=0,page_old=0;int finish=false,turn=BLACK,winner=0;int key;int redstate=SELECT,blackstate=SELECT;int board[10][9];/*----------------------------------------------------*/char *chessfile[15]={"","bmp\\rju.wfb", "bmp\\rma.wfb", "bmp\\rxiang.wfb","bmp\\rshi.wfb","bmp\\rjiang.wfb","bmp\\rpao.wfb","bmp\\rbin.wfb","bmp\\bju.wfb", "bmp\\bma.wfb", "bmp\\bxiang.wfb","bmp\\bshi.wfb","bmp\\bjiang.wfb","bmp\\bpao.wfb","bmp\\bbin.wfb"};char *boardfile[10][9]={{"bmp\\11.wfb","bmp\\1t.wfb","bmp\\1t.wfb","bmp\\14.wfb","bmp\\15.wfb","bmp\\16 .wfb","bmp\\1t.wfb","bmp\\1t.wfb","bmp\\19.wfb"},{"bmp\\21.wfb","bmp\\2c.wfb","bmp\\2c.wfb","bmp\\24.wfb","bmp\\25.wfb","bmp\\26 .wfb","bmp\\2c.wfb","bmp\\2c.wfb","bmp\\29.wfb"},{"bmp\\21.wfb","bmp\\3a.wfb","bmp\\3t.wfb","bmp\\34.wfb","bmp\\3t.wfb","bmp\\36 .wfb","bmp\\3t.wfb","bmp\\3a.wfb","bmp\\29.wfb"},{"bmp\\41.wfb","bmp\\4t.wfb","bmp\\4a.wfb","bmp\\4t.wfb","bmp\\4a.wfb","bmp\\4t .wfb","bmp\\4a.wfb","bmp\\4t.wfb","bmp\\49.wfb"},{"bmp\\51.wfb","bmp\\52.wfb","bmp\\5t.wfb","bmp\\54.wfb","bmp\\5t.wfb","bmp\\56 .wfb","bmp\\5t.wfb","bmp\\58.wfb","bmp\\59.wfb"},{"bmp\\61.wfb","bmp\\62.wfb","bmp\\6t.wfb","bmp\\64.wfb","bmp\\6t.wfb","bmp\\66 .wfb","bmp\\6t.wfb","bmp\\68.wfb","bmp\\69.wfb"},{"bmp\\71.wfb","bmp\\7t.wfb","bmp\\7a.wfb","bmp\\7t.wfb","bmp\\7a.wfb","bmp\\7t .wfb","bmp\\7a.wfb","bmp\\7t.wfb","bmp\\79.wfb"},{"bmp\\81.wfb","bmp\\8a.wfb","bmp\\8t.wfb","bmp\\84.wfb","bmp\\85.wfb","bmp\\86 .wfb","bmp\\8t.wfb","bmp\\8a.wfb","bmp\\89.wfb"},{"bmp\\91.wfb","bmp\\9t.wfb","bmp\\9t.wfb","bmp\\9t.wfb","bmp\\95.wfb","bmp\\9t .wfb","bmp\\9t.wfb","bmp\\9t.wfb","bmp\\99.wfb"},{"bmp\\101.wfb","bmp\\102.wfb","bmp\\102.wfb","bmp\\104.wfb","bmp\\105.wfb","bm p\\106.wfb","bmp\\108.wfb","bmp\\108.wfb","bmp\\109.wfb"}};char cursor[14][14]={0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,255,255,255,255,255,255,255,0,0,1,1,1,1,0,255,255,255,255,255,255,0,0,1,1,1,1,1,0,255,255,255,255,255,255,0,0,1,1,1,1,1,0,255,255,255,255,255,255,255,0,0,1,1,1,1,0,255,255,255,255,255,255,255,255,0,0,1,1,1,0,255,255,255,255,255,255,255,255,255,0,0,1,1,0,255,255,0,255,255,255,255,255,255,255,0,0,1,0,255,0,1,1,0,255,255,255,255,255,255,255,0,0,0,1,1,1,1,0,255,255,255,255,255,0,1,0,1,1,1,1,1,1,0,255,255,255,0,1,1,1,1,1,1,1,1,1,1,0,255,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1};struct pos{int x;int y;}position[10][9],redcurpos,redtemppos,redoldpos,blackcurpos,blacktemppos,blacko ldpos;/*----------------------------------------------------*/selectpage(register char page) /*换页函数*/{union REGS r;r.x.ax=0x4f05;r.x.bx=0;r.x.dx=page; /*选择页面*/int86(0x10,&r,&r);}unsigned char set_SVGA_mode(int vmode) /*设置SVGA屏幕模式*/{union REGS r;r.x.ax=0x4f02;r.x.bx=vmode;int86(0x10,&r,&r);return(r.h.ah);}unsigned int get_SVGA_mode() /*获取当前SVGA屏幕模式*/{union REGS r;r.x.ax=0x4f03;int86(0x10,&r,&r);return(r.x.bx);}drawbmp(int start_x,int start_y,char filename[]){char buffer[640];int i,j,k,n,r,g,b,width,length;long position;FILE *fp;if((fp=fopen(filename,"rb"))==NULL){printf("Error! Can't open file!");getch();return;}fseek(fp,28,SEEK_SET);fread(&i,2,1,fp);if(i!=8) /*检查是否为256色位图*/{puts("Error!Can't find bitmap!");fclose(fp);getch();exit(0);}fseek(fp,18,SEEK_SET);fread(&width,4,1,fp);fread(&length,4,1,fp);if(firsttime){fseek(fp,54,SEEK_SET);for(i=0;i<256;i++) /*按照该图片的DAC色表设置色彩寄存器*/{b=fgetc(fp);g=fgetc(fp);r=fgetc(fp); /*获取R、G、B分量*/outportb(0x3c8,i);outportb(0x3c9,r>>2); /*右移是要转化为VGA的6位寄存器形式*/ outportb(0x3c9,g>>2);outportb(0x3c9,b>>2);fgetc(fp);}}elsefseek(fp,300,SEEK_SET);k=(width%4)?(4-width%4):0; /*宽度修正值*/for(j=length-1+start_x;j>=start_x;j--){fread(buffer,width,1,fp);for(i=start_y,n=0;i<width+start_y;i++,n++){position=j*640l+i; /*计算要显示点的显存位置*/page_new=position/65536; /*计算显示页*/if(page_new!=page_old) /*当显示页不同时更换页面,提高一定的输出速度*/ {selectpage(page_new);page_old=page_new;}pokeb(0xa000,position%65536,buffer[n]); /*写到显存位置*/}fseek(fp,k,SEEK_CUR); /*每行绘制完后修正宽度*/}fclose(fp);}init(){savemode=get_SVGA_mode(); /*先保存原来的屏幕模式*/set_SVGA_mode(0x101); /*硬件无关性初始化屏幕为640*480 256色模式*/}end(){set_SVGA_mode(savemode); /*恢复屏幕*/}/*----------------------------------------------------*/initpos(){int i,j;for(i=0;i<10;i++)for (j=0;j<9;j++){position[i][j].x=35+i*39;position[i][j].y=43+j*40;}}initchessmap(){board[0][0]=BLACK_JU;board[0][1]=BLACK_MA;board[0][2]=BLACK_XIANG;board[0][3]=BLACK_SHI;board[0][4]=BLACK_JIANG;board[0][5]=BLACK_SHI;board[0][6]=BLACK_XIANG;board[0][7]=BLACK_MA;board[0][8]=BLACK_JU;board[2][1]=BLACK_PAO;board[2][7]=BLACK_PAO;board[3][0]=BLACK_BIN;board[3][2]=BLACK_BIN;board[3][4]=BLACK_BIN;board[3][6]=BLACK_BIN;board[3][8]=BLACK_BIN;board[9][0]=RED_JU;board[9][1]=RED_MA;board[9][2]=RED_XIANG;board[9][3]=RED_SHI;board[9][4]=RED_JIANG;board[9][5]=RED_SHI;board[9][6]=RED_XIANG;board[9][7]=RED_MA;board[9][8]=RED_JU;board[7][1]=RED_PAO;board[7][7]=RED_PAO;board[6][0]=RED_BIN;board[6][2]=RED_BIN;board[6][4]=RED_BIN;board[6][6]=RED_BIN;board[6][8]=RED_BIN;}initdrawchess(){int i,j;;for(i=0;i<10;i++)for(j=0;j<9;j++){if(board[i][j])drawbmp(position[i][j].x,position[i][j].y,chessfile[board[i][j]]); }}drawcursor(struct pos p){int i,j,n,m,x,y;long thisposition;x=position[p.x][p.y].x+20;y=position[p.x][p.y].y+25;for(j=13-1+x,m=13;j>=x;j--,m--){for(i=y,n=0;i<13+y;i++,n++){thisposition=j*640l+i; /*计算要显示点的显存位置*/page_new=thisposition/65536; /*计算显示页*/if(page_new!=page_old) /*当显示页不同时更换页面,提高一定的输出速度*/ {selectpage(page_new);page_old=page_new;}if(cursor[m][n]!=1)if(cursor[m][n]==0)pokeb(0xa000,thisposition%65536,0);elseif(turn==RED)pokeb(0xa000,thisposition%65536,153);elsepokeb(0xa000,thisposition%65536,255);}}}drawselecursor(struct pos p){int i,j,n,m,x,y;long thisposition;x=position[p.x][p.y].x+20;y=position[p.x][p.y].y+25;for(j=13-1+x,m=13;j>=x;j--,m--){for(i=y,n=0;i<13+y;i++,n++){thisposition=j*640l+i; /*计算要显示点的显存位置*/page_new=thisposition/65536; /*计算显示页*/if(page_new!=page_old) /*当显示页不同时更换页面,提高一定的输出速度*/ {selectpage(page_new);page_old=page_new;}if(cursor[m][n]!=1)pokeb(0xa000,thisposition%65536,0);}}}/*----------------------------------------------------*/ int getkey(){int press;while(bioskey(1) == 0);press=bioskey(0);press=press&0xff00;return(press);}/*--------------------红方操作--------------------*/int redcanselect(){int x,y;x=redcurpos.x;y=redcurpos.y;if(board[x][y]>=RED_JU&&board[x][y]<=RED_BIN)return 1;elsereturn 0;}int redcanmove(){int i,j,min,max,oldx,oldy,x,y;oldx=redoldpos.x;oldy=redoldpos.y;x=redcurpos.x;y=redcurpos.y;/*case1 目标位置是否是自己人*/if(board[x][y]>=RED_JU&&board[x][y]<=RED_BIN)return 0;/* 军、马、炮、相、士、将、卒的走法正确性的判断*/switch(board[oldx][oldy]){case RED_BIN: /*完成*/ if(oldx>=5){ if(y!=oldy||(oldx-x)!=1) return 0;}else{ if(x==(oldx-1)&&y==oldy) return 1;elseif(x==oldx&&y==(oldy+1)) return 1;elseif(x==oldx&&y==(oldy-1)) return 1;elsereturn 0;}break;case RED_JIANG: /*完成*/ if(x!=oldx&&y!=oldy) return 0;if(x!=oldx)if((x-oldx)>1||(oldx-x)>1) return 0;else if(x<7) return 0;else if(y!=oldy)if((y-oldy)>1||(oldy-y)>1) return 0;else if(y<3||y>5) return 0;break;case RED_JU: /*完成*/ if(x!=oldx&&y!=oldy) return 0;else if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0) return 0;}break;case RED_MA: /*完成*/ if((x-oldx)==2&&((y-oldy)==1||(oldy-y)==1)){if(board[oldx+1][oldy]!=0) return 0;}elseif((oldx-x)==2&&((y-oldy)==1||(oldy-y)==1)){if(board[oldx-1][oldy]!=0) return 0;}elseif((y-oldy)==2&&((x-oldx)==1||(oldx-x)==1)){if(board[oldx][oldy+1]!=0) return 0;}elseif((oldy-y)==2&&((x-oldx)==1||(oldx-x)==1)){if(board[oldx][oldy-1]!=0) return 0;}elsereturn 0;break;case RED_PAO: /*完成*/ if(x!=oldx&&y!=oldy) return 0;if(board[x][y]==0){if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0) return 0;}}else{if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1,j=0;i<max;i++)if(board[i][y]!=0) j++;if(j!=1) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+1,j=0;i<max;i++)if(board[x][i]!=0) j++;if(j!=1) return 0;}}break;case RED_SHI: /*完成*/ if(oldx==9||oldx==7){if(x!=8||y!=4) return 0;}else if(oldx==8){if(x==9&&y==3) return 1;elseif(x==9&&y==5) return 1;elseif(x==7&&y==3) return 1;elseif(x==7&&y==5) return 1;else return 0;}else return 0;break;case RED_XIANG: /*完成*/if(x<5) return 0;if(x!=oldx&&y!=oldy){if((x-oldx)==2&&(y-oldy)==2){i=oldx+1;j=oldy+1;}else if((x-oldx)==2&&(oldy-y)==2){i=oldx+1;j=oldy-1;}else if((oldx-x)==2&&(y-oldy)==2){i=oldx-1;j=oldy+1;}else if((oldx-x)==2&&(oldy-y)==2){i=oldx-1;j=oldy-1;}else return 0;if(board[i][j]!=0) return 0;}else return 0;break;}return 1;}redup(){int x,y,n;if(redcurpos.x>0){redcurpos.x--;x=position[redtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]==0)drawbmp(x,y,boardfile[redtemppos.x][redtemppos.y]);elseif(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)) {n=board[redtemppos.x][redtemppos.y];drawbmp(x,y,chessfile[n]);}if(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE) drawselecursor(redoldpos);drawcursor(redcurpos);redtemppos.x=redcurpos.x;redtemppos.y=redcurpos.y;}}reddown(){int x,y,n;if(redcurpos.x<9){redcurpos.x++;x=position[redtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]==0)drawbmp(x,y,boardfile[redtemppos.x][redtemppos.y]);elseif(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)){n=board[redtemppos.x][redtemppos.y];drawbmp(x,y,chessfile[n]);}if(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE) drawselecursor(redoldpos);drawcursor(redcurpos);redtemppos.x=redcurpos.x;redtemppos.y=redcurpos.y;}}redleft(){int x,y,n;if(redcurpos.y>0){redcurpos.y--;x=position[redtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]==0)drawbmp(x,y,boardfile[redtemppos.x][redtemppos.y]);elseif(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)){n=board[redtemppos.x][redtemppos.y];drawbmp(x,y,chessfile[n]);}if(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE) drawselecursor(redoldpos);drawcursor(redcurpos);redtemppos.x=redcurpos.x;redtemppos.y=redcurpos.y;}}redright(){int x,y,n;if(redcurpos.y<8){redcurpos.y++;x=position[redtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]==0)drawbmp(x,y,boardfile[redtemppos.x][redtemppos.y]);elseif(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)){n=board[redtemppos.x][redtemppos.y];drawbmp(x,y,chessfile[n]);}if(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE) drawselecursor(redoldpos);drawcursor(redcurpos);redtemppos.x=redcurpos.x;redtemppos.y=redcurpos.y;}}reddo(){int i,j,x,y,n;if(redstate==SELECT&&redcanselect()){if(board[redcurpos.x][redcurpos.y]<=RED&&board[redcurpos.x][redcurpos.y]>0) {redstate=MOVE;drawselecursor(redcurpos);redoldpos.x=redcurpos.x;redoldpos.y=redcurpos.y;}}else if(redstate==MOVE&&redcanmove()){x=position[redoldpos.x][redoldpos.y].x;y=position[redoldpos.x][redoldpos.y].y;drawbmp(x,y,boardfile[redoldpos.x][redoldpos.y]);x=position[redcurpos.x][redcurpos.y].x;y=position[redcurpos.x][redcurpos.y].y;n=board[redoldpos.x][redoldpos.y];drawbmp(x,y,chessfile[n]);if(board[redcurpos.x][redcurpos.y]==BLACK_JIANG){winner=RED;finish=1;return;}board[redcurpos.x][redcurpos.y]=n;board[redoldpos.x][redoldpos.y]=0;for(i=0;i<=2;i++)for(j=3;j<=5;j++)if(board[i][j]==BLACK_JIANG){x=i;y=j;}for(i=x+1,j=y,n=0;i<=9;i++){if(board[i][j]==RED_JIANG&&n==0){winner=BLACK;finish=1;break;}else if(board[i][j]!=0) n++;}turn=BLACK;blackstate=SELECT;drawcursor(blackcurpos);drawbmp(30,438,"bmp\\bzq.wfb"); /*转交控制权给黑方*/ }}redundo(){int x,y,n;if(redstate==MOVE){x=position[redoldpos.x][redoldpos.y].x;y=position[redoldpos.x][redoldpos.y].y;n=board[redoldpos.x][redoldpos.y];drawbmp(x,y,chessfile[n]);redoldpos.x=redcurpos.x;redoldpos.y=redcurpos.y;drawcursor(redcurpos);redstate=SELECT;}}/*--------------------黑方操作----------------------*/int blackcanselect(){int x,y;x=blackcurpos.x;y=blackcurpos.y;if(board[x][y]>=BLACK_JU&&board[x][y]<=BLACK_BIN)return 1;elsereturn 0;}int blackcanmove(){int i,j,min,max,oldx,oldy,x,y;oldx=blackoldpos.x;oldy=blackoldpos.y;x=blackcurpos.x;y=blackcurpos.y;/*case1 目标位置是否是自己人*/if(board[x][y]>=BLACK_JU&&board[x][y]<=BLACK_BIN)return 0;/* 军、马、炮、相、士、将、卒的走法正确性的判断*/switch(board[oldx][oldy]){case BLACK_JU: /*完成*/ if(x!=oldx&&y!=oldy) return 0;else if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0) return 0;}break;case BLACK_MA: /*完成*/ if((x-oldx)==2&&((y-oldy)==1||(oldy-y)==1)){if(board[oldx+1][oldy]!=0) return 0;}elseif((oldx-x)==2&&((y-oldy)==1||(oldy-y)==1)){if(board[oldx-1][oldy]!=0) return 0;}elseif((y-oldy)==2&&((x-oldx)==1||(oldx-x)==1)){if(board[oldx][oldy+1]!=0) return 0;}elseif((oldy-y)==2&&((x-oldx)==1||(oldx-x)==1)){if(board[oldx][oldy-1]!=0) return 0;}elsereturn 0;break;case BLACK_PAO: /*完成*/if(x!=oldx&&y!=oldy) return 0;if(board[x][y]==0){if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0) return 0; }else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0) return 0; }}else{j=0;if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0) j++;if(j!=1) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0) j++;if(j!=1) return 0;}}break;case BLACK_XIANG:if(x>4) return 0;if(x!=oldx&&y!=oldy){if((x-oldx)==2&&(y-oldy)==2){i=oldx+1;j=oldy+1;}else if((x-oldx)==2&&(oldy-y)==2) {i=oldx+1;j=oldy-1;}else if((oldx-x)==2&&(y-oldy)==2) {i=oldx-1;j=oldy+1;}else if((oldx-x)==2&&(oldy-y)==2) {i=oldx-1;j=oldy-1;}else return 0;if(board[i][j]!=0) return 0;}else return 0;break;case BLACK_SHI:if(oldx==0||oldx==2){if(x!=1||y!=4) return 0;}else if(oldx==1){if(x==0&&y==3) return 1;elseif(x==0&&y==5) return 1;elseif(x==2&&y==3) return 1;elseif(x==2&&y==5) return 1;else return 0;}else return 0;break;case BLACK_JIANG:if(x!=oldx&&y!=oldy) return 0;if(x!=oldx)if((x-oldx)>1||(oldx-x)>1) return 0; else if(x>2) return 0;else if(y!=oldy)if((y-oldy)>1||(oldy-y)>1) return 0; else if(y<3||y>5) return 0;break;case BLACK_BIN:if(oldx<=4){ if(y!=oldy||(x-oldx)!=1) return 0;} else{ if(x==(oldx+1)&&y==oldy) return 1; elseif(x==oldx&&y==(oldy+1)) return 1; elseif(x==oldx&&y==(oldy-1)) return 1; elsereturn 0;}break;}return 1;}blackup(){int x,y,n;if(blackcurpos.x>0){blackcurpos.x--;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]);elseif(!(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==MO VE)drawselecursor(blackoldpos);drawcursor(blackcurpos);blacktemppos.x=blackcurpos.x;blacktemppos.y=blackcurpos.y;}}blackdown(){int x,y,n;if(blackcurpos.x<9){blackcurpos.x++;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]);elseif(!(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==MO VE)drawselecursor(blackoldpos);drawcursor(blackcurpos);blacktemppos.x=blackcurpos.x;blacktemppos.y=blackcurpos.y;}}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]);elseif(!(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==MO VE)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]);elseif(!(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==MO VE)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=0;i<=2;i++)for(j=3;j<=5;j++)if(board[i][j]==BLACK_JIANG){x=i;y=j;}for(i=x+1,j=y,n=0;i<=9;i++){if(board[i][j]==RED_JIANG&&n==0){winner=RED;finish=1;break;}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();break;case 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=1;break;}}}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();}。
中国象棋算法
解剖大象的眼睛——中国象棋程序设计探索黄晨*2005年6月( * 联系地址:复旦大学化学系表面化学实验室,eMail:morning_yellow@elephantbas )(一) 引言我在今年2月写出了象棋程序ElephantEye的第一个版本(0.90),本来它只是象棋界面ElephantBoard的调试引擎。
在设计程序的过程中,我尝试性地加入了很多算法,发现每次改进都能让程序的棋力有大幅度的提高,因此便对象棋程序的算法产生了浓厚的兴趣。
到现在我已经陆续对ElephantEye作了几十次加工(目前版本为0.94),使得它的棋力接近了中等商业软件的水平,在公开源代码的象棋程序中,ElephantEye是最强的一个。
我希望能通过公开源代码的方式,推动中国象棋程序水平的整体发展,然而根据很多网友的反馈意见,发现源代码中的很多部分并不是那么容易理解的。
因此我才打算以《中国象棋程序设计探索》为题,写几篇详细介绍ElephantEye算法的连载,希望能让的源代码充分发挥它的作用。
下面我先简要谈一下我自己对ElephantEye的体会。
1.1 ElephantEye用到了哪些算法?在我写本次连载以前,我已经完成了《象棋百科全书》网站上《对弈程序基本技术》专题中所有文章的翻译,ElephantEye的大部分算法都参考了这些文章,这些算法我会在连载中一笔带过,详细的内容希望读者参考这些译文,那里还有我加的很多译注,希望它们能够加深读者对这些算法的体会。
当然,仅根据这些文章所提供的算法,是写不出很好的程序的,我参考了王小春的《PC游戏编程——人机博弈》一书,也参考了一些国际象棋的源程序,并通过自己的探索,在ElephantEye中加入了另外的非常重要的算法,尤其是启发算法,我认为它们在程序中发挥了关键性的作用,而且很多细节在绝大多数文字资料中没有详细给出,我会在我的连载中重点介绍。
我猜读者最感兴趣的内容是ElephantEye的着法生成器,这应该算是象棋程序的核心部分,同时也是各个程序差异最大的部分。
C语言编写象棋程序代码
/*--------------------chess.c----------------------*/ #include "dos.h"#include "stdio.h"/*----------------------------------------------------*/ #define RED 7#define BLACK 14#define true 1#define false 0#define SELECT 0#define MOVE 1#define RED_UP 0x1100#define RED_DOWN 0x1f00#define RED_LEFT 0x1e00#define RED_RIGHT 0x2000#define RED_DO 0x3900#define RED_UNDO 0x1000#define BLACK_UP 0x4800#define BLACK_DOWN 0x5000#define BLACK_LEFT 0x4b00#define BLACK_RIGHT 0x4d00#define BLACK_DO 0x1c00#define BLACK_UNDO 0x2b00#define ESCAPE 0x0100#define RED_JU 1#define RED_MA 2#define RED_XIANG 3#define RED_SHI 4#define RED_JIANG 5#define RED_PAO 6#define RED_BIN 7#define BLACK_JU 8#define BLACK_MA 9#define BLACK_XIANG 10#define BLACK_SHI 11#define BLACK_JIANG 12#define BLACK_PAO 13#define BLACK_BIN 14/*----------------------------------------------------*/ int firsttime=1;int savemode;char page_new=0,page_old=0;int finish=false,turn=BLACK,winner=0;int key;int redstate=SELECT,blackstate=SELECT;int board[10][9];/*----------------------------------------------------*/char *chessfile[15]={"","bmp\\rju.wfb", "bmp\\rma.wfb", "bmp\\rxiang.wfb","bmp\\rshi.wfb","bmp\\rjiang.wfb","bmp\\rpao.wfb","bmp\\rbin.wfb","bmp\\bju.wfb", "bmp\\bma.wfb", "bmp\\bxiang.wfb","bmp\\bshi.wfb","bmp\\bjiang.wfb","bmp\\bpao.wfb","bmp\\bbin.wfb"};char *boardfile[10][9]={{"bmp\\11.wfb","bmp\\1t.wfb","bmp\\1t.wfb","bmp\\14.wfb","bmp\\15.wfb","bmp\\16.wfb"," bmp\\1t.wfb","bmp\\1t.wfb","bmp\\19.wfb"},{"bmp\\21.wfb","bmp\\2c.wfb","bmp\\2c.wfb","bmp\\24.wfb","bmp\\25.wfb","bmp\\26.wfb", "bmp\\2c.wfb","bmp\\2c.wfb","bmp\\29.wfb"},{"bmp\\21.wfb","bmp\\3a.wfb","bmp\\3t.wfb","bmp\\34.wfb","bmp\\3t.wfb","bmp\\36.wfb"," bmp\\3t.wfb","bmp\\3a.wfb","bmp\\29.wfb"},{"bmp\\41.wfb","bmp\\4t.wfb","bmp\\4a.wfb","bmp\\4t.wfb","bmp\\4a.wfb","bmp\\4t.wfb"," bmp\\4a.wfb","bmp\\4t.wfb","bmp\\49.wfb"},{"bmp\\51.wfb","bmp\\52.wfb","bmp\\5t.wfb","bmp\\54.wfb","bmp\\5t.wfb","bmp\\56.wfb"," bmp\\5t.wfb","bmp\\58.wfb","bmp\\59.wfb"},{"bmp\\61.wfb","bmp\\62.wfb","bmp\\6t.wfb","bmp\\64.wfb","bmp\\6t.wfb","bmp\\66.wfb"," bmp\\6t.wfb","bmp\\68.wfb","bmp\\69.wfb"},{"bmp\\71.wfb","bmp\\7t.wfb","bmp\\7a.wfb","bmp\\7t.wfb","bmp\\7a.wfb","bmp\\7t.wfb"," bmp\\7a.wfb","bmp\\7t.wfb","bmp\\79.wfb"},{"bmp\\81.wfb","bmp\\8a.wfb","bmp\\8t.wfb","bmp\\84.wfb","bmp\\85.wfb","bmp\\86.wfb"," bmp\\8t.wfb","bmp\\8a.wfb","bmp\\89.wfb"},{"bmp\\91.wfb","bmp\\9t.wfb","bmp\\9t.wfb","bmp\\9t.wfb","bmp\\95.wfb","bmp\\9t.wfb"," bmp\\9t.wfb","bmp\\9t.wfb","bmp\\99.wfb"},{"bmp\\101.wfb","bmp\\102.wfb","bmp\\102.wfb","bmp\\104.wfb","bmp\\105.wfb","bmp\\10 6.wfb","bmp\\108.wfb","bmp\\108.wfb","bmp\\109.wfb"}};char cursor[14][14]={0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,255,255,255,255,255,255,255,0,0,1,1,1,1,0,255,255,255,255,255,255,0,0,1,1,1,1,1,0,255,255,255,255,255,255,0,0,1,1,1,1,1,0,255,255,255,255,255,255,255,0,0,1,1,1,1,0,255,255,255,255,255,255,255,255,0,0,1,1,1,0,255,255,255,255,255,255,255,255,255,0,0,1,1,0,255,255,0,255,255,255,255,255,255,255,0,0,1,0,255,0,1,1,0,255,255,255,255,255,255,255,0,0,0,1,1,1,1,0,255,255,255,255,255,0,1,0,1,1,1,1,1,1,0,255,255,255,0,1,1,1,1,1,1,1,1,1,1,0,255,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1};struct pos{int x;int y;}position[10][9],redcurpos,redtemppos,redoldpos,blackcurpos,blacktemppos,blackoldpos; /*----------------------------------------------------*/selectpage(register char page) /*换页函数*/{union REGS r;r.x.ax=0x4f05;r.x.bx=0;r.x.dx=page; /*选择页面*/int86(0x10,&r,&r);}unsigned char set_SVGA_mode(int vmode) /*设置SVGA屏幕模式*/{union REGS r;r.x.ax=0x4f02;r.x.bx=vmode;int86(0x10,&r,&r);return(r.h.ah);}unsigned int get_SVGA_mode() /*获取当前SVGA屏幕模式*/{union REGS r;r.x.ax=0x4f03;int86(0x10,&r,&r);return(r.x.bx);}drawbmp(int start_x,int start_y,char filename[]){char buffer[640];int i,j,k,n,r,g,b,width,length;long position;FILE *fp;if((fp=fopen(filename,"rb"))==NULL){printf("Error! Can't open file!");getch();return;}fseek(fp,28,SEEK_SET);fread(&i,2,1,fp);if(i!=8) /*检查是否为256色位图*/{puts("Error!Can't find bitmap!");fclose(fp);getch();exit(0);}fseek(fp,18,SEEK_SET);fread(&width,4,1,fp);fread(&length,4,1,fp);if(firsttime){fseek(fp,54,SEEK_SET);for(i=0;i<256;i++) /*按照该图片的DAC色表设置色彩寄存器*/{b=fgetc(fp);g=fgetc(fp);r=fgetc(fp); /*获取R、G、B分量*/outportb(0x3c8,i);outportb(0x3c9,r>>2); /*右移是要转化为VGA的6位寄存器形式*/ outportb(0x3c9,g>>2);outportb(0x3c9,b>>2);fgetc(fp);}}elsefseek(fp,300,SEEK_SET);k=(width%4)?(4-width%4):0; /*宽度修正值*/for(j=length-1+start_x;j>=start_x;j--){fread(buffer,width,1,fp);for(i=start_y,n=0;i<width+start_y;i++,n++){position=j*640l+i; /*计算要显示点的显存位置*/page_new=position/65536; /*计算显示页*/if(page_new!=page_old) /*当显示页不同时更换页面,提高一定的输出速度*/ {selectpage(page_new);page_old=page_new;}pokeb(0xa000,position%65536,buffer[n]); /*写到显存位置*/}fseek(fp,k,SEEK_CUR); /*每行绘制完后修正宽度*/}fclose(fp);}init(){savemode=get_SVGA_mode(); /*先保存原来的屏幕模式*/set_SVGA_mode(0x101); /*硬件无关性初始化屏幕为640*480 256色模式*/}end(){set_SVGA_mode(savemode); /*恢复屏幕*/}/*----------------------------------------------------*/initpos(){int i,j;for(i=0;i<10;i++)for (j=0;j<9;j++){position[i][j].x=35+i*39;position[i][j].y=43+j*40;}}initchessmap(){board[0][0]=BLACK_JU;board[0][1]=BLACK_MA;board[0][2]=BLACK_XIANG;board[0][3]=BLACK_SHI;board[0][4]=BLACK_JIANG;board[0][5]=BLACK_SHI;board[0][6]=BLACK_XIANG;board[0][7]=BLACK_MA;board[0][8]=BLACK_JU;board[2][1]=BLACK_PAO;board[2][7]=BLACK_PAO;board[3][0]=BLACK_BIN;board[3][2]=BLACK_BIN;board[3][4]=BLACK_BIN;board[3][6]=BLACK_BIN;board[3][8]=BLACK_BIN;board[9][0]=RED_JU;board[9][1]=RED_MA;board[9][2]=RED_XIANG;board[9][3]=RED_SHI;board[9][4]=RED_JIANG;board[9][5]=RED_SHI;board[9][6]=RED_XIANG;board[9][7]=RED_MA;board[9][8]=RED_JU;board[7][1]=RED_PAO;board[7][7]=RED_PAO;board[6][0]=RED_BIN;board[6][2]=RED_BIN;board[6][4]=RED_BIN;board[6][6]=RED_BIN;board[6][8]=RED_BIN;}initdrawchess(){int i,j;;for(i=0;i<10;i++)for(j=0;j<9;j++){if(board[i][j])drawbmp(position[i][j].x,position[i][j].y,chessfile[board[i][j]]);}}drawcursor(struct pos p){int i,j,n,m,x,y;long thisposition;x=position[p.x][p.y].x+20;y=position[p.x][p.y].y+25;for(j=13-1+x,m=13;j>=x;j--,m--){for(i=y,n=0;i<13+y;i++,n++){thisposition=j*640l+i; /*计算要显示点的显存位置*/page_new=thisposition/65536; /*计算显示页*/if(page_new!=page_old) /*当显示页不同时更换页面,提高一定的输出速度*/{selectpage(page_new);page_old=page_new;}if(cursor[m][n]!=1)if(cursor[m][n]==0)pokeb(0xa000,thisposition%65536,0);elseif(turn==RED)pokeb(0xa000,thisposition%65536,153);elsepokeb(0xa000,thisposition%65536,255);}}}drawselecursor(struct pos p){int i,j,n,m,x,y;long thisposition;x=position[p.x][p.y].x+20;y=position[p.x][p.y].y+25;for(j=13-1+x,m=13;j>=x;j--,m--){for(i=y,n=0;i<13+y;i++,n++){thisposition=j*640l+i; /*计算要显示点的显存位置*/page_new=thisposition/65536; /*计算显示页*/if(page_new!=page_old) /*当显示页不同时更换页面,提高一定的输出速度*/{selectpage(page_new);page_old=page_new;}if(cursor[m][n]!=1)pokeb(0xa000,thisposition%65536,0);}}}/*----------------------------------------------------*/int getkey(){int press;while(bioskey(1) == 0);press=bioskey(0);press=press&0xff00;return(press);}/*--------------------红方操作--------------------*/int redcanselect(){int x,y;x=redcurpos.x;y=redcurpos.y;if(board[x][y]>=RED_JU&&board[x][y]<=RED_BIN)return 1;elsereturn 0;}int redcanmove(){int i,j,min,max,oldx,oldy,x,y;oldx=redoldpos.x;oldy=redoldpos.y;x=redcurpos.x;y=redcurpos.y;/*case1 目标位置是否是自己人*/if(board[x][y]>=RED_JU&&board[x][y]<=RED_BIN)return 0;/* 军、马、炮、相、士、将、卒的走法正确性的判断*/switch(board[oldx][oldy]){case RED_BIN: /*完成*/ if(oldx>=5){ if(y!=oldy||(oldx-x)!=1) return 0;}else{ if(x==(oldx-1)&&y==oldy) return 1;elseif(x==oldx&&y==(oldy+1)) return 1;elseif(x==oldx&&y==(oldy-1)) return 1;elsereturn 0;}break;case RED_JIANG: /*完成*/ if(x!=oldx&&y!=oldy) return 0;if(x!=oldx)if((x-oldx)>1||(oldx-x)>1) return 0;else if(x<7) return 0;else if(y!=oldy)if((y-oldy)>1||(oldy-y)>1) return 0;else if(y<3||y>5) return 0;break;case RED_JU: /*完成*/ if(x!=oldx&&y!=oldy) return 0;else if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0) return 0;}break;case RED_MA: /*完成*/ if((x-oldx)==2&&((y-oldy)==1||(oldy-y)==1)){if(board[oldx+1][oldy]!=0) return 0;}elseif((oldx-x)==2&&((y-oldy)==1||(oldy-y)==1)){if(board[oldx-1][oldy]!=0) return 0;}elseif((y-oldy)==2&&((x-oldx)==1||(oldx-x)==1)){if(board[oldx][oldy+1]!=0) return 0;}elseif((oldy-y)==2&&((x-oldx)==1||(oldx-x)==1)){if(board[oldx][oldy-1]!=0) return 0;}elsereturn 0;break;case RED_PAO: /*完成*/ if(x!=oldx&&y!=oldy) return 0;if(board[x][y]==0){if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0) return 0;}}else{if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1,j=0;i<max;i++)if(board[i][y]!=0) j++;if(j!=1) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+1,j=0;i<max;i++)if(board[x][i]!=0) j++;if(j!=1) return 0;}}break;case RED_SHI: /*完成*/ if(oldx==9||oldx==7){if(x!=8||y!=4) return 0;}else if(oldx==8){if(x==9&&y==3) return 1;elseif(x==9&&y==5) return 1;elseif(x==7&&y==3) return 1;elseif(x==7&&y==5) return 1;else return 0;}else return 0;break;case RED_XIANG: /*完成*/if(x<5) return 0;if(x!=oldx&&y!=oldy){if((x-oldx)==2&&(y-oldy)==2){i=oldx+1;j=oldy+1;}else if((x-oldx)==2&&(oldy-y)==2){i=oldx+1;j=oldy-1;}else if((oldx-x)==2&&(y-oldy)==2){i=oldx-1;j=oldy+1;}else if((oldx-x)==2&&(oldy-y)==2){i=oldx-1;j=oldy-1;}else return 0;if(board[i][j]!=0) return 0;}else return 0;break;}return 1;}redup(){int x,y,n;if(redcurpos.x>0){redcurpos.x--;x=position[redtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]==0)drawbmp(x,y,boardfile[redtemppos.x][redtemppos.y]);else if(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)) {n=board[redtemppos.x][redtemppos.y];drawbmp(x,y,chessfile[n]);}if(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)drawselecursor(redoldpos);drawcursor(redcurpos);redtemppos.x=redcurpos.x;redtemppos.y=redcurpos.y;}}reddown(){int x,y,n;if(redcurpos.x<9){redcurpos.x++;x=position[redtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]==0)drawbmp(x,y,boardfile[redtemppos.x][redtemppos.y]);else if(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)) {n=board[redtemppos.x][redtemppos.y];drawbmp(x,y,chessfile[n]);}if(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)drawselecursor(redoldpos);drawcursor(redcurpos);redtemppos.x=redcurpos.x;redtemppos.y=redcurpos.y;}}redleft(){int x,y,n;if(redcurpos.y>0){redcurpos.y--;x=position[redtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]==0)drawbmp(x,y,boardfile[redtemppos.x][redtemppos.y]);else if(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)) {n=board[redtemppos.x][redtemppos.y];drawbmp(x,y,chessfile[n]);}if(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)drawselecursor(redoldpos);drawcursor(redcurpos);redtemppos.x=redcurpos.x;redtemppos.y=redcurpos.y;}}redright(){int x,y,n;if(redcurpos.y<8){redcurpos.y++;x=position[redtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]==0)drawbmp(x,y,boardfile[redtemppos.x][redtemppos.y]);else if(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)) {n=board[redtemppos.x][redtemppos.y];drawbmp(x,y,chessfile[n]);}if(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)drawselecursor(redoldpos);drawcursor(redcurpos);redtemppos.x=redcurpos.x;redtemppos.y=redcurpos.y;}}reddo(){int i,j,x,y,n;if(redstate==SELECT&&redcanselect()){if(board[redcurpos.x][redcurpos.y]<=RED&&board[redcurpos.x][redcurpos.y]>0) {redstate=MOVE;drawselecursor(redcurpos);redoldpos.x=redcurpos.x;redoldpos.y=redcurpos.y;}}else if(redstate==MOVE&&redcanmove()){x=position[redoldpos.x][redoldpos.y].x;y=position[redoldpos.x][redoldpos.y].y;drawbmp(x,y,boardfile[redoldpos.x][redoldpos.y]);x=position[redcurpos.x][redcurpos.y].x;y=position[redcurpos.x][redcurpos.y].y;n=board[redoldpos.x][redoldpos.y];drawbmp(x,y,chessfile[n]);if(board[redcurpos.x][redcurpos.y]==BLACK_JIANG){winner=RED;finish=1;return;}board[redcurpos.x][redcurpos.y]=n;board[redoldpos.x][redoldpos.y]=0;for(i=0;i<=2;i++)for(j=3;j<=5;j++)if(board[i][j]==BLACK_JIANG){x=i;y=j;}for(i=x+1,j=y,n=0;i<=9;i++){if(board[i][j]==RED_JIANG&&n==0){winner=BLACK;finish=1;break;}else if(board[i][j]!=0) n++;}turn=BLACK;blackstate=SELECT;drawcursor(blackcurpos);drawbmp(30,438,"bmp\\bzq.wfb"); /*转交控制权给黑方*/}}redundo(){int x,y,n;if(redstate==MOVE){x=position[redoldpos.x][redoldpos.y].x;y=position[redoldpos.x][redoldpos.y].y;n=board[redoldpos.x][redoldpos.y];drawbmp(x,y,chessfile[n]);redoldpos.x=redcurpos.x;redoldpos.y=redcurpos.y;drawcursor(redcurpos);redstate=SELECT;}}/*--------------------黑方操作----------------------*/int blackcanselect(){int x,y;x=blackcurpos.x;y=blackcurpos.y;if(board[x][y]>=BLACK_JU&&board[x][y]<=BLACK_BIN)return 1;elsereturn 0;}int blackcanmove(){int i,j,min,max,oldx,oldy,x,y;oldx=blackoldpos.x;oldy=blackoldpos.y;x=blackcurpos.x;y=blackcurpos.y;/*case1 目标位置是否是自己人*/if(board[x][y]>=BLACK_JU&&board[x][y]<=BLACK_BIN)return 0;/* 军、马、炮、相、士、将、卒的走法正确性的判断*/ switch(board[oldx][oldy]){case BLACK_JU: /*完成*/ if(x!=oldx&&y!=oldy) return 0;else if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0) return 0;}break;case BLACK_MA: /*完成*/ if((x-oldx)==2&&((y-oldy)==1||(oldy-y)==1)){if(board[oldx+1][oldy]!=0) return 0;}elseif((oldx-x)==2&&((y-oldy)==1||(oldy-y)==1)){if(board[oldx-1][oldy]!=0) return 0;}elseif((y-oldy)==2&&((x-oldx)==1||(oldx-x)==1)){if(board[oldx][oldy+1]!=0) return 0;}elseif((oldy-y)==2&&((x-oldx)==1||(oldx-x)==1)){if(board[oldx][oldy-1]!=0) return 0;}elsereturn 0;break;case BLACK_PAO: /*完成*/ if(x!=oldx&&y!=oldy) return 0;if(board[x][y]==0){if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0) return 0;}}else{j=0;if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0) j++;if(j!=1) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0) j++;if(j!=1) return 0;}}break;case BLACK_XIANG:if(x>4) return 0;if(x!=oldx&&y!=oldy){if((x-oldx)==2&&(y-oldy)==2){i=oldx+1;j=oldy+1;}else if((x-oldx)==2&&(oldy-y)==2){i=oldx+1;j=oldy-1;}else if((oldx-x)==2&&(y-oldy)==2){i=oldx-1;j=oldy+1;}else if((oldx-x)==2&&(oldy-y)==2){i=oldx-1;j=oldy-1;}else return 0;if(board[i][j]!=0) return 0;}else return 0;break;case BLACK_SHI:if(oldx==0||oldx==2){if(x!=1||y!=4) return 0;}else if(oldx==1){if(x==0&&y==3) return 1;elseif(x==0&&y==5) return 1;elseif(x==2&&y==3) return 1;elseif(x==2&&y==5) return 1;else return 0;}else return 0;break;case BLACK_JIANG:if(x!=oldx&&y!=oldy) return 0;if(x!=oldx)if((x-oldx)>1||(oldx-x)>1) return 0;else if(x>2) return 0;else if(y!=oldy)if((y-oldy)>1||(oldy-y)>1) return 0;else if(y<3||y>5) return 0;break;case BLACK_BIN:if(oldx<=4){ if(y!=oldy||(x-oldx)!=1) return 0;}else{ if(x==(oldx+1)&&y==oldy) return 1;elseif(x==oldx&&y==(oldy+1)) return 1;elseif(x==oldx&&y==(oldy-1)) return 1;elsereturn 0;}break;}return 1;}blackup(){int x,y,n;if(blackcurpos.x>0)blackcurpos.x--;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]);elseif(!(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;}}blackdown(){int x,y,n;if(blackcurpos.x<9){blackcurpos.x++;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]);elseif(!(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;}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]);elseif(!(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]);elseif(!(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=0;i<=2;i++)for(j=3;j<=5;j++)if(board[i][j]==BLACK_JIANG){x=i;y=j;}for(i=x+1,j=y,n=0;i<=9;i++){if(board[i][j]==RED_JIANG&&n==0){winner=RED;finish=1;break;}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(); break;case 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=1;break;}}}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();}。
中国象棋游戏源代码
using System;using System.Collections.Generic; using ponentModel; using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace象棋{enum player{blank,red,blue,};enum chesstype{blank,jiang,che,ma,pao,xiang,zu,shi};struct chess{public player side;public chesstype type;};//下载于struct block{public PictureBox container;public chess item;};public partial class Form1 : Form{public Form1(){InitializeComponent();pictureboxlist = new List<PictureBox>(81); Matrix=new block[10][];int i,j;for (i = 0; i < 10;i++ ){Matrix[i] = new block[9];}for(i=0;i<10;i++){for(j=0;j<9;j++){Control[] col =this.Controls.Find("pictureBox" + (i*9+j+1), false); Matrix[i][j].container=col[0] as PictureBox;Matrix[i][j].container.Location = new Point(60 * j, 60 * i);}}redcoll = new collecter();bluecool = new collecter();for (i = 91; i < 107;i++ ){Control[] col =this.Controls.Find("pictureBox" + i, false);bluecool.add(col[0] as PictureBox);}for (i = 107; i < 123;i++ ){Control[] col =this.Controls.Find("pictureBox" + i, false);redcoll.add(col[0] as PictureBox);}resetground();}List<PictureBox> pictureboxlist;block[][] Matrix;collecter redcoll;collecter bluecool;int chozenX;int chozenY;player currentside;bool beenchozen;bool clickswitch;private void click1(object sender, EventArgs e) {if(!clickswitch){resetground();return;}string name = (sender as PictureBox).Name;string number = name.Substring(10);int index = Convert.ToInt32(number);int i,j;bool flag = false;i=(index-1)/9;j=(index-1)%9;//下载于if (beenchozen){Matrix[chozenX][chozenY].container.BorderStyle = BorderStyle.None;Matrix[chozenX][chozenY].container.BackColor =Color.Transparent;beenchozen = false;if(Matrix[chozenX][chozenY].item.side==Matrix[i][j].it em.side){return;}if(Matrix[chozenX][chozenY].item.side != player.blank) {if(Matrix[i][j].item.type== chesstype.jiang){flag=true;}if(!movechess(i, j)){return;}if(flag){if (currentside == player.red) {MessageBox.Show("红方胜利!点击任意处重新开局");}else{MessageBox.Show("蓝方胜利!点击任意处重新开局");}clickswitch = false;}}if (currentside == player.red){currentside = player.blue;label1.Text = "蓝方";label1.ForeColor = Color.Blue;}else{currentside = player.red;label1.Text = "红方";label1.ForeColor = Color.Red;}}else if(Matrix[i][j].item.side== currentside){Matrix[i][j].container.BorderStyle = BorderStyle.FixedSingle;Matrix[i][j].container.BackColor = Color.Brown;chozenX = i;chozenY = j;beenchozen = true;}}private void resetground(){int i, j;for (i = 0; i < 10;i++ ){for(j=0;j<9;j++){Matrix[i][j].container.Image = null;Matrix[i][j].item.side = player.blank;Matrix[i][j].item.type = chesstype.blank;}}beenchozen = false;clickswitch = true;currentside = player.red;label1.Text = "红方";label1.ForeColor = Color.Red;redcoll.clear();bluecool.clear();Matrix[0][0].container.Image = global::象棋.Properties.Resources.蓝车;Matrix[0][1].container.Image = global::象棋.Properties.Resources.蓝马;Matrix[0][2].container.Image = global::象棋.Properties.Resources.蓝象;Matrix[0][3].container.Image = global::象棋.Properties.Resources.蓝士;Matrix[0][4].container.Image = global::象棋.Properties.Resources.蓝将;Matrix[0][5].container.Image = global::象棋.Properties.Resources.蓝士;Matrix[0][6].container.Image = global::象棋.Properties.Resources.蓝象;Matrix[0][7].container.Image = global::象棋.Properties.Resources.蓝马;Matrix[0][8].container.Image = global::象棋.Properties.Resources.蓝车;Matrix[2][1].container.Image = global::象棋.Properties.Resources.蓝炮;Matrix[2][7].container.Image = global::象棋.Properties.Resources.蓝炮;Matrix[3][0].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][2].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][4].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][6].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][8].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[6][0].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][2].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][4].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][6].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][8].container.Image = global::象棋.Properties.Resources.红卒;Matrix[7][1].container.Image = global::象棋.Properties.Resources.红炮;Matrix[7][7].container.Image = global::象棋.Properties.Resources.红炮;Matrix[9][0].container.Image = global::象棋.Properties.Resources.红车;Matrix[9][1].container.Image = global::象棋.Properties.Resources.红马;Matrix[9][2].container.Image = global::象棋.Properties.Resources.红象;Matrix[9][3].container.Image = global::象棋.Properties.Resources.红士;Matrix[9][4].container.Image = global::象棋.Properties.Resources.红将;Matrix[9][5].container.Image = global::象棋.Properties.Resources.红士;Matrix[9][6].container.Image = global::象棋.Properties.Resources.红象;Matrix[9][7].container.Image = global::象棋.Properties.Resources.红马;Matrix[9][8].container.Image = global::象棋.Properties.Resources.红车;//下载于Matrix[0][0].item.side = player.blue;Matrix[0][1].item.side = player.blue;Matrix[0][2].item.side = player.blue;Matrix[0][3].item.side = player.blue;Matrix[0][4].item.side = player.blue;Matrix[0][5].item.side = player.blue;Matrix[0][6].item.side = player.blue;Matrix[0][7].item.side = player.blue;Matrix[0][8].item.side = player.blue;Matrix[2][1].item.side = player.blue;Matrix[2][7].item.side = player.blue;Matrix[3][0].item.side = player.blue;Matrix[3][2].item.side = player.blue;Matrix[3][4].item.side = player.blue;Matrix[3][6].item.side = player.blue;Matrix[3][8].item.side = player.blue;Matrix[6][0].item.side = player.red;Matrix[6][2].item.side = player.red;Matrix[6][4].item.side = player.red;Matrix[6][6].item.side = player.red;Matrix[6][8].item.side = player.red;Matrix[7][1].item.side = player.red;Matrix[7][7].item.side = player.red;Matrix[9][0].item.side = player.red;Matrix[9][1].item.side = player.red;Matrix[9][2].item.side = player.red;Matrix[9][3].item.side = player.red;Matrix[9][4].item.side = player.red;Matrix[9][5].item.side = player.red;Matrix[9][6].item.side = player.red;Matrix[9][7].item.side = player.red;Matrix[9][8].item.side = player.red;Matrix[0][0].item.type = chesstype.che; Matrix[0][1].item.type = chesstype.ma;Matrix[0][2].item.type = chesstype.xiang; Matrix[0][3].item.type = chesstype.shi; Matrix[0][4].item.type = chesstype.jiang; Matrix[0][5].item.type = chesstype.shi; Matrix[0][6].item.type = chesstype.xiang; Matrix[0][7].item.type = chesstype.ma;Matrix[0][8].item.type = chesstype.che; Matrix[2][1].item.type = chesstype.pao; Matrix[2][7].item.type = chesstype.pao; Matrix[3][0].item.type = chesstype.zu;Matrix[3][2].item.type = chesstype.zu;Matrix[3][4].item.type = chesstype.zu;Matrix[3][6].item.type = chesstype.zu;Matrix[3][8].item.type = chesstype.zu;Matrix[6][0].item.type = chesstype.zu;Matrix[6][2].item.type = chesstype.zu;Matrix[6][4].item.type = chesstype.zu;Matrix[6][6].item.type = chesstype.zu;Matrix[6][8].item.type = chesstype.zu;Matrix[7][1].item.type = chesstype.pao;Matrix[7][7].item.type = chesstype.pao;Matrix[9][0].item.type = chesstype.che;Matrix[9][1].item.type = chesstype.ma;Matrix[9][2].item.type = chesstype.xiang; Matrix[9][3].item.type = chesstype.shi;Matrix[9][4].item.type = chesstype.jiang; Matrix[9][5].item.type = chesstype.shi;Matrix[9][6].item.type = chesstype.xiang; Matrix[9][7].item.type = chesstype.ma;Matrix[9][8].item.type = chesstype.che;}private bool movechess(int X,int Y){int i, j, k, n=0;switch(Matrix[chozenX][chozenY].item.type) {case chesstype.che:if(chozenX==X){i = chozenY < Y ? chozenY : Y; j = chozenY > Y ? chozenY : Y;for(k=i+1;k<j;k++){if(Matrix[X][k].item.side!= player.blank){return false;}}}if (chozenY == Y){i = chozenX < X ? chozenX : X; j = chozenX > X ? chozenX : X;for (k = i + 1; k < j; k++){if(Matrix[k][Y].item.side != player.blank){return false;}}}setmove(X, Y);return true;case chesstype.jiang:if(Matrix[X][Y].item.type== chesstype.jiang&&chozenY==Y){i = chozenX < X ? chozenX : X; j = chozenX > X ? chozenX : X;for (k = i + 1; k < j; k++){if(Matrix[k][Y].item.side != player.blank){return false;}}setmove(X, Y);return true;}if(Matrix[chozenX][chozenY].item.side== player.blue){if(Y<3||Y>5||X>2){return false;}}else{if(Y<3||Y>5||X<7){return false;}}if((chozenX-X)*(chozenX-X)+(chozenY-Y)*(chozenY-Y)!=1) {return false;}setmove(X, Y);return true;case chesstype.ma:if (Math.Abs(chozenX - X) == 1 && Math.Abs(chozenY - Y) == 2){if (Matrix[chozenX][chozenY +(Y - chozenY) / Math.Abs(Y - chozenY)].item.side!= player.blank){return false;}}else if (Math.Abs(chozenX - X) == 2 && Math.Abs(chozenY - Y) == 1){if (Matrix[chozenX + (X - chozenX) / Math.Abs(X - chozenX)][chozenY].item.side != player.blank){return false;}}else{return false;}setmove(X, Y);return true;case chesstype.pao: n = 0;if(chozenX==X){i = chozenY < Y ? chozenY : Y; j = chozenY > Y ? chozenY : Y; n = 0;for(k=i+1;k<j;k++){if(Matrix[X][k].item.side!= player.blank){n++;}}if(n>1){return false;}}else if (chozenY == Y){i = chozenX < X ? chozenX : X; j = chozenX > X ? chozenX : X; n = 0;for (k = i + 1; k < j; k++){if(Matrix[k][Y].item.side != player.blank){n++;}}if (n > 1){return false;}}else{return false;}if(n==0&&Matrix[X][Y].item.side!= player.blank){return false;}if(n==1&&Matrix[X][Y].item.side== player.blank){return false;}setmove(X, Y);return true;case chesstype.shi:if(Matrix[chozenX][chozenY].item.side== player.blue){if(Y<3||Y>5||X>2){return false;}}else{if(Y<3||Y>5||X<7){return false;}}if(Math.Abs(X-chozenX)!=1||Math.Abs(chozenY-Y)!=1){return false;}setmove(X, Y);return true;case chesstype.xiang:if(Matrix[chozenX][chozenY].item.side == player.blue){if (X>4){return false;}}else{if (X<5){return false;}}if ((X - chozenX) * (X - chozenX) + (chozenY - Y) * (chozenY - Y) != 8){return false;}if(Matrix[(X+chozenX)/2][(Y+chozenY)/2].item.side!=player.blank){return false;}setmove(X, Y);return true;case chesstype.zu:if(X!=chozenX&&Y!=chozenY){return false;}if(Matrix[chozenX][chozenY].item.side == player.blue) {if (chozenX<5&&X-chozenX!=1) {return false;}if(chozenX>4){if(X==chozenX&&Math.Abs(Y-chozenY)!=1){return false;}if(Y==chozenY&&X-chozenX!=1){return false;}}}else{if (chozenX>4&&chozenX-X!=1) {return false;}if (chozenX <5){if (X == chozenX && Math.Abs(Y - chozenY) != 1){return false;}if (Y == chozenY && chozenX-X != 1){return false;}}}setmove(X,Y);return true;}return false;}private void setmove(int X,int Y){if (Matrix[X][Y].item.side== player.red) {redcoll.push(Matrix[X][Y].container.Image);}else if (Matrix[X][Y].item.side == player.blue){bluecool.push(Matrix[X][Y].container.Image);}Matrix[X][Y].container.Image =Matrix[chozenX][chozenY].container.Image;Matrix[X][Y].item =Matrix[chozenX][chozenY].item;Matrix[chozenX][chozenY].container.Image = null;Matrix[chozenX][chozenY].item.side = player.blank;Matrix[chozenX][chozenY].item.type= chesstype.blank;}}class collecter{public PictureBox[] container;public int number;public int chessnum;public collecter(){number = 0;container = new PictureBox[16];chessnum = 0;}public void add(PictureBox box).{container[number++] = box;}public void push(Image ima){container[chessnum++].BackgroundImage = ima;}public void clear(){for (int i = 0; i < chessnum; i++){container[i].BackgroundImage = null; }chessnum = 0;}};}.。
中国象棋游戏代码
中国象棋游戏代码#include<stdio.h>#include<stdlib.h>#include<string.h>char x, y, z;int i, i1, j, j1, k, k1, ii, jj;int f(char X){if (X == '0')printf(" ");else{if (X >= 'a' || X <= 'g'){switch (X - 'a'){case 6:printf("兵");break;case 5:printf("包");break;case 4:printf("帅");break;case 3:printf("士");break;case 2:printf("相");break;case 1:printf("马");break;case 0:printf("车");}}if (X <= 'A' || X <= 'G'){switch (X - 'A'){case 6:printf("卒");break;case 5:printf("炮");break;case 4:printf("将");break;case 3:printf("仕");break;case 2:printf("象");break;case 1:printf("馬");break;case 0:printf("車");}}}}char f2(char **str2, int q){int m, n, o, p;char x1, x2, y2, y1, z1;if (q == 1)printf("\n轮到红子动!\n");elseprintf("\n轮到黑子动!\n");for (;;){if (q == 1)printf("\n攻(红)方:车(a) 马(b)\n");elseprintf("\n黑(守)方:将(E)\n");lm1:printf("请选择棋子代号:");scanf("%c", &x);scanf("%c", &x);if (q == 1 && x != 'a' && x != 'b'){printf("请按要求输入!\n");goto lm1;}if (q == 0 && x != 'E'){printf("请按要求输入!\n");goto lm1;}printf("(欲换棋子输入'0 0')请选择该子所在位置(行与列):");scanf("%d%d", &i, &j);if (i == 0 && j == 0)goto lm1;for (;;){if (i < 1 || i > 3 || j < 1 || j > 3){printf("请认真输入,位置应在棋盘内!\n再次输入:");scanf("%d%d", &i, &j);}elsebreak;}i--;j--;if (*(*(str2 + j) + i) == x)break;elseprintf("该位置没有该棋子!请认真输入!\n");}printf("请输入要下的位置(移动后位置):");label1:scanf("%d%d", &i1, &j1);for (;;){if (i1 < 1 || i1 > 3 || j1 < 1 || j1 > 3){printf("请认真输入,位置应在棋盘内!\n再次输入:");scanf("%d%d", &i1, &j1);}elsebreak;}i1--;j1--;y = *(*(str2 + j1) + i1);if (q == 1 && (y == 'a' || y == 'b')){printf("移动后位置有己方棋子\n重新输入下的位置:");goto label1;}else{if (i1 == i && j1 == j){printf("原地未动\n重新输入下的位置:");goto label1;}else{if (x == 'a'){if (i1 != i && j1 != j){printf("'车'应直走!\n重新输入下的位置:");goto label1;}else{if (i1 == i){if (j1 > j)for (k = j + 1; k < j1; k++){if (*(*(str2 + k) + i) != '0'){printf("中间有子挡\n重新输入下的位置:");goto label1;}}else{for (k = j1 + 1; k < j; k++){if (*(*(str2 + k) + i) != '0'){printf("中间有子挡\n重新输入下的位置:");goto label1;}}}}else{if (i1 > i)for (k = i + 1; k < i1; k++){if (*(*(str2 + j) + k) != '0'){printf("中间有子挡\n重新输入下的位置:");goto label1;}}else{for (k = i1 + 1; k < i; k++){if (*(*(str2 + j) + k) != '0'){printf("中间有子挡\n重新输入下的位置:");goto label1;}}}}}if (i1 != ii && j1 != jj){if (ii == 0){if ((jj == 0 || jj == 2)&& (*(*(str2 + 1) + 2) != 'b'|| ((*(*str2 + 1) + 2) == 'b' && *(*(str2 + 1) + 1) != '0'))) {printf("未将军!\n");goto lm1;}if (jj == 1&& (*(*(str2 + 0) + 2) != 'b'|| (*(*(str2 + 0) + 2) == 'b' && *(*(str2) + 1) != '0'))&& (*(*(str2 + 2) + 2) != 'b'|| (*(*(str2 + 2) + 2) == 'b' && *(*(str2 + 2) + 1) != '0'))) {printf("未将军!\n");goto lm1;}}if (ii == 1 && jj == 0&& (*(*(str2 + 2)) != 'b'|| (*(*(str2 + 2)) == 'b' && *(*(str2 + 1)) != '0'))&& (*(*(str2 + 2) + 2) != 'b'|| (*(*(str2 + 2) + 2) == 'b' && *(*(str2 + 1) + 2) != '0'))) {printf("未将军!\n");goto lm1;}if (ii == 1 && jj == 2&& (**str2 != 'b' || (**str2 == 'b' && *(*(str2 + 1)) != '0'))&& (*(*(str2) + 2) != 'b'|| (*(*(str2) + 2) == 'b' && *(*(str2 + 1) + 2) != '0'))) {printf("未将军!\n");goto lm1;}if (ii == 2){if ((jj == 0 || jj == 2)&& (*(*(str2 + 1)) != 'b'|| ((*(*str2 + 1)) == 'b' && *(*(str2 + 1) + 1) != '0'))) {printf("未将军!\n");goto lm1;}if (jj == 1&& (*(*(str2 + 0)) != 'b'|| (*(*(str2 + 0)) == 'b' && *(*(str2) + 1) != '0'))&& (*(*(str2 + 2)) != 'b'|| (*(*(str2 + 2)) == 'b' && *(*(str2 + 2) + 1) != '0'))) {printf("未将军!\n");goto lm1;}}}}if (x == 'b'){if (i1 == i + 1 || i1 == i - 1){if (j1 == j + 2){if (*(*(str2 + j + 1) + i) != '0'){printf("'马'撇腿!\n重新输入下的位置:");goto label1;}else{if (((i1 != ii + 1 || i1 != ii - 1)&& (j1 != jj + 2 || j1 != jj - 2)) && ((j1 != jj + 1|| j1 != jj - 1)&& (i1 != ii + 2|| i1 !=ii - 2))&& y != 'E'){printf("未将军\n");goto lm1;}goto la1;}}else{if (j1 == j - 2){if (*(*(str2 + j - 1) + i) != '0'){printf("'马'撇腿!\n重新输入下的位置:");goto label1;}else{if (((i1 != ii + 1 || i1 != ii - 1)&& (j1 != jj + 2 || j1 != jj - 2))&& ((j1 != jj + 1 || j1 != jj - 1)&& (i1 != ii + 2 || i1 != ii - 2)) && y != 'E'){printf("未将军\n");goto lm1;}goto la1;}}else{printf("'马'应走'日'\n重新输入下的位置:");goto label1;}}}else{if (i1 == i + 2 || i1 == i - 2){if (j1 == j + 1 || j1 == j - 1){if (*(*(str2 + j) + (i1 + i) / 2) != '0'){printf("'马'撇腿!\n重新输入下的位置:");goto label1;}else{if (((i1 != ii + 1 || i1 != ii - 1)&& (j1 != jj + 2 || j1 != jj - 2))&& ((j1 != jj + 1 || j1 != jj - 1)&& (i1 != ii + 2 || i1 != ii - 2)) && y != 'E'){printf("未将军\n");goto lm1;}goto la1;}}else{printf("'马'应走'日'\n重新输入下的位置:");goto label1;}}else{printf("'马'应走'日'\n重新输入下的位置:");goto label1;}}}if (x == 'E'){ii = i1;jj = j1;if ((i1 == i + 1 || i1 == i - 1) && j1 == j)goto la1;else{if (i1 == i && (j1 == j + 1 || j1 == j - 1))goto la1;else{printf("'帅'一次只(直)走一格\n重新输入下的位置:");goto label1;}}}}la1:*(*(str2 + j1) + i1) = x;*(*(str2 + j) + i) = '0';printf("\n");for (m = 0; m < 3; m++){for (n = 0; n < 3; n++){o = *(*(str2 + n) + m);f(o);}printf("\n\n");}}}char f1(char **str, int q){int m, n, o, p, o3;char x1, x2, y2, y1, z1;if (q == 1){x1 = 'a';x2 = 'A';y1 = 'g';y2 = 'G';o3 = 7;}else{x1 = 'A';x2 = 'a';y1 = 'G';y2 = 'g';o3 = 2;}if (q == 1)printf("\n轮到红子动!\n");elseprintf("\n轮到黑子动!\n");for (;;){if (q == 1)printf("\n红方:车(a) 马(b) 相(c) 士(d) 帅(e) 包(f) 兵(g)\n");elseprintf("\n黑方:車(A) 馬(B) 象(C) 仕(D) 将(E) 炮(F) 卒(G)\n"); lm:printf("请选择棋子代号:");scanf("%c", &x);scanf("%c", &x);if (x >= x2 && x <= y2){printf("请不要动对方棋子\n");goto lm;}else if (x > y1 || x < x1){printf("输入不合要求!\n");goto lm;}printf("(欲换棋子输入'0 0')请选择该子所在位置(行与列):");scanf("%d%d", &i, &j);if (i == 0 && j == 0)goto lm;for (;;){if (i < 1 || i > 10 || j < 1 || j > 9){printf("请认真输入,位置应在棋盘内!\n再次输入:");scanf("%d%d", &i, &j);}elsebreak;}i--;j--;if (*(*(str + j) + i) == x)break;elseprintf("该位置没有该棋子!请认真输入!\n");}printf("请输入要下的位置(移动后位置):");label:scanf("%d%d", &i1, &j1);for (;;){if (i1 < 1 || i1 > 10 || j1 < 1 || j1 > 9){printf("请认真输入,位置应在棋盘内!\n再次输入:");scanf("%d%d", &i1, &j1);}elsebreak;}i1--;j1--;y = *(*(str + j1) + i1);if (y >= x1 && y <= y1){printf("移动后位置有己方棋子\n重新输入下的位置:");goto label;}else{if (i1 == i && j1 == j){printf("原地未动\n重新输入下的位置:");goto label;}else{/*车的情况*/if (x == 'a' || x == 'A'){if (i1 != i && j1 != j){if (q == 1)printf("'车'应直走!\n");elseprintf("'車'应直走!\n重新输入下的位置:");goto label;}else{if (i1 == i){if (j1 > j)for (k = j + 1; k < j1; k++){if (*(*(str + k) + i) != '0'){printf("中间有子挡\n重新输入下的位置:");goto label;}}else{for (k = j1 + 1; k < j; k++){if (*(*(str + k) + i) != '0'){printf("中间有子挡\n重新输入下的位置:");goto label;}}}}else{if (i1 > i)for (k = i + 1; k < i1; k++){if (*(*(str + j) + k) != '0')printf("中间有子挡\n重新输入下的位置:");goto label;}}else{for (k = i1 + 1; k < i; k++){if (*(*(str + j) + k) != '0'){printf("中间有子挡\n重新输入下的位置:");goto label;}}}}}}/*车的情况*/ /*马的情况*/ if (x == 'b' || x == 'B'){if (i1 == i + 1 || i1 == i - 1){if (j1 == j + 2){if (*(*(str + j + 1) + i) != '0'){if (q == 1)printf("'马'撇腿!\n");elseprintf("'馬'撇腿!\n重新输入下的位置:");goto label;}elsegoto la;}else{if (j1 == j - 2){if (*(*(str + j - 1) + i) != '0'){if (q == 1)printf("'马'撇腿!\n");printf("'馬'撇腿!\n重新输入下的位置:");goto label;}elsegoto la;}else{if (q == 1)printf("'马'应走'日'\n");elseprintf("'馬'应走'日'\n重新输入下的位置:");goto label;}}}else{if (i1 == i + 2 || i1 == i - 2){if (j1 == j + 1 || j1 == j - 1){if (*(*(str + j) + (i1 + i) / 2) != '0'){if (q == 1)printf("'马'撇腿!\n");elseprintf("'馬'撇腿!\n重新输入下的位置:");goto label;}elsegoto la;}else{if (q == 1)printf("'马'应走'日'\n");elseprintf("'馬'应走'日'\n重新输入下的位置:");goto label;}}else{if (q == 1)printf("'马'应走'日'\n");elseprintf("'馬'应走'日'\n重新输入下的位置:");goto label;}}}/*马的情况*/ /*相的情况*/ if (x == 'c' || x == 'C'){if ((i1 == i + 2 || i1 == i - 2) && (j1 == j + 2 || j1 == j - 2)){if (*(*(str + (j + j1) / 2) + (i + i1) / 2) != '0'){if (q == 1)printf("'相'压田!\n");elseprintf("'象'压田!\n重新输入下的位置:");goto label;}elsegoto la;}else{if (q == 1)printf("'相'应飞'田'\n");elseprintf("'象'应飞'田'\n重新输入下的位置:");goto label;}}/*相的情况*/ /*士的情况*/ if (x == 'd' || x == 'D'){if (q == 1 && (j1 < 3 || j1 > 5 || i1 < o3)){printf("'士'不可出九宫格\n重新输入下的位置:");goto label;}else{if (q == 0 && (j1 < 3 || j1 > 5 || i1 > o3)){printf("'仕'不可出九宫格\n重新输入下的位置:");goto label;}else{if ((i1 == i + 1 || i1 == i - 1) && (j1 == j + 1 || j1 == j - 1))goto la;else{if (q == 1)printf("'士'应斜着走\n");elseprintf("'仕'应斜着走\n重新输入下的位置:");goto label;}}}}/*士的情况*/ /*帅的情况*/if (x == 'e' || x == 'E'){for (k = i1 - 1; k >= 0; k--){if (*(*(str + j1) + k) != '\0'){if (q == 1)z1 = 'E';elsez1 = 'e';if (*(*(str + j1) + k) == z1){printf("'将''帅' 照面\n重新输入下的位置:");goto label;}break;}}if (q == 1 && (j1 < 3 || j1 > 5 || i1 < o3)){printf("'帅'不可出九宫格\n重新输入下的位置:");goto label;}else{if (q == 0 && (j1 < 3 || j1 > 5 || i1 > o3)){printf("'将'不可出九宫格\n重新输入下的位置:");goto label;}else{if ((i1 == i + 1 || i1 == i - 1) && j1 == j)goto la;else{if (i1 == i && (j1 == j + 1 || j1 == j - 1))goto la;else{if (q == 1)printf("'帅'一次只(直)走一格\n");elseprintf("'将'一次只(直)走一格\n重新输入下的位置:");goto label;}}}}}/*帅的情况*/ /*包的情况*/ if (x == 'f' || x == 'F'){if (i1 != i && j1 != j){if (q == 1)printf("'包'应直走!\n");elseprintf("'炮'应直走!\n重新输入下的位置:");goto label;}else{if (i1 == i){if (j1 > j){for (p = 0, k = j + 1; k < j1; k++)if (*(*(str + k) + i) != '0')p++;if ((p == 0 && *(*(str + j1) + i1) != '0')|| (p == 1 && *(*(str + j1) + i1) == '0')){printf("有子挡\n重新输入下的位置:");goto label;}else{if (p > 1){printf("隔子太多\n重新输入下的位置:");goto label;}elsegoto la;}}else{for (p = 0, k = j1 + 1; k < j; k++)if (*(*(str + k) + i) != '0')p++;if ((p == 0 && *(*(str + j1) + i1) != '0')|| (p == 1 && *(*(str + j1) + i1) == '0')){printf("有子挡\n重新输入下的位置:");goto label;}else{if (p > 1){printf("隔子太多\n重新输入下的位置:");goto label;}elsegoto la;}}}else{if (i1 > i){for (p = 0, k = i + 1; k < i1; k++)if (*(*(str + k) + i) != '0')p++;if ((p == 0 && *(*(str + j1) + i1) != '0')|| (p == 1 && *(*(str + j1) + i1) == '0')){printf("有子挡\n重新输入下的位置:");goto label;}else{if (p > 1){printf("隔子太多\n重新输入下的位置:");goto label;}elsegoto la;}}else{for (p = 0, k = i1 + 1; k < i; k++)if (*(*(str + k) + i) != '0')p++;if ((p == 0 && *(*(str + j1) + i1) != '0')|| (p == 1 && *(*(str + j1) + i1) == '0')){printf("有子挡\n重新输入下的位置:");goto label;}else{if (p > 1){printf("隔子太多\n重新输入下的位置:");goto label;}elsegoto la;}}}}}/*包的情况*/ /*兵的情况*/if (x == 'g'){if (i1 > i){printf("'兵'不能后退\n重新输入下的位置:");goto label;}if (i1 == i && i1 > 5){printf("'兵'没过河不能横着走\n重新输入下的位置:");goto label;}else{if (i1 == i && (j1 == j + 1 || j1 == j - 1))goto la;else{if (j1 == j && i1 == i - 1)goto la;else{printf("'兵'一次只走一格\n重新输入下的位置:");goto label;}}}}/*兵的情况*/ /*卒的情况*/ if (x == 'G'){if (i1 < i){printf("'卒'不能后退\n重新输入下的位置:");goto label;}if (i1 == i && i1 < 5){printf("'卒'没过河不能横着走\n重新输入下的位置:");goto label;}else{if (i1 == i && (j1 == j + 1 || j1 == j - 1))goto la;else{if (j1 == j && i1 == i + 1)goto la;else{printf("'卒'一次只走一格\n重新输入下的位置:");goto label;}}}}/*卒的情况*/ la:*(*(str + j1) + i1) = x;*(*(str + j) + i) = '0';printf("\n");for (m = 0; m < 10; m++){for (n = 0; n < 9; n++){o = *(*(str + n) + m);f(o);}printf("\n\n");}}}}int main(){int i0, j0, k0, m, n, m1, n1, p1, p3, ww;char **str, **str2;char x0, str1[10];str = (char **)malloc(1600);for (i0 = 0; i0 < 11; i0++)*(str + i0) = (char *)malloc(40);str2 = (char **)malloc(144);for (i0 = 0; i0 < 4; i0++)*(str2 + i0) = (char *)malloc(12);ww1:printf("\n欢迎游戏!\n 中国象棋\n\n");printf("请选择游戏模式:\n1.双人大战!\n2.九宫象棋!\n请输入编号:");scanf("%d", &ww);if (ww != 1 && ww != 2){printf("请按要求输入!\n");goto ww1;}if (ww == 2){printf("\n游戏规则:\n攻(红)方先动棋子不可出九宫格,在步步将军的情况下最终吃将获胜!\n""守(黑)方一直逃,直到对方无法将死便获胜!\n""攻(红)方只有車,馬二子,走法同象棋\n\n");lma1:printf("\n");for (i0 = 0; i0 < 3; i0++){for (j0 = 0; j0 < 3; j0++)*(*(str2 + j0) + i0) = '0';}**str2 = 'a';**(str2 + 2) = 'b';*(*(str2 + 1) + 1) = 'E';*(*(str2) + 2) = 'b';*(*(str2 + 2) + 2) = 'a';for (i0 = 0; i0 < 3; i0++){for (j0 = 0; j0 < 3; j0++){x0 = *(*(str2 + j0) + i0);f(x0);}printf("\n\n");}ii = 1, jj = 1;}else{printf("\n游戏简介:本游戏与现实中象棋规则相同!为方便游戏运行,棋子将用字母替代\n\n""红方:车(a) 马(b) 相(c) 士(d) 帅(e) 包(f) 兵(g)\n""黑方:車(A) 馬(B) 象(C) 仕(D) 将(E) 炮(F) 卒(G)\n\n");lma:printf("\n");for (i0 = 0; i0 < 10; i0++)for (j0 = 0; j0 < 9; j0++)*(*(str + j0) + i0) = '0';**str = 'A';**(str + 1) = 'B';**(str + 2) = 'C';**(str + 3) = 'D';**(str + 4) = 'E';*(*(str + 1) + 2) = 'F';*(*(str) + 3) = 'G';*(*(str + 2) + 3) = 'G';*(*(str + 4) + 3) = 'G';*(*(str) + 6) = 'g';*(*(str + 2) + 6) = 'g';*(*(str + 4) + 6) = 'g';*(*(str + 1) + 7) = 'f';*(*(str) + 9) = 'a';*(*(str + 1) + 9) = 'b';*(*(str + 2) + 9) = 'c';*(*(str + 3) + 9) = 'd';*(*(str + 4) + 9) = 'e';for (i0 = 0; i0 < 10; i0++){for (j0 = 5; j0 < 9; j0++)*(*(str + j0) + i0) = *(*(str + 8 - j0) + i0);}for (i0 = 0; i0 < 10; i0++){for (j0 = 0; j0 < 9; j0++){x0 = *(*(str + j0) + i0);f(x0);}printf("\n\n");}}printf("Ready Go?(输入“start”开始):");scanf("%s", str1);for (;;){if (str1[0] == 's' && str1[1] == 't' && str1[2] == 'a' && str1[3] == 'r' && str1[4] == 't'&& str1[5] == '\0')break;elseprintf("请正确输入!{Not ready?)\n再次输入:");scanf("%s", str1);}printf("\n红子先动!\n\n是否开启悔棋功能\n""征求双方意见,同意输入1,反对输入0(将关闭悔棋功能)\n");l11:printf("红方意见:");scanf("%d", &m1);if (m1 != 0 && m1 != 1){printf("请按要求输入!\n");goto l11;}l12:printf("黑方意见:");scanf("%d", &n1);if (n1 != 0 && n1 != 1){printf("请按要求输入!\n");goto l12;}p3 = m1 * n1;for (m = 1;; m++){l3:m = m % 2;if (ww == 1)f1(str, m);elsef2(str2, m);if (p3 != 0){printf("是否悔棋\n征求双方意见,同意输入1,反对输入0,关闭悔棋功能输入2\n");l1:printf("红方意见:");scanf("%d", &m1);if (m1 != 0 && m1 != 1 && m1 != 2){printf("请按要求输入!\n");goto l1;}l2:printf("黑方意见:");scanf("%d", &n1);if (n1 != 0 && n1 != 1 && n1 != 2){printf("请按要求输入!\n");goto l2;}p1 = m1 * n1;if (p1 == 1 && m1 != 2 && n1 != 2){if (ww == 1){*(*(str + j) + i) = x;*(*(str + j1) + i1) = y;}else{*(*(str2 + j) + i) = x;*(*(str2 + j1) + i1) = y;}printf("\n");if (ww == 1){for (i0 = 0; i0 < 10; i0++){for (j0 = 0; j0 < 9; j0++){x0 = *(*(str + j0) + i0);f(x0);}printf("\n\n");}}else{for (i0 = 0; i0 < 3; i0++){for (j0 = 0; j0 < 3; j0++){x0 = *(*(str2 + j0) + i0);f(x0);}printf("\n\n");}}goto l3;}if (m1 == 2 || n1 == 2)p3 = 0;}if (ww == 1){if (y == 'E'){printf("红方胜\n");break;}if (y == 'e'){printf("黑方胜\n");break;}}else{if (y == 'E'){printf("攻(红)方胜\n");break;}}}ljl:printf("是否再来一局?是(Yes),否(No),返回主菜单(Return):");scanf("%s", str1);if (str1[0] == 'Y' && str1[1] == 'e' && str1[2] == 's' && str1[3] == '\0') {if (ww == 1)goto lma;if (ww == 2)goto lma1;}else{if (str1[0] == 'N' && str1[1] == 'o' && str1[2] == '\0')printf("Game Over!\n");else{if (str1[0] == 'R' && str1[1] == 'e' && str1[2] == 't' && str1[3] == 'u' && str1[4] == 'r' && str1[5] == 'n' && str1[6] == '\0')goto ww1;else{printf("请按要求输入!\n");goto ljl;}}}for (i0 = 0; i0 < 10; i0++)free(*(str + i0));free(str);for (i0 = 0; i0 < 3; i0++)free(*(str2 + i0));free(str2);system("PAUSE");return 0;}。
中国象棋(代码)
中国象棋(web版源代码)程序:using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;namespace WebApplication1{public partial class WebForm1 : System.Web.UI.Page{int tru = 20;int fals = 40;public ImageButton[,] _Image=new ImageButton[11,10];//将上一次点击点的坐标保存到数据库中的lastx和lastypublic void SaveToLast(){if (Session["user"].ToString() == "red" &&_GetUserState(Session["user"].ToString()) == 20){int x, y, lastx, lasty;x = Getpointx();y = Getpointy();lastx = x;lasty = y;Updatalastx(lastx);Updatalasty(lasty);}if (Session["user"].ToString() == "black" &&_GetUserState(Session["user"].ToString()) == 20){int x, y, lastx, lasty;x = Getpointx();y = Getpointy();lastx = x;lasty = y;Updatalastx(lastx);Updatalasty(lasty);}}//将棋盘上所有棋子图片显示到棋盘上private void _Drawqizi(){//_Init();int i,j,k;if (_GetUserState("red") != 0 && _GetUserState("black") != 0){if (Session["user"].ToString() == "red"){for (i = 1; i <= 10; i++)for (j = 1; j <= 9; j++){k = _GetDataQipan(i, j);_Image[i, j].ImageUrl = _GetImageAdd(k);}}if (Session["user"].ToString() == "black"){for (i = 1; i <= 10; i++)for (j = 1; j <= 9; j++){k = _GetDataQipan(i, j);_Image[11 - i, 10 - j].ImageUrl =_GetImageAdd(k);}}}//初始化:对_Image[,]赋值,对ImageButton进行编号private void _Init(){_Image[1, 1] = ImageButton1; _Image[1, 2] = ImageButton2; _Image[1, 3] = ImageButton3; _Image[1, 4] = ImageButton4; _Image[1, 5] = ImageButton5; _Image[1, 6] = ImageButton6; _Image[1, 7] = ImageButton7; _Image[1, 8] = ImageButton8; _Image[1, 9] = ImageButton9;_Image[2, 1] = ImageButton11; _Image[2, 2] = ImageButton12; _Image[2, 3] = ImageButton13; _Image[2, 4] = ImageButton14; _Image[2, 5] = ImageButton15; _Image[2, 6] = ImageButton16; _Image[2, 7] = ImageButton17; _Image[2, 8] = ImageButton18; _Image[2, 9] = ImageButton19;_Image[3, 1] = ImageButton21; _Image[3, 2] = ImageButton22; _Image[3, 3] = ImageButton23; _Image[3, 4] = ImageButton24; _Image[3, 5] = ImageButton25; _Image[3, 6] = ImageButton26; _Image[3, 7] = ImageButton27; _Image[3, 8] = ImageButton28; _Image[3, 9] = ImageButton29;_Image[4, 1] = ImageButton31; _Image[4, 2] = ImageButton32; _Image[4, 3] = ImageButton33; _Image[4, 4] = ImageButton34; _Image[4, 5] = ImageButton35; _Image[4, 6] = ImageButton36; _Image[4, 7] = ImageButton37; _Image[4, 8] = ImageButton38; _Image[4, 9] = ImageButton39;_Image[5, 1] = ImageButton41; _Image[5, 2] = ImageButton42; _Image[5, 3] = ImageButton43; _Image[5, 4] = ImageButton44; _Image[5, 5] = ImageButton45; _Image[5, 6] = ImageButton46; _Image[5, 7] = ImageButton47; _Image[5, 8] = ImageButton48; _Image[5, 9] = ImageButton49;_Image[6, 1] = ImageButton51; _Image[6, 2] = ImageButton52; _Image[6, 3] = ImageButton53; _Image[6, 4] = ImageButton54; _Image[6, 5] = ImageButton55; _Image[6, 6] = ImageButton56; _Image[6, 7] = ImageButton57; _Image[6, 8] = ImageButton58; _Image[6, 9] = ImageButton59;_Image[7, 1] = ImageButton61; _Image[7, 2] = ImageButton62; _Image[7, 3] = ImageButton63; _Image[7, 4] = ImageButton64; _Image[7, 5] = ImageButton65; _Image[7, 6] = ImageButton66; _Image[7, 7] = ImageButton67; _Image[7, 8] = ImageButton68; _Image[7, 9] = ImageButton69;_Image[8, 1] = ImageButton71; _Image[8, 2] = ImageButton72; _Image[8, 3] = ImageButton73; _Image[8, 4] = ImageButton74; _Image[8, 5] = ImageButton75; _Image[8, 6] = ImageButton76; _Image[8, 7] = ImageButton77; _Image[8, 8] = ImageButton78; _Image[8, 9] = ImageButton79;_Image[9, 1] = ImageButton81; _Image[9, 2] = ImageButton82; _Image[9, 3] = ImageButton83; _Image[9, 4] = ImageButton84; _Image[9, 5] = ImageButton85; _Image[9, 6] = ImageButton86; _Image[9, 7] = ImageButton87; _Image[9, 8] = ImageButton88; _Image[9, 9] = ImageButton89;_Image[10, 1] = ImageButton91; _Image[10, 2] = ImageButton92; _Image[10, 3] = ImageButton93; _Image[10, 4] = ImageButton94; _Image[10, 5] = ImageButton95; _Image[10, 6] = ImageButton96; _Image[10, 7] = ImageButton97; _Image[10, 8] = ImageButton98; _Image[10, 9] = ImageButton99;int i, j;for (i = 1; i <= 10; i++)for (j = 1; j <= 9; j++){_Image[i, j].ImageUrl = "~/image/back.gif";}}//初始化棋盘,将两方棋子放好位private void _InitQizi(){int i = 0, j = 0, k = 0;int[] initqipandata = new int[37];for (i = 1; i <= 10; i++)for (j = 1; j <= 9; j++){_UpdataQipan(i, j, k);}for (i = 0; i <= 36; i++)initqipandata[i] = i;_UpdataQipan(1, 1, initqipandata[8]); _UpdataQipan(1, 2, initqipandata[6]); _UpdataQipan(1, 3, initqipandata[4]); _UpdataQipan(1, 4, initqipandata[2]); _UpdataQipan(1, 5, initqipandata[1]); _UpdataQipan(1,6, initqipandata[3]); _UpdataQipan(1, 7, initqipandata[5]); _UpdataQipan(1, 8, initqipandata[7]); _UpdataQipan(1, 9, initqipandata[9]); _UpdataQipan(3, 2, initqipandata[10]); _UpdataQipan(3, 8, initqipandata[11]); _UpdataQipan(4, 1, initqipandata[12]); _UpdataQipan(4, 3, initqipandata[13]);_UpdataQipan(4, 5, initqipandata[14]); _UpdataQipan(4, 7,initqipandata[15]); _UpdataQipan(4, 9, initqipandata[16]);_UpdataQipan(10, 1, initqipandata[28]); _UpdataQipan(10, 2, initqipandata[26]); _UpdataQipan(10, 3, initqipandata[24]);_UpdataQipan(10, 4, initqipandata[22]); _UpdataQipan(10, 5,initqipandata[21]); _UpdataQipan(10, 6, initqipandata[23]);_UpdataQipan(10, 7, initqipandata[25]); _UpdataQipan(10, 8,initqipandata[27]); _UpdataQipan(10, 9, initqipandata[29]); _UpdataQipan(8, 2, initqipandata[30]); _UpdataQipan(8, 8, initqipandata[31]);_UpdataQipan(7, 1, initqipandata[32]); _UpdataQipan(7, 3,initqipandata[33]); _UpdataQipan(7, 5, initqipandata[34]); _UpdataQipan(7, 7, initqipandata[35]); _UpdataQipan(7, 9, initqipandata[36]);}//获取棋子图片地址private string _GetImageAdd(int xxx){string x="" ;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select add_image from qizi where(no_qizi='"+xxx+"');", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read())x = sr["add_image"].ToString();//Session["add_image"] = x;myconn.Close();return x;}//获取点击后的图片地址private string _GetImageDownAdd(int xxx){string x ="";SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select add_image_down from qizi where(no_qizi='" + xxx + "')", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read())x = sr["add_image_down"].ToString();myconn.Close();return x;}//读取鼠标点击点的坐标private int Getpointx(){int x = 0;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select x from zuobiao", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read()){x = int.Parse(sr["x"].ToString());}myconn.Close();return x;}private int Getpointy( ){int x = 0;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select y from zuobiao", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read()){x = int.Parse(sr["y"].ToString());}myconn.Close();return x;}private int Getpointlastx( ){int x = 0;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select lastx from zuobiao", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read()){x = int.Parse(sr["lastx"].ToString());}myconn.Close();return x;}private int Getpointlasty( ){int x = 0;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select lasty from zuobiao", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read()){x = int.Parse(sr["lasty"].ToString());}myconn.Close();return x;}//写入鼠标点击点的坐标private void Updatax(int xxx){SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("update zuobiao set x='" + xxx + "'", myconn);myconn.Open();int aa = cmd.ExecuteNonQuery();myconn.Close();}private void Updatay(int yyy){SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("update zuobiao set y='" + yyy + "'", myconn);myconn.Open();int aa = cmd.ExecuteNonQuery();myconn.Close();}private void Updatalastx(int lastxxx){SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("update zuobiao set lastx='"+lastxxx +"'" , myconn);myconn.Open();int aa = cmd.ExecuteNonQuery();myconn.Close();}private void Updatalasty(int lastyyy){SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("update zuobiao set lasty='"+ lastyyy +"'", myconn);myconn.Open();int aa = cmd.ExecuteNonQuery();myconn.Close();}//以上四个函数的集合,达到一次写入四个坐标值的目的private void _UpdataaZuobiao(int xxx, int yyy, int lastxxx, int lastyyy){Updatax(xxx);Updatay(yyy);Updatalastx(lastxxx);Updatalasty(lastyyy);}//保存当前坐标值到x和yprivate void _UpdatZuobiaoXY(int xxx, int yyy){if (Session["user"].ToString() == "red" &&_GetUserState(Session["user"].ToString()) == 20){Updatax(xxx);Updatay(yyy);}if (Session["user"].ToString() == "black" &&_GetUserState(Session["user"].ToString()) == 20){Updatax(xxx);Updatay(yyy);}}//读取棋盘上的棋子信息private int _GetDataQipan(int xxx, int yyy){int i,data=0;i = (xxx - 1) * 9 + yyy;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select data_qipan from qipan where(position='"+i+"')", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read())data = int.Parse(sr["data_qipan"].ToString());myconn.Close();return data;}//将棋子信息写入棋盘private void _UpdataQipan(int xxx, int yyy,int data){int i;i = (xxx - 1) * 9 + yyy;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("update qipan set data_qipan=" + data + "where (position='"+i+"')", myconn);myconn.Open();int aa = cmd.ExecuteNonQuery();myconn.Close();}//读出用户此时状态private int _GetUserState(string id){int data = 0;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select state from yonghu where(id='" + id + "')", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read())data = int.Parse(sr["state"].ToString());myconn.Close();return data;}//读出用户输赢状态 0表示在进行游戏 20表示赢 40表示输private int _GetUserWin(string id){int data = 0;SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("select winner from yonghu where(id='" + id + "')", myconn);myconn.Open();SqlDataReader sr = cmd.ExecuteReader();if (sr.Read())data = int.Parse(sr["winner"].ToString());myconn.Close();return data;}//写入用户状态private void _UpdataUserState(string id,int sta){SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("update yonghu set state="+ sta + "where (id='" + id + "')", myconn);myconn.Open();int aa = cmd.ExecuteNonQuery();myconn.Close();}//写入用户输赢状态 0表示在进行游戏 20表示赢 40表示输private void _UpdataUserWin(string id, int sta){SqlConnection myconn = newSqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRIN G"].ConnectionString);SqlCommand cmd = new SqlCommand("update yonghu set winner=" + sta + "where (id='" + id + "')", myconn);myconn.Open();int aa = cmd.ExecuteNonQuery();myconn.Close();}protected void Page_Load(object sender, EventArgs e){_Init();// _Test();}//测试程序函数private void _Test(){_UpdataUserState("red",tru);_UpdataUserState("black", tru);}private bool_IsAbleToPut()//********************************************************需要传递x,y,laastx,lasty{if (Session["user"].ToString() == "red" &&_GetUserState(Session["user"].ToString()) == 40)return false;if (Session["user"].ToString() == "black" &&_GetUserState(Session["user"].ToString()) == 40)return false;int x, y, lastx, lasty;int qipandata,lastqipandata;x = Getpointx();y = Getpointy();lastx = Getpointlastx();lasty = Getpointlasty();if (Session["user"].ToString() == "black"){x = 11 - x;y = 10 - y;lastx = 11 - lastx;lasty = 10 - lasty;}qipandata = _GetDataQipan(x, y);lastqipandata = _GetDataQipan(lastx, lasty);// if (lastqipandata==0)// return false;if(Session["user"].ToString() == "red"&&(lastqipandata <= 20 || qipandata >=20))//****************************************************************现以红方为对象return false;if (Session["user"].ToString() == "black" && ((lastqipandata == 0 || lastqipandata >= 20) || (qipandata > 0 && qipandata <= 20)))return false;int i, j, c;//将|帅if (lastqipandata == 1 || lastqipandata == 21){if ((x - lastx) * (y - lasty) != 0) return false;if(Math.Abs(x - lastx) > 1 || Math.Abs(y - lasty) > 1) return false;if (y < 4 || y > 6 || (x > 3 && x < 8)) return false;return true;}//士|仕if (lastqipandata == 2 || lastqipandata == 3 || lastqipandata == 22 || lastqipandata == 23){if ((x - lastx) * (y - lasty) == 0) return false;if(Math.Abs(x - lastx) > 1 || Math.Abs(y - lasty) > 1) return false;if (y < 4 || y > 6 || (x > 3 && x < 8)) return false;return true;}//象|相if (lastqipandata == 4 || lastqipandata == 5 || lastqipandata == 24 || lastqipandata == 25){if ((x - lastx) * (y - lasty) == 0) return false;if (Math.Abs(x - lastx) != 2 || Math.Abs(y - lasty) != 2) return false;if(Session["user"].ToString() == "red"&& x < 6) return false;if (Session["user"].ToString() == "black" && x > 5) return false;i = 0; j = 0;//i,j必须有初始值if (x - lastx == 2){i = x - 1;}if (x - lastx == -2){i = x + 1;}if (y - lasty == 2){j = y - 1;}if (y - lasty == -2){j = y + 1;}if (_GetDataQipan(i, j) != 0) return false;return true;}//马if (lastqipandata == 6 || lastqipandata == 7 || lastqipandata == 26 || lastqipandata == 27){if (Math.Abs(x - lastx) * Math.Abs(y - lasty) != 2)return false;if (x - lastx == 2){if (_GetDataQipan(x - 1, lasty) != 0)return false;}if (x - lastx == -2){if (_GetDataQipan(x + 1, lasty) != 0)return false;}if (y - lasty == 2){if (_GetDataQipan(lastx, y - 1) != 0)return false;}if (y - lasty == -2){if (_GetDataQipan(lastx, y + 1) != 0)return false;}return true;}//车if (lastqipandata == 8 || lastqipandata == 9 || lastqipandata == 28 || lastqipandata == 29){//判断是否直线if ((x - lastx) * (y - lasty) != 0) return false;//判断是否隔有棋子if (x != lastx){if (lastx > x) { int t = x; x = lastx; lastx = t; }for (i = lastx; i <= x; i += 1){if (i != x && i != lastx){if (_GetDataQipan(i, y) != 0)return false;}}}if (y != lasty){if (lasty > y) { int t = y; y = lasty; lasty = t; }for (j = lasty; j <= y; j += 1){if (j != y && j != lasty){if (_GetDataQipan(x, j) != 0)return false;}}}return true;}//炮if (lastqipandata == 10 || lastqipandata == 11 || lastqipandata == 30 || lastqipandata == 31){bool swapflagx = false;bool swapflagy = false;if ((x - lastx) * (y - lasty) != 0) return false;c = 0;if (x != lastx){if (lastx > x) { int t = x; x = lastx; lastx = t; swapflagx = true; }for (i = lastx; i <= x; i += 1){if (i != x && i != lastx){if (_GetDataQipan(i, y) != 0)c = c + 1;//IsAbleToPut = False: Exit Function}}}if (y != lasty){if (lasty > y) { int t = y; y = lasty; lasty = t; swapflagy = true; }for (j = lasty; j <= y; j += 1){if (j != y && j != lasty){if (_GetDataQipan(x, j) != 0)c = c + 1;//IsAbleToPut = False: Exit Function}}}if (c > 1) return false; //与目标处间隔1个以上棋子if (c == 0) //与目标处无间隔棋子{if(swapflagx == true) { int t = x; x = lastx; lastx = t; }if(swapflagy == true) { int t = y; y = lasty; lasty = t; }if (_GetDataQipan(x, y) != 0) return false;}if (c == 1)//与目标处间隔1个棋子{if(swapflagx == true) { int t = x; x = lastx; lastx = t; }if(swapflagy == true) { int t = y; y = lasty; lasty = t; }// if ((IsMyChess(qipan[x, y]) || qipan[x, y] == 0))//return false;//***********ismychess************************************************** ***************************if (qipandata == 0)return false;}return true;}//卒|兵if (lastqipandata == 12 || lastqipandata == 13 || lastqipandata == 14 || lastqipandata == 15 || lastqipandata == 16 || lastqipandata == 32 || lastqipandata == 33 || lastqipandata == 34 || lastqipandata == 35 || lastqipandata == 36){if ((x - lastx) * (y - lasty) != 0)return false;if(Math.Abs(x - lastx) > 1 || Math.Abs(y - lasty) > 1)return false;if (Session["user"].ToString() == "red" && (x >= 6 && (y - lasty) != 0)) return false;if(Session["user"].ToString() == "black"&& (x <= 5 && (y - lasty) != 0)) return false;if(Session["user"].ToString() == "red"&& (x - lastx > 0)) return false;if(Session["user"].ToString() == "black"&& (x - lastx < 0)) return false;return true;}return false;}//移动棋子private void _MoveChess()//********************************************************需要传递x,y,laastx,lasty{//如果能移动则移动棋子if (_IsAbleToPut()){int x, y, lastx, lasty,qipandata, lastqipandata, tr;tr = 0;x = Getpointx();y = Getpointy();lastx = Getpointlastx();lasty = Getpointlasty();if (Session["user"].ToString() == "black"){x = 11 - x;y = 10 - y;lastx = 11 - lastx;lasty = 10 - lasty;}qipandata = _GetDataQipan(x, y);lastqipandata = _GetDataQipan(lastx, lasty);_UpdataQipan(x, y, lastqipandata);_UpdataQipan(lastx, lasty, tr);if (Session["user"].ToString() == "red"){_UpdataUserState("red", fals);_UpdataUserState("black", tru);}if (Session["user"].ToString() == "black"){_UpdataUserState("red", tru);_UpdataUserState("black", fals);}if (qipandata == 1){Response.Write("<script language=javascript>alert('恭喜您,您赢啦');</script>");_UpdataUserState("red", fals);_UpdataUserState("black", fals);Button6.Enabled = true;_UpdataUserWin("red",20);_UpdataUserWin("black", 40);}if (qipandata == 21){Response.Write("<script language=javascript>alert('恭喜您,您赢啦');</script>");_UpdataUserState("red", fals);_UpdataUserState("black", fals);Button6.Enabled = true;_UpdataUserWin("red", 40);_UpdataUserWin("black", 20);}_UpdataaZuobiao(0, 0, 0, 0);}//如果不能移动则更换已方被点击棋子的背景图片}protected void ImageButton1_Click1(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 1; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton2_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 2; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton3_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 3; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton4_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 4; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton5_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 5; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton6_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 6; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton7_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 7; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton8_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 8; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton9_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 1; y = 9; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton11_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 1; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton12_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 2; _UpdatZuobiaoXY(x, y);_MoveChess(); _Drawqizi();}protected void ImageButton13_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 3; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton14_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 4; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton15_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 5; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton16_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 6; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton17_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 7; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton18_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 8; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton19_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 2; y = 9; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton21_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 1; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton22_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 2; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton23_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 3; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton24_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 4; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton25_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 5; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton26_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 6; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton27_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 7; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton28_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 8; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton29_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 3; y = 9; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton31_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 4; y = 1; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton32_Click(object sender,ImageClickEventArgs e){int x, y; SaveToLast(); x = 4; y = 2; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton33_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 4; y = 3; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton34_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 4; y = 4; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton35_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 4; y = 5; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton36_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 4; y = 6; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}protected void ImageButton37_Click(object sender, ImageClickEventArgs e){int x, y; SaveToLast(); x = 4; y = 7; _UpdatZuobiaoXY(x, y); _MoveChess(); _Drawqizi();}。
中国象棋游戏源代码
using System;using System.Collections.Generic; using ponentModel; using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace象棋{enum player{blank,red,blue,};enum chesstype{blank,jiang,che,ma,pao,xiang,zu,shi};struct chess{public player side;public chesstype type;};//下载于struct block{public PictureBox container;public chess item;};public partial class Form1 : Form{public Form1(){InitializeComponent();pictureboxlist = new List<PictureBox>(81); Matrix=new block[10][];int i,j;for (i = 0; i < 10;i++ ){Matrix[i] = new block[9];}for(i=0;i<10;i++){for(j=0;j<9;j++){Control[] col =this.Controls.Find("pictureBox" + (i*9+j+1), false); Matrix[i][j].container=col[0] as PictureBox;Matrix[i][j].container.Location = new Point(60 * j, 60 * i);}}redcoll = new collecter();bluecool = new collecter();for (i = 91; i < 107;i++ ){Control[] col =this.Controls.Find("pictureBox" + i, false);bluecool.add(col[0] as PictureBox);}for (i = 107; i < 123;i++ ){Control[] col =this.Controls.Find("pictureBox" + i, false);redcoll.add(col[0] as PictureBox);}resetground();}List<PictureBox> pictureboxlist;block[][] Matrix;collecter redcoll;collecter bluecool;int chozenX;int chozenY;player currentside;bool beenchozen;bool clickswitch;private void click1(object sender, EventArgs e) {if(!clickswitch){resetground();return;}string name = (sender as PictureBox).Name;string number = name.Substring(10);int index = Convert.ToInt32(number);int i,j;bool flag = false;i=(index-1)/9;j=(index-1)%9;//下载于if (beenchozen){Matrix[chozenX][chozenY].container.BorderStyle = BorderStyle.None;Matrix[chozenX][chozenY].container.BackColor =Color.Transparent;beenchozen = false;if(Matrix[chozenX][chozenY].item.side==Matrix[i][j].it em.side){return;}if(Matrix[chozenX][chozenY].item.side != player.blank) {if(Matrix[i][j].item.type== chesstype.jiang){flag=true;}if(!movechess(i, j)){return;}if(flag){if (currentside == player.red) {MessageBox.Show("红方胜利!点击任意处重新开局");}else{MessageBox.Show("蓝方胜利!点击任意处重新开局");}clickswitch = false;}}if (currentside == player.red){currentside = player.blue;label1.Text = "蓝方";label1.ForeColor = Color.Blue;}else{currentside = player.red;label1.Text = "红方";label1.ForeColor = Color.Red;}}else if(Matrix[i][j].item.side== currentside){Matrix[i][j].container.BorderStyle = BorderStyle.FixedSingle;Matrix[i][j].container.BackColor = Color.Brown;chozenX = i;chozenY = j;beenchozen = true;}}private void resetground(){int i, j;for (i = 0; i < 10;i++ ){for(j=0;j<9;j++){Matrix[i][j].container.Image = null;Matrix[i][j].item.side = player.blank;Matrix[i][j].item.type = chesstype.blank;}}beenchozen = false;clickswitch = true;currentside = player.red;label1.Text = "红方";label1.ForeColor = Color.Red;redcoll.clear();bluecool.clear();Matrix[0][0].container.Image = global::象棋.Properties.Resources.蓝车;Matrix[0][1].container.Image = global::象棋.Properties.Resources.蓝马;Matrix[0][2].container.Image = global::象棋.Properties.Resources.蓝象;Matrix[0][3].container.Image = global::象棋.Properties.Resources.蓝士;Matrix[0][4].container.Image = global::象棋.Properties.Resources.蓝将;Matrix[0][5].container.Image = global::象棋.Properties.Resources.蓝士;Matrix[0][6].container.Image = global::象棋.Properties.Resources.蓝象;Matrix[0][7].container.Image = global::象棋.Properties.Resources.蓝马;Matrix[0][8].container.Image = global::象棋.Properties.Resources.蓝车;Matrix[2][1].container.Image = global::象棋.Properties.Resources.蓝炮;Matrix[2][7].container.Image = global::象棋.Properties.Resources.蓝炮;Matrix[3][0].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][2].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][4].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][6].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[3][8].container.Image = global::象棋.Properties.Resources.蓝卒;Matrix[6][0].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][2].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][4].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][6].container.Image = global::象棋.Properties.Resources.红卒;Matrix[6][8].container.Image = global::象棋.Properties.Resources.红卒;Matrix[7][1].container.Image = global::象棋.Properties.Resources.红炮;Matrix[7][7].container.Image = global::象棋.Properties.Resources.红炮;Matrix[9][0].container.Image = global::象棋.Properties.Resources.红车;Matrix[9][1].container.Image = global::象棋.Properties.Resources.红马;Matrix[9][2].container.Image = global::象棋.Properties.Resources.红象;Matrix[9][3].container.Image = global::象棋.Properties.Resources.红士;Matrix[9][4].container.Image = global::象棋.Properties.Resources.红将;Matrix[9][5].container.Image = global::象棋.Properties.Resources.红士;Matrix[9][6].container.Image = global::象棋.Properties.Resources.红象;Matrix[9][7].container.Image = global::象棋.Properties.Resources.红马;Matrix[9][8].container.Image = global::象棋.Properties.Resources.红车;//下载于Matrix[0][0].item.side = player.blue;Matrix[0][1].item.side = player.blue;Matrix[0][2].item.side = player.blue;Matrix[0][3].item.side = player.blue;Matrix[0][4].item.side = player.blue;Matrix[0][5].item.side = player.blue;Matrix[0][6].item.side = player.blue;Matrix[0][7].item.side = player.blue;Matrix[0][8].item.side = player.blue;Matrix[2][1].item.side = player.blue;Matrix[2][7].item.side = player.blue;Matrix[3][0].item.side = player.blue;Matrix[3][2].item.side = player.blue;Matrix[3][4].item.side = player.blue;Matrix[3][6].item.side = player.blue;Matrix[3][8].item.side = player.blue;Matrix[6][0].item.side = player.red;Matrix[6][2].item.side = player.red;Matrix[6][4].item.side = player.red;Matrix[6][6].item.side = player.red;Matrix[6][8].item.side = player.red;Matrix[7][1].item.side = player.red;Matrix[7][7].item.side = player.red;Matrix[9][0].item.side = player.red;Matrix[9][1].item.side = player.red;Matrix[9][2].item.side = player.red;Matrix[9][3].item.side = player.red;Matrix[9][4].item.side = player.red;Matrix[9][5].item.side = player.red;Matrix[9][6].item.side = player.red;Matrix[9][7].item.side = player.red;Matrix[9][8].item.side = player.red;Matrix[0][0].item.type = chesstype.che; Matrix[0][1].item.type = chesstype.ma;Matrix[0][2].item.type = chesstype.xiang; Matrix[0][3].item.type = chesstype.shi; Matrix[0][4].item.type = chesstype.jiang; Matrix[0][5].item.type = chesstype.shi; Matrix[0][6].item.type = chesstype.xiang; Matrix[0][7].item.type = chesstype.ma;Matrix[0][8].item.type = chesstype.che; Matrix[2][1].item.type = chesstype.pao; Matrix[2][7].item.type = chesstype.pao; Matrix[3][0].item.type = chesstype.zu;Matrix[3][2].item.type = chesstype.zu;Matrix[3][4].item.type = chesstype.zu;Matrix[3][6].item.type = chesstype.zu;Matrix[3][8].item.type = chesstype.zu;Matrix[6][0].item.type = chesstype.zu;Matrix[6][2].item.type = chesstype.zu;Matrix[6][4].item.type = chesstype.zu;Matrix[6][6].item.type = chesstype.zu;Matrix[6][8].item.type = chesstype.zu;Matrix[7][1].item.type = chesstype.pao;Matrix[7][7].item.type = chesstype.pao;Matrix[9][0].item.type = chesstype.che;Matrix[9][1].item.type = chesstype.ma;Matrix[9][2].item.type = chesstype.xiang; Matrix[9][3].item.type = chesstype.shi;Matrix[9][4].item.type = chesstype.jiang; Matrix[9][5].item.type = chesstype.shi;Matrix[9][6].item.type = chesstype.xiang; Matrix[9][7].item.type = chesstype.ma;Matrix[9][8].item.type = chesstype.che;}private bool movechess(int X,int Y){int i, j, k, n=0;switch(Matrix[chozenX][chozenY].item.type) {case chesstype.che:if(chozenX==X){i = chozenY < Y ? chozenY : Y; j = chozenY > Y ? chozenY : Y;for(k=i+1;k<j;k++){if(Matrix[X][k].item.side!= player.blank){return false;}}}if (chozenY == Y){i = chozenX < X ? chozenX : X; j = chozenX > X ? chozenX : X;for (k = i + 1; k < j; k++){if(Matrix[k][Y].item.side != player.blank){return false;}}}setmove(X, Y);return true;case chesstype.jiang:if(Matrix[X][Y].item.type== chesstype.jiang&&chozenY==Y){i = chozenX < X ? chozenX : X; j = chozenX > X ? chozenX : X;for (k = i + 1; k < j; k++){if(Matrix[k][Y].item.side != player.blank){return false;}}setmove(X, Y);return true;}if(Matrix[chozenX][chozenY].item.side== player.blue){if(Y<3||Y>5||X>2){return false;}}else{if(Y<3||Y>5||X<7){return false;}}if((chozenX-X)*(chozenX-X)+(chozenY-Y)*(chozenY-Y)!=1) {return false;}setmove(X, Y);return true;case chesstype.ma:if (Math.Abs(chozenX - X) == 1 && Math.Abs(chozenY - Y) == 2){if (Matrix[chozenX][chozenY +(Y - chozenY) / Math.Abs(Y - chozenY)].item.side!= player.blank){return false;}}else if (Math.Abs(chozenX - X) == 2 && Math.Abs(chozenY - Y) == 1){if (Matrix[chozenX + (X - chozenX) / Math.Abs(X - chozenX)][chozenY].item.side != player.blank){return false;}}else{return false;}setmove(X, Y);return true;case chesstype.pao: n = 0;if(chozenX==X){i = chozenY < Y ? chozenY : Y; j = chozenY > Y ? chozenY : Y; n = 0;for(k=i+1;k<j;k++){if(Matrix[X][k].item.side!= player.blank){n++;}}if(n>1){return false;}}else if (chozenY == Y){i = chozenX < X ? chozenX : X; j = chozenX > X ? chozenX : X; n = 0;for (k = i + 1; k < j; k++){if(Matrix[k][Y].item.side != player.blank){n++;}}if (n > 1){return false;}}else{return false;}if(n==0&&Matrix[X][Y].item.side!= player.blank){return false;}if(n==1&&Matrix[X][Y].item.side== player.blank){return false;}setmove(X, Y);return true;case chesstype.shi:if(Matrix[chozenX][chozenY].item.side== player.blue){if(Y<3||Y>5||X>2){return false;}}else{if(Y<3||Y>5||X<7){return false;}}if(Math.Abs(X-chozenX)!=1||Math.Abs(chozenY-Y)!=1){return false;}setmove(X, Y);return true;case chesstype.xiang:if(Matrix[chozenX][chozenY].item.side == player.blue){if (X>4){return false;}}else{if (X<5){return false;}}if ((X - chozenX) * (X - chozenX) + (chozenY - Y) * (chozenY - Y) != 8){return false;}if(Matrix[(X+chozenX)/2][(Y+chozenY)/2].item.side!=player.blank){return false;}setmove(X, Y);return true;case chesstype.zu:if(X!=chozenX&&Y!=chozenY){return false;}if(Matrix[chozenX][chozenY].item.side == player.blue) {if (chozenX<5&&X-chozenX!=1) {return false;}if(chozenX>4){if(X==chozenX&&Math.Abs(Y-chozenY)!=1){return false;}if(Y==chozenY&&X-chozenX!=1){return false;}}}else{if (chozenX>4&&chozenX-X!=1) {return false;}if (chozenX <5){if (X == chozenX && Math.Abs(Y - chozenY) != 1){return false;}if (Y == chozenY && chozenX-X != 1){return false;}}}setmove(X,Y);return true;}return false;}private void setmove(int X,int Y){if (Matrix[X][Y].item.side== player.red) {redcoll.push(Matrix[X][Y].container.Image);}else if (Matrix[X][Y].item.side == player.blue){bluecool.push(Matrix[X][Y].container.Image);}Matrix[X][Y].container.Image =Matrix[chozenX][chozenY].container.Image;Matrix[X][Y].item =Matrix[chozenX][chozenY].item;Matrix[chozenX][chozenY].container.Image = null;Matrix[chozenX][chozenY].item.side = player.blank;Matrix[chozenX][chozenY].item.type= chesstype.blank;}}class collecter{public PictureBox[] container;public int number;public int chessnum;public collecter(){number = 0;container = new PictureBox[16];chessnum = 0;}public void add(PictureBox box).{container[number++] = box;}public void push(Image ima){container[chessnum++].BackgroundImage = ima;}public void clear(){for (int i = 0; i < chessnum; i++){container[i].BackgroundImage = null; }chessnum = 0;}};}.。
中国象棋源代码C#
//绘制棋盘上半部分的竖线 g.DrawLine(thinPen, new Point(_leftTop.X + _columnWidth * (col - 1), _leftTop.Y), new Point(_leftTop.X + _columnWidth * (col - 1), _leftTop.Y + 4 * _rowHeight)); //绘制棋盘下半部分的竖线 g.DrawLine(thinPen, new Point(_leftTop.X + _columnWidth * (col - 1), _leftTop.Y + 5 * _rowHeight), new Point(_leftTop.X + _columnWidth * (col - 1), _leftTop.Y + 9 * _rowHeight)); } //绘制链接楚河汉界左右两端的两条短竖线 g.DrawLine(thinPen, new Point(_leftTop.X, _leftTop.Y), new Point(_leftTop.X, _leftTop.Y + 9 * _rowHeight)); g.DrawLine(thinPen, new Point(_leftTop.X + 8 * _columnWidth, _leftTop.Y), new Point(_leftTop.X + 8 * _columnWidth, _leftTop.Y + 9 * _rowHeight)); //绘制上方大本营的交叉线 g.DrawLine(thinPen, new Point(_leftTop.X + 3 * _columnWidth, _leftTop.Y), new Point(_leftTop.X + 5 * _columnWidth, _leftTop.Y + 2 * _rowHeight)); g.DrawLine(thinPen, new Point(_leftTop.X + 5 * _columnWidth, _leftTop.Y), new Point(_leftTop.X + 3 * _columnWidth, _leftTop.Y + 2 * _columnWidth)); //绘制下方大本营交叉线 g.DrawLine(thinPen, new Point(_leftTop.X + 3 * _columnWidth, _leftTop.Y + 7 * _rowHeight), new Point(_leftTop.X + 5 * _columnWidth, _leftTop.Y + 9 * _rowHeight)); g.DrawLine(thinPen, new Point(_leftTop.X + 5 * _columnWidth, _leftTop.Y + 7 * _rowHeight), new Point(_leftTop.X + 3 * _columnWidth, _leftTop.Y + 9 * _rowHeight)); //书写“楚河”“汉界” Font font1 = new Font("隶书", (float)(_rowHeight * 0.8), FontStyle.Regular, GraphicsUnit.Pixel); SolidBrush fontBrush = new SolidBrush(Color.Black); g.DrawString("楚河", font1, fontBrush, new Point(_leftTop.X + _columnWidth, (int)(_leftTop.Y + 4.1 * _rowHeight))); g.DrawString("汉界", font1, fontBrush, new Point(_leftTop.X + 5 * _columnWidth, (int)(_leftTop.Y + 4.1 * _rowHeight))); //书写行数字编号 Font font2 = new Font("黑体", (float)(_rowHeight * 0.6), FontStyle.Regular, GraphicsUnit.Pixel);
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
本科毕业论文(科研训练、毕业设计)题目:中国象棋博弈算法研究姓名:李文耀学院:软件学院系:软件工程专业:软件工程年级:2004级学号:04369083指导教师:史亮职称:副教授2008年6月5日摘要计算机博弈是人工智能研究的一个重要分支,被专家门称为人工智能界的果蝇,意思是说人类对计算机博弈的研究衍生了大量的研究成果,这些成果在人工智能领域产生了重要影响。
国际象棋计算机博弈研究已经有了五十多年的历史,IBM公司在1997年开发出了超级计算机“深蓝”战胜了当时世界国际象棋大师卡斯帕罗夫,标志其水平已达到国际象棋世界冠军水平。
而中国象棋的历史更为悠久,虽然中国象棋计算机博弈研究起步晚于国际象棋,但起点高,国际象棋计算机博弈研究的成果为我们提供了很多的借鉴技术。
近年来随着研究的不断深入,中国象棋计算机博弈越来越成为继国际象棋后计算机博弈研究的热点之一。
本文在对目前主流的计算机博弈技术进行全面的综述后,对构成计算机博弈系统的四个组成部分进行了优化和改进,特别是针对静态估值算法不能应对局势变化的固有缺点,提出了动态局势再评估算法。
在此之上实现了一个中国象棋计算机博弈系统,论文主要研究了以下3方面的问题:第一、对计算机博弈系统的四个组成部分及基础技术进行了研究,包括数据结构,着法生成,搜索算法,估值算法。
第二、研究了建立在Alpha-Beta搜索算法基础之上的各种优化技术。
主要讨论了窗口探测,静寂搜索,历史启发,深层迭代,Null Move5个方面的优化方法,并根据实验结果结合置换表技术提出了具体的组合方案。
第三、论文针对目前广泛使用的静态估值算法不能应对局势变化的固有缺点,提出了动态局势再评估算法。
通过引入“局势因子”,使得估值算法根据当前局面形势做出攻防策略。
关键词:人工智能;中国象棋;博弈算法;动态局势再评估;局势因子AbstractComputer game is an important branch of artificial intelligence research. It is described as a fruit fly of the artificial intelligence by experts. That’s to say human’s research to the computer game has achieved massive research results. These achievements have played an important influence on a more widespread domain. Throug overseas researchers’ exploration of chess gambling system for more than 50 years, IBM Corporation developed super computer ”DarkBlue” in1997,and has defeated world chess master Ksparov; while the Chinese chess history is more glorious. The research of Chinese chess computer game is later than the research of chess computer game, but it based on the computer g ame’s research results. With the deeper study of research, Chinese chess computer game becomes one of the most active parts of computer game research area recently.After summarizing related researches on Chinese chess computer game. Some key problems are studied and discussed in this dissertation. Based on above work,an integrated Chinese chess computer game system are designed and developed. The whole work mainly focuses on the following three aspects:1. Introduce the key component parts of a Chinese chess computer game system which involve date structure, generate legal moves, search algorithms and evaluate algorithms.2. Make a study on the optimization of search algorithm based on the Alpha-Beta algorithm which included Principal Variation Search, quiescence search, history heuristic, interative deepening, null-move pruning and so on.3. This paper provides an algorithm called “dynamic situation evaluate algorithm” which avoids the drawback of static evaluate. As the introduction of “decision factor”, computer can make decisions by situation.Key words: artificial intelligence; Chinese chess; game algorithm; dynamic situation evaluate algorithm; decision factor目录第一章绪论 (8)1.1 选题背景和研究意义 (8)1.2 中国象棋计算机博弈的发展历程 (8)1.3 国内外研究现状 (10)1.4 本文的主要工作和论文结构 (11)第二章背景知识 (13)2.1 数据结构 (13)2.1.1 棋盘表示 (13)2.1.2 置换表 (14)2.2 着法生成 (15)2.3 搜索算法 (16)2.3.1 博弈树的基本概念 (16)2.3.2极大极小算法 (17)2.3.3 负极大值法 (19)2.3.4 Alpha-Beta搜索算法 (20)2.4 估值算法 (22)2.5 本章小结 (23)第三章搜索算法的优化 (24)3.1 窗口探测 (24)3.1.1渴望搜索 (24)3.1.2 极小窗口算法 (25)3.2 静寂搜索 (26)3.3 历史启发 (26)3.4 深层迭代 (27)3.5 Null Move (29)3.6 内存优化 (29)3.7 本章小结 (30)第四章动态局势再评估算法 (31)4.1 静态评估算法详述 (31)4.1.1 对子力和攻击性的评估 (31)4.1.2 对棋子位置附加值的评估 (31)4.1.3 对灵活性的评估 (32)4.1.4 对棋子的协调性和保护性的评估 (32)4.1.5 静态估值函数 (33)4.2 静态估值函数的缺陷 (33)4.3 局势因子及动态局势再评估函数 (33)4.4 动态局势再评估算法的步骤 (36)4.5 本章小结 (36)第五章中国象棋计算机博弈系统——出棋制胜的设计与实现 (38)5.1 系统设计 (38)5.1.1中国象棋通用引擎协议层(UCCI) (38)5.1.2 “出棋制胜”软件系统结构图 (39)5.2 详细设计 (39)5.2.1 棋盘棋子表示 (39)5.2.2 着法生成 (40)5.2.3 搜索算法 (43)5.2.4 评估算法 (45)5.2.5 置换表 (45)5.3 实验结果和相关问题的讨论 (46)5.4 本章小结 (47)第六章总结 (48)致谢 (49)参考文献 (50)ContentsChapter 1 Introduction (8)1.1 Research Topics’ Background and Significance (8)1.2 Chinese Chess Computer Game’s Developing Process (8)1.3 The Status Quo at Home and Abroad (10)1.4 The Main Work and Structure of this thesis (11)Chapter 2 Background Knowledge (13)2.1 Date Structure (13)2.1.1 Chess Board Expression (13)2.1.2 Transposing Table (14)2.2 Moves Generation (15)2.3 Search Algorithms (16)2.3.1 game tree’s concept (16)2.3.2 Minimax Algorithm (17)2.3.3 Negamax Algorithm (19)2.3.4 Alpha-Beta Algorithm (20)2.4 Evaluate Algorithm (22)2.5 Chapter Summary (23)Chapter 3 The Optimization of Search Algorithm (24)3.1 Window Detection (24)3.1.1 Eager Search (24)3.1.2 Principal Variation Search (25)3.2 Quiescence Search (26)3.3 History Heuristic (26)3.4 Interative Deepening (27)3.5 Null Move (29)3.6 Memory Optimization (29)3.7 Chapter Summary (30)Chaper 4 Dynamic Situation Evaluate Algorithm (31)4.1 Static Evaluate Algorithm (31)4.1.1 The Evaluation of The C hessman’s Value (31)4.1.2 The Evaluation of The C hessman’s Position (31)4.1.3 The Evaluation of The C hessman’s Movability (32)4.1.4 The Evaluation of The C hessman’s Compatibility (32)4.1.5 Static Evaluate Method (33)4.2 Static Evaluate Method’s Disadvantage (33)4.3 Decision Factor and Dynamic Situation Evaluate Algorithm (33)4.4 The Steps of Dynamic Situation Evaluate Algorithm (36)4.5 Chapter Summary (36)Chaper 5 Chinese Chess Computer Game System (38)5.1 System Design (38)5.1.1 Universal Chinese Chess Protocol (UCCI) (38)5.1.2 The Structure of The System (39)5.2 Detailed Design (39)5.2.1 Chess Board Expression (39)5.2.2 Moves Generation (40)5.2.3 Search Algorithms (43)5.2.4 Evaluate Algorithm (45)5.2.5 Transposing Table (45)5.3 The Experimental Results and Discuss Related Issues (46)5.4 Chapter Summary (47)Chapter 6 Summary (48)Acknowledgement (49)References (50)第一章绪论1.1 选题背景和研究意义计算机博弈是人工智能研究的一个重要分支,被专家门称为人工智能界的果蝇,意思是说人类对计算机博弈的研究衍生了大量的研究成果,这些成果在人工智能领域产生了重要影响。