java带图形界面的五子棋
Main:
package BlackWhite; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Main { public static void main(String[] args) { new Game().startGame(); //layout(); // JFrame mainWindow=new JFrame("黑白棋"); // mainWindow.setLayout(new FlowLayout());//布局器 // mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出时关闭 // mainWindow.setSize(400,300);//大小 // mainWindow.setVisible(true);//显示 // // JButton button=new JButton("点我"); // // mainWindow.add(button);//这里按钮铺满了 } public static void layout() { JFrame mainWindow=new JFrame("黑白棋"); mainWindow.setLayout(new BorderLayout()); mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainWindow.setSize(400,300); mainWindow.setVisible(true); JButton southButton=new JButton("South"); JButton NorthButton=new JButton("North"); JButton eastButton=new JButton("east"); JButton westButton=new JButton("west"); JButton centerButton=new JButton("center"); // // mainWindow.add(southButton,BorderLayout.SOUTH); // mainWindow.add(NorthButton,BorderLayout.NORTH); // mainWindow.add(eastButton,BorderLayout.EAST); // mainWindow.add(westButton,BorderLayout.WEST); // mainWindow.add(centerButton,BorderLayout.CENTER); JPanel centerPanel=new JPanel(); centerPanel.setLayout(new FlowLayout());//布局 centerPanel.setBackground(Color.black);//背景颜色 centerPanel.add(centerButton); JPanel southPanel=new JPanel(); southPanel.setLayout(new FlowLayout()); southPanel.setBackground(Color.white); southPanel.add(southButton); mainWindow.add(centerPanel,BorderLayout.CENTER); mainWindow.add(southPanel,BorderLayout.SOUTH); } }
Game:判断五子相连还有问题
package BlackWhite; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Insets; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Game { private JFrame mainWindow;//JFrame是顶层容器 private JPanel centerPanel;//放置棋盘 private JPanel sourthPanel;//信息提示框 private JLabel playInforLabel_1; private JLabel playInforLabel_2; private JButton newGameButton; private Insets margins=new Insets(0,0,0,0); private static JButton[][] buttonArray=new MyButton[25][25]; private int type=1;//1黑2白 private boolean isWin=false; public Game() { mainWindow=new JFrame("五子棋"); mainWindow.setLayout(new BorderLayout()); mainWindow.setSize(600, 750); mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainWindow.setResizable(false); centerPanel=new JPanel(); centerPanel.setLayout(new GridLayout(25,25)); for(int y=0;y<buttonArray.length;y++) { for(int x=0;x<buttonArray[0].length;x++) { buttonArray[y][x]=new MyButton(y,x); buttonArray[y][x].setText(""); buttonArray[y][x].setMargin(margins);// myButtonAddEvent((MyButton) buttonArray[y][x]); centerPanel.add(buttonArray[y][x]); } } sourthPanel=new JPanel(); sourthPanel.setLayout(new FlowLayout()); playInforLabel_1=new JLabel("黑方下棋"); playInforLabel_2=new JLabel("等待一方获胜"); newGameButton=new JButton("再来一局");//构建按钮 //newGameButton.setEnabled(false); newGameButtonAddEvent( newGameButton); mainWindow.add(centerPanel, BorderLayout.CENTER); mainWindow.add(sourthPanel, BorderLayout.SOUTH); sourthPanel.add(playInforLabel_1); sourthPanel.add(playInforLabel_2); sourthPanel.add(newGameButton); } public void startGame() { mainWindow.setVisible(true);//显示 } public void myButtonAddEvent(MyButton button) { button.addMouseListener(new MouseListener() {//MouseListener()是接口 @Override public void mouseClicked(MouseEvent e) { MyButton button =(MyButton)e.getComponent(); String butttonText=button.getText(); if(!butttonText.equals("")) { return; } if(type==1) { button.setText("●"); int x=button.getxIndex(); int y=button.getyIndex(); buttonArray[y][x].setText("●"); System.out.println(buttonArray[y][x].getText()); if(whoWin(1,y,x)) { System.out.println("黑方获胜"); }; System.out.print(("坐标"+(button.getyIndex()+1))); System.out.println((button.getxIndex()+1)); type=2; playInforLabel_1.setText("白方下棋"); }else { button.setText("○"); int x=button.getxIndex(); int y=button.getyIndex(); buttonArray[y][x].setText("○"); System.out.println(buttonArray[y][x].getText()); if(whoWin(2,y,x)) { System.out.println("白方获胜"); }; System.out.print(("坐标"+(button.getyIndex()+1))); System.out.println((button.getxIndex()+1)); type=1; playInforLabel_1.setText("黑方下棋"); } } @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } }); } public void newGameButtonAddEvent(JButton button) { newGameButton.addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent e) { //棋盘初始化 for(int y=0;y<buttonArray.length;y++) { for(int x=0;x<buttonArray[0].length;x++) { //buttonArray[y][x]=new MyButton(y,x); buttonArray[y][x].setText(""); } } type=1;//回到游戏开始的黑方下棋 isWin=false; playInforLabel_1.setText("黑方下棋"); } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } }); } public static boolean whoWin(int type,int y,int x){ //判断 是否已经获胜 String qizi = null; switch(type){ case 1 : qizi = "●"; break ; case 2 : qizi = "○"; break ; } //判断纵向是否5子相连 int count = 1; for(int i = y + 1 ; i < buttonArray.length ; i++){ if(buttonArray[i][x].getText().equals(qizi)){ count ++; }else{ break ; } } for(int i = y - 1 ; i >= 0 ; i--){ if(buttonArray[i][x].getText().equals(qizi)){ count ++; }else{ break ; } } if(count >= 5){ System.out.println("|相连了"); return true; } //判断横向是否5子相连 count = 1; for(int i = x + 1 ; i < buttonArray[0].length ; i++){ //System.out.println(buttonArray[0].length); if(buttonArray[i][x].getText().equals(qizi)){ count ++; }else{ break ; } } for(int i = x - 1 ; i >= 0 ; i--){ if(buttonArray[i][x].getText().equals(qizi)){ System.out.println(buttonArray[i][x].getText()); count ++; }else{ break ; } } if(count >= 5){ System.out.println("---相连"); return true; } //判断/是否相连System.out.println("判断/是否相连"); count = 1; for(int i = y + 1 , j = x + 1; i < buttonArray.length && j < buttonArray[0].length ; i++ , j++){ if(buttonArray[i][x].getText().equals(qizi)){ System.out.println(buttonArray[i][x].getText()); count ++; }else{ break ; } } for(int i = y - 1 , j = x - 1; i >=0 && j >= 0 ; i-- , j--){ if(buttonArray[i][x].getText().equals(qizi)){ System.out.println(buttonArray[i][x].getText()); count ++; }else{ break ; } } if(count >= 5){ System.out.println("/相连"); return true; } //判断\是否相连 count = 1; for(int i = y + 1 , j = x - 1; i < buttonArray.length && j >=0 ; i++ , j--){ if(buttonArray[i][x].getText().equals(qizi)){ System.out.println(buttonArray[i][x].getText()); count ++; }else{ break ; } } for(int i = y - 1 , j = x + 1; i >=0 && j < buttonArray[0].length ; i-- , j++){ if(buttonArray[i][x].getText().equals(qizi)){ System.out.println(buttonArray[i][x].getText()); count ++; }else{ break ; } } if(count >= 5){ System.out.println("\\相连"); return true; } return false; } }
Mybutton:
package BlackWhite; import javax.swing.JButton; public class MyButton extends JButton { private int yIndex; private int xIndex; public MyButton(int yIndex,int xIndex) { this.xIndex=xIndex; this.yIndex=yIndex; } public int getyIndex() { return yIndex; } public void setyIndex(int yIndex) { this.yIndex = yIndex; } public int getxIndex() { return xIndex; } public void setxIndex(int xIndex) { this.xIndex = xIndex; } }