Free pascal 中国象棋源代码
中国象棋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()这是一个简单的中国象棋游戏代码示例,包括棋盘的绘制、棋子移动的判断和执行等功能。
象棋手机游戏源代码

作者:dlut_608_#4 来源:本站整理发布时间:2005-11-24 17:22:42123;if(point[guard2][guard1]<=16){g.setColor(255,255,0);g.fillArc(x-chessR+guard1*cellWidth,x-chessR+guard2*cellWidth,2*chessR,2*chessR,0,360);g.setColor(0x00000000);g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE));g.drawString(word[guard2][guard1],x+guard1*cellWidth,x+chessR+guard2*cellWidth,Graphics.HCENTER|Graphics.BOTTOM); }}if(g2%2==0){if(point[guard2][guard1]>16){g.setColor(0,255,0);g.fillArc(x-chessR+guard1*cellWidth,x-chessR+guard2*cellWidth,2*chessR,2*chessR,0,360);g.setColor(0x00000000);g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE));g.drawString(word[guard2][guard1],x+guard1*cellWidth,x+chessR+guard2*cellWidth,Graphics.HCENTER|Graphics.BOTTOM); }}}}protected void whoIsGoing(Graphics g)//判断该谁走了{checkWin();g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE));if(isRedWin!=0){if(g2%2==1){g.setColor(255,0,0);g.drawString("该红方走了",x,x+chessR+10*cellWidth,Graphics.LEFT|Graphics.BOTTOM);}}else{ g.setColor(255,255,255);g.drawString("白方胜利",x,x+chessR+10*cellWidth,Graphics.LEFT|Graphics.BOTTOM);}if(isWhiteWin!=0){if(g2%2==0){g.setColor(255,255,255);g.drawString("该白方走了",x,x+chessR+10*cellWidth,Graphics.LEFT|Graphics.BOTTOM);}}else{ g.setColor(255,0,0);g.drawString("红方胜利",x,x+chessR+10*cellWidth,Graphics.LEFT|Graphics.BOTTOM);}}protected void checkWin()//判断输赢{ isRedWin=0;isWhiteWin=0;for(i=0;i<3;i++){for(j=0;j<3;j++){if(point[0+i][3+j]==5){isRedWin++;}}}for(i=0;i<3;i++){for(j=0;j<3;j++){if(point[7+i][3+j]==21){isWhiteWin++;}}}}protected void paintSelected(Graphics g)//画选择框{g.setColor(0,0,255);g.drawRect(x-chessR+selectedX*cellWidth,x-chessR+selectedY*cellWidth,2*chessR,2*chessR); }protected void paint(Graphics g){g.setColor(0x00000000);g.fillRect(0, 0, getWidth(), getHeight());paintMapa(g);paintMapb(g);paintMapc(g);paintAllChess(g);if(guard%2==1){chooseChess(g);}paintSelected(g);whoIsGoing(g);}protected void changTwoChessNum(int m,int n,int selectedX,int selectedY)//改变两个格子的值{g2++;p=point[selectedY][selectedX];point[selectedY][selectedX]=point[n][m];point[n][m]=0;q=word[selectedY][selectedX];word[selectedY][selectedX]=word[n][m];word[n][m]="空";}protected void theRuleOfChe(int m,int n,int selectedX,int selectedY)//车的规则{g=0;if(m==selectedX){if(n>selectedY){for(i=1;i {if(point[selectedY+i][m]!=0){g++;}}}else{for(i=1;i {if(point[n+i][m]!=0){g++;}}}if(g==0){changTwoChessNum(m,n,selectedX,selectedY);}}if(n==selectedY){if(m>selectedX){for(i=1;i {if(point[n][i+selectedX]!=0){g++;}}}else{for(i=1;i {if(point[n][m+i]!=0){g++;}}}if(g==0){changTwoChessNum(m,n,selectedX,selectedY);}}}protected void theRuleOfMa(int m,int n,int selectedX,int selectedY)//马的规则{if(n<9){if(point[n+1][m]==0){if(selectedX-m==1){if(selectedY-n==2){changTwoChessNum(m,n,selectedX,selectedY);}} }}if(n>0){if(point[n-1][m]==0){if(m-selectedX==1){if(n-selectedY==2){changTwoChessNum(m,n,selectedX,selectedY);}} }}if(n<9){if(point[n+1][m]==0){if(selectedX-m==-1){if(selectedY-n==2){changTwoChessNum(m,n,selectedX,selectedY);}} }}if(n>0){if(point[n-1][m]==0){if(m-selectedX==-1){if(n-selectedY==2){changTwoChessNum(m,n,selectedX,selectedY);}} }}if(m<8){if(point[n][m+1]==0){if(selectedX-m==2){if(selectedY-n==1){changTwoChessNum(m,n,selectedX,selectedY);}}}}if(m>0){if(point[n][m-1]==0){if(m-selectedX==2){if(n-selectedY==1){changTwoChessNum(m,n,selectedX,selectedY);}}}}if(m<8){if(point[n][m+1]==0){if(selectedimport Javax.microedition.lcdui.Display;import javax.microedition.midlet.MIDlet;public class Game extends MIDlet {GameCanvas game;//定义游戏界面的Canvas类GameCanvas的对象gobangpublic Game() {super();game=new GameCanvas(this);//生成GameCanvas类的对象game}protected void startApp(){Display.getDisplay(this).setCurrent(game);//在屏幕上绘出游戏见面game}protected void pauseApp(){}protected void destroyApp(boolean arg0){}}然后就是程序的主题部分了--GameCanvas,这里实现了从画棋盘棋子一直到判定和输出.我的主题思想是把棋盘初始化为一个2维数组,在有棋子的地方初始化为非0数,其他的都初始化为0;大家可在代码中看到,在图象输出和棋子移动也都是基于这个数组进行的。
中国象棋源代码Java程序

