0

我正在用 java 制作一个 Connect 4 游戏,但对于如何为其制作撤消方法有点困惑。我知道这可以使用 ArrayList 或 Stack 来完成,但我不太确定如何实现它。我的游戏和 GUI 代码如下所示,任何帮助将不胜感激!

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!");
            }




        }

    }


}
4

2 回答 2

1

简要查看您的代码,您可以在每次移动后维护板状态的堆栈/列表。

初始化很容易使用:Stack<int[][]> aStack = new Stack<int[][]>();

创建一个方法来复制你的板数组getBoardCopy()

然后对于每一个动作

aStack.push(getBoardCopy());

对于撤消按钮创建一个设置板方法setBoard(int[][] aBoard()),然后在撤消时调用它

setBoard(aStack.pop())
于 2013-09-29T22:44:05.157 回答
0

您使用 Stack 的直觉是正确的。您可以使用堆栈来记录每个玩家选择的列。您不需要记录行或玩家,因为您可以从游戏的当前状态推断出这两件事。当播放发生时,将列推入堆栈。当您撤消时,从堆栈中弹出该列,并将该列中最顶部的部分更改回 -1。

编辑:不要忘记每次撤消时切换用户。

于 2013-09-29T22:55:21.863 回答