1 reason to get akismet

Akismet is a plug-in that WordPress uses to stop spam. It is very helpful and I recently came across an old WordPress blog that I thought about revising. This blog did not have the plug-in initialized and thus I was led to find some spam with not so nice words.

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.

Programmers are…