When I use nextInt, it keeps repeating after the first go, ex: it'll go 10 50 50 50 50
it works perfectly fine with my friend's computer, the only difference is that he is using dr. java and i'm using rtp
Java: |
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.Random;
import java.io.*;
import hsa.Console;
class FallingLetters extends JFrame
{
char choice, difficulty, test;
int score;
Console c;
public FallingLetters ()
{
c = new Console ("Falling Letters");
getContentPane (). add (new JTextField ());
KeyboardFocusManager manager = KeyboardFocusManager. getCurrentKeyboardFocusManager ();
manager. addKeyEventDispatcher (new KeyDispatch ());
}
private class KeyDispatch implements KeyEventDispatcher
{
public boolean dispatchKeyEvent (KeyEvent e )
{
if (e. getID () == KeyEvent. KEY_PRESSED)
{
test = e. getKeyChar ();
}
return false;
}
}
public void game ()
{
title ();
Letter [] a = new Letter [50];
int letters = 0, score = 0;
Random r = new Random ();
char [] ex = new char [50];
for (int x = 0 ; x < a. length ; x++ )
{
ex [x ] = (char ) (r. nextInt (26) + 97);
if (difficulty == '1')
{
a [x ] = new Letter (c, ex [x ], 2, 0);
}
else if (difficulty == '2')
{
a [x ] = new Letter (c, ex [x ], 1, 500);
}
else
{
a [x ] = new Letter (c, ex [x ], 1, 0);
}
}
c. setCursor (2, 1);
c. println ("Press space in the text field to start.");
while (test != ' ')
{
}
while (letters < 50)
{
c. setCursor (2, 1);
c. println ("Score: " + score + "/50 Progress: " + letters + "/50");
a [letters ]. start ();
// try
// {
// Robot robot = new Robot ();
// robot.keyPress (ex - 32);
// }
// catch (AWTException e)
// {
// }
// try
// {
// if (difficulty == '1')
// Thread.sleep (333);
// else if (difficulty == '2')
// Thread.sleep (250);
// else
// Thread.sleep (166);
// }
// catch (InterruptedException e)
// {
// }
try
{
if (difficulty == '1')
Thread. sleep (920);
else if (difficulty == '2')
Thread. sleep (690);
else
Thread. sleep (450);
}
catch (InterruptedException e )
{
}
if (test == ex [letters ])
{
score++;
a [letters ]. letterGot ();
}
test = ' ';
letters++;
}
System. out. println (score );
}
public void intro ()
{
title ();
c. println ("This program will display falling letters and you must type them in time to get points.");
pauseProgram ("continue");
}
public void mainMenu ()
{
title ();
c. println ("Press 1 to continue.");
c. println ("Press 2 to exit.");
c. print ("What would you like to do?");
while (true)
{
c. setCursor (5, 27);
choice = c. getChar ();
if (choice == '1' || choice == '2')
break;
JOptionPane. showMessageDialog (null, "Please enter 1 or 2", "Error", JOptionPane. ERROR_MESSAGE);
}
}
public void askData ()
{
title ();
c. println ("Press 1 for easy difficulty.");
c. println ("Press 2 for medium difficulty.");
c. println ("Press 3 for hard difficulty.");
c. print ("What would you like to do?");
while (true)
{
c. setCursor (6, 27);
difficulty = c. getChar ();
if (difficulty == '1' || difficulty == '2' || difficulty == '3')
break;
JOptionPane. showMessageDialog (null, "Please enter 1, 2 or 3.", "Error", JOptionPane. ERROR_MESSAGE);
}
}
public void display ()
{
int[] scores = new int [10];
title ();
c. println ("Name Difficulty Score");
try
{
BufferedReader input = new BufferedReader (new FileReader ("HighScore.txt"));
for (int x = 0 ; x < 10 ; x++ )
{
String temp = input. readLine ();
c. println (temp );
}
}
catch (IOException e )
{
System. out. println (e );
}
pauseProgram ("continue");
}
public void goodBye ()
{
title ();
c. println ("Made by, Bruno Maillette 2012.");
pauseProgram ("exit");
c. close ();
}
private void pauseProgram (String next )
{
c. print ("Please press any key to " + next + ".");
c. getChar ();
}
private void title ()
{
c. setTextColor (Color. white);
c. setTextBackgroundColor (Color. black);
c. clear ();
c. print (' ', 33);
c. println ("Falling Letters");
c. println ();
}
public static void main (String[] args )
{
FallingLetters p = new FallingLetters ();
p. intro ();
while (true)
{
p. mainMenu ();
if (p. choice == '2')
break;
p. askData ();
p. pack ();
p. setVisible (true);
p. game ();
p. display ();
}
p. goodBye ();
}
}
|
Java: |
import hsa.Console;
import java.util.*;
import java.awt.*;
public class Letter extends Thread
{
Console c;
char letter;
boolean got = false;
Random r = new Random ();
int x = 0, time, time2;
Font font = new Font ("Verdana", 0, 14);
public Letter (Console con, char random, int millis, int nanos )
{
c = con;
letter = random;
time = millis;
time2 = nanos;
}
public void start ()
{
c. setFont (font );
int pos = r. nextInt (620) + 10;
for (x = 40 ; x < 500 ; x++ )
{
c. setColor (Color. black);
c. drawString ("" + letter, pos, x - 1);
c. setColor (Color. white);
c. drawString ("" + letter, pos, x );
if (got )
break;
try
{
super. sleep (time, time2 );
}
catch (InterruptedException e )
{
}
}
c. setColor (Color. black);
c. drawString ("" + letter, pos, x - 1);
}
public void letterGot ()
{
got = true;
}
}
|
|