Add JUNIT tests
For Board.java in the project, every method that is not a printing method or a main method, create 2 JUnit tests.
If getters and setters are tested by other code (they turn green) then you do not need explicit tests for those getters and setters.
If a getter or setter is not tested by other code (I think there is one) then make it package-private (not public) so that you can test it within the package.
Void methods have side effects. If the side effect is something other than printing, it must be tested.
JUnit tests must be written to pass (assuming the code is correct). Do not write a test to fail unless the code has an error. If you expect two values to not match then you can use assertNotEquals
Board.java
import java.util.ArrayList;
import java.util.concurrent.*;
import static java.lang.Math.*;
import java.util.Random;
class Board{
int difficulty;
boolean lost;
public Player player;
public int timer;
private ArrayList<Rock> Rocks;
private final int width = 20;
private final int height = 20;
private static StringBuilder[] screen;
private static StringBuilder buffer;
Board(int hard){
timer = 150;
difficulty = hard;
screen = new StringBuilder[height];
player = new Player(width,height);
Rocks = new ArrayList();
StringBuilder spaces = new StringBuilder(width);
buffer = new StringBuilder(width);
for(int i = 0; i < width; i++){
spaces.insert(0,” “);
buffer.insert(0, “_”);
}
for(int i = 0; i < height; i++){
screen[i] = new StringBuilder(spaces);
}
screen[height – 1].replace((width/2), (width/2) + 1, player.toString());
}
private void spawnRock(){
Rock rock = new Rock(width, height);
Rocks.add(rock);
screen[0].replace(rock.getX(), rock.getX() + 1, rock.toString());
}
public void setTimer(int timer){
this.timer = timer;
}
private void moveRocks(){
for(int r = 0; r < (Rocks.size() – 1); r++){
Rocks.get(r).moveRock();
if(Rocks.get(r).getY() == height){
if(Rocks.get(r).getX() == player.getX()){
lost = true;
break;
}
screen[(Rocks.get(r).getY() – 1)].replace(Rocks.get(r).getX(),
(Rocks.get(r).getX() + 1), ” “);
removeRock(r);
r–;
}
else{
screen[(Rocks.get(r).getY() – 1)].replace(Rocks.get(r).getX(),
(Rocks.get(r).getX() + 1), ” “);
screen[Rocks.get(r).getY()].replace(Rocks.get(r).getX(),
(Rocks.get(r).getX() + 1), (Rocks.get(r).toString()));
}
}
}
private void removeRock(int i){
Rocks.remove(i);
}
private void moveLeft(){
if(player.getX() > 0){
player.moveLeft();
if(player.getX() == (width – 1)){
screen[height – 1].replace(width – 1, width, ” “);
screen[height – 1].replace(width – 2, width – 1, player.toString());
}
else{
screen[height – 1].replace(player.getX() + 1, player.getX() + 2, ” “);
screen[height – 1].replace(player.getX(), player.getX() + 1, player.toString());
}
}
}
private void moveRight(){
if(player.getX() < width – 1){
player.moveRight();
if(player.getX() == 0){
screen[height – 1].replace(0, 1, ” “);
screen[height – 1].replace(1, 2, player.toString());
}
else{
screen[height – 1].replace(player.getX() – 1, player.getX(), ” “);
screen[height – 1].replace(player.getX(), player.getX() + 1, player.toString());
}
}
}
void processChar(int i){
switch(i){
case ‘o’:
moveLeft();
break;
case ‘p’:
moveRight();
break;
case ‘k’:
player.upScore();
break;
}
}
void printState(){
System.out.println(buffer);
for(int l = 0; l < height; l++){
System.out.println(screen[l]);
}
System.out.println(buffer);
System.out.println((int) (60 – player.getScore()*timer/1000));
}
void printLosingState(){
printFall();
int score = player.getScore();
long time = player.getTime();
System.out.println(buffer);
scoring(score, time/1000);
System.out.print(“You made it “);
System.out.print((score));
System.out.print(” moves”);
System.out.print(” in “);
System.out.print((time/1000));
System.out.println(” seconds”);
System.out.print(“Your score is: “);
System.out.println((score – time/1000)*difficulty);
System.out.println(buffer);
}
void takeTurn(int i){
Random rand = new Random();
int stop = rand.nextInt((difficulty-48)*3);//Turn down if too hard. Turn up if too easy
for(int j = 0; j < stop; j++){
spawnRock();
}
moveRocks();
processChar(i);
if(player.getTime()/1000 > 59){
lost = true;
}
}
void scoring(int score,long time){
score = score*(difficulty – 48);
if(false){
}
else if(score > 650){
System.out.println(“You are a true champion!”);
}
else if(score > 500){
System.out.println(“Holy Canoli, you are a monster!”);
}
else if(score > 400){
System.out.println(“You are getting better at this :D”);
}
else if(score > 300){
System.out.println(“Pff *rolls eyes*”);
}
else if(score > 200){
System.out.println(“Are you even using the keys?”);
}
else if(score > 150){
System.out.println(“You tried…”);
}
else{
System.out.println(“Wow…uhm…so…wow…”);
}
}
private void printFall(){
StringBuilder endSpaces = new StringBuilder(width);
StringBuilder endRocks = new StringBuilder(width);
System.out.println(buffer);
for(int i = 0; i < width; i++){
endSpaces.insert(0,” “);
endRocks.insert(0,”O”);
}
for(int i = 0; i < height; i++){
for(int j = i; j < height; j++){
System.out.println(endSpaces);
}
for(int j = (height – i); j < height; j++){
System.out.println(endRocks);
}
System.out.println(buffer);
try{
TimeUnit.MILLISECONDS.sleep(100);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
gameWrapper.java
import java.util.concurrent.*;
import jline.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.util.Timer;
import java.util.TimerTask;
public class gameWrapper {
static Board game;
public static void main(String[] args) throws IOException {
Character mask = null;
String trigger = null;
int j = 0;
int i = 0;
char[] dif = {‘1′,’2′,’3’};
char[] play = {‘p’, ‘q’};
while(j != (int) ‘q’){
ConsoleReader starter = new ConsoleReader(System.in, new PrintWriter(System.out));
System.out.println(“Press p to play or q to quit:”);
j = starter.readCharacter(play);
if(j == (int)’q’){
break;
}
System.out.println(“”);
System.out.println(“”);
System.out.println(“Press: o to move left and p to move right! You have 60 seconds, Good luck!”);
System.out.println(“”);
System.out.println(“”);
System.out.println(“Press: 1 for easy, 2 for medium, 3 for hard”);
i = starter.readCharacter(dif);
game = new Board(i);
starter = null;
try {
WaitForCharThread wfct = new WaitForCharThread();
wfct.start();
int l = 0;
game.printState();
while(l != (int)’q’) {
int timer = 150;
if (wfct.i != 0){
l = wfct.i;
game.takeTurn(l);
game.printState();
wfct = new WaitForCharThread();
wfct.start();
TimeUnit.MILLISECONDS.sleep(timer);
}
else{
game.takeTurn(‘k’);
game.printState();
TimeUnit.MILLISECONDS.sleep(timer);
}
if(game.lost){
game.printLosingState();
l = ‘q’;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Player.java
public class Player {
private int Xcoord;
private int Ycoord;
private long time;
private int score;
private String model;
public Player(int height, int width) {
Xcoord = width/2;
Ycoord = height;
model = “X”;
this.time = System.currentTimeMillis();
this.score = 0;
}
public int getX() {
return Xcoord;
}
public int getY() {
return Ycoord;
}
public long getTime() {
return System.currentTimeMillis() – time;
}
public int getScore(){
return score;
}
public void setXcoord(int xcoord) {
Xcoord = xcoord;
}
public void setYcoord(int ycoord) {
Ycoord = ycoord;
}
public void moveLeft() {
this.setXcoord(this.getX() -1);
score++;
}
public void moveRight() {
this.setXcoord(this.getX() +1);
score++;
}
public String toString(){
return model;
}
public void upScore(){
score++;
}
}
Rock.java
import java.util.Random;
public class Rock {
private int xCoord;
private int yCoord;
private Random gen = new Random();
private String model;
public Rock (int width, int height) {
this.xCoord = gen.nextInt(width);
this.yCoord = 0;
model =”O”;
}
public int getX() {
return this.xCoord;
}
public int getY() {
return this.yCoord;
}
private void setxCoord(int xCoord) {
this.xCoord = xCoord;
}
private void setyCoord(int yCoord) {
this.yCoord = yCoord;
}
public void moveRock() {
this.setyCoord(this.getY() + 1);
}
public String toString(){
return model;
}
}
WaitForCharThread.java
import java.util.concurrent.*;
import jline.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.util.Timer;
import java.util.TimerTask;
class WaitForCharThread extends Thread{
int i = 0;
static int count = 0;
char[] allowed = {‘o’,’p’,’q’,’k’};
public void run(){
try {
ConsoleReader reader = new ConsoleReader(System.in, new PrintWriter(System.out));
i = reader.readCharacter(allowed);
} catch (IOException e){
System.out.println(e);
}
}
}
Solution
Board.java
import java.util.ArrayList;
import java.util.concurrent.*;
import static java.lang.Math.*;
import java.util.Random;
class Board {
int difficulty;
boolean lost;
private Player player;
private int timer;
private ArrayList<Rock> Rocks;
private final int width = 20;
private final int height = 20;
private static StringBuilder[] screen;
private static StringBuilder buffer;
Board(int hard) {
timer = 150;
difficulty = hard;
screen = new StringBuilder[height];
player = new Player(width, height);
Rocks = new ArrayList<>();
StringBuilder spaces = new StringBuilder(width);
buffer = new StringBuilder(width);
for (int i = 0; i < width; i++) {
spaces.insert(0, ” “);
buffer.insert(0, “_”);
}
for (int i = 0; i < height; i++) {
screen[i] = new StringBuilder(spaces);
}
screen[height – 1].replace((width / 2), (width / 2) + 1, player.toString());
}
public StringBuilder[] getScreen() {
return screen;
}
private void spawnRock() {
Rock rock = new Rock(width, height);
Rocks.add(rock);
screen[0].replace(rock.getX(), rock.getX() + 1, rock.toString());
}
private void setTimer(int timer) {
this.timer = timer;
}
private void moveRocks() {
for (int r = 0; r < (Rocks.size() – 1); r++) {
Rocks.get(r).moveRock();
if (Rocks.get(r).getY() == height) {
if (Rocks.get(r).getX() == player.getX()) {
lost = true;
break;
}
screen[(Rocks.get(r).getY() – 1)].replace(Rocks.get(r).getX(),
(Rocks.get(r).getX() + 1), ” “);
removeRock(r);
r–;
} else {
screen[(Rocks.get(r).getY() – 1)].replace(Rocks.get(r).getX(),
(Rocks.get(r).getX() + 1), ” “);
screen[Rocks.get(r).getY()].replace(Rocks.get(r).getX(),
(Rocks.get(r).getX() + 1), (Rocks.get(r).toString()));
}
}
}
private void removeRock(int i) {
Rocks.remove(i);
}
private void moveLeft() {
if (player.getX() > 0) {
player.moveLeft();
if (player.getX() == (width – 1)) {
screen[height – 1].replace(width – 1, width, ” “);
screen[height – 1].replace(width – 2, width – 1, player.toString());
} else {
screen[height – 1].replace(player.getX() + 1, player.getX() + 2, ” “);
screen[height – 1].replace(player.getX(), player.getX() + 1, player.toString());
}
}
}
private void moveRight() {
if (player.getX() < width – 1) {
player.moveRight();
if (player.getX() == 0) {
screen[height – 1].replace(0, 1, ” “);
screen[height – 1].replace(1, 2, player.toString());
} else {
screen[height – 1].replace(player.getX() – 1, player.getX(), ” “);
screen[height – 1].replace(player.getX(), player.getX() + 1, player.toString());
}
}
}
void processChar(int i) {
switch (i) {
case ‘o’:
moveLeft();
break;
case ‘p’:
moveRight();
break;
case ‘k’:
player.upScore();
break;
}
}
void printState() {
System.out.println(buffer);
for (int l = 0; l < height; l++) {
System.out.println(screen[l]);
}
System.out.println(buffer);
System.out.println((int) (60 – player.getScore() * timer / 1000));
}
void printLosingState() {
printFall();
int score = player.getScore();
long time = player.getTime();
System.out.println(buffer);
scoring(score, time / 1000);
System.out.print(“You made it “);
System.out.print((score));
System.out.print(” moves”);
System.out.print(” in “);
System.out.print((time / 1000));
System.out.println(” seconds”);
System.out.print(“Your score is: “);
System.out.println((score – time / 1000) * difficulty);
System.out.println(buffer);
}
void takeTurn(int i) {
Random rand = new Random();
int stop = rand.nextInt((difficulty – 48) * 3);//Turn down if too hard. Turn up if too easy
for (int j = 0; j < stop; j++) {
spawnRock();
}
moveRocks();
processChar(i);
if (player.getTime() / 1000 > 59) {
lost = true;
}
}
void scoring(int score, long time) {
score = score * (difficulty – 48);
if (false) {
} else if (score > 650) {
System.out.println(“You are a true champion!”);
} else if (score > 500) {
System.out.println(“Holy Canoli, you are a monster!”);
} else if (score > 400) {
System.out.println(“You are getting better at this :D”);
} else if (score > 300) {
System.out.println(“Pff *rolls eyes*”);
} else if (score > 200) {
System.out.println(“Are you even using the keys?”);
} else if (score > 150) {
System.out.println(“You tried…”);
} else {
System.out.println(“Wow…uhm…so…wow…”);
}
}
private void printFall() {
StringBuilder endSpaces = new StringBuilder(width);
StringBuilder endRocks = new StringBuilder(width);
System.out.println(buffer);
for (int i = 0; i < width; i++) {
endSpaces.insert(0, ” “);
endRocks.insert(0, “O”);
}
for (int i = 0; i < height; i++) {
for (int j = i; j < height; j++) {
System.out.println(endSpaces);
}
for (int j = (height – i); j < height; j++) {
System.out.println(endRocks);
}
System.out.println(buffer);
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public Player getPlayer() {
return player;
}
public ArrayList<Rock> getRocks() {
return Rocks;
}
public int getHeight() {
return height;
}
}
BoardTest.java
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class BoardTest {
private Board board;
@Before
public void setUp() {
this.board = new Board(50);
}
@Test
public void processCharTest1() {
this.board.processChar(‘o’);
assertEquals(this.board.getScreen()[19].toString(),” X “);
this.board.processChar(‘o’);
assertEquals(this.board.getScreen()[19].toString(),” X “);
this.board.processChar(‘p’);
this.board.processChar(‘p’);
this.board.processChar(‘p’);
assertEquals(this.board.getScreen()[19].toString(),” X “);
}
@Test
public void processCharTest2() {
this.board.processChar(‘k’);
assertEquals(this.board.getPlayer().getScore(), 1);
this.board.processChar(‘o’);
this.board.processChar(‘o’);
assertEquals(this.board.getPlayer().getScore(), 3);
this.board.processChar(‘p’);
this.board.processChar(‘k’);
this.board.processChar(‘o’);
assertEquals(this.board.getPlayer().getScore(), 6);
}
@Test
public void takeTurnTest1() {
board.takeTurn(10);
for (int r = 0; r < (board.getRocks().size() – 1); r++) {
assertTrue((! (board.getRocks().get(r).getY() == board.getHeight() &&
board.getRocks().get(r).getX() == board.getPlayer().getX())) ||
! board.lost
);
}
}
@Test
public void takeTurnTest2() {
board.takeTurn(10);
assertTrue(board.getPlayer().getTime() / 1000 <= 59 || board.lost);
}
}
gameWrapper.java
import java.util.concurrent.*;
import jline.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.util.Timer;
import java.util.TimerTask;
public class gameWrapper {
static Board game;
public static void main(String[] args) throws IOException {
Character mask = null;
String trigger = null;
int j = 0;
int i = 0;
char[] dif = {‘1′,’2′,’3’};
char[] play = {‘p’, ‘q’};
while(j != (int) ‘q’){
ConsoleReader starter = new ConsoleReader(System.in, new PrintWriter(System.out));
System.out.println(“Press p to play or q to quit:”);
j = starter.readCharacter(play);
if(j == (int)’q’){
break;
}
System.out.println(“”);
System.out.println(“”);
System.out.println(“Press: o to move left and p to move right! You have 60 seconds, Good luck!”);
System.out.println(“”);
System.out.println(“”);
System.out.println(“Press: 1 for easy, 2 for medium, 3 for hard”);
i = starter.readCharacter(dif);
game = new Board(i);
starter = null;
try {
WaitForCharThread wfct = new WaitForCharThread();
wfct.start();
int l = 0;
game.printState();
while(l != (int)’q’) {
int timer = 150;
if (wfct.i != 0){
l = wfct.i;
game.takeTurn(l);
game.printState();
wfct = new WaitForCharThread();
wfct.start();
TimeUnit.MILLISECONDS.sleep(timer);
}
else{
game.takeTurn(‘k’);
game.printState();
TimeUnit.MILLISECONDS.sleep(timer);
}
if(game.lost){
game.printLosingState();
l = ‘q’;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Player.java
public class Player {
private int Xcoord;
private int Ycoord;
private long time;
private int score;
private String model;
public Player(int height, int width) {
Xcoord = width/2;
Ycoord = height;
model = “X”;
this.time = System.currentTimeMillis();
this.score = 0;
}
public int getX() {
return Xcoord;
}
public int getY() {
return Ycoord;
}
public long getTime() {
return System.currentTimeMillis() – time;
}
public int getScore(){
return score;
}
public void setXcoord(int xcoord) {
Xcoord = xcoord;
}
public void setYcoord(int ycoord) {
Ycoord = ycoord;
}
public void moveLeft() {
this.setXcoord(this.getX() -1);
score++;
}
public void moveRight() {
this.setXcoord(this.getX() +1);
score++;
}
public String toString(){
return model;
}
public void upScore(){
score++;
}
}
Rock.java
import java.util.Random;
public class Rock {
private int xCoord;
private int yCoord;
private Random gen = new Random();
private String model;
public Rock (int width, int height) {
this.xCoord = gen.nextInt(width);
this.yCoord = 0;
model =”O”;
}
public int getX() {
return this.xCoord;
}
public int getY() {
return this.yCoord;
}
private void setxCoord(int xCoord) {
this.xCoord = xCoord;
}
private void setyCoord(int yCoord) {
this.yCoord = yCoord;
}
public void moveRock() {
this.setyCoord(this.getY() + 1);
}
public String toString(){
return model;
}
}
WaitForCharThread.java
import java.util.concurrent.*;
import jline.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.util.Timer;
import java.util.TimerTask;
class WaitForCharThread extends Thread{
int i = 0;
static int count = 0;
char[] allowed = {‘o’,’p’,’q’,’k’};
public void run(){
try {
ConsoleReader reader = new ConsoleReader(System.in, new PrintWriter(System.out));
i = reader.readCharacter(allowed);
} catch (IOException e){
System.out.println(e);
}
}
}