2
\$\begingroup\$

So as you will soon be able to tell, I am very new to this programming. But I thought that the best way to learn would be to just jump on in and create my own game. Was wondering what things I should learn to improve my craft.

here is the main class package Main;

import java.util.ArrayList;
import java.util.Scanner;

public class story {

public static void main(String[]args){
    ArrayList<String> inventory= new ArrayList<String>();

int southnorth = 0;
boolean pretendtoberetarted = false;


    firstmission obj = new firstmission();
Scanner reader = new Scanner(System.in);

System.out.println(" Would you like to start a game,"
        + "1 = Yes 2 = no");
boolean telek = false;
int option = reader.nextInt();

 if(option == 1){
System.out.println("You have no idea who you are all that you have on you is 
    1000$ and a map with your current");
        System.out.println(" location on it and a firestarter,'PS: someone 
    must have put that map on you after drugging you");


   obj.firstline();
  }
    else{
   System.out.println(" Would you like to start a game,"
        + "1 = Yes 2 = no");



 }

}
}

here is the firstmission class`package Main;

 import java.util.ArrayList;
import java.util.Scanner;

public class firstmission {


 int eastwest =0;
int southnorth = 0;
 boolean telek = false;
boolean murderer = false;
boolean intownentrance = false;
boolean ownhouse = false;
boolean pretendedtobedisabled = false;
boolean shelterbuilt = false;


  City cityGate = new City();

  Scanner reader = new Scanner(System.in);

 void firstline(){
Scanner reader = new Scanner(System.in);
System.out.println("Its very cold and getting dark very fast, you know that a 
town is located about 10km north ");
   System.out.println("of you but you have no clue as to which way north 
is,");
  System.out.println("the sun just set to your right");
  System.out.println("Which way do you head");
  System.out.println("------------------");
  System.out.println(" 1 = forward");
  System.out.println(" 2 = Backwards");
  System.out.println(" 3 = random");
  System.out.println(" -----------------");

int option = reader.nextInt();
switch (option){

case 1: 
System.out.print(" You've been walking for roughly half a day now and you 
 have not found anything there is a cave");
System.out.println("just to your left it smells like rotten flesh, what do 
 you do?");
System.out.println("----------------");
System.out.println(" 1 = enter the cave ( WARNING IT SMELLS BAD)");
System.out.println(" 2 = Build a shelter");
System.out.println("----------------");

southnorth = -10;
System.out.println(" Your cordinateds are " + southnorth  + "kilometres 
 south, north and " + eastwest  + " kilometres east ,west");
System.out.println("Please note that the signs indicate the direction/ 
 negative indicate south and west");


Scanner reader1 = new Scanner(System.in);

int option2 = reader1.nextInt();
    switch(option2){
    case 1: System.out.println("You find an old wrinkled man who appears to 
     have been living here a while");
    System.out.println(" he is leaning against the wall of the cave and 
    appears to be in a deep meditative state");
    System.out.println(" just to his right their is a big crate of food 
    supplies, it could last you months");
    System.out.println("what do you do?");
    System.out.println("----------------");
    System.out.println(" 1 = sit beside him a start to meditate");
    System.out.println(" 2 = knock him out and take his food ");
    System.out.println(" 3 = wake him up");
Scanner reader2 = new Scanner(System.in);
int option3 = reader2.nextInt();
        switch(option3){
        case 1: System.out.println("You sit there for roughly a full day 
    meditating, why? you dont know, maybe you");
        System.out.println(" are hoping for what this man seems to have");
        System.out.println("after this elasped time the man starts speaking 
    to you he says very calmly");
        System.out.println("' won tong youza noww minee new sudent pwease'");
        System.out.println("to this you reply sure, you have pretty much 
     given up on life and this seems like the only viable option.");
        System.out.println(" So it goes that you spend years and years in isolation with your new master and he shows you many things");
        System.out.println("eventually you attain pure internal happiness and you live a good life.");
        System.out.println(" Your master has also taught you how to control matter with your mind and how ");
        System.out.println("never be hungry again through mastering breathing techniques, you now decide to venture off to the town.");

        southnorth = 10;
        telek = true;
        break;

        case 2: System.out.println(" Instead of knocking out the man you end up killing him, a few seconds later you hear footsteps around the corner.");
        System.out.println(" A small asian woman appears around the corner yelling 'Bing bong you killa my muster, now I must kiwl yousa");
        System.out.println(" You instanly kill her with a quick punch and then decide to head to bed");
        System.out.println("You wake up and it is somewhat warm outside, now that you know that you headed in the wrong direction do you retrace your steps"
        + " 1 = retrace steps back to town ' will take you a full days treck to reach the town now'"
        + " 2 = continue with what must be south");



        Scanner reader3 = new Scanner(System.in);
        int option4 = reader3.nextInt();


            switch(option4){
            case 1: southnorth = 10;

            break;

            case 2: southnorth = -30;
            }


            break;

        case 3: System.out.println(" The man dies instantly, you hear footsteps coming from around the corner");
        System.out.println(" A small asian woman appears around the corner yelling 'Bing bong you killa my muster, now I must kiwl yousa");
        System.out.println(" You instanly kill her with a quick punch and then decide to head to bed");
        System.out.println("You wake up and it is somewhat warm outside, now that you know that you headed in the wrong direction do you retrace your steps");
        System.out.println("----------------------");       
        System.out.println(" 1 = retrace steps back to town ' will take you a full days treck to reach the town now'");
        System.out.println(" 2 = continue with what must be south");
        System.out.println("----------------------");


        Scanner reader4 = new Scanner(System.in);
        int option5 = reader4.nextInt();


            switch(option5){
            case 1: southnorth = 10;

            break;

            case 2: southnorth = -30;
            }
        }
        break;

case 2: System.out.println(" You build a somewhat decent shelter that will make do while you are out"
        + " while you are out here surviving, you go to bed for the night");
System.out.println("You wake up and it is somewhat warm outside, now that you know that you headed in the wrong direction");
        System.out.println(" do you retrace your steps?");
        System.out.println("-----------------------");
        System.out.println("1 = retrace steps back to town ' will take you a full days treck to reach the town now'");
        System.out.println( "2 = continue with what must be south");
        System.out.println("-----------------------");
        shelterbuilt= true;

        Scanner reader5 = new Scanner(System.in);
        int option6 = reader5.nextInt();


            switch(option6){
            case 1: southnorth = 10;

            break;

            case 2: southnorth = -30;
            }


break;
}
}


if(southnorth == 10){

    City.cityGate();


    }
}

}

any my city class package Main;

import java.util.Scanner;

 public class City {

static int southnorth;

 static void cityGate(){
    System.out.println("-------------------");
    System.out.println("Welcome to Calgary");
    System.out.println("-------------------");
    System.out.println("You arrive at the town, it has a small wooden wall 
    around it and a guard standing at the gate.");
    System.out.println(" what do you do?");
    System.out.println(" Ask politely to enter the town = 1");
    System.out.println(" Ask forcefully to enter the town = 2");
    System.out.println(" pretend you are disabled ( who would deny a disabled 
     person entry)= 3");

    Scanner reader = new Scanner(System.in);
    int option = reader.nextInt();

    boolean intownentrance;
    switch(option){

    case 1:
        System.out.println("The guard tells you to turn away");
        cityGate();
        break;

        case 2:
            intownentrance = true;
            System.out.println("You are now in the town, its a rather small town with only about 1000 people living there.");

            System.out.println(" What would you like to do ?");
            System.out.println("Go to bar = 1");
            System.out.println(" Go to inn = 2");
            System.out.println(" leave town = 3");

            Scanner reader2 = new Scanner(System.in);

            int option3 = reader2.nextInt();

            switch(option3){


            }    
\$\endgroup\$
  • \$\begingroup\$ The code of the City class is incomplete. The package Main is not formatted correctly. \$\endgroup\$ – Roland Illig Jun 26 at 5:20
0
\$\begingroup\$
  • Are you using an IDE? These provide code formatting and refactor tools, use them.
  • You naming convention is clear, it reflects the problem domain but doesn't follow the Java style. Classes definitions should have a capitalised first letter. Use camelCase for method names to make them easier to read FirstMission class or firstLine method..
  • Use TDD and Junit to drive the development, Java IDEs support these natively and this will help you to develop a clean interface for your code.
  • Your main method does to0 much work, remove it and replace with an initialisation method and normal entry point that are not static. Create an instance of the class and use those method. You can use the constructor as initialisation method or seperate it.
  • Avoid using System.out create a show method that is easy to refactor to display the text. During development use something like [slf4j][1] longer term you more easily replace this with whatever UI system you choose.
  • Follow the single responsibility principle break down your classes into more methods, put everything under each switch into a seperate method.
  • Follow the interface segregation principle for Mission classes, give them a uniform entry point to make them polymorphic which will make this easier to expand and gain some reuse as you do so. Consider the same for Gates.
  • In your Story class Use a switch statement for your option instead of if(option == 1) like you have done in your firstmission class.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.