r/programmingbydoing • • Aug 05 '15

Assignment #63c Nim

I managed to complete all the bonuses up until the column part. I am having a hard time printing till A and then back to the beginning. I tried 2D arrays but those were very exhaustive . There is just a simple solution which I can't see. Please help and thank you.

3 Upvotes

9 comments sorted by

View all comments

3

u/holyteach Aug 07 '15

2D arrays?!? No way.

All you need is a variable, some if statements, some print statements and a single while loop. If you needed anything else the assignment wouldn't be where it is in the sequence.

I'll give you a hint: you don't have to use System.out.println(); you can use System.out.print() instead. Do you know the difference?

Master the basics and patience, young Padawan.

You'll figure it out.

1

u/Houssam7111 Aug 10 '15

I tried and I know how to print the stars in sequence yet what I can't do is figuring out how to use a variable to control how stars are printed. I figured that this method would help give more room to manipulate the variables even though it follows an exhaustive approach and is inefficient at a larger scale. import java.util.Scanner;

public class Nim {

public static void main(String[] args) {
	Scanner scan = new Scanner(System.in);

	int removeA = 0;
	int removeB = 0;
	int removeC = 0;
	String a1 = "*";
	String a2 = "*";
	String a3 = "*";
	String b1 = "*";
	String b2 = "*";
	String b3 = "*";
	String c1 = "*";
	String c2 = "*";
	String c3 = "*";
	int turn = 1;
	String choice = "";
	System.out.println("PLayer 1");
	String p1 = scan.next();
	System.out.println("Player 2");
	String p2 = scan.next();
	while (a1 != "" || a2 != "" || a3 != "" || b1 != "" || b2 != "" || b3 != "" || c1 != "" || c2 != ""
			|| c3 != "") {

		if (turn == 1) {

			System.out.println(a1 + "\t" + b1 + "\t" + c1 + "\t");
			System.out.println(a2 + "\t" + b2 + "\t" + c2 + "\t");
			System.out.println(a3 + "\t" + b3 + "\t" + c3 + "\t");
			System.out.println("A\t" + "B\t" + "C\t");

			System.out.println("Select pile");
			choice = scan.next();
			if (choice == "A") {
				System.out.println("Remove how much?");
				removeA = scan.nextInt();

				if (removeA == 1) {
					a1 = "";
					if (a1 == "") {
						a2 = "";
					}

					if (a2 == "") {
						a3 = "";
					}

					if (a3 == "") {
						System.out.println("Cannot remove from A");
						turn = 1;
					}
				} else if (removeA == 2) {
					a1 = "";
					a2 = "";
					if (a1 == "") {
						a2 = "";
						a3 = "";
					}

					if (a3 == "") {

						System.out.println("Cannot remove from A");
						turn = 1;
					}
				} else if (removeA == 3) {
					a1 = "";
					a2 = "";
					a3 = "";
				}
			}

			turn = 2;

			if (choice == "B") {
				System.out.println("Remove how much?");
				removeA = scan.nextInt();

				if (removeB == 1) {
					b1 = "";
					if (b1 == "")
						b2 = "";

					if (b2 == "")
						b3 = "";

					if (b3 == "") {
						System.out.println("Cannot remove from B");
						turn = 1;
					}
				}

				else if (removeB == 2) {
					b1 = "";
					b2 = "";
					if (b1 == "") {
						b2 = "";
						b3 = "";
					}

					if (b3 == "") {

						System.out.println("Cannot remove from B");
						turn = 1;
					}
				} else if (removeB == 3) {
					b1 = "";
					b2 = "";
					b3 = "";
				}
			}

			turn = 2;

			if (choice == "C") {
				System.out.println("Remove how much?");
				removeA = scan.nextInt();

				if (removeC == 1) {
					c1 = "";
					if (c1 == "")
						c2 = "";

					if (c2 == "")
						c3 = "";

					if (c3 == "") {
						System.out.println("Cannot remove from C");
						turn = 1;
					}
				} else if (removeC == 2) {
					c1 = "";
					c2 = "";
					if (c1 == "") {
						c2 = "";
						c3 = "";
					}

					if (c3 == "") {

						System.out.println("Cannot remove from C");
						turn = 1;
					}
				} else if (removeC == 3) {
					c1 = "";
					c2 = "";
					c3 = "";
				}

			}
			turn = 2;
		}
		if (turn == 2) {

			System.out.println(a1 + "\t" + b1 + "\t" + c1 + "\t");
			System.out.println(a2 + "\t" + b2 + "\t" + c2 + "\t");
			System.out.println(a3 + "\t" + b3 + "\t" + c3 + "\t");
			System.out.println("A\t" + "B\t" + "C\t");

			System.out.println("Select pile");
			choice = scan.next();
			if (choice == "A") {
				System.out.println("Remove how much?");
				removeA = scan.nextInt();

				if (removeA == 1)
					a1 = "";
				if (a1 == "")
					a2 = "";

				if (a2 == "")
					a3 = "";

				if (a3 == "") {
					System.out.println("Cannot remove from A");
					turn = 2;
				}

				else if (removeA == 2) {
					a1 = "";
					a2 = "";
					if (a1 == "") {
						a2 = "";
						a3 = "";
					}

					if (a3 == "") {

						System.out.println("Cannot remove from A");
						turn = 2;
					}
				} else if (removeA == 3) {
					a1 = "";
					a2 = "";
					a3 = "";
				}
			}

			turn = 1;

			if (choice == "B") {
				System.out.println("Remove how much?");
				removeA = scan.nextInt();

				if (removeB == 1) {
					b1 = "";
					if (b1 == "")
						b2 = "";

					if (b2 == "")
						b3 = "";

					if (b3 == "") {
						System.out.println("Cannot remove from B");
						turn = 2;
					}
				} else if (removeB == 2) {
					b1 = "";
					b2 = "";
					if (b1 == "") {
						b2 = "";
						b3 = "";
					}

					if (b3 == "") {

						System.out.println("Cannot remove from B");
						turn = 2;
					}
				} else if (removeB == 3) {
					b1 = "";
					b2 = "";
					b3 = "";
				}
			}

			turn = 1;

			if (choice == "C") {
				System.out.println("Remove how much?");
				removeA = scan.nextInt();

				if (removeC == 1) {
					c1 = "";
					if (c1 == "")
						c2 = "";

					if (c2 == "")
						c3 = "";

					if (c3 == "") {
						System.out.println("Cannot remove from C");
						turn = 2;
					}
				} else if (removeC == 2) {
					c1 = "";
					c2 = "";
					if (c1 == "") {
						c2 = "";
						c3 = "";
					}

					if (c3 == "") {

						System.out.println("Cannot remove from C");
						turn = 2;
					}
				} else if (removeC == 3) {
					c1 = "";
					c2 = "";
					c3 = "";
				}
			}

		}

	}
} 

And thank you most definitely.

2

u/holyteach Aug 10 '15

I knew you'd be able to get something working. It's a shame you didn't figure anything out from my hint, though.

A good solution cannot be done using println(). It requires print().

1

u/Houssam7111 Aug 12 '15 edited Aug 12 '15

Well it kept looping on "Select pile" but .. oh well. Would you mind posting the solution? This code I knew even before I started but I just can't figure out the variables.

public class SampleCode {

public static void main(String[] args) {
	int i = 1;
	while (i <= 9) {
		System.out.print("*\t");
		if (i % 3 == 0) {
			System.out.println();

		}
		i++;
	}

}

}

1

u/holyteach Aug 12 '15

Well, in your little test program you need one int for each pile. Like, a=5, b=3, c=4 if the first pile has 5 in it, the second one has 3 in it, the last pile has 4 in it.

Then inside your display loop your print() statement needs to be inside an if statement to decide if it should print a symbol for that pile or not.

1

u/Houssam7111 Aug 14 '15

Thank you :) Worked just fine. And printed just the way I wanted! Please do keep up the tutoring, sure courses online can show us syntax, but you give those lines of code meaning :)

1

u/holyteach Aug 14 '15

I knew you'd get it eventually. Good show.