user3024595
user3024595

Reputation:

How to reduce code in main method by making new methods for my code?

Hi I am using Eclipse to program a game. Basically, the user chooses 1. Even or 2. Odd A randomly generated dice rolls. If the user lands on the choice he made (even number or odd number) he stays otherwise, he moves 5 steps back. The user is vsing the computer. So if user is Even, computer is Odd, and vice versa

My problem is I don't know how to call this kind of method. Please look at my code and give me advice. If it can be better let me know also.

package Testing;

import java.util.Scanner;
import java.util.Random;

  public class hundredsteps {

  public static int playerChoice(){
   System.out.println("Please choose an option below");
   System.out.println("1. Even");
   System.out.println("2.  Odd");
   Scanner input = new Scanner(System.in);
   int choice = input.nextInt();

   return choice;
  // player set EVEN by default
  }

  public static int playerRoll(){

   System.out.println("Press enter to roll");
   Scanner input = new Scanner(System.in);
   String roll = input.nextLine();
   Random generator = new Random();
   int dice1 = generator.nextInt(6)+1;
   int dice2 = generator.nextInt(6)+1;
   int sumOfDice = dice1 + dice2;
   return sumOfDice;
  }
  public static int cpuRoll(){
   Random generator = new Random();
   int dice1 = generator.nextInt(6)+1;
   int dice2 = generator.nextInt(6)+1;
   int sumOfDice = dice1 + dice2;
   return sumOfDice;
  }

  public static String gameWinner(int player, int cpu){

   String winner = " ";

   if (player == 100 && cpu == 100){
    winner = " TIE ";
    System.out.println("TIE GAME!!");
    }else if (player == 100){
    System.out.println();
    winner = " player ";
    System.out.println("Congratulations! You've won!");
    }else if (cpu == 100){
     winner = " cpu ";
    System.out.println("Sorry, you lost. The computer won this round");
    }
   return winner;
  }


  public static void main(String[] args) {


  int playerPosition = 0, cpuPosition = 0;
  int playerRoll = 0, cpuRoll = 0;
  do{
   System.out.println();
   playerRoll = playerRoll();
   System.out.println();
   System.out.println("You rolled " +playerRoll);
   playerPosition = playerPosition + playerRoll;

   if (playerPosition % 2 == 0){
   System.out.println("You are now on step number " +playerPosition);
   } else if (playerPosition % 2 != 0){
   System.out.println("You landed on an odd number! You must move 5 steps back");
   playerPosition = playerPosition - 5;
   System.out.println("You are now on step number " +playerPosition);
   }
   cpuRoll = cpuRoll();
   System.out.println("The computer rolled "+cpuRoll);
   cpuPosition = cpuPosition + cpuRoll;


  if (cpuPosition % 2 != 0){
   System.out.println("The computer is now on step number " +cpuPosition);
   } else if(cpuPosition % 2 == 0){
   System.out.println("The computer landed on an even number! The computer moved 5 steps back");
   cpuPosition = cpuPosition - 5;
   System.out.println("The computer is now on step number " +cpuPosition);
   System.out.println();
   }


  if (playerPosition > 100){
   System.out.println("You rolled too high!");
   System.out.println("You moved 20 steps back");
   playerPosition = playerPosition - 20;
   } if (cpuPosition > 100){
   System.out.println("The computer rolled too high");
   System.out.println("The computer moves 20 steps back");
   cpuPosition = cpuPosition - 20;
  }
   } while (playerPosition <= 99 && cpuPosition <= 99);

  gameWinner(playerPosition, cpuPosition);

}

}

Upvotes: 0

Views: 303

Answers (2)

dm78
dm78

Reputation: 1600

You must be making this too hard for yourself. If you want all of the code from you main() in another method then make it so.

public static void main(String[] args) {

    newMethod();
}

public static void newMethod() {

    // All the stuff from your old main goes in here
}

I can only imagine that you don't understand static methods and why the methods you call from your main() must be static.

Alternatively, do exactly what @nexus_2006 said and put your game in it's own class and then in your main() create an instance of that class and start the game from there.

public static void main(String[] args) {

    DiceGame game = new DiceGame();
    game.play();
}

The main() above can even go inside DiceGame.java. Either way works fine.

Upvotes: 0

nexus_2006
nexus_2006

Reputation: 754

Do you mean all the methods you call in main() keep throwing compile errors if they aren't static, so you "can't" call them?

If thats what you mean, you need to make some code to run/test your code. You need some objects. Java is object oriented (OO), so always think about "things" rather than "procedures/sequences of commands."

For example, make a test class, that will "contain" your "game object"

public class GameTest {
    public static void main(String[] args) {
        DiceGame game = new DiceGame();
    }
}

Put your game in a separate class ( a whole new file):

public class DiceGame {
    //all your primitive/object references

    //add a constructor
    public DiceGame() {
        //initialize primitives/objects
        play();
    }

    public void play() {
        //game logic goes here
        //the stuff you have in main() right now
        //this isn't a static method, so call any other methods you want
    }

    //put your other methods here
}

Then run the file GameTest, not DiceGame. This is just one of several patterns you can use to get your code running.

If this is not what you mean, can you clarify your question?

Upvotes: 1

Related Questions