DOC

goBang

By Russell Warren,2014-09-04 13:11
7 views 0
goBanggoBa

package gobangSwing;

import java.awt.*;

    import javax.imageio.ImageIO;

    import javax.swing.*;

    import java.awt.event.*;

    import java.awt.image.*;

    import java.io.*;

public class GoBang {

     public static File tableImage = new File("image/board.jpg");

     JFrame f = new JFrame("五子棋");

     BufferedImage table;

     BufferedImage black;

     BufferedImage white;

     BufferedImage selected;

     private static int BOARD_SIZE = 15;

     private final int TABLE_W = 535;

     private final int TABLE_H = 536;

     private final int RATE = (TABLE_W-10)/BOARD_SIZE;

     private final int X_OFFSET = 5;

     private final int Y_OFFSET = 6;

     private int x = 0;

     private int y = 0;

     private int[][] board = new int[BOARD_SIZE][BOARD_SIZE];

     ChessBoard chessBoard = new ChessBoard();

     //IsWon iswon = new IsWon();

     public void init()throws Exception

     {

     table = ImageIO.read(tableImage);

     black = ImageIO.read(new File("image/black.gif"));

     white = ImageIO.read(new File("image/white.gif"));

     selected = ImageIO.read(new File("image/selected.gif"));

     for(int i=0;i<BOARD_SIZE;i++)

     {

     for(int j=0;j<BOARD_SIZE;j++)

     {

     board[i][j] = -1;

     }

     }

     chessBoard.setPreferredSize(new Dimension(TABLE_W,TABLE_H));

     chessBoard.addMouseListener(new MouseAdapter()

     {

     public void mouseClicked(MouseEvent e)

     {

     boolean flag = false;

     int xPos = (int)(e.getX() - X_OFFSET) / RATE;

     int yPos = (int)(e.getY() - Y_OFFSET) / RATE;

     //System.out.println(xPos+"xxx"+yPos);

     while(board[xPos][yPos] == -1)

     {

     board[xPos][yPos] = 1;

     if(isWon(xPos, yPos, 1,board))

     {

     System.out.println("you are winner");

     JDialog d1= new JDialog(f,true);

     d1.add(new JTextArea("you are winner"),BorderLayout.NORTH);

     JButton queDing = new JButton("确定");

     queDing.addActionListener(new ActionListener()

     {

     @Override

     public void actionPerformed(ActionEvent e) {

     // TODO Auto-generated method stub

     System.exit(0);

     }

     });

     d1.add(queDing,BorderLayout.SOUTH);

     d1.pack();

     d1.setLocationRelativeTo(null);

     d1.setVisible(true);

     }

     flag = true;

     }

     while(flag)

     {

     flag = false;

     xPos = (int) (Math.random() * (BOARD_SIZE ));

     yPos = (int) (Math.random() * (BOARD_SIZE ));

     if(board[yPos][xPos] != -1)

     {

     flag = true;

     }

     else

     {

     board[yPos][xPos] = 0;

     if(isWon(xPos, yPos, 0,board))

     {

     System.out.println("you are loster");

     JDialog d2= new JDialog(f,true);

     JButton queDing2 = new JButton("确定");

     queDing2.addActionListener(new ActionListener()

     {

     @Override

     public void actionPerformed(ActionEvent e) {

     // TODO Auto-generated method stub

     System.exit(0);

     }

     });

     d2.add(new TextArea("you are loster"),BorderLayout.NORTH);

     d2.add(queDing2,BorderLayout.SOUTH);

     d2.pack();

     d2.setLocationRelativeTo(null);

     d2.setVisible(true);

     }

     }

     }

     chessBoard.repaint();

     }

     public void mouseExited(MouseEvent e)

     {

     x = 0;

     y = 0;

     chessBoard.repaint();

     }

     });

     chessBoard.addMouseMotionListener(new MouseMotionAdapter()

     {

     public void mouseMoved(MouseEvent e)

     {

     x = (e.getX() - X_OFFSET) / RATE;

     y = (e.getY() - Y_OFFSET) / RATE;

     chessBoard.repaint();

     }

     });

     //chessBoard.repaint();

     f.setSize(table.getWidth(), table.getHeight()+20);

     f.setLocationRelativeTo(null);

     f.add(chessBoard);

     f.setVisible(true);

     }