import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;import java.io.*;public class Chess{public static void main(String args[]){new ChessMainFrame("中国象棋:观棋不语真君子,棋死无悔大丈夫");}}class ChessMainFrame extends JFrame implements ActionListener,MouseListener,Runnable{//玩家JLabel play[] = new JLabel[32];//棋盘JLabel image;//窗格Container con;//工具栏JToolBar jmain;//重新开始JButton anew;//悔棋JButton repent;//退出JButton exit;//当前信息JLabel text;//保存当前操作Vector Var;//规则类对象(使于调用方法)ChessRule rule;/**** 单击棋子** chessManClick = true 闪烁棋子并给线程响应** chessManClick = false 吃棋子停止闪烁并给线程响应*/boolean chessManClick;/**** 控制玩家走棋** chessPlayClick=1 黑棋走棋** chessPlayClick=2 红棋走棋默认红棋** chessPlayClick=3 双方都不能走棋*/int chessPlayClick=2;//控制棋子闪烁的线程Thread tmain;//把第一次的单击棋子给线程响应static int Man,i;ChessMainFrame(){new ChessMainFrame("中国象棋");}/**** 构造函数** 初始化图形用户界面*/ChessMainFrame(String Title){//获行客格引用con = this.getContentPane();con.setLayout(null);//实例化规则类rule = new ChessRule();Var = new Vector();//创建工具栏jmain = new JToolBar();text = new JLabel("欢迎使用象棋对弈系统");//当鼠标放上显示信息text.setToolTipText("信息提示");anew = new JButton(" 新游戏 ");anew.setToolTipText("重新开始新的一局");exit = new JButton(" 退出 ");exit.setToolTipText("退出象棋程序程序");repent = new JButton(" 悔棋 ");repent.setToolTipText("返回到上次走棋的位置");//把组件添加到工具栏jmain.setLayout(new GridLayout(0,4));jmain.add(anew);jmain.add(repent);jmain.add(exit);jmain.add(text);jmain.setBounds(0,0,558,30);con.add(jmain);//添加棋子标签drawChessMan();//注册按扭监听anew.addActionListener(this);repent.addActionListener(this);exit.addActionListener(this);//注册棋子移动监听for (int i=0;i<32;i++){con.add(play[i]);play[i].addMouseListener(this);}//添加棋盘标签con.add(image = new JLabel(new ImageIcon("image\\Main.GIF")));image.setBounds(0,30,558,620);image.addMouseListener(this);//注册窗体关闭监听this.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent we){System.exit(0);}});//窗体居中Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();Dimension frameSize = this.getSize();if (frameSize.height > screenSize.height){frameSize.height = screenSize.height;}if (frameSize.width > screenSize.width){frameSize.width = screenSize.width;}this.setLocation((screenSize.width - frameSize.width) / 2 - 280 ,(screenSize.height - frameSize.height ) / 2 - 350);//设置this.setIconImage(new ImageIcon("image\\红将.GIF").getImage());this.setResizable(false);this.setTitle(Title);this.setSize(558,670);this.show();}/**** 添加棋子方法*/public void drawChessMan(){//流程控制int i,k;//图标Icon in;//黑色棋子//车in = new ImageIcon("image\\黑车.GIF");for (i=0,k=24;i<2;i++,k+=456){play[i] = new JLabel(in);play[i].setBounds(k,56,55,55);play[i].setName("车1");}//马in = new ImageIcon("image\\黑马.GIF");for (i=4,k=81;i<6;i++,k+=342){play[i] = new JLabel(in);play[i].setBounds(k,56,55,55);play[i].setName("马1");}//相in = new ImageIcon("image\\黑象.GIF");for (i=8,k=138;i<10;i++,k+=228){play[i] = new JLabel(in);play[i].setBounds(k,56,55,55);play[i].setName("象1");}//士in = new ImageIcon("image\\黑士.GIF"); for (i=12,k=195;i<14;i++,k+=114){ play[i] = new JLabel(in);play[i].setBounds(k,56,55,55);play[i].setName("士1");}//卒in = new ImageIcon("image\\黑卒.GIF"); for (i=16,k=24;i<21;i++,k+=114){ play[i] = new JLabel(in);play[i].setBounds(k,227,55,55);play[i].setName("卒1" + i);}//炮in = new ImageIcon("image\\黑炮.GIF"); for (i=26,k=81;i<28;i++,k+=342){ play[i] = new JLabel(in);play[i].setBounds(k,170,55,55);play[i].setName("炮1" + i);}//将in = new ImageIcon("image\\黑将.GIF"); play[30] = new JLabel(in);play[30].setBounds(252,56,55,55);play[30].setName("将1");//红色棋子//车in = new ImageIcon("image\\红车.GIF"); for (i=2,k=24;i<4;i++,k+=456){play[i] = new JLabel(in);play[i].setBounds(k,569,55,55);play[i].setName("车2");}//马in = new ImageIcon("image\\红马.GIF"); for (i=6,k=81;i<8;i++,k+=342){play[i] = new JLabel(in);play[i].setBounds(k,569,55,55);play[i].setName("马2");}//相in = new ImageIcon("image\\红象.GIF"); for (i=10,k=138;i<12;i++,k+=228){ play[i] = new JLabel(in);play[i].setBounds(k,569,55,55);play[i].setName("象2");}//士in = new ImageIcon("image\\红士.GIF"); for (i=14,k=195;i<16;i++,k+=114){ play[i] = new JLabel(in);play[i].setBounds(k,569,55,55);play[i].setName("士2");}//兵in = new ImageIcon("image\\红卒.GIF"); for (i=21,k=24;i<26;i++,k+=114){ play[i] = new JLabel(in);play[i].setBounds(k,398,55,55);play[i].setName("卒2" + i);}//炮in = new ImageIcon("image\\红炮.GIF"); for (i=28,k=81;i<30;i++,k+=342){ play[i] = new JLabel(in);play[i].setBounds(k,455,55,55);play[i].setName("炮2" + i);}//帅in = new ImageIcon("image\\红将.GIF"); play[31] = new JLabel(in);play[31].setBounds(252,569,55,55);play[31].setName("帅2");}/**** 线程方法控制棋子闪烁*/public void run(){while (true){//单击棋子第一下开始闪烁if (chessManClick){play[Man].setVisible(false);//时间控制try{tmain.sleep(200);}catch(Exception e){}play[Man].setVisible(true);}//闪烁当前提示信息以免用户看不见else {text.setVisible(false);//时间控制try{tmain.sleep(250);}catch(Exception e){}text.setVisible(true);}try{tmain.sleep(350);}catch (Exception e){}}}/**** 单击棋子方法*/public void mouseClicked(MouseEvent me){System.out.println("Mouse");//当前坐标int Ex=0,Ey=0;//启动线程if (tmain == null){tmain = new Thread(this);tmain.start();}//单击棋盘(移动棋子)if (me.getSource().equals(image)){//该红棋走棋的时候if (chessPlayClick == 2 && play[Man].getName().charAt(1) == '2'){Ex = play[Man].getX();Ey = play[Man].getY();//移动卒、兵if (Man > 15 && Man < 26){rule.armsRule(Man,play[Man],me);}//移动炮else if (Man > 25 && Man < 30){rule.cannonRule(play[Man],play,me);}//移动车else if (Man >=0 && Man < 4){rule.cannonRule(play[Man],play,me);}//移动马else if (Man > 3 && Man < 8){rule.horseRule(play[Man],play,me);}//移动相、象else if (Man > 7 && Man < 12){rule.elephantRule(Man,play[Man],play,me);}//移动仕、士else if (Man > 11 && Man < 16){rule.chapRule(Man,play[Man],play,me);}//移动将、帅else if (Man == 30 || Man == 31){rule.willRule(Man,play[Man],play,me);}//是否走棋错误(是否在原地没有动)if (Ex == play[Man].getX() && Ey == play[Man].getY()){text.setText(" 红棋走棋");chessPlayClick=2;}else {text.setText(" 黑棋走棋");chessPlayClick=1;}}//if//该黑棋走棋的时候else if (chessPlayClick == 1 && play[Man].getName().charAt(1) == '1'){Ex = play[Man].getX();Ey = play[Man].getY();//移动卒、兵if (Man > 15 && Man < 26){rule.armsRule(Man,play[Man],me);}//移动炮else if (Man > 25 && Man < 30){rule.cannonRule(play[Man],play,me);}//移动车else if (Man >=0 && Man < 4){rule.cannonRule(play[Man],play,me);}//移动马else if (Man > 3 && Man < 8){rule.horseRule(play[Man],play,me);}//移动相、象else if (Man > 7 && Man < 12){rule.elephantRule(Man,play[Man],play,me);}//移动仕、士else if (Man > 11 && Man < 16){rule.chapRule(Man,play[Man],play,me);}//移动将、帅else if (Man == 30 || Man == 31){rule.willRule(Man,play[Man],play,me);}//是否走棋错误(是否在原地没有动)if (Ex == play[Man].getX() && Ey == play[Man].getY()){ text.setText(" 黑棋走棋");chessPlayClick=1;}else {text.setText(" 红棋走棋");chessPlayClick=2;}}//else if//当前没有操作(停止闪烁)chessManClick=false;}//if//单击棋子else{//第一次单击棋子(闪烁棋子)if (!chessManClick){for (int i=0;i<32;i++){//被单击的棋子if (me.getSource().equals(play[i])){//告诉线程让该棋子闪烁Man=i;//开始闪烁chessManClick=true;break;}}//for}//if//第二次单击棋子(吃棋子)else if (chessManClick){//当前没有操作(停止闪烁)chessManClick=false;for (i=0;i<32;i++){//找到被吃的棋子if (me.getSource().equals(play[i])){//该红棋吃棋的时候if (chessPlayClick == 2 && play[Man].getName().charAt(1) == '2'){Ex = play[Man].getX();Ey = play[Man].getY();//卒、兵吃规则if (Man > 15 && Man < 26){rule.armsRule(play[Man],play[i]);}//炮吃规则else if (Man > 25 && Man < 30){rule.cannonRule(0,play[Man],play[i],play,me);}//车吃规则else if (Man >=0 && Man < 4){rule.cannonRule(1,play[Man],play[i],play,me);}//马吃规则else if (Man > 3 && Man < 8){rule.horseRule(play[Man],play[i],play,me);}//相、象吃规则else if (Man > 7 && Man < 12){rule.elephantRule(play[Man],play[i],play);}//士、仕吃棋规则else if (Man > 11 && Man < 16){rule.chapRule(Man,play[Man],play[i],play);}//将、帅吃棋规则else if (Man == 30 || Man == 31){rule.willRule(Man,play[Man],play[i],play);play[Man].setVisible(true);}//是否走棋错误(是否在原地没有动)if (Ex == play[Man].getX() && Ey == play[Man].getY()){text.setText(" 红棋走棋");chessPlayClick=2;break;}else{text.setText(" 黑棋走棋");chessPlayClick=1;break;}}//if//该黑棋吃棋的时候else if (chessPlayClick == 1 &&play[Man].getName().charAt(1) == '1'){Ex = play[Man].getX();Ey = play[Man].getY();//卒吃规则if (Man > 15 && Man < 26){rule.armsRule(play[Man],play[i]);}//炮吃规则else if (Man > 25 && Man < 30){rule.cannonRule(0,play[Man],play[i],play,me);}//车吃规则else if (Man >=0 && Man < 4){rule.cannonRule(1,play[Man],play[i],play,me);}//马吃规则else if (Man > 3 && Man < 8){rule.horseRule(play[Man],play[i],play,me);}//相、象吃规则else if (Man > 7 && Man < 12){rule.elephantRule(play[Man],play[i],play);}//士、仕吃棋规则else if (Man > 11 && Man < 16){rule.chapRule(Man,play[Man],play[i],play);}//将、帅吃棋规则else if (Man == 30 || Man == 31){rule.willRule(Man,play[Man],play[i],play);play[Man].setVisible(true);}//是否走棋错误(是否在原地没有动)if (Ex == play[Man].getX() && Ey == play[Man].getY()){text.setText(" 黑棋走棋");chessPlayClick=1;break;}else {text.setText(" 红棋走棋");chessPlayClick=2;break;}}//else if}//if}//for//是否胜利if (!play[31].isVisible()){JOptionPane.showConfirmDialog(this,"黑棋胜利","玩家一胜利",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);//双方都不可以在走棋了chessPlayClick=3;text.setText(" 黑棋胜利");}//ifelse if (!play[30].isVisible()){JOptionPane.showConfirmDialog(this,"红棋胜利","玩家二胜利",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);chessPlayClick=3;text.setText(" 红棋胜利");}//else if}//else}//else}public void mousePressed(MouseEvent me){}public void mouseReleased(MouseEvent me){}public void mouseEntered(MouseEvent me){}public void mouseExited(MouseEvent me){}/**** 定义按钮的事件响应*/public void actionPerformed(ActionEvent ae) { //重新开始按钮if (ae.getSource().equals(anew)){int i,k;//重新排列每个棋子的位置//黑色棋子//车for (i=0,k=24;i<2;i++,k+=456){play[i].setBounds(k,56,55,55);}//马for (i=4,k=81;i<6;i++,k+=342){play[i].setBounds(k,56,55,55);}//相for (i=8,k=138;i<10;i++,k+=228){play[i].setBounds(k,56,55,55);}//士for (i=12,k=195;i<14;i++,k+=114){play[i].setBounds(k,56,55,55);}//卒for (i=16,k=24;i<21;i++,k+=114){ play[i].setBounds(k,227,55,55); }//炮for (i=26,k=81;i<28;i++,k+=342){ play[i].setBounds(k,170,55,55); }//将play[30].setBounds(252,56,55,55);//红色棋子//车for (i=2,k=24;i<4;i++,k+=456){ play[i].setBounds(k,569,55,55); }//马for (i=6,k=81;i<8;i++,k+=342){ play[i].setBounds(k,569,55,55); }//相for (i=10,k=138;i<12;i++,k+=228){ play[i].setBounds(k,569,55,55); }//士for (i=14,k=195;i<16;i++,k+=114){ play[i].setBounds(k,569,55,55); }//兵for (i=21,k=24;i<26;i++,k+=114){ play[i].setBounds(k,398,55,55); }//炮for (i=28,k=81;i<30;i++,k+=342){ play[i].setBounds(k,455,55,55); }//帅play[31].setBounds(252,569,55,55);chessPlayClick = 2;text.setText(" 红棋走棋");for (i=0;i<32;i++){play[i].setVisible(true);}//清除Vector中的容Var.clear();}//悔棋按钮else if (ae.getSource().equals(repent)){try{//获得setVisible属性值String S = (String)Var.get(Var.size()-4);//获得X坐标int x = Integer.parseInt((String)Var.get(Var.size()-3));//获得Y坐标int y = Integer.parseInt((String)Var.get(Var.size()-2));//获得索引int M = Integer.parseInt((String)Var.get(Var.size()-1));//赋给棋子play[M].setVisible(true);play[M].setBounds(x,y,55,55);if (play[M].getName().charAt(1) == '1'){text.setText(" 黑棋走棋");chessPlayClick = 1;}else{text.setText(" 红棋走棋");chessPlayClick = 2;}//删除用过的坐标Var.remove(Var.size()-4);Var.remove(Var.size()-3);Var.remove(Var.size()-2);Var.remove(Var.size()-1);//停止旗子闪烁chessManClick=false;}catch(Exception e){}}//退出else if (ae.getSource().equals(exit)){int j=JOptionPane.showConfirmDialog(this,"真的要退出吗?","退出",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);if (j == JOptionPane.YES_OPTION){System.exit(0);}}}/*定义中国象棋规则的类*/class ChessRule {/**卒子的移动规则*/public void armsRule(int Man,JLabel play,MouseEvent me){ //黑卒向下if (Man < 21){//向下移动、得到终点的坐标模糊成合法的坐标if ((me.getY()-play.getY()) > 27 && (me.getY()-play.getY()) < 86 && (me.getX()-play.getX()) < 55 && (me.getX()-play.getX()) > 0){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));play.setBounds(play.getX(),play.getY()+57,55,55);}//向右移动、得到终点的坐标模糊成合法的坐标、必须过河else if (play.getY() > 284 && (me.getX() - play.getX()) >= 57 && (me.getX() - play.getX()) <= 112){play.setBounds(play.getX()+57,play.getY(),55,55);}//向左移动、得到终点的坐标模糊成合法的坐标、必须过河else if (play.getY() > 284 && (play.getX() - me.getX()) >= 2 && (play.getX() - me.getX()) <=58){//模糊坐标play.setBounds(play.getX()-57,play.getY(),55,55);}}//红卒向上else{//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));//向上移动、得到终点的坐标模糊成合法的坐标if ((me.getX()-play.getX()) >= 0 && (me.getX()-play.getX()) <= 55 && (play.getY()-me.getY()) >27 && play.getY()-me.getY() < 86){play.setBounds(play.getX(),play.getY()-57,55,55);}//向右移动、得到终点的坐标模糊成合法的坐标、必须过河else if (play.getY() <= 341 && (me.getX() - play.getX()) >= 57 && (me.getX() - play.getX()) <= 112){play.setBounds(play.getX()+57,play.getY(),55,55);}//向左移动、得到终点的坐标模糊成合法的坐标、必须过河else if (play.getY() <= 341 && (play.getX() - me.getX()) >= 3 && (play.getX() - me.getX()) <=58){play.setBounds(play.getX()-57,play.getY(),55,55);}}}//卒移动结束/**卒吃棋规则*/public void armsRule(JLabel play1,JLabel play2){//向右走if ((play2.getX() - play1.getX()) <= 112 && (play2.getX() - play1.getX()) >= 57 && (play1.getY() - play2.getY()) < 22 && (play1.getY() - play2.getY()) > -22 && play2.isVisible() && play1.getName().charAt(1)!=play2.getName().charAt(1)){//黑棋要过河才能右吃棋if (play1.getName().charAt(1) == '1' && play1.getY() > 284 && play1.getName().charAt(1) != play2.getName().charAt(1)){play2.setVisible(false);//把对方的位置给自己play1.setBounds(play2.getX(),play2.getY(),55,55);}//红棋要过河才左能吃棋else if (play1.getName().charAt(1) == '2' && play1.getY() < 341 && play1.getName().charAt(1) != play2.getName().charAt(1)){play2.setVisible(false);//把对方的位置给自己play1.setBounds(play2.getX(),play2.getY(),55,55);}}//向左走else if ((play1.getX() - play2.getX()) <= 112 && (play1.getX() - play2.getX()) >= 57 && (play1.getY() - play2.getY()) < 22 && (play1.getY() - play2.getY()) > -22 && play2.isVisible() && play1.getName().charAt(1)!=play2.getName().charAt(1)){//黑棋要过河才能左吃棋if (play1.getName().charAt(1) == '1' && play1.getY() > 284 && play1.getName().charAt(1) != play2.getName().charAt(1)){play2.setVisible(false);//把对方的位置给自己play1.setBounds(play2.getX(),play2.getY(),55,55);}//红棋要过河才能右吃棋else if (play1.getName().charAt(1) == '2' && play1.getY() < 341 && play1.getName().charAt(1) != play2.getName().charAt(1)){play2.setVisible(false);//把对方的位置给自己play1.setBounds(play2.getX(),play2.getY(),55,55);}}//向上走else if (play1.getX() - play2.getX() >= -22 && play1.getX() - play2.getX() <= 22 && play1.getY() - play2.getY() >= -112 && play1.getY() - play2.getY() <= 112){//黑棋不能向上吃棋if (play1.getName().charAt(1) == '1' && play1.getY() < play2.getY() && play1.getName().charAt(1) != play2.getName().charAt(1)){play2.setVisible(false);//把对方的位置给自己play1.setBounds(play2.getX(),play2.getY(),55,55);}//红棋不能向下吃棋else if (play1.getName().charAt(1) == '2' && play1.getY() > play2.getY() && play1.getName().charAt(1) != play2.getName().charAt(1)){play2.setVisible(false);//把对方的位置给自己play1.setBounds(play2.getX(),play2.getY(),55,55);}}//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play1.isVisible()));Var.add(String.valueOf(play1.getX()));Var.add(String.valueOf(play1.getY()));Var.add(String.valueOf(Man));//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play2.isVisible()));Var.add(String.valueOf(play2.getX()));Var.add(String.valueOf(play2.getY()));Var.add(String.valueOf(i));}//卒吃结束/**炮、车移动规则*/public void cannonRule(JLabel play,JLabel playQ[],MouseEventme){//起点和终点之间是否有棋子int Count = 0;//上、下移动if (play.getX() - me.getX() <= 0 && play.getX() - me.getX() >= -55){//指定所有模糊Y坐标for (int i=56;i<=571;i+=57){//移动的Y坐标是否有指定坐标相近的if (i - me.getY() >= -27 && i - me.getY() <= 27){//所有的棋子for (int j=0;j<32;j++){//找出在同一条竖线的所有棋子、并不包括自己if (playQ[j].getX() - play.getX() >= -27 && playQ[j].getX() - play.getX() <= 27 && playQ[j].getName()!=play.getName() && playQ[j].isVisible()){//从起点到终点(从左到右)for (int k=play.getY()+57;k<i;k+=57){//大于起点、小于终点的坐标就可以知道中间是否有棋子if (playQ[j].getY() < i && playQ[j].getY() > play.getY()){//中间有一个棋子就不可以从这条竖线过去Count++;break;}}//for//从起点到终点(从右到左)for (int k=i+57;k<play.getY();k+=57){//找起点和终点的棋子if (playQ[j].getY() < play.getY() && playQ[j].getY() > i){Count++;break;}}//for}//if}//for//起点和终点没有棋子就可以移动了if (Count == 0){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));play.setBounds(play.getX(),i,55,55);break;}}//if}//for}//if//左、右移动else if (play.getY() - me.getY() >=-27 && play.getY() - me.getY() <= 27){//指定所有模糊X坐标for (int i=24;i<=480;i+=57){//移动的X坐标是否有指定坐标相近的if (i - me.getX() >= -55 && i-me.getX() <= 0){//所有的棋子for (int j=0;j<32;j++){//找出在同一条横线的所有棋子、并不包括自己if (playQ[j].getY() - play.getY() >= -27 && playQ[j].getY() - play.getY() <= 27 && playQ[j].getName()!=play.getName() && playQ[j].isVisible()){//从起点到终点(从上到下)for (int k=play.getX()+57;k<i;k+=57){//大于起点、小于终点的坐标就可以知道中间是否有棋子if (playQ[j].getX() < i && playQ[j].getX() > play.getX()){//中间有一个棋子就不可以从这条横线过去Count++;break;}}//for//从起点到终点(从下到上)for (int k=i+57;k<play.getX();k+=57){//找起点和终点的棋子if (playQ[j].getX() < play.getX() && playQ[j].getX() > i){Count++;break;}}//for}//if}//for//起点和终点没有棋子if (Count == 0){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));play.setBounds(i,play.getY(),55,55);break;}}//if}//for}//else}//炮、车移动方法结束/**炮、车吃棋规则*/public void cannonRule(int Chess,JLabel play,JLabel playTake,JLabel playQ[],MouseEvent me){//起点和终点之间是否有棋子int Count = 0;//所有的棋子for (int j=0;j<32;j++){//找出在同一条竖线的所有棋子、并不包括自己if (playQ[j].getX() - play.getX() >= -27 && playQ[j].getX() - play.getX() <= 27 && playQ[j].getName()!=play.getName() && playQ[j].isVisible()){//自己是起点被吃的是终点(从上到下)for (int k=play.getY()+57;k<playTake.getY();k+=57){//大于起点、小于终点的坐标就可以知道中间是否有棋子if (playQ[j].getY() < playTake.getY() && playQ[j].getY() > play.getY()){//计算起点和终点的棋子个数Count++;break;}}//for//自己是起点被吃的是终点(从下到上)for (int k=playTake.getY();k<play.getY();k+=57){//找起点和终点的棋子if (playQ[j].getY() < play.getY() && playQ[j].getY() > playTake.getY()){Count++;break;}}//for}//if//找出在同一条竖线的所有棋子、并不包括自己else if (playQ[j].getY() - play.getY() >= -10 && playQ[j].getY() - play.getY() <= 10 && playQ[j].getName()!=play.getName() && playQ[j].isVisible()){//自己是起点被吃的是终点(从左到右)for (int k=play.getX()+50;k<playTake.getX();k+=57){//大于起点、小于终点的坐标就可以知道中间是否有棋子if (playQ[j].getX() < playTake.getX() && playQ[j].getX() > play.getX()){Count++;break;}}//for//自己是起点被吃的是终点(从右到左)for (int k=playTake.getX();k<play.getX();k+=57){//找起点和终点的棋子if (playQ[j].getX() < play.getX() && playQ[j].getX() > playTake.getX()){Count++;break;}}//for}//if}//for//起点和终点之间要一个棋子是炮的规则、并不能吃自己的棋子if (Count == 1 && Chess == 0 && playTake.getName().charAt(1) != play.getName().charAt(1)){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(playTake.isVisible()));Var.add(String.valueOf(playTake.getX()));Var.add(String.valueOf(playTake.getY()));Var.add(String.valueOf(i));playTake.setVisible(false);play.setBounds(playTake.getX(),playTake.getY(),55,55);}//起点和终点之间没有棋子是车的规则、并不能吃自己的棋子else if (Count ==0 && Chess == 1 && playTake.getName().charAt(1) != play.getName().charAt(1)){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(playTake.isVisible()));Var.add(String.valueOf(playTake.getX()));Var.add(String.valueOf(playTake.getY()));Var.add(String.valueOf(i));playTake.setVisible(false);play.setBounds(playTake.getX(),playTake.getY(),55,55);}}//炮、车吃棋方法结束/**马移动规则*/public void horseRule(JLabel play,JLabel playQ[],MouseEvent me){ //保存坐标和障碍int Ex=0,Ey=0,Move=0;//上移、左边if (play.getX() - me.getX() >= 2 && play.getX() - me.getX() <= 57 && play.getY() - me.getY() >= 87 && play.getY() - me.getY() <= 141){ //合法的Y坐标for (int i=56;i<=571;i+=57){//移动的Y坐标是否有指定坐标相近的if (i - me.getY() >= -27 && i - me.getY() <= 27){Ey = i;break;}}//合法的X坐标for (int i=24;i<=480;i+=57){//移动的X坐标是否有指定坐标相近的if (i - me.getX() >= -55 && i-me.getX() <= 0){Ex = i;break;}}//正前方是否有别的棋子for (int i=0;i<32;i++){if (playQ[i].isVisible() && play.getX() - playQ[i].getX() == 0 && play.getY() - playQ[i].getY() == 57 ){Move = 1;break;}}//可以移动该棋子if (Move == 0){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));play.setBounds(Ex,Ey,55,55);}}//if//左移、上边else if (play.getY() - me.getY() >= 27 && play.getY() - me.getY() <= 86 && play.getX() - me.getX() >= 70 && play.getX() - me.getX() <= 130){//Yfor (int i=56;i<=571;i+=57){if (i - me.getY() >= -27 && i - me.getY() <= 27){Ey = i;}}//Xfor (int i=24;i<=480;i+=57){if (i - me.getX() >= -55 && i-me.getX() <= 0){Ex = i;}}//正左方是否有别的棋子for (int i=0;i<32;i++){if (playQ[i].isVisible() && play.getY() - playQ[i].getY() == 0 && play.getX() - playQ[i].getX() == 57 ){Move = 1;break;}}if (Move == 0){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));play.setBounds(Ex,Ey,55,55);}}//else//下移、右边else if (me.getY() - play.getY() >= 87 && me.getY() - play.getY() <= 141 && me.getX() - play.getX() <= 87 && me.getX() - play.getX() >= 2 ){//Yfor (int i=56;i<=571;i+=57){if (i - me.getY() >= -27 && i - me.getY() <= 27){Ey = i;}}//Xfor (int i=24;i<=480;i+=57){if (i - me.getX() >= -55 && i-me.getX() <= 0){Ex = i;}}//正下方是否有别的棋子for (int i=0;i<32;i++){if (playQ[i].isVisible() && play.getX() - playQ[i].getX() == 0 && playQ[i].getY() - play.getY() == 57 ){Move = 1;break;}}if (Move == 0){//当前记录添加到集合(用于悔棋)Var.add(String.valueOf(play.isVisible()));Var.add(String.valueOf(play.getX()));Var.add(String.valueOf(play.getY()));Var.add(String.valueOf(Man));play.setBounds(Ex,Ey,55,55);}}//else//上移、右边else if (play.getY() - me.getY() >= 87 && play.getY() - me.getY() <= 141 && me.getX() - play.getX() <= 87 && me.getX() - play.getX() >= 30 ){//合法的Y坐标for (int i=56;i<=571;i+=57){if (i - me.getY() >= -27 && i - me.getY() <= 27){。
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();}。
中国象棋(代码)

