Question

In: Computer Science

As piece flipping to the Java Reversi program. Python program provided as an example. pdf document...

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

Solutions

Expert Solution

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:


Related Solutions

Write a Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
JAVA Write a program that checks the spelling of words in a document. This program uses...
JAVA Write a program that checks the spelling of words in a document. This program uses two text files: A dictionary file containing all known correctly spelled words, and A document to be spell-checked against the dictionary. As the document to be spell checked is read, each of its words is checked against the dictionary words. The program determines whether each word is misspelled. Misspelled words are recorded. is spelled correctly. Correctly spelled words are recorded and their frequency counted....
In Java Please!!! 6.22 LAB: Python and sqlite basics Write a Python program that connects to...
In Java Please!!! 6.22 LAB: Python and sqlite basics Write a Python program that connects to a sqlite database. Create a table called Horses with the following fields: id (integer): a primary key and not null name (text) breed (text) height (real) birthday (text) Next, insert the following data row into the Horses table: id: 1 name: 'Babe' breed: 'Quarter Horse' height: 15.3 birthday: '2015-02-10' Output all records from the Horses table. Ex: With the above row inserted, the output...
This document describes a computer program you must write in Python and submit to Gradescope. For...
This document describes a computer program you must write in Python and submit to Gradescope. For this project, the program will simulate the operation of a vending machine that sells snacks, accepting coins and dispensing products and change. In an actual vending machine, a program running on an embedded computer accepts input from buttons (e.g. a numeric keypad) and devices such as coin acceptors, and its output consists of signals that control a display, actuate motors to dispense products, etc.....
make a C / C++, Java, or Python program with two processes, a producer and a...
make a C / C++, Java, or Python program with two processes, a producer and a consumer. The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into...
Java: create a program that reads in a piece of DNA sequence from a sequence file...
Java: create a program that reads in a piece of DNA sequence from a sequence file (dna.seq) (alternatively you can use the getRandomSeq(long) method of the RandomSeq class to generate a piece of DNA sequence), and then print out all the codons in three forward reading frames. Design a method called codon() that can be used to find all the codons from three reading frames. The method will take in an argument, the reading frame (1, 2, or 3), and...
Write a java simple document retrieval program that first asks the user to enter a single...
Write a java simple document retrieval program that first asks the user to enter a single term query, then goes through two docuements named doc1.txt and doc2.txt (provided with assignment7) to find which document is more relevant by calculating the frequency of the query term in each document. Output the conclusion to a file named asmnt7output.txt in the following format (as an example for query “java” and “problem”). The percentages in parenthese are respective supporting frequencies. java: doc1(6.37%) is more...
Give an example of a program in java that creates a GUI with at least one...
Give an example of a program in java that creates a GUI with at least one button and several textfields. Some of the textfields should be for input and others for output. Make the output textfields uneditable. When the button is clicked, the input fields should be read, some calculation performed and the result displayed in the output textfield(s).
Java Programming: Can I get an example of a program that will allow 4 options? Example...
Java Programming: Can I get an example of a program that will allow 4 options? Example Output: Choose on of the options below: 1. Display all items in CSV file. (CSV file includes for example brand of phone, size, price) 2. Pick an item by linear searching 3. Pick an item by bineary searching 4. Quit Program
Java Write a program that will only accept input from a file provided as the first...
Java Write a program that will only accept input from a file provided as the first command line argument. If no file is given or the file cannot be opened, simply print “Error opening file!” and stop executing. A valid input file should contain two lines. The first line is any valid string, and the second line should contain a single integer. The program should then print the single character from the string provided as the first line of input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT