• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

need help in a easy java game

Initiate Mage
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?

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;
		**
	
	

**
 
Back
Top