Created
May 6, 2018 17:49
-
-
Save danthegoodman1/a8edb8b87210a1e6729a1de53bc64b9d to your computer and use it in GitHub Desktop.
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
package cisc181.egghunt; | |
// import cisc181.egghunt.GamePiece; | |
// import cisc181.egghunt.eggHuntGame; | |
// import cisc181.egghunt.eggHuntAction; | |
import java.util.*; | |
public class eggHuntTest{ | |
private String up = "up"; | |
private String down = "down"; | |
private String left = "left"; | |
private String right = "right"; | |
public void testGame(){ | |
eggHuntGame game = new eggHuntGame(); | |
System.out.println(game); | |
game.placeEgg(); | |
// lets pretend the egg was placed at (4,5) | |
int eggX = game.getEggX(); | |
int eggY = game.getEggY(); | |
eggHuntAction action1 = new eggHuntAction(down, eggX, eggY); | |
action1.isValid(); | |
action1.update(game); | |
System.out.println(game); | |
game.isEnd(action1.checkEnd()); | |
eggHuntAction action2 = new eggHuntAction(down, eggX, eggY); | |
action2.isValid(); | |
action2.update(game); | |
System.out.println(game); | |
game.isEnd(action2.checkEnd()); | |
eggHuntAction action3 = new eggHuntAction(right, eggX, eggY); | |
action3.isValid(); | |
action3.update(game); | |
System.out.println(game); | |
game.isEnd(action3.checkEnd()); | |
eggHuntAction action4 = new eggHuntAction(right, eggX, eggY); | |
action4.isValid(); | |
action4.update(game); | |
System.out.println(game); | |
game.isEnd(action4.checkEnd()); | |
eggHuntAction action5 = new eggHuntAction(down, eggX, eggY); | |
action5.isValid(); | |
action5.update(game); | |
System.out.println(game); | |
game.isEnd(action5.checkEnd()); | |
eggHuntAction action6 = new eggHuntAction(down, eggX, eggY); | |
action6.isValid(); | |
action6.update(game); | |
System.out.println(game); | |
game.isEnd(action6.checkEnd()); | |
eggHuntAction action7 = new eggHuntAction(right, eggX, eggY); | |
action7.isValid(); | |
action7.update(game); | |
System.out.println(game); | |
game.isEnd(action7.checkEnd()); | |
eggHuntAction action8 = new eggHuntAction(right, eggX, eggY); | |
action8.isValid(); | |
action8.update(game); | |
System.out.println(game); | |
game.isEnd(action8.checkEnd()); | |
// isEnd should return true, ending game, because we are one spot away from egg and it shows now, triggering the ending message | |
} | |
} | |
class eggHuntGame extends Game{ | |
private GameBoard huntBoard; | |
private GamePiece eggPiece = new GamePiece('O'); | |
private GamePiece playerPiece = new GamePiece('*'); | |
private int numSteps; | |
private int eggY; | |
private int eggX; | |
private int plyX; | |
private int plyY; | |
public eggHuntGame(){ | |
GamePiece E = new GamePiece(GamePiece.EMPTY); | |
GamePiece H = new GamePiece('#'); // Hidden | |
this.huntBoard = new GameBoard(new GamePiece[][]{ | |
// Create a 10x10 empty board, playerPiece starts at (0,0) | |
{playerPiece,H,H,H,H,H,H,H,H,H}, | |
{H,H,H,H,H,H,H,H,H,H}, | |
{H,H,H,H,H,H,H,H,H,H}, | |
{H,H,H,H,H,H,H,H,H,H}, | |
{H,H,H,H,H,H,H,H,H,H}, | |
{H,H,H,H,H,H,H,H,H,H}, | |
{H,H,H,H,H,H,H,H,H,H}, | |
{H,H,H,H,H,H,H,H,H,H}, | |
{H,H,H,H,H,H,H,H,H,H}, | |
{H,H,H,H,H,H,H,H,H,H} }); | |
} | |
public GameBoard getHuntBoard(){ | |
return this.huntBoard; | |
} | |
public int getEggX(){ | |
return this.eggX; | |
} | |
public int getEggY(){ | |
return this.eggY; | |
} | |
public boolean isEnd(boolean ending){ // insert the actions checkEnd as ending | |
if(ending == true){ | |
onEnd(); | |
return true; | |
} else { | |
return false; | |
} | |
} | |
@Override | |
public void onEnd(){ | |
System.out.println("Found the egg!"); | |
} | |
// set the egg somwhere on the board in a 2 piece radius from the start | |
public void placeEgg() { | |
Random generator = new Random(); | |
eggY = generator.nextInt(7) + 2; | |
eggX = generator.nextInt(7) + 2; | |
// huntBoard.setPiece(eggY,eggX,this.eggPiece); wait dont do this it will show the piece | |
} | |
public String toString(){ | |
return (this.getHuntBoard().toString()); | |
} | |
public int getScore(){ | |
return this.numSteps; | |
} | |
public int getSteps(){ | |
return this.numSteps; | |
} | |
} | |
class eggHuntAction implements Action<eggHuntGame>{ | |
private int posY; | |
private int posX; | |
private String pDir; | |
private GamePiece playerPiece = new GamePiece('*'); | |
private int eggX; | |
private int eggY; | |
public eggHuntAction(String direction, int eggX, int eggY) { | |
this.posY = 2; | |
this.posX = 2; | |
this.pDir = direction; | |
this.eggX = eggX; | |
this.eggY = eggY; | |
} | |
// why is this a problem | |
public boolean isValid(){ | |
if(this.pDir.equals("down") && this.posY > 8){ | |
return false; | |
} else if (this.pDir.equals("up") && this.posY < 1){ | |
return false; | |
} else if (this.pDir.equals("left") && this.posX < 1){ | |
return false; | |
} else if (this.pDir.equals("right") && this.posX > 8){ | |
return false; | |
} else { | |
return true; | |
} | |
} | |
public boolean checkEnd(){ | |
if((this.posX - this.eggX == 1 || this.posX - this.eggX == (-1)) && (this.posY - this.eggY == 1 || this.posY = this.eggY == -1)){ | |
return true; | |
} else { | |
return false; | |
} | |
} | |
@Override | |
// why is the coloring weird? I think that reason might be why its not working | |
public void update(eggHuntGame game){ | |
if(this.pDir.equals("down") && this.posY > 8){ | |
game.setPiece(this.posY, this.posX, this.playerPiece); | |
GamePiece E = new GamePiece(GamePiece.EMPTY); | |
game.setPiece((this.posY + 1), this.posX, E); | |
} else if (this.pDir.equals("up") && this.posY < 1){ | |
game.setPiece(this.posY, this.posX, this.playerPiece); | |
GamePiece E = new GamePiece(GamePiece.EMPTY); | |
game.setPiece((this.posY - 1), this.posX, E); | |
} else if (this.pDir.equals("left") && this.posX < 1){ | |
game.setPiece(this.posY, this.posX, this.playerPiece); | |
GamePiece E = new GamePiece(GamePiece.EMPTY); | |
game.setPiece(this.posY, (this.posX - 1), E); | |
} else if (this.pDir.equals("right") && this.posX > 8){ | |
game.setPiece(this.posY, this.posX, this.playerPiece); | |
GamePiece E = new GamePiece(GamePiece.EMPTY); | |
game.setPiece(this.posY, (this.posX + 1), E); | |
} else { | |
System.out.println("What just happened? You can't do that!"); | |
} | |
} | |
public String toString(){ | |
return "Player is at: (" + this.posX + ", " + this.posY + ")"; | |
} | |
} | |
class GamePiece { | |
public static final char EMPTY = 'E'; | |
private char symbol; | |
public GamePiece(char symbol) { | |
this.symbol = symbol; | |
} | |
public char getSymbol() { | |
return symbol; | |
} | |
public boolean isEmpty() { | |
return symbol == EMPTY; | |
} | |
public String toString() { | |
return Character.toString(symbol); | |
} | |
public boolean equals(GamePiece otherPiece) { | |
return this.symbol == otherPiece.getSymbol(); | |
} | |
} | |
class GameBoard { | |
private GamePiece[][] board; | |
public GameBoard( GamePiece[][] board) { | |
this.board = board; | |
} | |
public GamePiece[][] getBoard() { | |
return board; | |
} | |
public void setPiece(int row, int column, GamePiece piece) { | |
board[row][column] = piece; | |
} | |
public boolean hasEmptySpace() { | |
for (GamePiece[] row : board) { | |
for (GamePiece value : row) { | |
if (value.isEmpty()) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
public GamePiece getPiece(int row,int column){ | |
return board[row][column]; | |
} | |
// accepts a start row, a start column, direction to change row, direction to change column, symbol | |
private int numConsecutive(int row, int column, int dr, int dc, char symbol) { | |
int maxSequence = 0; | |
int currentSequence = 0; | |
while (isInBounds(row, column)) { | |
// we are looping down the sequence looking for chars == symbol | |
if (board[row][column].getSymbol() == symbol) { | |
currentSequence++; | |
} | |
else { | |
// the square does not belong to player | |
currentSequence = 0; | |
} | |
maxSequence = Math.max(maxSequence, currentSequence); | |
row += dr; | |
column += dc; | |
} | |
return maxSequence; | |
} | |
// find max number of symbols in a row | |
public int getMaxConsecutive(char symbol) { | |
int maxScore = 0; | |
for (int i = 0; i < 3; i++) { | |
// for each row (left to right) | |
// start at row 0, column 0 , don't change row, change column +1 | |
maxScore = Math.max(maxScore, numConsecutive(i, 0, 0, 1, symbol)); | |
// for each column (top to bottom) | |
// start at row 0, column i , change row + 1, don't change column | |
maxScore = Math.max(maxScore, numConsecutive(0, i, 1, 0, symbol)); | |
} | |
// for diagonal (top-left to bottom-right) | |
// start at row 0, column i , change row + 1, change column + 1 | |
maxScore = Math.max(maxScore, numConsecutive(0, 0, 1, 1, symbol)); | |
// for diagonal (top-right to bottom-left) | |
// start at row 0, column 2 , change row + 1, change column -1 | |
maxScore = Math.max(maxScore, numConsecutive(0, 2, 1, -1, symbol)); | |
return maxScore; | |
} | |
public boolean isInBounds(int row, int column) { | |
return row >= 0 && column >= 0 && | |
row < getBoard().length && | |
column < getBoard()[row].length; | |
} | |
/** | |
* Returns a "visual" representation of a TicTacToe board | |
*/ | |
public String toString() { | |
StringBuilder buffer = new StringBuilder(); | |
for (int i = 0; i < board.length; i++) { | |
for (int j = 0; j < board.length; j++) { | |
buffer.append( " " + board[i][j]); | |
} | |
buffer.append("\n"); | |
} | |
return buffer.toString(); | |
} | |
} | |
abstract class Game { | |
// Don't override this method | |
public final void start() { | |
// we will be adding code to this when we cover ticks and listeners | |
onStart(); | |
} | |
/** | |
* This is the method to override for game specific start | |
* logic. | |
*/ | |
protected void onStart() { | |
} | |
// Don't override this method | |
private final void end() { | |
// we will be adding code to this when we cover ticks and listeners | |
onEnd(); | |
} | |
/** | |
* This is the method to override for game specific end | |
* logic. | |
*/ | |
protected void onEnd() { | |
} | |
/** | |
* return true if the Game is over | |
*/ | |
public abstract boolean isEnd(); | |
/** | |
* It is expected that the Game return a status | |
* or String representation of its current state | |
*/ | |
public abstract String toString(); | |
/** | |
* Performs an action on the game. | |
*/ | |
public final void perform(Action action) { | |
if (action.isValid(this)) { | |
onPerformAction(action); | |
if (isEnd()) { | |
end(); | |
} | |
} | |
} | |
/** | |
* This is the method to override if you need to do something | |
* different than the default update for the Action. | |
*/ | |
protected void onPerformAction(Action action) { | |
action.update(this); | |
} | |
} | |
interface Action<G extends Game> { | |
/** | |
* Returns true if this Action is valid to perform | |
* on the given state of the game | |
*/ | |
public boolean isValid(G game); | |
/** | |
* Mutates state of the game according to the properties | |
* of this Action | |
*/ | |
public void update(G game); | |
} | |
// Add setPiece method somewhere |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment