|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

08-08-2007, 08:14 AM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 8
|
|
|
illegal start of expression & class, interface, or enum expected
i face these 2 errors when compiling my codes. Both errors are from the codes below. Can anyone help? Thanks in advance!
private static void newGame() throws IOException
{
int size = 4; //the length for the answer
char[] answer; //answer of the game
char[] contents = {'0','1','2','3','4','5','6','7','8','9'}; //the characters appear in the answer
int max_trial = 10; //max number of trial
int current_trial = 0; //count the number of trial
int num_complete_correct = 0; //number of correct characters in correct positions
int num_partial_correct = 0; //number of correct characters in wrong positions
size = 4; //initialize the answer
answer = new char[size];
Random ran = new Random(); //randomly generate the answer
for(int i=0;i<size;i++){
answer[i] = contents[ran.nextInt(contents.length)];
}
public boolean check_input(String input){
if (input.length() != size){ // check input length
return false;
}
for(int i=0;i<input.length();i++){ //check input content
boolean contain = false;
for(int j=0;j<contents.length&&!contain;j++){
if (input.charAt(i)==contents[j]){
contain = true;
}
}
if (!contain){
return false;
}
}
return true;
}
public boolean guess(String input){
num_complete_correct = 0; //initialize the variables
num_partial_correct = 0;
boolean[] match_input = new boolean[size];
boolean[] match_answer = new boolean[size];
for(int i=0;i<size;i++){
match_input[i] = false;
match_answer[i] = false;
}
int i,j;
for(i=0;i<size;i++){ //check correct input in correct position
if (input.charAt(i) == answer[i]){
num_complete_correct++;
match_input[i] = true;
match_answer[i] = true;
}
}
for(i=0;i<size;i++){ //check correct input in wrong position
if (match_input[i]){
continue;
}
for(j=0;j<size;j++){
if (match_answer[j]){
continue;
}
if (input.charAt(i)==answer[j]){
match_input[i] = true;
match_answer[j] = true;
num_partial_correct++;
break;
}
}
}
current_trial++;
if (current_trial >= max_trial)
{
return false;
}
else
{
return true;
}
}
public int get_all_correct(){
return num_complete_correct;
}
public int get_partial_correct(){
return num_partial_correct;
}
public boolean complete(){
return (num_complete_correct==size);
}
public void set_answer(String input){
answer = input.toCharArray();
}
public String get_answer(){
return String.valueOf(answer);
}
}
|
|

08-08-2007, 10:16 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,191
|
|
Learning to format your code will help avoid/detect this kind of thing.
private static void newGame() throws IOException
{
int size = 4; //the length for the answer
char[] answer; //answer of the game
//the characters appear in the answer
char[] contents = {'0','1','2','3','4','5','6','7','8','9'};
int max_trial = 10; //max number of trial
int current_trial = 0; //count the number of trial
//number of correct characters in correct positions
int num_complete_correct = 0;
//number of correct characters in wrong positions
int num_partial_correct = 0;
size = 4; //initialize the answer
answer = new char[size];
Random ran = new Random(); //randomly generate the answer
for(int i=0;i<size;i++){
answer[i] = contents[ran.nextInt(contents.length)];
}
} // this curley brace ended up at the end (commented out)
public boolean check_input(String input){
...
public String get_answer(){
return String.valueOf(answer);
}
// } // closing brace of newGame method above
|
|

08-08-2007, 11:40 AM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 8
|
|
Hi, thanks for the reply. I've come across another problem. For this part of the code, is there any way that i can remove the dialog box. I just want to remove the JOption.showInputDialog box and create a simple output without the dialog box. Thanks in advance...
import javax.swing.JOptionPane;
public class GameInterface {
// create a main() method that start the game
// while not reach the max trial and not win the game
// get input
// process input
// display result
// display final result
public static void main(String[] ags) {
MasterMind mind = new MasterMind();
String input;
boolean finish = false;
System.out.println("answer is " + mind.get_answer());
while (!finish && !mind.complete()){
do{
input = JOptionPane.showInputDialog("Guess?(size is "+mind.get_answer().length()+")");
}while(!mind.check_input(input));
finish = !mind.guess(input);
System.out.println("your input is "+input);
System.out.println("you have "+mind.get_all_correct()+" in correct position");
System.out.println("you have "+mind.get_partial_correct()+ " in wrong position");
}
if (mind.complete()){
System.out.println("Congratulation.");
}else{
System.out.println("Game Over! Please try again");
System.out.println("The answer is " + mind.get_answer());
}
}
}
Last edited by silverq_82 : 08-08-2007 at 11:58 AM.
|
|

08-08-2007, 03:42 PM
|
|
Senior Member
|
|
Join Date: Dec 2006
Posts: 748
|
|
You can obtain user input as follows if you want:
BufferedReader console = new BufferedReader (new InputStreamReader(System.in));
String input;
input = console.readLine();
|
|

08-08-2007, 06:04 PM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 8
|
|
Hey, thanks for the guidance... Btw, i have another question.  I have these set of codes and i want to compare the random generated set of 5 numbers with user's input of 5 numbers. Not sure how to start. I'm stuck after prompting user to key in the 5 numbers. Below are the codes:-
private static void newGame() throws IOException
{
//Random generator
Random gen = new Random();
int num = gen.nextInt(9) + 1;
//Generate 5 non-duplicate numbers within an array
int mixcount=1;
int setcount=5;
int[] numbers = new int[5]; //declare and create array
Random mix = new Random();
for(int i = 0 ; i< numbers.length; i++)
{
boolean diff = false; //generate non-duplicate numbers
while (!diff)
{
numbers[i] = mix.nextInt(9);
diff=true;
for (int j=0 ; j < i ; j++)
{
if (numbers[i]==numbers[j])
{
diff = false;
break;
}
}
}
System.out.print(numbers[i]+" "); //remove this after all is complete
}
InputStreamReader isr = new InputStreamReader( System.in ); //Create an InputStreamReader using the standard input stream
BufferedReader stdin = new BufferedReader( isr ); //Create a BufferedReader using the InputStreamReader created
System.out.print( "Enter 5 numbers (1 to 9): " );
String input = stdin.readLine(); // Read a line of text from the user
|
|

08-08-2007, 06:10 PM
|
|
Senior Member
|
|
Join Date: Dec 2006
Posts: 748
|
|
|
silverq_82, at least you can try to develop the game logic. As far as i see, you already collected the code segments needed. I think you should try combining them all yourself at least at this stage if you want to learn Java, programming .. whatever the course objective is.
Good luck.
|
|

08-08-2007, 06:26 PM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 8
|
|
|
erm... i tried a few ways of getting the codes to work but keep getting errors. Not sure if you could help me a little? Thanks. If you don't mind...
P.S. The part where i need to compare with user's input & random generated number.
|
|

08-08-2007, 06:36 PM
|
|
Senior Member
|
|
Join Date: Dec 2006
Posts: 748
|
|
|
Keep the random numbers in an array. Keep user input in another array. Write a loop to compare each element from first array to the second one. If the purpose of the game is to print number of mismatches between these two lists, then define an integer variable and count the number of mismatches with this variable inside your loop.
|
|

08-08-2007, 07:05 PM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 8
|
|
|
Hi, once again, thanks for the info.
|
|

08-08-2007, 08:16 PM
|
|
Senior Member
|
|
Join Date: Dec 2006
Posts: 748
|
|
|
No problem. Let us know if you cant solve an error..
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|