diff --git a/README.md b/README.md index 6f03192..b3a5105 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,17 @@ # HellsGamers -Pong Game developed by Eren Esen, Berkan Sarp, Justin Senn, Brice Tchoumi +"3-Games" developed by Eren Esen, Berkan Sarp, Justin Senn, Brice Tchoumi Pong game is a two-player table tennis-themed console game. The game involves two paddles and a moving ball. The players have to move paddles in an upwards or downwards direction and save the ball from getting hit onto the wall. If the ball hits the wall then it’s a score for another player. The game ends at 20 Points. + +Battleship is a strategy type guessing game for two players. It is played on ruled grids on which each +player's fleet of warships are marked. The locations of the fleets are concealed from the other player. Players +alternate turns calling "shots" at the other player's ships, and the objective of the game is to destroy the +opposing player's fleet. + +Snake is an action video game in which the player maneuvers the end of a growing snake, often represented as a snake. +The objective of the game is to pick up the randomly appearing "apples" that are offered as food. As the snake grows +with each bite, maneuvering becomes increasingly difficult as the playing field becomes more crowded. You lose as soon +as you collide with yourself. \ No newline at end of file diff --git a/src/main/java/BattleShip/AIGridGUI.java b/src/main/java/BattleShip/AIGridGUI.java index ddcae2f..f19e441 100644 --- a/src/main/java/BattleShip/AIGridGUI.java +++ b/src/main/java/BattleShip/AIGridGUI.java @@ -23,10 +23,10 @@ public class AIGridGUI extends JPanel { boolean[] cellsHit; boolean[] cellsKilled; boolean randomGuess = true; - int f; // it is the first - Color Red = new Color(100, 0, 0); // + int firstHit; + Color darkRed = new Color(100, 0, 0); // Border loweredBevel = BorderFactory.createLoweredBevelBorder(); - Border raisedbevel = BorderFactory.createRaisedBevelBorder(); + //Border raisedbevel = BorderFactory.createRaisedBevelBorder(); Border defaultBorder; //empty = BorderFactory.createEmptyBorder(4, 4, 4, 4); Border compound = BorderFactory.createCompoundBorder(); @@ -252,7 +252,7 @@ public class AIGridGUI extends JPanel { } else { //If nonrandom guess (locked onto a particular ship that has been hit but not killed), determine where to guess. - int attempts = 1; + int attempts = 0; while(!isClear) { attempts++; @@ -261,7 +261,7 @@ public class AIGridGUI extends JPanel { //Starting from the location of the first hit on the ship, test each direction to determine how many consecutive hits have been made in that direction. - int u = f; + int u = firstHit; int upCount = -1; while(u >= 0 && cellsHit[u] && !cellsKilled[u]) { u = moveUp(u); @@ -270,7 +270,7 @@ public class AIGridGUI extends JPanel { up.setCell(u); up.setCount(upCount); - int d = f; + int d = firstHit; int downCount = -1; while(d >= 0 && cellsHit[d] && !cellsKilled[d]) { d = moveDown(d); @@ -279,7 +279,7 @@ public class AIGridGUI extends JPanel { down.setCell(d); down.setCount(downCount); - int r = f; + int r = firstHit; int rightCount = -1; while(r >= 0 && cellsHit[r] && !cellsKilled[r]) { r = moveRight(r); @@ -288,7 +288,7 @@ public class AIGridGUI extends JPanel { right.setCell(r); right.setCount(rightCount); - int l = f; + int l = firstHit; int leftCount = -1; while(l >= 0 && cellsHit[l] && !cellsKilled[l]) { l = moveLeft(l); @@ -306,15 +306,15 @@ public class AIGridGUI extends JPanel { //If first guess is not clear or is out of bounds, continue trying other directions until one is found that works. - if(attempts == 1) { + if(attempts == 2) { guessLocation = directions.get(1).getCell(); } - if(attempts == 2) { + if(attempts == 3) { guessLocation = directions.get(2).getCell(); } - if(attempts == 3) { + if(attempts == 4) { guessLocation = directions.get(3).getCell(); } @@ -343,7 +343,7 @@ public class AIGridGUI extends JPanel { if(s == null) { //If no ship in that cell, mark as a miss. text = "Other player missed. Your turn."; - b.setBackground(Color.BLUE); + b.setBackground(Color.lightGray); } else { //Check if guess killed a ship. killed = s.counter(); @@ -353,12 +353,12 @@ public class AIGridGUI extends JPanel { for(BSButton bu : buttons) { //Mark killed cells. if(bu.getCellContents() == s) { - bu.setBackground(Red); + bu.setBackground(darkRed); cellsKilled[bu.getGridLocation()] = true; } //Mark if any cell remains that has been hit but not yet killed. If so, lock onto that cell. if(cellsHit[bu.getGridLocation()] && !cellsKilled[bu.getGridLocation()]) { - f = bu.getGridLocation(); + firstHit = bu.getGridLocation(); unkilledCells = true; } } @@ -372,7 +372,7 @@ public class AIGridGUI extends JPanel { b.setBackground(Color.red); //If previously random guessing, switch to locking onto the hit cell. if(randomGuess) { - f = b.getGridLocation(); + firstHit = b.getGridLocation(); randomGuess = false; } cellsHit[guessLocation] = true; @@ -502,7 +502,7 @@ public class AIGridGUI extends JPanel { public void highlightCells(BSButton b, int x) { BSButton cell = b; - int action = x; + int actionToTake = x; clear = true; //Check whether sufficient spaces are clear to place the ship. @@ -521,20 +521,39 @@ public class AIGridGUI extends JPanel { for(int i = 0; i < shipToPlace.getLength(); i++) { BSButton bsb = buttons.get(cell.getGridLocation() + (i * columns)); //If mouse entered, highlight cells via lowered bevel. - if(action == 1) { - bsb.setBorder(raisedbevel); + if(actionToTake == 0) { + bsb.setBorder(loweredBevel); } else { //If mouse released, place ship and color ship cells. - if(action == 2) { + if(actionToTake == 1) { bsb.setCellContents(shipToPlace); - bsb.setBackground(Color.gray); - bsb.setBorder(raisedbevel); + bsb.setBackground(Color.blue); + bsb.setBorder(loweredBevel); } else { //If mouse exited, unhighlight cells. bsb.setBorder(compound); } } } + }else { + + for(int i = 0; i < shipToPlace.getLength(); i++) { + BSButton bsb = buttons.get(cell.getGridLocation() + i); + if(actionToTake == 0) { + //If mouse entered, highlight cells via lowered bevel. + bsb.setBorder(loweredBevel); + } else { + //If mouse released, place ship and color ship cells. + if(actionToTake == 1) { + bsb.setCellContents(shipToPlace); + bsb.setBackground(Color.blue); + bsb.setBorder(loweredBevel); + } else { + //If mouse exited, unhighlight cells. + bsb.setBorder(defaultBorder); + } + } + } } } } diff --git a/src/main/java/BattleShip/GridGUI.java b/src/main/java/BattleShip/GridGUI.java index 7756d31..76f4037 100644 --- a/src/main/java/BattleShip/GridGUI.java +++ b/src/main/java/BattleShip/GridGUI.java @@ -1,16 +1,26 @@ package BattleShip; +import javax.swing.*; +import javax.swing.border.Border; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Random; -public class GridGUI { +public class GridGUI extends JPanel { ArrayList buttons = new ArrayList(); ArrayList allShips = new ArrayList(); int[] testLocations; int numOfGuesses = 0; + String text = ""; int rows; int columns; + boolean clicked = false; + boolean endGame = false; + Color darkRed = new Color(100, 0, 0); + Border loweredBevel = BorderFactory.createLoweredBevelBorder(); Ship destroyer = new Ship(2, "destroyer"); Ship cruiser = new Ship(3, "cruiser"); @@ -22,7 +32,33 @@ public class GridGUI { rows = r; columns = c; } + public void build() { + allShips.add(destroyer); + allShips.add(cruiser); + allShips.add(submarine); + allShips.add(battleship); + allShips.add(aircraftCarrier); + + for(int i = 0; i < (rows * columns); i++) { + BSButton b = new BSButton(); + b.setEnabled(false); + b.setGridLocation(i); + buttons.add(b); + } + + setShipLocations(); + + GridLayout g = new GridLayout(rows,columns); + this.setLayout(g); + + //Add listeners to all cells in grid to listen for guesses. + + for (BSButton bsb : buttons) { + bsb.addActionListener(new MyCellListener()); + this.add(bsb); + } + } public void setShipLocations() { @@ -61,6 +97,79 @@ public class GridGUI { } } } + for(int i = 0; i < shipLength; i++) { + buttons.get(testLocations[i]).setCellContents(s); + } + + testLocations = null; + } + + } + public boolean getClicked() {return clicked;} + public void setClicked() { + clicked = false; + } + public boolean getEndGame() { + return endGame; + } + public void setEndGame() { + for(JButton j : buttons){ + j.setEnabled(false); } } -} \ No newline at end of file + public class MyCellListener implements ActionListener { + public void actionPerformed(ActionEvent a) { + if(!clicked) { + BSButton cell = (BSButton) a.getSource(); + Ship s = cell.getCellContents(); + boolean killed = false; + numOfGuesses++; + boolean gameOver = true; + cell.setEnabled(false); + cell.setBorder(loweredBevel); + + if(s == null) { + //Mark cell as missed. + text = "You missed. Other player's turn..."; + cell.setBackground(Color.lightGray); + } else { + killed = s.counter(); + if(killed) { + //Mark all of the ship's cells as killed. + text = "You sunk the " + s.getName() + "! Other player's turn..."; + for(BSButton bu : buttons) { + if(bu.getCellContents() == s) { + bu.setBackground(darkRed); + } + } + } else { + //Mark cell as hit. + text = "You got a hit. Other player's turn..."; + cell.setBackground(Color.red); + } + } + for(Ship sh : allShips) { + if(!sh.isKilled()) { + gameOver = false; + } + } + if(gameOver) { + text = "You win! You took " + numOfGuesses + " guesses."; + endGame = true; + } + + clicked = true; + } + } + } + public void enableCells() { + for(BSButton bsb : buttons) { + bsb.setEnabled(true); + } + } + public String getText() { + return text; + } +} + + diff --git a/target/classes/BattleShip/AIGridGUI.class b/target/classes/BattleShip/AIGridGUI.class index 0daa0ac..a8a030f 100644 Binary files a/target/classes/BattleShip/AIGridGUI.class and b/target/classes/BattleShip/AIGridGUI.class differ