- Joined
- Jan 7, 2004
- Messages
- 4
- Reaction score
- 0
need help in a battleship kinda game
If i get a hit i can choose the same number to win the game .. how can i make it so that i can only pick 1 number once?
If i get a hit i can choose the same number to win the game .. how can i make it so that i can only pick 1 number once?
Code:
public class simpleDotComDrive {
public static void main (String[] args){
int numOfGuesses = 0;
GameHelper helper = new GameHelper();
simpleDotCom dot = new simpleDotCom();
int randomNum = (int) (Math.random() * 5);
int[] locations = {randomNum,randomNum+1,randomNum+2**;
dot.setLocationCells(locations);
boolean isAlive = true;
while (isAlive == true){
String userGuess = helper.getUserInput("enter a number");
String result = dot.checkYourSelf(userGuess);
numOfGuesses++;
if (result.equals("kill")){
isAlive = false;
System.out.println(numOfGuesses + " guesses");
**
**
**
**
Code:
public class simpleDotCom {
int [] locationCells;
int numOfHits = 0;
public void setLocationCells(int[] locs){
locationCells = locs;
**
public String checkYourSelf(String stringGuess){
int guess = Integer.parseInt (stringGuess);
String result = "miss";
for (int i = 0;i < locationCells.length; i++)
{
if (guess == locationCells[i]){
result = "hit";
numOfHits++;
break;
**
**
if (numOfHits == locationCells.length)
{
result = "kill";
**
System.out.println(result);
return result;
**
**
Code:
import java.io.*;
public class GameHelper {
public String getUserInput(String prompt){
String inputLine = null;
System.out.print(prompt + " ");
try{
BufferedReader is = new BufferedReader(new InputStreamReader(System.in)
);
inputLine = is.readLine();
if (inputLine.length() == 0) return null;
** catch (IOException e) {
System.out.println("IOException: " + e);
** return inputLine;
**
**