Created
October 12, 2017 01:26
-
-
Save chasefloyd/cf86afc00b7f1d3f488d1d02958f71c5 to your computer and use it in GitHub Desktop.
The Othello game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Chase Floyd | |
* | |
* This class should represent the entire set of 64 tiles. | |
* This class is used to store the current state of the game | |
* | |
* This class should include 2 instance variables | |
* 2-D array of tile objects | |
* The number of rows and columns on the board (write so that it could change) | |
* | |
* This class should include at least these methods | |
* A method that counts the pieces of a specific color on the board | |
* a method that places a specific color at a specific location & flipping any pieces to the opposing color it needs | |
* a method that determines whether a valid move exists for a specific color | |
* A method that determines the current state of the game( still in progress, victory to black, vistory to white) | |
* A toString method that returns how the board should be displayed | |
* | |
*/ | |
public class gameBoard { | |
int row = 8; | |
int column = 8; | |
protected Tile[][] tile; | |
protected String finalBoard = " "; | |
protected int turn; | |
protected boolean gameOver = false; | |
protected String player; | |
public gameBoard(String player, Tile tile[][]) { | |
this.tile = tile; | |
this.player = player; | |
} | |
protected boolean validMove(String player, int r, int col) { | |
if (tile[r][col].getTileState() == " ") { | |
//Check right | |
for (int i = 1; i < column - col; i++) { | |
if (tile[r][col + i].getTileState().equals(player) && i > 1) { | |
return true; | |
} else if (tile[r][col + i].getTileState().equals(" ")) ; | |
break; | |
} | |
//check left | |
for (int i = 1; i <= col; i++) { | |
if (tile[r][col - i].getTileState().equals(player) && i > 1) { | |
return true; | |
} else if (tile[r][col - i].getTileState().equals(" ")) { | |
break; | |
} | |
} | |
//check up | |
for (int i = 1; i < row - r; i++) { | |
if (tile[r + i][col].getTileState().equals(player) && i > 1) { | |
return true; | |
} else if (tile[r + i][col].getTileState().equals(" ")) { | |
break; | |
} | |
} | |
//check down | |
for (int i = 1; i <= r; i++) { | |
if (tile[r - i][col].getTileState().equals(player) && i > 1) { | |
return true; | |
} else if (tile[r - i][col].getTileState().equals(" ")) { | |
break; | |
} | |
} | |
//check up and right diagonal | |
int min = row - r; | |
if(column - col < min){ | |
min = column - col; | |
} | |
for (int i = 1; i < min; i++) { | |
if (tile[r + i][col + i].getTileState().equals(player) && i > 1) { | |
return true; | |
} else if (tile[r + i][col + i].getTileState().equals(" ")) { | |
break; | |
} | |
} | |
//check up and left | |
min = row - r; | |
if(col < min) | |
min = col; | |
for (int i = 1; i < min; i++) { | |
if (tile[r + i][col - 1].getTileState().equals(player) && i > 1) { | |
return true; | |
} else if (tile[r + i][col - i].getTileState().equals(" ")) { | |
break; | |
} | |
} | |
//check down and right | |
min = column - col; | |
if(r < min) | |
min = r; | |
for (int i = 1; i < min; i++) { | |
if (tile[r - i][col + 1].getTileState().equals(player) && i > 1) { | |
return true; | |
} else if (tile[r - i][col + i].getTileState().equals(" ")) { | |
break; | |
} | |
} | |
//check down and left | |
min = r; | |
if(col < min) | |
min = col; | |
for (int i = 1; i < min; i++) { | |
if (tile[r - i][col - 1].getTileState().equals(player) && i > 1) { | |
return true; | |
} else if (tile[r - i][col - i].getTileState().equals(" ")) { | |
break; | |
} | |
} | |
} | |
return false; | |
} | |
protected void placeMove(String player, int r, int col) { | |
if (tile[r][col].getTileState() == " ") { | |
tile[r][col].setTileState(player); | |
//Check right | |
for (int i = 1; i < column - col; i++) { | |
if (tile[r][col + i].getTileState().equals(player) && i > 1) { | |
for (int j = 1; j < i; j++) { | |
tile[r][col + j].flipTile(); | |
} | |
} else if (tile[r][col + i].getTileState().equals(" ")) ; | |
break; | |
} | |
//check left | |
for (int i = 1; i <= col; i++) { | |
if (tile[r][col - i].getTileState().equals(player) && i > 1) { | |
for (int j = 1; j < i; j++) { | |
tile[r][col - j].flipTile(); | |
} | |
} else if (tile[r][col - i].getTileState().equals(" ")) {//changed [col + 1] to [col - 1] | |
break; | |
} | |
} | |
//check up | |
for (int i = 1; i < row - r; i++) { | |
if (tile[r + i][col].getTileState().equals(player) && i > 1) { | |
for (int j = 1; j < i; j++) { | |
tile[r + j][col].flipTile(); | |
} | |
} else if (tile[r + i][col].getTileState().equals(" ")) { | |
break; | |
} | |
} | |
//check down | |
for (int i = 1; i <= r; i++) { | |
if (tile[r - i][col].getTileState().equals(player) && i > 1) { | |
for (int j = 1; j < i; j++) { | |
tile[r - j][col].flipTile(); | |
} | |
} else if (tile[r - i][col].getTileState().equals(" ")) { | |
break; | |
} | |
} | |
//check up and right diagonal | |
int min = row - r; | |
if (column - col < min) { | |
min = column - col; | |
for (int i = 1; i < min; i++) { | |
if (tile[r + i][col + i].getTileState().equals(player) && i > 1) { | |
for (int j = 1; j < i; j++) { | |
tile[r + j][col + j].flipTile(); | |
} | |
} else if (tile[r + i][col + i].getTileState().equals(" ")) { | |
break; | |
} | |
} | |
//check up and left | |
min = row - r; | |
if (col < min) | |
min = col; | |
for (int i = 1; i < min; i++) { | |
if (tile[r + i][col - 1].getTileState().equals(player) && i > 1) { | |
for (int j = 1; j < i; j++) { | |
tile[r + j][col - j].flipTile(); | |
} | |
} else if (tile[r + i][col - i].getTileState().equals(" ")) { | |
break; | |
} | |
} | |
//check down and right | |
min = column - col; | |
if (r < min) | |
min = r; | |
for (int i = 1; i < min; i++) { | |
if (tile[r - i][col + 1].getTileState().equals(player) && i > 1) { | |
for (int j = 1; j < i; j++) { | |
tile[r - j][col + j].flipTile(); | |
} | |
} else if (tile[r - i][col + i].getTileState().equals(" ")) { | |
break; | |
} | |
} | |
//check down and left | |
min = r; | |
if (col < min) | |
min = col; | |
for (int i = 1; i < min; i++) { | |
if (tile[r - i][col - 1].getTileState().equals(player) && i > 1) { | |
for (int j = 1; j < i; j++) { | |
tile[r - j][col - j].flipTile(); | |
} | |
} else if (tile[r - i][col - i].getTileState().equals(" ")) { | |
break; | |
} | |
} | |
} | |
//return verdict; | |
} | |
} | |
protected void displayCount(int turn){ | |
//Counter for the black pieces on the board | |
int blackCount = 0; | |
//Counter for the white pieces on the board | |
int whiteCount = 0; | |
//Counter for the total pieces on the board | |
int totalCount = blackCount + whiteCount; | |
boolean fullBoard = false; | |
for(int i = 0; i < 8; i++){ | |
for(int j = 0; j < 8; j++){ | |
if (tile[i][j].getTileState() == "b"){ | |
blackCount++; | |
} else if(tile[i][j].getTileState() == "w"){ | |
whiteCount++; | |
} | |
} | |
} | |
//Display the number of pieces each color has on the board | |
System.out.println("Black has " + blackCount + " pieces, and white has" + whiteCount + " pieces"); | |
if(totalCount == 64) { | |
displayGameStatus(totalCount, blackCount, whiteCount); | |
} | |
} | |
protected void displayGameStatus(int totalCount, int blackCount, int whiteCount){ | |
if(totalCount == 64){ | |
boolean gameOver = true; | |
System.out.println("Game over"); | |
if(blackCount > whiteCount){ | |
System.out.println("Black wins");} | |
else if(blackCount < whiteCount){ | |
System.out.println("White wins"); } | |
else{ | |
System.out.println("Its a draw"); | |
} | |
} | |
} | |
public String toString(){ | |
for(int i = 0; i < row; i++){ | |
if(i == 0){ | |
finalBoard += " |"; | |
for(int z = 0; z < column; z++){ | |
finalBoard += " " + z + " |"; | |
} | |
finalBoard += "\n"; | |
for(int z = 0; z < column + 1; z++){ | |
finalBoard += "---+"; | |
} | |
finalBoard += "\n"; | |
} | |
else{ | |
finalBoard += "\n"; | |
} | |
for(int k = 0; k < column; k++){ | |
if(k == 0){ | |
finalBoard += " " + i + " |"; | |
} | |
finalBoard += " " + tile[i][k].toString() + " |"; | |
} | |
finalBoard += "\n"; | |
for(int z = 0; z < column + 1; z++){ | |
finalBoard += "---+"; | |
} | |
} | |
return finalBoard; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Chase Floyd | |
* | |
* This class serves as the main client program for the game | |
* | |
* This class should include at least these instance varibales | |
* a GameBoard object to store the state of the game | |
* a Scanner object to read user input | |
* | |
* This class should contain the main method that is executed to start the game | |
*/ | |
import java.util.Scanner; | |
public class Othello { | |
public static void main(String[] args) { | |
String player = "b"; | |
int turn = 0; | |
boolean gameOver = false; | |
//Create a scanner for user input | |
Scanner s = new Scanner(System.in); | |
//Create an array of tiles and put the tile array into the Gameboard | |
Tile[][] tile = new Tile[8][8]; | |
for (int i = 0; i < tile.length; i++) { | |
for (int j = 0; j < tile[i].length; j++) { | |
tile[i][j] = new Tile(); | |
} | |
} | |
tile[3][3].setTileState("w"); | |
tile[4][4].setTileState("w"); | |
tile[3][4].setTileState("b"); | |
tile[4][3].setTileState("b"); | |
gameBoard board = new gameBoard(player, tile); | |
System.out.println(board); | |
while (!gameOver) { | |
System.out.println("Enter a row and a column for your move:"); | |
int row = s.nextInt(); | |
int column = s.nextInt(); | |
if (turn == 0) { | |
player = "b"; | |
if (board.validMove(player, row, column) == true) { | |
board.placeMove(player, row, column); | |
//System.out.println() | |
} else { | |
System.out.println("The is not a valid move, try again: "); | |
} | |
turn++; | |
} else { | |
player = "w"; | |
if (board.validMove(player, row, column) == true) { | |
board.placeMove(player, row, column); | |
//System.out.println(board); | |
} else { | |
System.out.println("The is not a valid move, try again: "); | |
} | |
turn--; | |
} | |
System.out.println(board); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Chase Floyd | |
* | |
* This class is suppose to represent a single tile on the Othello board. | |
* | |
* This class should include an instance variable that gives the state of the tile | |
* The tile can either be EMPTY, BLACK, or WHITE | |
* | |
* This class should contain methods that - FLIP THE TILE, and a toString method | |
* that returns how the tile should be displayed | |
* | |
*/ | |
public class Tile | |
{ | |
protected String tileState = " "; | |
public String getTileState(){ | |
return tileState; | |
} | |
public void setTileState(String tileState){ | |
this.tileState = tileState; | |
} | |
public void flipTile(){ | |
if (tileState == "b") | |
tileState = "w"; | |
else | |
tileState = "b"; | |
} | |
public String toString(){ | |
return tileState; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment