This is my first working java game. I am thrilled it works. What I want to know is.... what can I do to improve it?

I would like to use a simple datebase to collect every possible combination of computer names. I would like to collect new information every time a player enters data. I would then like to take that information and use it.
"My big idea is to create a game that can expand on its own. Every time it is played it saves all the card names, short descriptions, hit points and defense points so they can be used later. Eventually, I would like a game that will be fun to play because you don't know what will come up."

******************************************************* 
*** Date: 12/09/2009 
*** Name: Matthew Stitt 
*** Purpose: To create a game that compares an calculated
*** integer using two types of cards.    
*** A baseball card and a fantasy card.  
*** Object of the Game:  The object of the game is to pick the
*** highest set of numbers.  
*************************************************************/

// Import Statements
import javax.swing.*;
import java.io.*;
import java.util.*;


public class War_Game_final
{


public static void main(String[] args)throws
				FileNotFoundException										

{


// Declare Variables -- Input all variables then (data types)
String inputStr = "";

String errorStr = "";
String outputStr = "";
String displayRules = "";

int oppwin = 0;       // variable -- Opponent is the winner
int playerwin = 0;    // variable -- Player is the winner




//DISPLAY RULES

displayRules = "Welcome to a new game: \n" +
"You are playing against the computer \n" +
"Pick a card either a role playing card or a baseball card. \n" + 
"If you don't have a card on you just make it up. \n" +
"With the role playing card pick two numbers between 0 and 10. \n" +
"With the baseball card pick two decimals between .100 and .600";
JOptionPane.showMessageDialog(null, displayRules, "The Game of War",
		JOptionPane.INFORMATION_MESSAGE);


//Create and associate the scanner object to the input/output source

PrintWriter outFile = new PrintWriter("stats.txt");


// Enter variable for player					
String cardName = "";
int hitPoints = 0;
int defensePoints = 0;
double battingAvg = 0;
double sluggingPct = 0; 
char cardType;


// Begin For loop and player query

int battleHP = 0;     // variable -- Hp for the battle
for(int count = 1; count < 10; count ++)
{
if(count == 9)
{
	outputStr = "This is your last round.  Thank you for Playing";
JOptionPane.showMessageDialog(null, outputStr,"Last Round",
		JOptionPane.INFORMATION_MESSAGE);
}

// Enter the card name

inputStr = JOptionPane.showInputDialog			
(" Enter a Name(or the Name on the Card): ");
cardName = inputStr;
outFile.println("Card Name: " + cardName);


// Enter the card type

inputStr = JOptionPane.showInputDialog
(" Enter 'M' to Enter Points for your card " + "\n" +
" or Enter 'S' to Enter a Sport Card stat.");

cardType = (char)inputStr.charAt(0);


// Magic input panes

if (((cardType == 'M') || (cardType == 'm')) || ((cardType == 'S') || (cardType == 's')))
{		
outFile.println("Card Type: " + cardType);					
}
else if
(((cardType != 'M') || (cardType != 'm')) || ((cardType != 'S') || (cardType != 's')))
{

	inputStr = JOptionPane.showInputDialog
(" Again, Enter 'M' to Enter Points for your card " + "\n" +
" or Enter 'S' to Enter a Sport Card stat.");					
cardType = (char)inputStr.charAt(0);
	outFile.println("Card Type: " + cardType);

}

if ((cardType == 'M') || (cardType == 'm')) 
{


// Enter the hitPoints

	inputStr = JOptionPane.showInputDialog
	(" Enter the Hit Points on the Card " + "\n" +
	 " or " + 
	 " Pick a Number between 0 and 10: ");

	hitPoints = Integer.parseInt(inputStr);

	if(( hitPoints >= 0) && (hitPoints <= 10))
	{
		outFile.println("Hit Points: " + hitPoints);					

	}
	else
	{
	 inputStr = JOptionPane.showInputDialog
	 ("Please enter a number between 0 and 10. " +
	 " \n" + "Enter the Hit Points if it is a Magic-type card: ");
	 hitPoints = Integer.parseInt(inputStr);
}

// Enter the defense points

	inputStr = JOptionPane.showInputDialog
	(" Enter the Defense Points on the Card " + "\n" +
	 " or pick a Number between 0 and 10: ");//Check for int only if double then error
	 defensePoints = Integer.parseInt(inputStr);

if(( defensePoints >= 0) && (defensePoints <= 10))
	{
	 outFile.println("Defense Points: " + defensePoints);				

	}
else
{
	inputStr = JOptionPane.showInputDialog
	("Please enter a number between 0 and 10. " +
	" \n" + "Enter the Hit Points if it is a Magic-type card: ");
	defensePoints = Integer.parseInt(inputStr);
} 
}				

// Sports input panes

else if ((cardType == 'S') || (cardType == 's'))
{

// Enter Batting Avg

	inputStr = JOptionPane.showInputDialog
	("Please enter a number between .100 and .600. " +
	" \n" + "Enter the Batting Average for a baseball card: ");			 
	battingAvg = Double.parseDouble(inputStr);

if(( battingAvg >= .100) && (battingAvg <= .600))
	{

	battingAvg = (battingAvg * 15);
	hitPoints = (int)battingAvg; 

	outFile.println("Hit Points: " + hitPoints);				

	}
else
{
	inputStr = JOptionPane.showInputDialog
	("Again, Please enter a number between .100 and .600. " +
	" \n" + "Enter the Batting Average for a baseball card: ");
	battingAvg = Double.parseDouble(inputStr);		 
	battingAvg = (battingAvg * 15);
	hitPoints = (int)battingAvg; 

	outFile.println("Hit Points: " + hitPoints);	
}

// Enter SluggingPct

inputStr = JOptionPane.showInputDialog	  	
	("Please enter a number between .100 and .600. " +
	" \n" + "Enter the Slugging Percentage for a baseball card:"); 

	sluggingPct = Double.parseDouble(inputStr);

if((sluggingPct >= .100) && (sluggingPct <= .600))
	{

	sluggingPct = (sluggingPct * 15);					
	defensePoints = (int)sluggingPct;				

	}
else	
{
	inputStr = JOptionPane.showInputDialog
	("Again, Please enter a number between .100 and .600. " +
	" \n" + "Enter the Slugging Percentage for a baseball card: ");
	sluggingPct = Double.parseDouble(inputStr);
	sluggingPct = (sluggingPct * 15);					
	defensePoints = (int)sluggingPct;				
outFile.println("Defense Points: " + defensePoints);  			
}


}


// Display user input

outputStr = 
("Card Type: " + cardType + "\n" +
"Name of Player: " + cardName + "\n" +
"Hit Points: " + hitPoints + "\n" +													
"Defense points: " + defensePoints);
JOptionPane.showMessageDialog(null, outputStr,
						"You Entered",
		JOptionPane.INFORMATION_MESSAGE);

battleHP = (hitPoints + defensePoints); // Create Player BattleHP	


// Computer Play
int ComphitPoints = 0;
int CompdefensePoints = 0;
int CompbattleHP = 0;
Random generator = new Random();
String CompoutputStr;		

// Generate Computer name
int Name1;
int Name2;

String[] Fname = new String[14]; 

			Fname[0] = "Zhang Liao";   
			Fname[1] = "Roger";   
			Fname[2] = "Walter";  
			Fname[3] = "Ted";    
			Fname[4] = "Robert"; 
			Fname[5] = "Payton";
			Fname[6] = "Alan";
			Fname[7] = "Tom"; 
			Fname[8] = "Cliff";  
			Fname[9] = "Femeref";    
			Fname[10] = "Pouncing"; 
			Fname[11] = "Necrotic";
			Fname[12] = "Icarian";
			Fname[13] = "Osai";    

for(int index = 0; index < Fname.length; index++);

Name1 = (int) (Math.random() * 13) + 1;						

//2nd Array

String[] Lname = new String[14];						
			Lname[0] = "The Dragon";
			Lname[1] = "Williams";
			Lname[2] = "Payton";
			Lname[3] = "Hero of Hefei";
			Lname[4] = "The Vampire";
			Lname[5] = "Trammell";
			Lname[6] = "The Mermaid";
			Lname[7] = "The Tooth Fairy";
			Lname[8] = "Manning";  
			Lname[9] = "The Archer";    
			Lname[10] = "Worm"; 
			Lname[11] = "Sliver";
			Lname[12] = "Scout";
			Lname[13] = "Vultures";    

for(int index = 0; index < Lname.length; index++);
Name2 = (int) (Math.random() * 13) + 1;


// DISPLAY RULES

displayRules = "The computer will pick a card";
JOptionPane.showMessageDialog(null, displayRules, "The Game of War",
		JOptionPane.INFORMATION_MESSAGE);

// Calculate Computer BattleHP

ComphitPoints = (int) (Math.random() * 9) + 1;
CompdefensePoints = (int) (Math.random() * 9) + 1; 
CompbattleHP = (ComphitPoints + CompdefensePoints);

// Display computer input

CompoutputStr = 
("Name of Computer: " + " " + Fname[Name1] + " " + Lname[Name2] + "\n" + "Hit Points: " + 
ComphitPoints + "\n" +													
"Defense points: " + CompdefensePoints);
JOptionPane.showMessageDialog(null, CompoutputStr,"Computer Player is:",
	JOptionPane.INFORMATION_MESSAGE);// Get Computer Info

// Battle Results

if (battleHP > CompbattleHP)
{
outputStr = ("Congratulations, You Win");
JOptionPane.showMessageDialog(null, outputStr,
					"WINNER", JOptionPane.INFORMATION_MESSAGE);
					playerwin++;
outputStr = 
("Name of Player: " + cardName + "\n" +
"Player Hit Points: " + hitPoints + "\n" +	
"Player Defense Points: " + defensePoints + " \n" +
"You have won: " + playerwin + " times.");
JOptionPane.showMessageDialog(null, outputStr,
						"The winner is:",
				JOptionPane.INFORMATION_MESSAGE);
				playAgain(inputStr, outputStr, playerwin, oppwin, count);
}
else if (battleHP < CompbattleHP) 
{
outputStr = ("Sorry, You Lose");
JOptionPane.showMessageDialog(null, outputStr,
				"You Lost", JOptionPane.INFORMATION_MESSAGE);
				oppwin++;
outputStr =					
("Name of Computer: " + " " + Fname[Name1] + " " + Lname[Name2] + "\n" + "Computer Hit Points: " + 
ComphitPoints + "\n" +										
"Computer Defense points: " + CompdefensePoints + "\n" +
"The Computer has won " + oppwin + " times.");
JOptionPane.showMessageDialog(null, outputStr,
				"Computer Wins", JOptionPane.INFORMATION_MESSAGE);
				playAgain(inputStr, outputStr, playerwin, oppwin, count);

}
else if (battleHP == CompbattleHP)
{
outputStr = ("You have tied");
JOptionPane.showMessageDialog(null, outputStr,
					"Play Again", JOptionPane.INFORMATION_MESSAGE);	
				playAgain(inputStr, outputStr, playerwin, oppwin, count);
}

} // End of for loop	

outFile.close(); //Close file
System.exit(0);				

} // end of Main	


// Play Again
public static void playAgain(String inputStr, String outputStr,
int playerwin, int oppwin, int count)
{
char playAgain;
inputStr = JOptionPane.showInputDialog
( "You have won " + playerwin + " times." + "\n"
+ "The computer has won " + oppwin + " times." + "\n" +
"You have played " + count + " times." + " \n" +
"Play again? Y/N: " + "\n");
	playAgain = (char)inputStr.charAt(0);

if ((playAgain == 'Y') || (playAgain == 'y'))
{

return;

}
else if ((playAgain == 'N') || (playAgain == 'n'))
{
outputStr =
" I'm sorry you don't want to play anymore.  Your score is: " +
" \n" + " Player: " + playerwin + "\n" + "Computer: " + oppwin +
"\n" + " You played " + count + " times.";
	JOptionPane.showMessageDialog(null, outputStr,
		"Quit", JOptionPane.INFORMATION_MESSAGE);
}


					System.exit(0);
}
} // End of War Game Class

Recommended Answers

All 2 Replies

make use of Java's features and create your own Objects (classes) for (for instance) your players, ..
even if you're not familiar with the use of classes yet, you should write more methods, and minimize the lines of code you place in your main method.
I see that your main method throws an Exception, but where do you plan to 'catch' it? My advice: put the instructions that would throw that Exception in another method, and have it caught in the main method.
this makes sure your program doesn't have to crash after encountering such problem.

Thanks for the supply. I have a new version using classes. The problem I am having with this code is the interface. Do you have advice on how I should develop a good interface template?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.