Need to make a Reversi/Othello Board game in JAVA (please read entire post i\'ve
ID: 3778348 • Letter: N
Question
Need to make a Reversi/Othello Board game in JAVA (please read entire post i've already wasted three questions on this topic- THIS IS A BASIC JAVA PROGRAM NO APPLETS, NO THREADS, NO GUI, JUST SIMPLE CONSOLE COMMANDS, WHICH WILL UPDATE THE BOARD VIA SYSTEM.OUT)
Don't post code beginning like this because it's not what i need, and it would obviously be wrong.
import java.awt.*;
import java.applet.*;
public class Othello_game extends Applet implements Runnable
{
Thread runner;
boolean black_shown=false;
boolean show_em=false;
final int BLACK = 1;
final int WHITE = 2;
final int EMPTY = 0;
final int OFFBOARD = -1;
final static int Game[][] = new int[10][10];
protected int black_count = 0;
protected int white_count = 0;
Event evt;
int x,y;
public void start()
The board size must be N x N (so 4x4 is default, and it needs to be CHANGEABLE to stuff like 5x5, 6x6 etc while still working. NEEDS TO BE ABLE TO WORK WITH ODD SIZE)
Player 1 is going to be you and your going to be the black tiles, and the AI is going to be the white tiles. Black tiles will start the game, and then white will go after them, then back to black, repeat. The players must put a disc of their color on an empty square, adjacent to their opponent's disc. Any of the oppponents tiles in between the tile put down, and another of your tiles will cause them to flip to your color. The winner is decided when the board is completely filled or no viable moves are left, and whoever has the most pieces.
This program is meant to be SIMPLE DESIGN (no GUI or big interface designs- everything should be done in the bottom console through repeating inputs from the user), broken into multiple classes (can be extension's off the class or abstract interface off each other), and the user will be entering the coordinates of where to put the pieces (so just using scanner method)?. The start of the game must always have 4 tiles put down in the center (so the center can change depending on the size of the board), and the gameplay would look something like this
Scenario in which the game ends early.
Please post JAVA code that is simple as this is a beginner homework, this is not meant to be an application. Do not just copy down the code I said earlier is wrong and spit it back out at me as it's already been done twice. Please help thanks.
BW Success: Black move at C1, 0) BW Score: Black: 4, White: 1 Success: White move at C2, 0DExplanation / Answer
import java.awt.*;
import java.applet.*;
public class Othello_game extends Applet implements Runnable
{
Thread runner;
boolean black_shown=false;
boolean show_em=false;
final int BLACK = 1;
final int WHITE = 2;
final int EMPTY = 0;
final int OFFBOARD = -1;
final static int Game[][] = new int[10][10];
protected int black_count = 0;
protected int white_count = 0;
Event evt;
int x,y;
public void start()
{
if (runner == null)
{
black_shown=false;
runner = new Thread(this);
runner.start();
}
}
public void stop()
{
if (runner != null)
{
runner.stop();
runner = null;
black_shown=false;
}
}
public synchronized void run()
{
setBackground(Color.green);
for (int i=0; i<10; i++)
{
Game[i][0] = OFFBOARD;
Game[i][9] = OFFBOARD;
Game[0][i] = OFFBOARD;
Game[9][i] = OFFBOARD;
}
for (int i=1; i<9; i++)
for (int j=1; j<9; j++)
Game[i][j] = EMPTY;
Game[4][4] = WHITE;
Game[5][4] = BLACK;
Game[4][5] = BLACK;
Game[5][5] = WHITE;
while (runner!=null)
{
while (!black_shown)
{
try {
wait();
} catch (InterruptedException e){ }
black_shown=false;
showStatus("Good move!");
pause(1000);
whiteResponds();
}
}
}
public synchronized boolean mouseUp(Event evt, int x, int y)
{
Dimension d = size();
int c = (x*8)/d.width + 1;
int r = (y*8)/d.height + 1;
boolean black_done;
if (legalMove(r,c,BLACK,WHITE,true))
{
Game[r][c] = BLACK;
repaint();
black_shown=true;
notify();
}
else showStatus("Not a legal move");
black_done=true;
for (int i=1; i<9; i++)
for (int j=1; j<9; j++)
if (legalMove(i,j,BLACK,WHITE,false) )
black_done=false;
if (black_done)
for (int i=1; i<65; i++)
whiteResponds();
return true;
}
public void whiteResponds()
{
boolean found;
int i, j;
found=false;
if (legalMove(1,1,WHITE,BLACK,true))
{
Game[1][1]=WHITE;
found=true;
}
if ( (!found) && (legalMove(8,8,WHITE,BLACK,true)) )
{
Game[8][8]=WHITE;
found=true;
}
if ( (!found) && (legalMove(1,8,WHITE,BLACK,true)) )
{
Game[1][8]=WHITE;
found=true;
}
if ( (!found) && (legalMove(8,1,WHITE,BLACK,true)) )
{
Game[8][1]=WHITE;
found=true;
}
i=3;
while ((!found) && (i < 7))
{
j=3;
while ( (!found) && (j < 7))
{
if (legalMove(i,j,WHITE,BLACK,true))
{
Game[i][j]=WHITE;
found=true;
}
j++;
}
i++;
}
i=3;
while ((!found) && (i < 7))
{
if (legalMove(1,i,WHITE,BLACK,true))
{
Game[1][i]=WHITE;
found=true;
}
if ( (!found) && (legalMove(8,i,WHITE,BLACK,true)))
{
Game[8][i]=WHITE;
found=true;
}
if ( (!found) && (legalMove(i,1,WHITE,BLACK,true)))
{
Game[i][1]=WHITE;
found=true;
}
if ( (!found) && (legalMove(i,8,WHITE,BLACK,true)))
{
Game[i][8]=WHITE;
found=true;
}
i++;
}
i=3;
while ((!found) && (i < 7))
{
if (legalMove(2,i,WHITE,BLACK,true))
{
Game[2][i]=WHITE;
found=true;
}
if ( (!found) && (legalMove(7,i,WHITE,BLACK,true)))
{
Game[7][i]=WHITE;
found=true;
}
if ( (!found) && (legalMove(i,2,WHITE,BLACK,true)))
{
Game[i][2]=WHITE;
found=true;
}
if ( (!found) && (legalMove(i,7,WHITE,BLACK,true)))
{
Game[i][7]=WHITE;
found=true;
}
i++;
}
i=1;
while ((!found) && (i < 9))
{
j=1;
while ((!found) && (j < 9))
{
if (legalMove(i,j,WHITE,BLACK,true))
{
found=true;
Game[i][j]=WHITE;
}
j++;
}
i++;
}
repaint();
}
public boolean legalMove(int r, int c, int color, int othercolor,
boolean flip)
{
int i,j;
boolean legal;
int stepcount;
legal = false;
if (Game[r][c] == EMPTY)
{
for (int xdir=-1; xdir < 2; xdir++)
for (int ydir=-1; ydir < 2; ydir++)
{
stepcount = 0;
do
{
stepcount++;
i = r + stepcount*xdir;
j = c + stepcount*ydir;
}
while ( (i > 0) && (i < 9) && (j > 0) && (j < 9) &&
(Game[i][j] == othercolor));
if (( i > 0) && (i < 9) && (j > 0) && (j < 9) &&
(stepcount > 1) &&
(Game[i][j] == color) )
{ legal = true;
if (flip)
for (int k = 1; k < stepcount; k++)
Game[r+xdir*k][c+ydir*k] = color;
}
}
}
if ( legal==true) return true;
else return false;
}
void pause(int time)
{
try { runner.sleep(time); }
catch (InterruptedException e) { }
}
public void paint(Graphics g)
{
Dimension d = size();
g.setColor(Color.black);
int xoff = d.width/8;
int yoff = d.height/8;
black_count=0;
white_count=0;
boolean done;
for (int i=1; i<=8; i++)
{
g.drawLine(i*xoff, 0, i*xoff, d.height);
g.drawLine(0, i*yoff, d.width, i*yoff);
}
for (int i=1; i<9; i++)
for (int j=1; j<9; j++)
{
if (Game[i][j] == BLACK)
{
g.fillOval((j*yoff+3)-yoff,(i*xoff+3)-xoff,33,33);
black_count++;
}
}
g.setColor(Color.white);
for (int i=1; i<9; i++)
for (int j=1; j<9; j++)
{
if (Game[i][j] == WHITE)
{
g.fillOval((j*yoff+3)-yoff,(i*xoff+3)-xoff,33,33);
white_count++;
}
}
g.setColor(Color.red);
done=true;
for (int i=1; i<9; i++)
for (int j=1; j<9; j++)
if ((legalMove(i,j,BLACK,WHITE,false)) ||
(legalMove(i,j,WHITE,BLACK,false)))
done=false;
if (done==true)
{
if (white_count>black_count)
g.drawString("White won with "+white_count+" discs.",10,20);
else if (black_count>white_count)
g.drawString("Black won with "+black_count+" discs.",10,20);
else g.drawString("Tied game",10,20);
}
else
{
if (white_count > black_count)
g.drawString("White is winning with "+white_count+" discs",10,20);
else if (black_count > white_count)
g.drawString("Black is winning with "+black_count+" discs",10,20);
else g.drawString("Currently tied",10,20);
}
if (show_em==true)
{
for (int i=1; i<9; i++)
for (int j=1; j<9; j++)
if (legalMove(i,j,BLACK,WHITE,false))
g.fillOval((j*yoff+15)-yoff,(i*xoff+15)-xoff,5,5);
}
}
public void Action()
{
if (show_em==false)
{
show_em=true;
repaint();
}
else
{
show_em=false;
repaint();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.