我正在用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!");
}
}
}
}
答案 0 :(得分:1)
简要地查看您的代码,您可以在每次移动后维护一个董事会状态的堆栈/列表。
易于使用:Stack<int[][]> aStack = new Stack<int[][]>();
创建一种复制电路板阵列getBoardCopy()
然后每次搬家
aStack.push(getBoardCopy());
对于撤消按钮,创建一个设置板方法setBoard(int[][] aBoard())
,然后在撤消时调用它
setBoard(aStack.pop())
答案 1 :(得分:0)
您使用堆栈的直觉是正确的。您可以使用堆栈记录每个玩家选择的列。您不需要记录行或播放器,因为您可以从游戏的当前状态推断出这两件事。发生播放时,按下堆栈上的列。撤消时,从堆栈中弹出列,并将该列中最顶层的部分更改回-1。
编辑:每次撤消时都不要忘记切换用户。