patternjavaMinor
Adventure text game
Viewed 0 times
textgameadventure
Problem
I'm making a text based java game to help me learn java. I would like to know if I am doing things ok and if there is a better way to do the things I'm doing thanks. I have two separate files.
adventure.java
the second is TheGame.java
```
package sam;
import java.util.Scanner;
import org.omg.Messaging.SyncScopeHelper;
public class TheGame {
public static void Start(String args[]){
//Scanner
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
//Money and things
int money = 20;
@SuppressWarnings("unused")
boolean Sword = false;
@SuppressWarnings("unused")
boolean Axe = false;
int damage = 0;
double health = 1;
//first question
System.out.println("Hello what would you like t
adventure.java
package sam;
import java.util.Scanner;
public class Adventure extends TheGame{
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("Welcome to my adventure game, in this game you have certain quests and you can pick options each time. There is shops where you can buy wepons and where you can upgrade damage."
+ "In the shops it will ask you to pick numbers that go with each wepon and the wepons cost money. If you press a number that isnt on the shop then you will skip that part with no wepon or "
+ "upgrades so you will probably lose. ");
System.out.println("--------------------------------");
System.out.println("What is your name: ");
String name = input.next();
System.out.println("--------------------------------");
System.out.println("Hello " + name + " Would you like to start type 1 if you would");
int Wantstostart = input.nextInt();
if(Wantstostart==1){
System.out.println("Ok lets start then");
System.out.println("--------------------------------");
System.out.println("--------------------------------");
Start(null);
}
}
}the second is TheGame.java
```
package sam;
import java.util.Scanner;
import org.omg.Messaging.SyncScopeHelper;
public class TheGame {
public static void Start(String args[]){
//Scanner
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
//Money and things
int money = 20;
@SuppressWarnings("unused")
boolean Sword = false;
@SuppressWarnings("unused")
boolean Axe = false;
int damage = 0;
double health = 1;
//first question
System.out.println("Hello what would you like t
Solution
Usually I start with pros, but I didn't find any. Sorry...
cons
General coding
You have only static methods.
Java is an object oriented programming language, so get used to use objects.
You may later find the benefits of it when it comes to polymorphism.
Naming
You don't use Java Naming conventions which is in general:
Inheritance
Your class
This looks OK at first glance since you schould use inheritance when you have an "is a" relationship which is obviously true for Adventure and TheGame by means of the words.
But if we look closer to what the classes really do then it is false:
Code duplication
This code has lots of duplicated code.
This should be extracted to parameterized methods.
cons
General coding
You have only static methods.
Java is an object oriented programming language, so get used to use objects.
You may later find the benefits of it when it comes to polymorphism.
Naming
You don't use Java Naming conventions which is in general:
- Identifiers use CamelCaseNaming except constants which are all UPPER_CASE.
- Class names begin with capital letter.
- Methods and variables begin with a lower case letter.
- Methods begin with a "do" word (a verb).
- Variables are "thing" words (nouns).
Inheritance
Your class
Adventure extends your TheGame class.This looks OK at first glance since you schould use inheritance when you have an "is a" relationship which is obviously true for Adventure and TheGame by means of the words.
But if we look closer to what the classes really do then it is false:
Adventure is only launching TheGame so its name is misleading and the inheritance is not justified.Code duplication
This code has lots of duplicated code.
This should be extracted to parameterized methods.
Context
StackExchange Code Review Q#147705, answer score: 4
Revisions (0)
No revisions yet.