diff --git a/Blackjack/src/Blackjack.java b/Blackjack/src/Blackjack.java index ca01178..06b7469 100644 --- a/Blackjack/src/Blackjack.java +++ b/Blackjack/src/Blackjack.java @@ -11,150 +11,263 @@ public class Blackjack extends ListeningGameComponent Deck deck; BlackjackHand player; BlackjackHand dealer; - enum State{SETUP, BET, PLAY, DEALER, RESET, LOSE, WIN, PUSH}; + public enum State{SETUP, BET, PLAY, DEALER, RESET, LOSE, WIN, PUSH}; State state = State.SETUP; int bet = 25; int money = 250; + //Getter method for deck + public Deck getDeck() { + return deck; + } + + //Setter method for deck + public void setDeck(Deck deck) { + this.deck = deck; + } + + //Getter method for player + public BlackjackHand getPlayer() { + return player; + } + + //Setter method for player + public void setPlayer(BlackjackHand player) { + this.player = player; + } + + //Getter method for state + public State getState() { + return state; + } + + //Setter method for state + public void setState(State state) { + this.state = state; + } + + //Getter method for dealer + public BlackjackHand getDealer() { + return dealer; + } + + //Setter method for dealer + public void setDealer(BlackjackHand dealer) { + this.dealer = dealer; + } + + //Getter method for bet + public int getBet() { + return bet; + } + + //Setter method for bet + public void setBet(int bet) { + this.bet = bet; + } + + //Getter method for money + public int getMoney() { + return money; + } + + //Setter method for money + public void setMoney(int money) { + this.money = money; + } + public Blackjack() throws IOException { super(Card.WIDTH*3,Card.HEIGHT*3/2); - deck = new Deck(); - deck.shuffle(5); - deck.setAutoShuffle(true); - player = new BlackjackHand(deck, true); - dealer = new BlackjackHand(deck, false); + + setDeck(new Deck()); + + getDeck().shuffle(5); + getDeck().setAutoShuffle(true); + + setPlayer(new BlackjackHand(getDeck(), true)); + + setDealer(new BlackjackHand(getDeck(), false)); } + // Update the State public void update() { if(isKeyPressed("Q")) - System.out.println(state); - if(state == State.SETUP) + System.out.println(getState()); + updateStateSeam( getState() ); + resetKeys(); + } + + /* + * Seam for state in updateMethod. + * + * Enter in a State: + * Call to correlating State function + * + */ + private void updateStateSeam(State s) { + + //SETUP, BET, PLAY, DEALER, RESET, LOSE, WIN, PUSH + switch(s) { + case SETUP : + stateSetup(); + break; + case BET : + stateBet(); + break; + case PLAY : + statePlay(); + break; + case DEALER : + stateDealer(); + break; + case RESET : + stateReset(); + break; + case LOSE : + stateLose(); + break; + case WIN : + stateWin(); + break; + case PUSH : + statePush(); + break; + } + } + + // Operations for Setup Stage. + private void stateSetup() { + if(getDealer() != null) { - if(dealer != null) - { - player.hit(); - dealer.hit(); - player.hit(); - dealer.hit(); - state = State.BET; - } + getPlayer().hit(); + getDealer().hit(); + getPlayer().hit(); + getDealer().hit(); + setState(State.BET); } - else if(state == State.BET) + } + + // Operationsfor Bet State + private void stateBet() { + if(isKeyPressed("Up")) + setBet( (getBet() + 5) ); + else if(isKeyPressed("Down")) + setBet( (getBet() - 5) ); + else if(isKeyPressed("Enter")) + setState( State.PLAY ); + + if(getBet() < 0) + setBet( 0 ); + else if( getBet() > getMoney() ) + setBet( getMoney() ); + } + + // Operations for Play State + private void statePlay() { + if(getPlayer().busted()) + setState( State.LOSE ); + else if(isKeyPressed("Space")) + getPlayer().hit(); + else if(isKeyPressed("Enter")) { - if(isKeyPressed("Up")) - bet+=5; - else if(isKeyPressed("Down")) - bet-=5; - else if(isKeyPressed("Enter")) - state = State.PLAY; - - if(bet < 0) - bet = 0; - else if(bet > money) - bet = money; + getDealer().setShow(true); + setState( State.DEALER ); } - else if(state == State.PLAY) - { - if(player.busted()) - state = State.LOSE; - else if(isKeyPressed("Space")) - player.hit(); - else if(isKeyPressed("Enter")) - { - dealer.setShow(true); - state = State.DEALER; - } + } + + // Operations for Dealer State + private void stateDealer() { + if( getDealer().getValue() < 17 ) { + dealer.hit(); } - else if(state == State.DEALER) + else if( getDealer().busted() || (getDealer().getValue() < getPlayer().getValue()) ) { - if(dealer.getValue() < 17) - dealer.hit(); - else if(dealer.busted() || (dealer.getValue() < player.getValue())) - { - System.out.println("WIN"); - state = State.WIN; - } - else if(dealer.getValue() == player.getValue()) - { - System.out.println("PUSH"); - state = State.PUSH; - } - else - { - System.out.println("LOSE"); - state = State.LOSE; - } + System.out.println("WIN"); + setState( State.WIN ); } - else if(state == State.WIN) + else if(dealer.getValue() == player.getValue()) { - if(isKeyPressed("Space") || isKeyPressed("Enter")) - { - money+=bet; - state = State.RESET; - } + System.out.println("PUSH"); + setState( State.PUSH ); } - else if(state == State.PUSH) + else { - if(isKeyPressed("Space") || isKeyPressed("Enter")) - { - state = State.RESET; - } + System.out.println("LOSE"); + setState( State.LOSE ); } - else if(state == State.LOSE) + } + + // Operations for Reset State + private void stateReset() { + if( getMoney() == 0 ) + setMoney( getMoney() + 2 ); + getPlayer().clear(); + getDealer().clear(); + getDealer().setShow(false); + setState( State.SETUP ); + } + + // Operations for Lose State + private void stateLose() { + if(isKeyPressed("Space") || isKeyPressed("Enter")) { - if(isKeyPressed("Space") || isKeyPressed("Enter")) - { - money-=bet; - state = State.RESET; - } + setMoney( getMoney() - getBet() ); + setState( State.RESET ); + } + } + + // Operations for Win State + private void stateWin() { + if(isKeyPressed("Space") || isKeyPressed("Enter")) + { + setMoney( getMoney() + getBet() ); + setState( State.RESET ); } - else if(state == State.RESET) + } + + // Operations for Reset State + private void statePush() { + if(isKeyPressed("Space") || isKeyPressed("Enter")) { - if(money == 0) - money++; - player.clear(); - dealer.clear(); - dealer.setShow(false); - state = State.SETUP; + setState( State.RESET ); } - resetKeys(); } - + public void draw(Graphics g1) { Graphics2D g = (Graphics2D)g1; g.setColor(Color.BLACK); - g.drawString("Money: " + money + " Bet: " + bet, 10, 20); + g.drawString("Money: " + money + " Bet: " + getBet(), 10, 20); - if(state == State.BET) + if(getState() == State.BET) { - g.drawString("SET YOUR BET", WIDTH-150, 20); + g.drawString("SET YOUR BET", getWIDTH()-150, 20); } - if(state == State.PLAY) + if(getState() == State.PLAY) { - dealer.draw(g1, 157, 12); - player.draw(g1, 25, 158); + getDealer().draw(g1, 157, 12); + getPlayer().draw(g1, 25, 158); } - if(state == State.WIN) + if(getState() == State.WIN) { - dealer.draw(g1, 157, 12); - player.draw(g1, 25, 158); - g.drawString("YOU WIN!", WIDTH-150, 20); + getDealer().draw(g1, 157, 12); + getPlayer().draw(g1, 25, 158); + g.drawString("YOU WIN!", getWIDTH()-150, 20); } - if(state == State.LOSE) + if(getState() == State.LOSE) { - dealer.draw(g1, 157, 12); - player.draw(g1, 25, 158); - g.drawString("YOU LOSE!", WIDTH-150, 20); + getDealer().draw(g1, 157, 12); + getPlayer().draw(g1, 25, 158); + g.drawString("YOU LOSE!", getWIDTH()-150, 20); } - if(state == State.PUSH) + if(getState() == State.PUSH) { - dealer.draw(g1, 157, 12); - player.draw(g1, 25, 158); - g.drawString("PUSH", WIDTH-150, 20); + getDealer().draw(g1, 157, 12); + getPlayer().draw(g1, 25, 158); + g.drawString("PUSH", getWIDTH()-150, 20); } } diff --git a/Blackjack/src/BlackjackHand.java b/Blackjack/src/BlackjackHand.java index 2ad2937..2303b07 100644 --- a/Blackjack/src/BlackjackHand.java +++ b/Blackjack/src/BlackjackHand.java @@ -1,38 +1,73 @@ import java.awt.*; import java.util.*; +/* + * 6 Getter Setter methods added + */ public class BlackjackHand { Vector cards; Deck deck; boolean player; + public int value; + //Gettermethod for deck + public Deck getDeck() { + return deck; + } + + //Setter method for deck + public void setDeck(Deck deck) { + this.deck = deck; + } + + //Getter method for player + public boolean isPlayer() { + return player; + } + + //Setter method for player + public void setPlayer(boolean player) { + this.player = player; + } + + //Getter for Value property + public int getValue() { + return this.value; + } + + //Setter for Value property + public void setValue(int value) { + this.value = value; + } + public BlackjackHand(Deck d, boolean p) { - deck=d; + setDeck(d); cards = new Vector(); - player = p; + setPlayer(p); } public void setShow(boolean p) { - player = p; + setPlayer(p); } public void hit() { - cards.add(deck.deal()); + cards.add(getDeck().deal()); + calculateValue(); } public void clear() { for(int i = 0; i < cards.size(); i++) - deck.add(cards.get(i)); + getDeck().add(cards.get(i)); cards = new Vector(); } - public int getValue() - { + //Calculates the Value of the hand + public int calculateValue(){ int aces = 0; int value = 0; for(int i = 0; i < cards.size(); i++) @@ -46,9 +81,10 @@ public int getValue() value-=10; aces--; } + setValue(value); return value; } - + public boolean busted() { return getValue() > 21; @@ -56,7 +92,7 @@ public boolean busted() public void draw(Graphics g, int x, int y) { - if(player) + if(isPlayer()) { for(int i = cards.size()-1; i >= 0; i--) { diff --git a/Blackjack/src/BlackjackHandTest.java b/Blackjack/src/BlackjackHandTest.java new file mode 100644 index 0000000..2f1eb85 --- /dev/null +++ b/Blackjack/src/BlackjackHandTest.java @@ -0,0 +1,185 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; + +import java.io.IOException; +import java.util.Vector; + +import org.junit.jupiter.api.Test; + +/* + * 5/7 original methods under test. + * + * Methods not under test + * -CalculateValue + * -Draw + * + * 6 getter/setter methods created. + */ +class BlackjackHandTest { + + //Test creating object without any errors. + @Test + void testBlackjackHand() { + BlackjackHand bjh = null; + boolean isExceptionCaught = false; + + try { + bjh = new BlackjackHand(new Deck(),true); + } catch (IOException e) { + isExceptionCaught = true; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertNotNull(bjh); + } + + //Test setShow() method. As a Player + @Test + void testSetShow1() { + BlackjackHand bjh = null; + boolean isExceptionCaught = false; + + boolean isPlayer = false; + + try { + bjh = new BlackjackHand(new Deck(),true); + bjh.setShow(true); + isPlayer = bjh.isPlayer(); + + } catch (IOException e) { + isExceptionCaught = true; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertEquals("Is player, thus true", true ,isPlayer); + } + + //Test setShow() method. As a Dealer + @Test + void testSetShow2() { + BlackjackHand bjh = null; + boolean isExceptionCaught = false; + + boolean isPlayer = false; + + try { + bjh = new BlackjackHand(new Deck(),false); + bjh.setShow(false); + isPlayer = bjh.isPlayer(); + + } catch (IOException e) { + isExceptionCaught = true; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertEquals("Is Dealer, thus false", false,isPlayer); + } + + //Test the hit() method. + @Test + void testHit() { + BlackjackHand bjh = null; + boolean isExceptionCaught = false; + + int oldValue = 0; + int newValue = 0; + + try { + bjh = new BlackjackHand(new Deck(),false); + oldValue = bjh.getValue(); + + // Action + bjh.hit(); + + newValue = bjh.getValue(); + + } catch (IOException e) { + isExceptionCaught = true; + e.printStackTrace(); + } + + boolean isGreaterThan = newValue > oldValue; + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertEquals("Value is greater after another hit", true,isGreaterThan); + } + + //Test the Clear() method + @Test + void testClear() { + BlackjackHand bjh = null; + boolean isExceptionCaught = false; + + boolean isSameObject = false; + + try { + bjh = new BlackjackHand(new Deck(),false); + + // Action + bjh.clear(); + + + Vector testToObject = new Vector(); + + isSameObject = bjh.cards.equals(testToObject); + + } catch (IOException e) { + isExceptionCaught = true; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertEquals("Vector will be a new initialzed vector after clear method", true,isSameObject); + } + + //Test busted() method. Busts in this case + @Test + void testBusted1() { + BlackjackHand bjh = null; + boolean isExceptionCaught = false; + + boolean isBusted = false; + + try { + bjh = new BlackjackHand(new Deck(),false); + bjh.setValue(22); + + // Action + isBusted = bjh.busted(); + + } catch (IOException e) { + isExceptionCaught = true; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertEquals("22 is busted. True", true,isBusted); + } + + //Test busted() method. Does not bust in this case + @Test + void testBusted2() { + BlackjackHand bjh = null; + boolean isExceptionCaught = false; + + boolean isBusted = false; + + try { + bjh = new BlackjackHand(new Deck(),false); + bjh.setValue(20); + + // Action + isBusted = bjh.busted(); + + } catch (IOException e) { + isExceptionCaught = true; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertEquals("20 is not busted. False", false,isBusted); + } + +} diff --git a/Blackjack/src/BlackjackTest.java b/Blackjack/src/BlackjackTest.java new file mode 100644 index 0000000..50b72e9 --- /dev/null +++ b/Blackjack/src/BlackjackTest.java @@ -0,0 +1,495 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; + +import java.io.IOException; +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; + +/* + * Added 12 Getter/Setter methods for class properties + * + * 3/4 originally public funtions under test + * + * functions not under test + * -draw() + * + * seam created : updateStateSeam() + */ +class BlackjackTest { + + //Test updating from SETUP -> BET + @Test + void testUpdate1() { + boolean isSucessful = true; + + Blackjack.State state = null; + try { + Blackjack blackJack = new Blackjack(); + + blackJack.setState( Blackjack.State.SETUP ); + + // Action + blackJack.update(); + + //retrieved the data + state = blackJack.getState(); + + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus true", true ,isSucessful); + assertEquals("Update after SETUP causes state change to BET ",Blackjack.State.BET,state); + } + + /* + * Test updating from BET State + * + * -Betvalue > 0. Should return betValue = 0 + */ + @Test + void testUpdate2() { + boolean isSucessful = true; + + int betValue = -1; + + try { + Blackjack blackJack = new Blackjack(); + + blackJack.setBet(-2); + blackJack.setState( Blackjack.State.BET ); + + // Action + blackJack.update(); + + betValue = blackJack.getBet(); + + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus true", true ,isSucessful); + assertEquals("bound checking on betValue ",0,betValue); + } + + + /* + * Test updating from BET State + * + * -Betvalue > Moneyvalue. Should return betValue = Moneyvalue. + */ + @Test + void testUpdate3() { + boolean isSucessful = true; + + int betValue = -1; + + try { + Blackjack blackJack = new Blackjack(); + + blackJack.setBet(1000); + blackJack.setMoney(10); + blackJack.setState( Blackjack.State.BET ); + + // Action + blackJack.update(); + + betValue = blackJack.getBet(); + + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus true", true ,isSucessful); + assertEquals("bound checking on betValue ",10,betValue); + } + + /* + * Test updating from BET State + * + * -Betvalue >= Moneyvalue. Should return betValue = Moneyvalue. + */ + @Test + void testUpdate4() { + boolean isSucessful = true; + + int betValue = -1; + + try { + Blackjack blackJack = new Blackjack(); + + blackJack.setBet(1000); + blackJack.setMoney(1000); + blackJack.setState( Blackjack.State.BET ); + + // Action + blackJack.update(); + + betValue = blackJack.getBet(); + + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus true", true ,isSucessful); + assertEquals("bound checking on betValue ",1000,betValue); + } + + /* + * Test updating from PLAY State + * + * Updates to Dealer state if enter is pressed + */ + @Test + void testUpdate5() { + boolean isSucessful = true; + + int betValue = -1; + + Blackjack.State state = null; + + try { + Blackjack blackJack = new Blackjack(); + + blackJack.setState( Blackjack.State.PLAY ); + blackJack.keysPressed.add("Enter"); + + // Action + blackJack.update(); + + + state = blackJack.getState(); + + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus true", true ,isSucessful); + assertEquals("Play to Dealer state ",Blackjack.State.DEALER,state); + } + + /* + * Test updating from DEALER State + * + * Updates to Dealer state if it is a PUSH + */ + @Test + void testUpdate6() { + boolean isSucessful = true; + + int betValue = -1; + + Blackjack.State state = null; + + try { + Blackjack blackJack = new Blackjack(); + + blackJack.setState( Blackjack.State.DEALER ); + + blackJack.dealer.setValue(18); + blackJack.player.setValue(18); + + // Action + blackJack.update(); + + state = blackJack.getState(); + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus true", true ,isSucessful); + assertEquals("Update after SETUP causes state change to BET ",Blackjack.State.PUSH,state); + } + + /* + * Test updating from DEALER State + * + * Updates to Dealer state if it is a LOSE + */ + @Test + void testUpdate7() { + boolean isSucessful = true; + Blackjack.State state = null; + + try { + Blackjack blackJack = new Blackjack(); + + blackJack.setState( Blackjack.State.DEALER ); + + blackJack.dealer.setValue(18); + blackJack.player.setValue(17); + + // Action + blackJack.update(); + + state = blackJack.getState(); + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus true", true ,isSucessful); + assertEquals("Player has less points than dealer. Lose ",Blackjack.State.LOSE,state); + } + + /* + * Test updating from DEALER State + * + * Updates to Dealer state if it is a WIN + */ + @Test + void testUpdate8() { + boolean isSucessful = true; + Blackjack.State state = null; + + try { + Blackjack blackJack = new Blackjack(); + + blackJack.setState( Blackjack.State.DEALER ); + + blackJack.dealer.setValue(17); + blackJack.player.setValue(18); + + // Action + blackJack.update(); + + state = blackJack.getState(); + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus true", true ,isSucessful); + assertEquals("Player has more points than dealer. Win ",Blackjack.State.WIN,state); + + } + + /* + * Test updating from RESET State + * + * Updates to SETUP State + */ + @Test + void testUpdate9() { + boolean isSucessful = true; + Blackjack.State state = null; + + try { + Blackjack blackJack = new Blackjack(); + + blackJack.setState( Blackjack.State.RESET ); + + + // Action + blackJack.update(); + + state = blackJack.getState(); + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus true", true ,isSucessful); + assertEquals("Reset to Setup state transition ",Blackjack.State.SETUP,state); + + } + + /* + * Test updating from LOSE State + * + * Updates to RESET + */ + @Test + void testUpdate10() { + boolean isSucessful = true; + Blackjack.State state = null; + + try { + Blackjack blackJack = new Blackjack(); + + blackJack.setState( Blackjack.State.LOSE ); + blackJack.keysPressed.add("Enter"); + + + // Action + blackJack.update(); + + state = blackJack.getState(); + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus true", true ,isSucessful); + assertEquals("Lose to Reset state transition ",Blackjack.State.RESET,state); + } + + /* + * Test updating from WIN State + * + * Updates to RESET state + */ + @Test + void testUpdate11() { + boolean isSucessful = true; + Blackjack.State state = null; + + try { + Blackjack blackJack = new Blackjack(); + + blackJack.setState( Blackjack.State.WIN ); + blackJack.keysPressed.add("Enter"); + + + // Action + blackJack.update(); + + state = blackJack.getState(); + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus true", true ,isSucessful); + assertEquals("Win to Reset state transition",Blackjack.State.RESET,state); + } + + /* + * Test updating from WIN State + * + * Updates to RESET state + */ + @Test + void testUpdate12() { + boolean isSucessful = true; + Blackjack.State state = null; + + try { + Blackjack blackJack = new Blackjack(); + + blackJack.setState( Blackjack.State.PUSH ); + blackJack.keysPressed.add("Enter"); + + + // Action + blackJack.update(); + + state = blackJack.getState(); + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus true", true ,isSucessful); + assertEquals("Push to Reset state transition",Blackjack.State.RESET,state); + } + //Test creating object + @Test + void testBlackjack1() { + Blackjack blackJack = null; + + try { + blackJack = new Blackjack(); + } catch (IOException e) { + e.printStackTrace(); + } + + assertNotNull(blackJack); + } + + //Test creating object without exceptions + @Test + void testBlackjack2() { + + boolean isSucessful = true; + Blackjack blackJack = null; + + try { + blackJack = new Blackjack(); + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus true", true ,isSucessful); + assertNotNull(blackJack); + } + + //Test object properties without exceptions + @Test + void testBlackjack3() { + + boolean isSucessful = true; + Blackjack blackJack = null; + + try { + blackJack = new Blackjack(); + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + assertEquals("Should not have errored thus true", true ,isSucessful); + assertNotNull(blackJack.getDeck()); + } + + //Test object properties without exceptions + @Test + void testBlackjack4() { + + boolean isSucessful = true; + Blackjack blackJack = null; + + try { + blackJack = new Blackjack(); + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + //times + assertEquals("Should not have errored thus true", true ,isSucessful); + assertNotNull( blackJack.getPlayer() ); + } + + //Test object properties without exceptions + @Test + void testBlackjack5() { + + boolean isSucessful = true; + Blackjack blackJack = null; + + try { + blackJack = new Blackjack(); + } catch (IOException e) { + isSucessful = false; + e.printStackTrace(); + } + + //times + assertEquals("Should not have errored thus true", true ,isSucessful); + assertNotNull( blackJack.getDealer() ); + } + + //Test running main() without exception + @Test + void testMain() { + boolean isSuccessful = true; + try { + Blackjack blackJack = new Blackjack(); + } catch (IOException e) { + isSuccessful = false; + e.printStackTrace(); + } + + assertEquals("Should be true if sucessful ",true,isSuccessful); + } + +} diff --git a/Blackjack/src/CardTest.java b/Blackjack/src/CardTest.java new file mode 100644 index 0000000..40a3f4e --- /dev/null +++ b/Blackjack/src/CardTest.java @@ -0,0 +1,127 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; + +import java.awt.image.BufferedImage; +import java.io.BufferedReader; +import java.io.IOException; + +import org.junit.jupiter.api.Test; +/* + * 3/4 original methods under test + * + * Method not under test + * getCardBack + */ +class CardTest { + + //Test creating object without error + @Test + void testCard1() { + Card card = null; + + boolean isExceptionCaught = false; + try { + Deck deck = new Deck(); + card = new Card(Card.Type.NINE, Card.Suit.CLUBS, deck.all ); + } catch (IOException e) { + isExceptionCaught = true; + } + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertNotNull(card); + } + + //Test creating object with error + @Test + void testCard2() { + Card card = null; + + boolean isExceptionCaught = false; + try { + card = new Card(Card.Type.NINE, Card.Suit.CLUBS, new BufferedImage(0, 0, 0)); + } catch (Exception e) { + isExceptionCaught = true; + } + assertEquals("Should have errored thus true", true ,isExceptionCaught); + } + + //Test getCardNumber() + @Test + void testGetCardNumber() { + Card card = null; + + boolean isExceptionCaught = false; + int x = 0; + try { + Deck deck = new Deck(); + card = new Card(Card.Type.NINE, Card.Suit.CLUBS, deck.all ); + + x = card.getCardNumber(); + + } catch (IOException e) { + isExceptionCaught = true; + } + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertEquals("card number is 36. true",36,x); + } + + //Test getBJValue() + @Test + void testGetBJValue1() { + Card card = null; + + boolean isExceptionCaught = false; + int x = 0; + try { + Deck deck = new Deck(); + card = new Card(Card.Type.NINE, Card.Suit.CLUBS, deck.all ); + + x = card.getBJValue(); + + } catch (IOException e) { + isExceptionCaught = true; + } + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertEquals("card number is 9. true",9,x); + } + + //Test getBJValue() + @Test + void testGetBJValue2() { + Card card = null; + + boolean isExceptionCaught = false; + int x = 0; + try { + Deck deck = new Deck(); + card = new Card(Card.Type.ACE, Card.Suit.CLUBS, deck.all ); + + x = card.getBJValue(); + + } catch (IOException e) { + isExceptionCaught = true; + } + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertEquals("card number is 11. true",11,x); + } + + //Test getBJValue() + @Test + void testGetBJValue3() { + Card card = null; + + boolean isExceptionCaught = false; + int x = 0; + try { + Deck deck = new Deck(); + card = new Card(Card.Type.QUEEN, Card.Suit.CLUBS, deck.all ); + + x = card.getBJValue(); + + } catch (IOException e) { + isExceptionCaught = true; + } + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertEquals("card number is 10. true",10,x); + } + +} diff --git a/Blackjack/src/Deck.java b/Blackjack/src/Deck.java index 97c7b47..5189f55 100644 --- a/Blackjack/src/Deck.java +++ b/Blackjack/src/Deck.java @@ -3,6 +3,9 @@ import java.util.*; import java.io.*; +/* + * Added 6 getter/setter methods + */ public class Deck { public static final String DEFAULT_FILE = "cards.jpg"; @@ -10,10 +13,41 @@ public class Deck Vector cards = new Vector(); BufferedImage back; BufferedImage all; - boolean autoShuffle = false; - int added = 0; - int maxCards = 52; - + private boolean autoShuffle = false; + private int added = 0; + private int maxCards = 52; + + //Setter method for autoshuffle + public void setAutoShuffle(boolean s) + { + autoShuffle = s; + } + + //Getter method for autoshuffle + public boolean getAutoShuffle() { + return autoShuffle; + } + + //Setter method for added + public void setAdded(int i) { + added = i; + } + + //Getter method for added + public int getAdded() { + return added; + } + + //Setter method for maxCards + public void setMaxCards(int i) { + maxCards = i; + } + + //Getter method for maxCards + public int getMaxCards() { + return maxCards; + } + public Deck(String fileName) throws IOException { all = ImageIO.read(new File(fileName)); @@ -51,12 +85,12 @@ public Card deal() public void add(Card c) { - added++; + setAdded(getAdded()+1); cards.add(c); - if(cards.size() > maxCards) - maxCards = cards.size(); + if(cards.size() > getMaxCards()) + setMaxCards( cards.size() ); - if(added >= maxCards) + if(getAdded() >= getMaxCards()) shuffle(10); } @@ -75,7 +109,7 @@ public void reload() public void shuffle(int times) { - added = 0; + setAdded(0); times*=cards.size(); for(int i = 0; i < times; i++) { @@ -85,9 +119,4 @@ public void shuffle(int times) cards.add(c); } } - - public void setAutoShuffle(boolean s) - { - autoShuffle = s; - } } diff --git a/Blackjack/src/DeckTest.java b/Blackjack/src/DeckTest.java new file mode 100644 index 0000000..654c985 --- /dev/null +++ b/Blackjack/src/DeckTest.java @@ -0,0 +1,195 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; + +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.util.Vector; + +import org.junit.jupiter.api.Test; + +/* + * 6 getter/setter methods + * + * 6/6 original methods under test + */ +class DeckTest { + + //Test creating the object without an exception + @Test + void testDeckString1() { + Deck deck = null; + + boolean isExceptionCaught = false; + try { + deck = new Deck("cards.jpg"); + } catch (IOException e) { + isExceptionCaught = true; + e.printStackTrace(); + } + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertNotNull(deck); + } + + //Test creating the object with an exception + @Test + void testDeckString2() { + Deck deck = null; + + boolean isExceptionCaught = false; + try { + deck = new Deck("rando.jpg"); + } catch (IOException e) { + isExceptionCaught = true; + //e.printStackTrace(); + } + assertEquals("Should have errored thus true", true ,isExceptionCaught); + } + + //Test creating the object without an exception + @Test + void testDeck() { + Deck deck = null; + + boolean isExceptionCaught = false; + try { + deck = new Deck(); + } catch (IOException e) { + isExceptionCaught = true; + } + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertNotNull(deck); + } + + //test deal() method. + @Test + void testDeal() { + Deck deck = null; + + boolean isExceptionCaught = false; + try { + deck = new Deck(); + deck.cards = new Vector(); + + } catch (IOException e) { + isExceptionCaught = true; + } + + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertNull(deck.deal(), "Deal will return null because card count = 0"); + } + + //Test Add() method + @Test + void testAdd() { + Deck deck = null; + + boolean isExceptionCaught = false; + + int oldValue = 0; + int newValue = 0; + + try { + deck = new Deck(); + deck.cards = new Vector(); + + oldValue = deck.getAdded(); + + // Action + deck.add(new Card(Card.Type.NINE, Card.Suit.CLUBS, deck.all )); + + + newValue = deck.getAdded(); + } catch (IOException e) { + isExceptionCaught = true; + } + + boolean isAddedIsBigger = newValue > oldValue; + + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertEquals("Value will be bigger after adding a card. true", true,isAddedIsBigger); + } + + // Test reload method() + @Test + void testReload() { + Deck deck = null; + + boolean isExceptionCaught = false; + + int deckSize = 0; + + try { + deck = new Deck(); + deck.cards = new Vector(); + + // Action + deck.reload(); + + deckSize = deck.cards.size(); + + } catch (IOException e) { + isExceptionCaught = true; + } + + boolean is52deck = 52 == deckSize; + + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertEquals("Value will be bigger 52 after getting a new deck. true", true,is52deck); + } + + //Test shuffle + @Test + void testShuffle1() { + Deck deck = null; + + boolean isExceptionCaught = false; + + int addedSize = 0; + + try { + deck = new Deck(); + deck.cards = new Vector(); + + // Action + deck.shuffle(-1); + + addedSize = deck.getAdded(); + + } catch (IOException e) { + isExceptionCaught = true; + } + + boolean is0added= 0 == addedSize; + + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertEquals("Value will be 0 because no new cards are added. true", true,is0added); + } + + //Tests shuffle() + @Test + void testShuffle2() { + Deck deck = null; + + boolean isExceptionCaught = false; + + int addedSize = 0; + + try { + deck = new Deck(); + deck.cards = new Vector(); + + // Action + deck.shuffle(2); + + addedSize = deck.getAdded(); + + } catch (IOException e) { + isExceptionCaught = true; + } + + boolean is0added= 0 == addedSize; + + assertEquals("Should not have errored thus false", false ,isExceptionCaught); + assertEquals("Value will be 0 because no new cards are added. true", true,is0added); + } +} diff --git a/Blackjack/src/gameutil/GameComponent.java b/Blackjack/src/gameutil/GameComponent.java index 0b86466..615f9a3 100644 --- a/Blackjack/src/gameutil/GameComponent.java +++ b/Blackjack/src/gameutil/GameComponent.java @@ -5,11 +5,44 @@ import java.awt.image.*; import javax.swing.*; +/* + * Added 6 Getter/Setter methods and implemented them + */ public abstract class GameComponent extends JPanel { - public static int WIDTH, HEIGHT; + private static int WIDTH, HEIGHT; protected BufferedImage background = null; - public int delay = 25; + private int delay = 25; + + //Getter method for delay + public int getDelay() { + return delay; + } + + //Setter method for delay + public void setDelay(int delay) { + this.delay = delay; + } + + //Getter method for width + protected static int getWIDTH() { + return WIDTH; + } + + //Setter method for width + public static void setWIDTH(int wIDTH) { + WIDTH = wIDTH; + } + + //Getter method for height + public static int getHEIGHT() { + return HEIGHT; + } + + //Setter method for height + public static void setHEIGHT(int hEIGHT) { + HEIGHT = hEIGHT; + } /** * Constructs a GameComponent with a width of w, and a height of h. @@ -21,10 +54,10 @@ public abstract class GameComponent extends JPanel public GameComponent(int w, int h) { super(); - WIDTH = w; - HEIGHT = h; - setSize(WIDTH, HEIGHT); - setPreferredSize(new Dimension(WIDTH, HEIGHT)); + setWIDTH(w); + setHEIGHT(h); + setSize(getWIDTH(), getHEIGHT()); + setPreferredSize(new Dimension(getWIDTH(), getHEIGHT())); setBackground(Color.WHITE); setVisible(true); Thread t = new Thread() @@ -36,9 +69,9 @@ public void run() long time = System.currentTimeMillis(); if(background == null) { - background = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); + background = new BufferedImage(getWIDTH(), getHEIGHT(), BufferedImage.TYPE_INT_RGB); background.getGraphics().setColor(Color.WHITE); - background.getGraphics().fillRect(0,0,WIDTH,HEIGHT); + background.getGraphics().fillRect(0,0,getWIDTH(),getHEIGHT()); } requestFocus(); @@ -55,8 +88,8 @@ public void run() time = System.currentTimeMillis()-time; try { - if(delay-(int)time > 0) - sleep(delay-(int)time); + if(getDelay()-(int)time > 0) + sleep(getDelay()-(int)time); } catch(Exception ex) { @@ -73,10 +106,10 @@ private Graphics getCanvas() { if(background == null) { - background = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); + background = new BufferedImage(getWIDTH(), getHEIGHT(), BufferedImage.TYPE_INT_RGB); } background.getGraphics().setColor(Color.WHITE); - background.getGraphics().fillRect(0,0,WIDTH,HEIGHT); + background.getGraphics().fillRect(0,0,getWIDTH(),getHEIGHT()); return background.getGraphics(); } @@ -124,7 +157,7 @@ public JFrame makeFullScreenWindow() frame.getContentPane().setLayout(null); frame.getContentPane().add(this, 0, 0); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setSize(WIDTH, HEIGHT); + frame.setSize(getWIDTH(), getHEIGHT()); frame.setResizable(false); frame.setUndecorated(true); frame.setVisible(true); diff --git a/Blackjack/src/gameutil/ImageLoaderTest.java b/Blackjack/src/gameutil/ImageLoaderTest.java new file mode 100644 index 0000000..80a99f2 --- /dev/null +++ b/Blackjack/src/gameutil/ImageLoaderTest.java @@ -0,0 +1,137 @@ +package gameutil; + +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; + +import java.awt.Image; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; +/* + * Tested 8/9 methods + */ +class ImageLoaderTest { + + //Test Creates object + @Test + void testImageLoader() { + ImageLoader il = new ImageLoader(); + assertNotNull(il); + } + + //Test unsucessfully loading a picture through url + @Test + void testLoad() { + ImageLoader il = new ImageLoader(); + + boolean errorDidOccur = false; + try { + URL url = new URL("cards.jpg"); + il.load("cards.jpg"); + + } catch (Exception e) { + errorDidOccur = true; + } + + assertEquals("Error should be thrown", true, errorDidOccur); + } + + //Test sucessfully loading a picture through string + @Test + void testLoadString1() { + ImageLoader il = new ImageLoader(); + il.load("cards.jpg"); + + boolean containsFile = il.names.contains("cards.jpg"); + assertEquals("File was added to arrayList", true, containsFile); + } + + //Test unsucessfully loading a picture through string + @Test + void testLoadString2() { + ImageLoader il = new ImageLoader(); + il.load("blahblahblah.jpg"); + + boolean containsFile = il.names.contains("blahblahblah.jpg"); + assertEquals("File was added to arrayList", true, containsFile); + } + + //Test sucessful alreadyLoaded() method + @Test + void testAlreadyLoaded1() { + ImageLoader il = new ImageLoader(); + il.load("cards.jpg"); + + boolean isLoaded = il.alreadyLoaded("cards.jpg"); + + assertEquals("File was added to arrayList", true, isLoaded); + } + + + //Test unsucessful alreadyLoaded() method + @Test + void testAlreadyLoaded2() { + ImageLoader il = new ImageLoader(); + il.load("cards.jpg"); + + boolean isLoaded = il.alreadyLoaded("cards2.jpg"); + + assertEquals("File was not added to arrayList", false, isLoaded); + } + + //Test getImageString() method + @Test + void testGetImageString() { + ImageLoader il = new ImageLoader(); + il.load("cards.jpg"); + + + Image image = il.getImage("cards.jpg"); + + assertNotNull(image); + } + + //Tests getImages + @Test + void testGetImages() { + ImageLoader il = new ImageLoader(); + il.load("cards.jpg"); + + ArrayList imageCollection1 = il.images; + + ArrayList imageCollection2 = il.getImages(); + + assertEquals("image collection 1 and two should be equal", imageCollection1, imageCollection2); + } + + //Test getImages + @Test + void testGetImageInt() { + ImageLoader il = new ImageLoader(); + il.load("cards.jpg"); + + Image image1 = il.images.get(0); + Image image2 = il.getImage(0); + + + assertEquals("image collection 1 and two should be equal", image1, image2); + } + + //Tests getImages + @Test + void testGetImagesIntInt() { + ImageLoader il = new ImageLoader(); + il.load("cards.jpg"); + + ArrayList imageCollection1 = il.images; + + ArrayList imageCollection2 = il.getImages(0,0); + + assertEquals("image collection 1 and two should be equal", imageCollection1, imageCollection2); + } + + + +} diff --git a/Blackjack/src/gameutil/ListeningGameComponent.java b/Blackjack/src/gameutil/ListeningGameComponent.java index 7251f24..48a4b78 100644 --- a/Blackjack/src/gameutil/ListeningGameComponent.java +++ b/Blackjack/src/gameutil/ListeningGameComponent.java @@ -6,6 +6,9 @@ import javax.swing.*; import java.awt.event.*; +/* + * Added 10 Getter Setter methods and implemented them + */ public abstract class ListeningGameComponent extends GameComponent implements MouseListener, MouseMotionListener, KeyListener { public boolean mousePressed1 = false, mousePressed2 = false, mousePressed3 = false; @@ -74,11 +77,11 @@ public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e) { if(e.getButton() == e.BUTTON1) - mousePressed1 = true; + setMousePressed1(true); if(e.getButton() == e.BUTTON2) - mousePressed2 = true; + setMousePressed2(true); if(e.getButton() == e.BUTTON3) - mousePressed3 = true; + setMousePressed3(true); } @@ -89,11 +92,11 @@ public void mousePressed(MouseEvent e) public void mouseReleased(MouseEvent e) { if(e.getButton() == e.BUTTON1) - mousePressed1 = false; + setMousePressed1(false); if(e.getButton() == e.BUTTON2) - mousePressed2 = false; + setMousePressed2(false); if(e.getButton() == e.BUTTON3) - mousePressed3 = false; + setMousePressed3(false); } @@ -104,13 +107,13 @@ public void mouseReleased(MouseEvent e) public void mouseDragged(MouseEvent e) { if(e.getButton() == e.BUTTON1) - mousePressed1 = !mousePressed1; + setMousePressed1(!isMousePressed1()); if(e.getButton() == e.BUTTON2) - mousePressed2 = !mousePressed2; + setMousePressed2(!isMousePressed2()); if(e.getButton() == e.BUTTON3) - mousePressed3 = !mousePressed3; - mouseX = e.getX(); - mouseY = e.getY(); + setMousePressed3(!isMousePressed3()); + setMouseX(e.getX()); + setMouseY(e.getY()); } @@ -120,12 +123,12 @@ public void mouseDragged(MouseEvent e) */ public void mouseMoved(MouseEvent e) { - mousePressed1 = false; - mousePressed2 = false; - mousePressed3 = false; + setMousePressed1(false); + setMousePressed2(false); + setMousePressed3(false); - mouseX = e.getX(); - mouseY = e.getY(); + setMouseX(e.getX()); + setMouseY(e.getY()); } @@ -170,11 +173,11 @@ public void keyTyped(KeyEvent e){} public boolean isMousePressed(int b) { if(b == 1) - return mousePressed1; + return isMousePressed1(); else if(b == 2) - return mousePressed2; + return isMousePressed2(); else if(b == 3) - return mousePressed3; + return isMousePressed3(); return false; } @@ -185,12 +188,12 @@ else if(b == 3) */ public boolean isMousePressed() { - if(mousePressed1) - return mousePressed1; - else if(mousePressed2) - return mousePressed2; - else if(mousePressed3) - return mousePressed3; + if(isMousePressed1()) + return isMousePressed1(); + else if(isMousePressed2()) + return isMousePressed2(); + else if(isMousePressed3()) + return isMousePressed3(); return false; } @@ -222,4 +225,55 @@ public void resetKeys() { keysPressed = new ArrayList(); } + + //Getter method for mousePressed1 + public boolean isMousePressed1() { + return mousePressed1; + } + + //Setter method for mousePressed1 + public void setMousePressed1(boolean mousePressed1) { + this.mousePressed1 = mousePressed1; + } + + //Getter method for mousePressed + public boolean isMousePressed2() { + return mousePressed2; + } + + //Setter method for mousePressed2 + public void setMousePressed2(boolean mousePressed2) { + this.mousePressed2 = mousePressed2; + } + + //Getter method for mousePressed3 + public boolean isMousePressed3() { + return mousePressed3; + } + + //Setter method for mousePressed3 + public void setMousePressed3(boolean mousePressed3) { + this.mousePressed3 = mousePressed3; + } + + //Getter method for mouseX + public int getMouseX() { + return mouseX; + } + + //Setter method for MouseX + public void setMouseX(int mouseX) { + this.mouseX = mouseX; + } + + //Getter method for MouseY + public int getMouseY() { + return mouseY; + } + + //Setter methof for mouseY + public void setMouseY(int mouseY) { + this.mouseY = mouseY; + } + } \ No newline at end of file diff --git a/Catch/src/Ball.java b/Catch/src/Ball.java index 6015055..d3ef65c 100644 --- a/Catch/src/Ball.java +++ b/Catch/src/Ball.java @@ -10,28 +10,59 @@ public class Ball private boolean good; private int x,y; - public Ball(int x, int y) - { + //Getter method for x + public int getX() { + return x; + } + + //Setter method for x + public void setX(int x) { this.x = x; + } + + //Getter method for y + public int getY() { + return y; + } + + //Setter method for y + public void setY(int y) { this.y = y; - good = true; + } + + //Setter method for Good + public void setGood(boolean good) { + this.good = good; + } + + //Getter method for Good + public boolean isGood() + { + return good; + } + + public Ball(int x, int y) + { + setX(x); + setY(y); + setGood(true); } public Ball(int x, int y, boolean good) { - this.x = x; - this.y = y; - this.good = good; + setX(x); + setY(y); + setGood(good); } public Ball moveTo(int x, int y) { - return new Ball(x,y,good); + return new Ball(x,y,isGood()); } public Ball move(int dx, int dy) { - return new Ball(x+dx, y+dy, good); + return new Ball(getX()+dx, getY()+dy, isGood()); } public Ball moveDown() @@ -41,22 +72,17 @@ public Ball moveDown() public Point getLocation() { - return new Point(x,y); + return new Point(getX(),getY()); } public Rectangle getBounds() { - return new Rectangle(x-RADIUS, y-RADIUS, RADIUS*2, RADIUS*2); - } - - public boolean isGood() - { - return good; + return new Rectangle(getX()-RADIUS, getY()-RADIUS, RADIUS*2, RADIUS*2); } public void draw(Graphics g) { g.setColor(good?GOOD_COLOR:BAD_COLOR); - g.fillOval(x-RADIUS, y-RADIUS, RADIUS*2, RADIUS*2); + g.fillOval(getX()-RADIUS, getY()-RADIUS, RADIUS*2, RADIUS*2); } } \ No newline at end of file diff --git a/Catch/src/BallTest.java b/Catch/src/BallTest.java new file mode 100644 index 0000000..186adf4 --- /dev/null +++ b/Catch/src/BallTest.java @@ -0,0 +1,141 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; + +import java.awt.Point; +import java.awt.Rectangle; + +import org.junit.jupiter.api.Test; + +/* + * Added 3 getter Setter methods + * + * 8/9 methods under test + * + * Methods not under test + * - Draw() + */ +class BallTest { + + //Test creation of object + @Test + void testBallIntInt1() { + Ball ball = new Ball(0,0); + assertNotNull(ball); + } + + //Test properties of object after instantiation + @Test + void testBallIntInt2() { + Ball ball = new Ball(0,1); + + int x = ball.getX(); + int y = ball.getY(); + boolean isGood = ball.isGood(); + + assertEquals("x =0 ",0,x); + assertEquals("y =1 ",1,y); + assertEquals("isGood = true", true, isGood); + } + + //Test creation of object + @Test + void testBallIntIntBoolean1() { + Ball ball = new Ball(0,0,false); + assertNotNull(ball); + } + + //Test properties of object after instantiation + @Test + void testBallIntIntBoolean2() { + Ball ball = new Ball(0,1,false); + + int x = ball.getX(); + int y = ball.getY(); + boolean isGood = ball.isGood(); + + assertEquals("x =0 ",0,x); + assertEquals("y =1 ",1,y); + assertEquals("isGood = false", false, isGood); + } + + // Test MoveTo() function + @Test + void testMoveTo() { + Ball ball = new Ball(0,1); + + // Action + ball = ball.moveTo(3, 0); + + int x = ball.getX(); + int y = ball.getY(); + boolean isGood = ball.isGood(); + + + assertEquals("x =3 ",3,x); + assertEquals("y =0 ",0,y); + assertEquals("isGood = true", true, isGood); + } + + // Test Move() function + @Test + void testMove() { + Ball ball = new Ball(5,2,false); + + // Action + ball = ball.move(1, 2); + + int x = ball.getX(); + int y = ball.getY(); + boolean isGood = ball.isGood(); + + assertEquals("x =6 ",6,x); + assertEquals("y =4 ",4,y); + assertEquals("isGood = false", false, isGood); + } + + // Test MoveDown() function + @Test + void testMoveDown() { + Ball ball = new Ball(5,2,true); + + // Action + ball = ball.moveDown(); + + int x = ball.getX(); + int y = ball.getY(); + boolean isGood = ball.isGood(); + + assertEquals("x =5 ",5,x); + assertEquals("y =4 ",4,y); + assertEquals("isGood = true", true, isGood); + } + + // Test GetLocation Function + @Test + void testGetLocation() { + Ball ball = new Ball(5,2,true); + + // Action + Point p = ball.getLocation(); + + int x = p.x; + int y = p.y; + + assertEquals("x =5 ",5,x); + assertEquals("y =2 ",2,y); + } + + // Test getBounds() function + @Test + void testGetBounds() { + Ball ball = new Ball(5,2,true); + + Rectangle r = ball.getBounds(); + + int x = r.x; + int y = r.y; + + assertEquals("x =0 ",0,x); + assertEquals("y =-3 ",-3,y); + } +} diff --git a/Catch/src/Bucket.java b/Catch/src/Bucket.java index 07b678e..65d3187 100644 --- a/Catch/src/Bucket.java +++ b/Catch/src/Bucket.java @@ -8,12 +8,32 @@ public class Bucket private int x,y; - public Bucket(int x, int y) - { + //Getter method for X + public int getX() { + return x; + } + + //Setter method for X + public void setX(int x) { this.x = x; + } + + //Getter method for Y + public int getY() { + return y; + } + + //Setter method for Y + public void setY(int y) { this.y = y; } + public Bucket(int x, int y) + { + setX(x); + setY(y); + } + public Bucket moveTo(int x, int y) { return new Bucket(x,y); @@ -21,7 +41,7 @@ public Bucket moveTo(int x, int y) public Bucket move(int dx, int dy) { - return new Bucket(x+dx, y+dy); + return new Bucket(getX()+dx, getY()+dy); } public Bucket moveLeft() @@ -36,12 +56,12 @@ public Bucket moveRight() public Point getLocation() { - return new Point(x,y); + return new Point(getX(),getY()); } public Rectangle getBounds() { - return new Rectangle(x-WIDTH/2, y-HEIGHT/2, WIDTH, HEIGHT); + return new Rectangle(getX()-WIDTH/2, getY()-HEIGHT/2, WIDTH, HEIGHT); } public boolean contains(Ball b) @@ -52,6 +72,6 @@ public boolean contains(Ball b) public void draw(Graphics g) { g.setColor(Color.BLACK); - g.fillRect(x-WIDTH/2, y-HEIGHT/2, WIDTH, HEIGHT); + g.fillRect(getX()-WIDTH/2, getY()-HEIGHT/2, WIDTH, HEIGHT); } } \ No newline at end of file diff --git a/Catch/src/BucketTest.java b/Catch/src/BucketTest.java new file mode 100644 index 0000000..f3dc7e0 --- /dev/null +++ b/Catch/src/BucketTest.java @@ -0,0 +1,114 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; + +import java.awt.Point; + +import org.junit.jupiter.api.Test; + +/* + * Created 4 getter/setter methods + * + * 6/9 original methods under test + * + * Methods not under test + * -getBounds + * -contains + * -draw + */ +class BucketTest { + + // Creates the object and makes sure its not null + @Test + void testBucket1() { + Bucket bucket = new Bucket(1,1); + assertNotNull(bucket); + } + + // Creates the object and checks class properties + @Test + void testBucket2() { + Bucket bucket = new Bucket(1,1); + int x = bucket.getX(); + + assertEquals("x =1 ",1,x); + } + + // Creates the object and checks class properties + @Test + void testBucket3() { + Bucket bucket = new Bucket(1,2); + int y = bucket.getY(); + + assertEquals("y =2 ",2,y); + } + + // test MoveTo() method + @Test + void testMoveTo() { + Bucket bucket = new Bucket(2,2); + bucket = bucket.moveTo(3, 4); + + int x = bucket.getX(); + int y = bucket.getY(); + assertEquals("x = 3 ",3,x); + assertEquals("y = 4", 4, y); + } + + // test Move() method + @Test + void testMove() { + Bucket bucket = new Bucket(2,2); + bucket = bucket.move(1, 2); + + int x = bucket.getX(); + int y = bucket.getY(); + + assertEquals("x = 3 ",3,x); + assertEquals("y = 4", 4, y); + } + + // test MoveLeft() method + @Test + void testMoveLeft() { + Bucket bucket = new Bucket(2,2); + + int beforeX = bucket.getX(); + + bucket = bucket.moveLeft(); + + int afterX = bucket.getX(); + + boolean beforeXIsGreater = beforeX > afterX; + assertEquals("Before move x coordinate is greater than after move left x coordinate",true,beforeXIsGreater); + } + + // Test moveRight() function + @Test + void testMoveRight() { + Bucket bucket = new Bucket(2,2); + + int beforeX = bucket.getX(); + + bucket = bucket.moveRight(); + + int afterX = bucket.getX(); + + boolean afterXIsGreater = beforeX < afterX; + assertEquals("Before move x coordinate is less than after move right x coordinate",true,afterXIsGreater); + } + + // Test getLocation() method + @Test + void testGetLocation() { + Bucket bucket = new Bucket(2,3); + + Point p = bucket.getLocation(); + int x = p.x; + int y = p.y; + + assertEquals("x = 2 ",2,x); + assertEquals("y = 3 ",3,y); + + } + +} diff --git a/Catch/src/CatchComponent.java b/Catch/src/CatchComponent.java index 9816a66..3c01f80 100644 --- a/Catch/src/CatchComponent.java +++ b/Catch/src/CatchComponent.java @@ -8,11 +8,41 @@ public class CatchComponent extends JComponent implements KeyListener, Runnable private boolean leftPressed = false; private boolean rightPressed = false; + //Getter method for leftPressed + public boolean isLeftPressed() { + return leftPressed; + } + + //Setter method for leftPressed + public void setLeftPressed(boolean leftPressed) { + this.leftPressed = leftPressed; + } + + //Getter method for rightPressed + public boolean isRightPressed() { + return rightPressed; + } + + //Setter method for rightPressed + public void setRightPressed(boolean rightPressed) { + this.rightPressed = rightPressed; + } + + //Getter method for engine + public CatchEngine getEngine() { + return engine; + } + + //Setter method for engine + public void setEngine(CatchEngine engine) { + this.engine = engine; + } + public CatchComponent() { super(); - engine = new CatchEngine(); - setPreferredSize(new Dimension(engine.WIDTH, engine.HEIGHT)); + setEngine(new CatchEngine()); + setPreferredSize(new Dimension(getEngine().WIDTH, getEngine().HEIGHT)); addKeyListener(this); Thread run = new Thread(this); @@ -43,9 +73,9 @@ public void paint(Graphics g) public void updateState() { - if(leftPressed) + if(isLeftPressed()) engine.moveLeft(); - if(rightPressed) + if(isRightPressed()) engine.moveRight(); engine.update(); } @@ -53,20 +83,21 @@ public void updateState() public void keyPressed(KeyEvent ke) { if(ke.getKeyCode() == KeyEvent.VK_LEFT) - leftPressed = true; + setLeftPressed(true); else if(ke.getKeyCode() == KeyEvent.VK_RIGHT) - rightPressed = true; + setRightPressed(true); } public void keyReleased(KeyEvent ke) { if(ke.getKeyCode() == KeyEvent.VK_LEFT) - leftPressed = false; + setLeftPressed(false); else if(ke.getKeyCode() == KeyEvent.VK_RIGHT) - rightPressed = false; + setRightPressed(false); } - public void keyTyped(KeyEvent ke) - { + @Override + public void keyTyped(KeyEvent e) { + } } diff --git a/Catch/src/CatchComponentTest.java b/Catch/src/CatchComponentTest.java new file mode 100644 index 0000000..6b09855 --- /dev/null +++ b/Catch/src/CatchComponentTest.java @@ -0,0 +1,154 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; + +import java.awt.event.KeyEvent; + +import org.junit.jupiter.api.Test; + +/* + * Added 6 getter setter methods for class properties + * + * 5/7 original methods under test + * + * Methods not under test + * - run + * - paint + */ +class CatchComponentTest { + + //Test creation of object + @Test + void testCatchComponent1() { + CatchComponent cc = new CatchComponent(); + assertNotNull(cc); + } + + //Test properties of object after creation + @Test + void testCatchComponent2() { + CatchComponent cc = new CatchComponent(); + CatchEngine ce = cc.getEngine(); + assertNotNull(ce); + } + + //Test updateState() method. Will move left + @Test + void testUpdateState1() { + CatchComponent cc = new CatchComponent(); + + cc.setLeftPressed(true); + CatchEngine ce = cc.getEngine(); + int beforeCoord = ce.getBucket().getBounds().x; + + //Will move left + cc.updateState(); + + ce = cc.getEngine(); + int afterCoord = ce.getBucket().getBounds().x; + + Boolean isSmallerAfterMove = (beforeCoord > afterCoord); + + assertEquals("Coordinates will be smaller after moving left",true,isSmallerAfterMove); + } + + //Test updateState() method. Will move right + @Test + void testUpdateState2() { + CatchComponent cc = new CatchComponent(); + + cc.setLeftPressed(false); + cc.setRightPressed(true); + CatchEngine ce = cc.getEngine(); + int beforeCoord = ce.getBucket().getBounds().x; + + //Will move right + cc.updateState(); + + ce = cc.getEngine(); + int afterCoord = ce.getBucket().getBounds().x; + + Boolean isSmallerAfterMove = (beforeCoord > afterCoord); + + assertEquals("Coordinates will be larger after moving right",false,isSmallerAfterMove); + } + + //Test updateState() method. Will move left then right + @Test + void testUpdateState3() { + CatchComponent cc = new CatchComponent(); + + cc.setLeftPressed(true); + cc.setRightPressed(true); + CatchEngine ce = cc.getEngine(); + int beforeCoord = ce.getBucket().getBounds().x; + + //Will move left then right + cc.updateState(); + + ce = cc.getEngine(); + int afterCoord = ce.getBucket().getBounds().x; + + Boolean isSameAfterMove = (beforeCoord == afterCoord); + + assertEquals("Coordinates will be the same after moving left then right",true,isSameAfterMove); + } + + // Test KeyPress passing through a key Event with left key being pressed + @Test + void testKeyPressed1() { + CatchComponent cc = new CatchComponent(); + + KeyEvent keyEvent = new KeyEvent(cc, 0, 0, 0, 0); + keyEvent.setKeyCode(KeyEvent.VK_LEFT); + cc.keyPressed(keyEvent); + + boolean isLeftTrue = cc.isLeftPressed(); + assertEquals("True, left key is pressed", true, isLeftTrue); + } + + // Test KeyPress passing through a key Event with right key being pressed + @Test + void testKeyPressed2() { + CatchComponent cc = new CatchComponent(); + + KeyEvent keyEvent = new KeyEvent(cc, 0, 0, 0, 0); + keyEvent.setKeyCode(KeyEvent.VK_RIGHT); + cc.keyPressed(keyEvent); + + boolean isRightTrue = cc.isRightPressed(); + assertEquals("True, right key is pressed", true, isRightTrue); + } + + // Test keyReleased() passing through key event with left key being pressed + @Test + void testKeyReleased1() { + CatchComponent cc = new CatchComponent(); + + KeyEvent keyEvent = new KeyEvent(cc, 0, 0, 0, 0); + keyEvent.setKeyCode(KeyEvent.VK_LEFT); + cc.keyPressed(keyEvent); + + // Action + cc.keyReleased(keyEvent); + + boolean isLeftTrue = cc.isLeftPressed(); + assertEquals("false, left key is released", false, isLeftTrue); + } + + // Test keyReleased() passing through key event with left key being pressed + @Test + void testKeyReleased2() { + CatchComponent cc = new CatchComponent(); + + KeyEvent keyEvent = new KeyEvent(cc, 0, 0, 0, 0); + keyEvent.setKeyCode(KeyEvent.VK_RIGHT); + cc.keyPressed(keyEvent); + + // Action + cc.keyReleased(keyEvent); + + boolean isRightTrue = cc.isLeftPressed(); + assertEquals("false, left right is released", false, isRightTrue); + } + +} diff --git a/Catch/src/CatchEngine.java b/Catch/src/CatchEngine.java index 1020e06..85d9165 100644 --- a/Catch/src/CatchEngine.java +++ b/Catch/src/CatchEngine.java @@ -17,41 +17,82 @@ public class CatchEngine private int speedDelay = 0; private int addBallDelay = MAX_BALL_DELAY; private int ballDelay = 0; + + + //Getter method for Balls + public Vector getBalls() { + return balls; + } + + //Setter method for Balls + public void setBalls(Vector balls) { + this.balls = balls; + } + + //Getter method for bucket + public Bucket getBucket() { + return bucket; + } + + //Setter method for bucket + public void setBucket(Bucket bucket) { + this.bucket = bucket; + } + + //Getter method for lives + public int getLives() { + return lives; + } + + //Setter method for lives + public void setLives(int lives) { + this.lives = lives; + } + + //Getter method for points + public int getPoints() { + return points; + } + //Setter method for points + public void setPoints(int points) { + this.points = points; + } + public CatchEngine() { - balls = new Vector(); - bucket = new Bucket(WIDTH/2, HEIGHT-Bucket.HEIGHT/2); + setBalls(new Vector()); + setBucket(new Bucket(WIDTH/2, HEIGHT-Bucket.HEIGHT/2)); } public void moveLeft() { - if(lives > 0) + if(getLives() > 0) { - bucket = bucket.moveLeft(); - if(bucket.getLocation().getX() < -Bucket.WIDTH/2) - bucket = bucket.moveTo(WIDTH+Bucket.WIDTH/2, (int)bucket.getLocation().getY()); + setBucket(bucket.moveLeft()); + if(getBucket().getLocation().getX() < -Bucket.WIDTH/2) + setBucket(bucket.moveTo(WIDTH+Bucket.WIDTH/2, (int)getBucket().getLocation().getY())); } } public void moveRight() { - if(lives > 0) + if(getLives() > 0) { - bucket = bucket.moveRight(); - if(bucket.getLocation().getX() > WIDTH+Bucket.WIDTH/2) - bucket = bucket.moveTo(-Bucket.WIDTH/2, (int)bucket.getLocation().getY()); + setBucket(bucket.moveRight()); + if(getBucket().getLocation().getX() > WIDTH+Bucket.WIDTH/2) + setBucket(getBucket().moveTo(-Bucket.WIDTH/2, (int)getBucket().getLocation().getY())); } } public void addBall() { - balls.add(new Ball((int)(Math.random()*WIDTH), 0, Math.random()>.5)); + getBalls().add(new Ball((int)(Math.random()*WIDTH), 0, Math.random()>.5)); } public void moveBalls() { - for(int i = 0; i < balls.size(); i++) + for(int i = 0; i < getBalls().size(); i++) { balls.add(i, balls.get(i).moveDown()); balls.remove(i+1); @@ -62,19 +103,19 @@ public void testBallCatch() { for(int i = 0; i < balls.size(); i++) { - if(bucket.contains(balls.get(i))) + if(getBucket().contains(balls.get(i))) { - if(balls.get(i).isGood()) - points++; + if(getBalls().get(i).isGood()) + setPoints(this.points+1); else - lives--; + setLives(this.lives-1); balls.remove(i); i--; } else if(balls.get(i).getLocation().getY() >= HEIGHT+Ball.RADIUS) { - if(balls.get(i).isGood()) - lives--; + if(getBalls().get(i).isGood()) + setLives(this.lives-1); balls.remove(i); i--; } @@ -83,7 +124,7 @@ else if(balls.get(i).getLocation().getY() >= HEIGHT+Ball.RADIUS) public void update() { - if(lives > 0) + if(getLives() > 0) { ballDelay++; speedDelay++; @@ -107,9 +148,9 @@ public void update() public void draw(Graphics g) { g.setColor(Color.BLACK); - g.drawString("Points: " + points, 10, 20); - g.drawString("Lives: " + lives, 10, 30); - if(lives <= 0) + g.drawString("Points: " + getPoints(), 10, 20); + g.drawString("Lives: " + getLives(), 10, 30); + if(getLives() <= 0) { g.setColor(Color.RED); g.drawString("You Lose", WIDTH/2-20, HEIGHT/2); @@ -117,6 +158,6 @@ public void draw(Graphics g) for(int i = 0; i < balls.size(); i++) balls.get(i).draw(g); - bucket.draw(g); + getBucket().draw(g); } } diff --git a/Catch/src/CatchEngineTest.java b/Catch/src/CatchEngineTest.java new file mode 100644 index 0000000..a0c68f2 --- /dev/null +++ b/Catch/src/CatchEngineTest.java @@ -0,0 +1,134 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; +import java.awt.Rectangle; +import java.util.Vector; +import org.junit.jupiter.api.Test; + +/* + * Added 8 getter/setter methods + * + * 5/8 original public methods tested + * + * Methods not under test + * -testBallCatch + * -update + * -draw + */ +class CatchEngineTest { + + //Test creation of object + @Test + void testCatchEngine1() { + CatchEngine ce = new CatchEngine(); + assertNotNull(ce); + } + + //Test properties of object after creation + @Test + void testCatchEngine2() { + CatchEngine ce = new CatchEngine(); + Vector balls = ce.getBalls(); + assertNotNull(balls); + } + + //Test properties of object after creation + @Test + void testCatchEngine3() { + CatchEngine ce = new CatchEngine(); + Bucket bucket = ce.getBucket(); + assertNotNull(bucket); + } + + //Test properties of object after creation + @Test + void testCatchEngine4() { + CatchEngine ce = new CatchEngine(); + int points = ce.getPoints(); + assertEquals("0 is default value of points ",0,points); + } + + //Test properties of object after creation + @Test + void testCatchEngine5() { + CatchEngine ce = new CatchEngine(); + int lives = ce.getLives(); + assertEquals("3 is default value of lives ",3,lives); + } + + + //Test adding a ball. addBall() method + @Test + void testAddBall() { + // Prep + CatchEngine ce = new CatchEngine(); + Vector balls = ce.getBalls(); + + // Grab before value + int beforeAddSize = balls.size(); + + // Add ball + ce.addBall(); + + // Grab after value + balls = ce.getBalls(); + int afterAddSize = balls.size(); + + Boolean isLargerAfterAdd = (afterAddSize > beforeAddSize); + + assertEquals("Ball size after adding a ball should be larger",true,isLargerAfterAdd); + } + + //Test moveBalls() method + @Test + void testMoveBalls() { + // Prep + CatchEngine ce = new CatchEngine(); + Vector balls = ce.getBalls(); + + // Grab before value + int beforeAddSize = balls.size(); + + ce.moveBalls(); + + // Grab after value + balls = ce.getBalls(); + int afterAddSize = balls.size(); + + Boolean isLargerAfterMove = (afterAddSize > beforeAddSize); + + assertEquals("Ball size should be the same after moving a ball",false,isLargerAfterMove); + + } + + //Test moveLeft() method + @Test + void testMoveLeft() { + CatchEngine ce = new CatchEngine(); + int beforeCoord = ce.getBucket().getBounds().x; + + // Action + ce.moveLeft(); + + int afterCoord = ce.getBucket().getBounds().x; + + Boolean isSmallerAfterMove = (beforeCoord > afterCoord); + + assertEquals("Coordinates will be smaller after moving left",true,isSmallerAfterMove); + } + + //Test moveRight() method + @Test + void testMoveRight() { + CatchEngine ce = new CatchEngine(); + int beforeCoord = ce.getBucket().getBounds().x; + + // Action + ce.moveRight(); + + int afterCoord = ce.getBucket().getBounds().x; + + Boolean isLargerAfterMove = (afterCoord > beforeCoord); + + assertEquals("Coordinates will be larger after moving right",true,isLargerAfterMove); + } +} diff --git a/Comet/src/Comet.java b/Comet/src/Comet.java index d34b7c0..ef91edd 100644 --- a/Comet/src/Comet.java +++ b/Comet/src/Comet.java @@ -57,6 +57,13 @@ public void move(Direction d) yV+= SPEED*d.y; } + public double getxVel() { + return xV; + } + + public double getyVel() { + return yV; + } public void update() { @@ -91,4 +98,4 @@ public void draw(Graphics g) } } -} \ No newline at end of file +} diff --git a/Comet/src/CometTests.java b/Comet/src/CometTests.java new file mode 100644 index 0000000..d620b65 --- /dev/null +++ b/Comet/src/CometTests.java @@ -0,0 +1,189 @@ +/** +*author: @Sean LaFleur +* +*provides testing for the Planet, Goal, and Comet classes +*of the comet game +*/ +import static org.junit.Assert.fail; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import static java.util.concurrent.TimeUnit.SECONDS; +import org.junit.Test; +import org.junit.Rule; +import org.junit.rules.Timeout; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.MethodSorters; +import org.junit.FixMethodOrder; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class CometTests { + + @Rule public Timeout globalTimeout = new Timeout(2, SECONDS); + + @Test public void planet1() { + Planet p1 = new Planet(100.0, 4, 4); + Planet p2 = new Planet(80.0, 3, 3); + + assertTrue("p1 does not contain p2", p1.contains(p2)); + } + + @Test public void planet2() { + Planet p1 = new Planet(10.0, 10, 10); + Planet p2 = new Planet(8.0, 2, 2); + + assertFalse("p2 contains p1", p2.contains(p1)); + } + + @Test public void planet3() { + Planet p1 = new Planet(10.0, 5, 5); + + assertEquals("The mass is not within .001 of 4188.79", 4188.79, p1.getMass(), .001); + } + + @Test public void goal1() { + Goal g = new Goal(10, 10, 10); + Comet c = new Comet(10, 3, 3); + + g.testReached(c); + + assertTrue("reached is false", g.reached); + } + + @Test public void goal2() { + Goal g = new Goal(10, 10, 10); + Comet c = new Comet(10, 1, 1); + + g.testReached(c); + + assertFalse("reached is true", g.reached); + } + + @Test public void goal3() { + Goal g = new Goal(10, 10, 10); + Comet c = new Comet(10, 3, 3); + + g.testReached(c); + + g.reset(); + + assertFalse("reached is still true after reset", g.reached); + } + + @Test public void comet1() { + Comet c = new Comet(3, 300, 250); + Planet p = new Planet(15, 298, 248); + + assertEquals("Distance is not within .01 of 2.83", 2.83, c.getDistance(p), .01); + } + + @Test public void comet2() { + Comet c = new Comet(3, 300, 250); + Planet p = new Planet(15, 138, 90); + + assertEquals("Distance is not withing .01 of 227.69", 227.69, c.getDistance(p), .01); + } + + @Test public void comet3() { + Comet c = new Comet(5, 289, 315); + + c.move(Direction.NORTH); + + assertEquals("Y velocity is not -.33", -.33, c.getyVel(), .01); + assertEquals("X velocity is not zero", 0, c.getxVel(), .01); + } + + @Test public void comet4() { + Comet c = new Comet(5, 289, 315); + + c.move(Direction.EAST); + + assertEquals("X velocity is not .33", .33, c.getxVel(), .01); + assertEquals("Y velocity is not 0", 0, c.getyVel(), .01); + } + + @Test public void comet5() { + Comet c = new Comet(5, 290, 320); + + c.move(Direction.WEST); + c.move(Direction.NORTH); + + assertEquals("X velocity is not -.33", -.33, c.getxVel(), .01); + assertEquals("Y veloctiy is not -.33", -.33, c.getyVel(), .01); + } + + @Test public void comet6() { + Comet c = new Comet(5, 300, 200); + + c.move(Direction.WEST); + c.move(Direction.WEST); + + assertEquals("X velocity is not -.66", -.66, c.getxVel(), .01); + } + + @Test public void comet7() { + Comet c = new Comet(); + + assertEquals("Mass is not 113.1 with radius of 3", 113.1, c.getMass(), .01); + } + + @Test public void comet8() { + Comet c = new Comet(5, 300, 200); + + c.move(Direction.EAST); + c.move(Direction.SOUTH); + c.decelerate(); + + assertEquals(.32, c.getxVel(), .01); + assertEquals(.32, c.getyVel(), .01); + } + + @Test public void comet9() { + Comet c = new Comet(5, 300, 200); + + c.move(Direction.EAST); + c.move(Direction.SOUTH); + + for(int i = 0; i < 10; i ++) { + c.decelerate(); + } + + assertEquals(.3, c.getxVel(), .01); + assertEquals(.3, c.getyVel(), .01); + } + + @Test public void comet10() { + Comet c = new Comet(5, 10, 30); + + c.move(Direction.EAST); + c.move(Direction.NORTH); + + c.update(); + + assertEquals("Velocity was not added onto 10", 10.33, c.x, .01); + assertEquals("Velocity was not subtracted from 30", 29.67, c.y, .01); + } + + @Test public void comet11() { + Comet c = new Comet(5, 1000, 1000); + + c.move(Direction.EAST); + c.move(Direction.NORTH); + + c.update(); + + assertEquals("x value was not within .01 of 953.78", 953.78, c.x, .01); + assertEquals("y value was not within .01 of 947.69", 947.69, c.y, .01); + } + + @Test public void comet12() { + Comet c = new Comet(3, 400, 250); + Planet p = new Planet(10, 370, 290); + + c.move(p); + + assertEquals("x velocity was not within .01 of -.03", -.03, c.getxVel(), .01); + assertEquals("y velocity was not within .01 of .04", .04, c.getyVel(), .01); + } +} diff --git a/Comet/src/Goal.java b/Comet/src/Goal.java index 104e68b..9314687 100644 --- a/Comet/src/Goal.java +++ b/Comet/src/Goal.java @@ -33,4 +33,4 @@ public void draw(Graphics g) g.setColor(Color.RED); g.drawOval((int)(x-radius+.5), (int)(y-radius+.5), (int)(radius*2+.5), (int)(radius*2+.5)); } -} \ No newline at end of file +} diff --git a/Comet/src/Play.java b/Comet/src/Play.java index eb829fb..b1f8760 100644 --- a/Comet/src/Play.java +++ b/Comet/src/Play.java @@ -27,136 +27,180 @@ public void update() if(!isMousePressed(1)) drag = false; - if((comet != null) && (!drag)) - { - if(isKeyPressed("UP")) - comet.move(Direction.NORTH); - if(isKeyPressed("DOWN")) - comet.move(Direction.SOUTH); - if(isKeyPressed("RIGHT")) - comet.move(Direction.EAST); - if(isKeyPressed("LEFT")) - comet.move(Direction.WEST); - - for(int i = 0; i < planets.size(); i++) - comet.move(planets.get(i)); - - comet.update(); - - for(int i = 0; i < goals.size(); i++) - goals.get(i).testReached(comet); - - for(int i = 0; i < planets.size(); i++) - { - if(comet.getDistance(planets.get(i)) < comet.radius+planets.get(i).radius) - { - comet = null; - for(i = 0; i < goals.size(); i++) - goals.get(i).reset(); - i = planets.size(); - } - } - } + cometDrag(); + if(win() && goals.size()>0) comet = null; - if(isMousePressed(1)) - { - if(!drag) - { - drag = true; - //mousePressed1 = false; - comet = new Comet(5, mouseX, mouseY); - } - else if(comet != null) - { - comet.xV = (comet.x-mouseX)/10.0; - comet.yV = (comet.y-mouseY)/10.0; - } - } - if(isMousePressed(3)) - { - mousePressed3 = false; - planets.add(new Planet(25, mouseX, mouseY)); - } - if(isMousePressed(2)) - { - mousePressed2 = false; - goals.add(new Goal(15, mouseX, mouseY)); - } - if(isKeyPressed("S")) - { - try - { - resetKeys(); - ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("levels"+File.separator+JOptionPane.showInputDialog(this, "Save As: ")))); - oos.writeObject(planets); - oos.writeObject(goals); - oos.close(); - } - catch(IOException ex) - { - } - } - if(isKeyPressed("L")) + mousePresses(); + + keyPresses(); + } + + } + + /** + * Methods mousePresses() through getGoals() are used to make a snarled/bulleted + * method look much cleaner + * + * mousePresses() is the methods that handles all of the mouse presses + */ + private void mousePresses() { + if(isMousePressed(1)) + { + if(!drag) { - try - { - resetKeys(); - Scanner kb = new Scanner(System.in); - ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("levels"+File.separator+JOptionPane.showInputDialog(this, "Load: ")))); - planets = (Vector)ois.readObject(); - goals = (Vector)ois.readObject(); - ois.close(); - comet = null; - for(int i = 0; i < goals.size(); i++) - goals.get(i).reset(); - } - catch(Exception ex) - { - } + drag = true; + //mousePressed1 = false; + comet = new Comet(5, mouseX, mouseY); } - if(isKeyPressed("ENTER")) + else if(comet != null) { - try - { - resetKeys(); - File levels = new File("levels"); - File[] level = levels.listFiles(); - ObjectInputStream ois = new ObjectInputStream(new FileInputStream(level[(int)(Math.random()*level.length)])); - planets = (Vector)ois.readObject(); - goals = (Vector)ois.readObject(); - ois.close(); - comet = null; - for(int i = 0; i < goals.size(); i++) - goals.get(i).reset(); - } - catch(Exception ex) - { - ex.printStackTrace(); - } + comet.xV = (comet.x-mouseX)/10.0; + comet.yV = (comet.y-mouseY)/10.0; } - if(isKeyPressed("N")) + } + + if(isMousePressed(3)) + { + mousePressed3 = false; + planets.add(new Planet(25, mouseX, mouseY)); + } + + if(isMousePressed(2)) + { + mousePressed2 = false; + goals.add(new Goal(15, mouseX, mouseY)); + } + } + + //handles all the different key presses + private void keyPresses() { + if(isKeyPressed("S")) + { + try { resetKeys(); - planets = new Vector(); - goals = new Vector(); + ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("levels"+File.separator+JOptionPane.showInputDialog(this, "Save As: ")))); + oos.writeObject(planets); + oos.writeObject(goals); + oos.close(); } - if(isKeyPressed("R")) + catch(IOException ex) { - resetKeys(); - comet = new Comet(5, mouseX, mouseY); - for(int i = 0; i < goals.size(); i++) - goals.get(i).reset(); } - if(isKeyPressed("H")) + } + + if(isKeyPressed("L")) + { + pressL(); + } + + if(isKeyPressed("ENTER")) + { + checkEnter(); + } + + if(isKeyPressed("N")) + { + resetKeys(); + planets = new Vector(); + goals = new Vector(); + } + + if(isKeyPressed("R")) + { + resetKeys(); + comet = new Comet(5, mouseX, mouseY); + for(int i = 0; i < goals.size(); i++) + goals.get(i).reset(); + } + + if(isKeyPressed("H")) + { + resetKeys(); + JOptionPane.showMessageDialog(this, "H: Help\nEnter: load a random level\nL: Load a level by file name\nS: Save a level by file name\nR: Restart the level\nN: new level\nArrows: apply thrust to the commet\nLeft Click: select the initial position, and volocity of the comet\nRight Click: Place planet\nScroll Click: Place Goal"); + } + } + + //Handles the long inner code for when L is pressed + private void pressL() { + try + { + resetKeys(); + Scanner kb = new Scanner(System.in); + ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("levels"+File.separator+JOptionPane.showInputDialog(this, "Load: ")))); + planets = (Vector)ois.readObject(); + goals = (Vector)ois.readObject(); + ois.close(); + comet = null; + for(int i = 0; i < goals.size(); i++) + goals.get(i).reset(); + } + catch(Exception ex) + { + } + } + + //Handles the long inner code for when enter is pressed + private void checkEnter() { + try + { + resetKeys(); + File levels = new File("levels"); + File[] level = levels.listFiles(); + ObjectInputStream ois = new ObjectInputStream(new FileInputStream(level[(int)(Math.random()*level.length)])); + planets = (Vector)ois.readObject(); + goals = (Vector)ois.readObject(); + ois.close(); + comet = null; + for(int i = 0; i < goals.size(); i++) + goals.get(i).reset(); + } + catch(Exception ex) + { + ex.printStackTrace(); + } + } + + //checks the movement controls keys during certain situations + private void cometDrag() { + if((comet != null) && (!drag)) + { + if(isKeyPressed("UP")) + comet.move(Direction.NORTH); + if(isKeyPressed("DOWN")) + comet.move(Direction.SOUTH); + if(isKeyPressed("RIGHT")) + comet.move(Direction.EAST); + if(isKeyPressed("LEFT")) + comet.move(Direction.WEST); + + for(int i = 0; i < planets.size(); i++) + comet.move(planets.get(i)); + + comet.update(); + + for(int i = 0; i < goals.size(); i++) + goals.get(i).testReached(comet); + + getGoals(); + } + } + + //resets the number of goals + private void getGoals() { + for(int i = 0; i < planets.size(); i++) + { + if(comet.getDistance(planets.get(i)) < comet.radius+planets.get(i).radius) { - resetKeys(); - JOptionPane.showMessageDialog(this, "H: Help\nEnter: load a random level\nL: Load a level by file name\nS: Save a level by file name\nR: Restart the level\nN: new level\nArrows: apply thrust to the commet\nLeft Click: select the initial position, and volocity of the comet\nRight Click: Place planet\nScroll Click: Place Goal"); + comet = null; + for(i = 0; i < goals.size(); i++) + goals.get(i).reset(); + i = planets.size(); } } - } public boolean win() diff --git a/FallDown/src/Ball.java b/FallDown/src/Ball.java index 1207d11..147aa3a 100644 --- a/FallDown/src/Ball.java +++ b/FallDown/src/Ball.java @@ -7,6 +7,46 @@ public class Ball private int x, y; private double dx, dy; + //Getter method for x + protected int getX() { + return x; + } + + //Setter method for x + protected void setX(int x) { + this.x = x; + } + + //Getter method for y + protected int getY() { + return y; + } + + //Setter method for x + protected void setY(int y) { + this.y = y; + } + + //Getter method for dx + protected double getDx() { + return dx; + } + + //Setter method for dx + protected void setDx(double dx) { + this.dx = dx; + } + + //Getter method for dy + protected double getDy() { + return dy; + } + + //Setter method for dy + protected void setDy(double dy) { + this.dy = dy; + } + public Ball(int x, int y) { this(x,y,0,0); @@ -14,30 +54,30 @@ public Ball(int x, int y) public Ball(int x, int y, double dx, double dy) { - this.x = x; - this.y = y; - this.dx = dx; - this.dy = dy; + setX(x); + setY(y); + setDx(dx); + setDy(dy); } public Ball moveLeft() { - return new Ball(x-SPEED, y, dx, dy); + return new Ball(getX()-SPEED, getY(), getDx(), getDy()); } public Ball moveRight() { - return new Ball(x+SPEED, y, dx, dy); + return new Ball(getX()+SPEED, getY(), getDx(), getDy()); } public Ball accelerate(double ax, double ay) { - return new Ball(x, y, dx+ax, dy+ay); + return new Ball(getX(), getY(), getDx()+ax, getDy()+ay); } public Ball setVelocity(double dx, double dy) { - return new Ball(x, y, dx, dy); + return new Ball(getX(), getY(), dx, dy); } public Ball setPosition(int x, int y) @@ -47,27 +87,27 @@ public Ball setPosition(int x, int y) public Ball move(int dx, int dy) { - return new Ball(x+dx, y+dy, 0, 0); + return new Ball(getX()+dx, getY()+dy, 0, 0); } public Ball move() { - return new Ball(x+(int)dx, y+(int)dy, dx, dy); + return new Ball(getX()+(int)getDx(), getY()+(int)getDy(), getDx(), getDy()); } public Rectangle getBounds() { - return new Rectangle(x-RADIUS, y-RADIUS, RADIUS*2, RADIUS*2); + return new Rectangle(getX()-RADIUS, getY()-RADIUS, RADIUS*2, RADIUS*2); } public Point getLocation() { - return new Point(x,y); + return new Point(getX(),getY()); } public void draw(Graphics g) { g.setColor(Color.BLACK); - g.fillOval(x-RADIUS, y-RADIUS, RADIUS*2, RADIUS*2); + g.fillOval(getX()-RADIUS, getY()-RADIUS, RADIUS*2, RADIUS*2); } } \ No newline at end of file diff --git a/FallDown/src/BallTest.java b/FallDown/src/BallTest.java new file mode 100644 index 0000000..dfc8042 --- /dev/null +++ b/FallDown/src/BallTest.java @@ -0,0 +1,203 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; + +import java.awt.Point; +import java.awt.Rectangle; + +import org.junit.jupiter.api.Test; + +class BallTest { + + //Test creation of object + @Test + void testBallIntInt1() { + Ball ball = new Ball(1,0); + assertNotNull(ball); + } + + //Test correct class properties after creation of object + @Test + void testBallIntInt2() { + Ball ball = new Ball(1,3); + + int x = ball.getX(); + int y = ball.getY(); + double dx = ball.getDx(); + double dy = ball.getDy(); + + assertEquals("x = 1 ",1,x); + assertEquals("y = 3 ",3,y); + assertEquals("dx = 0 ",(int)0,(int)dx); + assertEquals("dy = 0 ",(int)0,(int)dy); + } + + //Test creation of object + @Test + void testBallIntIntDoubleDouble1() { + Ball ball = new Ball(1,5,2,3); + assertNotNull(ball); + } + + //Test correct class properties after creation of object + @Test + void testBallIntIntDoubleDouble2() { + Ball ball = new Ball(1,5,2,3); + + int x = ball.getX(); + int y = ball.getY(); + double dx = ball.getDx(); + double dy = ball.getDy(); + + assertEquals("x = 1 ",1,x); + assertEquals("y = 5 ",5,y); + assertEquals("dx = 2 ",(int)2,(int)dx); + assertEquals("dy = 3 ",(int)3,(int)dy); + } + + //Test moveLeft() method + @Test + void testMoveLeft() { + Ball ball = new Ball(11,5,2,3); + ball = ball.moveLeft(); + + int x = ball.getX(); + int y = ball.getY(); + double dx = ball.getDx(); + double dy = ball.getDy(); + + assertEquals("x = 1 ",1,x); + assertEquals("y = 5 ",5,y); + assertEquals("dx = 2 ",(int)2,(int)dx); + assertEquals("dy = 3 ",(int)3,(int)dy); + } + + //Test moveRight() method + @Test + void testMoveRight() { + Ball ball = new Ball(11,5,2,3); + ball = ball.moveRight(); + + int x = ball.getX(); + int y = ball.getY(); + double dx = ball.getDx(); + double dy = ball.getDy(); + + assertEquals("x = 21 ",21,x); + assertEquals("y = 5 ",5,y); + assertEquals("dx = 2 ",(int)2,(int)dx); + assertEquals("dy = 3 ",(int)3,(int)dy); + } + + //Test accelerate method + @Test + void testAccelerate() { + Ball ball = new Ball(11,5,2,3); + ball = ball.accelerate(1, 1); + + int x = ball.getX(); + int y = ball.getY(); + double dx = ball.getDx(); + double dy = ball.getDy(); + + assertEquals("x = 11 ",11,x); + assertEquals("y = 5 ",5,y); + assertEquals("dx = 3 ",(int)3,(int)dx); + assertEquals("dy = 4 ",(int)4,(int)dy); + } + + //Test setVelocity method + @Test + void testSetVelocity() { + Ball ball = new Ball(11,5,2,3); + ball = ball.setVelocity(1, 2); + + int x = ball.getX(); + int y = ball.getY(); + double dx = ball.getDx(); + double dy = ball.getDy(); + + assertEquals("x = 11 ",11,x); + assertEquals("y = 5 ",5,y); + assertEquals("dx = 1 ",(int)1,(int)dx); + assertEquals("dy = 2 ",(int)2,(int)dy); + } + + //Test setPosition() method + @Test + void testSetPosition() { + Ball ball = new Ball(11,5,2,3); + ball = ball.setPosition(1, 2); + + int x = ball.getX(); + int y = ball.getY(); + double dx = ball.getDx(); + double dy = ball.getDy(); + + assertEquals("x = 1 ",1,x); + assertEquals("y = 2 ",2,y); + assertEquals("dx = 0 ",(int)0,(int)dx); + assertEquals("dy = 0 ",(int)0,(int)dy); + } + + //Test move() method + @Test + void testMoveIntInt() { + Ball ball = new Ball(11,5,2,3); + ball = ball.move(1, 2); + + int x = ball.getX(); + int y = ball.getY(); + double dx = ball.getDx(); + double dy = ball.getDy(); + + assertEquals("x = 12 ",12,x); + assertEquals("y = 7 ",7,y); + assertEquals("dx = 0 ",(int)0,(int)dx); + assertEquals("dy = 0 ",(int)0,(int)dy); + } + + //Test move() method + @Test + void testMove() { + Ball ball = new Ball(11,5,2,3); + ball = ball.move(); + + int x = ball.getX(); + int y = ball.getY(); + double dx = ball.getDx(); + double dy = ball.getDy(); + + assertEquals("x = 13 ",13,x); + assertEquals("y = 8 ",8,y); + assertEquals("dx = 2 ",(int)2,(int)dx); + assertEquals("dy = 3 ",(int)3,(int)dy); + } + + //Test getBounds() method + @Test + void testGetBounds() { + Ball ball = new Ball(11,5,2,3); + + Rectangle r = ball.getBounds(); + + int x = r.x; + int y = r.y; + + assertEquals("x = 1 ",1,x); + assertEquals("y = -5 ",-5,y); + } + + //Test getLocation() method + @Test + void testGetLocation() { + Ball ball = new Ball(11,5,2,3); + + Point p = ball.getLocation(); + + int x = p.x; + int y = p.y; + + assertEquals("x = 11 ",11,x); + assertEquals("y = 5 ",5,y); + } +} diff --git a/FallDown/src/Brick.java b/FallDown/src/Brick.java index c924f32..f8609a4 100644 --- a/FallDown/src/Brick.java +++ b/FallDown/src/Brick.java @@ -6,21 +6,41 @@ public class Brick public static final int HEIGHT = 25; private int x, y; + + //Getter method for Y + protected int getY() { + return y; + } + + //Setter method for Y + protected void setY(int y) { + this.y = y; + } + + //Getter method for X + protected int getX() { + return x; + } + + //Setter method for X + protected void setX(int x) { + this.x = x; + } public Brick(int x, int y) { - this.x = x; - this.y = y; + setX(x); + setY(y); } public Brick move(int dx, int dy) { - return new Brick(x+dx, y+dy); + return new Brick(getX()+dx, getY()+dy); } public Rectangle getBounds() { - return new Rectangle(x-WIDTH/2, y-HEIGHT/2, WIDTH, HEIGHT); + return new Rectangle(getX()-WIDTH/2, getY()-HEIGHT/2, WIDTH, HEIGHT); } public boolean intersects(Ball b) @@ -39,12 +59,12 @@ public Ball affect(Ball b) public Point getLocation() { - return new Point(x,y); + return new Point(getX(),getY()); } public void draw(Graphics g) { g.setColor(Color.RED); - g.fillRect(x-WIDTH/2, y-HEIGHT/2, WIDTH-1, HEIGHT-1); + g.fillRect(getX()-WIDTH/2, getY()-HEIGHT/2, WIDTH-1, HEIGHT-1); } } \ No newline at end of file diff --git a/FallDown/src/BrickTest.java b/FallDown/src/BrickTest.java new file mode 100644 index 0000000..0587be3 --- /dev/null +++ b/FallDown/src/BrickTest.java @@ -0,0 +1,89 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; + +import java.awt.Point; +import java.awt.Rectangle; + +import org.junit.jupiter.api.Test; + +class BrickTest { + + //Test creation of object + @Test + void testBrick1() { + Brick brick = new Brick(1,0); + assertNotNull(brick); + } + + //Test correct class properties after creation of object + @Test + void testBrick2() { + Brick brick = new Brick(1,0); + + int x = brick.getX(); + int y = brick.getY(); + + assertEquals("x = 1 ",1,x); + assertEquals("y = 0 ",0,y); + } + + //Test Move method + @Test + void testMove() { + Brick brick = new Brick(1,0); + + brick = brick.move(1, 3); + + int x = brick.getX(); + int y = brick.getY(); + + assertEquals("x = 2 ",2,x); + assertEquals("y = 3 ",3,y); + } + + //Test GetBounds method + @Test + void testGetBounds() { + Brick brick = new Brick(1,0); + Rectangle r = brick.getBounds(); + + int x = r.x; + int y = r.y; + + assertEquals("x = -24 ",-24,x); + assertEquals("y = -12 ",-12,y); + } + + //Test passing case of intersect() + @Test + void testIntersects1() { + Brick brick = new Brick(1,0); + + boolean isIntersect = brick.intersects( new Ball(0,0)); + assertEquals("intersects, true",true,isIntersect); + } + + //Test failing case of intersect() + @Test + void testIntersects2() { + Brick brick = new Brick(50,20); + + boolean isIntersect = brick.intersects( new Ball(0,0)); + assertEquals("!intersects, !true",false,isIntersect); + } + + //Test getLocation method + @Test + void testGetLocation() { + Brick brick = new Brick(1,0); + + Point p = brick.getLocation(); + + int x = p.x; + int y = p.y; + + assertEquals("x = 1 ",1,x); + assertEquals("y = 0 ",0,y); + } + +} diff --git a/FallDown/src/FallDownComponent.java b/FallDown/src/FallDownComponent.java index b04dbca..ccaea23 100644 --- a/FallDown/src/FallDownComponent.java +++ b/FallDown/src/FallDownComponent.java @@ -7,6 +7,22 @@ public class FallDownComponent extends JComponent implements KeyListener, Runnab private FallDownEngine engine; private boolean leftPressed, rightPressed; + protected boolean isLeftPressed() { + return leftPressed; + } + + protected void setLeftPressed(boolean leftPressed) { + this.leftPressed = leftPressed; + } + + protected boolean isRightPressed() { + return rightPressed; + } + + protected void setRightPressed(boolean rightPressed) { + this.rightPressed = rightPressed; + } + public FallDownComponent() { super(); @@ -45,9 +61,9 @@ public void paint(Graphics g) public void update() { - if(leftPressed) + if(isLeftPressed()) engine.moveLeft(); - if(rightPressed) + if(isRightPressed()) engine.moveRight(); engine.update(); } @@ -55,17 +71,17 @@ public void update() public void keyPressed(KeyEvent ke) { if(ke.getKeyCode() == KeyEvent.VK_LEFT) - leftPressed = true; + setLeftPressed(true); else if(ke.getKeyCode() == KeyEvent.VK_RIGHT) - rightPressed = true; + setRightPressed(true); } public void keyReleased(KeyEvent ke) { if(ke.getKeyCode() == KeyEvent.VK_LEFT) - leftPressed = false; + setLeftPressed(false); else if(ke.getKeyCode() == KeyEvent.VK_RIGHT) - rightPressed = false; + setRightPressed(false); } public void keyTyped(KeyEvent ke) diff --git a/FallDown/src/FallDownComponentTest.java b/FallDown/src/FallDownComponentTest.java new file mode 100644 index 0000000..1a798af --- /dev/null +++ b/FallDown/src/FallDownComponentTest.java @@ -0,0 +1,121 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; + +import java.awt.event.KeyEvent; + +import org.junit.jupiter.api.Test; + +class FallDownComponentTest { + + //Test creating object + @Test + void testFallDownComponent1() { + FallDownComponent fdc = new FallDownComponent(); + assertNotNull(fdc); + } + + //Test Update method + @Test + void testUpdate1() { + FallDownComponent fdc = new FallDownComponent(); + fdc.setLeftPressed(true); + + fdc.update(); + + FallDownEngine fde = new FallDownEngine(); + + Ball b1 = fde.getBall(); + + fde.moveLeft(); + + Ball b2 = fde.getBall(); + + boolean ballIsBigger = b1.getX() > b2.getX(); + + assertEquals("b1 > b2 ",true,ballIsBigger); + } + + //Test Update method + @Test + void testUpdate2() { + FallDownComponent fdc = new FallDownComponent(); + fdc.setRightPressed(true); + + fdc.update(); + + FallDownEngine fde = new FallDownEngine(); + + Ball b1 = fde.getBall(); + + fde.moveRight(); + + Ball b2 = fde.getBall(); + + boolean ballIsBigger = b1.getX() > b2.getX(); + + assertEquals("b1 > b2 ",false,ballIsBigger); + } + + //Test keyPressed method + @Test + void testKeyPressed1() { + FallDownComponent fdc = new FallDownComponent(); + + KeyEvent keyEvent = new KeyEvent(fdc, 0, 0, 0, 0); + keyEvent.setKeyCode(KeyEvent.VK_LEFT); + + fdc.keyPressed(keyEvent); + + boolean isLeftPressed = fdc.isLeftPressed(); + + assertEquals("left is pressed. true",true,isLeftPressed); + } + + //Test keyPressed method + @Test + void testKeyPressed2() { + FallDownComponent fdc = new FallDownComponent(); + + KeyEvent keyEvent = new KeyEvent(fdc, 0, 0, 0, 0); + keyEvent.setKeyCode(KeyEvent.VK_RIGHT); + + fdc.keyPressed(keyEvent); + + boolean isRightPressed = fdc.isRightPressed(); + + assertEquals("right is pressed. true",true,isRightPressed); + } + + //Test keyReleased method + @Test + void testKeyReleased1() { + FallDownComponent fdc = new FallDownComponent(); + fdc.setLeftPressed(true); + + KeyEvent keyEvent = new KeyEvent(fdc, 0, 0, 0, 0); + keyEvent.setKeyCode(KeyEvent.VK_LEFT); + + fdc.keyReleased(keyEvent); + + boolean isLeftPressed = fdc.isLeftPressed(); + + assertEquals("left is no longer pressed. true",false,isLeftPressed); + } + + //Test keyReleased method + @Test + void testKeyReleased2() { + FallDownComponent fdc = new FallDownComponent(); + fdc.setRightPressed(true); + + KeyEvent keyEvent = new KeyEvent(fdc, 0, 0, 0, 0); + keyEvent.setKeyCode(KeyEvent.VK_RIGHT); + + fdc.keyReleased(keyEvent); + + boolean isRightPressed = fdc.isRightPressed(); + + assertEquals("right is no longer pressed. true",false,isRightPressed); + } + +} diff --git a/FallDown/src/FallDownEngine.java b/FallDown/src/FallDownEngine.java index df3fa1f..ac6e02f 100644 --- a/FallDown/src/FallDownEngine.java +++ b/FallDown/src/FallDownEngine.java @@ -15,10 +15,58 @@ public class FallDownEngine private int brickDelay = 0; private int speedDelay = 0; private int points = -1; + + protected Vector getBricks() { + return bricks; + } + + protected void setBricks(Vector bricks) { + this.bricks = bricks; + } + + protected Ball getBall() { + return ball; + } + + protected void setBall(Ball ball) { + this.ball = ball; + } + + protected int getBrickSpeed() { + return brickSpeed; + } + + protected void setBrickSpeed(int brickSpeed) { + this.brickSpeed = brickSpeed; + } + + protected int getBrickDelay() { + return brickDelay; + } + + protected void setBrickDelay(int brickDelay) { + this.brickDelay = brickDelay; + } + + protected int getSpeedDelay() { + return speedDelay; + } + + protected void setSpeedDelay(int speedDelay) { + this.speedDelay = speedDelay; + } + + protected int getPoints() { + return points; + } + + protected void setPoints(int points) { + this.points = points; + } public FallDownEngine() { - ball = new Ball(WIDTH/2, HEIGHT/2); + setBall(new Ball(WIDTH/2, HEIGHT/2)); createBrickLayer(); } @@ -32,7 +80,7 @@ public void createBrickLayer() bricks.add(new Brick(i*Brick.WIDTH+Brick.WIDTH/2, HEIGHT+Brick.HEIGHT)); } } - points++; + setPoints(getPoints()+1); } public void removeOldBricks() @@ -60,43 +108,43 @@ public void affectBall() { for(int i = 0; i < bricks.size(); i++) { - ball = bricks.get(i).affect(ball); + setBall( bricks.get(i).affect(ball) ); } - ball = ball.accelerate(0, GRAVITY); - if(ball.getLocation().getY() > HEIGHT) - ball = ball.setPosition((int)ball.getLocation().getX(), HEIGHT); + setBall(getBall().accelerate(0, GRAVITY)); + if(getBall().getLocation().getY() > HEIGHT) + setBall( ball.setPosition((int)ball.getLocation().getX(), HEIGHT) ); } public void moveLeft() { - ball = ball.moveLeft(); + setBall(getBall().moveLeft()); while(ball.getLocation().getX() < 0) - ball = ball.moveRight(); + setBall( ball.moveRight() ); } public void moveRight() { - ball = ball.moveRight(); + setBall(ball.moveRight()); while(ball.getLocation().getX() > WIDTH) - ball = ball.moveLeft(); + setBall( ball.moveLeft() ); } public void update() { - if(ball.getLocation().getY() >= -Ball.RADIUS) + if(getBall().getLocation().getY() >= -Ball.RADIUS) { - ball = ball.move(); + setBall( ball.move() ); moveBricks(); removeOldBricks(); - brickDelay = brickDelay+brickSpeed; - if(brickDelay > BRICK_LAYER_DELAY) + setBrickDelay(getBrickDelay()+getBrickSpeed()); + if(getBrickDelay() > BRICK_LAYER_DELAY) { - brickDelay = 0; - speedDelay++; - if(speedDelay > SPEED_UP_DELAY) + setBrickDelay(0); + setSpeedDelay(getSpeedDelay() + 1); + if(getSpeedDelay() > SPEED_UP_DELAY) { - speedDelay = 0; - brickSpeed++; + setSpeedDelay(0); + setBrickSpeed(getBrickSpeed() + 1); } createBrickLayer(); } diff --git a/FallDown/src/FallDownEngineTest.java b/FallDown/src/FallDownEngineTest.java new file mode 100644 index 0000000..4ba306d --- /dev/null +++ b/FallDown/src/FallDownEngineTest.java @@ -0,0 +1,80 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class FallDownEngineTest { + + //Test creation of object + @Test + void testFallDownEngine1() { + FallDownEngine fde = new FallDownEngine(); + assertNotNull(fde); + } + + //Test correct class properties after creation of object + @Test + void testFallDownEngine2() { + FallDownEngine fde = new FallDownEngine(); + + int points = fde.getPoints(); + assertEquals("points = 0 ",0,points); + } + + //Test createBrickLayer method + @Test + void testCreateBrickLayer() { + FallDownEngine fde = new FallDownEngine(); + + int points = fde.getPoints(); + assertEquals("points = 0 ",0,points); + } + + //Test moveBricks() method + @Test + void testMoveBricks() { + FallDownEngine fde = new FallDownEngine(); + + Brick brick1 = fde.getBricks().elementAt(0); + + fde.moveBricks(); + + Brick brick2 = fde.getBricks().elementAt(0); + + boolean brickIsBigger = brick1.getY() > brick2.getY(); + assertEquals("brick 1 > brick2 ",true,brickIsBigger); + } + + //Test moveLeft() method + @Test + void testMoveLeft() { + FallDownEngine fde = new FallDownEngine(); + + Ball b1 = fde.getBall(); + + fde.moveLeft(); + + Ball b2 = fde.getBall(); + + boolean ballIsBigger = b1.getX() > b2.getX(); + + assertEquals("b1 > b2 ",true,ballIsBigger); + } + + //Test moveRigHt() method + @Test + void testMoveRight() { + FallDownEngine fde = new FallDownEngine(); + + Ball b1 = fde.getBall(); + + fde.moveRight(); + + Ball b2 = fde.getBall(); + + boolean ballIsBigger = b1.getX() > b2.getX(); + + assertEquals("b1 !> b2 ",false,ballIsBigger); + } + +} diff --git a/Frogger/src/Car.java b/Frogger/src/Car.java index c8832b8..190a11b 100644 --- a/Frogger/src/Car.java +++ b/Frogger/src/Car.java @@ -15,6 +15,30 @@ public Car(int x, int y, int speed, Color color) this.color = color; } + public int getSpeed() { + return this.speed; + } + + public Color getColor() { + return this.color; + } + + public int getx() { + return this.x; + } + + public int gety() { + return this.y; + } + + public int getW() { + return WIDTH; + } + + public int getH() { + return HEIGHT; + } + public Car moveLeft() { return new Car(x-speed,y,speed,color); @@ -38,4 +62,4 @@ public void draw(Graphics g) g.fillRect(x,y,WIDTH,HEIGHT); } } -} \ No newline at end of file +} diff --git a/Frogger/src/Cronometro.java b/Frogger/src/Cronometro.java index 4045146..04b0947 100644 --- a/Frogger/src/Cronometro.java +++ b/Frogger/src/Cronometro.java @@ -22,56 +22,52 @@ * @author Fabio */ public class Cronometro { - private JFrame form; - private JLabel label; - private JPanel panelButtons; - - private Timer timer; - private long startTime; - public Cronometro() { - form = new JFrame("Time"); - form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - form.setSize(220, 140); + private JFrame form; + private JLabel label; + private JPanel panelButtons; + + private Timer timer; + private long startTime; + public Cronometro() { + form = new JFrame("Time"); + form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + form.setSize(220, 140); form.setResizable(false); - - label = new JLabel("0:00:00.0"); - label.setFont (new Font("SansSerif", Font.BOLD, 30)); - label.setHorizontalAlignment(JLabel.CENTER); - panelButtons = new JPanel(new GridLayout(1, 2)); -// panelButtons.add(buttonStart); -// panelButtons.add(buttonStop); - form.add(label, BorderLayout.CENTER); - form.add(panelButtons, BorderLayout.SOUTH); - timer = new Timer(100, new ActionListener() { - public void actionPerformed(ActionEvent e) { - long diffTime = System.currentTimeMillis() - startTime; - int decSeconds = (int)(diffTime % 1000 / 100); - int seconds = (int)(diffTime / 1000 % 60); - int minutes = (int)(diffTime / 60000 % 60); - int hours = (int)(diffTime / 3600000); - String s = String.format("%d:%02d:%02d.%d", hours, minutes, seconds, decSeconds); - label.setText(s); - } - }); - - - form.setVisible (true); -} - + + label = new JLabel("0:00:00.0"); + label.setFont (new Font("SansSerif", Font.BOLD, 30)); + label.setHorizontalAlignment(JLabel.CENTER); + panelButtons = new JPanel(new GridLayout(1, 2)); +// panelButtons.add(buttonStart); +// panelButtons.add(buttonStop); + form.add(label, BorderLayout.CENTER); + form.add(panelButtons, BorderLayout.SOUTH); + timer = new Timer(100, new ActionListener() { + public void actionPerformed(ActionEvent e) { + long diffTime = System.currentTimeMillis() - startTime; + int decSeconds = (int)(diffTime % 1000 / 100); + int seconds = (int)(diffTime / 1000 % 60); + int minutes = (int)(diffTime / 60000 % 60); + int hours = (int)(diffTime / 3600000); + String s = String.format("%d:%02d:%02d.%d", hours, minutes, seconds, decSeconds); + label.setText(s); + } + }); + + + form.setVisible (true); +} + public void inizio() - { - - startTime = System.currentTimeMillis(); - timer.start(); - + startTime = System.currentTimeMillis(); + timer.start(); } - + public void stop() { - timer.stop(); - + timer.stop(); } - - + + } diff --git a/Frogger/src/Frog.java b/Frogger/src/Frog.java index c9a2522..bda5fc9 100644 --- a/Frogger/src/Frog.java +++ b/Frogger/src/Frog.java @@ -1,69 +1,79 @@ import java.awt.*; -public class Frog -{ +public class Frog { public static final int RADIUS = 5; - public static final Color COLOR = new Color(0,128,0); + public static final Color COLOR = new Color(0, 128, 0); public static final int SPEED = 4; - private int x,y; + private int x, y; private Rectangle bounds; - public Frog(int x,int y, Rectangle bounds) - { + public Frog(int x, int y, Rectangle bounds) { this.x = x; this.y = y; this.bounds = bounds; } - public Frog move(int dx, int dy) - { - Frog newFrog = new Frog(x+dx, y+dy, bounds); - if(bounds.contains(newFrog.getBounds())) + public Frog move(int dx, int dy) { + Frog newFrog = new Frog(x + dx, y + dy, bounds); + if (bounds.contains(newFrog.getBounds())) return newFrog; else return this; } - public Frog moveTo(int x, int y) - { - return new Frog(x,y,bounds); + public Frog moveTo(int x, int y) { + return new Frog(x, y, bounds); } - public Frog moveUp() - { - return move(0,-SPEED); + public Frog moveUp() { + return move(0, -SPEED); } - public Frog moveDown() - { - return move(0,SPEED); + public Frog moveDown() { + return move(0, SPEED); } - public Frog moveLeft() - { + public Frog moveLeft() { return move(-SPEED, 0); } - public Frog moveRight() - { + public Frog moveRight() { return move(SPEED, 0); } - public Rectangle getBounds() - { - return new Rectangle(x-RADIUS, y-RADIUS, RADIUS*2, RADIUS*2); + public Rectangle getBounds() { + return new Rectangle(x - RADIUS, y - RADIUS, RADIUS * 2, RADIUS * 2); } - public void draw(Graphics g) - { + public int getX() { + return this.x; + } + + public int getY() { + return this.y; + } + + public void setX(int x) { + this.x = x; + } + + public void setY(int y) { + this.y = y; + } + + public Rectangle getRectangle() { + return this.bounds; + } + + public void draw(Graphics g) { + g.setColor(COLOR); + g.fillOval(x - RADIUS, y - RADIUS, RADIUS * 2, RADIUS * 2); + } + + public void drawLevel(Graphics g, int levelNumber) { + String level = "LEVEL " + levelNumber; g.setColor(COLOR); - g.fillOval(x-RADIUS, y-RADIUS, RADIUS*2, RADIUS*2); + g.drawString(level, 300, 400); } - public void drawLevel(Graphics g,int levelNumber) - { - String level="LEVEL "+levelNumber; - g.setColor(COLOR); - g.drawString(level,300,400); - } -} \ No newline at end of file +} diff --git a/Frogger/src/FroggerComponent.java b/Frogger/src/FroggerComponent.java index c27f6e4..5bfea91 100644 --- a/Frogger/src/FroggerComponent.java +++ b/Frogger/src/FroggerComponent.java @@ -13,7 +13,7 @@ public class FroggerComponent extends JComponent implements KeyListener, Runnabl public static Vector levels = new Vector(); private int level = 0; //private JLabel l; - private int life=5; + private int life = 5; private int score = 0; private int highscore = readPreference(); private static final String HIGHSCORE = "highscore1"; @@ -31,25 +31,70 @@ public FroggerComponent() { run.start(); } - public void levelInit() { + public int getLevel() { + return this.level; + } + + public void setLevel(int level) { + this.level = level; + } + + public int getLife() { + return this.life; + } + + public void setLife(int life) { + this.life = life; + } + + public int getScore() { + return this.score; + } + + public void setScore(int score) { + this.score = score; + } + + public int getHighscore() { + return this.highscore; + } + + public void setHighscore(int highscore) { + this.highscore = highscore; + } + + public void level1Init() { - levels.add(new FroggerLevel( new int[]{1, 2, 1}, new String[]{" YYYY L ", " BB RR ", " RR MM "})); + } + + public void level2Init() { + levels.add(new FroggerLevel( new int[]{1, 5, 1}, new String[]{"LLL GGG RRR", "RRR MMM", "RR MM LL RR RRR"})); + } + + public void level3Init() { levels.add(new FroggerLevel( new int[]{2, 1, 2, 1, 2}, new String[]{"RR L ", "BB CCCCCC RR ", " RR MM ", "MMM MMM ", "RR L "})); + } + + public void level4Init() { + levels.add(new FroggerLevel( new int[]{1, 2, 1, 3, 1}, new String[]{"LLL MM RRR ", "RRR GGGG LL MMM ", "RR MM LL RR RRR ", " LLLL BBBB MMM ", " MMMMM LLLL MMMM "})); + } + + public void level5Init() { levels.add(new FroggerLevel( new int[]{1, 1, 1, 1, 1, 1, 1}, @@ -57,30 +102,54 @@ public void levelInit() { "MMM MMM MMM ", "RR RR RR ", "Y Y B B B B ", "MMM MMM MMM L ", " BBB BBB L BBB"})); + } + + public void level6Init() { levels.add(new FroggerLevel( new int[]{1, 1, 2, 2, 3, 2, 1}, new String[]{"RR LL B ", "LLL BB R ", "RRR LL ", "MMM MM ", "L L ", "RR L M ", "RRR BL "})); + } + + public void level7Init() { levels.add(new FroggerLevel( new int[]{1, 2, 3, 4, 5}, new String[]{"BB L RRR M", "RR B MMM L ", "MM LL BB ", "M L BB ", "LL "})); + } + + public void level8Init() { + levels.add(new FroggerLevel( new int[]{2, 3, 1, 2, 3, 2}, new String[]{"LLL LLL ", " RRRR RRRR ", "RR BB LL MM ", "LLL BB RR ", "MMMMM RBRBR LLLL ", " BBB MMMM LLLLL"})); + } + public void level9Init() { levels.add(new FroggerLevel( new int[]{1, 3, 4, 3, 1, 2}, new String[]{"MMM LL RR ", "BBB LL RR ", "BB LL ", "BBB MM MM ", "MMM LL B R L B " })); + } + + public void levelInit() { + level1Init(); + level2Init(); + level3Init(); + level4Init(); + level5Init(); + level6Init(); + level7Init(); + level8Init(); + level9Init(); } public void run() { @@ -174,10 +243,10 @@ public void update() throws InterruptedException { PlayFrogger.lab2.setText(hearts); } } -// +// // if(levels.get(level)) // { -// +// // } if (level == 0) { // JLabel l = new JLabel(); @@ -204,7 +273,7 @@ public void update() throws InterruptedException { PlayFrogger.lab1.setForeground(Color.RED); PlayFrogger.lab3.setLocation((369 - PlayFrogger.lab3.getWidth()), 56); PlayFrogger.lab3.setForeground(Color.BLUE); - + } else if (level == 2) { checkHighscore(); @@ -284,7 +353,7 @@ public void update() throws InterruptedException { PlayFrogger.lab3.setForeground(Color.BLUE); } - + } public void keyPressed(KeyEvent ke) { @@ -318,18 +387,16 @@ public void keyReleased(KeyEvent ke) { public void keyTyped(KeyEvent ke) { } - + private void checkHighscore() { if (score > highscore) { highscore = score; - } PlayFrogger.lab4.setText("HIGHSCORE: " + Integer.toString(highscore)); PlayFrogger.lab4.setLocation((369 - PlayFrogger.lab4.getWidth()), 46); PlayFrogger.lab4.setForeground(Color.RED); savePreference(highscore); - } public void savePreference(int highscore) { diff --git a/Frogger/src/FroggerTests.java b/Frogger/src/FroggerTests.java new file mode 100644 index 0000000..3658aae --- /dev/null +++ b/Frogger/src/FroggerTests.java @@ -0,0 +1,477 @@ +/* FILE.java + * + * @author Thomas Johnson + * + * Frogger tests suite + */ + +import static org.junit.Assert.fail; + +import java.awt.Color; +import java.awt.Rectangle; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import static java.util.concurrent.TimeUnit.SECONDS; +import org.junit.Test; + +import org.junit.Rule; +import org.junit.rules.Timeout; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.MethodSorters; +import org.junit.FixMethodOrder; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) + +public class FroggerTests { + + @Rule +public Timeout globalTimeout = new Timeout(10, SECONDS); + +Rectangle rectangle = new Rectangle(); // rectangle for testing, created at postion (0, 0, 0, 0) + +boolean boolF = false; // boolean false variable +boolean boolT = true; // boolean true variable + +Color black = Color.black; // black color +Color white = Color.white; // white color +Color blue = Color.blue; // blue color +Color red = Color.red; // red color +Color green = Color.green; // green color +Color yellow = Color.yellow; // yellow color +Color nil = null; // null color for testing + +/* + * FROGGER GAME TESTS SUITE + */ + +// Car class Tests + +@Test +public void testCar1() { + + // empty Car test +} + +@Test +public void testCar2() { + + // test with one single Car object + Car car = new Car(1, 1, 10, black); +} + +@Test +public void testCar3() { + + // testing coordinates of car, along with its height and width + Car car = new Car(1, 1, 10, black); + + assertEquals("This car has an x-coord of 1", 1, car.getx()); + assertEquals("This car has an y-coord of 1", 1, car.gety()); + + // according to given code, height and weight of cars are always 18 + assertTrue("This car has a width and height of 18", car.getW() == 18 && car.getH() == 18); +} + +@Test +public void testGetSpeed() { + + // tests for getSpeed() method + Car car = new Car(1, 1, 10, black); + Car car1 = new Car(1, 1, 100, black); + Car car2 = new Car(1, 1, 1, green); + Car car3 = new Car(1, 1, 55, blue); + Car car4 = new Car(1, 1, 120, black); + Car car5 = new Car(1, 1, 0, black); + + assertTrue("This car has a speed of 10", car.getSpeed() == 10); + assertFalse("This car has a speed of 10, not 100", car.getSpeed() == 100); + assertEquals("The speed of the car is 10.", 10, car.getSpeed()); + assertEquals("The speed of the car is 100.", 100, car1.getSpeed()); + assertEquals("The speed of the car is 1.", 1, car2.getSpeed()); + assertEquals("The speed of the car is 55.", 55, car3.getSpeed()); + assertEquals("The speed of the car is 120.", 120, car4.getSpeed()); + assertEquals("The speed of the car is 0.", 0, car5.getSpeed()); +} + +@Test +public void testGetColor() { + + // testing getColor() method + Car car = new Car(1, 1, 10, black); + Car car1 = new Car(1, 1, 10, white); + Car car2 = new Car(1, 1, 10, red); + Car car3 = new Car(1, 1, 10, blue); + Car car4 = new Car(1, 1, 10, green); + Car car5 = new Car(1, 1, 10, yellow); + + + assertTrue("This color of this car is black", car.getColor() == black); + assertFalse("This color of this car is black, not yellow", car.getColor() == yellow); + assertEquals("The color of the car is black", black, car.getColor()); + assertEquals("The color of the car is white", white, car1.getColor()); + assertEquals("The color of the car is red", red, car2.getColor()); + assertEquals("The color of the car is blue", blue, car3.getColor()); + assertEquals("The color of the car is green", green, car4.getColor()); + assertEquals("The color of the car is yellow", yellow, car5.getColor()); +} + +@Test +public void testColorNull() { + + // testing empty method when the color of the car is null + Car car = new Car(1, 1, 10, nil); + + // check if color is null + assertNull("Testing when the color of the car is null", car.getColor()); +} + +// Frog class Tests + +@Test +public void testFrog1() { + + // empty Frog test +} + +@Test +public void testFrog2() { + + // test for creating one single Frog object + Frog frog = new Frog(1, 1, rectangle); +} + +@Test +public void testFrog3() { + + // testing Frog coordinates with getX() and getY() methods + Frog frog = new Frog(1, 1, rectangle); + + assertEquals("The beginning x poistion of the Frog should be at 1", 1, frog.getX()); + assertEquals("The beginning y poistion of the Frog should be at 1", 1, frog.getY()); + + assertEquals("Testing the getRectangle method to make sure rectangles are the same", rectangle, + frog.getRectangle()); +} + +@Test +public void testFrogMove() { + + // test for move() method + Frog frog = new Frog(1, 1, rectangle); + frog.move(5, 5); + + assertEquals("Testing the move() method in Frog class", 1, frog.getX()); + assertEquals("Testing the move() method in Frog class", 1, frog.getY()); +} + +@Test +public void testGetBoundsFrog() { + + // test for checking + Frog frog = new Frog(1, 1, rectangle); + + assertTrue("Testing to make sure frog bounds don't equal rectangle bounds", + frog.getBounds() != rectangle.getBounds()); +} + +@Test +public void testGetX() { + + // tests for getX() and setX() methods + Frog frog = new Frog(1, 1, rectangle); + + frog.setX(2); + assertEquals("The x poistion of the Frog should be at 2", 2, frog.getX()); + + frog.setX(50); + assertEquals("The x poistion of the Frog should be at 50", 50, frog.getX()); + + frog.setX(100); + assertEquals("The x poistion of the Frog should be at 100", 100, frog.getX()); + + frog.setX(10); + assertEquals("The x poistion of the Frog should be at 10", 10, frog.getX()); + + frog.setX(1); + assertEquals("The x poistion of the Frog should be at 1", 1, frog.getX()); +} + +@Test +public void testGetY() { + + // tests for getY() and setY() methods + Frog frog = new Frog(1, 1, rectangle); + + frog.setY(2); + assertEquals("X poistion of the Frog should be at 2", 2, frog.getY()); + + frog.setY(5); + assertEquals("X poistion of the Frog should be at 5", 5, frog.getY()); + + frog.setY(100); + assertEquals("X poistion of the Frog should be at 100", 100, frog.getY()); + + frog.setY(50); + assertEquals("X poistion of the Frog should be at 50", 50, frog.getY()); + + frog.setY(10); + assertEquals("X poistion of the Frog should be at 10", 10, frog.getY()); +} + +// FroggerComponent class Tests + +@Test +public void testFroggerComponent1() { + + // empty FroggerComponent test +} + +@Test +public void testFroggerComponent2() { + + // testing constructor with one single FroggerComponent object + FroggerComponent fc = new FroggerComponent(); +} + +@Test +public void testGetLevel() { + + // tests for setLevel() and getLevel() methods + FroggerComponent fc = new FroggerComponent(); + + fc.setLevel(2); + assertEquals("The current level is 2", 2, fc.getLevel()); + + fc.setLevel(5); + assertEquals("The current level is 5", 5, fc.getLevel()); + + fc.setLevel(1); + assertEquals("The current level is 1", 1, fc.getLevel()); + + fc.setLevel(10); + assertEquals("The current level is 10", 10, fc.getLevel()); + + fc.setLevel(25); + assertEquals("The current level is 25", 25, fc.getLevel()); +} + +@Test +public void testGetLife() { + + // tests for setLife() and getLife() methods + FroggerComponent fc = new FroggerComponent(); + + fc.setLife(5); + assertEquals("The Frog has 5 lives", 5, fc.getLife()); + + fc.setLife(4); + assertEquals("The Frog has 4 lives", 4, fc.getLife()); + + fc.setLife(3); + assertEquals("The Frog has 3 lives", 3, fc.getLife()); + + fc.setLife(1); + assertEquals("The Frog has 1 lives", 1, fc.getLife()); + + fc.setLife(0); + assertEquals("The Frog has 0 lives", 0, fc.getLife()); +} + +@Test +public void testGetScore() { + + // tests for getScore() method + FroggerComponent fc = new FroggerComponent(); + + fc.setScore(50); + assertEquals("There is a current score of 50", 50, fc.getScore()); + + fc.setScore(150); + assertEquals("There is a current score of 150", 150, fc.getScore()); + + fc.setScore(500); + assertEquals("There is a current score of 500", 500, fc.getScore()); + + fc.setScore(1000); + assertEquals("There is a current score of 1000", 1000, fc.getScore()); + + fc.setScore(15000); + assertEquals("There is a current score of 50", 15000, fc.getScore()); +} + +@Test +public void testGetHighscore() { + + // tests for getHighscore() and setHighscore() methods + FroggerComponent fc = new FroggerComponent(); + + fc.setHighscore(1000); + assertEquals("The highscore is 1000", 1000, fc.getHighscore()); + + fc.setHighscore(5000); + assertEquals("The highscore is 5000", 5000, fc.getHighscore()); + + fc.setHighscore(10000); + assertEquals("The highscore is 10000", 10000, fc.getHighscore()); + + fc.setHighscore(20000); + assertEquals("The highscore is 20000", 20000, fc.getHighscore()); + + fc.setHighscore(1000000); + assertEquals("The highscore is 1000000", 1000000, fc.getHighscore()); +} + +// TrafficPattern class Tests + +@Test +public void testTrafficPattern1() { + + // empty TrafficPattern test +} + +@Test +public void testTrafficPattern2() { + + // test for creating single TrafficPattern object + String s = "Hello"; + Rectangle r = new Rectangle(1, 1); + + TrafficPattern tp = new TrafficPattern(1, s, true, r, 1); +} + +@Test +public void testGetNext() { + + // tests for getNext() method + String s = "Hello"; + Rectangle r = new Rectangle(1, 1); + + TrafficPattern tp = new TrafficPattern(1, s, true, r, 1); + + assertTrue("The next value of tp object is 1", tp.getNext() == 1); + assertFalse("The next value of tp object is 1, not 100", tp.getNext() == 100); + assertEquals("The next value of tp object is 1", 1, tp.getNext()); +} + +@Test +public void testgetTrafficSpeed() { + + String s = "Hello"; + Rectangle r = new Rectangle(1, 1); + + TrafficPattern tp = new TrafficPattern(1, s, true, r, 1); + + assertEquals("The speed of tp object is 1", 1, tp.getSpeed()); +} + +@Test +public void testgetTrafficPattern() { + + String s = "Hello"; + Rectangle r = new Rectangle(1, 1); + + TrafficPattern tp = new TrafficPattern(1, s, true, r, 1); + + assertEquals("The pattern is equal to s variable", s, tp.getPattern()); +} + +@Test +public void testgetTrafficBool() { + + String s = "Hello"; + Rectangle r = new Rectangle(1, 1); + boolean b = true; + + TrafficPattern tp = new TrafficPattern(1, s, b, r, 1); + + assertTrue("The boolean value in tp object is true", tp.getBool()); +} + +@Test +public void testgetTrafficRect() { + + String s = "Hello"; + Rectangle r = new Rectangle(1, 1); + + TrafficPattern tp = new TrafficPattern(1, s, true, r, 1); + + assertEquals("The rectangle is tp object is equal to r object", r, tp.getRect()); +} + +// Cronometro class Tests + +@Test +public void testCronometro1() { + + // empty test +} + +@Test +public void testCronometro2() { + + // test for creating single Cronometro object + Cronometro cron = new Cronometro(); +} + +// FroggerLevel class Tests + +@Test +public void testFroggerLevel1() { + + // empty test +} + +@Test +public void testFroggerLevel2() { + + int[] speeds = new int[2]; + String[] patterns = new String[5]; + + // test for one single FroggerLevel object + FroggerLevel fl = new FroggerLevel(speeds, patterns); +} + +@Test +public void testGetSpeeds() { + + // test for getSpeed() method + int[] speeds = new int[2]; + String[] patterns = new String[5]; + + FroggerLevel fl = new FroggerLevel(speeds, patterns); + + assertTrue("There are 2 ints in this speed.", fl.getSpeeds() == speeds); + assertEquals("There are 2 speeds.", 2, fl.getSpeeds().length); +} + +@Test +public void testGetPatterns() { + + // test for getPattern() method + int[] speeds = new int[2]; + String[] patterns = new String[5]; + + FroggerLevel fl = new FroggerLevel(speeds, patterns); + + assertTrue("There are 5 strings in this pattern.", fl.getPatterns() == patterns); + assertEquals("There are 5 patterns.", 5, fl.getPatterns().length); +} + +// PlayFrogger class Tests + +@Test +public void testPlayFrogger1() { + + // empty test +} + +@Test +public void testPlayFrogger2() { + + // test for creating one single PlayFrogger object + PlayFrogger play = new PlayFrogger(); +} +} diff --git a/Frogger/src/TrafficPattern.java b/Frogger/src/TrafficPattern.java index f300cf1..6fe08f1 100644 --- a/Frogger/src/TrafficPattern.java +++ b/Frogger/src/TrafficPattern.java @@ -31,6 +31,26 @@ public TrafficPattern(int speed, String pattern, boolean left, Rectangle bounds, initCars(); } + public int getNext() { + return this.next; + } + + public int getSpeed() { + return this.speed; + } + + public String getPattern() { + return this.pattern; + } + + public boolean getBool() { + return this.left; + } + + public Rectangle getRect() { + return this.bounds; + } + public void initCars() { cars = new Vector(); @@ -153,4 +173,4 @@ public void update() moveCars(); testForNewCar(); } -} \ No newline at end of file +} diff --git a/MathHero/src/Addition.java b/MathHero/src/Addition.java index 5d79d9c..e9d7322 100644 --- a/MathHero/src/Addition.java +++ b/MathHero/src/Addition.java @@ -2,14 +2,15 @@ public class Addition extends Enemy { protected String problem; protected int solution; + private int n1, n2; public Addition() { super(.6); - int n1 = (int)(Math.random()*9)+1; - int n2 = (int)(Math.random()*9)+1; + n1 = (int)(Math.random()*9)+1; + n2 = (int)(Math.random()*9)+1; solution = n1+n2; - problem = ""+n1+"+"+n2; + problem = n1+"+"+n2; } public String getProblem() @@ -21,4 +22,12 @@ public int getSolution() { return solution; } -} \ No newline at end of file + + public int getNum1() { + return n1; + } + + public int getNum2() { + return n2; + } +} diff --git a/MathHero/src/BigAddition.java b/MathHero/src/BigAddition.java index 8afbdb5..d1cfc4e 100644 --- a/MathHero/src/BigAddition.java +++ b/MathHero/src/BigAddition.java @@ -2,12 +2,13 @@ public class BigAddition extends Enemy { protected String problem; protected int solution; + private int n1, n2; public BigAddition() { super(.3); - int n1 = (int)(Math.random()*90)+10; - int n2 = (int)(Math.random()*90)+10; + n1 = (int)(Math.random()*90)+10; + n2 = (int)(Math.random()*90)+10; solution = n1+n2; problem = ""+n1+"+"+n2; radius = 15; @@ -22,4 +23,12 @@ public int getSolution() { return solution; } -} \ No newline at end of file + + public int getNum1() { + return n1; + } + + public int getNum2() { + return n2; + } +} diff --git a/MathHero/src/BigMultiplication.java b/MathHero/src/BigMultiplication.java index 3767702..c3cddd9 100644 --- a/MathHero/src/BigMultiplication.java +++ b/MathHero/src/BigMultiplication.java @@ -2,12 +2,13 @@ public class BigMultiplication extends Enemy { protected String problem; protected int solution; + private int n1,n2; public BigMultiplication() { super(.05); - int n1 = (int)(Math.random()*90)+10; - int n2 = (int)(Math.random()*90)+10; + n1 = (int)(Math.random()*90)+10; + n2 = (int)(Math.random()*90)+10; solution = n1*n2; problem = ""+n1+"*"+n2; radius = 20; @@ -22,4 +23,12 @@ public int getSolution() { return solution; } -} \ No newline at end of file + + public int getNum1() { + return n1; + } + + public int getNum2() { + return n2; + } +} diff --git a/MathHero/src/Division.java b/MathHero/src/Division.java index 9a69e58..49e8770 100644 --- a/MathHero/src/Division.java +++ b/MathHero/src/Division.java @@ -2,14 +2,15 @@ public class Division extends Enemy { protected String problem; protected int solution; + private int n1, n2; public Division() { super(.3); - int n1 = (int)(Math.random()*9)+1; - int n2 = (int)(Math.random()*9)+1; + n1 = (int)(Math.random()*9)+1; + n2 = (int)(Math.random()*9)+1; solution = n1; - problem = ""+(n1*n2)+"/"+n2; + problem = (n1*n2)+"/"+n2; radius = 8; color = java.awt.Color.RED; } @@ -23,4 +24,12 @@ public int getSolution() { return solution; } -} \ No newline at end of file + + public int getNum1() { + return n1; + } + + public int getNum2() { + return n2; + } +} diff --git a/MathHero/src/Enemy.java b/MathHero/src/Enemy.java index 47b01a8..1c74cfe 100644 --- a/MathHero/src/Enemy.java +++ b/MathHero/src/Enemy.java @@ -2,7 +2,7 @@ public abstract class Enemy { - protected double r,t,speed; + private double r,t,speed; protected Color color = Color.BLACK; protected int radius = 5; private boolean dying1 = false; @@ -18,6 +18,14 @@ public Enemy(double speed) t = Math.random()*Math.PI*2; } + public double getR() { + return r; + } + + public void setT(double d) { + t = d; + } + public int x() { return Util.MAX_R+(int)(Math.cos(t)*r+.5); @@ -28,22 +36,22 @@ public int y() return Util.MAX_R+(int)(Math.sin(t)*r+.5); } - private int arrowX1() + public int arrowX1() { return Util.MAX_R+(int)(Math.cos(t)*arrowR+.5); } - private int arrowY1() + public int arrowY1() { return Util.MAX_R+(int)(Math.sin(t)*arrowR+.5); } - private int arrowX2() + public int arrowX2() { return Util.MAX_R+(int)(Math.cos(t)*(arrowR-Util.ARROW_LENGTH)+.5); } - private int arrowY2() + public int arrowY2() { return Util.MAX_R+(int)(Math.sin(t)*(arrowR-Util.ARROW_LENGTH)+.5); } @@ -53,6 +61,14 @@ public void die() dying1 = true; } + public boolean getDying1() { + return dying1; + } + + public boolean getDying2() { + return dying2; + } + public boolean dead() { return dead; @@ -63,6 +79,10 @@ public boolean hitting() return r == Util.PLAYER_RADIUS+radius && !dying1; } + public void setDead(boolean b) { + dead = b; + } + public void update() { if(!dying2) @@ -104,4 +124,4 @@ public void draw(Graphics g) color = new Color(color.getRed(),color.getGreen(),color.getBlue(),alpha); } } -} \ No newline at end of file +} diff --git a/MathHero/src/GameComponent.java b/MathHero/src/GameComponent.java index fe0e685..0fa5d75 100644 --- a/MathHero/src/GameComponent.java +++ b/MathHero/src/GameComponent.java @@ -36,12 +36,8 @@ public void run() while(true) { long time = System.currentTimeMillis(); - if(background == null) - { - background = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); - background.getGraphics().setColor(Color.WHITE); - background.getGraphics().fillRect(0,0,WIDTH,HEIGHT); - } + + backgroundCheck(); requestFocus(); @@ -70,6 +66,16 @@ public void run() t.start(); } + //made to make method look cleaner + private void backgroundCheck() { + if(background == null) + { + background = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); + background.getGraphics().setColor(Color.WHITE); + background.getGraphics().fillRect(0,0,WIDTH,HEIGHT); + } + } + //get a blank image to draw onto private Graphics getCanvas() { @@ -137,7 +143,7 @@ public JFrame makeFullScreenWindow() } else { - System.out.println("ARGS! NO FULLSCRENE!"); + System.out.println("ARGS! NO FULLSCREEN!"); } return frame; } @@ -170,4 +176,4 @@ public void standardDraw(Graphics g) * The method that updates the component. */ public abstract void update(); -} \ No newline at end of file +} diff --git a/MathHero/src/Level.java b/MathHero/src/Level.java index 5551475..058032b 100644 --- a/MathHero/src/Level.java +++ b/MathHero/src/Level.java @@ -69,4 +69,4 @@ public void drawEnemies(Graphics g) actives.get(i).draw(g); } } -} \ No newline at end of file +} diff --git a/MathHero/src/MathHeroTests.java b/MathHero/src/MathHeroTests.java new file mode 100644 index 0000000..c83f407 --- /dev/null +++ b/MathHero/src/MathHeroTests.java @@ -0,0 +1,280 @@ +/** +*author: @Sean LaFleur +* +*provides testing for the Addition, BigAddition, BigMultiplication, +*Division, Subtraction, Player, Enemy, and Level classes of the +*MathHero game +*/ +import static org.junit.Assert.fail; + +import java.util.Vector; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import static java.util.concurrent.TimeUnit.SECONDS; +import org.junit.Test; +import org.junit.Rule; +import org.junit.rules.Timeout; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.MethodSorters; +import org.junit.FixMethodOrder; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class MathHeroTests { + + @Rule public Timeout globalTimeout = new Timeout(2, SECONDS); + + @Test public void addition1() { + Addition add = new Addition(); + + assertTrue("Actual solution for Addition does not equal expected", + (add.getNum1() + add.getNum2()) == add.getSolution()); + assertTrue("Actual problem for Addition does not equal expected", + (add.getNum1()+"+"+add.getNum2()).equals(add.getProblem())); + } + + @Test public void bigAddition1() { + BigAddition add = new BigAddition(); + + assertTrue("Actual solution for BigAddition does not equal expected", + (add.getNum1() + add.getNum2()) == add.getSolution()); + assertTrue("Actual problem for BigAddition does not equal expected", + (add.getNum1()+"+"+add.getNum2()).equals(add.getProblem())); + } + + @Test public void multiplication1() { + Multiplication mult = new Multiplication(); + + assertTrue("Actual solution for Multiplication does not equal expected", + (mult.getNum1()*mult.getNum2()) == mult.getSolution()); + assertTrue("Actual problem for Multiplication does not equal expected", + (mult.getNum1()+"*"+mult.getNum2()).equals(mult.getProblem())); + } + + @Test public void bigMultiplication1() { + BigMultiplication bg = new BigMultiplication(); + + assertTrue("Actual solution for BigMultiplication does not equal expected", + (bg.getNum1()*bg.getNum2()) == bg.getSolution()); + assertTrue("Actual problem for BigMultiplication does not equal expected", + (bg.getNum1()+"*"+bg.getNum2()).equals(bg.getProblem())); + } + + @Test public void division1() { + Division div = new Division(); + + assertTrue("Actual solution for Division does not equal expected", + div.getNum1() == div.getSolution()); + assertTrue("Actual problem for Division does not equal expected", + ((div.getNum1()*div.getNum2())+ "/" + div.getNum2()).equals(div.getProblem())); + } + + @Test public void subtraction1() { + Subtraction sub = new Subtraction(); + + assertTrue("Actual solution for Subtraction does not equal expected", + sub.getNum1() == sub.getSolution()); + assertTrue("Actual problem for Subtraction does not equal expected", + ((sub.getNum1()+sub.getNum2())+ "-" + sub.getNum2()).equals(sub.getProblem())); + } + + @Test public void player1() { + Player player = new Player(); + + player.hit(10); + + assertEquals("Health is not 90 after hit of 10", 90, player.getHealth()); + assertFalse("Player is dead after health is above 0", player.dead()); + } + + @Test public void player2() { + Player player = new Player(); + + player.hit(100); + + assertEquals("Health is not 0 after hit of 100", 0, player.getHealth()); + assertTrue("Player is not dead after health is 0", player.dead()); + } + + @Test public void player3() { + Player player = new Player(); + + player.addExp(13); + + assertEquals("Experience was not 2", 3, player.getExp()); + assertEquals("Level was not 2", 2, player.getLevel()); + } + + @Test public void player4() { + Player player = new Player(); + + player.addExp(9); + + assertEquals("Experience was not 9", 9, player.getExp()); + assertEquals("Level was not 1", 1, player.getLevel()); + } + + @Test public void player5() { + Player player = new Player(); + + player.addExp(12); + player.heal(); + + assertEquals("Health was not 125", 125, player.getHealth()); + } + + @Test public void player6() { + Player player = new Player(); + + player.addExp(30); + + assertEquals("Experience was not 0", 0, player.getExp()); + assertEquals("Level was not 3", 3, player.getLevel()); + } + + @Test public void player7() { + Player player = new Player(); + + player.addExp(30); + player.heal(); + + assertEquals("Health is not 150", 150, player.getHealth()); + } + + @Test public void enemy1() { + Addition add = new Addition(); + + assertFalse("dying1 is not false", add.getDying1()); + } + + @Test public void enemy2() { + Addition add = new Addition(); + + add.die(); + + assertTrue("dying1 is not true", add.getDying1()); + assertFalse("dying2 is not false", add.getDying2()); + } + + @Test public void enemy3() { + Addition add = new Addition(); + + add.die(); + + for(int i = 0; i < 31; i ++) { + add.update(); + } + + assertTrue("dying2 is false", add.getDying2()); + } + + @Test public void enemy4() { + Addition add = new Addition(); + + for(int i = 0; i < 392; i ++) { + add.update(); + } + + assertTrue("Add is not hitting the player", add.hitting()); + } + + @Test public void enemy5() { + Addition add = new Addition(); + + assertEquals("Max_R is not 250", 250, add.getR(), .01); + } + + @Test public void enemy6() { + Addition add = new Addition(); + + add.setT(0); + + assertEquals("X was not 500 t = 0", 500, add.x()); + assertEquals("Y was not 250 with t = 0", 250, add.y()); + } + + @Test public void enemy7() { + Addition add = new Addition(); + + add.setT(0); + + assertEquals("arrowX1 was not 265 with t = 0", 265, add.arrowX1()); + assertEquals("arrowX2 was not 260 with t = 0", 260, add.arrowX2()); + } + + @Test public void enemy8() { + Addition add = new Addition(); + + add.setT(0); + + assertEquals("arrowY1 was not 265 with t = 0", 250, add.arrowY1()); + assertEquals("arrowY2 was not 260 with t = 0", 250, add.arrowY2()); + } + + @Test public void level1() { + Vector vect= new Vector<>(); + Addition add = new Addition(); + vect.add(add); + + Level lvl = new Level(1, 1, vect); + for(int i = 0; i < 1000; i ++) { + lvl.update(); + } + + assertEquals("1 enemy was not killed", 1, lvl.process(add.getSolution())); + } + + @Test public void level2() { + Vector vect = new Vector<>(); + Addition add = new Addition(); + add.setDead(true); + Division div = new Division(); + div.setDead(true); + vect.add(add); + vect.add(div); + + Level lvl = new Level(2, 1, vect); + + for(int i = 0; i < 1000; i ++) { + lvl.update(); + } + + assertTrue("Not all enemies were killed", lvl.finished()); + } + + @Test public void level3() { + Vector vect = new Vector<>(); + Addition add = new Addition(); + add.setDead(true); + Division div = new Division(); + vect.add(add); + vect.add(div); + + Level lvl = new Level(2, 1, vect); + + lvl.update(); + + assertFalse("All Enemies were Killed", lvl.finished()); + } + + @Test public void level4() { + Vector vect= new Vector<>(); + Addition add = new Addition(); + + for(int i = 0; i < 391; i ++) { + add.update(); + } + + vect.add(add); + + Level lvl = new Level(1, 1, vect); + + for(int i = 0; i < 1000; i ++) { + lvl.update(); + } + + assertEquals("No enemies were hitting", 1, lvl.getHitting()); + } +} diff --git a/MathHero/src/Multiplication.java b/MathHero/src/Multiplication.java index 3ff7680..96b94bf 100644 --- a/MathHero/src/Multiplication.java +++ b/MathHero/src/Multiplication.java @@ -2,14 +2,15 @@ public class Multiplication extends Enemy { protected String problem; protected int solution; + private int n1,n2; public Multiplication() { super(.4); - int n1 = (int)(Math.random()*9)+1; - int n2 = (int)(Math.random()*9)+1; + n1 = (int)(Math.random()*9)+1; + n2 = (int)(Math.random()*9)+1; solution = n1*n2; - problem = ""+n1+"*"+n2; + problem = n1+"*"+n2; radius = 8; } @@ -22,4 +23,12 @@ public int getSolution() { return solution; } -} \ No newline at end of file + + public int getNum1() { + return n1; + } + + public int getNum2() { + return n2; + } +} diff --git a/MathHero/src/Player.java b/MathHero/src/Player.java index 975de75..13383a7 100644 --- a/MathHero/src/Player.java +++ b/MathHero/src/Player.java @@ -13,6 +13,19 @@ public void hit(int amount) health-=amount; } + //added the getters + public int getHealth() { + return health; + } + + public int getExp() { + return exp; + } + + public int getLevel() { + return level; + } + public void heal() { health = maxHealth; @@ -26,7 +39,8 @@ public boolean dead() public void addExp(int amount) { exp+=amount; - while(exp>expToNextLevel) + //fixed loop + while(exp>=expToNextLevel) levelUp(); } @@ -50,4 +64,4 @@ public void draw(Graphics g) g.fillRect(Util.MAX_R-Util.PLAYER_RADIUS,Util.MAX_R-Util.PLAYER_RADIUS-1, Util.PLAYER_RADIUS*2*exp/expToNextLevel,1); } -} \ No newline at end of file +} diff --git a/MathHero/src/Subtraction.java b/MathHero/src/Subtraction.java index 912735b..2ee9679 100644 --- a/MathHero/src/Subtraction.java +++ b/MathHero/src/Subtraction.java @@ -2,14 +2,15 @@ public class Subtraction extends Enemy { protected String problem; protected int solution; + private int n1, n2; public Subtraction() { super(.4); - int n1 = (int)(Math.random()*9)+1; - int n2 = (int)(Math.random()*9)+1; + n1 = (int)(Math.random()*9)+1; + n2 = (int)(Math.random()*9)+1; solution = n1; - problem = ""+(n1+n2)+"-"+n2; + problem = (n1+n2)+"-"+n2; color = java.awt.Color.RED; } @@ -22,4 +23,12 @@ public int getSolution() { return solution; } -} \ No newline at end of file + + public int getNum1() { + return n1; + } + + public int getNum2() { + return n2; + } +} diff --git a/Pong/src/Ball.java b/Pong/src/Ball.java index beaaf6d..106d2c6 100644 --- a/Pong/src/Ball.java +++ b/Pong/src/Ball.java @@ -21,6 +21,42 @@ public Ball(Color c, int x, int y) speed = 4; } + public int getBallRadius() { + return this.RADIUS; + } + + public int getX() { + return this.x; + } + + public int getY() { + return this.y; + } + + public int getDx() { + return this.dx; + } + + public int getDy() { + return this.dy; + } + + public Color getColor() { + return this.color; + } + + public int getSpeed() { + return this.speed; + } + + public int getSpeedUpDelay() { + return this.speedUpDelay; + } + + public int getDelay() { + return this.delay; + } + public void move() { delay = (delay+1)%speedUpDelay; @@ -64,4 +100,4 @@ public void draw(Graphics g) g.setColor(color); g.fillOval(x-RADIUS, y-RADIUS, RADIUS*2, RADIUS*2); } -} \ No newline at end of file +} diff --git a/Pong/src/Paddle.java b/Pong/src/Paddle.java index 8be4c87..493f704 100644 --- a/Pong/src/Paddle.java +++ b/Pong/src/Paddle.java @@ -14,6 +14,26 @@ public Paddle(int x, int y) this.y = y; } + public int getX() { + return this.x; + } + + public int getY() { + return this.y; + } + + public int getWidth() { + return WIDTH; + } + + public int getHeight() { + return HEIGHT; + } + + public int getMaxSpeed() { + return MAX_SPEED; + } + public void moveTo(int y) { if(Math.abs(this.y-y) > MAX_SPEED) @@ -59,4 +79,4 @@ public void draw(Graphics g) g.setColor(Color.BLACK); g.fillRect(x-WIDTH/2, y-HEIGHT/2, WIDTH, HEIGHT); } -} \ No newline at end of file +} diff --git a/Pong/src/PlayPong.java b/Pong/src/PlayPong.java index e72d6a9..30d5ffd 100644 --- a/Pong/src/PlayPong.java +++ b/Pong/src/PlayPong.java @@ -5,6 +5,7 @@ public class PlayPong extends JFrame { public PlayPong() { + //checking git stuff getContentPane().setLayout(new FlowLayout()); add(new PongComponent()); pack(); @@ -16,4 +17,4 @@ public static void main(String[] args) { new PlayPong(); } -} \ No newline at end of file +} diff --git a/Pong/src/PongEnvironment.java b/Pong/src/PongEnvironment.java index 39f26d7..c14640e 100644 --- a/Pong/src/PongEnvironment.java +++ b/Pong/src/PongEnvironment.java @@ -23,6 +23,26 @@ public void setComputer(boolean l, boolean r) rightComputer = r; } + public boolean getLeftComputer() { + return this.leftComputer; + } + + public boolean getRightComputer() { + return this.rightComputer; + } + + public Ball getBall() { + return this.ball; + } + + public int getPaddleWidth() { + return WIDTH; + } + + public int getPaddleHeight() { + return HEIGHT; + } + public Paddle getLeft() { return left; @@ -67,27 +87,56 @@ private void rightScore() right = new Paddle(WIDTH-10, HEIGHT/2); } - public void update() - { - ball.move(); - if(ball.getLocation().getY() - Ball.RADIUS <= 0) + public void updateTop() { + if(ball.getLocation().getY() + Ball.RADIUS >= HEIGHT) ball.bounceTop(); - else if(ball.getLocation().getY() + Ball.RADIUS >= HEIGHT) + } + + public void updateBottom() { + + if(ball.getLocation().getY() - Ball.RADIUS <= 0) ball.bounceTop(); - else if(left.contains(ball)) + } + + public void updateLeftSide() { + + if(left.contains(ball)) ball.bounceSide(); - else if(right.contains(ball)) + } + + public void updateRightSide() { + + if(right.contains(ball)) ball.bounceSide(); - else if(ball.getLocation().getX() < 0) - rightScore(); - else if(ball.getLocation().getX() > WIDTH) + } + + public void updateLeftScore() { + + if(ball.getLocation().getX() > WIDTH) leftScore(); + } + + public void updateRightScore() { + + if(ball.getLocation().getX() < 0) + rightScore(); + } + + public void update() + { + ball.move(); + + updateBottom(); + updateTop(); + updateLeftSide(); + updateRightSide(); + updateLeftScore(); + updateRightScore(); if(leftComputer) left.moveTo((int)ball.getLocation().getY()); if(rightComputer) right.moveTo((int)ball.getLocation().getY()); - } public void draw(Graphics g) @@ -99,4 +148,4 @@ public void draw(Graphics g) g.drawString("Score: "+ leftScore, 25, 12); g.drawString("Score: "+ rightScore, WIDTH-125, 12); } -} \ No newline at end of file +} diff --git a/Pong/src/PongTests.java b/Pong/src/PongTests.java new file mode 100644 index 0000000..282f4e3 --- /dev/null +++ b/Pong/src/PongTests.java @@ -0,0 +1,407 @@ +/* FILE.java + * + * @author Thomas Johnson + * + * Pong tests suite + */ + +import java.awt.Color; +import java.awt.Rectangle; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static java.util.concurrent.TimeUnit.SECONDS; +import org.junit.Test; + +import org.junit.Rule; +import org.junit.rules.Timeout; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.MethodSorters; +import org.junit.FixMethodOrder; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) + +public class PongTests { + + @Rule + public Timeout globalTimeout = new Timeout(10, SECONDS); + + Color black = Color.black; // black color + Color white = Color.white; // white color + Color blue = Color.blue; // blue color + Color red = Color.red; // red color + Color green = Color.green; // green color + Color yellow = Color.yellow; // yellow color + Color nil = null; // null color for testing + + boolean boolF = false; // boolean false variable + boolean boolT = true; // boolean true variable + + /* + * PONG GAME TESTS SUITE + */ + +// Ball Tests + +@Test +public void testBall1() { + + // empty test +} + +@Test +public void testBall2() { + + // test for creating one single Ball object + Ball ball = new Ball(black, 1, 1); +} + +@Test +public void testBallGetX() { + + // tests for getX() method + Ball ball = new Ball(black, 1, 1); + Ball ball1 = new Ball(black, 5, 1); + Ball ball2 = new Ball(black, 10, 1); + Ball ball3 = new Ball(black, 150, 1); + Ball ball4 = new Ball(black, 25, 1); + Ball ball5 = new Ball(black, 100, 1); + + assertEquals("The x-coordinate of Ball object is at 1", 1, ball.getX()); + assertEquals("The x-coordinate of Ball object is at 5", 5, ball1.getX()); + assertEquals("The x-coordinate of Ball object is at 10", 10, ball2.getX()); + assertEquals("The x-coordinate of Ball object is at 150", 150, ball3.getX()); + assertEquals("The x-coordinate of Ball object is at 25", 25, ball4.getX()); + assertEquals("The x-coordinate of Ball object is at 100", 100, ball5.getX()); +} + +@Test +public void testBallGetY() { + + // tests for getY() method + Ball ball = new Ball(black, 1, 1); + Ball ball1 = new Ball(black, 5, 5); + Ball ball2 = new Ball(black, 10, 10); + Ball ball3 = new Ball(black, 150, 150); + Ball ball4 = new Ball(black, 25, 25); + Ball ball5 = new Ball(black, 100, 100); + + assertEquals("The y-coordinate of Ball object is at 1", 1, ball.getY()); + assertEquals("The y-coordinate of Ball object is at 5", 5, ball1.getY()); + assertEquals("The y-coordinate of Ball object is at 10", 10, ball2.getY()); + assertEquals("The y-coordinate of Ball object is at 150", 150, ball3.getY()); + assertEquals("The y-coordinate of Ball object is at 25", 25, ball4.getY()); + assertEquals("The y-coordinate of Ball object is at 100", 100, ball5.getY()); +} + +@Test +public void testBallGetDx() { + + // tests for getDx() method + int dx = (Math.random() < .5)?1:-1; + Ball ball = new Ball(black, dx, 1); + + assertTrue("The result of dx and ball's dx is -1 or 1", ball.getDx() == -1 || ball.getDx() == 1); + assertFalse("The result of ball's dx is either -1 or 1", ball.getDx() == 0); +} + +@Test +public void testBallGetDy() { + + // tests for getDy() method + int dy = (Math.random() < .5)?1:-1; + + Ball ball = new Ball(black, 1, dy); + + assertTrue("The result of dy and ball's dy is equal to -1 or 1", ball.getDy() == -1 || ball.getDy() == 1); + assertFalse("The result of ball's dy is either -1 or 1", ball.getDy() == 0); +} + +@Test +public void testBallGetColor() { + + // tests for getColor() method + Ball ball = new Ball(black, 1, 1); + Ball ball1 = new Ball(white, 1, 1); + Ball ball2 = new Ball(red, 1, 1); + Ball ball3 = new Ball(blue, 1, 1); + Ball ball4 = new Ball(green, 1, 1); + Ball ball5 = new Ball(yellow, 1, 1); + + assertEquals("The color of the ball object is black", black, ball.getColor()); + assertEquals("The color of the ball object is white", white, ball1.getColor()); + assertEquals("The color of the ball object is red", red, ball2.getColor()); + assertEquals("The color of the ball object is blue", blue, ball3.getColor()); + assertEquals("The color of the ball object is green", green, ball4.getColor()); + assertEquals("The color of the ball object is yellow", yellow, ball5.getColor()); +} + +@Test +public void testBallGetSpeed() { + + // test for getSpeed() method + Ball ball = new Ball(black, 1, 1); + + assertEquals("The speed of the ball object is always 4", 4, ball.getSpeed()); +} + +@Test +public void testBallGetSpeedUpDelay() { + + // test for getSpeedUpDelay() method + Ball ball = new Ball(black, 1, 1); + + assertEquals("The speed up delay of the ball object is always 25", 25, ball.getSpeedUpDelay()); +} + +@Test +public void testBallDelay() { + + // test for getDelay() method + Ball ball = new Ball(black, 1, 1); + + assertEquals("The delay of the ball object is always 0", 0, ball.getDelay()); +} + +@Test +public void testBallRadius() { + + // test for getSpeed() method + Ball ball = new Ball(black, 1, 1); + + assertEquals("The delay of the ball object is always 10", 10, ball.getBallRadius()); +} + +@Test +public void testGetLocation() { + + // test for getLocation() method + Ball ball = new Ball(black, 1, 1); + Point p = new Point(1, 1); + + assertEquals("The location of this ball is at point p(1, 1)", p, ball.getLocation()); +} + +// Paddle Tests + +@Test +public void testPaddle1() { + + // empty test +} + +@Test +public void testPaddle2() { + + // test for creating one single Paddle object + Paddle paddle = new Paddle(1, 1); +} + +@Test +public void testPaddleGetX() { + + // tests for getX() method + Paddle paddle = new Paddle(1, 1); + Paddle paddle1 = new Paddle(15, 1); + Paddle paddle2 = new Paddle(100, 1); + Paddle paddle3 = new Paddle(25, 1); + Paddle paddle4 = new Paddle(200, 1); + + assertEquals("The x-coordinate for paddle object is 1", 1, paddle.getX()); + assertEquals("The x-coordinate for paddle1 object is 15", 15, paddle1.getX()); + assertEquals("The x-coordinate for paddle2 object is 100", 100, paddle2.getX()); + assertEquals("The x-coordinate for paddle3 object is 25", 25, paddle3.getX()); + assertEquals("The x-coordinate for paddle4 object is 200", 200, paddle4.getX()); +} + +@Test +public void testPaddleGetY() { + + // tests for getY() method + Paddle paddle = new Paddle(1, 1); + Paddle paddle1 = new Paddle(15, 15); + Paddle paddle2 = new Paddle(100, 100); + Paddle paddle3 = new Paddle(25, 25); + Paddle paddle4 = new Paddle(200, 200); + + assertEquals("The y-coordinate for paddle object is 1", 1, paddle.getY()); + assertEquals("The y-coordinate for paddle1 object is 15", 15, paddle1.getY()); + assertEquals("The y-coordinate for paddle2 object is 100", 100, paddle2.getY()); + assertEquals("The y-coordinate for paddle3 object is 25", 25, paddle3.getY()); + assertEquals("The y-coordinate for paddle4 object is 200", 200, paddle4.getY()); +} + +@Test +public void testPaddleGetWidth() { + + // tests for getWidth() method + Paddle paddle = new Paddle(1, 1); + + assertEquals("The width of the paddle is always 10", 10, paddle.getWidth()); +} + +@Test +public void testPaddleGetHeight() { + + // tests for getHeight() method + Paddle paddle = new Paddle(1, 1); + + assertEquals("The height of the paddle is always 50", 50, paddle.getHeight()); +} + +@Test +public void testPaddleGetMaxSpeed() { + + // tests for getMaxSpeed() method + Paddle paddle = new Paddle(1, 1); + + assertEquals("The max speed of the paddle is always 10", 10, paddle.getMaxSpeed()); +} + +@Test +public void testPaddleMove() { + + // tests for move() method + Paddle paddle = new Paddle(5, 10); + + paddle.move(10); + assertEquals("After moving 10 in the y-direction, the new y value is 20", 20, paddle.getY()); + + paddle.move(10); + assertEquals("After moving 10 in the y-direction, the new y value is 30", 30, paddle.getY()); + + paddle.move(50); + assertEquals("After moving 10 in the y-direction, the new y value is 80", 80, paddle.getY()); + + paddle.move(100); + assertEquals("After moving 10 in the y-direction, the new y value is 180", 180, paddle.getY()); + + paddle.move(-50); + assertEquals("After moving 10 in the y-direction, the new y value is 130", 130, paddle.getY()); + + paddle.move(0); + assertEquals("After moving 10 in the y-direction, the new y value is 130", 130, paddle.getY()); +} + +// PongComponent Tests + +@Test +public void testPongComponent1() { + + // empty test +} + +@Test +public void testPongComponent2() { + + // test for creating one single PongComponent object + PongComponent pc = new PongComponent(); +} + +// PongEnvironment Tests + +@Test +public void testPongEnvironment1() { + + // empty test +} + +@Test +public void testPongEnvironment2() { + + // test for creating one single PongEnvironment object + PongEnvironment pe = new PongEnvironment(); +} + +@Test +public void testSetGetComputer() { + + // tests for setComputer() and both getComputer() methods + PongEnvironment pe = new PongEnvironment(); + + pe.setComputer(boolT, boolF); + assertEquals("The left computer of pe object is true", boolT, pe.getLeftComputer()); + assertEquals("The right computer of pe object is false", boolF, pe.getRightComputer()); + + pe.setComputer(boolF, boolT); + assertEquals("The left computer of pe object is false", boolF, pe.getLeftComputer()); + assertEquals("The right computer of pe object is true", boolT, pe.getRightComputer()); + + pe.setComputer(boolF, boolF); + assertEquals("The left computer of pe object is false", boolF, pe.getLeftComputer()); + assertEquals("The right computer of pe object is false", boolF, pe.getRightComputer()); + + pe.setComputer(boolT, boolT); + assertEquals("The left computer of pe object is true", boolT, pe.getLeftComputer()); + assertEquals("The right computer of pe object is true", boolT, pe.getRightComputer()); +} + +@Test +public void testPaddleWidth() { + + // test for getPaddleWidth() method + PongEnvironment pe = new PongEnvironment(); + + assertEquals("The width of the paddle is always 400", 400, pe.getPaddleWidth()); +} + +@Test +public void testPaddleHeight() { + + // test for getPaddleHeight() method + PongEnvironment pe = new PongEnvironment(); + + assertEquals("The height of the paddle is always 300", 300, pe.getPaddleHeight()); +} + +@Test +public void testgetBall() { + + // test for getBall() method + PongEnvironment pe = new PongEnvironment(); + Ball ball = new Ball(green, 100, 100); + + assertTrue("The local ball object does not equal pe object's ball", ball != pe.getBall()); + assertFalse("The local ball object does not equal pe object's ball", ball == pe.getBall()); +} + +@Test +public void testGetLeftPaddle() { + + // test for getLeftPaddle() method + PongEnvironment pe = new PongEnvironment(); + Paddle left = new Paddle(200, 100); + + assertTrue("The local paddle object does not equal the left paddle in PongEnvironment class", left != pe.getLeft()); + assertFalse("The local paddle object does not equal the left paddle in PongEnvironment class", left == pe.getLeft()); +} + +@Test +public void testGetRightPaddle() { + + // test for getRightPaddle() method + PongEnvironment pe = new PongEnvironment(); + Paddle right = new Paddle(200, 100); + + assertTrue("The local paddle object does not equal the right paddle in PongEnvironment class", right != pe.getRight()); + assertFalse("The local paddle object does not equal the right paddle in PongEnvironment class", right == pe.getRight()); +} + +// PlayPong Tests + +@Test +public void testPlayPong1() { + + // empty test +} + +@Test +public void testPlayPong2() { + + // test for creating one single PlayPong object + PlayPong pong = new PlayPong(); +} +} diff --git a/Tetris/src/TetrisTests.java b/Tetris/src/TetrisTests.java new file mode 100644 index 0000000..ee41d13 --- /dev/null +++ b/Tetris/src/TetrisTests.java @@ -0,0 +1,182 @@ +/* FILE.java + * + * @author Thomas Johnson + * + * Tetris tests suite + */ + +import static org.junit.Assert.fail; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import static java.util.concurrent.TimeUnit.SECONDS; +import org.junit.Test; +import org.junit.Rule; +import org.junit.rules.Timeout; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.MethodSorters; +import org.junit.FixMethodOrder; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) + +public class TetrisTests { + + @Rule public Timeout globalTimeout = new Timeout(2, SECONDS); + + /* + * TETRIS GAME TESTS SUITE + */ + +// BlockGrid Tests + + @Test +public void testBlockGrid1() { + + // empty test +} + +@Test +public void testBlockGrid2() { + + // test for creating one single BlockGrid object + BlockGrid bg = new BlockGrid(1, 1); +} + +@Test +public void testSetColorBlockGrid() { + + // tests for checking the setColor() and getColor() methods + BlockGrid bg = new BlockGrid(1, 1); + + bg.setColor(white); + assertEquals("The background color of the tetris game is white", white, bg.getColor()); + + bg.setColor(black); + assertEquals("The background color of the tetris game is black", black, bg.getColor()); + + bg.setColor(red); + assertEquals("The background color of the tetris game is red", red, bg.getColor()); + + bg.setColor(blue); + assertEquals("The background color of the tetris game is blue", blue, bg.getColor()); + + bg.setColor(green); + assertEquals("The background color of the tetris game is green", green, bg.getColor()); + + bg.setColor(yellow); + assertEquals("The background color of the tetris game is yellow", yellow, bg.getColor()); +} + +@Test +public void testGetWidthBlockGrid() { + + // tests for checking the getWidth() method + BlockGrid bg = new BlockGrid(1, 1); + assertEquals("The width of the block grid is 1", 1, bg.getWidth()); + + BlockGrid bg1 = new BlockGrid(10, 10); + assertEquals("The width of the block grid is 10", 10, bg1.getWidth()); + + BlockGrid bg2 = new BlockGrid(100, 10); + assertEquals("The width of the block grid is 100", 100, bg2.getWidth()); + + BlockGrid bg3 = new BlockGrid(50, 10); + assertEquals("The width of the block grid is 50", 50, bg3.getWidth()); + + BlockGrid bg4 = new BlockGrid(0, 10); + assertEquals("The width of the block grid is 0", 0, bg4.getWidth()); +} + +@Test +public void testGetHeightBlockGrid() { + + // tests for checking the getHeight() method + BlockGrid bg = new BlockGrid(1, 1); + assertEquals("The width of the block grid is 1", 1, bg.getHeight()); + + BlockGrid bg1 = new BlockGrid(10, 100); + assertEquals("The width of the block grid is 100", 100, bg1.getHeight()); + + BlockGrid bg2 = new BlockGrid(100, 50); + assertEquals("The width of the block grid is 50", 50, bg2.getHeight()); + + BlockGrid bg3 = new BlockGrid(50, 10); + assertEquals("The width of the block grid is 10", 10, bg3.getHeight()); + + BlockGrid bg4 = new BlockGrid(1, 25); + assertEquals("The width of the block grid is 25", 25, bg4.getHeight()); +} + +@Test +public void testGetGraphicsWidthBlockGrid() { + + // test for checking graphics width of tetris grid + BlockGrid bg = new BlockGrid(1, 1); + + assertEquals("The graphics width of the block grid is 25", 25, bg.getGraphicsWidth()); +} + +@Test +public void testGetGraphicsHeightBlockGrid() { + + // test for checking graphics height of tetris grid + BlockGrid bg = new BlockGrid(1, 1); + + assertEquals("The graphics height of the block grid is 25", 25, bg.getGraphicsHeight()); +} + +@Test +public void testToStringBlockGrid1() { + + // test for toString() method + BlockGrid bg = new BlockGrid(1, 1); + + assertEquals("The expected result of toString() is -","-", bg.toString()); +} + +// TetrisComponent Tests + +@Test +public void testTetrisComponent1() { + + // empty test +} + +@Test +public void testTetrisComponent2() { + + // test for creating one single TetrisComponent object + TetrisComponent tc = new TetrisComponent(1, 1); +} + +// TetrisGrid Tests + +@Test +public void testTetrisGrid1() { + + // empty constructor +} + +@Test +public void testTetrisGrid2() { + + // test for creating one single TetrisGrid object + TetrisGrid tg = new TetrisGrid(1, 1); +} + +// PlayTetris Tests + +@Test +public void testPlayTetris1() { + + // empty test +} + +@Test +public void testPlayTetris2() { + + // test for creating one single PlayTetris object + PlayTetris pt = new PlayTetris(); +} +}