I'm posting the code of a game that I wrote recently. Game name is FifteenPuzzle or N-Puzzle.
You can download the file here.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections;
<p> <code><b></b></code> <code></b></code></b> <b></b> <code><b> </b></code> <code><b></b></code> <b></b> <code><b> </b></code> <code><b> </b></code> </p>
@author @gmail
public class FifteenPuzzle implements ActionListener {
private static final int DIM = 4; final int SIZE = DIM * DIM; final String[] WIN = new String[SIZE-1]; private static final int HEIGHT = 400; private static final int WIDTH = 400; private int emptyCell = DIM * DIM; private JButton[][] board = new JButton[DIM][DIM]; private JFrame frame; private JPanel panel = new JPanel();
public FifteenPuzzle() { if(DIM <= 1){ JOptionPane.showMessageDialog(null, "You Win The Game."); System.err.println("Dimention Should be greater than 1 to play"); System.exit(0); } for (int i = 1; i < SIZE; i++) { WIN[i-1] = Integer.toString(i); } System.out.println("Win State:" + Arrays.asList(WIN) ); }
public static void main(String[] args) { FifteenPuzzle game = new FifteenPuzzle(); game.initializeBoard();
}
@param @param @return private int getIndex(int i, int j) { return ((i * DIM) + j);
}
private void initializeBoard() { ArrayList<Integer> intialList = new ArrayList<Integer>(SIZE);
for (boolean isSolvable = false; isSolvable == false;) {
intialList = new ArrayList<Integer>(SIZE); for (int i = 0; i < SIZE; i++) { intialList.add(i, i); }
Collections.shuffle(intialList);
isSolvable = isSolvable(intialList); } System.out.println("Initial Board state:" + intialList);
for (int index = 0; index < SIZE; index++) { final int ROW = index / DIM; final int COL = index % DIM; board[ROW][COL] = new JButton(String.valueOf(intialList.get(index))); if (intialList.get(index) == 0) { emptyCell = index; board[ROW][COL].setVisible(false); }
board[ROW][COL].setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); board[ROW][COL].setBackground(Color.BLACK); board[ROW][COL].setForeground(Color.GREEN); board[ROW][COL].addActionListener(this); panel.add(board[ROW][COL]); }
frame = new JFrame("Shuffle Game"); frame.setLocation(400, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setSize(HEIGHT, WIDTH);
panel.setLayout(new GridLayout(DIM, DIM)); panel.setBackground(Color.GRAY);
java.awt.Container content = frame.getContentPane(); content.add(panel, BorderLayout.CENTER); content.setBackground(Color.GRAY); frame.setVisible(true); }
@param @return private boolean isSolvable(ArrayList<Integer> list) { int inversionSum = 0; for (int i = 0; i < list.size(); i++) { if (list.get(i) == 0) { inversionSum += ((i / DIM) + 1); continue; }
int count = 0; for (int j = i + 1; j < list.size(); j++) { if (list.get(j) == 0) { continue; } else if (list.get(i) > list.get(j)) { count++; } } inversionSum += count; }
return ((inversionSum & 1) == 0) ? true : false; }
is
@param @throws <tt></tt> public void actionPerformed(ActionEvent event) throws IllegalArgumentException { JButton buttonPressed = (JButton) event.getSource(); int index = indexOf(buttonPressed.getText()); if (index == -1) { throw (new IllegalArgumentException("Index should be between 0-15")); } int row = index / DIM; int column = index % DIM;
makeMove(row, column);
if (isFinished()) { JOptionPane.showMessageDialog(null, "You Win The Game."); } }
@param @return private int indexOf(String cellNum) {
for (int ROW = 0; ROW < board.length; ROW++) { for (int COL = 0; COL < board[ROW].length; COL++) { if (board[ROW][COL].getText().equals(cellNum)) { return (getIndex(ROW, COL)); } } } return -1;
}
@return
private boolean makeMove(int row, int col) { final int emptyRow = emptyCell / DIM; final int emptyCol = emptyCell % DIM; int rowDiff = emptyRow - row; int colDiff = emptyCol - col; boolean isInRow = (row == emptyRow); boolean isInCol = (col == emptyCol); boolean isNotDiagonal = (isInRow || isInCol);
if (isNotDiagonal) { int diff = Math.abs(colDiff); if (colDiff < 0 & isInRow) { for (int i = 0; i < diff; i++) { board[emptyRow][emptyCol + i].setText( board[emptyRow][emptyCol + (i + 1)].getText()); }
} else if (colDiff > 0 & isInRow) { for (int i = 0; i < diff; i++) { board[emptyRow][emptyCol - i].setText( board[emptyRow][emptyCol - (i + 1)].getText()); } }
diff = Math.abs(rowDiff);
if (rowDiff < 0 & isInCol) { for (int i = 0; i < diff; i++) { board[emptyRow + i][emptyCol].setText( board[emptyRow + (i + 1)][emptyCol].getText()); }
} else if (rowDiff > 0 & isInCol) { for (int i = 0; i < diff; i++) { board[emptyRow - i][emptyCol].setText( board[emptyRow - (i + 1)][emptyCol].getText()); } }
board[emptyRow][emptyCol].setVisible(true); board[row][col].setText(Integer.toString(0)); board[row][col].setVisible(false); emptyCell = getIndex(row, col); }
return true; }
@return private boolean isFinished() { for (int index = WIN.length - 1; index >= 0; index--) { String number = board[index / DIM][index % DIM].getText(); if (!number.equals(WIN[index])) { return false;
} } return true; } }
|
Comments and suggestions will be great.
-Anil Madamala