Battleships

Run Settings
LanguageJava
Language Version
Run Command
class Main { public static void main(String[] args) { // Create a battleshipCore instance BattleshipCore core = new BattleshipCore(); // make a int array for location of battleship // [2,3,4] int[] locations = {2, 3, 4}; // set cellLocations into battleshipCore core.setLocationCells(locations); // make a fake user guess // "4" String userGuess = "4"; // call checkYourself with fake guess String result = core.checkYourself(userGuess); // if return "hit" if (result.equals("hit")) { System.out.println("success"); } else { System.out.println("failed"); } } }
/** * Created by fengxiaoping on 5/7/16. */ public class BattleshipCore { int[] locationCells; int numOfHits = 0; public void setLocationCells(int[] locations) { this.locationCells = locations; } public String checkYourself(String userGuess) { int guess = Integer.parseInt(userGuess); String result = null; for (int i = 0; i < locationCells.length; i++) { if (guess == locationCells[i]) { result = "hit"; numOfHits++; } } if (numOfHits == locationCells.length) { result = "kill"; } System.out.println(result); return result; } }
Editor Settings
Theme
Key bindings
Full width
Lines