日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java五子棋以当前空位为中心 取9个点_java 五子棋有点问题,哪位帮忙破一下、、...

發布時間:2025/3/21 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java五子棋以当前空位为中心 取9个点_java 五子棋有点问题,哪位帮忙破一下、、... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

該樓層疑似違規已被系統折疊?隱藏此樓查看此樓

import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.util.*;

//棋子類class PieceChess{public static int getDIAMETER(){ return DIAMETER;} public static void setDIAMETER(int aRADIUS){ DIAMETER = aRADIUS;}private Color color;private int XIndex;private int YIndex;private static int DIAMETER = 30;public PieceChess(Color color,int x,int y){ this.color = color; this.XIndex = x; this.YIndex = y;}public Color getColor(){ return color;}public void serColor(Color color){ this.color = color;}public int getXIndex(){ return XIndex;}public void setXIndex(int XIndex){ this.XIndex = XIndex;}public int getYIndex(){ return YIndex;}public void setYIndex(int YIndex){ this.YIndex = YIndex;}@Override public String toString(){ return this.color+"_"+this.XIndex+","+this.YIndex; }

} class ChessBoard1 extends JPanel implements MouseMotionListener{ private static final int SIDE = 36;//寬 private static final int MARGIN = 30;//邊距 private static final int XROWS = 15;//行數 private static final int YROWS = 15;//列數 private Color Chesscolor = Color.BLACK; private Vector chess_list = new Vector(); private String str = ""; boolean isFinished=true; public ChessBoard1(){ //背景 setBackground(Color.ORANGE); this.addMouseMotionListener(this); this.addMouseListener(new MouseAdapter(){ @Override public void mousePressed(MouseEvent me){ System.out.println(Thread.currentThread()); int x = me.getX(); int y = me.getY(); int xIndex = -10; int yIndex = -10; //根據鼠標坐標取得棋子索引 if (x>SIDE/2&&y>SIDE/2&&x

}

//重新設置棋盤 void StartChess(){ Chesscolor = Color.BLACK; this.isFinished = false; str = "黑棋先行"; repaint(); } //悔棋 void goBack(){ if(isFinished){ JOptionpane.showMessageDialog(this,"棋局已經結束!"); return; } else { chess_list.remove(chess_list.size()-1);Chesscolor = Chesscolor == Color.BLACK ? Color.WHITE : Color.BLACK;}if (chess_list.size() !=0) str = chess_list.elementAt(chess_list.size()-1).getColor() == Color.BLACK ? "白棋請走":"黑棋請走";else str = "黑棋先行";this.repaint(); } //重新開始void reStart(){ Chesscolor = Color.BLACK;chess_list.removeAllElements();this.isFinished = false;str = "黑棋先行";this.repaint(); }

public void paintComponent(Graphics g){ super.paintComponent(g); //畫棋盤 for(int i = 0;i it = chess_list.iterator(); while(it.hasNext()){ PieceChess pc = it.next(); int Ox = (pc.getYIndex()-1)*SIDE+MARGIN; int Oy = (pc.getXIndex()-1)*SIDE+MARGIN; g.setColor(pc.getColor()); g.fillOval(Ox-PieceChess.getDIAMETER()/2,Oy-PieceChess.getDIAMETER()/2,PieceChess.getDIAMETER(),PieceChess.getDIAMETER());

} }

public Dimension getPreferredSize() {return new Dimension(MARGIN*2+(XROWS-1)*SIDE,MARGIN*2+(YROWS-1)*SIDE); }

public void mouseMoved(MouseEvent me){ //獲取鼠標坐標 int x = me.getX(); int y = me.getY(); int xIndex = ((y-MARGIN)+SIDE/2)/SIDE+1; int yIndex = ((x-MARGIN)+SIDE/2)/SIDE+1; //當鼠標在規定范圍內設置默認形狀或手型 if (xXROWS||yIndex>YROWS) { setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } else setCursor(new Cursor(Cursor.HAND_CURSOR)); } public void mouseDragged(MouseEvent me){

}

boolean isChess(int xIndex,int yIndex){

boolean findchess = false;Iterator it = chess_list.iterator();if (!it.hasNext())return false;while(it.hasNext()){PieceChess pc = it.next();if (pc.getXIndex() == xIndex&&pc.getYIndex() == yIndex){

findchess = true;break;}

}

return findchess;}

private boolean isOver(PieceChess pc){Color color = pc.getColor();int xIndex = pc.getIndex();int yIndex = pc.getIndex();int ChessCount = 1 ; //設置計數器//向東尋找for (int i = 1;i<=4 ;i++ ){if(!isSameChess(color,xIndex,yIndex+i))break;elseChessCount++;}//如果計數器達到5if (ChessCount ==5)return true;//向西尋找for (int i=1;i<=4 ;i++ ){if(!isSameChess(color,xIndex,yIndex-i))break;elseChessCount++;}if(ChessCount ==5) return true; ChessCount = 1; //向北尋找 for (int i=1;i<=4 ;i++ ) {if(!isSameChess(color,xIndex-i,yIndex))break;elseChessCount++;}if(ChessCount ==5) return true;//向南尋找for (int i=1;i<=4 ;i++ ){if(!isSameChess(color,xIndex+i,yIndex))break;elseChessCount++;}if(ChessCount ==5) return true; ChessCount = 1; //向東北方尋找for (int i=1;i<=4 ;i++ ) {if(!isSameChess(color,xIndex-i,yIndex+i))break;elseChessCount++; } //向西南方尋找 if(ChessCount ==5) return true; for (int i=1;i<=4 ;i++ ) {if(!isSameChess(color,xIndex+i,yIndex-i))break;elseChessCount++;}if(ChessCount ==5) return true; ChessCount = 1; //向西北方尋找 for (int i=1;i<=4 ;i++ ){if(!isSameChess(color,xIndex-i,yIndex-i))break;elseChessCount++;}//向東南方尋找 if(ChessCount ==5) return true; for (int i=1;i<=4 ;i++ ) {if(!isSameChess(color,xIndex+i,yIndex+i))break;elseChessCount++;}if(ChessCount ==5) return true; else return false;}

public boolean isSameChess(Color color,int xIndex,int yIndex){

boolean isSame = false; Iterator it = chess_list.iteratir(); if (!it.hasNext()) return false; while (it.hasNext()) { PieceChess pc = it.next(); if(pc.getXIndex() == xIndex&&pc.getYIndex() == yIndex&&pc.getColor() == color){ idSame = true; break; }}return isSame;}

public void mousePressed (MouseEvent me){ System.out.println(Thread.currentThread());int x = me.getX();int y = me.getY();int xIndex = -10;int yIndex = -10;if (isFinished){return;}//根據鼠標坐標取得棋子索引if(x>SIDE/2&&y>SIDE/2&&x

class Five_in_a_row1 extends JFrame{ private JMenuBar jmb; private JMenu jm_sys; private ChessBoard1 chessBoard; private JMenuItem jmi_start;private JMenuItem jmi_back;private JMenuItem jmi_restart;private JMenuItem jmi_over;private JMenuItem jmi_exit; //為菜單按鈕添加事件 private void jmi_start_action(){ chessBoard.StartChess(); } private void jmi_back_action(){ chessBoard.goBack(); } private void jmi_restart_action(){ chessBoard.reStart(); } private void jmi_over_action(){ chessBoard.Over(); } private void jmi_exit_action(){ System.exit(0); }

public Five_in_a_row1(String name){ super(name); jmb = new JMenuBar(); jmb.setLayout(new FlowLayout(FlowLayout.LEFT)); jm_sys = new JMenu("系統"); jmi_start = new JMenuItem("開局"); jmi_back = new JMenuItem("悔棋"); jmi_restart = new JMenuItem("重新開局"); jmi_over = new JMenuItem("結束回合"); jmi_exit = new JMenuItem("退出系統");

chessBoard = new ChessBoard1(); jmb.add(jm_sys); //添加菜單按鈕 jm_sys.add(jmi_start);jm_sys.add(jmi_back);jm_sys.add(jmi_restart);jm_sys.add(jmi_over);jm_sys.add(jmi_exit);jmb.add(jm_sys);

this.add(jmb,BorderLayout.NORTH); this.add(chessBoard); this.setLocation(410,75); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); //開始按鈕事件 jmi_start.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ jmi_start_action(); } }); //悔棋按鈕事件 jmi_.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ jmi_back_action(); } }); //重新開局事件 jmi_restart.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ jmi_restart_action(); } }); //結束按鈕事件 jmi_over.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ jmi_back_action(); } }); //退出系統事件 jmi_exit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ jmi_exit_action(); } });

}public static void main(String[] arge){ JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); try{ UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); }catch (Exception e){ e.printStackTrace(); }Five_in_a_row1 fiar = new Five_in_a_row1("制作——五子棋——蛋雞班");fiar.setVisible(true);

}

}

總結

以上是生活随笔為你收集整理的java五子棋以当前空位为中心 取9个点_java 五子棋有点问题,哪位帮忙破一下、、...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。