[java] loop help with swing

Started by
13 comments, last by Javacell 19 years, 5 months ago
Here is what I tried:

     public void run(){     String name = inputField.getText()     text.append("\n" + "You chose " + name +  " as your player name."+ "\n"); //print name     text.append("Your enemy will be Eggman."+ "\n");     text.append("You and your opponent will have 100 life points."+ "\n");     text.append("The player to lose all of their life points loses the game."+ "\n");		     if(choose_name) //what happens when a player is chosen     {         players[r] = 0;         text.append("\n" + "Eggman goes first." + "\n");     }     else     {     text.append("\n" + "You go first." + "\n");     }     while(!gameOver)//loops until gameover     {           if(first_player) // what to do with the first player	   {                players[r] = 0; //eggman's turn	        //attack damage		int E_attack1 = 5; 		int E_attack2 = 10;		int E_attack3 = 15;		                //random attacks		int[] attacks = { E_attack1, E_attack2, E_attack3 };    	 	int A = attacks.length;		int p = (int) (Math.random() * A);		 				//what happens when an attack is chosen		if(attacks == E_attack1)         	{                   Player_LP -=E_attack1;         	}         	else         	if(attacks == E_attack2)         	{         	     Player_LP -=E_attack2;         		         	}         	else         	if(attacks == E_attack3)         	{                     Player_LP -=E_attack3;         		         	}         	//display the attack         	text.append( "\n" + "Eggman does "+ attacks + " damage." + "\n");         }         else         {         	int attack1 = 5;		int attack2 = 10;		int attack3 = 15;												//get input for attacks		String battacks = inputField.getText();	                       //display attack               text.append( "\n" + "Choose attack1 , attack2, or attack3: " + battacks);							     			               		//what happens when an attack is chosen                if(battacks.equals("attack1"))		{	            Eggman_LP -=attack1;                    text.append("\n" + "You attack with attack1 causing " + attack1 + " damage to Eggman."+"\n");        			        	}        	else        	if (battacks.equals("attack2"))        	{                     Eggman_LP -=attack2;                     text.append("\n" + "You attack with attack2 causing " + attack2 + " damage to Eggman."+"\n");        	}        	else        	if(battacks.equals("attack3"))		{	             Eggman_LP -=attack3;                     text.append("\n" + "You attack with attack3 causing " + attack3 + " damage to Eggman."+"\n");		}	}		   			                           first_player = !first_player; //make it turn base                //how to determine gameover        if (Player_LP <=0)          {               gameover = true;               text.append("Eggman Wins");        }        else        if (Eggman_LP <=0)        {               gameover = true;               text.append("You Win");             }	        	if (inputEntered)	{             inputEntered = false;	}        try         {             Thread.sleep(1000); //how long to wait for input        } catch (InterruptedException e)        {        }                //display life points        player_stats.setText("" + Player_LP);        eggman_stats.setText(""+ Eggman_LP);    }}//Button Classprivate static class ButtonHandler implements ActionListener{      public void actionPerformed(ActionEvent event)//Event handler method      {            inputEntered = false;      }		}

Only problem now is that it doesn't give me a chance to input my name, it doesn't wait for me to input something, if I make the thread sleep longer, just to get to my or eggman's turn takes to long, it takes to long for eggman to attack, clicking the button doesn't do anything, and it takes the input of what I typed in the textfield even though I didn't click a button to tell it to do so.

[Edited by - Javacell on October 30, 2004 1:04:59 AM]
Advertisement
Now with better results:


     public void run(){     String name = inputField.getText()     text.append("\n" + "You chose " + name +  " as your player name."+ "\n"); //print name     text.append("Your enemy will be Eggman."+ "\n");     text.append("You and your opponent will have 100 life points."+ "\n");     text.append("The player to lose all of their life points loses the game."+ "\n");		     if(choose_name) //what happens when a player is chosen     {         players[r] = 0;         text.append("\n" + "Eggman goes first." + "\n");     }     else     {     text.append("\n" + "You go first." + "\n");     }     while(!gameOver)//loops until gameover     {                  	if (inputEntered)	{ if(first_player) // what to do with the first player	   {                players[r] = 0; //eggman's turn	        //attack damage		int E_attack1 = 5; 		int E_attack2 = 10;		int E_attack3 = 15;		                //random attacks		int[] attacks = { E_attack1, E_attack2, E_attack3 };    	 	int A = attacks.length;		int p = (int) (Math.random() * A);		 				//what happens when an attack is chosen		if(attacks == E_attack1)         	{                   Player_LP -=E_attack1;         	}         	else         	if(attacks == E_attack2)         	{         	     Player_LP -=E_attack2;         		         	}         	else         	if(attacks == E_attack3)         	{                     Player_LP -=E_attack3;         		         	}         	//display the attack         	text.append( "\n" + "Eggman does "+ attacks + " damage." + "\n");         }         else         {         	int attack1 = 5;		int attack2 = 10;		int attack3 = 15;												//get input for attacks		String battacks = inputField.getText();	                       //display attack               text.append( "\n" + "Choose attack1 , attack2, or attack3: " + battacks);							     			               		//what happens when an attack is chosen                if(battacks.equals("attack1"))		{	            Eggman_LP -=attack1;                    text.append("\n" + "You attack with attack1 causing " + attack1 + " damage to Eggman."+"\n");        			        	}        	else        	if (battacks.equals("attack2"))        	{                     Eggman_LP -=attack2;                     text.append("\n" + "You attack with attack2 causing " + attack2 + " damage to Eggman."+"\n");        	}        	else        	if(battacks.equals("attack3"))		{	             Eggman_LP -=attack3;                     text.append("\n" + "You attack with attack3 causing " + attack3 + " damage to Eggman."+"\n");		}	}		   			                           first_player = !first_player; //make it turn base                //how to determine gameover        if (Player_LP <=0)          {               gameover = true;               text.append("Eggman Wins");        }        else        if (Eggman_LP <=0)        {               gameover = true;               text.append("You Win");             }	             inputEntered = false;	}        try         {             Thread.sleep(1000); //how long to wait for input        } catch (InterruptedException e)        {        }                //display life points        player_stats.setText("" + Player_LP);        eggman_stats.setText(""+ Eggman_LP);    }}//Button Classprivate static class ButtonHandler implements ActionListener{      public void actionPerformed(ActionEvent event)//Event handler method      {            inputEntered = false;      }		}

Now the only problems are it doesn't give a chance to input my name(if I put the 'input name' part of the code in the while loop or in if (inputEntered), it is repeatedly displayed over and over again) and that I have to click a button in order to get each line to display.

Ex:
You go first.
//Now I have to click a button for it to ask for my attack

Choose attack1 , attack2, or attack3: attack3
//of course I have to click a button now to submit the attack

You attack with attack3 causing 15 damage to Eggman.
//Now I have to click a button in order for eggman to do his damage

Eggman does 10 damage.
//have to click a button in order for it to ask for my attack, but yet it gets my attack input at this point also

[Edited by - Javacell on November 1, 2004 3:54:13 PM]
Really, this should be in "for beginners".
when I use to post in Beginners, they told me to post here instead since it deals with Java.
anyone?

This topic is closed to new replies.

Advertisement