     class ChessBoard extends JPanel

     {

     public void paint(Graphics g)

     {

     g.drawImage(table, 0,0, null);

     if(x >= 0&&y >= 0)

     {

     g.drawImage(selected, x*RATE+X_OFFSET, y*RATE+Y_OFFSET, null);

     }

     for(int i = 0 ; i < BOARD_SIZE ; i++)

     {

     for(int j = 0 ; j < BOARD_SIZE ; j++)

     {

     if(board[i][j] == 1)

     {

     g.drawImage(white,i*RATE+X_OFFSET,j*RATE+Y_OFFSET,null);

     }

     if(board[i][j] == 0)

     {

     g.drawImage(black,i*RATE+X_OFFSET,j*RATE+Y_OFFSET,null);

     }

     }

     }

     }

     }

     //class IsWon

     //{

     boolean isWon(int x , int y ,int PANDUAN,int [][]board)

     {

     int y_start;

     int y_end;

     int x_start;

     int x_end;

     int count = 0;

     //左右

     if((y-4) >= 0 && y+4 < BOARD_SIZE)

     {

     y_start = y-4;

     y_end = y+4;

     }

     else

     {

     if(y+4 < BOARD_SIZE)

     {

     y_start = 0;

     y_end = y+4;

     }

     else

     {

     y_start = y-4;

     y_end = BOARD_SIZE-1;

     }

     }

     for(int i = y_start ; i <= y_end ; i++)

     {

     if(board[x][i] == PANDUAN)

     {

     count++;

     if(count == 5)

     return true;

     }

     else

     {

     count = 0;

     }

     }

     //上下

     if(x-4 >= 0 && x+4 < BOARD_SIZE)

     {

     x_start = x-4;

     x_end = x+4;

     }

     else

     {

     if(x+4 < BOARD_SIZE)

     {

     x_start = 0;

     x_end = x+4;

     }

     else

     {

     x_start = x-4;

     x_end = BOARD_SIZE-1;

     }

     }

     for(int i = x_start ; i <= x_end ; i++)

     {

     if(board[i][y] == PANDUAN)

     {

     count++;

     if(count == 5)

     return true;

     }

     else

     {

     count = 0;

     }

     }

     //左上到右下

     int xx = x-1;

     int xy = y-1;

     boolean flag = true;

     count = 1;

     while(flag && xx >= 0 && xy >= 0 && xx < BOARD_SIZE && xy <

    BOARD_SIZE)

     {

     if(board[xx][xy] == PANDUAN)

     {

     xx--;

     xy--;

     count++;

     }

     else

     {

     flag = false;

     }

     }

     xx = x+1;

     xy = y+1;

     flag = true;

     while(flag && xx >= 0 && xy >= 0 && xx < BOARD_SIZE && xy <

    BOARD_SIZE)

     {

     if(board[xx][xy] == PANDUAN )

     {

     xx--;

     xy--;

     count++;

     }

     else

     {

     flag = false;

     }

     }

     if(count >= 5)

     {

     return true;

     }

     //右上到左下

     xx = x+1;

     xy = y-1;

     count = 1;

     flag = true ;

     while(flag&& xx >= 0 && xy >= 0 && xx < BOARD_SIZE && xy <

    BOARD_SIZE)

     {

     if(board[xx][xy] == PANDUAN )

     {

     xx++;

     xy--;

     count++;

     }

     else

     {

     flag = false;

     }

     }

     xx = x-1;

     xy = y+1;

     flag = true;

     while(flag && xx >= 0 && xy >= 0 && xx < BOARD_SIZE && xy <

    BOARD_SIZE)

     {

     if(board[xx][xy] == PANDUAN)

     {

     xx--;

     xy++;

     count++;

     }

     else

     {

     flag = false;

     }

     }

     if(count >= 5)

     {

     return true;

     }

     return false;

     }

     //}

     public static void main(String []args) throws Exception

     {

     new GoBang().init();

     }

}

Report this document

For any questions or suggestions please email
cust-service@docsford.com