0

I am supposed to be writing the game of nim. I am having trouble finding out how to fix the compile error

cannot find symbol - method getPlayer()

Also, is this the only problem you see? Or are there other issues that would cause this program to fail to compile or work properly.

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

public class Nim {
    private int n;
    private int compMode;
    private int numberLeft;
    private int numberTaken;
    private boolean whoseTurn;
    private String inputName;
    private String name;
    private String play;
    private boolean yes;
    Scanner in = new Scanner(System.in);
    Random num = new Random();

    public void setState() {
        numberLeft = 100;

        numberTaken = numberLeft;
    }

    public String getPlayer() {
        inputName = in.next("");
        inputName = name;
        return name;
    }

    public void getCompPlay() {
        compMode = num.nextInt(2);
        if (compMode == 0) System.out.println("The computer is in smart mode");

        if (compMode == 1) System.out.println("The computer is in random mode");
    }

    public void playGame() {
        if (whoseTurn == true) {
            System.out.println(name + "It is your turn...");
            System.out.printf("Please enter the number you wish to take from the pile (Must be less than " + (numberLeft / 2) + "): ");
            numberTaken = in.nextInt();
            numberLeft -= numberTaken;
            System.out.println("The number left is " + numberLeft);
            whoseTurn = false;
        }

        if (whoseTurn == false) {
            System.out.println("It is the computer's turn...");
            if (compMode == 0) {
                numberLeft = smartComputer(numberLeft);
                System.out.println("The number left is " + numberLeft);
            }

            if (compMode == 1) {
                numberLeft -= num.nextInt(numberLeft / 2);
                System.out.println("The number left is " + numberLeft);
            }
            whoseTurn = true;
            return;
        }

        if (yes == true) {

        }

        if (numberLeft <= 1) {
            if (whoseTurn = false) {
                System.out.println("You Win!");
            } else {
                System.out.println("You're horrible...you lost to a computer.");
            }
        }

        if (numberLeft <= 1) {
            if (whoseTurn = false) {
                System.out.println("You Win!");
            } else {
                System.out.println("you lost to a computer.");
            }
        }

    }

    public static int smartComputer(int num) {
        int power = 2;
        while (power < num) {
            power *= 2;
        }
        power /= 2;
        num = power - 1;
        return num;
    }

    public boolean playAnother() {
        System.out.println("/nPlay Again? (y/n)");
        play = in.next("");
        if (play.equalsIgnoreCase("y")) return true;
        else return false;
    }

    public void displayTotals() {}
}

And here is my Tester

public class NimTester {
    public static void header() {
        System.out.println("Eric Magnusson");
        System.out.println("AP Comp Sci");
        System.out.println("Game of Nim (P6.16)");
    }
    public static void main() {
        Nim nim = new Nim(getPlayer(), getCompPlay());

        do {
            nim.setState();
            nim.playGame();
            nim.printWinner();
        } while (playAnother());
        nim.displayTotals();
    }
}
bluenote10
  • 23,414
  • 14
  • 122
  • 178
  • `getPlayer()` is a method of the class `Nim`. You need an instance of `Nim` to call it. – dguay Nov 09 '15 at 19:45
  • `if (whoseTurn = false)` is a problem. Should be `if (whoseTurn == false)`, but I'll let you in on a hint that it can also be written `if (!whoseTurn)` – OneCricketeer Nov 09 '15 at 19:49

1 Answers1

0

Your class either has no specific Constructor, or it is not shwon in your code. To learn more about Construcotrs, please read the documentation here. To put it simple, a Constructor is a method that allows the JVM to organize the memory and initialize the object. The default object constructor is a non-argument constructor that sets each child variable the default value.

Example:

Class Car{
   public Car(){
      System.out.print("car is built.");
   }
}

Car aCar = new Car();

What you have is Nim nim = new Nim(getPlayer(), getCompPlay()); therefore your Nimclass has to have a public Nim(String x, String y) method.

Another problem with the code shown is, you are using an object method to create the object... Either the method has to be static (read about it here), or it has to not depend on the object you are creating. To simplify your program, just save that value before using it in a variable, then use that variable in the object construction. Or even create a staic method like: public static String getJohn(){return "John"}, and then your code will work past that point.

Other issues with the code are related to readability, please read on Java Naming conventions, on how to name your variables, they will help maintenance, as well as allow other programmers to assist you.

Community
  • 1
  • 1
Bonatti
  • 2,778
  • 5
  • 23
  • 42
  • These are all comments rather than answers to the problem. – OneCricketeer Nov 09 '15 at 19:58
  • Yea didn't write any code for that part yet. I wanted this problem to compile first – Eric Magnusson Nov 09 '15 at 20:06
  • @cricket_007 The questions was `is this the only problem you see? Or are there other issues that would cause this program to fail to compile or work properly. `... having a constructor non-existant, having a non-boolean as expression, as well as a non-started variable compared are issues. – Bonatti Nov 10 '15 at 10:19
  • Sure, but you could update your answer with explanations of why those are problems and how to fix them rather than simply point them out. For example, maybe OP doesn't know what a constructor is or why one is needed, or the difference is between using static and nonstatic methods, etc. – OneCricketeer Nov 10 '15 at 12:40