- numOfHits was previously just an integer variable that would go from 0 to 3 and then you used it to terminate the program when it hit three. Now that you're using an arraylist, I'd recommend instead making your while loop in the BattleshipGame class make use of the size() function. For example:
while (myShip.alreadyGuessed.size() < 3) {
You'll also need to update other parts of the program that use numOfHits so that the program will work as expected. - I'd recommend putting aside bonus problems until after you get the core program working. For now, comment out anything having to do with the bonuses (for example, your variable
numOfGuesses
and your methodgetNumOfGuesses()
). - It looks like you call the guess that the user made
userGuess
at one point in the program, while calling ittheGuess
later on. Make sure you're consistent and only use a single variable for the user's guess. - In order to better understand your program, consolidating multiple lines into single ones may help. For example,
import java.util.*;
serves the same purpose as bothimport java.util.Scanner;
andimport java.util.ArrayList;
together. Additionally, you can makeint guess
andguess = myShip.getGuess();
into one line like this:int guess = myShip.getGuess();
.
Last active
November 17, 2015 05:01
-
-
Save christopherturner/87c9ff41ea4e0b28e987 to your computer and use it in GitHub Desktop.
Notes on Battleship/BattleshipGame for William Pai.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Written by William Pai. | |
import java.util.Scanner; | |
import java.util.ArrayList; | |
//imports the Scanner and ArrayList. I could have done import java.util.*; but I just prefer to keep them seperate so I know which I have | |
public class BattleshipGame{ | |
public static void main(String[] args) { | |
Battleship myShip = new Battleship(); | |
int location = (int) (Math.random() * 5); | |
//picks a random integer for the first block the ship is on | |
int[] cells = {location, location + 1, location + 2}; | |
//the blocks must be consecutive so this adds the two next blocks to the int[] | |
int guess; | |
myShip.setLocationCells(cells); | |
while (myShip.getNumOfHits() < 3) { | |
//this is saying that while the ship is still afloat, get a user guess and check it | |
guess = myShip.getGuess(); | |
myShip.checkGuess(guess); | |
System.out.println("The ship has been hit " + myShip.getNumOfHits() + " times."); | |
} | |
//if the program gets outside the while loop then the NumOfHits must be 3 so the ship is sunk | |
System.out.println("Sunk!"); | |
System.out.println("It took you " + myShip.getNumOfGuesses() + " guesses to sink the ship."); | |
} | |
} | |
class Battleship { | |
Scanner userInput = new Scanner(System.in); | |
int numOfHits = 0; | |
int numOfGuesses = 0; | |
//initializing new int variables | |
int[] locationCells; | |
//new integer array | |
ArrayList<Integer> alreadyGuessed = new ArrayList<Integer>(); | |
String userGuess; | |
public int getGuess() { | |
boolean inputNeeded = true; | |
int userGuess = 0; | |
while (inputNeeded) { | |
System.out.println("Welcome! The enemy ship is 3 blocks long on a board 7 blocks long. Enter a guess between 0-6"); | |
if (userInput.hasNextInt()) { | |
userGuess = userInput.nextInt(); | |
inputNeeded = false; | |
} | |
else { | |
System.out.println("That is not an integer, guess again"); | |
} | |
String clearbuf = userInput.nextLine(); | |
} | |
//I trued to use a boolean to make sure the input was an integer but I coudln't get it to work... The program still runs fine though | |
for (int value: alreadyGuessed) { | |
while (theGuess > 6 || theGuess < 0 || theGuess == value) { | |
//while loop with all the values theGuess cannot equal | |
if (theGuess == value) { | |
System.out.println("That has already been guessed. Guess again between 0-6."); | |
userGuess = userInput.nextLine(); | |
theGuess = Integer.parseInt(userGuess); | |
//procedure if there is a repeat guess | |
} | |
else { | |
System.out.println("That is not in the range of spaces. Please re-enter your guess 0-6"); | |
userGuess = userInput.nextLine(); | |
theGuess = Integer.parseInt(userGuess); | |
//if the gueuss is outside the range | |
} | |
} | |
} | |
alreadyGuessed.add(theGuess); | |
numOfGuesses++; | |
return theGuess; | |
//adding the guessed value to the ArrayList of guessed values and adding 1 to number of guesses | |
} | |
public void setLocationCells(int[] userPosition) { | |
locationCells = userPosition; | |
} | |
public void checkGuess(int guess) { | |
int temp = numOfHits; | |
for (int value: locationCells) { | |
if (guess == value) { | |
numOfHits++; | |
System.out.println("Hit!"); | |
//if the user guesses correctly | |
} | |
if (temp == numOfHits) { | |
System.out.println("Miss!"); | |
//if the user guesses incorrectly | |
} | |
} | |
public int getNumOfHits() { | |
return numOfHits; | |
} | |
public int getNumOfGuesses() { | |
return numberOfGuesses; | |
//gets and returns the number of guesses the user took | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment