How to make Game Using Java software

The Java programming language can be used to develop complex applications involving databases, sounds and video. But, it can also be used to develop simple games, including word puzzles. A good way to learn how to make a game in Java is to copy, compile and run an existing one, such as the one that follows.

Download and install the Java Development Kit (JDK) from java.sun.com. Then, enter the program code that creates the game: open up Notepad (Start>"Notepad") and enter the following Java source code, which manages the game. Be sure to enter the correct case of each letter. Save the source file with the filename the Game.java.
import java.io.*;
public class theGame
{
public theGame()
{
}
public static void main (String args[])
{
String strGuess;
gameJudge qm = new gameJudge();
qm.pickWord();
//Open console for input
Console c = System.console();
if (c == null) {
System.err.println("No console.");
System.exit(1);
}
while (!qm.gameEnded()) {
qm.displayWord();
System.out.format("You have %d attempts remaining.\n", qm.getRemainingGuesses() );
strGuess = c.readLine("Enter your guess: ");
qm.judgeGuess(strGuess);
}// end main loop
if (qm.blnWin() ) {
System.out.format("You Won! It took you %d attempts.\n", qm.nGuessesNeeded());
System.out.format( qm.getWord());
}// if player won
else {
System.out.format("You lost. The word was %s\n", qm.getWord());
}// if player won
}//end of main
}
Type or enter the source code that represents the person who picks the random word. Do this in a new NotePad file called game Judge.java. Save this file in the same folder as theGame.java.
import java.util.*;
public class gameJudge
{
private final int MAX_GUESSES = 8;
private String wordList[]= {"dog", "house", "element", "mother", "earth" };
private String goalWord;
private int nGuessesLeft;
private wordDisplay gb;
public gameJudge()
{
nGuessesLeft = MAX_GUESSES;
return;
}
public void pickWord()
{
Random obRand = new Random();
int i = obRand.nextInt(wordList.length);
goalWord = wordList[i];
gb = new wordDisplay(goalWord);
}
public boolean gameEnded() {
if (gb.blnMatch()) return true;
if (nGuessesLeft ==0) return true; else return false;
}
public boolean blnWin () {
return (gb.blnMatch() );
}//player won
public int getRemainingGuesses(){
return nGuessesLeft;
}
public String getWord () {
return goalWord;
}
public void displayWord() {
gb.showWord();
}
public int nGuessesNeeded () {
return MAX_GUESSES - nGuessesLeft;
}
public void judgeGuess(String strGuess) {
nGuessesLeft -=1;
gb.exposeLetters(strGuess);
}
};// end of gameJudge class
Enter the source code that shows the letters of the target word. Save this file with the others and call it wordDisplay.java:
import java.io.*;
public class wordDisplay
{
private String strDisplay;
private String strGoal;
private String lettersTried; // the letters player has tried
public wordDisplay(String str)
{
strGoal = str;
strDisplay = new String(str);
lettersTried = new String("");
strDisplay = strDisplay.replaceAll(".", "_");
return;
}
public void exposeLetters(String str)
{
String strRE;
//If a guess is longer than 1 char, uncover all or no letters
if (str.length() == 1) {
// concatenate new letter with letters already tested
lettersTried = lettersTried + str;
strRE = "[^" + lettersTried + "]";
// hide all non-matching chars: replace all letters in target that do NOT match pattern with the underscore
strDisplay = strGoal.replaceAll(strRE, "_");
}
else {
if (str.compareToIgnoreCase(strGoal) == 0) {
strDisplay = strGoal;
}
}
return;
}
public boolean blnMatch() {
return ( strGoal==strDisplay );
}
public void showWord() {
int i;
for (i=0; i<strGoal.length(); i++) {
System.out.format("%c ", strDisplay.charAt(i));
}
System.out.println("\n");
}
}
Compile and run the program: choose Start>"cmd" and use "cd <dir>" to navigate to the folder containing the .java source files. The "<dir>" refers to that directory. Set the path to the java compiler: "set PATH= C:\Program Files\Java\jdk1.6.0_14\bin". Replace the "1.6.0_14" with the correct version of your compiler, which you can get from the documentation in the JDK you downloaded.
Enter "javac *.java" followed by "java theGame," then play the game like this: notice the number of blank spaces; type one letter at a time until you guess the word; type the whole word once you recognize it.
Make copies of the original source files and change the game. Start by changing the wordList variable, and also the MAX_GUESSES constant.
0 Comments
Disqus
Fb Comments
Comments :

0 comments:

Post a Comment