In: Computer Science
As piece flipping to the Java Reversi program.
Python program provided as an example.
pdf document describes one possible approach in Java.
I need a solution, these are provided,
Reversi.java
Reversi_030_v3.py
flip_list.pdf
emptySquare80.gif
blackPiece80.gif
whitePiece80.gif
package GUI;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// Defines a class Reversi
public class Reversi
{
// Defines main method
public static void main(String args[])
{
JFrame app = new JFrame("Reversi");
app.setContentPane(new GameBoard());
// handle window closing
//----------------------
app.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
app.addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);// end addWindowListener
app.pack(); //need this to set application size
app.setVisible(true); // do this after setting look and feel
}
}// end class Reversi
//==========================================================================
// The JPanel that displays the Game Board
//
class GameBoard extends JPanel
{
private final int boardSize = 8; // number of squares on a side
private int squareSize; // square size in pixels
private int displaySize; // game size in pixels
private int board[][]; // game board contents
private final int EMPTY = 0; // board value for an empty square
private final int BLACK = -1; // board value for a black piece
private final int WHITE = 1; // board value for a white piece
private int turn; // turn == WHITE or BLACK
Image whitePiece; // references to the images
Image blackPiece;
Image emptySquare;
//-------------------------------------------------------
// Constructor
//
GameBoard()
{
// allocate the game board array
//------------------------------
board = new int[boardSize][boardSize];
// set all board squares to empty
//-------------------------------
for(int row = 0; row < boardSize; row++)
for(int col = 0; col < boardSize; col++)
board[row][col] = EMPTY;
// setup the initial position
//---------------------------
board[3][3] = board[4][4] = WHITE;
board[3][4] = board[4][3] = BLACK;
turn = BLACK;
// load the images
//-------------------------------------
Toolkit t = Toolkit.getDefaultToolkit();
MediaTracker mt = new MediaTracker(this);
mt.addImage(whitePiece = t.getImage("whitePiece80.jpg"), 0);
mt.addImage(blackPiece = t.getImage("blackPiece80.jpg"), 0);
mt.addImage(emptySquare = t.getImage("emptySquare80.png"), 0);
try
{
mt.waitForAll();
}
catch (InterruptedException e) {};
// calculate the game size
//------------------------
squareSize = emptySquare.getWidth(null);
displaySize = boardSize*squareSize;
setPreferredSize(new Dimension(displaySize, displaySize));
// Handle mouse clicks.
//-----------------------------------
addMouseListener(new MouseListener()
{
public void mousePressed(MouseEvent e)
{
int row = e.getY()/squareSize; // find what square the mouse was clicked on
int col = e.getX()/squareSize;
if(board[row][col] != EMPTY)
return; // don't allow a piece to go on top of another
//================================================
// Put code here to compute flip list
// Return if flip list is empty (not a legal move)
//================================================
board[row][col] = turn; // place a piece on the selected square
paintImmediately(0, 0, displaySize, displaySize); // draw the board before flipping
try
{
Thread.sleep(500);
}
catch (InterruptedException ex) { ; } // wait a moment
//================================================
// Put piece flipping method call here.
// Code below draws some pieces to illustrate how
// to do piece flipping.
//================================================
for (int c = col + 1; c < 8 && board[row][c] == EMPTY; c++)
{
board[row][c] = -turn;
turn = -turn; // flip turn
paintImmediately(0, 0, displaySize, displaySize);
try
{
Thread.sleep(100);
}
catch (InterruptedException ex) { ; }
}
turn = -turn; // flip turn
}
// Provide null implementations for remaining
// required interface methods.
//-------------------------------------------
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
});
setSize(displaySize, displaySize);
setVisible(true);
}// end constructor
//-------------------------------------
// Draw one square of the GameBoard
//
private void drawSquare(Graphics g, int row, int col)
{
int x = col*squareSize;
int y = row*squareSize;
if(board[row][col] == WHITE)
g.drawImage(whitePiece, x, y, null);
else
if(board[row][col] == BLACK)
g.drawImage(blackPiece, x, y, null);
else
g.drawImage(emptySquare, x, y, null);
}
//-------------------------------------
// Draw the GameBoard
//
public void paintComponent(Graphics g)
{
for(int row = 0; row < boardSize; row++)
for(int col = 0; col < boardSize; col++)
drawSquare(g, row, col);
}
} // end class GameBoard
Sample Output: