私は Java で Connect 4 Game を作成していますが、元に戻す方法を作成する方法について少し困っています。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!");
}
}
}
}