(please help) (In java) (This should not work like the actual reversi game_ This
ID: 3835457 • Letter: #
Question
(please help) (In java) (This should not work like the actual reversi game_ This is just the look of it_This is a beginner program)(Read below...)
this game should work like this:
-First, you choose your First Move (you can choose if you start with Black or White)_For choosing, you need to select your "First Move"(it is right there on the right side panel)
-Let's say, you choose white! then, you can flip/click on any disks(there is no limit of how many times you flip a disk) and turn it to white.
-When clicking on each disk to turn it to "white", the game(the right panel) should keep track of how many times you clicked on the disks. For example, let's say you fliped/clicked-on 12 disks/Buttons, so at the top, under "White:" it should say 12. But make sure initially you have 2 for each color(black and white) because we start with 2 Black and 2 White.
-To change the color(or to change turn) you just need to click on "Done Moving" Button! For example, you start with white and you flip disks, then you click on "Done Moving" button to start fliping Black disks. remeber that you still need to keep tract of how many Balck disks you turned!
-Also, the game should start over if the button "New Game" is clicked!
******(Please use the provided templates) (Also, you don't need to make the round circle, because that is provided in the template)
(This is just a simple game, and does not work like the actual Reversi game)
The templates: (put codes in these...)(there are 4 different templates)(1 is already done!)
Revers Turn: White White Black: Done Moving First Move: White Black New GameExplanation / Answer
The Source code for the given game is given below :
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Reversigame extends JPanel
{
final static int BLACK = 1; // Declare the state of each square
final static int WHITE = 2;
final static int EMPTY = 0;
final static int OFFBOARD = -1;
Black black = new Black();
White white = new White();
private Game game = new Game();
private javax.swing.Timer timer;
private static int delay;
private static long startTime, stopTime, runTime = 0;
private int turn = BLACK;
private boolean black_done = false;
private boolean white_done = false;
/**
* This constructor sets up the initial game configuration,
* and starts the timer with a default delay of 1 second.
*/
public Reversigame() { this(1000); }
/**
* This constructor sets up the initial game configuration,
* and starts the timer with a user specified delay.
*/
public Reversigame(int delay) {
// Initialize the game state
initGame(game);
// Run the game with GUI - computer vs. computer using a timer
if (delay > 0) {
setBackground(Color.GREEN);
timer = new javax.swing.Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
playerMove();
repaint();
}
});
// Create the Start and Stop buttons
JButton start = new JButton("Start");
start.setBounds(10,20,80,25);
add(start);
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
timer.start();
}
});
JButton stop = new JButton("Stop");
stop.setBounds(10,80,80,25);
add(stop);
stop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
timer.stop();
}
});
}
// Run the game with GUI - human vs. computer.
// The human player always plays with the black discs.
if (delay == 0) {
setBackground(Color.GREEN);
addMouseListener( new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
// Find out which square was clicked
int x = evt.getX();
int y = evt.getY();
int screenWidth = getWidth();
int screenHeight = getHeight();
int column = (x*(game.WIDTH-2))/screenWidth+1;
int row = (y*(game.HEIGHT-2))/screenHeight+1;
if (!game.legalMove(row,column,BLACK,true))
System.out.println("Not a legal move - try again!");
else {
game.board[row][column] = BLACK;
repaint();
black_done = true;
for (int i=1; i<game.HEIGHT-1; i++)
for (int j=1; j<game.WIDTH-1; j++)
if (game.legalMove(i,j,BLACK,false) )
black_done=false;
whiteMove();
}
}
});
}
// Run the game without the GUI - as many times as specified in delay.
if (delay < 0) {
// Start timing how long it takes to play "delay" games
startTime = new Date().getTime();
// Keep track of how many wins each color has
int white_won = 0;
int black_won = 0;
int ties = 0;
// Play a bunch of games!
for (int times=0; times < -delay; times++) {
initGame(game);
boolean done = false;
white_done = false;
black_done = false;
while (!done) {
playerMove();
int bC = 0;
int wC = 0;
for (int i=1; i<game.HEIGHT-1; i++) {
for (int j=1; j<game.WIDTH-1; j++) {
if (game.board[i][j] == BLACK)
bC++;
else if (game.board[i][j] == WHITE)
wC++;
}
}
// Check if there are any more moves to make
done = true;
for (int i=1; i<game.HEIGHT-1; i++)
for (int j=1; j<game.WIDTH-1; j++)
if ((game.legalMove(i,j,BLACK,false)) ||
(game.legalMove(i,j,WHITE,false)))
done=false;
if (done)
if (wC > bC) {
//System.out.println("White won with " + wC);
white_won++;
}
else if (bC > wC) {
//System.out.println("Black won with "+ bC);
black_won++;
}
else {
//System.out.println("Tied game");
ties++;
}
}
}
stopTime = new Date().getTime();
runTime = (stopTime - startTime);
System.out.println("===========================");
System.out.println("Total no. of games = " + -delay);
System.out.println("Whites won " + white_won + " times");
System.out.println("Blacks won " + black_won + " times");
System.out.println("Number of tied games is = " + ties);
System.out.println(" White lost the game "+(-delay-white_won) + " times");
System.out.print("Runtime for " + -delay + " games = ");
System.out.println(runTime + " milliseconds");
System.out.println("===========================");
}
}
/**
* Initializing the game state
*
*/
public void initGame(Game game) {
turn = BLACK;
//System.out.println("Turn is: " + turn);
// Initialize off-board squares
for (int i=0; i<game.WIDTH; i++) {
game.board[i][0] = OFFBOARD;
game.board[i][game.WIDTH-1] = OFFBOARD;
game.board[0][i] = OFFBOARD;
game.board[game.HEIGHT-1][i] = OFFBOARD;
}
// Initialize game board to be empty except for initial setup
for (int i=1; i<game.HEIGHT-1; i++)
for (int j=1; j<game.WIDTH-1; j++)
game.board[i][j] = EMPTY;
game.board[game.HEIGHT/2-1][game.WIDTH/2-1] = WHITE;
game.board[game.HEIGHT/2][game.WIDTH/2-1] = BLACK;
game.board[game.HEIGHT/2-1][game.WIDTH/2] = BLACK;
game.board[game.HEIGHT/2][game.WIDTH/2] = WHITE;
}
public void playerMove() {
if (turn == BLACK) {
blackMove();
turn = WHITE;
}
else {
whiteMove();
turn = BLACK;
}
}
/**
* Black takes the turn.
*/
public void blackMove() {
// Check if Black can move anywhere
black_done = true;
for (int i=1; i<game.HEIGHT-1; i++)
for (int j=1; j<game.WIDTH-1; j++)
if (game.legalMove(i,j,BLACK,false) )
black_done=false;
game = black.strategy(game, black_done, BLACK);
}
/**
* White takes a turn.
*/
public void whiteMove() {
// Check if White can move
white_done = true;
for (int i=1; i<game.HEIGHT-1; i++)
for (int j=1; j<game.WIDTH-1; j++)
if (game.legalMove(i,j,WHITE,false) )
white_done=false;
game = white.strategy(game, white_done, WHITE);
}
/**
* Draw the board and the current state of the game using graphics.
*/
public void paintComponent(Graphics g) {
super.paintComponent(g); // Fill panel with background color
int width = getWidth();
int height = getHeight();
int xoff = width/(game.WIDTH-2);
int yoff = height/(game.HEIGHT-2);
int bCount=0;
int wCount=0;
// Draw the lines on the board
g.setColor(Color.BLACK);
for (int i=1; i<=game.HEIGHT-2; i++) {
g.drawLine(i*xoff, 0, i*xoff, height);
g.drawLine(0, i*yoff, width, i*yoff);
}
// Draw discs on the board and show the legal moves
for (int i=1; i<game.HEIGHT-1; i++) {
for (int j=1; j<game.WIDTH-1; j++) {
// Draw the discs
if (game.board[i][j] == BLACK) {
g.setColor(Color.BLACK);
g.fillOval((j*yoff)-yoff+7,(i*xoff)-xoff+7,50,50);
bCount++;
}
else if (game.board[i][j] == WHITE) {
g.setColor(Color.WHITE);
g.fillOval((j*yoff)-yoff+7,(i*xoff)-xoff+7,50,50);
wCount++;
}
// Show the legal moves for the current player
if (turn == BLACK && game.legalMove(i,j,BLACK,false)) {
g.setColor(Color.BLACK);
g.fillOval((j*yoff+29)-yoff,(i*xoff+29)-xoff,6,6);
}
// If other player cannot move, current player cleans up
if (turn == WHITE && game.legalMove(i,j,WHITE,false)) {
g.setColor(Color.WHITE);
g.fillOval((j*yoff+29)-yoff,(i*xoff+29)-xoff,6,6);
}
}
}
// Check if there are any more moves to make in the game
boolean done = true;
for (int i=1; i<game.HEIGHT-1; i++)
for (int j=1; j<game.WIDTH-1; j++)
if ((game.legalMove(i,j,BLACK,false)) ||
(game.legalMove(i,j,WHITE,false)))
done=false;
g.setColor(Color.RED);
if (done) {
if (wCount > bCount)
g.drawString("White won with " + wCount + " discs.",10,20);
else if (bCount > wCount)
g.drawString("Black won with " + bCount + " discs.",10,20);
else g.drawString("Tied game",10,20);
}
else {
if (wCount > bCount)
g.drawString("White is winning with " + wCount+" discs",10,20);
else if (bCount > wCount)
g.drawString("Black is winning with " + bCount+" discs",10,20);
else g.drawString("Currently tied",10,20);
}
}
/**
* The main programis given below.
*/
public static void main(String [] args) {
Reversigame content;
if (args.length > 1) {
System.out.println("Usage: java Reversigame delayTime");
System.exit(0);
}
if (args.length == 1) {
try {
delay = Integer.parseInt(args[0]);
}
catch (NumberFormatException e) {
System.out.println("Command line arg must be an integer");
System.exit(0);
}
content = new Reversigame(delay);
if (delay >= 0) {
JFrame window = new JFrame("Reversigame Game");
window.setContentPane(content);
window.setSize(530,557);
window.setLocation(100,100);
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
}
}
else {
content = new Reversigame();
JFrame window = new JFrame("Reversigame Game");
window.setContentPane(content);
window.setSize(530,557);
window.setLocation(100,100);
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.