五子棋源程序
知识共享-五子棋java实现源代码(雷惊风)

经典的五子棋java程序(源带码)直接复制粘贴importjava.awt.*;importjava.awt.event.*;importjava.applet.Applet;importjava.awt.Color;publicclassenzitextends Applet implements ActionListener,MouseListener,MouseMotionListener,ItemListener{intcolor_Qizi=0;//旗子的颜色标识0:白子1:黑子intintGame_Start=0;//游戏开始标志0未开始1游戏中intintGame_Body[][]=newint[16][16]; //设置棋盘棋子状态0 无子1 白子2 黑子Button b1=new Button("游戏开始");Button b2=new Button("重置游戏");Label lblWin=new Label(" ");Checkbox ckbHB[]=new Checkbox[2];CheckboxGroupckgHB=new CheckboxGroup();publicvoidinit(){setLayout(null);addMouseListener(this);add(b1);b1.setBounds(330,50,80,30);b1.addActionListener(this);add(b2);b2.setBounds(330,90,80,30);b2.addActionListener(this);ckbHB[0]=new Checkbox("白子先",ckgHB,false);ckbHB[0].setBounds(320,20,60,30);ckbHB[1]=new Checkbox("黑子先",ckgHB,false);ckbHB[1].setBounds(380,20,60,30);add(ckbHB[0]);add(ckbHB[1]);ckbHB[0].addItemListener(this);ckbHB[1].addItemListener(this);add(lblWin);lblWin.setBounds(330,130,80,30);Game_start_csh();}publicvoiditemStateChanged(ItemEvent e){if (ckbHB[0].getState()) //选择黑子先还是白子先{color_Qizi=0;}else{color_Qizi=1;}}publicvoidactionPerformed(ActionEvent e){Graphics g=getGraphics();if (e.getSource()==b1){Game_start();}else{Game_re();}}publicvoidmousePressed(MouseEvent e){}publicvoidmouseClicked(MouseEvent e){Graphics g=getGraphics();int x1,y1;x1=e.getX();y1=e.getY();if (e.getX()<20 || e.getX()>300 || e.getY()<20 || e.getY()>300) {return;}if (x1%20>10){x1+=20;}if(y1%20>10){y1+=20;}x1=x1/20*20;y1=y1/20*20;set_Qizi(x1,y1);}publicvoidmouseEntered(MouseEvent e){}publicvoidmouseExited(MouseEvent e){}publicvoidmouseReleased(MouseEvent e){}publicvoidmouseDragged(MouseEvent e){}publicvoidmouseMoved(MouseEvent e){}publicvoid paint(Graphics g){draw_qipan(g);}publicvoid set_Qizi(intx,int y) //落子{if (intGame_Start==0) //判断游戏未开始{return;}if (intGame_Body[x/20][y/20]!=0){return;}Graphics g=getGraphics();if (color_Qizi==1)//判断黑子还是白子{g.setColor(Color.black);color_Qizi=0;}else{g.setColor(Color.white);color_Qizi=1;}g.fillOval(x-10,y-10,20,20);intGame_Body[x/20][y/20]=color_Qizi+1;if (Game_win_1(x/20,y/20)) //判断输赢{lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!"); intGame_Start=0;}if (Game_win_2(x/20,y/20)) //判断输赢{lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!"); intGame_Start=0;}if (Game_win_3(x/20,y/20)) //判断输赢{lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!"); intGame_Start=0;}if (Game_win_4(x/20,y/20)) //判断输赢{lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!"); intGame_Start=0;}}public String Get_qizi_color(int x){if (x==0){return"黑子";}else{return"白子";}}publicvoid draw_qipan(Graphics G) //画棋盘15*15{G.setColor(Color.lightGray);G.fill3DRect(10,10,300,300,true);G.setColor(Color.black);for(inti=1;i<16;i++){G.drawLine(20,20*i,300,20*i);G.drawLine(20*i,20,20*i,300);}}publicvoid Game_start() //游戏开始{intGame_Start=1;Game_btn_enable(false);b2.setEnabled(true);}publicvoid Game_start_csh() //游戏开始初始化{intGame_Start=0;Game_btn_enable(true);b2.setEnabled(false);ckbHB[0].setState(true);for (inti=0;i<16 ;i++ ){for (int j=0;j<16 ;j++ ){intGame_Body[j]=0;}}lblWin.setText("");}publicvoid Game_re() //游戏重新开始{repaint();Game_start_csh();}publicvoid Game_btn_enable(boolean e) //设置组件状态{b1.setEnabled(e);b2.setEnabled(e);ckbHB[0].setEnabled(e);ckbHB[1].setEnabled(e);}publicboolean Game_win_1(intx,int y) //判断输赢横{int x1,y1,t=1;x1=x;y1=y;for (inti=1;i<5 ;i++ ){if (x1>15){break;}if (intGame_Body[x1+i][y1]==intGame_Body[x][y]) {t+=1;}else{break;}}for (inti=1;i<5 ;i++ ){if (x1<1){break;}if(intGame_Body[x1-i][y1]==intGame_Body[x][y]) {t+=1;}else{break;}}if (t>4){returntrue;}else{returnfalse;}}publicboolean Game_win_2(intx,int y) //判断输赢竖{int x1,y1,t=1;x1=x;y1=y;for (inti=1;i<5 ;i++ ){if (x1>15){break;}if (intGame_Body[x1][y1+i]==intGame_Body[x][y]){t+=1;}else{break;}}for (inti=1;i<5 ;i++ ){if (x1<1){break;}if(intGame_Body[x1][y1-i]==intGame_Body[x][y]) {t+=1;}else{break;}}if (t>4){returntrue;}else{returnfalse;}}publicboolean Game_win_3(intx,int y) //判断输赢左斜{int x1,y1,t=1;x1=x;y1=y;for (inti=1;i<5 ;i++ ){if (x1>15){break;}if (intGame_Body[x1+i][y1-i]==intGame_Body[x][y]) {t+=1;}else{break;}}for (inti=1;i<5 ;i++ ){if (x1<1){break;}if(intGame_Body[x1-i][y1+i]==intGame_Body[x][y]) {t+=1;}else{break;}}if (t>4){returntrue;}else{returnfalse;}}publicboolean Game_win_4(intx,int y) //判断输赢左斜{int x1,y1,t=1;x1=x;y1=y;for (inti=1;i<5 ;i++ ){if (x1>15){break;}if (intGame_Body[x1+i][y1+i]==intGame_Body[x][y]) {t+=1;}else{break;}}for (inti=1;i<5 ;i++ ){if (x1<1){break;}if(intGame_Body[x1-i][y1-i]==intGame_Body[x][y]) {t+=1;}else{break;}}if (t>4){returntrue;}else{returnfalse;}}}。
Java五子棋游戏源代码(人机对战)

//Java编程:五子棋游戏源代码import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import java.io.PrintStream;import javax.swing.JComponent;import javax.swing.JPanel;/**main方法创建了ChessFrame类的一个实例对象(cf),*并启动屏幕显示显示该实例对象。
**/public class FiveChessAppletDemo {public static void main(String args[]){ChessFrame cf = new ChessFrame();cf.show();}}/**类ChessFrame主要功能是创建五子棋游戏主窗体和菜单**/class ChessFrame extends JFrame implements ActionListener { private String[] strsize={"20x15","30x20","40x30"};private String[] strmode={"人机对弈","人人对弈"};public static boolean iscomputer=true,checkcomputer=true; private int width,height;private ChessModel cm;private MainPanel mp;//构造五子棋游戏的主窗体public ChessFrame() {this.setTitle("五子棋游戏");cm=new ChessModel(1);mp=new MainPanel(cm);Container con=this.getContentPane();con.add(mp,"Center");this.setResizable(false);this.addWindowListener(new ChessWindowEvent());MapSize(20,15);JMenuBar mbar = new JMenuBar();this.setJMenuBar(mbar);JMenu gameMenu = new JMenu("游戏");mbar.add(makeMenu(gameMenu, new Object[] {"开局", "棋盘","模式", null, "退出"}, this));JMenu lookMenu =new JMenu("视图");mbar.add(makeMenu(lookMenu,new Object[] {"Metal","Motif","Windows"},this));JMenu helpMenu = new JMenu("帮助");mbar.add(makeMenu(helpMenu, new Object[] {"关于"}, this));}//构造五子棋游戏的主菜单public JMenu makeMenu(Object parent, Object items[], Object target){ JMenu m = null;if(parent instanceof JMenu)m = (JMenu)parent;else if(parent instanceof String)m = new JMenu((String)parent);elsereturn null;for(int i = 0; i < items.length; i++)if(items[i] == null)m.addSeparator();else if(items[i] == "棋盘"){JMenu jm = new JMenu("棋盘");ButtonGroup group=new ButtonGroup();JRadioButtonMenuItem rmenu;for (int j=0;j<strsize.length;j++){rmenu=makeRadioButtonMenuItem(strsize[j],target);if (j==0)rmenu.setSelected(true);jm.add(rmenu);group.add(rmenu);}m.add(jm);}else if(items[i] == "模式"){JMenu jm = new JMenu("模式");ButtonGroup group=new ButtonGroup();JRadioButtonMenuItem rmenu;for (int h=0;h<strmode.length;h++){rmenu=makeRadioButtonMenuItem(strmode[h],target);if(h==0)rmenu.setSelected(true);jm.add(rmenu);group.add(rmenu);}m.add(jm);}elsem.add(makeMenuItem(items[i], target));return m;}//构造五子棋游戏的菜单项public JMenuItem makeMenuItem(Object item, Object target){ JMenuItem r = null;if(item instanceof String)r = new JMenuItem((String)item);else if(item instanceof JMenuItem)r = (JMenuItem)item;elsereturn null;if(target instanceof ActionListener)r.addActionListener((ActionListener)target);return r;}//构造五子棋游戏的单选按钮式菜单项public JRadioButtonMenuItem makeRadioButtonMenuItem( Object item, Object target){JRadioButtonMenuItem r = null;if(item instanceof String)r = new JRadioButtonMenuItem((String)item);else if(item instanceof JRadioButtonMenuItem)r = (JRadioButtonMenuItem)item;elsereturn null;if(target instanceof ActionListener)r.addActionListener((ActionListener)target);return r;}public void MapSize(int w,int h){setSize(w * 20+50 , h * 20+100 );if(this.checkcomputer)this.iscomputer=true;elsethis.iscomputer=false;mp.setModel(cm);mp.repaint();}public boolean getiscomputer(){return this.iscomputer;}public void restart(){int modeChess = cm.getModeChess();if(modeChess <= 3 && modeChess >= 1){cm = new ChessModel(modeChess);MapSize(cm.getWidth(),cm.getHeight());}else{System.out.println("\u81EA\u5B9A\u4E49");}}public void actionPerformed(ActionEvent e){String arg=e.getActionCommand();try{if (arg.equals("Windows"))UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");else if(arg.equals("Motif"))UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");elseUIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel" ); SwingUtilities.updateComponentTreeUI(this);}catch(Exception ee){}if(arg.equals("20x15")){this.width=20;this.height=15;cm=new ChessModel(1);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("30x20")){this.width=30;this.height=20;cm=new ChessModel(2);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("40x30")){this.width=40;this.height=30;cm=new ChessModel(3);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人机对弈")){this.checkcomputer=true;this.iscomputer=true;cm=new ChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人人对弈")){this.checkcomputer=false;this.iscomputer=false;cm=new ChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("开局")){restart();}if(arg.equals("关于"))JOptionPane.showMessageDialog(this, "五子棋游戏测试版本", "关于", 0);if(arg.equals("退出"))System.exit(0);}}/**类ChessModel实现了整个五子棋程序算法的核心*/class ChessModel {//棋盘的宽度、高度、棋盘的模式(如20×15)private int width,height,modeChess;//棋盘方格的横向、纵向坐标private int x=0,y=0;//棋盘方格的横向、纵向坐标所对应的棋子颜色,//数组arrMapShow只有3个值:1,2,3,-5,//其中1代表该棋盘方格上下的棋子为黑子,//2代表该棋盘方格上下的棋子为白子,//3代表为该棋盘方格上没有棋子,//-5代表该棋盘方格不能够下棋子private int[][] arrMapShow;//交换棋手的标识,棋盘方格上是否有棋子的标识符private boolean isOdd,isExist;public ChessModel() {}//该构造方法根据不同的棋盘模式(modeChess)来构建对应大小的棋盘public ChessModel(int modeChess){this.isOdd=true;if(modeChess == 1){PanelInit(20, 15, modeChess);}if(modeChess == 2){PanelInit(30, 20, modeChess);}if(modeChess == 3){PanelInit(40, 30, modeChess);}}//按照棋盘模式构建棋盘大小private void PanelInit(int width, int height, int modeChess){this.width = width;this.height = height;this.modeChess = modeChess;arrMapShow = new int[width+1][height+1];for(int i = 0; i <= width; i++){for(int j = 0; j <= height; j++){arrMapShow[i][j] = -5;}}}//获取是否交换棋手的标识符public boolean getisOdd(){return this.isOdd;}//设置交换棋手的标识符public void setisOdd(boolean isodd){if(isodd)this.isOdd=true;elsethis.isOdd=false;}//获取某棋盘方格是否有棋子的标识值public boolean getisExist(){return this.isExist;}//获取棋盘宽度public int getWidth(){return this.width;}//获取棋盘高度public int getHeight(){return this.height;}//获取棋盘模式public int getModeChess(){return this.modeChess;}//获取棋盘方格上棋子的信息public int[][] getarrMapShow(){return arrMapShow;}//判断下子的横向、纵向坐标是否越界private boolean badxy(int x, int y){if(x >= width+20 || x < 0)return true;return y >= height+20 || y < 0;}//计算棋盘上某一方格上八个方向棋子的最大值,//这八个方向分别是:左、右、上、下、左上、左下、右上、右下public boolean chessExist(int i,int j){if(this.arrMapShow[i][j]==1 || this.arrMapShow[i][j]==2)return true;return false;}//判断该坐标位置是否可下棋子public void readyplay(int x,int y){if(badxy(x,y))return;if (chessExist(x,y))return;this.arrMapShow[x][y]=3;}//在该坐标位置下棋子public void play(int x,int y){if(badxy(x,y))return;if(chessExist(x,y)){this.isExist=true;return;}elsethis.isExist=false;if(getisOdd()){setisOdd(false);this.arrMapShow[x][y]=1;}else{setisOdd(true);this.arrMapShow[x][y]=2;}}//计算机走棋/**说明:用穷举法判断每一个坐标点的四个方向的的最大棋子数,*最后得出棋子数最大值的坐标,下子**/public void computerDo(int width,int height){int max_black,max_white,max_temp,max=0;setisOdd(true);System.out.println("计算机走棋...");for(int i = 0; i <= width; i++){for(int j = 0; j <= height; j++){if(!chessExist(i,j)){//算法判断是否下子max_white=checkMax(i,j,2);//判断白子的最大值max_black=checkMax(i,j,1);//判断黑子的最大值max_temp=Math.max(max_white,max_black);if(max_temp>max){max=max_temp;this.x=i;this.y=j;}}}}setX(this.x);setY(this.y);this.arrMapShow[this.x][this.y]=2;}//记录电脑下子后的横向坐标public void setX(int x){this.x=x;}//记录电脑下子后的纵向坐标public void setY(int y){this.y=y;}//获取电脑下子的横向坐标public int getX(){return this.x;}//获取电脑下子的纵向坐标public int getY(){return this.y;}//计算棋盘上某一方格上八个方向棋子的最大值,//这八个方向分别是:左、右、上、下、左上、左下、右上、右下public int checkMax(int x, int y,int black_or_white){int num=0,max_num,max_temp=0;int x_temp=x,y_temp=y;int x_temp1=x_temp,y_temp1=y_temp;//judge rightfor(int i=1;i<5;i++){x_temp1+=1;if(x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++;elsebreak;}//judge leftx_temp1=x_temp;for(int i=1;i<5;i++){x_temp1-=1;if(x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++;elsebreak;}if(num<5)max_temp=num;//judge upx_temp1=x_temp;y_temp1=y_temp;num=0;for(int i=1;i<5;i++){y_temp1-=1;if(y_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++;elsebreak;}//judge downy_temp1=y_temp;for(int i=1;i<5;i++){y_temp1+=1;if(y_temp1>this.height)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;//judge left_upx_temp1=x_temp;y_temp1=y_temp;num=0;for(int i=1;i<5;i++){x_temp1-=1;y_temp1-=1;if(y_temp1<0 || x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++;elsebreak;}//judge right_downx_temp1=x_temp;y_temp1=y_temp;for(int i=1;i<5;i++){x_temp1+=1;y_temp1+=1;if(y_temp1>this.height || x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;//judge right_upx_temp1=x_temp;y_temp1=y_temp;num=0;for(int i=1;i<5;i++){x_temp1+=1;y_temp1-=1;if(y_temp1<0 || x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++;elsebreak;}//judge left_downx_temp1=x_temp;y_temp1=y_temp;for(int i=1;i<5;i++){x_temp1-=1;y_temp1+=1;if(y_temp1>this.height || x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;max_num=max_temp;return max_num;}//判断胜负public boolean judgeSuccess(int x,int y,boolean isodd){ int num=1;int arrvalue;int x_temp=x,y_temp=y;if(isodd)arrvalue=2;elsearrvalue=1;int x_temp1=x_temp,y_temp1=y_temp;//判断右边for(int i=1;i<6;i++){x_temp1+=1;if(x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}//判断左边x_temp1=x_temp;for(int i=1;i<6;i++){if(x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++;elsebreak;}if(num==5)return true;//判断上方x_temp1=x_temp;y_temp1=y_temp;num=1;for(int i=1;i<6;i++){y_temp1-=1;if(y_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++;elsebreak;}//判断下方y_temp1=y_temp;for(int i=1;i<6;i++){y_temp1+=1;if(y_temp1>this.height)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++;elsebreak;}if(num==5)return true;//判断左上x_temp1=x_temp;y_temp1=y_temp;num=1;for(int i=1;i<6;i++){x_temp1-=1;if(y_temp1<0 || x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++;elsebreak;}//判断右下x_temp1=x_temp;y_temp1=y_temp;for(int i=1;i<6;i++){x_temp1+=1;y_temp1+=1;if(y_temp1>this.height || x_temp1>this.width) break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++;elsebreak;}if(num==5)return true;//判断右上x_temp1=x_temp;y_temp1=y_temp;num=1;for(int i=1;i<6;i++){x_temp1+=1;y_temp1-=1;if(y_temp1<0 || x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++;elsebreak;}//判断左下x_temp1=x_temp;y_temp1=y_temp;for(int i=1;i<6;i++){x_temp1-=1;y_temp1+=1;if(y_temp1>this.height || x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}if(num==5)return true;return false;}//赢棋后的提示public void showSuccess(JPanel jp){JOptionPane.showMessageDialog(jp,"你赢了,好厉害!","win",RMATION_MESSAGE);}//输棋后的提示public void showDefeat(JPanel jp){JOptionPane.showMessageDialog(jp,"你输了,请重新开始!","lost",RMATION_MESSAGE);}}/**类MainPanel主要完成如下功能:*1、构建一个面板,在该面板上画上棋盘;*2、处理在该棋盘上的鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等)**/class MainPanel extends JPanelimplements MouseListener,MouseMotionListener{private int width,height;//棋盘的宽度和高度private ChessModel cm;//根据棋盘模式设定面板的大小MainPanel(ChessModel mm){cm=mm;width=cm.getWidth();height=cm.getHeight();addMouseListener(this);}//根据棋盘模式设定棋盘的宽度和高度public void setModel(ChessModel mm){cm = mm;width = cm.getWidth();height = cm.getHeight();}//根据坐标计算出棋盘方格棋子的信息(如白子还是黑子),//然后调用draw方法在棋盘上画出相应的棋子public void paintComponent(Graphics g){super.paintComponent(g);for(int j = 0; j <= height; j++){for(int i = 0; i <= width; i++){int v = cm.getarrMapShow()[i][j];draw(g, i, j, v);}}}//根据提供的棋子信息(颜色、坐标)画棋子public void draw(Graphics g, int i, int j, int v){int x = 20 * i+20;int y = 20 * j+20;//画棋盘if(i!=width && j!=height){g.setColor(Color.white);g.drawRect(x,y,20,20);}//画黑色棋子if(v == 1 ){g.setColor(Color.gray);g.drawOval(x-8,y-8,16,16);g.setColor(Color.black);g.fillOval(x-8,y-8,16,16);}//画白色棋子if(v == 2 ){g.setColor(Color.gray);g.drawOval(x-8,y-8,16,16);g.setColor(Color.white);g.fillOval(x-8,y-8,16,16);}if(v ==3){g.setColor(Color.cyan);g.drawOval(x-8,y-8,16,16);}}//响应鼠标的点击事件,根据鼠标的点击来下棋,//根据下棋判断胜负等public void mousePressed(MouseEvent evt){int x = (evt.getX()-10) / 20;int y = (evt.getY()-10) / 20;System.out.println(x+" "+y);if (evt.getModifiers()==MouseEvent.BUTTON1_MASK){cm.play(x,y);System.out.println(cm.getisOdd()+" "+cm.getarrMapShow()[x][y]);repaint();if(cm.judgeSuccess(x,y,cm.getisOdd())){cm.showSuccess(this);evt.consume();ChessFrame.iscomputer=false;}//判断是否为人机对弈if(ChessFrame.iscomputer&&!cm.getisExist()){puterDo(cm.getWidth(),cm.getHeight());repaint();if(cm.judgeSuccess(cm.getX(),cm.getY(),cm.getisOdd())){cm.showDefeat(this);evt.consume();}}}}public void mouseClicked(MouseEvent evt){}public void mouseReleased(MouseEvent evt){}public void mouseEntered(MouseEvent mouseevt){}public void mouseExited(MouseEvent mouseevent){}public void mouseDragged(MouseEvent evt){}//响应鼠标的拖动事件public void mouseMoved(MouseEvent moveevt){int x = (moveevt.getX()-10) / 20;int y = (moveevt.getY()-10) / 20;cm.readyplay(x,y);repaint();}}class ChessWindowEvent extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0);}ChessWindowEvent(){}}。
五子棋游戏源代码

import java.awt.*;import java.awt.event.*;import java.io.*;import .*;import java.util.*;class clientThread extends Thread {chessClient chessclient;clientThread(chessClient chessclient) {this.chessclient = chessclient;}public void acceptMessage(String recMessage) {if (recMessage.startsWith("/userlist ")) {StringTokenizer userToken = new StringTokenizer(recMessage, " ");int userNumber = 0;erList.removeAll();erChoice.removeAll();erChoice.addItem("所有人");while (userToken.hasMoreTokens()) {String user = (String) userToken.nextToken(" ");if (userNumber > 0 && !user.startsWith("[inchess]")) {erList.add(user);erChoice.addItem(user);}userNumber++;}erChoice.select("所有人");} else if (recMessage.startsWith("/yourname ")) {chessclient.chessClientName = recMessage.substring(10);chessclient.setTitle("Java五子棋客户端" + "用户名:"+ chessclient.chessClientName);} else if (recMessage.equals("/reject")) {try {chessclient.chesspad.statusText.setText("不能加入游戏");chessclient.controlpad.cancelGameButton.setEnabled(false);chessclient.controlpad.joinGameButton.setEnabled(true);chessclient.controlpad.creatGameButton.setEnabled(true);} catch (Exception ef) {chessclient.chatpad.chatLineArea.setText("chessclient.chesspad.chessSocket.close无法关闭");}chessclient.controlpad.joinGameButton.setEnabled(true);} else if (recMessage.startsWith("/peer ")) {chessclient.chesspad.chessPeerName = recMessage.substring(6);if (chessclient.isServer) {chessclient.chesspad.chessColor = 1;chessclient.chesspad.isMouseEnabled = true;chessclient.chesspad.statusText.setText("请黑棋下子");} else if (chessclient.isClient) {chessclient.chesspad.chessColor = -1;chessclient.chesspad.statusText.setText("已加入游戏,等待对方下子...");}} else if (recMessage.equals("/youwin")) {chessclient.isOnChess = false;chessclient.chesspad.chessVictory(chessclient.chesspad.chessColor);chessclient.chesspad.statusText.setText("对方退出,请点放弃游戏退出连接");chessclient.chesspad.isMouseEnabled = false;} else if (recMessage.equals("/OK")) {chessclient.chesspad.statusText.setText("创建游戏成功,等待别人加入...");} else if (recMessage.equals("/error")) {chessclient.chatpad.chatLineArea.append("传输错误:请退出程序,重新加入\n");} else {chessclient.chatpad.chatLineArea.append(recMessage + "\n");chessclient.chatpad.chatLineArea.setCaretPosition(chessclient.chatpad.chatLineArea.getText().length());}}public void run() {String message = "";try {while (true) {message = chessclient.in.readUTF();acceptMessage(message);}} catch (IOException es) {}}}public class chessClient extends Frame implements ActionListener, KeyListener { userPad userpad = new userPad();chatPad chatpad = new chatPad();controlPad controlpad = new controlPad();chessPad chesspad = new chessPad();inputPad inputpad = new inputPad();Socket chatSocket;DataInputStream in;DataOutputStream out;String chessClientName = null;String host = null;int port = 4331;boolean isOnChat = false; // 在聊天?boolean isOnChess = false; // 在下棋?boolean isGameConnected = false; // 下棋的客户端连接?boolean isServer = false; // 如果是下棋的主机boolean isClient = false; // 如果是下棋的客户端Panel southPanel = new Panel();Panel northPanel = new Panel();Panel centerPanel = new Panel();Panel westPanel = new Panel();Panel eastPanel = new Panel();chessClient() {super("Java五子棋客户端");setLayout(new BorderLayout());host = controlpad.inputIP.getText();westPanel.setLayout(new BorderLayout());westPanel.add(userpad, BorderLayout.NORTH);westPanel.add(chatpad, BorderLayout.CENTER);westPanel.setBackground(Color.pink);inputpad.inputWords.addKeyListener(this);chesspad.host = controlpad.inputIP.getText();centerPanel.add(chesspad, BorderLayout.CENTER);centerPanel.add(inputpad, BorderLayout.SOUTH);centerPanel.setBackground(Color.pink);controlpad.connectButton.addActionListener(this);controlpad.creatGameButton.addActionListener(this);controlpad.joinGameButton.addActionListener(this);controlpad.cancelGameButton.addActionListener(this);controlpad.exitGameButton.addActionListener(this);controlpad.creatGameButton.setEnabled(false);controlpad.joinGameButton.setEnabled(false);controlpad.cancelGameButton.setEnabled(false);southPanel.add(controlpad, BorderLayout.CENTER);southPanel.setBackground(Color.pink);addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {if (isOnChat) {try {chatSocket.close();} catch (Exception ed) {}}if (isOnChess || isGameConnected) {try {chesspad.chessSocket.close();} catch (Exception ee) {}}System.exit(0);}public void windowActivated(WindowEvent ea) {}});add(westPanel, BorderLayout.WEST);add(centerPanel, BorderLayout.CENTER);add(southPanel, BorderLayout.SOUTH);pack();setSize(670, 548);setVisible(true);setResizable(false);validate();}public boolean connectServer(String serverIP, int serverPort)throws Exception {try {chatSocket = new Socket(serverIP, serverPort);in = new DataInputStream(chatSocket.getInputStream());out = new DataOutputStream(chatSocket.getOutputStream());clientThread clientthread = new clientThread(this);clientthread.start();isOnChat = true;return true;} catch (IOException ex) {chatpad.chatLineArea.setText("chessClient:connectServer:无法连接,建议重新启动程序\n");}return false;}public void actionPerformed(ActionEvent e) {if (e.getSource() == controlpad.connectButton) {host = chesspad.host = controlpad.inputIP.getText();try {if (connectServer(host, port)) {chatpad.chatLineArea.setText("");controlpad.connectButton.setEnabled(false);controlpad.creatGameButton.setEnabled(true);controlpad.joinGameButton.setEnabled(true);chesspad.statusText.setText("连接成功,请创建游戏或加入游戏");}} catch (Exception ei) {chatpad.chatLineArea.setText("controlpad.connectButton:无法连接,建议重新启动程序\n");}}if (e.getSource() == controlpad.exitGameButton) {if (isOnChat) {try {chatSocket.close();} catch (Exception ed) {}}if (isOnChess || isGameConnected) {try {chesspad.chessSocket.close();} catch (Exception ee) {}}System.exit(0);}if (e.getSource() == controlpad.joinGameButton) {String selectedUser = erList.getSelectedItem();if (selectedUser == null || selectedUser.startsWith("[inchess]")|| selectedUser.equals(chessClientName)) {chesspad.statusText.setText("必须先选定一个有效用户");} else {try {if (!isGameConnected) {if (chesspad.connectServer(chesspad.host, chesspad.port)) {isGameConnected = true;isOnChess = true;isClient = true;controlpad.creatGameButton.setEnabled(false);controlpad.joinGameButton.setEnabled(false);controlpad.cancelGameButton.setEnabled(true);chesspad.chessthread.sendMessage("/joingame "+ erList.getSelectedItem() + " "+ chessClientName);}} else {isOnChess = true;isClient = true;controlpad.creatGameButton.setEnabled(false);controlpad.joinGameButton.setEnabled(false);controlpad.cancelGameButton.setEnabled(true);chesspad.chessthread.sendMessage("/joingame "+ erList.getSelectedItem() + " "+ chessClientName);}} catch (Exception ee) {isGameConnected = false;isOnChess = false;isClient = false;controlpad.creatGameButton.setEnabled(true);controlpad.joinGameButton.setEnabled(true);controlpad.cancelGameButton.setEnabled(false);chatpad.chatLineArea.setText("chesspad.connectServer无法连接\n" + ee);}}}if (e.getSource() == controlpad.creatGameButton) {try {if (!isGameConnected) {if (chesspad.connectServer(chesspad.host, chesspad.port)) {isGameConnected = true;isOnChess = true;isServer = true;controlpad.creatGameButton.setEnabled(false);controlpad.joinGameButton.setEnabled(false);controlpad.cancelGameButton.setEnabled(true);chesspad.chessthread.sendMessage("/creatgame "+ "[inchess]" + chessClientName);}} else {isOnChess = true;isServer = true;controlpad.creatGameButton.setEnabled(false);controlpad.joinGameButton.setEnabled(false);controlpad.cancelGameButton.setEnabled(true);chesspad.chessthread.sendMessage("/creatgame "+ "[inchess]" + chessClientName);}} catch (Exception ec) {isGameConnected = false;isOnChess = false;isServer = false;controlpad.creatGameButton.setEnabled(true);controlpad.joinGameButton.setEnabled(true);controlpad.cancelGameButton.setEnabled(false);ec.printStackTrace();chatpad.chatLineArea.setText("chesspad.connectServer无法连接\n"+ ec);}}if (e.getSource() == controlpad.cancelGameButton) {if (isOnChess) {chesspad.chessthread.sendMessage("/giveup " + chessClientName);chesspad.chessVictory(-1 * chesspad.chessColor);controlpad.creatGameButton.setEnabled(true);controlpad.joinGameButton.setEnabled(true);controlpad.cancelGameButton.setEnabled(false);chesspad.statusText.setText("请建立游戏或者加入游戏");}if (!isOnChess) {controlpad.creatGameButton.setEnabled(true);controlpad.joinGameButton.setEnabled(true);controlpad.cancelGameButton.setEnabled(false);chesspad.statusText.setText("请建立游戏或者加入游戏");}isClient = isServer = false;}}public void keyPressed(KeyEvent e) {TextField inputWords = (TextField) e.getSource();if (e.getKeyCode() == KeyEvent.VK_ENTER) {if (erChoice.getSelectedItem().equals("所有人")) {try {out.writeUTF(inputWords.getText());inputWords.setText("");} catch (Exception ea) {chatpad.chatLineArea.setText("chessClient:KeyPressed无法连接,建议重新连接\n");erList.removeAll();erChoice.removeAll();inputWords.setText("");controlpad.connectButton.setEnabled(true);}} else {try {out.writeUTF("/" + erChoice.getSelectedItem()+ " " + inputWords.getText());inputWords.setText("");} catch (Exception ea) {chatpad.chatLineArea.setText("chessClient:KeyPressed无法连接,建议重新连接\n");erList.removeAll();erChoice.removeAll();inputWords.setText("");controlpad.connectButton.setEnabled(true);}}}}public void keyTyped(KeyEvent e) {}public void keyReleased(KeyEvent e) {}public static void main(String args[]) {chessClient chessClient = new chessClient();}}。
C语言五子棋游戏源代码

ncl ud e< #define N 10void welcome();void initqipan();void showqi(int i);void save(int p);void panduan(int p);void heqi();void over();int zouqihang();int zouqilie();/****************** 结构体**************** */struct zuobiao{int x[N*N];inty[N*N]; }weizhi[N*N];主函数**************** *//******************void main() {int p=0;welcome();for(p=1;p<=N*N;p++){weizhi[p].x[p]=zouqihang();weizhi[p].y[p]=zouqilie(); save(p);showqi(p);panduan(p);}if(p==N*N)heqi();over();}/****************** 建立棋盘*****************/ void initqipan(){int i,j;for(i=0;i<N;i++){printf("%d",i);printf(" ");}for(i=1;i<N;i++){for(j=0;j<N;j++){if(j==0)printf("%d",i);elseprin tf("•");}printf("\n");}}显示棋子**************** */ void showqi(int p){int i,j,k,m;int a[N*N],b[N*N];FILE *fp;fp=fopen("wuzi_list","rb");for(i=1;i<=N*N;i++)fread(&weizhi[i],sizeof(struct zuobiao),1,fp);{a[i]=weizhi[i].x[i];b[i]=weizhi[i].y[i];}for(m=1;m<p;m++){while(weizhi[p].x[p]==a[m]&&weizhi[p].y[p]==b[m]) {printf("error!\n");weizhi[p].x[p]=zouqihang();weizhi[p].y[p]=zouqilie();m=1;}}for(i=0;i<N;i++){printf("%d",i);printf(" ");}printf("\n");for(i=1;i<N;i++){for(j=1;j<N;j++){if(j==1)printf("%d",i);for(k=1;k<=p;k++){if(i==weizhi[k].x[k]&&j==weizhi[k].y[k]){if(k%2==1){pr intf(" O ");break;} else if(k%2==0){printf(" •");break;} }}if(k>p)pri ntf(" •");else continue;}printf("\n");int zouqihang()走棋行**************** */}int x;printf(" 请输入要走棋子所在行数 !\n" printf("x="); scanf("%d",&x);while(x>N-1||x<1){ printf("error!\n");printf(" 请输入要走棋子所在行数printf("x=");scanf("%d",&x);}return x;} int zouqilie(){int y;printf(" 请输入要走棋子所在列数 !\n printf("y="); scanf("%d",&y);while(y>N-1||y<1) !\n");走棋列 ****************printf("error!\n");printf(" 请输入要走棋子所在列数!\n");printf("y=");scanf("%d",&y);}return y;}文件保存**************** */ void save(int i){FILE *fp; fp=fopen("wuzi_list","wb");fwrite(&weizhi[i],sizeof(struct zuobiao),1,fp);}判断输赢****************** */ void panduan(int p){int i,j,k[8]={1,1,1,1,1,1,1,1,};int a[N*N],b[N*N];FILE *fp;fp=fopen("wuzi_list","rb");for(i=1;i<=p;i++){fread(&weizhi[i],sizeof(struct zuobiao),1,fp);a[i]=weizhi[i].x[i];b[i]=weizhi[i].y[i];判断行***************** */ for(i=1;i<=p;i++){if(i%2==1){for(j=1;j<=p;j=j+2){if((a[i]==a[j])&&(b[i]==b[j]-1)){k[0]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-2)){k[0]++;else if((a[i]==a[j])&&(b[i]==b[j]-3)){continue;k[0]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-4)){k[0]++;continue;}else if(k[0]==5){printf("Player 1 wins!!!\n");}elsecontinue;}if(k[0]==5)break;k[0]=1;}else if(k[0]==5)break;else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j])&&(b[i]==b[j]-1)){k[1]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-2)){k[1]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-3)){k[1]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-4)){k[1]++;continue;}else if(k[1]==5){printf("Player 2 wins!!!\n");}elsecontinue;} if(k[1]==5) break;k[1]=1;判断列********************** for(i=1;i<=p;i++){if(k[0]==5||k[1]==5)break;else if(i%2==1)for(j=1;j<=p;j=j+2){if((a[i]==a[j]-1)&&(b[i]==b[j])){k[2]++;continue;}else if((a[i]==a[j]-2)&&(b[i]==b[j])) {k[2]++;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j])) {k[2]++;continue;}else if((a[i]==a[j]-4)&&(b[i]==b[j])) {k[2]++;else if(k[2]==5){continue;printf("Player 1 wins!!!\n");}elsecontinue;}if(k[2]==5)break;k[2]=1;}else if(k[2]==5)break;else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j]-1)&&(b[i]==b[j])) { k[3]++;else if((a[i]==a[j]-2)&&(b[i]==b[j])) {k[3]++;continue;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j])) {k[3]++;continue;}else if((a[i]==a[j]-4)&&(b[i]==b[j])) {k[3]++;continue;}else if(k[3]==5){printf("Player 2 wins!!!\n");}elsecontinue;三 eaiq(++■—d u—****************(K 49— T tt )**************三 eaiq(9H S >!)七((CXI s q H "二q )0303(cxl sy ''=s 七①s ai宀(DFIUQU8F+T N((L s q H "二q )0303(二=丫世5七(cxl +H r c k v r v D」s(V H XI %W ① s aik[4]++;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j]-3)) {k[4]++;continue;}else if((a[i]==a[j]-4)&&(b[i]==b[j]-4)) {k[4]++;continue;}else if(k[4]==5){printf("Player 1 wins!!!\n");}elsecontinue;}if(k[4]==5)break;k[4]=1;}else if(k[2]==5)break;else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j]-1)&&(b[i]==b[j]-1)){k[5]++;continue;}else if((a[i]==a[j]-2)&&(b[i]==b[j]-2)){k[5]++;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j]-3)){k[5]++;continue;(D S _(D宀 厂三二s u '?cxl」9e _d =)七U K巧“丄亠乂)七①S 05宀(DFIUQU8++◎>!((寸s q H "二q )0303(寸自丫"^))七① S 05-************(++■—d u—(r ^—K tt )«K********三 eaiq(9世令)七宀break;else if(i%2==1){for(j=1;j<=p;j=j+2){if((a[i]==a[j]+1)&&(b[i]==b[j]-1)){k[6]++;continue;}else if((a[i]==a[j]+2)&&(b[i]==b[j]-2)){k[6]++;continue;}else if((a[i]==a[j]+3)&&(b[i]==b[j]-3)){k[6]++;continue;}else if((a[i]==a[j]+4)&&(b[i]==b[j]-4)){continue;}else if(k[6]==5){printf("Player 1 wins!!!\n");}elsecontinue;}if(k[6]==5)break;k[6]=1;}else if(k[6]==5)break;else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j]+1)&&(b[i]==b[j]-1)) continue;}{else if((a[i]==a[j]+2)&&(b[i]==b[j]-2)) {k[7]++;continue;}else if((a[i]==a[j]+3)&&(b[i]==b[j]-3)) {k[7]++;continue;}else if((a[i]==a[j]+4)&&(b[i]==b[j]-4)) {k[7]++;continue;}else if(k[7]==5){printf("Player 2 wins!!!\n");}elsecontinue;}if(k[7]==5)break;k[7]=1;}}}和棋****************** */ void heqi()printf( H************************************ \n");printf(" Tie!!!\n");printf( H************************************ \n");}游戏结束****************** */ void over()printf( H************************************ \n");printf(" game over!!!\n");printf( H************************************ \n");}游戏开始****************** */ void welcome()printf( H************************************ \n");printf(" Welcome!!!\n");printf( H************************************ \n");}。
C语言实现五子棋小游戏源代码

break;
case ESC:
break;
case SPACE:
if(chessx>=1&&chessx<=19&&chessy>=1&&chessy<=19)
{
if(chess[chessx][chessy]==0)
}
if(flag==2)
{
cleardevice();
setfont(36, 0, "宋体", 900, 900, 0, false, false, false);
outtextxy(80,200,"RED win!");
getch();
closegraph();
exit(0);
}
}
if(flag==1)
flag=2;
else
flag=1;
break;
}
void draw_pixel(int x,int y,int color)
{
x=(x+2)*20;
y=(y+2)*20;
{
putpixel(x+8,y,color);
putpixel(x,y-8,color);
putpixel(x+8,y+8,color);
{
chess[chessx][chessy]=flag;
if(result(chessx,chessy)==1)
{
if(flag==1)
{
cleardevice();
n1=0;
n2=0;
C语言五子棋游戏源代码

C语言五子棋游戏源代码标准化管理处编码[BBX968T-XBB8968-NNJ668-MM9N]#define N 10void welcome();void initqipan();void showqi(int i);void save(int p);void panduan(int p);void heqi();void over();int zouqihang();int zouqilie();/******************结构体*****************/ struct zuobiao{int x[N*N];int y[N*N];}weizhi[N*N];/******************主函数*****************/ void main(){int p=0;welcome();initqipan();for(p=1;p<=N*N;p++){weizhi[p].x[p]=zouqihang();weizhi[p].y[p]=zouqilie();save(p);showqi(p);panduan(p);}if(p==N*N)heqi();over();}/******************建立棋盘*****************/ void initqipan(){int i,j;for(i=0;i<N;i++){printf("%d",i);printf(" ");}printf("\n");for(i=1;i<N;i++){for(j=0;j<N;j++){if(j==0)printf("%d",i);elseprintf("·");}printf("\n");}}/******************显示棋子*****************/ void showqi(int p){int i,j,k,m;int a[N*N],b[N*N];FILE *fp;fp=fopen("wuzi_list","rb");for(i=1;i<=N*N;i++){fread(&weizhi[i],sizeof(struct zuobiao),1,fp);a[i]=weizhi[i].x[i];b[i]=weizhi[i].y[i];}for(m=1;m<p;m++){while(weizhi[p].x[p]==a[m]&&weizhi[p].y[p]==b[m]) {printf("error!\n");weizhi[p].x[p]=zouqihang();weizhi[p].y[p]=zouqilie();m=1;}}for(i=0;i<N;i++){printf("%d",i);printf(" ");}printf("\n");for(i=1;i<N;i++){for(j=1;j<N;j++){if(j==1)printf("%d",i);for(k=1;k<=p;k++){if(i==weizhi[k].x[k]&&j==weizhi[k].y[k]) {if(k%2==1){printf("○");break;} else if(k%2==0){printf("●");break;} }}if(k>p)printf("·");else continue;}printf("\n");}}/******************走棋行*****************/ int zouqihang(){int x;printf("请输入要走棋子所在行数!\n");printf("x=");scanf("%d",&x);while(x>N-1||x<1){printf("error!\n");printf("请输入要走棋子所在行数!\n"); printf("x=");scanf("%d",&x);}return x;}/******************走棋列*****************/ int zouqilie(){int y;printf("请输入要走棋子所在列数!\n");printf("y=");scanf("%d",&y);while(y>N-1||y<1){printf("error!\n");printf("请输入要走棋子所在列数!\n"); printf("y=");scanf("%d",&y);}return y;}/******************文件保存*****************/ void save(int i){FILE *fp;fp=fopen("wuzi_list","wb");fwrite(&weizhi[i],sizeof(struct zuobiao),1,fp);}/****************判断输赢*******************/void panduan(int p){int i,j,k[8]={1,1,1,1,1,1,1,1,};int a[N*N],b[N*N];FILE *fp;fp=fopen("wuzi_list","rb");for(i=1;i<=p;i++){fread(&weizhi[i],sizeof(struct zuobiao),1,fp); a[i]=weizhi[i].x[i];b[i]=weizhi[i].y[i];}/*****************判断行******************/for(i=1;i<=p;i++){if(i%2==1){for(j=1;j<=p;j=j+2){if((a[i]==a[j])&&(b[i]==b[j]-1)){k[0]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-2)) {k[0]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-3)) {k[0]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-4)) {k[0]++;continue;}else if(k[0]==5){printf("Player 1 wins!!!\n");}elsecontinue;}if(k[0]==5)break;k[0]=1;}else if(k[0]==5)break;else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j])&&(b[i]==b[j]-1)) {k[1]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-2)) {k[1]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-3)) {k[1]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-4)) {k[1]++;continue;}else if(k[1]==5){printf("Player 2 wins!!!\n");}elsecontinue;}if(k[1]==5)break;k[1]=1;}}/**********************判断列************************/ for(i=1;i<=p;i++){if(k[0]==5||k[1]==5)break;else if(i%2==1){for(j=1;j<=p;j=j+2){if((a[i]==a[j]-1)&&(b[i]==b[j])){k[2]++;continue;}else if((a[i]==a[j]-2)&&(b[i]==b[j])) {k[2]++;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j])) {k[2]++;continue;}else if((a[i]==a[j]-4)&&(b[i]==b[j])) {k[2]++;continue;}else if(k[2]==5){printf("Player 1 wins!!!\n");}elsecontinue;}if(k[2]==5)break;k[2]=1;}else if(k[2]==5)break;else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j]-1)&&(b[i]==b[j])){k[3]++;continue;}else if((a[i]==a[j]-2)&&(b[i]==b[j])) {k[3]++;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j])) {k[3]++;continue;}else if((a[i]==a[j]-4)&&(b[i]==b[j])) {k[3]++;continue;}else if(k[3]==5){printf("Player 2 wins!!!\n");}elsecontinue;}if(k[3]==5)break;k[3]=1;}}/****************判断对角(左上-右下)******************/ for(i=1;i<=p;i++){if(k[0]==5||k[1]==5||k[2]==5||k[3]==5)break;else if(i%2==1){for(j=1;j<=p;j=j+2){if((a[i]==a[j]-1)&&(b[i]==b[j]-1)){k[4]++;continue;}else if((a[i]==a[j]-2)&&(b[i]==b[j]-2)) {k[4]++;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j]-3)) {k[4]++;continue;}else if((a[i]==a[j]-4)&&(b[i]==b[j]-4)) {k[4]++;continue;}else if(k[4]==5){printf("Player 1 wins!!!\n");}elsecontinue;}if(k[4]==5)break;k[4]=1;}else if(k[2]==5)break;else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j]-1)&&(b[i]==b[j]-1)){k[5]++;continue;}else if((a[i]==a[j]-2)&&(b[i]==b[j]-2)) {k[5]++;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j]-3)) {k[5]++;continue;}else if((a[i]==a[j]-4)&&(b[i]==b[j]-4)) {k[5]++;continue;}else if(k[5]==5){printf("Player 2 wins!!!\n");}elsecontinue;}if(k[5]==5)break;k[5]=1;}}/**********判断对角(左下-右上)************/for(i=1;i<=p;i++){if(k[0]==5||k[1]==5||k[2]==5||k[3]==5||k[4]==5||k[5]==5) break;else if(i%2==1){for(j=1;j<=p;j=j+2){if((a[i]==a[j]+1)&&(b[i]==b[j]-1)){k[6]++;continue;}else if((a[i]==a[j]+2)&&(b[i]==b[j]-2)) {k[6]++;continue;}else if((a[i]==a[j]+3)&&(b[i]==b[j]-3)) {k[6]++;continue;}else if((a[i]==a[j]+4)&&(b[i]==b[j]-4))k[6]++;continue;}else if(k[6]==5){printf("Player 1 wins!!!\n"); }elsecontinue;}if(k[6]==5)break;k[6]=1;}else if(k[6]==5)else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j]+1)&&(b[i]==b[j]-1)){k[7]++;continue;}else if((a[i]==a[j]+2)&&(b[i]==b[j]-2)) {k[7]++;continue;}else if((a[i]==a[j]+3)&&(b[i]==b[j]-3))k[7]++;continue;}else if((a[i]==a[j]+4)&&(b[i]==b[j]-4)) {k[7]++;continue;}else if(k[7]==5){printf("Player 2 wins!!!\n");}elsecontinue;}if(k[7]==5)break;k[7]=1;}}}/****************和棋*******************/void heqi(){printf("************************************\n"); printf(" Tie!!!\n");printf("************************************\n"); }/****************游戏结束*******************/void over(){printf("************************************\n"); printf(" game over!!!\n");printf("************************************\n"); }/****************游戏开始*******************/void welcome(){printf("************************************\n"); printf(" Welcome!!!\n");printf("************************************\n"); }。
五子棋源代码-Java_Applet小程序
五子棋源代码-Java_Applet小程序importjava.applet.Applet; importjava.awt.*;importjava.util.Random;public class wzq extends Applet implements Runnable{public void stop(){LoopThread = null;}privateintCalcZi(inti, int j, byte byte0){CXY cxy = new CXY(); int k = 0;int l = 0;do{int i1 = 0;int j1 = 0;do{cxy.x = i;cxy.y = j;if(MoveAGrid(cxy, l + 4 * j1) &&QiPan[cxy.x][cxy.y] == byte0) do{ if(QiPan[cxy.x][cxy.y] == 0 || QiPan[cxy.x][cxy.y] != byte0) break;i1++;} while(MoveAGrid(cxy, l + 4 * j1));}while(++j1 < 2);if(i1 > k)k = i1;}while(++l < 4);return ++k;}privatebooleanCanDo(){return steps < ((GRIDSUM * GRIDSUM) / 100) * 80;}//电脑下棋privateintCPUDo(CXY cxy, byte byte0){intai[] = new int[2];int ai1[] = new int[2];int ai2[] = new int[2];boolean flag = false;EnterTimes++;ai2[0] = 0;for(inti = recLU.x; i<= recRD.x; i++){for(int k = recLU.y; k <= recRD.y; k++){int l = 0;if(QiPan[i][k] == 0){DoAStep(i, k, byte0);l = CalcCPU(i, k, byte0);}if(l > 0){int i1 = 0;byte byte1;if(byte0 == 1)byte1 = 2; elsebyte1 = 1; if(EnterTimes<= level && steps < ((GRIDSUM * GRIDSUM) / 100) * 80)i1 = CPUDo(cxy, byte1);l += i1;if(l + Math.abs(rd.nextInt()) % 5 > ai2[0] || !flag){ai[0] = i;ai1[0] = k;ai2[0] = l;flag = true;}QiPan[i][k] = 0;}}}if(EnterTimes<= 1){cxy.x = ai[0];cxy.y = ai1[0];int j = 0;do{try{Thread.sleep(300L);}catch(InterruptedException _ex) { } QiPan[cxy.x][cxy.y] = byte0;repaint();try{Thread.sleep(300L);}catch(InterruptedException _ex) { } QiPan[cxy.x][cxy.y] = 0; repaint(); }while(++j < 2);}EnterTimes--;return ai2[0];}public void ClearPan(){for(inti = 0; i< GRIDSUM; i++){for(int j = 0; j < GRIDSUM; j++) QiPan[i][j] = 0;}scHong = 0;scHei = 0;steps = 0;recLU.x = 8;recRD.x = 9;recLU.y = 8;recRD.y = 9;}privatebooleanMoveAGrid(CXY cxy, inti){boolean flag = false;i %= 8;int j = cxy.x + oAdd[i][0]; int k = cxy.y + oAdd[i][1]; if(j >= 0 && j < GRIDSUM && k >= 0 && k < GRIDSUM){cxy.x = j;cxy.y = k;flag = true;}return flag;}public void paint(Graphics g){super.paint(g);for(inti = 0; i< GRIDSUM + 1; i++){g.drawLine(0, i * GRIDWIDTH, GRIDSUM * GRIDWIDTH, i * GRIDWIDTH);g.drawLine(i * GRIDWIDTH, 0, i * GRIDWIDTH, GRIDSUM * GRIDWIDTH);}for(int j = 0; j < GRIDSUM; j++){for(int k = 0; k < GRIDSUM; k++) drawQi(g, j, k, QiPan[j][k]);}}private void CPUInit(){PosAdd[0][0] = 8;PosAdd[0][1] = -2;PosAdd[1][0] = -2;PosAdd[0][2] = 3;PosAdd[2][0] = 3;PosAdd[0][3] = 2;PosAdd[3][0] = 2; PosAdd[1][1] = -7; PosAdd[1][2] = -1; PosAdd[2][1] = -1; PosAdd[1][3] = -1; PosAdd[3][1] = -1;PosAdd[2][2] = 4; PosAdd[3][3] = 4; PosAdd[2][3] = 4; PosAdd[3][2] = 4;}public void mouseDOWNThis(Event event){if(playerdo)xiazi.put(event.x, event.y);}privateintDoAStep(inti, int j, byte byte0){if(QiPan[i][j] != 0 || byte0 == 0 || byte0 > 2){return 0;}else{QiPan[i][j] = byte0; return 1;}}private void FreshRec(inti, int j){if(i - recLU.x< 2){recLU.x = i - 2;if(recLU.x< 0)recLU.x = 0;}if(recRD.x - i< 2){recRD.x = i + 2;if(recRD.x>= GRIDSUM) recRD.x = GRIDSUM - 1; }if(j - recLU.y< 2){recLU.y = j - 2;if(recLU.y< 0)recLU.y = 0;}if(recRD.y - j < 2){recRD.y = j + 2;if(recRD.y>= GRIDSUM) recRD.y = GRIDSUM - 1;}}publicwzq(){GRIDWIDTH = 18;GRIDSUM = 18; QiPan = new byte[GRIDSUM][GRIDSUM];oAdd = new int[8][2]; playing = false;playerdo = true;xy = new CXY();xiazi = new CXiaZi(); rd = new Random(); recLU = new CXY(); recRD = new CXY(); PosAdd = new int[4][4];}public void update(Graphics g){paint(g);}//画棋public void drawQi(Graphics g, inti, int j, int k){switch(k){case 0: // '\0'g.clearRect(i * GRIDWIDTH + 1, j * GRIDWIDTH + 1, GRIDWIDTH -2,GRIDWIDTH - 2);return;case 1: // '\001'g.setColor(Color.red);g.fillArc(i * GRIDWIDTH + 2, j * GRIDWIDTH + 2, GRIDWIDTH -4,GRIDWIDTH - 4, 0, 360);return;case 2: // '\002'g.setColor(Color.black);break;}g.fillArc(i * GRIDWIDTH + 2, j * GRIDWIDTH + 2, GRIDWIDTH -4,GRIDWIDTH - 4, 0, 360);}public void start(){if(LoopThread == null)LoopThread = new Thread(this, "wbqloop"); LoopThread.setPriority(1);LoopThread.start();}public void run(){for(; Thread.currentThread() == LoopThread; xiazi.get(xy)) {ClearPan();repaint();playing = true;//谁先下随机who = (byte)(Math.abs(rd.nextInt()) % 2 + 1); for(passes = 0; playing && passes < 2;){if(who == 1){lblStatus.setText("\u7EA2\u65B9\u4E0B");lblStatus.setForeground(Color.red);}else{lblStatus.setText("\u9ED1\u65B9\u4E0B");lblStatus.setForeground(Color.black);}if(steps < ((GRIDSUM * GRIDSUM) / 100) * 80){passes = 0;if(who == 1) //人下棋{xiazi.get(xy);for(; DoAStep(xy.x, xy.y, who) == 0; xiazi.get(xy)); scHong = CalcZi(xy.x, xy.y, who); FreshRec(xy.x, xy.y); steps++;}else //机器下棋{if(scHong == 0 &&scHei == 0){ xy.x = 9;xy.y = 9;} else{ CPUDo(xy, who);} for(; DoAStep(xy.x, xy.y, who) == 0; CPUDo(xy, who)); scHei = CalcZi(xy.x, xy.y, who); FreshRec(xy.x, xy.y); steps++;}}else{passes = 2;}if(scHong>= 5 || scHei>= 5) playing = false; repaint();//交换下棋方who = (byte)((1 - (who - 1)) + 1); Thread.yield(); }if(scHong>= 5) //红方胜{Status = "\u7EA2\u65B9\u80DC!";lblStatus.setForeground(Color.red); LoseTimes++; }else if(scHei>= 5)//黑方胜{Status = "\u9ED1\u65B9\u80DC!";lblStatus.setForeground(Color.black);if(LoseTimes> 0)LoseTimes--;}else //平局{Status = "\u4E0D\u5206\u80DC\u8D1F!";}lblStatus.setText(Status); repaint();}}//入口,开始下棋,初始化public void init() {super.init(); LoopThread = null; oAdd[0][0] = 0; oAdd[0][1] = -1; oAdd[1][0] = 1; oAdd[1][1] = -1; oAdd[2][0] = 1; oAdd[2][1] = 0; oAdd[3][0] = 1; oAdd[3][1] = 1; oAdd[4][0] = 0; oAdd[4][1] = 1; oAdd[5][0] = -1; oAdd[5][1] = 1; oAdd[6][0] = -1; oAdd[6][1] = 0; oAdd[7][0] = -1; oAdd[7][1] = -1; CPUInit();setLayout(null);resize(325, 352);lblStatus = new Label("Welcome"); lblStatus.setFont(new Font("Dialog", 1, 14));add(lblStatus);lblStatus.reshape(14, 332, 175, 15);lblLevel = new Label("JAVA\u4E94\u5B50\u68CB");lblLevel.setFont(new Font("Dialog", 1, 14));add(lblLevel);lblLevel.reshape(196, 332, 119, 15);}publicbooleanhandleEvent(Event event){if(event.id != 501 || event.target != this){returnsuper.handleEvent(event);}else{mouseDOWNThis(event);return true;}}privateintCalcCPU(inti, int j, byte byte0){CXY cxy = new CXY();String s = "";String s2 = "";String s4 = ""; byte byte1 = 0;CalcTimes++;if(byte0 == 1)byte1 = 2; elseif(byte0 == 2)byte1 = 1; int k = 0;int l = 0;do{int i1 = 0;String s1 = "";String s3 = "";String s5 = ""; int j1 = 0;do{int k1 = 0;cxy.x = i;for(cxy.y = j; MoveAGrid(cxy, l + 4 * j1) && k1 < 6 &&QiPan[cxy.x][cxy.y] != byte1; k1++) if(QiPan[cxy.x][cxy.y] == byte0) {if(j1 == 0)s3 += "1"; elses5 = "1" + s5; i1++;}elseif(j1 == 0)s3 += "0"; elses5 = "0" + s5;if(j1 == 0)s3 += "2";elses5 = "2" + s5;}while(++j1 < 2);i1++;s1 = s5 + "1" + s3;if(s1.indexOf("11111") != -1)i1 += 1000;elseif(s1.indexOf("011110") != -1)i1 += 500;elseif(s1.indexOf("211110") != -1 || s1.indexOf("011112") != -1 || s1.indexOf("01110") != -1 || s1.indexOf("01110") != -1 ||s1.indexOf("011010")!= -1 || s1.indexOf("010110") != -1 || s1.indexOf("11101") != -1 || s1.indexOf("10111") != -1 ||s1.indexOf("11011") != -1)i1 += 100;elseif(s1.indexOf("21110") != -1 || s1.indexOf("01112") != -1 ||s1.indexOf("0110") != -1 || s1.indexOf("211010") != -1 ||s1.indexOf("210110")!= -1)i1 += 20;if(l == 1 || l == 3)i1 += (i1 * 20) / 100;k += i1;}while(++l < 4); if(CalcTimes<= 1)k += CalcCPU(i, j, byte1);elseif(k > 10)k -= 10; CalcTimes--;return k;}int GRIDWIDTH; //网格宽度int GRIDSUM; //网格总数byte QiPan[][]; //棋盘intoAdd[][];Thread LoopThread;intscHong; //红方intscHei; //黑方byte who; //byte winner; //赢方boolean playing; booleanplayerdo; CXY xy;CXiaZixiazi; //下子String Status; //状态Random rd; //随机数 int passes;int steps;intLoseTimes;CXY recLU;CXY recRD; intPosAdd[][]; int level; intEnterTimes; intCalcTimes;Label lblStatus;Label lblLevel; }classCXiaZi{public synchronized void get(CXY cxy) {ready = false;notify();while(!ready)try{wait();}catch(InterruptedException _ex) { }ready = false;notify();cxy.x = xy.x;cxy.y = xy.y;}public synchronized void put(inti, int j){if(i< GRIDWIDTH * GRIDSUM && j < GRIDWIDTH * GRIDSUM) {xy.x = i / GRIDWIDTH; xy.y = j / GRIDWIDTH; ready = true; notify();}}publicCXiaZi(){GRIDWIDTH = 18;GRIDSUM = 18; xy = new CXY();ready = false;}private CXY xy;privateboolean ready; privateint GRIDWIDTH; privateint GRIDSUM; } class CXY{public CXY(){x = 0;y = 0;}publicint x; publicint y; }内部资料,请勿外传~。
c语言 五子棋游戏 代码 源代码 解释
C语言教程:简易五子棋程序收集于网络/* 纯用字符和数组编的五子棋,棋盘也是用字符画的。
编了1上午了,主要是算法跟按键比较烦,发现有bug-- 按键速度过快会产生延时显示,可能是算法不好。
操作:玩家1:a,s,w,d(方向)空格(落子)玩家2:上、下、左、右回车(落子)ESC:退出编译测试环境:TC3.0*/#include <stdio.h>#include <stdlib.h>#include <bios.h>#include <conio.h>#define CRRU 0xbf /*右上角点197*/#define CRLU 0xda /*左上角点218*/#define CRLD 0xc0 /*左下角点192*/#define CRRD 0xd9 /*右下角点217*/#define CRL 0xc3 /*左边195*/#define CRR 0xb4 /*右边190*/#define CRU 0xc2 /*上边194*/#define CRD 0xc1 /*下边193*/#define CR 0xc5 /*十字交叉点197*/#define size 19char a[size][size];int i,j; //跟踪光标在数组中对应的位置int x=10;int y=3; //光标所在位的坐标int side=1; //持子方1为玩家1,2为玩家2;int CB=1; int CW=2; // 棋子图形void inita() ;void inits();void pressco(int );void pressct(int );int judge(int);int main(){inita();inits();getch();while(1){int press=bioskey(0);if(press==283)break;if(side==1){pressco(press);if(side==2)if(judge(1)==1) {gotoxy(1,1);printf("the play1 win");break;}}if(side==2){pressct(press);if(side==1)if(judge(2)==1){gotoxy(1,1);printf("the play2 win");break;}}}getch();return 0;}void inita() //数组初始化;{a[0][0]=CRLU;a[0][size-1]=CRRU;a[size-1][0]=CRLD;a[size-1][size-1]=CRRD;for(int i=1;i<size-1;i++){a[0][i]=CRU;a[size-1][i]=CRD;a[i][0]=CRL;a[i][size-1]=CRR;for(int j=1;j<size-1;j++)a[i][j]=CR;}return ;}void inits() //界面初始化{for(int i=0;i<size;i++){gotoxy(x,y+i);for(int j=0;j<size;j++)putch(a[i][j]);}gotoxy(x,y);i=0;j=0;return ;}void pressco(int m){switch(m){case 7777: //Aif(i>0) {i--;x--;gotoxy(x,y);} break;case 8051: //Sif(j<size-1){j++;y++;gotoxy(x,y);} break;case 4471: //wif(j>0) {j--;y--;gotoxy(x,y);} break;case 8292: //Dif(i<size-1){i++;x++;gotoxy(x,y);} break;case 14624: //空格if(a[i][j]!=CB&&a[i][j]!=CW){a[i][j]=CB;putch(CB);gotoxy(x,y);side=2;}break;default: break;}return ;}void pressct(int m){switch(m){case 19200: //左if(i>0) {i--;x--;gotoxy(x,y);} break;case 20480: //下if(j<size-1){j++;y++;gotoxy(x,y);} break;case 18432: //上if(j>0) {j--;y--;gotoxy(x,y);} break;case 19712: //右if(i<size-1){i++;x++;gotoxy(x,y);} break;case 7181: //回车if(a[i][j]!=CB&&a[i][j]!=CW){a[i][j]=CW;putch(CW);gotoxy(x,y);side=1;}break;default: break;}return ;}int judge(int pa) //判断是否胜利,胜利则返回1,否则返回0;// 其中i,j为当前的落子位;{int m;int sum=1;for(m=1;m<=i&&m<=j;m++) {if(a[i-m][j-m]!=pa) break;sum++;}for(m=1;m<(size-i)&&m<(size-j);m++) {if(a[i+m][j+m]!=pa) break;sum++;}if(sum>=5) return 1;else sum=1;for(m=1;m<=i;m++) {if(a[i-m][j]!=pa) break;sum++;}for(m=1;m<(size-j);m++) {if(a[i+m][j]!=pa) break;sum++;}if(sum>=5) return 1;else sum=1;for(m=1;m<=j;m++) {if(a[i][j-m]!=pa) break;sum++;}for(m=1;m<(size-j);m++) {if(a[i][j+m]!=pa) break;sum++;}if(sum>=5) return 1;else sum=1;for(m=1;m<=i&&m<(size-j);m++) {if(a[i-m][j+m]!=pa) break;sum++;}for(m=1;m<(size-i)&&m<=j;m++) {if(a[i+m][j-m]!=pa) break;sum++;}if(sum>=5) return 1;else return 0;本文章来自 21视频教程网C语言教程:简易五子棋程序_C语言程序设计教程原文链接:/html/96307.shtml。
五子棋源代码(2)
设计题目:五子棋。
一、程序目的和功能实现图形用户界面的五子棋双人对战。
二、各个类的功能说明1.ChessPad面板类,用于绘制棋盘,实现用鼠标进行走棋的动作。
棋盘如下:关键代码:public void paint(Graphics g){for (int i=40;i<=400;i=i+20){g.drawLine(40,i,400,i);}for(int j=40;j<=400;j=j+20){g.drawLine(j,40,j,400);}g.fillOval(97,97,6,6);g.fillOval(337,97,6,6);g.fillOval(97,337,6,6);g.fillOval(337,337,6,6);g.fillOval(217,217,6,6);}定义鼠标在棋盘上的走棋,关键代码:public void mousePressed(MouseEvent e)//走棋2.ChessPoint_black画布类,绘制黑棋,实现黑棋的悔棋,关键代码:public void paint(Graphics g){g.setColor(Color.black);g.fillOval(0,0,18,18);}public void mousePressed(MouseEvent e){if(e.getModifiers()==InputEvent.BUTTON3_MASK){chesspad.remove(this);chesspad.棋子颜色=1;chesspad.i--;chesspad.text_3.setText("这是第"+chesspad.i+"步");chesspad.text_2.setText("");chesspad.text_1.setText("请黑棋下子");}}3.ChessPoint_white画布类,绘制白棋,实现白棋的悔棋,关键代码与ChessPoint_black类似。
五子棋源程序
源程序包括五个类:类autoplay:public class AutoPlay{//实现电脑可以自动下棋int x,y;//X统计玩家的连起来的棋子数//Y电脑玩家的连起来的棋子数public void autoPlay(int chesspad[][],int a,int b){int randomNumber=(int)(Math.random()*8)+1;//随即选择一个方向switch(randomNumber){//通过a,b的值执行不同的运算,最后返回不同的//值。
达到执行不同的操作case(1)://左上if(a-1>=0 && b-1>=0 && chesspad[a-1][b-1]==0){x=a-1;y=b-1;break;}else if(a-2>=0 && b-2>=0 && chesspad[a-2][b-2]==0){x=a-2;y=b-2;break;}else{}case(2)://向左if(a-1>=0 && chesspad[a-1][b]==0){x=a-1;y=b;break;}else if(a-2>=0 && chesspad[a-2][b]==0){x=a-2;y=b;break;}else{}case(3)://左下if(a-1>=0 && b+1<=18 && chesspad[a-1][b+1]==0){x=a-1;y=b+1;break;}else if(a-2>=0 && b+2<=18 && chesspad[a-2][b+2]==0){x=a-2;y=b+2;break;}else{}case(4)://向下if(b+1<=18 && chesspad[a][b+1]==0){x=a;y=b+1;break;}else if( b+2<=18 && chesspad[a][b+2]==0 ){x=a;y=b+2;break;}else{}case(5)://右下if(a+1<=18 && b+1<=18 && chesspad[a+1][b+1]==0 ){x=a+1;y=b+1;break;}else if(a+2<=18 && b+2<=18 && chesspad[a+2][b+2]==0){x=a+2;y=b+2;break;}else{}case(6)://向右if(a+1<=18 && chesspad[a+1][b]==0){x=a+1;y=b;break;}else if(a+2<=18 && chesspad[a+2][b]==0){x=a+2;y=b;break;}else{}case(7)://右上if( a+1<=18 && b-1>=0 && chesspad[a+1][b-1]==0){x=a+1;y=b-1;break;}else if( a+2<=18 && b-2>=0 && chesspad[a+2][b-2]==0){x=a+2;y=b-2;break;}else{}case(8)://向上if(b-1>=0 && chesspad[a][b-1]==0){x=a;y=b-1;break;}else if(b-2>=0 && chesspad[a][b-2]==0){x=a;y=b-2;break;}else{}}}}类evaluate:public class Evaluate{//电脑估算估值点的坐标int max_x,max_y,max;public void evaluate(int shape[][][]){int i=0,j=0;for(i=0;i<=18;i++)for(j=0;j<=18;j++){switch(shape[i][j][0]) {//电脑根据传进来的三维数组决定该怎么行棋case 5:shape[i][j][4]=200;break;case 4:switch(shape[i][j][1]){case 4:shape[i][j][4]=150+shape[i][j][2]+shape[i][j][3];break;case 3:shape[i][j][4]=100+shape[i][j][2]+shape[i][j][3];break;default:shape[i][j][4]=50+shape[i][j][2]+shape[i][j][3];}break;case 3:switch(shape[i][j][1]){case 3:shape[i][j][4]=75+shape[i][j][2]+shape[i][j][3];break;default:shape[i][j][4]=20+shape[i][j][2]+shape[i][j][3];}break;case 2:shape[i][j][4]=10+shape[i][j][1]+shape[i][j][2]+shape[i][j][3];break;case 1:shape[i][j][4]=shape[i][j][0]+shape[i][j][1]+shape[i][j][2]+shape[i][j][3];default : shape[i][j][4]=0;}}int x=0,y=0;max=0;for(x=0;x<=18;x++)for(y=0;y<=18;y++)if(max<shape[x][y][4]){ //周围有同色棋子max=shape[x][y][4];max_x=x;max_y=y;//获得该点位置}}}主类fivechess:public class FiveChess extends JFrame implements MouseListener {// 取得屏幕的宽度int width = Toolkit.getDefaultToolkit().getScreenSize().width;// 取得屏幕的高度int height = Toolkit.getDefaultToolkit().getScreenSize().height;// 背景图片BufferedImage bgImage = null;// 保存之前下过的全部棋子的坐标// 其中数据内容0:表示这个点并没有棋子,1:表示这个点是黑子,2:表示这个点是白子int[][] allChess = new int[19][19];// 标识当前应该黑棋还是白棋下下一步0:表示黑子,1:表示白子int color_Qizi=0;String message="玩家执黑子先下";//游戏开始标志0:未开始1:游戏中int intGame_Start=0;//判定棋子步数int step=0;//p表示人,c表示电脑Scan scanp=new Scan();//personScan scanc=new Scan();//computerAutoPlay autoPlay=new AutoPlay();Evaluate evaluatep=new Evaluate();Evaluate evaluatec=new Evaluate();Sort sort=new Sort();public static void main(String[] args) {new FiveChess();}public FiveChess() {Game_start_csh();//设置窗体标题this.setTitle("黄洪华的五子棋V1.0");// 设置窗体大小this.setSize(500, 500);// 设置窗体出现位置this.setLocation((width - 500) / 2, (height - 500) / 2);// 将窗体设置为大小不可改变this.setResizable(false);// 将窗体的关闭方式设置为默认关闭后程序结束this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 为窗体加入监听器this.addMouseListener(this);// 将窗体显示出来this.setVisible(true);// 刷新屏幕,防止开始游戏时出现无法显示的情况.this.repaint();String imagePath = "" ;try {imagePath = System.getProperty("user.dir")+"/bin/image/background.jpg" ;bgImage = ImageIO.read(new File(imagePath.replaceAll("\\\\", "/")));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}this.repaint();// 刷新屏幕}//棋盘界面public void paint(Graphics g) {// 双缓冲技术防止屏幕闪烁BufferedImage bi = new BufferedImage(500, 500,BufferedImage.TYPE_INT_RGB);Graphics g2 = bi.createGraphics();g2.setColor(Color.BLACK);// 绘制背景g2.drawImage(bgImage, 1, 20, this);//输出步数情况g2.setFont(new Font("宋体", Font.BOLD, 18));g2.drawString("这是第" + step+"步!", 60, 470);//输出先后信息g2.setFont(new Font("黑体", Font.BOLD, 20));g2.drawString(message, 130, 60);// 绘制棋盘for (int i = 0; i < 19; i++) {g2.drawLine(10, 70 + 20 * i, 370, 70 + 20 * i);g2.drawLine(10 + 20 * i, 70, 10 + 20 * i, 430);}// 标注点位g2.fillOval(67, 127, 6, 6);g2.fillOval(307, 127, 6, 6);g2.fillOval(307, 367, 6, 6);g2.fillOval(67, 367, 6, 6);g2.fillOval(307, 247, 6, 6);g2.fillOval(187, 127, 6, 6);g2.fillOval(67, 247, 6, 6);g2.fillOval(187, 367, 6, 6);g2.fillOval(187, 247, 6, 6);// 绘制全部棋子(每一个位置的)for (int i = 0; i < 19; i++) {for (int j = 0; j < 19; j++) {if (allChess[i][j] == 1) {// 绘制黑子int tempX = i * 20 + 10;int tempY = j * 20 + 70;g2.fillOval(tempX - 9, tempY - 9, 18, 18);}if (allChess[i][j] == 2) {// 绘制白子int tempX = i * 20 + 10;int tempY = j * 20 + 70;g2.setColor(Color.WHITE);g2.fillOval(tempX - 9, tempY - 9, 18, 18);g2.setColor(Color.BLACK);g2.drawOval(tempX - 9, tempY - 9, 18, 18);}}}g.drawImage(bi, 0, 0, this);}//计算棋子的精确落位public void mouseClicked(MouseEvent e) {int x1,y1;x1=e.getX();y1=e.getY();//在棋盘外if (e.getX()<10 || e.getX()>370 || e.getY()<70 || e.getY()>430){return;}//取整,让棋子落在交叉线上if ((x1-10)%20>10){x1+=20;}if((y1-70)%20>10){y1+=20;}x1=(x1-10)/20*20;y1=(y1-70)/20*20;set_Qizi(x1,y1);}public void mouseEntered(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}public void mouseExited(MouseEvent e) {}//点击不同坐标的功能键public void mousePressed(MouseEvent e) {// 点击开始游戏按钮if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 70&& e.getY() <= 100) {int result = JOptionPane.showConfirmDialog(this, "是否确定开始?");if (result == 0) {Game_start_csh();intGame_Start=1;}}// 点击游戏重置按钮if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 120&& e.getY() <= 150) {Game_start_csh();}// 点击游戏说明按钮if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 170&& e.getY() <= 200) {JOptionPane.showMessageDialog(this,"这个一个单机五子棋游戏程序,黑白(人机)双方轮流下棋,当某一方连到五子时,游戏结束!");}// 点击认输按钮if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 270&& e.getY() <= 300) {if(intGame_Start==1){int result = JOptionPane.showConfirmDialog(this, "是否确认认输");if (result == 0) {JOptionPane.showMessageDialog(this, "您已经认输,游戏结束!");Game_start_csh();}}else{JOptionPane.showMessageDialog(this,"游戏还未开始或已经结束,不能认输,请重新开始下一局!");}}// 点击关于按钮if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 320&& e.getY() <= 350) {JOptionPane.showMessageDialog(this,}// 点击退出按钮if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 370&& e.getY() <= 400) {int result = JOptionPane.showConfirmDialog(this, "是否确认退出游戏?");if(result == 0)System.exit(0);}this.repaint();}public void Game_start_csh() //游戏开始初始化{intGame_Start=0;step=0;color_Qizi=0;for (int i=0;i<=18 ;i++ ){for (int j=0;j<=18 ;j++ ){allChess[i][j]=0;}}for(int i=0;i<=18;i++)for(int j=0;j<=18;j++)for(int h=0;h<5;h++){scanp.shape[i][j][h]=0;scanc.shape[i][j][h]=0;}}public String Get_qizi_color(int x)//得到棋子的颜色{if (x==0){return "五子连珠白棋";}else{return "五子连珠黑棋";}}public void set_Qizi(int x,int y) //落子{if (intGame_Start==0) //判断游戏未开始{return;}if (allChess[x/20][y/20]!=0){return;}if(intGame_Start==1&&allChess[x/20][y/20]==0) step++;Graphics g=getGraphics();if (color_Qizi==0)//判断黑子还是白子:是黑子{allChess[x/20][y/20]=color_Qizi+1;color_Qizi=1;Judge(x/20,y/20);}if(color_Qizi==1 && step>2){scanp.scan(allChess,1);scanc.scan(allChess,2);sort.sort(scanp.shape);//不要会出现乱走-->实现人与电脑有次序的下棋sort.sort(scanc.shape);evaluatep.evaluate(scanp.shape);evaluatec.evaluate(scanc.shape);if(evaluatec.max!=0||evaluatep.max!=0){if(evaluatep.max>evaluatec.max && scanp.shape[evaluatep.max_x][evaluatep.max_y][4]>3){g.fillOval(((evaluatep.max_x)*20+10)-9,((evaluatep.max_y)*20+70)-9, 18, 18);//防守allChess[evaluatep.max_x][evaluatep.max_y]=color_Qizi+1;color_Qizi=0;Judge(evaluatep.max_x,evaluatep.max_y);for(int i=0;i<=18;i++)for(int j=0;j<=18;j++)for(int h=0;h<5;h++){scanp.shape[i][j][h]=0;scanc.shape[i][j][h]=0;}}else{g.fillOval(((evaluatec.max_x)*20+10)-9,((evaluatec.max_y)*20+70)-9,18,18);//进攻allChess[evaluatec.max_x][evaluatec.max_y]=color_Qizi+1;color_Qizi=0;Judge(evaluatec.max_x,evaluatec.max_y);for(int i=0;i<=18;i++)//0---(-1)for(int j=0;j<=18;j++)for(int h=0;h<5;h++){scanp.shape[i][j][h]=0;scanc.shape[i][j][h]=0;}}}else{autoPlay.autoPlay(allChess,x/20,y/20); //随即选择一个方向下棋g.fillOval(((autoPlay.x)*20+10)-9,((autoPlay.y)*20+70)-9,18,18);allChess[autoPlay.x][autoPlay.y]=color_Qizi+1;color_Qizi=0;Judge(autoPlay.x,autoPlay.y);}}if(color_Qizi==1 && step<=2){if(allChess[9][9]==0){g.fillOval(190-9, 250-9, 18, 18);allChess[9][9]=color_Qizi+1;color_Qizi=0;}else if(allChess[8][8]==0){g.fillOval(170-9, 230-9, 18, 18);allChess[8][8]=color_Qizi+1;color_Qizi=0;}else if(allChess[8][9]==0){g.fillOval(170-9, 250-9, 18, 18);allChess[8][9]=color_Qizi+1;color_Qizi=0;}else if(allChess[9][8]==0){g.fillOval(190-9, 230-9, 18, 18);allChess[9][8]=color_Qizi+1;color_Qizi=0;}}}private void Judge(int a, int b) {//判断总的输赢if (Game_win_1(a,b)) //判断输赢{JOptionPane.showMessageDialog(this, "游戏结束,"+ Get_qizi_color(color_Qizi) + "获胜!");intGame_Start=0;}if (Game_win_2(a,b)) //判断输赢{JOptionPane.showMessageDialog(this, "游戏结束,"+ Get_qizi_color(color_Qizi) + "获胜!");intGame_Start=0;}if (Game_win_3(a,b)) //判断输赢{JOptionPane.showMessageDialog(this, "游戏结束,"+ Get_qizi_color(color_Qizi) + "获胜!");intGame_Start=0;}if (Game_win_4(a,b)) //判断输赢{JOptionPane.showMessageDialog(this, "游戏结束,"+ Get_qizi_color(color_Qizi) + "获胜!");intGame_Start=0;}this.repaint();}public boolean Game_win_1(int x,int y) //判断输赢横{int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i++ ){if (x1>18){break;}if (x+i<=18 && allChess[x1+i][y1]==allChess[x][y]){t+=1;}else{break;}}for (int i=1;i<5 ;i++ ){if (x1<0){break;}if(x-i>=0 && allChess[x1-i][y1]==allChess[x][y]){t+=1;}else{break;}}if (t>4){return true;}else{}}public boolean Game_win_2(int x,int y) //判断输赢竖{int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i++ ){if (x1>18){break;}if (y+i<=18 && allChess[x1][y1+i]==allChess[x][y] ){t+=1;}else{break;}}for (int i=1;i<5 ;i++ ){if (x1<0){break;}if(y-i>=0 && allChess[x1][y1-i]==allChess[x][y]){t+=1;}else{break;}}if (t>4){return true;}else{}}public boolean Game_win_3(int x,int y) //判断输赢左斜{int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i++ ){if (x1>18){break;}if (x+i<=18 && y-i>0&& allChess[x1+i][y1-i]==allChess[x][y]){t+=1;}else{break;}}for (int i=1;i<5 ;i++ ){if (x1<=0){break;}if(x-i>0 && y+i <=18 && allChess[x1-i][y1+i]==allChess[x][y]){t+=1;}else{break;}}if (t>4){return true;}else{}}public boolean Game_win_4(int x,int y) //判断输赢右斜{int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i++ ){if (x1>18){break;}if (x+i<=18 && y+i<=18 && allChess[x1+i][y1+i]==allChess[x][y]){t+=1;}else{break;}}for (int i=1;i<5 ;i++ ){if (x1<=0){break;}if(x-i>0 && y-i>0 && allChess[x1-i][y1-i]==allChess[x][y]){t+=1;}else{break;}}if (t>4){return true;}else{}}}类scan:public class Scan{int shape[][][]=new int[19][19][5];void scan(int chesspad[][],int colour){//查看八方向上相邻同色棋子个数int i,j;for(i=0;i<=18;i++){for(j=0;j<=18;j++){if(chesspad[i][j]==0){int m=i,n=j;while(n-1>=0 && chesspad[m][--n]==colour){//向上shape[i][j][0]++;}n=j;while(n+1<=18 && chesspad[m][++n]==colour){//向下shape[i][j][0]++;}n=j;while(m-1>=0 && chesspad[--m][n]==colour){//向左shape[i][j][1]++;}m=i;while(m+1<=18 && chesspad[++m][n]==colour){//向右shape[i][j][1]++;}m=i;while(m-1>=0 &&n+1<=18 && chesspad[--m][++n]==colour){//左下shape[i][j][2]++;}m=i;n=j;while(m+1<=18 && n-1>=0 && chesspad[++m][--n]==colour){//右上shape[i][j][2]++;}m=i;n=j;while(m-1>=0 && n-1>=0 && chesspad[--m][--n]==colour){//左上shape[i][j][3]++;}m=i;n=j;while(m+1<=18 && n+1<=18 && chesspad[++m][++n]==colour){//右下shape[i][j][3]++;}}}}}}类sort:public class Sort{ //实现人与电脑有次序的下棋public void sort(int shape[][][]){int temp;for(int i=0;i<=18;i++)for(int j=0;j<=18;j++){for(int h=1;h<=4;h++){for(int w=3;w>=h;w--){if(shape[i][j][w-1]<shape[i][j][w]){//如果前者小于后者,则交换temp=shape[i][j][w-1];shape[i][j][w-1]=shape[i][j][w];shape[i][j][w]=temp;}}}}}}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Page No.1WordsFromSlideC语言综合编程训练WordsFromNotePage1Page No.2WordsFromSlideC程序组成开发方法: 自上向下,逐步细化,模块化设计,结构化编码WordsFromNotePage2Page No.3WordsFromSlideWordsFromNotePage3Page No.4WordsFromSlide一般来说,开发一个软件要经过以下步骤: 确定软件的功能定义核心数据结构对整个软件进行功能模块划分编写程序实现各功能模块对源程序进行编译和调试,形成软件产品查看源程序WordsFromNotePage4Page No.5WordsFromSlide功能分析五子棋棋盘两位玩家交替行棋五子相连判定赢棋查看源程序WordsFromNotePage5Page No.6WordsFromSlide定义核心数据结构定义char gChessBoard[19][19];表示棋盘棋盘上每个交叉点有三种状态当前光标位置表示查看源程序struct point{ int x;int y;};WordsFromNotePage6Page No.7WordsFromSlide程序的模块划分查看源程序WordsFromNotePage7Page No.8WordsFromSlide程序的编制细节查看源程序定义核心数据结构初始化接收按键移动光标落子与判定胜负main()函数程序中用到的库函数介绍WordsFromNotePage8Page No.9 WordsFromSlidebioskey程序的编制细节程序中用到的库函数textmodeclrscrputchcputsgotoxytextcolordelaysound 与nosound WordsFromNotePage9Page No.10 WordsFromSlide用户手册查看源程序WordsFromNotePage10Page No.11WordsFromSlide设计任务系统以菜单方式工作(文本菜单或图形菜单)输入数据模块,数据用文件保存输出数据模块,数据用文件保存基本算法运用模块(排序、查找、插入、比较算法中至少包含一种)系统进入画面(静态或动画)系统其它功能实现(任选)WordsFromNotePage11Page No.12WordsFromSlide参考设计题目一:职工信息管理系统设计职工信息包括职工号、姓名、性别、出生年月、学历、职务、工资、住址、电话等(职工号不重复)。
试设计一职工信息管理系统,使之能提供以下功能:系统以菜单方式工作职工信息录入功能(职工信息用文件保存)--输入职工信息浏览功能--输出查询或排序功能:(至少一种查询方式) --算法按工资查询按学历查询等系统进入画面(静态或动画)职工信息删除、修改功能(任选项)WordsFromNotePage12Page No.13WordsFromSlide参考设计题目二:飞机订票系统设计假定民航机场共有n个航班,每个航班有一航班号、确定的航线(起始站、终点站)、确定的飞行时间(星期几)和一定的成员订额。
试设计一民航订票系统,使之能提供下列服务:系统以菜单方式工作航班信息录入功能(航班信息用文件保存)--输入航班信息浏览功能--输出查询航线:(至少一种查询方式)--算法按航班号查询按终点站查询系统进入画面(静态或动画)承办订票和退票业务(可选项)WordsFromNotePage13Page No.14WordsFromSlide参考设计题目三:学生选修课程系统设计假定有n门课程,每门课程有课程编号,课程名称,课程性质,总学时,授课学时,实验或上机学时,学分,开课学期等信息,学生可按要求(如总学分不得少于60)自由选课。
试设计一选修课程系统,使之能提供以下功能:系统以菜单方式工作课程信息录入功能(课程信息用文件保存)--输入课程信息浏览功能--输出查询功能:(至少一种查询方式)--算法按学分查询按课程性质查询系统进入画面(静态或动画)学生选修课程(可选项)WordsFromNotePage14Page No.15WordsFromSlide参考设计题目四:图书信息管理系统设计图书信息包括:登录号、书名、作者名、分类号、出版单位、出版时间、价格等。
试设计一图书信息管理系统,使之能提供以下功能:系统以菜单方式工作图书信息录入功能(图书信息用文件保存)--输入图书信息浏览功能--输出查询或排序功能:(至少一种查询方式)--算法按书名查询按作者名查询系统进入画面(静态或动画)图书信息的删除与修改(可选项)WordsFromNotePage15Page No.16WordsFromSlide设计要求与设计报告设计要求可自己选定题目,但至少包含五个功能模块模块化程序设计锯齿型书写格式必须上机调试通过实习报告语言简练,条理清楚,图表规范程序设计组成框图、流程图模块功能说明(如函数功能、入口及出口参数说明,函数调用关系描述等)调试与测试:调试方法,测试结果的分析与讨论,测试过程中遇到的主要问题及采取的解决措施源程序清单和执行结果(软盘):清单中应有足够的注释WordsFromNotePage16Page No.17WordsFromSlide图形设计1 显示系统显示器:CRT(阴极射线管)与控制电路组成适配器(图形卡):CPU与显示器的接口,由寄存器组、存储器和控制电路组成参考书:C高级实用程序设计王士元清华大学出版社将要显示的字符和图形以数字形式存储在卡上的视频RAM中,再转换成视频模拟信号传送给相应的显示器WordsFromNotePage17Page No.18WordsFromSlide显示模式文本模式:显示缓冲区中存放显示字符的代码(ASCII码或汉字机内码),及字符的属性,显示屏幕按字符分成若干行、列,如80列 25行图形模式:显示缓冲区中存放显示屏幕上每点(象素)的颜色或灰度值,显示屏幕按象素分成若干行、列,如640 480一般开机后的模式都是80 ×25的文本模式WordsFromNotePage18Page No.19WordsFromSlide2 Turbo C绘图Turbo C支持的适配器和图形模式图形库文件与图形头文件graphics.lib与graphics.h图形显示的坐标与象素WordsFromNotePage19Page No.20WordsFromSlide图形系统的初始化图形系统初始化函数void initgraph(int *driver,int *mode,char *pathtodriver)其中;driver——显示适配器驱动程序的枚举变量mode——图形模式(模式号或模式标识符)pathtodriver——适配器驱动程序的寻找路径,“”表示在当前目录下(*.BGI)enum graphics_driver{DETECT,CGA,MCGA,EGA,EGA64,………,VGA,PC3270};DETECT——自动检测所用显示适配器的类型,将相应的驱动程序装入,并将其最高显示模式作为当前显示模式使用图形方式时先进行图形系统初始化,检测或设置指定的图形模式WordsFromNotePage20Page No.21WordsFromSlide#include <graphics.h>main(){ int driver=DETECT,mode;initgraph(&driver,&mode, “”);……….closegraph();}initgraph(&driver,&mode, “d:\\tc\\bgi”);#include <graphics.h>main(){ int driver=VGA;int mode=VGAHI;initgraph(&driver,&mode, “”);……….closegraph();}将指定模式的驱动程序(*.GBI)从当前路径下调入内存关闭图形系统回到文本模式WordsFromNotePage21Page No.22WordsFromSlide图形程序的独立运行问题为使图形方式的C源程序能够脱离TC环境下运行,应对EGAVGA显示器的驱动程序.BGI转换成.OBJ文件:C:\TC\BGIOBJ EGAVGA然后,将EGAVGA.OBJ与GRAPHICS.LIB进行连接:C:\TC\TLIB LIB\GRAPHICS.LIB+EGAVGA.OBJ程序中,initgraph()调用前要用registerbgidriver()进行登记:registerbgidriver(EGAVGA_driver);WordsFromNotePage22Page No.23WordsFromSlide清屏和恢复显示方式函数void cleardevice(void);void closegraph(void);基本图形函数画点:putpixel() getpixel()坐标移动:moveto() moverel() getx() gety()画线:line() lineto() linerel()画矩形和条形图:rectangle() bar()椭圆、圆、圆弧和扇形:ellipse() circle() arc() pieslice() 颜色控制函数设调色板:setpalette()设背景色:setbkcolor()设绘图色:setcolor()关闭图形系统回到文本模式WordsFromNotePage23Page No.24WordsFromSlideWordsFromNotePage24Page No.25WordsFromSlide设定线型函数: setlinestyle()填充函数设定填充模式:setfillstyle()常用函数:bar3d() sector() fillellipse() fillpoly()屏幕操作函数屏幕图象存储与显示:getimage() putimage() imagesize()视口函数setviewport()clearviewport()图形方式下的文本输出文本输出:outtext() outtextxy() 定义文本字型:settextstyle()WordsFromNotePage25Page No.26WordsFromSlide文本的屏幕输出#include <conio.h>屏幕坐标WordsFromNotePage26Page No.27WordsFromSlide文本方式控制函数void textmode(int newmode); Turbo C支持5种文本显示方式WordsFromNotePage27Page No.28WordsFromSlide文本方式下颜色控制函数文本颜色textcolor()文本背景textbackground()文本属性textattr()字符显示亮度控制函数highvideo()lowvideo()normvideo() WordsFromNotePage28Page No.29WordsFromSlide窗口设置与文本输出函数window()cprintf()cputs()putch()清屏与光标操作函数clrscr()clreol()delline()gotoxy() wherex() wherey()屏幕文本移动与存取函数movetext()gettext()puttext()WordsFromNotePage29Page No.30WordsFromSlide菜单及其设计要点菜单作用:由若干任务项组成的画面(界面),选择一项按键确定后,即执行相应的功能菜单显示方式:文本方式菜单与图形方式菜单菜单种类:固定式菜单、弹出式菜单,下拉式菜单optmenu.cpopdown.ctext_men.cmenu_s.cmenu_gra.cplane_e.cWordsFromNotePage30Page No.31WordsFromSlide菜单设计要点:菜单窗口图象的存储与显示新的窗口出现时,会覆盖原窗口的区域可事先保存要覆盖的区域,新窗口消失后,再恢复原区域的显示图形方式:getimage() putimage() imagesize() malloc()文本方式:gettext() puttext()菜单窗口和菜单项的生成生成背景图象,在指定位置显示菜单项文字光条的生成光条的产生及光条在光标键的控制下而移动(保存与再现)键识别当按下菜单上的选项(如:功能键或光标移动键)时,根据键值转入相应的功能处理菜单的连接功能执行小程序可以嵌入在菜单中,大程序做成功能模块,根据键码直接调用WordsFromNotePage31。