Java教程

Java--五子棋

本文主要是介绍Java--五子棋,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

近期由于某些原因暂时不能进行安卓开发的学习,今天仿照网上写了一个五子棋游戏

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class FiveChessGame {
private JFrame frame;//窗口
private GameMap map;//窗口容器
private JMenuBar bar;//菜单
private JMenu game;//菜单项
private JMenuItem[] items;//菜单下拉项
    //属性
    private static final int BOARD_SIZE=15;//棋盘大小
    private static final int ROW_WIDTH=36;//间距
    private static final int SPACE=ROW_WIDTH/2;
    private boolean player;//设置棋
    private char[][]board;
    public FiveChessGame(){
        player=true;//黑棋先下
        board=new char[BOARD_SIZE][BOARD_SIZE];
        clearMap();
        createGUI();
    }
    private void createGUI(){
       frame=new JFrame("五子棋");//框架
        map=new GameMap();//容器
        bar=new JMenuBar();//建立菜单栏
        game=new JMenu("游戏");//建立菜单
        items=new JMenuItem[2];//建立菜单项
        game.add(items[0]=new JMenuItem("重新开始"));
        game.add(items[1]=new JMenuItem("退出"));
    }
    //清空
    protected void clearMap(){
        for(int i=0;i<BOARD_SIZE;i++){
            for(int j=0;j<BOARD_SIZE;j++){
                board[i][j]='+';
            }
        }
    }
    //运行游戏
    public void start(){
        map.setPreferredSize(new Dimension(ROW_WIDTH*(BOARD_SIZE-1)+SPACE*2,ROW_WIDTH*(BOARD_SIZE-1)+SPACE*2));
        map.addMouseListener (new MouseAdapter(){
           //鼠标点击
           public void mouseClicked(MouseEvent e){
               //转换,将点击位置坐标转换为棋子数组
               int xPos=(int)(e.getX()/ROW_WIDTH);
               int yPos=(int)(e.getY()/ROW_WIDTH);
               if(board[xPos][yPos]=='+'){
                   board[xPos][yPos]=player ? 'b':'w';
                   map.repaint();
                   if(isWin(xPos,yPos)){//判断是否有玩家胜利
                       map.repaint();//刷新
                       String str =player?"黑方胜":"白方胜";
                       JOptionPane.showMessageDialog(null,str,"游戏结束",JOptionPane.PLAIN_MESSAGE);
                       clearMap();//清空棋盘
                       map.repaint();//刷新棋盘
                       player=true;//新一局
                   }else{
                       player=player?false:true;//切换玩家
                   }
               }
           }
        });
        //重新开始
        items[0].addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {

            }

            public void acctionPerformed(ActionEvent e){
              String str="是否重新开始游戏";
              if(JOptionPane.showConfirmDialog(null,str,"重新开始",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
                  clearMap();
                  map.repaint();
                  player=true;
              }
            }
    });
        //退出事件
        items[1].addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {

            }

            public void acctionPerformed(ActionEvent e){
              String str="是否要退出游戏";
              if(JOptionPane.showConfirmDialog(null,str,"重新开始",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
                  System.exit(0);//退出
              }
            }
        });
        bar.add(game);
        frame.setJMenuBar(bar);
        frame.add(map);
        frame.pack();
        frame.setLocation(250, 50);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    }

    protected boolean find(int x, int y) {
        int i, num = 1;
        char temp = player ? 'b' : 'w';
        for (i = x + 1; i < 15; i++) {// 向下找
            if (board[i][y] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        for (i = x - 1; i >= 0; i--) {// 向上找
            if (board[i][y] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        return false;
    }
    protected boolean find2(int x, int y) {
        int i, num = 1;
        char temp = player ? 'b' : 'w';
        for (i = y + 1; i < 15; i++) { // 向右找
            if (board[x][i] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        for (i = y - 1; i >= 0; i--) {// 向左找
            if (board[x][i] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        return false;
    }

    protected boolean find3(int x, int y) {
        int i, j, num = 1;
        char temp = player ? 'b' : 'w';
        for (i = x + 1, j = y + 1; i < 15 && j < 15; i++, j++) {// 向下方
            if (board[i][j] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        for (i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) {// 向上方
            if (board[i][j] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        return false;
    }

    protected boolean find4(int x, int y) {
        int i, j, num = 1;
        char temp = player ? 'b' : 'w';
        for (i = x + 1, j = y - 1; i < 15 && j >= 0; i++, j--) {// 向下
            if (board[i][j] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        for (i = x - 1, j = y + 1; i >= 0 && j < 15; i--, j++) { // 向上
            if (board[i][j] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        return false;
    }

   //判断胜负
    protected boolean isWin(int x, int y) {
        return (find(x, y) || find2(x, y) || find3(x, y) || find4(x, y));
    }

    private class GameMap extends JPanel {
        private static final long serialVersionUID = 16578987565248L;

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            // 棋盘
            g.setColor(new Color(200, 100, 50)); // 设为桔黄色
            g.fillRect(0, 0, ROW_WIDTH * (BOARD_SIZE + 1), ROW_WIDTH
                    * (BOARD_SIZE + 1)); // 填充棋盘
            g.setColor(Color.black); // 设为黑色
            for (int i = 0; i < BOARD_SIZE; i++) {// 画竖线
                g.drawLine(SPACE + ROW_WIDTH * i, SPACE, SPACE + ROW_WIDTH * i,
                        SPACE + ROW_WIDTH * (BOARD_SIZE - 1));
            }
            for (int i = 0; i < BOARD_SIZE; i++) {// 画横线
                g.drawLine(SPACE, SPACE + ROW_WIDTH * i, SPACE + ROW_WIDTH
                        * (BOARD_SIZE - 1), SPACE + ROW_WIDTH * i);
            }
            // 画点
            g.fillOval(SPACE + 3 * ROW_WIDTH - 3, SPACE + 3 * ROW_WIDTH - 3, 7,
                    7);
            g.fillOval(SPACE + 11 * ROW_WIDTH - 3, SPACE + 3 * ROW_WIDTH - 3,
                    7, 7);
            g.fillOval(SPACE + 3 * ROW_WIDTH - 3, SPACE + 11 * ROW_WIDTH - 3,
                    7, 7);
            g.fillOval(SPACE + 7 * ROW_WIDTH - 3, SPACE + 7 * ROW_WIDTH - 3, 7,
                    7);
            g.fillOval(SPACE + 11 * ROW_WIDTH - 3, SPACE + 11 * ROW_WIDTH - 3,
                    7, 7);

            // 棋子
            // 遍历数组,绘制棋子。
            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] == 'b') {// 绘制黑棋
                        g.setColor(Color.black);
                        g.fillOval(SPACE + i * ROW_WIDTH - ROW_WIDTH / 2, SPACE
                                        + j * ROW_WIDTH - ROW_WIDTH / 2, ROW_WIDTH,
                                ROW_WIDTH);
                    }
                    if (board[i][j] == 'w') {// 绘制白棋
                        g.setColor(Color.white);
                        g.fillOval(SPACE + i * ROW_WIDTH - ROW_WIDTH / 2, SPACE
                                        + j * ROW_WIDTH - ROW_WIDTH / 2, ROW_WIDTH,
                                ROW_WIDTH);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        FiveChessGame game = new FiveChessGame();
        game.start();
    }
}

  

这篇关于Java--五子棋的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!