连接4 Java游戏撤销按钮 [英] Connect 4 Java Game Undo Button

查看:150
本文介绍了连接4 Java游戏撤销按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在在Java连接4个游戏和我有点卡住至于如何撤销的方法吧。我知道这可以既使用ArrayList或堆栈来完成,但我不太清楚如何实现它。我的code为游戏和GUI下面可以看到,任何帮助将大大AP preciated!

进口javax.swing.JOptionPane中;
公共类ConnectFourGame {    私人INT [] []板;
    //私人GameStatus状态;;
    私人诠释的球员,BSIZE;
    公共ConnectFourGame(){
        //状态= GameStatus.InProgress;        对象[]可能性= {4,5,6,7,8,9,10,
                11,12,13,14,15,16,17,18,19};        字符串s =(字符串)JOptionPane.showInputDialog(NULL,
                选择线路板尺寸:,尺寸,JOptionPane.PLAIN_MESSAGE,
                空,可能性,10);        如果(S ==空||(S =空&放大器;!及(.equals(S)))){
            S =10;
        }        BSIZE =的Integer.parseInt(S);        对象[] playerSelect = {1,2};
        字符串s2 =(字符串)JOptionPane.showInputDialog(NULL,
                选择播放器启动,开始,JOptionPane.PLAIN_MESSAGE,
                空,playerSelect,1);        如果(S2 ==空||(S2 =空&放大器;!及(.equals(S)))){
            S2 =1;
        }        玩家=的Integer.parseInt(S2);        板=新INT [BSIZE] [BSIZE];
        重置();
    }    公众诠释的getSize(){
        返回BSIZE;
    }    公共无效复位(){
        对于(INT R = 0;为r BSIZE; R ++)
            对于(INT C = 0;℃下BSIZE; C ++)
                板[R] [C] = -1;
    }
    公众诠释selectCol(INT PCOL){        对于(INT R = BSIZE - 1; R> = 0; R--)
            如果(板[R] [PCOL] == -1){
                板[R] [PCOL] =玩家;
                返回ř;
            }        返回-1;
    }    公众诠释nextPlayer(){        如果(玩家== 1)
            玩家= 2;
        其他
            玩家= 1;        返回的球员;
    }    公众诠释getCurrentPlayer(){
        返回的球员;
    }    公共GameStatus isWinner(){
        诠释计数= 0;
        对于(INT R = 0;为r BSIZE; R ++)
            对于(INT C = 0; C< BSIZE - 3; C ++)
                如果((板[R] [C] == 1)及及(板[R] [C + 1] == 1)及和放大器;
                    (板[R] [C + 2] == 1)及和放大器; (板[R] [C + 3] == 1)){
                        返回GameStatus.Player1WON;
                }        对于(INT R = 0;为r BSIZE; R ++)
            对于(INT C = 0; C< BSIZE - 3; C ++)
                如果((板[R] [C] == 2)及及(板[R] [C + 1] == 2)及和放大器;
                    (板[R] [C + 2] == 2)及和放大器; (板[R] [C + 3] == 2)){
                        返回GameStatus.Player2WON;
    }        对于(INT C = 0;℃下BSIZE; C ++)
            对(INT R = 0;为r(BSIZE - 3); R ++)
                如果((板[R] [C] == 1)及及(板[R + 1] [C] == 1)及和放大器;
                    (板[R + 2] [C] == 1)及和放大器; (板[R + 3] [C] == 1)){
                        返回GameStatus.Player1WON;
    }
        对于(INT C = 0;℃下BSIZE; C ++)
            对(INT R = 0;为r(BSIZE - 3); R ++)
                如果((板[R] [C] == 2)及及(板[R + 1] [C] == 2)及和放大器;
                    (板[R + 2] [C] == 2)及和放大器; (板[R + 3] [C] == 2)){
                        返回GameStatus.Player2WON;
                }        对于(INT R = 0;为r BSIZE - 3; R ++)
            对于(INT C = 0; C< BSIZE - 3; C ++)
                如果((板[R] [C] == 1)及及(板[R + 1] [c + 1] == 1)及;&放大器;
                    (板[R + 2] [c + 2以下] == 1)及;&放大器; (板[R + 3] [C + 3] == 1)){
                        返回GameStatus.Player1WON;
    }        对于(INT R = BSIZE - 1; R> = 3; R--)
            对于(INT C = 0; C< BSIZE - 3; C ++)
                如果((板[R] [C] == 1)及及(板[R-1] [c + 1] == 1)及;&放大器;
                    (板[R-2] [c + 2以下] == 1)及;&放大器; (板[R-3] [C + 3] == 1)){
                        返回GameStatus.Player1WON;
                }        对于(INT R = 0;为r BSIZE - 3; R ++)
            对于(INT C = 0; C< BSIZE - 3; C ++)
                如果((板[R] [C] == 2)及及(板[R + 1] [C + 1] == 2)及和放大器;
                    (板[R + 2] [c + 2以下] == 2)及;&放大器; (板[R + 3] [C + 3] == 2)){
                        返回GameStatus.Player2WON;
                }        对于(INT R = BSIZE - 1; R> = 3; R--)
            对于(INT C = 0; C< BSIZE - 3; C ++)
                如果((板[R] [C] == 2)及及(板[R-1] [C + 1] == 2)及和放大器;
                    (板[R-2] [c + 2以下] == 2)及;&放大器; (板[R-3] [C + 3] == 2)){
                        返回GameStatus.Player2WON;
                }        对于(INT R = 0;为r BSIZE; R ++)
            对于(INT C = 0;℃下BSIZE; C ++)
                如果(板[R] [C]!= -1)
                    算上++;
                如果(计数==(BSIZE)*(BSIZE))
                    返回GameStatus.Cats;        返回GameStatus.InProgress;
    }    公众诠释[] [] getBoard(){
        返回板;
    }    公共无效撤消(){    }}

进口java.awt中的*。
java.awt.event中导入*。进口的javax.swing *。
公共类ConnectFourPanel继承JPanel {    静态最后的serialVersionUID长1L =;
    私人的JLabel [] []板;
    私人的JButton []选择;
    私人JPanel的顶部;
    私人JPanel的底部;
    私人JButton的退出;
    私人JButton的复位;
    私人JButton的撤消;
    私人ConnectFourGame游戏;
    私人诠释boardSize;    私人的JMenuItem quitItem;
    私人的JMenuItem newGameItem;    公共ConnectFourPanel(JMenuItem的quitItem,JMenuItem的gameItem){
        游戏=新ConnectFourGame();
        boardSize = game.getSize();
        this.quitItem = quitItem;
        this.newGameItem = gameItem;        顶部=新JPanel();
        底部=新JPanel();        复位=的新的JButton(重置);
        top.add(重置);
        撤消=的新的JButton(撤消);
        top.add(撤销);
        出口=的新的JButton(退出);
        top.add(退出);        bottom.setLayout(新的GridLayout(boardSize + 1,boardSize,1,1)); //室顶行        ButtonListener监听器=新ButtonListener();
        exit.addActionListener(监听);
        reset.addActionListener(监听);
        undo.addActionListener(监听);
        quitItem.addActionListener(监听);
        newGameItem.addActionListener(监听);        选择=的新的JButton [boardSize]        对于(INT COL = 0;&山坳下,boardSize;西++){
            选择[COL] =的新的JButton(选择);
            选择[COL] .addActionListener(监听);
            bottom.add(选择[COL]);
        }        板=新的JLabel [boardSize] [boardSize];        对于(INT行= 0;&行LT; boardSize;排++){
            对于(INT COL = 0;&山坳下,boardSize;西++){
                板[行] [COL] =新的JLabel(X);
                板[行] [COL] .setForeground(Color.RED);
                bottom.add(板[行] [COL]);
            }
        }        的setLayout(新的BorderLayout());
        加(BorderLayout.NORTH,顶部);
        加(BorderLayout.CENTER,底部);
    }    // ************ *****************
    //重新presents为按下按钮(动作)事件侦听器。
    // ************ *****************
    私有类ButtonListener实现的ActionListener
    {
        // ------------------------------------------------ --------------
        //更新计数和标签,当按钮被按下。
        // ------------------------------------------------ --------------
        公共无效的actionPerformed(ActionEvent的事件)
        {            JComponent的补偿=(JComponent中)event.getSource();
            boardSize = game.getSize();            如果((对比==出口)||(quitItem == COMP))
                System.exit(1);            如果(对比== ||重置== newGameItem COMP){
                bottom.removeAll();
                游戏=新ConnectFourGame();
                boardSize = game.getSize();
                bottom.setLayout(新的GridLayout(boardSize + 1,boardSize,1,1));
                ButtonListener监听器=新ButtonListener();
                选择=的新的JButton [boardSize]
                对于(INT COL = 0;&山坳下,boardSize;西++){
                    选择[COL] =的新的JButton(选择);
                    选择[COL] .addActionListener(监听);
                    bottom.add(选择[COL]);
                }                板=新的JLabel [boardSize] [boardSize];                对于(INT行= 0;&行LT; boardSize;排++){
                    对于(INT COL = 0;&山坳下,boardSize;西++){
                        板[行] [COL] =新的JLabel(X);
                        板[行] [COL] .setForeground(Color.RED);
                        bottom.add(板[行] [COL]);
                    }
                }                重新验证();
                重绘();            }            对于(INT COL = 0;&山坳下,boardSize;西++)
                如果(对比==选择[COL]){
                    INT行= game.selectCol(COL);
                    如果(行!= -1){
                        板[行] [COL] .setText(+ game.getCurrentPlayer());
                        game.nextPlayer();
                    }其他
                        JOptionPane.showMessageDialog(NULL,列满了!);                }            如果(game.isWinner()== GameStatus.Player1WON){
                JOptionPane.showMessageDialog(NULL,PLAYER1赢了!);
            }            如果(game.isWinner()== GameStatus.Player2WON){
                JOptionPane.showMessageDialog(NULL,Player2赢了!);
            }            如果(game.isWinner()== GameStatus.Cats){
                JOptionPane.showMessageDialog(NULL,猫的游戏!);
            }
        }    }
}


解决方案

简单看你的code可以在每次移动后保持板状态的堆叠/列表。

要initalise很容易使用:堆栈< INT [] []> aStack =新的堆栈< INT [] []>();

创建复制你的板阵列的方法 getBoardCopy()

然后为每个移动

  aStack.push(getBoardCopy());

有关撤消按钮创建一组板的方法 setBoard(INT [] []乘坐())然后就撤消时,调用此

  setBoard(aStack.pop())

I am making a Connect 4 Game in java and am a little stuck as to how to make an undo method for it. I know this can be done using either an ArrayList or a Stack but I am not too sure how to implement it. My code for the game and GUI can be seen below, any help would be greatly appreciated!

import javax.swing.JOptionPane;


public class ConnectFourGame {

    private int[][] board;
    //private GameStatus status;;
    private int player, bSize;


    public ConnectFourGame () {
        //status = GameStatus.InProgress;

        Object[] possibilities = {"4", "5", "6", "7", "8", "9", "10", 
                "11", "12", "13", "14", "15", "16", "17", "18", "19"};

        String s = (String)JOptionPane.showInputDialog(null,
                "Choose Board Size:", "Sizes", JOptionPane.PLAIN_MESSAGE,
                null, possibilities, "10");

        if(s == null || (s != null && ("".equals(s)))){
            s = "10";
        }

        bSize = Integer.parseInt(s);

        Object[] playerSelect = {"1", "2"};
        String s2 = (String)JOptionPane.showInputDialog(null,
                "Choose Player to Start", "Start", JOptionPane.PLAIN_MESSAGE,
                null, playerSelect, "1");

        if(s2 == null || (s2 != null && ("".equals(s)))){
            s2 = "1";
        }

        player = Integer.parseInt(s2);

        board = new int[bSize][bSize];
        reset();
    }

    public int getSize(){
        return bSize;
    }

    public void reset(){
        for (int r = 0; r < bSize; r++)
            for (int c = 0; c < bSize; c++)
                board[r][c] = -1;
    }
    public int selectCol (int pCol) {

        for (int r = bSize - 1; r >= 0; r--)
            if (board[r][pCol] == -1){
                board[r][pCol] = player;
                return r;
            }

        return -1;
    }

    public int nextPlayer() {

        if (player == 1)
            player = 2;
        else
            player = 1;

        return player;
    }

    public int getCurrentPlayer () {
        return player;
    }

    public GameStatus isWinner() {  
        int count = 0;
        for (int r = 0; r < bSize; r++)
            for (int c = 0; c < bSize - 3; c++)
                if ((board[r][c] == 1) && (board[r][c + 1] == 1) && 
                    (board[r][c + 2] == 1) && (board[r][c + 3] == 1)){
                        return GameStatus.Player1WON;
                }

        for (int r = 0; r < bSize; r++)
            for (int c = 0; c < bSize - 3; c++)
                if ((board[r][c] == 2) && (board[r][c + 1] == 2) && 
                    (board[r][c + 2] == 2) && (board[r][c + 3] == 2)){
                        return GameStatus.Player2WON;
    }

        for (int c = 0; c < bSize; c++)
            for (int r = 0; r < (bSize - 3); r++)
                if ((board[r][c] == 1) && (board[r + 1][c] == 1) && 
                    (board[r + 2][c] == 1) && (board[r + 3][c] == 1)){
                        return GameStatus.Player1WON;
    }   
        for (int c = 0; c < bSize; c++)
            for (int r = 0; r < (bSize - 3); r++)
                if ((board[r][c] == 2) && (board[r + 1][c] == 2) && 
                    (board[r + 2][c] == 2) && (board[r + 3][c] == 2)){
                        return GameStatus.Player2WON;
                }

        for (int r = 0; r < bSize - 3; r++)
            for (int c = 0; c < bSize - 3; c++)
                if ((board[r][c] == 1) && (board[r+1][c + 1] == 1) && 
                    (board[r+2][c + 2] == 1) && (board[r+3][c + 3] == 1)){
                        return GameStatus.Player1WON;
    }       

        for (int r = bSize - 1; r >= 3; r--)
            for (int c = 0; c < bSize - 3; c++)
                if ((board[r][c] == 1) && (board[r-1][c + 1] == 1) && 
                    (board[r-2][c + 2] == 1) && (board[r-3][c + 3] == 1)){
                        return GameStatus.Player1WON;
                }

        for (int r = 0; r < bSize - 3; r++)
            for (int c = 0; c < bSize - 3; c++)
                if ((board[r][c] == 2) && (board[r+1][c + 1] == 2) && 
                    (board[r+2][c + 2] == 2) && (board[r+3][c + 3] == 2)){
                        return GameStatus.Player2WON;
                }

        for (int r = bSize - 1; r >= 3; r--)
            for (int c = 0; c < bSize - 3; c++)
                if ((board[r][c] == 2) && (board[r-1][c + 1] == 2) && 
                    (board[r-2][c + 2] == 2) && (board[r-3][c + 3] == 2)){
                        return GameStatus.Player2WON;
                }

        for (int r = 0; r < bSize; r++)
            for (int c = 0; c < bSize; c++)
                if(board[r][c] != -1)
                    count ++;
                if(count == (bSize)*(bSize))
                    return GameStatus.Cats;



        return GameStatus.InProgress;
    }

    public int [][] getBoard() {
        return board;
    }

