By BoLOBOSE payday loan

Java: Pass as many strings as you want through a method

When working on a text based role playing game I wanted a simple way to check for user input. An example of this is as follows:

You enter a small clearing, there is a path north, south and north-west. In the tree line you can also see something shimmering.

user: go north-west

In this case the user wants to go north-west but I would also accept other commands to go in that direction such as: “walk north-west”, “northwest”, “north west”, “go north west”, “go north-west.” The easiest way to properly do this is to pass multiple strings through a method. Naturally you can only have as many strings as declared or you would have to use the alternative of passing an array.

The code to solve this problem goes as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public boolean check(String... x){
//normally when passing a string it is required to do the following:
// String x, but with arguments we can pass it something like this:
//check("go north","head north","travel north","north")
 
for(String words : x){
//creates a loop declaring the variable words and loading it with
//the value of x
 
if(words.equalsIgnoreCase(input)){
//this checks to see if the word is equal to the users input.
 
return true;
// if so then return true
 
}
 
}
 
return false;
//after it loops through all of the possibilities it will return false.
 
}

In order to use this in the way that I have you must know that the variable “input” is loaded from the keyboard. As a matter of fact this method is placed inside of a class called “Input.” This class has a static variable called string, a way to prompt for a string, number, etc and checks the input for errors. An example of this class in action would be something like this:

1
2
3
4
5
6
7
8
inp1.promptInput();
if(inp1.check("attack","fight","1")){
attack();
}else if(inp1.check("flee","run","2")){
run();
}else{
System.out.println("Please enter a valid command.");
}

The code first asks the user to prompt one of the menu items. calls a method if check returns true or displays an error if there is nothing there.

Create a press enter to continue with java

Sometime a pause in your program is needed and the process of implementing this is easier then you would think. It involves one simple method that we will call into our main method when we need to pause.

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.Scanner;
public class appletTest1{
 
public static void main(String[] args) {
pauseProg();
 
}
public static void pauseProg(){
System.out.println("Press enter to continue...");
Scanner keyboard = new Scanner(System.in);
keyboard.nextLine();
}
}

The only important piece of code here that you will need to go over is the ‘keyboard.nextLine();’ This will take the object we created earlier in the program and set the next line breaker which is enter. When the program runs up to this line it waits for the user to import a string. By pressing enter you then process that string which does nothing because it does not get stored into a variable. The line breaks and the program continues.

Introduction to zombies, a java game

Introduction

Zombies is a text based rpg style game that involves a little more strategy since you are trying to rebuild mankind. The idea behind the game so far is to create something that users can play over and over again trying out different options to get a step up in the game.

There are multiple types of zombies,  16 to be exact(possibly more in the future), with varying attributes such as speed, strength and health. The chance of which you encounter these creatures is based on your level. You  can encounter these zombies on chance when collecting resources, finding survivors and hunting zombies. They will also appear when building outside of the confinements of your settlement walls or during the dark when lighting is poor.

As the game continues and your settlement grows bigger(if you even choose to have a settlement) the zombies will become more attracted to you as a food source as the population of survivors lessens. The whole game is based around the idea of building up something that was lost and the zombies are the one thing making that even more of a challenge.

Done

So far in the code the menu systems have been created, the zombies have a chance on entering battle, player stats and town stats are counted, new variables are brought into play, text commands are being inputted, battle systems are partially completed(pending on how much more is needed or will be added), day counters have been created.

On the list…

The game is well under way as far as being created goes, the only thing that is now required is the finishing touches that consist of; research, technology trees, weather, settlement chores and the ability to have survivors do those tasks for you, finishing up the battle system and adding a few fun Easter eggs.

Tags: , ,

Creating user input with menus in java

When working with console command lines in java I found myself needing a solution that works when in certain situations. The console was a java based game and the need was user input because the game was being built in a text style format.

In this article I will be using ‘equalsIgnoreCase();’ to match a string with what I would like it to be. I will also be using the ‘Scanner’ class, keyboard inputs, and String objects. Here is the code that I will be working with…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner;
 
public class userInputWithMenus {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
 
System.out.println("1. Menu.");
System.out.println("2. Item.");
System.out.println("3. Close.\n");
 
String inp = keyboard.nextLine();
 
if(inp.equalsIgnoreCase("1") || inp.equalsIgnoreCase("main")){
 
//execute what happens on main.
 
}else if(inp.equalsIgnoreCase("2") || inp.equalsIgnoreCase("item")){
 
//execute what happens on item
 
}else if(inp.equalsIgnoreCase("3") || inp.equalsIgnoreCase("exit")){
 
//execute what happens on close
 
}
 
}
 
}

The first thing we do is import a class that is built into java. Here we can access the scanner and assign a variable to it. Once we have our main class and main method setup we will display our menu and request the next line of text from the user and load this into the string ‘inp.’ The next block of code is the If else statements normally you would execute another method or run the program from this point on however that was not what I intended on covering here.

Inside of the if else blocks..

1
2
3
if(){
 
}

I placed code that matched the string contents with what I needed it to; in this case the menu items. I didn’t want the user to be forced into typing menu, item or exit so I made an or statement using ‘||’ to introduce the option of matching the string with the numbers. This is all done with a line that looks as follows.

1
inp.equalsIgnoreCase("main") || inp.equalsIgnoreCase("1");

You can just use ‘imp.equals();’ however I in creating this some copying and pasting was done. To get your menu to work even better you can try putting it into a loop and breaking the loop when the user enters a valid command. This can be easily done with a boolean and a while loop.

Java: basic for and while loops

The basis of for and while loops in Java are similar to most programming languages. First the loops type is called, parameters are applied, a block of statements are executed based on the parameters then finally the loop is closed off.

For Loop

The basis of the for loops is as follows:

1
2
3
4
for(Set of Parameters)
{ //opening the block
Block of statements to be executed.
} //closing the block

An example of the for loop will look something like this:

1
2
3
for (int a = 0; a <= 5; a++) {
System.out.println("integer a = " + a )
}

While Loop

Now we are going to show the basic outline of the while loop.

1
2
3
4
while(True or False statement, Boolean value)
{
Statement to be executed.
}

Do While

It is also possible to switch the statements around to create a do-while statement. This statement follows the while statements format except it switches around the while parameters and the block statements. It looks something like this:

1
2
3
do {
These statements while
} while (this Boolean value is true or false.)

These loops are quite simple to execute and easy to work with. The block statements can be replaced with other loops that are nested.