Below is a homework assignment I've just completed. I am curious as to how, with only a basic understanding of Java, I could make this code more robust, organized, efficient, etc. I intend to turn this in the way it is right now (so I won't be "taking credit for work that isn't mine"), but I want to see if there are ways in which I can improve. Thanks.
Code:
Code:
/********************
* NimGame.java
* Roger Tschida
********************/
import java.util.Scanner;
public class NimGame {
public static void main(String[] args) {
int stones = 0, turn, reduceStones = 0, wins = 0, total = 0, temp;
Scanner stdIn = new Scanner(System.in);
boolean invalidResponse;
String inputStr = " ";
System.out.println("T H E G A M E O F N I M");
do {
total++;
do {
System.out.print("\nEnter number of starting stones: ");
inputStr = stdIn.next();
try {
stones = Integer.parseInt(inputStr);
} catch (NumberFormatException nfe) {
System.out.println("Error: Invalid number");
}
} while (stones == 0);
turn = 0;
do {
System.out.print("Would you like to go first? (y/n): ");
inputStr = stdIn.next();
if (inputStr.equalsIgnoreCase("y") || inputStr.equalsIgnoreCase("yes")) {
turn = 2;
} else if (inputStr.equalsIgnoreCase("n") || inputStr.equalsIgnoreCase("no")) {
turn = 1;
} else {
System.out.println("Invalid Response");
}
} while (turn == 0);
System.out.print("\n");
while (stones > 0) {
if (turn == 1) {
if (stones % 3 == 0) {
System.out.println("The computer removes 2 stones.");
stones -= 2;
}
else {
System.out.println("The computer removes 1 stone.");
stones--;
}
turn++;
System.out.println("The number of stones left is " + stones + ".");
}
if (turn == 2 && stones > 0) {
do {
invalidResponse = false;
System.out.print("How many would you like to remove (1 or 2)? ");
inputStr = stdIn.next();
try {
reduceStones = Integer.parseInt(inputStr);
if (reduceStones != 1 && reduceStones != 2) {
throw new NumberFormatException();
}
if (reduceStones < 0 || stones - reduceStones < 0) {
throw new Exception();
}
} catch (NumberFormatException nfe) {
System.out.println("Error: Invalid response!");
invalidResponse = true;
} catch (Exception e) {
System.out.println("Error: Negative stones are invalid!");
invalidResponse = true;
}
} while (invalidResponse);
stones -= reduceStones;
turn--;
System.out.println("The number of stones left is " + stones + ".");
}
}
if (turn == 2) {
System.out.println("You win!");
wins++;
} else if (turn == 1) {
System.out.println("The computer wins!");
}
temp = 1;
while (temp == 1) {
System.out.print("\nDo you want to play again? (y/n): ");
inputStr = stdIn.next();
if (inputStr.equalsIgnoreCase("y") || inputStr.equalsIgnoreCase("yes") || inputStr.equalsIgnoreCase("n") || inputStr.equalsIgnoreCase("no")) {
temp = 0;
}
}
} while (inputStr.equals("y") || inputStr.equals("yes"));
System.out.println("\nResults:");
System.out.println("Computer Wins: " + (total - wins));
System.out.println("Player Wins: " + wins);
System.out.println("Total Games: " + total);
if (2 * wins == total) {
System.out.println("It is a DRAW!");
} else if (2 * wins < total) {
System.out.println("The Computer Wins Overall!");
} else {
System.out.println("You Win Overall!");
}
System.out.println("\nThank you, good bye!");
}
}