JAVA (please read entire post--- THIS IS A BASIC JAVA PROGRAM NO APPLETS, NO THR
ID: 3778647 • Letter: J
Question
JAVA (please read entire post--- THIS IS A BASIC JAVA PROGRAM NO APPLETS, NO THREADS, NO GUI, JUST SIMPLE CONSOLE COMMANDS, WHICH WILL UPDATE THE BOARD VIA SYSTEM.OUT)
.
IF YOUR ANSWER BEGINS LIKE THIS, THEN THE ANSWER IS GOING TO BE WRONG!
.
ONCE again that answer is wrong!
.
So a JAVA program for the game OTHELLO is needed, but it's very simple code, so once again please don't answer my question with anything retaining to applets, or anything that incorporates using an interface outside of console.
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.
Once again please do not answer with code containing applet, as that would be a straight thumbs down for incorrect answer. This is meant to be simple beginner code, not anything that runs outside the java ide/console. Thanks if you help me out after reading entire post!
package Reversi Othello GameReversiothello: import java.awt. import java applet. public class Reversi Othello GameReversiothello extends Applet implements able declare a thread for this GameReversiothello Thread runner, boolean black shown false ll flag to signal pause boolean show em false: final int BLACK 1 declare state of each square final int WHITE 2; final int EMPTY 0: final int OFFBOARD -1; final static int GameReversiothelloDO new int[10][10: M10x10 matrix of squaresExplanation / Answer
Here it the Java Console Othello Game. Sample output is also shown. Please don't forget to rate the answer if it helped . Thank you very much.
import java.util.Arrays;
import java.util.Scanner;
public class Othello {
private int size;
private char board[][];
private char player1='B', player2 ='W';
private int player1Count, player2Count;
public Othello(int size)
{
this.size = size;
if(this.size < 4)
this.size = 4;
board = new char[size][size];
initBoad();
}
private void initBoad()
{
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size ; j++)
{
board[i][j] = ' ';
}
}
//the center of the board
int center = size/2;
board[center - 1][center] = player1;
board[center][center - 1] = player1;
board[center - 1][center - 1] = player2;
board[center][center] = player2;
player1Count = 2;
player2Count = 2;
}
public boolean isLegalMove(int row, int col, char player)
{
//each pair show different directions of x,y changing
int directions[][] = { {0,1} , {1, 1} , {1,0}, {1, -1}, {0, -1}, {-1,-1}, {-1,0},{-1,1}};
if(board[row][col] != ' ') //if the slot is not empty
return false;
boolean foundOpposite = false;
int r,c;
for(int i = 0; i < directions.length; i++) //try in each of the 8 possible directions
{
r = row + directions[i][1];
c = col + directions[i][0];
foundOpposite = false;
while(true)
{
if(!(0 <= r && r < size && 0 <= c && c < size)) //out of board
break;
if(board[r][c] == ' ') //found a blank
break;
else if(board[r][c] == player) //found this player tile
{
if(foundOpposite) //was already opponent tile found
return true;
else
break; //oppoent was not found but we found again the same player tile
}
else //found opponent
{
foundOpposite = true;
}
r += directions[i][1];
c += directions[i][0];
}
}
return false;
}
//precondition .. it is a legal move. It tries to move in all directions and find the number of flips based on this player's move.
//also if flip is set to to true, it will flip the opponent's tiles otherwise it will simply count the number of flips.
private int flipCount(int row, int col, char player, boolean flip)
{
int flips = 0;
int directions[][] = { {0,1} , {1, 1} , {1,0}, {1, -1}, {0, -1}, {-1,-1}, {-1,0},{-1,1}};
int r,c;
boolean foundOpposite;
for(int i = 0; i < directions.length; i++)
{
r = row + directions[i][1];
c = col + directions[i][0];
foundOpposite = false;
while(true)
{
if(!(0 <= r && r < size && 0 <= c && c < size)) //out of board
break;
if(board[r][c] == ' ') //a blank
break;
else if(board[r][c] == player) //reached the player's tile
{
if(foundOpposite) //if an opponent tile was found in between
{
int er = r, ec = c ;
int sr, sc;
sr = row + directions[i][1];
sc = col + directions[i][0];
//count all the tiles that were seen, and if need to be flipped, flip them
while( sr!= er || sc != ec)
{
flips++;
if(flip)
board[sr][sc] = player;
sr += directions[i][1];
sc += directions[i][0];
}
}
break;
}
else
{
foundOpposite = true;
}
r += directions[i][1];
c += directions[i][0];
}
}
return flips;
}
//generate a move for the player specified, mainly used for computer i.e player2. When best is set to true, it give the best
//move for the player i.e. the one where there are maximum flips of opponent. When best is false, it just get the first possible legal
//move. When no legal move is possible, it returns an array { -1, -1} otherwise it returns a legal move with {row, col}.
public int[] getLegalMove(char player, boolean best)
{
int count, max = 0;
int r = -1, c = -1 ;
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
if(isLegalMove(i, j, player))
{
count = flipCount(i, j, player, false);
if(count > max)
{
r = i;
c = j;
max = count;
if(!best)
break;
}
}
}
}
int move[] = new int[2];
move[0] = r;
move[1] = c;
return move;
}
private void displayBoard()
{
char l[] = new char[size * 4 +1];
Arrays.fill(l, '-');
String line = new String(l);
System.out.print(" " + line);
for(int i = 0; i < size; i++)
{
System.out.print(" |");
for(int j = 0; j < size; j++)
{
System.out.print(" " + board[i][j] + " |");
}
System.out.print(" " + line);
}
System.out.print(" ");
}
private boolean isGameOver(char currentPlayer)
{
int move[] = getLegalMove(currentPlayer, false);
if(move[0] == -1 && move[1] == -1)
return true;
else
return false;
}
public boolean makeMove(int row, int col, char player)
{
int flips;
if(isLegalMove(row, col, player))
{
flips = flipCount(row, col, player, true); //flip the tiles and also get the count of how many were flipped
board[row][col] = player;
if(player == player1)
{
player1Count += flips + 1;//increase player tile count
player2Count -= flips;//reduce the opponent's tiles count
}
else
{
player2Count += flips + 1; //increase player tile count
player1Count -= flips; //reduce the opponent's tiles count
}
return true;
}
else
return false;
}
public char getWinner()
{
if(player1Count > player2Count)
return player1;
else if(player1Count == player2Count)
return ' ';
else
return player2;
}
public int getPlayerCount(char player)
{
if(player1 == player)
return player1Count;
else
return player2Count;
}
public void start()
{
Scanner keybd = new Scanner(System.in);
char player = player1;
int r, c;
displayBoard();
while(!isGameOver(player))
{
if(player == player1)
{
System.out.print(" Enter your move ("+ player +") : ");
r = keybd.nextInt();
c = keybd.nextInt();
}
else
{
int move[] = getLegalMove(player, true);
r = move[0];
c = move[1];
}
if(makeMove(r, c, player))
{
System.out.print(" Success: " + player + " move at (" + r + ", " + c + ")");
if(player == player1)
{
System.out.print(" Score: " + player1Count);
player = player2;
}
else
{
System.out.print(" Score: " + player2Count);
player = player1;
}
displayBoard();
}
else
{
System.out.println("Not a legal move for player " + player + " to ("+ r + ", " + c + ")");
}
}
keybd.close();
}
public void displayResults()
{
char winner = getWinner();
System.out.println("Player1 : " + player1 + " Score: " + player1Count);
System.out.println("Player2 : " + player2 + " Score: " + player2Count);
if(winner == ' ')
System.out.println("Its a Tie! ");
else
{
if(player1 == winner)
System.out.println("Player 1 wins!");
else
System.out.println("Player 2 wins!");
}
}
public static void main(String[] args) {
Othello game = new Othello(4);
game.start();
game.displayResults();
}
}
output
-----------------
| | | | |
-----------------
| | W | B | |
-----------------
| | B | W | |
-----------------
| | | | |
-----------------
Enter your move (B) : 2 3
Success: B move at (2, 3) Score: 4
-----------------
| | | | |
-----------------
| | W | B | |
-----------------
| | B | B | B |
-----------------
| | | | |
-----------------
Success: W move at (1, 3) Score: 3
-----------------
| | | | |
-----------------
| | W | W | W |
-----------------
| | B | B | B |
-----------------
| | | | |
-----------------
Enter your move (B) : 0 3
Success: B move at (0, 3) Score: 6
-----------------
| | | | B |
-----------------
| | W | B | B |
-----------------
| | B | B | B |
-----------------
| | | | |
-----------------
Success: W move at (3, 1) Score: 3
-----------------
| | | | B |
-----------------
| | W | B | B |
-----------------
| | W | B | B |
-----------------
| | W | | |
-----------------
Enter your move (B) : 1 0
Success: B move at (1, 0) Score: 7
-----------------
| | | | B |
-----------------
| B | B | B | B |
-----------------
| | W | B | B |
-----------------
| | W | | |
-----------------
Success: W move at (0, 1) Score: 4
-----------------
| | W | | B |
-----------------
| B | W | B | B |
-----------------
| | W | B | B |
-----------------
| | W | | |
-----------------
Enter your move (B) : 0 0
Success: B move at (0, 0) Score: 8
-----------------
| B | W | | B |
-----------------
| B | B | B | B |
-----------------
| | W | B | B |
-----------------
| | W | | |
-----------------
Player1 : B Score: 8
Player2 : W Score: 3
Player 1 wins!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.