dimanche 5 novembre 2017

Why are my lines printing several two times instead of one? How do I use a loop to avoid invalid answers?

I'm using a main method and two smaller methods to play a human vs. computer game of Nim. However, when it is the computer's turn, sometimes two lines are printed with different output values. For example, the output may say:

*** There are 66 marbles in the pile.
Computer will take 3.
Computer will take 2.

I'm not sure why it continues to print twice. It doesn't always happen either. Here is my full code:

import java.util.Scanner;

public class Program10 {

   public static void main(String args[]) {

      Scanner input = new Scanner(System.in);
      int take = 0;
      int turn = (int)(Math.random() * 2 + 0);
      int start = turn;
      int marbles = (int)(Math.random() * 100 + 10);
      int moveType;
      while (marbles > 0) {
         System.out.println("\n*** There are " + marbles + " marbles in the pile.");
         if (turn % 2 == 0) {
            if (marbles == 1) {
               System.out.print("How many do you want to take (1-1)");
               take = input.nextInt();
               while (take < 1 && take >= (marbles / 2)) {
                  System.out.println("\n\n*** Illegal entry!");
                  System.out.print("How many do you want to take (1-" + (marbles / 2) + ") ");
                  take = input.nextInt();
               }
            }
            else {
               System.out.print("How many do you want to take (1-" + (marbles / 2) + ") ");
               take = input.nextInt();
               while (take < 1 && take >= (marbles / 2)) {
                  System.out.println("\n\n*** Illegal entry!");
                  System.out.print("How many do you want to take (1-" + (marbles / 2) + ") ");
                  take = input.nextInt();
               }
            }
         }
         else {
            moveType = (int)(Math.random() * 2 + 0);
            if (moveType == 0) {
               take = getSmartMove(marbles);
            }
            else {
               take = getStupidMove(marbles);
            }
         }
         marbles = marbles - take;
         turn++;
      }
      if (turn % 2 == start) {
         System.out.println("Human wins!");
      }
      else {
         System.out.println("Computer wins :(");
      }
   }


   public static int getSmartMove(int marbles) {
      int take;
      if (marbles == 1) {
         take = 1;
         System.out.println("Computer will take " + take + ".");
      }
      if (marbles > 63) {
         take = (marbles - 63);
         System.out.println("Computer will take " + take + ".");
      }
      if (marbles > 31 && marbles < 63) {
         take = (marbles - 31);
         System.out.println("Computer will take " + take + ".");
      }
      if (marbles > 15 && marbles < 31) {
         take = (marbles - 15);
         System.out.println("Computer will take " + take + ".");
      }
      if (marbles > 7 && marbles < 15) {
         take = (marbles - 7);
         System.out.println("Computer will take " + take + ".");
      }
      if (marbles > 3 && marbles < 7) {
         take = (marbles - 3);
         System.out.println("Computer will take " + take + ".");
      }
      else {
         take = (int)(Math.random() * (marbles / 2) + 1);
         System.out.println("Computer will take " + take + ".");
      }
      return take;
   }


   public static int getStupidMove(int marbles) {
      int take = (int)(Math.random() * (marbles / 2) + 1);
      System.out.println("Computer will take " + take + ".");
      return take;
   }

}

The user should also not be allowed to enter a value less than one or higher than the number of marbles divided by two. The valid input range is listed beside the question like so:

How many do you want to take (1-33)

However, it seems that I can still enter invalid numbers like 0. Please let me know if I'm doing something wrong with my coding. I've been trying to find patterns that may lead me to the answer, but so far I'm blank. Thank you!




Aucun commentaire:

Enregistrer un commentaire