here is the phrase game. please keep in mind i am new at java. first language. thanks in advance
import java.util.*;
public class gussinggame {
// Variables
public static String[] words = {"detroit tigers"};
public static boolean gameOver = false;
public static StringBuilder newString = new StringBuilder();
public static int missedBy = 0;
public static void main(String[] args)
{
Random rand = new Random();
int randWord = rand.nextInt(words.length);
String wordSelected = words[0];
runGame(wordSelected);
printAstr();
}
public static void runGame(String wordSelected)
{
int i = 0;
char charInputed;
for(i = 0; i < wordSelected.length(); i++)
{
newString.append("*");
}
while(gameOver == false)
{
if(! wordSelected.equals(newString.toString()))
{
Scanner input = new Scanner(System.in);
System.out.println("(Guess) Enter a letter in word " + newString + " > ");
charInputed = input.next().charAt(0);
calculate(wordSelected, charInputed);
}
else
{
System.out.println("The word is " + newString + ". You missed " + missedBy + " time.");
gameOver = true;
}
}
}
public static void calculate(String wordSelected, char charInputed)
{
int i = 0;
String temp;
boolean isItAMiss = false;
for(i = 0; i < wordSelected.length(); i++)
{
if(wordSelected.charAt(i) == charInputed)
{
temp = Character.toString(wordSelected.charAt(i));
newString.replace(i, (i + 1), temp);
}
else
{
isItAMiss = true;
}
}
if(isItAMiss = true)
{
missedBy++;
isItAMiss = false;
}
}
public static void printAstr()
{
System.out.println(newString);
}
}
Attached File(s)
-
gussinggame.zip (828bytes)
Number of downloads: 10
This post has been edited by Atli: 06 December 2012 - 05:27 PM
Reason for edit:: Use [code] tags when posting code.

New Topic/Question
Reply



MultiQuote




|