Java棋類游戲理論之單機版五子棋。本站提示廣大學習愛好者:(Java棋類游戲理論之單機版五子棋)文章只能為提供參考,不一定能成為您想要的結果。以下是Java棋類游戲理論之單機版五子棋正文
本文實例講述了java完成的五子棋游戲代碼,分享給年夜家供年夜家參考,詳細代碼以下
1、理論目的
1.控制JavaGUI界面設計
2.控制鼠標事宜的監聽(MouseListener,MouseMotionListener)
2、理論內容
設計一個簡略的五子棋法式,可以或許完成五子棋下棋進程。以下圖所示
1.五子棋棋盤類
package cn.edu.ouc.fiveChess; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RadialGradientPaint; import java.awt.RenderingHints; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.geom.Ellipse2D; import javax.swing.*; /** * 五子棋--棋盤類 */ public class ChessBoard extends JPanel implements MouseListener { public static final int MARGIN=30;//邊距 public static final int GRID_SPAN=35;//網格間距 public static final int ROWS=15;//棋盤行數 public static final int COLS=15;//棋盤列數 Point[] chessList=new Point[(ROWS+1)*(COLS+1)];//初始每一個數組元素為null boolean isBlack=true;//默許開端是黑棋先 boolean gameOver=false;//游戲能否停止 int chessCount;//以後棋盤棋子的個數 int xIndex,yIndex;//以後剛下棋子的索引 Image img; Image shadows; Color colortemp; public ChessBoard(){ // setBackground(Color.blue);//設置配景色為橘黃色 img=Toolkit.getDefaultToolkit().getImage("board.jpg"); shadows=Toolkit.getDefaultToolkit().getImage("shadows.jpg"); addMouseListener(this); addMouseMotionListener(new MouseMotionListener(){ public void mouseDragged(MouseEvent e){ } public void mouseMoved(MouseEvent e){ int x1=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN; //將鼠標點擊的坐標地位轉成網格索引 int y1=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN; //游戲曾經停止不克不及下 //落在棋盤外不克不及下 //x,y地位曾經有棋子存在,不克不及下 if(x1<0||x1>ROWS||y1<0||y1>COLS||gameOver||findChess(x1,y1)) setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); //設置成默許狀況 else setCursor(new Cursor(Cursor.HAND_CURSOR)); } }); } //繪制 public void paintComponent(Graphics g){ super.paintComponent(g);//畫棋盤 int imgWidth= img.getWidth(this); int imgHeight=img.getHeight(this);//取得圖片的寬度與高度 int FWidth=getWidth(); int FHeight=getHeight();//取得窗口的寬度與高度 int x=(FWidth-imgWidth)/2; int y=(FHeight-imgHeight)/2; g.drawImage(img, x, y, null); for(int i=0;i<=ROWS;i++){//畫橫線 g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN); } for(int i=0;i<=COLS;i++){//畫豎線 g.drawLine(MARGIN+i*GRID_SPAN, MARGIN, MARGIN+i*GRID_SPAN, MARGIN+ROWS*GRID_SPAN); } //畫棋子 for(int i=0;i<chessCount;i++){ //網格穿插點x,y坐標 int xPos=chessList[i].getX()*GRID_SPAN+MARGIN; int yPos=chessList[i].getY()*GRID_SPAN+MARGIN; g.setColor(chessList[i].getColor());//設置色彩 // g.fillOval(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, //Point.DIAMETER, Point.DIAMETER); //g.drawImage(shadows, xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER, null); colortemp=chessList[i].getColor(); if(colortemp==Color.black){ RadialGradientPaint paint = new RadialGradientPaint(xPos-Point.DIAMETER/2+25, yPos-Point.DIAMETER/2+10, 20, new float[]{0f, 1f} , new Color[]{Color.WHITE, Color.BLACK}); ((Graphics2D) g).setPaint(paint); ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT); } else if(colortemp==Color.white){ RadialGradientPaint paint = new RadialGradientPaint(xPos-Point.DIAMETER/2+25, yPos-Point.DIAMETER/2+10, 70, new float[]{0f, 1f} , new Color[]{Color.WHITE, Color.BLACK}); ((Graphics2D) g).setPaint(paint); ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT); } Ellipse2D e = new Ellipse2D.Float(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, 34, 35); ((Graphics2D) g).fill(e); //標志最初一個棋子的紅矩形框 if(i==chessCount-1){//假如是最初一個棋子 g.setColor(Color.red); g.drawRect(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, 34, 35); } } } public void mousePressed(MouseEvent e){//鼠標在組件上按下時挪用 //游戲停止時,不再能下 if(gameOver) return; String colorName=isBlack?"黑棋":"白棋"; //將鼠標點擊的坐標地位轉換成網格索引 xIndex=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN; yIndex=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN; //落在棋盤外不克不及下 if(xIndex<0||xIndex>ROWS||yIndex<0||yIndex>COLS) return; //假如x,y地位曾經有棋子存在,不克不及下 if(findChess(xIndex,yIndex))return; //可以停止時的處置 Point ch=new Point(xIndex,yIndex,isBlack?Color.black:Color.white); chessList[chessCount++]=ch; repaint();//告訴體系從新繪制 //假如勝出則給出提醒信息,不克不及持續下棋 if(isWin()){ String msg=String.format("祝賀,%s贏了!", colorName); JOptionPane.showMessageDialog(this, msg); gameOver=true; } isBlack=!isBlack; } //籠罩mouseListener的辦法 public void mouseClicked(MouseEvent e){ //鼠標按鍵在組件上單擊時挪用 } public void mouseEntered(MouseEvent e){ //鼠標進入到組件上時挪用 } public void mouseExited(MouseEvent e){ //鼠標分開組件時挪用 } public void mouseReleased(MouseEvent e){ //鼠標按鈕在組件上釋放時挪用 } //在棋子數組中查找能否有索引為x,y的棋子存在 private boolean findChess(int x,int y){ for(Point c:chessList){ if(c!=null&&c.getX()==x&&c.getY()==y) return true; } return false; } private boolean isWin(){ int continueCount=1;//持續棋子的個數 //橫向向西尋覓 for(int x=xIndex-1;x>=0;x--){ Color c=isBlack?Color.black:Color.white; if(getChess(x,yIndex,c)!=null){ continueCount++; }else break; } //橫向向東尋覓 for(int x=xIndex+1;x<=COLS;x++){ Color c=isBlack?Color.black:Color.white; if(getChess(x,yIndex,c)!=null){ continueCount++; }else break; } if(continueCount>=5){ return true; }else continueCount=1; //持續另外一種搜刮縱向 //向上搜刮 for(int y=yIndex-1;y>=0;y--){ Color c=isBlack?Color.black:Color.white; if(getChess(xIndex,y,c)!=null){ continueCount++; }else break; } //縱向向下尋覓 for(int y=yIndex+1;y<=ROWS;y++){ Color c=isBlack?Color.black:Color.white; if(getChess(xIndex,y,c)!=null) continueCount++; else break; } if(continueCount>=5) return true; else continueCount=1; //持續另外一種情形的搜刮:斜向 //西南尋覓 for(int x=xIndex+1,y=yIndex-1;y>=0&&x<=COLS;x++,y--){ Color c=isBlack?Color.black:Color.white; if(getChess(x,y,c)!=null){ continueCount++; } else break; } //東北尋覓 for(int x=xIndex-1,y=yIndex+1;x>=0&&y<=ROWS;x--,y++){ Color c=isBlack?Color.black:Color.white; if(getChess(x,y,c)!=null){ continueCount++; } else break; } if(continueCount>=5) return true; else continueCount=1; //持續另外一種情形的搜刮:斜向 //東南尋覓 for(int x=xIndex-1,y=yIndex-1;x>=0&&y>=0;x--,y--){ Color c=isBlack?Color.black:Color.white; if(getChess(x,y,c)!=null) continueCount++; else break; } //西北尋覓 for(int x=xIndex+1,y=yIndex+1;x<=COLS&&y<=ROWS;x++,y++){ Color c=isBlack?Color.black:Color.white; if(getChess(x,y,c)!=null) continueCount++; else break; } if(continueCount>=5) return true; else continueCount=1; return false; } private Point getChess(int xIndex,int yIndex,Color color){ for(Point p:chessList){ if(p!=null&&p.getX()==xIndex&&p.getY()==yIndex &&p.getColor()==color) return p; } return null; } public void restartGame(){ //消除棋子 for(int i=0;i<chessList.length;i++){ chessList[i]=null; } //恢復游戲相干的變量值 isBlack=true; gameOver=false; //游戲能否停止 chessCount =0; //以後棋盤棋子個數 repaint(); } //悔棋 public void goback(){ if(chessCount==0) return ; chessList[chessCount-1]=null; chessCount--; if(chessCount>0){ xIndex=chessList[chessCount-1].getX(); yIndex=chessList[chessCount-1].getY(); } isBlack=!isBlack; repaint(); } //矩形Dimension public Dimension getPreferredSize(){ return new Dimension(MARGIN*2+GRID_SPAN*COLS,MARGIN*2 +GRID_SPAN*ROWS); } }
2.棋子類
package cn.edu.ouc.fiveChess; import java.awt.Color; /** * 棋子類 */ public class Point { private int x;//棋盤中的x索引 private int y;//棋盤中的y索引 private Color color;//色彩 public static final int DIAMETER=30;//直徑 public Point(int x,int y,Color color){ this.x=x; this.y=y; this.color=color; } public int getX(){//拿到棋盤中x的索引 return x; } public int getY(){ return y; } public Color getColor(){//取得棋子的色彩 return color; } }
3.五子棋主框架類
package cn.edu.ouc.fiveChess; import java.awt.event.*; import java.awt.*; import javax.swing.*; /* 五子棋主框架類,法式啟動類 */ public class StartChessJFrame extends JFrame { private ChessBoard chessBoard; private JPanel toolbar; private JButton startButton,backButton,exitButton; private JMenuBar menuBar; private JMenu sysMenu; private JMenuItem startMenuItem,exitMenuItem,backMenuItem; //從新開端,加入,和悔棋菜單項 public StartChessJFrame(){ setTitle("單機版五子棋");//設置題目 chessBoard=new ChessBoard(); Container contentPane=getContentPane(); contentPane.add(chessBoard); chessBoard.setOpaque(true); //創立和添加菜單 menuBar =new JMenuBar();//初始化菜單欄 sysMenu=new JMenu("體系");//初始化菜單 //初始化菜單項 startMenuItem=new JMenuItem("從新開端"); exitMenuItem =new JMenuItem("加入"); backMenuItem =new JMenuItem("悔棋"); //將三個菜單項添加到菜單上 sysMenu.add(startMenuItem); sysMenu.add(exitMenuItem); sysMenu.add(backMenuItem); //初始化按鈕事宜監聽器外部類 MyItemListener lis=new MyItemListener(); //將三個菜單注冊到事宜監聽器上 this.startMenuItem.addActionListener(lis); backMenuItem.addActionListener(lis); exitMenuItem.addActionListener(lis); menuBar.add(sysMenu);//將體系菜單添加到菜單欄上 setJMenuBar(menuBar);//將menuBar設置為菜單欄 toolbar=new JPanel();//對象面板實例化 //三個按鈕初始化 startButton=new JButton("從新開端"); exitButton=new JButton("加入"); backButton=new JButton("悔棋"); //將對象面板按鈕用FlowLayout結構 toolbar.setLayout(new FlowLayout(FlowLayout.LEFT)); //將三個按鈕添加到對象面板 toolbar.add(startButton); toolbar.add(exitButton); toolbar.add(backButton); //將三個按鈕注冊監聽事宜 startButton.addActionListener(lis); exitButton.addActionListener(lis); backButton.addActionListener(lis); //將對象面板結構到界面”南邊“也就是下方 add(toolbar,BorderLayout.SOUTH); add(chessBoard);//將面板對象添加到窗體上 //設置界面封閉事宜 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setSize(800,800); pack();//自順應年夜小 } private class MyItemListener implements ActionListener{ public void actionPerformed(ActionEvent e){ Object obj=e.getSource();//取得事宜源 if(obj==StartChessJFrame.this.startMenuItem||obj==startButton){ //從新開端 //JFiveFrame.this外部類援用內部類 System.out.println("從新開端"); chessBoard.restartGame(); } else if (obj==exitMenuItem||obj==exitButton) System.exit(0); else if (obj==backMenuItem||obj==backButton){ System.out.println("悔棋..."); chessBoard.goback(); } } } public static void main(String[] args){ StartChessJFrame f=new StartChessJFrame();//創立主框架 f.setVisible(true);//顯示主框架 } }
3、總結
1.菜單的設計與完成?
2.鼠標點擊棋盤後,若何繪制棋子?若何為剛下的棋子繪制一個白色框?
3.棋譜是若何一個數據構造?
以上就是本文的全體內容,願望對年夜家進修java法式設計有所贊助。