JAVA 超简易版五子棋

来源:互联网 发布:大华dss7016网络配置 编辑:程序博客网 时间:2024/04/29 03:12

超简易版的五子棋

ui很丑 棋子全是用button实现

在判断是否组成五子的算法方面也是有问题的 

只实现了水平 竖直两方面的  而且是从下的这个子开始算的连续的 这显然也是有问题的。。

就先这么写吧  目前懒得改进了。。

package src;import java.io.*;import java.nio.Buffer;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;import java.util.*;public class Chess extends JFrame{JPanel p1=new JPanel();JButton resetB=new JButton("Reset");JButton readB=new JButton("Read");JButton saveB=new JButton("Save");JPanel p2=new JPanel();JButton button[][]=new JButton[25][25];boolean isBlack=true;//记录是否是黑棋先手int data[][]=new int[25][25];boolean gameOver=false;public Chess(){Container c=this.getContentPane();c.add(p1, BorderLayout.NORTH);p1.add(resetB);p1.add(readB);p1.add(saveB);c.add(p2, BorderLayout.CENTER);p2.setLayout(new GridLayout(25, 25));for(int i=0;i<25;i++){for(int j=0;j<25;j++){button[i][j]=new JButton("");}}for(int i=0;i<25;i++){for(int j=0;j<25;j++){p2.add(button[i][j]);button[i][j].setBackground(Color.GREEN);button[i][j].addActionListener(new Handler(i, j));}}resetB.addActionListener(new resetHandler());saveB.addActionListener(new saveHandler());readB.addActionListener(new readHandler());}class resetHandler implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubfor(int i=0;i<25;i++){for(int j=0;j<25;j++){button[i][j].setBackground(Color.GREEN);}}isBlack=true;gameOver=false;}}/** * @author Lenovo * 实现保存按钮的功能 */class saveHandler implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubfor(int i=0;i<25;i++){for(int j=0;j<25;j++){if(button[i][j].getBackground()==Color.BLACK)data[i][j]=1;else if(button[i][j].getBackground()==Color.WHITE)data[i][j]=-1;else data[i][j]=0;}}try{File file=new File("out.txt");BufferedWriter bw=new BufferedWriter(new FileWriter(file));for(int i=0;i<25;i++){for(int j=0;j<25;j++){if(j!=0)bw.write(",");bw.write(String.valueOf(data[i][j]));}bw.write("\r\n");}bw.write(isBlack+"\r\n");bw.flush();bw.close();}catch (Exception e1) {// TODO: handle exceptione1.printStackTrace();}}}/** * @author Lenovo * 实现打开按钮的功能 */class readHandler implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubtry{File file=new File("out.txt");BufferedReader br=new BufferedReader(new FileReader(file));String line=br.readLine();int num=0;while(line!=null&&num<25){String[] temp=line.split(",");//System.out.println("size: "+temp.length);for(int i=0;i<25;i++){data[num][i]=Integer.parseInt(temp[i]);}line=br.readLine();num++;}isBlack=Boolean.getBoolean(line);for(int i=0;i<25;i++){for(int j=0;j<25;j++){if(data[i][j]==1)button[i][j].setBackground(Color.BLACK);else if(data[i][j]==-1)button[i][j].setBackground(Color.WHITE);else button[i][j].setBackground(Color.GREEN);}}String ss=new String();if(isBlack)ss="黑";else ss="白";JOptionPane.showMessageDialog(null, "成功打开!现在到"+ss+"方下棋!");}catch (Exception e2) {// TODO: handle exceptione2.printStackTrace();}}}class Handler implements ActionListener{int row=-1,col=-1;public Handler(int x,int y){row=x;col=y;}boolean isValid(){if(button[row][col].getBackground()!=Color.GREEN)return false;return true;}public void changePlayer(){if(isBlack)isBlack=false;else isBlack=true;}public void whoWin(){//System.out.println("row : "+row+"  col:  "+col);//System.out.println(button[row][col].getBackground()==Color.BLACK);int count=0;if(isBlack){//此时是黑方下棋count=0;for(int i=row;i<row+5&&i<25;i++){//System.out.println(button[i][col].getBackground()==Color.BLACK);if(button[i][col].getBackground()==Color.BLACK)count++;}System.out.println("count: "+count);if(count==5){gameOver=true;JOptionPane.showMessageDialog(null, "黑方获胜");return;}count=0;for(int i=row;i>row-5&&i>=0;i--){if(button[i][col].getBackground()==Color.BLACK)count++;}System.out.println("count: "+count);if(count==5){gameOver=true;JOptionPane.showMessageDialog(null, "黑方获胜");return;}count=0;for(int j=col;j<col+5&&j<25;j++){if(button[row][j].getBackground()==Color.BLACK)count++;}if(count==5){gameOver=true;JOptionPane.showMessageDialog(null, "黑方获胜");return;}count=0;for(int j=col;j>col-5&&j>=0;j--){if(button[row][j].getBackground()==Color.BLACK)count++;}if(count==5){gameOver=true;JOptionPane.showMessageDialog(null, "黑方获胜");return;}}else{count=0;for(int i=row;i<row+5&&i<25;i++){if(button[i][col].getBackground()==Color.WHITE)count++;}System.out.println("count: "+count);if(count==5){gameOver=true;JOptionPane.showMessageDialog(null, "白方获胜");return;}count=0;for(int i=row;i>row-5&&i>=0;i--){if(button[i][col].getBackground()==Color.WHITE)count++;}System.out.println("count: "+count);if(count==5){gameOver=true;JOptionPane.showMessageDialog(null, "白方获胜");return;}count=0;for(int j=col;j<col+5&&j<25;j++){if(button[row][j].getBackground()==Color.WHITE)count++;}if(count==5){gameOver=true;JOptionPane.showMessageDialog(null, "白方获胜");return;}count=0;for(int j=col;j>col-5&&j>=0;j--){if(button[row][j].getBackground()==Color.white)count++;}if(count==5){gameOver=true;JOptionPane.showMessageDialog(null, "白方获胜");return;}}}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(gameOver){JOptionPane.showMessageDialog(null, "游戏结束 请重新开始");return;}if(!isValid()){JOptionPane.showMessageDialog(null, "位置重叠不合法");return;}if(isBlack)button[row][col].setBackground(Color.BLACK);else button[row][col].setBackground(Color.WHITE);whoWin();changePlayer();}}public static void main(String[] args){Chess chess=new Chess();chess.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);chess.setSize(500,500);chess.setVisible(true);}}


效果图:




    

0 0
原创粉丝点击