中国象棋(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();}。
中国象棋游戏代码

中国象棋游戏代码#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;}。
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;}}*/。
pascal中国象棋

else if (x2<>1)and(x2<>3)and(x2<>5) then begin writeln('象不可以过河');inc(e);panduan;end;
if (x1=0)or(x2=0)or(y1=0)or(y2=0) then begin writeln('请输入正确的格式!');inc(e);panduan; end;
if (x1=x2)and(y1=y2) then k:=true ;
if (cishu mod 2=0)and(x[x1,y1]>7) then begin writeln('该红方下了!');inc(e);panduan; end
else if xh then begin xh:=false; write('绿方是否同意?(Y or N)');end
else begin writeln('红方悔棋次数已满');t:=false;end;
if t then begin ch:=readkey;
if ch in ['y','Y'] then begin x[x1,y1]:=x[x2,y2];x[x2,y2]:=huancun;clrscr;dec(cishu);exit;end;end;
乱码没关系 程序本体不乱 能运行 否则无法使用(棋子看不清)
规则: 请输入红方姓名和绿方姓名
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Free pascal 中国象棋源代码[By angwuy]中国象棋的源程序,中文模式下方可使用。
update:对修改程序向原作者angwuy道歉程序代码:typeqp=array[0..9,1..9]of shortint;constes:array['a'..'i']of byte=(1,2,3,4,5,6,7,8,9);se:array[ 1 .. 9 ]of char=('a','b','c','d','e','f','g','h','i'); ci:array['0'..'9']of byte=(0,1,2,3,4,5,6,7,8,9);qz:array[ 1 ..14 ]of string[2]=('車','馬','炮','仕','相','兵','帅','车','马','包','士','象','卒','将');yqp:qp=(( 8, 9,12,11,14,11,12, 9, 8),( 0, 0, 0, 0, 0, 0, 0, 0, 0),( 0,10, 0, 0, 0, 0, 0,10, 0),(13, 0,13, 0,13, 0,13, 0,13),( 0, 0, 0, 0, 0, 0, 0, 0, 0),( 0, 0, 0, 0, 0, 0, 0, 0, 0),( 6, 0, 6, 0, 6, 0, 6, 0, 6),( 0, 3, 0, 0, 0, 0, 0, 3, 0),( 0, 0, 0, 0, 0, 0, 0, 0, 0),( 1, 2, 5, 4, 7, 4, 5, 2, 1));vart,sx,sy,ex,ey,bushu:integer;qipan:qp;procedure initqp(var a:qp);var i,j:integer;beginfillchar(a,sizeof(a),0);for i:=1 to 9 dofor j:=0 to 9 doa[j,i]:=yqp[j,i];end;procedure print(q:qp);var i,j:integer;b:array[1..10,1..9]of string[2];beginwriteln;writeln('中国象棋[By angwuy]');writeln('红:帅仕相車馬炮兵');writeln('黑:将士象车马包卒');writeln;for i:=1 to 10 dofor j:=1 to 8 dob[i,j]:='+-';for i:=1 to 10 dob[i,9]:='-+';for i:=1 to 10 dofor j:=1 to 9 doif q[i-1,j]>0 then b[i,j]:=qz[q[i-1,j]];writeln(' a b c d e f g h i');writeln('0',b[1,1],'--',b[1,2],'--',b[1,3],'--',b[1,4],'--',b[1,5],'--',b[1 ,6],'--',b[1,7],'--',b[1,8],'-',b[1,9]);writeln(' | | | | \ | / | | | |');writeln('1',b[2,1],'--',b[2,2],'--',b[2,3],'--',b[2,4],'--',b[2,5],'--',b[2 ,6],'--',b[2,7],'--',b[2,8],'-',b[2,9]);writeln(' | | | | / | \ | | | |');writeln('2',b[3,1],'--',b[3,2],'--',b[3,3],'--',b[3,4],'--',b[3,5],'--',b[3 ,6],'--',b[3,7],'--',b[3,8],'-',b[3,9]);writeln(' | | | | | | | | |');writeln('3',b[4,1],'--',b[4,2],'--',b[4,3],'--',b[4,4],'--',b[4,5],'--',b[4 ,6],'--',b[4,7],'--',b[4,8],'-',b[4,9]);writeln(' | | | | | | | | |');writeln('4',b[5,1],'--',b[5,2],'--',b[5,3],'--',b[5,4],'--',b[5,5],'--',b[5 ,6],'--',b[5,7],'--',b[5,8],'-',b[5,9]);writeln(' | 楚河汉界 |');writeln('5',b[6,1],'--',b[6,2],'--',b[6,3],'--',b[6,4],'--',b[6,5],'--',b[6 ,6],'--',b[6,7],'--',b[6,8],'-',b[6,9]);writeln(' | | | | | | | | |');writeln('6',b[7,1],'--',b[7,2],'--',b[7,3],'--',b[7,4],'--',b[7,5],'--',b[7 ,6],'--',b[7,7],'--',b[7,8],'-',b[7,9]);writeln(' | | | | | | | | |');writeln('7',b[8,1],'--',b[8,2],'--',b[8,3],'--',b[8,4],'--',b[8,5],'--',b[8 ,6],'--',b[8,7],'--',b[8,8],'-',b[8,9]);writeln(' | | | | \ | / | | | |');writeln('8',b[9,1],'--',b[9,2],'--',b[9,3],'--',b[9,4],'--',b[9,5],'--',b[9,6],'--',b[9,7],'--',b[9,8],'-',b[9,9]);writeln(' | | | | / | \ | | | |');writeln('9',b[10,1],'--',b[10,2],'--',b[10,3],'--',b[10,4],'--',b[10,5],'--',b[10,6],'--',b[10,7],'--',b[10,8],'-',b[10,9]);end;function checkred(a:qp;sx,sy,ex,ey:integer):boolean;var i,j,t:integer;begincheckred:=true;if not(a[sy,sx] in [1..7]) then begin checkred:=false;exit;end; if a[ey,ex] in [1..7] then begin checkred:=false;exit;end;if (ey=sy)and(ex=sx) then begin checkred:=false;exit;end;case a[sy,sx] of1:beginif (ey=sy)or(ex=sx) then else begin checkred:=false;exit;end; if sx=ex thenbeginif ey>sy thenbeginfor i:=sy+1 to ey-1 doif a[i,sx]>0 then begin checkred:=false;exit;end;endelse if sy>ey thenbeginfor i:=sy-1 downto ey+1 doif a[i,sx]>0 then begin checkred:=false;exit;end;end;endelsebeginif ex>sx thenbeginfor i:=sx+1 to ex-1 doif a[sy,i]>0 then begin checkred:=false;exit;end;endelse if sy>ey thenbeginfor i:=sx-1 downto ex+1 doif a[sy,i]>0 then begin checkred:=false;exit;end;end;end;end;2:begini:=ey-sy;j:=ex-sx;if ((abs(i)=1)and(abs(j)=2))or((abs(i)=2)and(abs(j)=1)) thenelse begin checkred:=false;exit;end;if (j=2) thenbeginif a[sy,sx+1]>0 then begin checkred:=false;exit;end;endelse if (j=-2) thenbeginif a[sy,sx-1]>0 then begin checkred:=false;exit;end;endelse if (i=2) thenbeginif a[sy+1,sx]>0 then begin checkred:=false;exit;end;endelse if (i=-2) thenbeginif a[sy-1,sx]>0 then begin checkred:=false;exit;end;end;end;3:beginif (ey=sy)or(ex=sx) then else begin checkred:=false;exit;end; if sx=ex thenbeginif ey>sy thenbegint:=0;for i:=sy+1 to ey-1 doif a[i,sx]>0 then inc(t);if ((t=0)and(a[ey,ex]=0))or((t=1)and(a[ey,ex]>0)) thenelse begin checkred:=false;exit;end;endelse if sy>ey thenbegint:=0;for i:=sy-1 downto ey+1 doif a[i,sx]>0 then inc(t);if ((t=0)and(a[ey,ex]=0))or((t=1)and(a[ey,ex]>0)) thenelse begin checkred:=false;exit;end;end;end elseif sy=ey thenbeginif ex>sx thenbegint:=0;for i:=sx+1 to ex-1 doif a[sy,i]>0 then inc(t);if ((t=0)and(a[ey,ex]=0))or((t=1)and(a[ey,ex]>0)) thenelse begin checkred:=false;exit;end;endelse if sx>ex thenbegint:=0;for i:=sx-1 downto ex+1 doif a[sy,i]>0 then inc(t);if ((t=0)and(a[ey,ex]=0))or((t=1)and(a[ey,ex]>0)) thenelse begin checkred:=false;exit;end;end;end;end;4:begini:=ey-sy;j:=ex-sx;if (abs(i)=1)and(abs(j)=1) then else begin checkred:=false;exit;end;if (ey in [7..9])and(ex in [4..6]) then else begin checkred:=false;exit;end;end;5:begini:=ey-sy;j:=ex-sx;if (abs(i)=2)and(abs(j)=2) then else begin checkred:=false;exit;end;if a[(ey+sy)div 2,(ex+sx)div 2]>0 then begin checkred:=false;exit;end;if (ey in [9,7,5])and(ex in [1,3,5,7,9]) then else begin checkred:=false;exit;end;end;6:begini:=ey-sy;j:=ex-sx;if (i=-1)and(j=0) thenelse if (i=0)and(abs(j)=1)and(sy<5) thenelse begin checkred:=false;exit;end;end;7:begini:=ey-sy;j:=ex-sx;if ((abs(i)=1)and(j=0))or((abs(j)=1)and(i=0)) thenbeginif (ey in [7..9])and(ex in [4..6]) then else begin checkred:=false;exit;end;endelsebeginif a[ey,ex]<>14 then begin checkred:=false;exit;end;for i:=sy-1 downto ey+1 do if a[i,ex]>0 then begin checkred:=false;exit;end;end;end;end;end;function checkblack(a:qp;sx,sy,ex,ey:integer):boolean;var i,j,t:integer;begincheckblack:=true;if not(a[sy,sx] in [8..14]) then begin checkblack:=false;exit;end; if a[ey,ex] in [8..14] then begin checkblack:=false;exit;end;if (ey=sy)and(ex=sx) then begin checkblack:=false;exit;end;case a[sy,sx] of8:beginif (ey=sy)or(ex=sx) then else begin checkblack:=false;exit;end;if sx=ex thenbeginif ey>sy thenbeginfor i:=sy+1 to ey-1 doif a[i,sx]>0 then begin checkblack:=false;exit;end; endelse if sy>ey thenbeginfor i:=sy-1 downto ey+1 doif a[i,sx]>0 then begin checkblack:=false;exit;end; end;endelsebeginif ex>sx thenbeginfor i:=sx+1 to ex-1 doif a[sy,i]>0 then begin checkblack:=false;exit;end; endelse if sx>ex thenbeginfor i:=sx-1 downto ex+1 doif a[sy,i]>0 then begin checkblack:=false;exit;end; end;end;end;9:begini:=ey-sy;j:=ex-sx;if ((abs(i)=1)and(abs(j)=2))or((abs(i)=2)and(abs(j)=1)) thenelse begin checkblack:=false;exit;end;if (j=2) thenbeginif a[sy,sx+1]>0 then begin checkblack:=false;exit;end;endelse if (j=-2) thenbeginif a[sy,sx-1]>0 then begin checkblack:=false;exit;end;endelse if (i=2) thenbeginif a[sy+1,sx]>0 then begin checkblack:=false;exit;end;endelse if (i=-2) thenbeginif a[sy-1,sx]>0 then begin checkblack:=false;exit;end;end;end;10:beginif (ey=sy)or(ex=sx) then else begin checkblack:=false;exit;end;if sx=ex thenbeginif ey>sy thenbegint:=0;for i:=sy+1 to ey-1 doif a[i,sx]>0 then inc(t);if ((t=0)and(a[ey,ex]=0))or((t=1)and(a[ey,ex]>0)) thenelse begin checkblack:=false;exit;end;endelse if sy>ey thenbegint:=0;for i:=sy-1 downto ey+1 doif a[i,sx]>0 then inc(t);if ((t=0)and(a[ey,ex]=0))or((t=1)and(a[ey,ex]>0)) thenelse begin checkblack:=false;exit;end;end;end;if sy=ey thenbeginif ex>sx thenbegint:=0;for i:=sx+1 to ex-1 doif a[sy,i]>0 then inc(t);if ((t=0)and(a[ey,ex]=0))or((t=1)and(a[ey,ex]>0)) thenelse begin checkblack:=false;exit;end;endelse if sx>ex thenbegint:=0;for i:=sx-1 downto ex+1 doif a[sy,i]>0 then inc(t);if ((t=0)and(a[ey,ex]=0))or((t=1)and(a[ey,ex]>0)) thenelse begin checkblack:=false;exit;end;end;end;end;11:begini:=ey-sy;j:=ex-sx;if (abs(i)=1)and(abs(j)=1) then else begin checkblack:=false;exit;end;if (ey in [0..2])and(ex in [4..6]) then else begin checkblack:=false;exit;end;end;12:begini:=ey-sy;j:=ex-sx;if (abs(i)=2)and(abs(j)=2) then else begincheckblack:=false;exit;end;if a[(ey+sy)div 2,(ex+sx)div 2]>0 then begin checkblack:=false;exit;end;if (ey in [0,2,4])and(ex in [1,3,5,7,9]) then else begin checkblack:=false;exit;end;end;13:begini:=ey-sy;j:=ex-sx;if (i=1)and(j=0) thenelse if (i=0)and(abs(j)=1)and(sy>4) thenelse begin checkblack:=false;exit;end;end;14:begini:=ey-sy;j:=ex-sx;if ((abs(i)=1)and(j=0))or((abs(j)=1)and(i=0)) thenbeginif (ey in [0..2])and(ex in [4..6]) then else begin checkblack:=false;exit;end;endelsebeginif a[ey,ex]<>7 then begin checkblack:=false;exit;end;for i:=sy+1 to ey-1 do if a[i,ex]=0 then begin checkblack:=false;exit;end;end;end;end;end;procedure getline(var c1,c2,c3,c4:integer);var st:string;beginwhile true dobeginwrite('red:');readln(st);if not(st[1] in ['a'..'i']) then continue;if not(st[2] in ['0'..'9']) then continue;if not(st[3] in ['a'..'i']) then continue;if not(st[4] in ['0'..'9']) then continue;if copy(st,1,2)=copy(st,3,2) then continue;c1:=es[st[1]];c2:=ci[st[2]];c3:=es[st[3]];c4:=ci[st[4]];if checkred(qipan,c1,c2,c3,c4) then break;end;end;function fenzhi(q:qp):integer;var i,j,i1,j1:integer;begint:=0;for i:=1 to 9 dofor j:=0 to 9 dobeginif (q[j,i]=8)and(i in [2,4,6,8])and(bushu<30) then inc(t,10); if (q[i,j]=8)and(j in [1,4,6,7]) then inc(t,10);if (q[i,j]=8)and(j=3) then dec(t,5);if (q[j,i]=yqp[j,i])and(q[j,i] in [8..14])and(bushu<50) then dec(t,2);if (q[j,i] in [8..10,13])and(j>5)and(bushu>10) then inc(t,(14-q[j,i]));if (q[j,i]=13)and(q[j+2,i]=6)and(q[j+3,i]=2) then inc(t,10); if (q[j,i]=13)and(q[j-2,i]=9)and(q[j+2,i]=6) then inc(t,10); if (q[j,i]=8)and(j=1)and(i=5) then dec(t,40);case q[j,i] of1:dec(t,100);2:if bushu<30 then dec(t,40) else dec(t,50);3:if bushu<50 then dec(t,50) else dec(t,40);4,5:dec(t,20);6:if bushu<50 then dec(t,10)else if (j>5)or(j=0) then dec(t,20)else dec(t,30);7:dec(t,10000);8:inc(t,100);9:if bushu<30 then inc(t,40) else inc(t,50);10:if bushu<50 then inc(t,50) else inc(t,40);11,12:inc(t,20);13:if bushu<50 then inc(t,10)else if (j>5)or(j=0) then inc(t,20)else inc(t,30);14:inc(t,10000);end;end;if q[1,5] in[8,9,10,14] then dec(t,10);if (bushu<50)and(q[0,5]<>14) then dec(t,18);if (q[3,5]=3)and checkred(q,5,3,5,1) and (bushu<50) then dec(t,30); if (q[4,5]=3)and checkred(q,5,4,5,1) and (bushu<50) then dec(t,30); if (q[5,5]=3)and checkred(q,5,5,5,1) and (bushu<50) then dec(t,30); if (q[6,5]=3)and checkred(q,5,6,5,1) and (bushu<50) then dec(t,30); if (q[7,5]=3)and checkred(q,5,7,5,1) and (bushu<50) then dec(t,30);if (q[2,1]=12) then dec(t,18);if (q[2,9]=12) then dec(t,18);if (q[2,5]=12) then inc(t,10);if (q[2,5] in [1..9,10..13,14])and(q[4,5]=13)and(q[7,5] in [0,3]) then dec(t,10);if (bushu<10)and(q[2,5]=10) then inc(t,15);if (q[0,1]=8) then dec(t,25);if (q[0,9]=8) then dec(t,25);if (q[0,2]=9) then dec(t,18);if (q[0,8]=9) then dec(t,18);if (q[2,1]=9)and(q[2,9]=9) then dec(t,10);fenzhi:=t;end;function panfen(q:qp;dep:integer):integer;varqi1,qi2,hqi:qp;i1,i2,i3,i4,j1,j2,j3,j4,t,t1,t2:integer;beginif dep=0 thenbeginpanfen:=fenzhi(q);exit;end;t:=-32768;for i1:=1 to 9 dofor i2:=0 to 9 doif q[i2,i1] in [8..14] thenfor i3:=1 to 9 dofor i4:=0 to 9 doif checkblack(q,i1,i2,i3,i4) thenbeginqi1:=q;qi1[i4,i3]:=qi1[i2,i1];qi1[i2,i1]:=0;t1:=32767;for j1:=1 to 9 dofor j2:=0 to 9 doif q[j2,j1] in [1..7] thenfor j3:=1 to 9 dofor j4:=0 to 9 doif checkred(qi1,j1,j2,j3,j4) thenbeginqi2:=qi1;qi2[j4,j3]:=qi2[j2,j1];qi2[j2,j1]:=0;t2:=panfen(qi2,0);if t2<=t1 then begin t1:=t2;hqi:=qi2;end;end;if t1<-5000 then continue;t1:=panfen(hqi,dep-1);if t1>t thenbegint:=t1;end;end;panfen:=t;end;procedure searchblack(q:qp;var c1,c2,c3,c4:integer);varqi1,qi2,hqi:qp;i1,i2,i3,i4,j1,j2,j3,j4,t,h1,h2,h3,h4,t1,t2:integer;begint:=-32768;for i1:=1 to 9 dofor i2:=0 to 9 doif q[i2,i1] in [8..14] thenfor i3:=1 to 9 dofor i4:=0 to 9 doif checkblack(q,i1,i2,i3,i4) thenbeginqi1:=q;qi1[i4,i3]:=qi1[i2,i1];qi1[i2,i1]:=0;if fenzhi(qi1)>5000 then begin c1:=i1;c2:=i2;c3:=i3;c4:=i4;exit;end;t1:=32767;for j1:=1 to 9 dofor j2:=0 to 9 doif q[j2,j1] in [1..7] thenfor j3:=1 to 9 dofor j4:=0 to 9 doif checkred(qi1,j1,j2,j3,j4) thenbeginqi2:=qi1;qi2[j4,j3]:=qi2[j2,j1];qi2[j2,j1]:=0;t2:=panfen(qi2,0);if t2<=t1 then begin t1:=t2;hqi:=qi2;end;end;if t1<-5000 then continue;t1:=panfen(hqi,1);if t1>t thenbegint:=t1;h1:=i1;h2:=i2;h3:=i3;h4:=i4;end;end;c1:=h1;c2:=h2;c3:=h3;c4:=h4;end;beginwriteln('使用说明:输入包括4个字符,分别为字母和数字,字母数字'); writeln('前面两个表示你要移动的那个子现在的坐标,后面代表目标坐标'); initqp(qipan);print(qipan);bushu:=1;while true dobegingetline(sx,sy,ex,ey);qipan[ey,ex]:=qipan[sy,sx];qipan[sy,sx]:=0;writeln('busy...');searchblack(qipan,sx,sy,ex,ey);writeln('black:',se[sx],sy,se[ex],ey);qipan[ey,ex]:=qipan[sy,sx];qipan[sy,sx]:=0;inc(bushu,2);print(qipan);end;end.。