    public void undo(){

    }



}

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class ConnectFourPanel extends JPanel{

    static final long serialVersionUID = 1L;
    private JLabel[][] board;
    private JButton[] selection;
    private JPanel top;
    private JPanel bottom;
    private JButton exit;
    private JButton reset;
    private JButton undo;
    private ConnectFourGame game;
    private int boardSize;

    private JMenuItem quitItem;
    private JMenuItem newGameItem;

    public ConnectFourPanel(JMenuItem quitItem, JMenuItem gameItem){
        game = new ConnectFourGame();
        boardSize = game.getSize();
        this.quitItem = quitItem;
        this.newGameItem = gameItem;

        top = new JPanel();
        bottom = new JPanel();  

        reset = new JButton ("Reset");
        top.add(reset);
        undo = new JButton("Undo");
        top.add(undo);
        exit = new JButton ("Exit");
        top.add(exit);

        bottom.setLayout(new GridLayout(boardSize+1,boardSize,1,1));  // room for top row

        ButtonListener listener = new ButtonListener();
        exit.addActionListener(listener);
        reset.addActionListener(listener);
        undo.addActionListener(listener);
        quitItem.addActionListener(listener);
        newGameItem.addActionListener(listener);

        selection = new JButton[boardSize];

        for (int col = 0; col < boardSize; col++) {
            selection[col] = new JButton ("Select");
            selection[col].addActionListener(listener);
            bottom.add(selection[col]);
        }

        board = new JLabel[boardSize][boardSize];

        for (int row = 0; row < boardSize; row++) {
            for (int col = 0; col < boardSize; col++) {
                board[row][col] = new JLabel("X");
                board[row][col].setForeground(Color.RED);
                bottom.add(board[row][col]);                    
            }
        }

        setLayout(new BorderLayout());
        add (BorderLayout.NORTH,top);
        add (BorderLayout.CENTER,bottom);
    }



    //*****************************************************************
    //  Represents a listener for button push (action) events.
    //*****************************************************************
    private class ButtonListener implements ActionListener
    {
        //--------------------------------------------------------------
        //  Updates the counter and label when the button is pushed.
        //--------------------------------------------------------------
        public void actionPerformed (ActionEvent event)
        {

            JComponent comp = (JComponent) event.getSource();
            boardSize = game.getSize();

            if ((comp == exit) || (quitItem == comp))
                System.exit(1);

            if(comp == reset || newGameItem == comp){
                bottom.removeAll();
                game = new ConnectFourGame();
                boardSize = game.getSize();
                bottom.setLayout(new GridLayout(boardSize + 1,boardSize,1,1));


                ButtonListener listener = new ButtonListener();
                selection = new JButton[boardSize];
                for (int col = 0; col < boardSize; col++) {
                    selection[col] = new JButton ("Select");
                    selection[col].addActionListener(listener);
                    bottom.add(selection[col]);
                }

                board = new JLabel[boardSize][boardSize];

                for (int row = 0; row < boardSize; row++) {
                    for (int col = 0; col < boardSize; col++) {
                        board[row][col] = new JLabel("X");
                        board[row][col].setForeground(Color.RED);
                        bottom.add(board[row][col]);                    
                    }
                }

                revalidate();
                repaint();

            }

            for(int col = 0; col < boardSize; col++)
                if(comp == selection[col]){
                    int row = game.selectCol(col);
                    if(row != -1){
                        board[row][col].setText("" + game.getCurrentPlayer());
                        game.nextPlayer();
                    }else
                        JOptionPane.showMessageDialog(null, "Column is full!");

                }

            if (game.isWinner() == GameStatus.Player1WON){
                JOptionPane.showMessageDialog(null,"Player1 won!");
            }

            if (game.isWinner() == GameStatus.Player2WON){
                JOptionPane.showMessageDialog(null,"Player2 won!");
            }

            if (game.isWinner() == GameStatus.Cats){
                JOptionPane.showMessageDialog(null,"Cats Game!");
            }




        }

    }


}

解决方案

Briefly looking at your code you could maintain a Stack/List of the board states after each move.

To initalise is easy use: Stack<int[][]> aStack = new Stack<int[][]>();

Create a method to copy your board array getBoardCopy()

Then for each move

aStack.push(getBoardCopy());

For undo button create a set board method setBoard(int[][] aBoard()) then just call this when undoing

setBoard(aStack.pop())

这篇关于连接4 Java游戏撤销按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