How to do looping in this Java game?
I am a college student just learning the basics of programming in Java. I was given the task of recreating the game of NIM playing against a computer. The rules I was given where that there are 3 piles of 10, you can remove any amount from one pile at a time and the one to remove the last point loses the game.
This is what I currently have done, it plays through one turn for the user and one turn for the computer. I know I need to loop this process until the game is over but am not sure how or the best way to do so.
public class Assing3
public static void main(String args)
Nim test = new Nim();
test.playerTurn();
test.calculate();
test.computerTurn();
test.display();
import java.util.Random;
import java.util.Scanner;
public class Nim
Scanner input = new Scanner(System.in);
Random num = new Random();
private String pile;
private int a, b, c, remove;
private boolean turn = true;
private int compPile = num.nextInt(2);
private int compRemove = num.nextInt(9) + 1;
public Nim()
a = 10;
b = 10;
c = 10;
public void playerTurn()
System.out.println("Welsom to the game of NIM");
System.out.println("We will use the misère rules...");
//display the the piles and size
System.out.println("tAtBtC");
System.out.println("t" + a + "t" + b + "t" + c);
//user trun
while (turn == true) //end true while 1
while (turn == true) //select number to remove
System.out.println("How many do you want to remove from the: " + pile + " pile?");
remove = input.nextInt();
if (remove > 0) else
System.err.println("Must ener a number greater then 0...");
//end true while 2
turn = false;
//end of play method
public void computerTurn()
System.out.println("It is now the computers turn...");
System.out.println("The computer is removing: " + compRemove + " from the: " + compPile + " pile...(0 = a, 1 = b, 2 = c ");
while (turn == false)
if (compPile == 0)
if (a == 0)
compPile = num.nextInt(2);
System.err.println("Selected pile is empty... New pile is: " + compPile);
continue;
else if (compRemove > a)
compRemove = num.nextInt(a) + 1;
System.err.println("The amount the computer wanted to remove is to hight... New number to be removed is: " + compRemove);
continue;
else
a = a - compRemove;
if (compPile == 1)
if (b == 0)
compPile = num.nextInt(2);
System.err.println("Selected pile is empty... New pile is: " + compPile);
continue;
else if (compRemove > b)
compRemove = num.nextInt(b) + 1;
System.err.println("The amount the computer wanted to remove is to hight... New number to be removed is: " + compRemove);
continue;
else
b = b - compRemove;
if (compPile == 2)
if (c == 0)
compPile = num.nextInt(2);
System.err.println("Selected pile is empty... New pile is: " + compPile);
continue;
else if (compRemove > c)
compRemove = num.nextInt(c) + 1;
System.err.println("The amount the computer wanted to remove is to hight... New number to be removed is: " + compRemove);
continue;
else
c = c - compRemove;
break;
public void calculate()
if (pile.equals("A"))
a = a - remove;
if (pile.equals("B"))
b = b - remove;
if (pile.equals("C"))
c = c - remove;
//end of calculate method
public void display()
System.out.println("tAtBtC");
System.out.println("t" + a + "t" + b + "t" + c);
java nim-game
add a comment |
I am a college student just learning the basics of programming in Java. I was given the task of recreating the game of NIM playing against a computer. The rules I was given where that there are 3 piles of 10, you can remove any amount from one pile at a time and the one to remove the last point loses the game.
This is what I currently have done, it plays through one turn for the user and one turn for the computer. I know I need to loop this process until the game is over but am not sure how or the best way to do so.
public class Assing3
public static void main(String args)
Nim test = new Nim();
test.playerTurn();
test.calculate();
test.computerTurn();
test.display();
import java.util.Random;
import java.util.Scanner;
public class Nim
Scanner input = new Scanner(System.in);
Random num = new Random();
private String pile;
private int a, b, c, remove;
private boolean turn = true;
private int compPile = num.nextInt(2);
private int compRemove = num.nextInt(9) + 1;
public Nim()
a = 10;
b = 10;
c = 10;
public void playerTurn()
System.out.println("Welsom to the game of NIM");
System.out.println("We will use the misère rules...");
//display the the piles and size
System.out.println("tAtBtC");
System.out.println("t" + a + "t" + b + "t" + c);
//user trun
while (turn == true) //end true while 1
while (turn == true) //select number to remove
System.out.println("How many do you want to remove from the: " + pile + " pile?");
remove = input.nextInt();
if (remove > 0) else
System.err.println("Must ener a number greater then 0...");
//end true while 2
turn = false;
//end of play method
public void computerTurn()
System.out.println("It is now the computers turn...");
System.out.println("The computer is removing: " + compRemove + " from the: " + compPile + " pile...(0 = a, 1 = b, 2 = c ");
while (turn == false)
if (compPile == 0)
if (a == 0)
compPile = num.nextInt(2);
System.err.println("Selected pile is empty... New pile is: " + compPile);
continue;
else if (compRemove > a)
compRemove = num.nextInt(a) + 1;
System.err.println("The amount the computer wanted to remove is to hight... New number to be removed is: " + compRemove);
continue;
else
a = a - compRemove;
if (compPile == 1)
if (b == 0)
compPile = num.nextInt(2);
System.err.println("Selected pile is empty... New pile is: " + compPile);
continue;
else if (compRemove > b)
compRemove = num.nextInt(b) + 1;
System.err.println("The amount the computer wanted to remove is to hight... New number to be removed is: " + compRemove);
continue;
else
b = b - compRemove;
if (compPile == 2)
if (c == 0)
compPile = num.nextInt(2);
System.err.println("Selected pile is empty... New pile is: " + compPile);
continue;
else if (compRemove > c)
compRemove = num.nextInt(c) + 1;
System.err.println("The amount the computer wanted to remove is to hight... New number to be removed is: " + compRemove);
continue;
else
c = c - compRemove;
break;
public void calculate()
if (pile.equals("A"))
a = a - remove;
if (pile.equals("B"))
b = b - remove;
if (pile.equals("C"))
c = c - remove;
//end of calculate method
public void display()
System.out.println("tAtBtC");
System.out.println("t" + a + "t" + b + "t" + c);
java nim-game
What denotes when the game is over?
– GBlodgett
Nov 13 '18 at 1:31
1
witch plays - too scary for me
– Scary Wombat
Nov 13 '18 at 1:37
1
Well, if you want a loop, you need a loop statement. It looks like you are familiar withwhile
loops, so that would be a good place to start...
– Kevin Anderson
Nov 13 '18 at 2:01
Once the players turn and the computers first turn are done the program simply terminates since there is no loop... and i don't think its as simple as adding another loop because the program separated in method and called to the main method on a different class (the "public class Assing3")
– Marcus
Nov 13 '18 at 3:58
add a comment |
I am a college student just learning the basics of programming in Java. I was given the task of recreating the game of NIM playing against a computer. The rules I was given where that there are 3 piles of 10, you can remove any amount from one pile at a time and the one to remove the last point loses the game.
This is what I currently have done, it plays through one turn for the user and one turn for the computer. I know I need to loop this process until the game is over but am not sure how or the best way to do so.
public class Assing3
public static void main(String args)
Nim test = new Nim();
test.playerTurn();
test.calculate();
test.computerTurn();
test.display();
import java.util.Random;
import java.util.Scanner;
public class Nim
Scanner input = new Scanner(System.in);
Random num = new Random();
private String pile;
private int a, b, c, remove;
private boolean turn = true;
private int compPile = num.nextInt(2);
private int compRemove = num.nextInt(9) + 1;
public Nim()
a = 10;
b = 10;
c = 10;
public void playerTurn()
System.out.println("Welsom to the game of NIM");
System.out.println("We will use the misère rules...");
//display the the piles and size
System.out.println("tAtBtC");
System.out.println("t" + a + "t" + b + "t" + c);
//user trun
while (turn == true) //end true while 1
while (turn == true) //select number to remove
System.out.println("How many do you want to remove from the: " + pile + " pile?");
remove = input.nextInt();
if (remove > 0) else
System.err.println("Must ener a number greater then 0...");
//end true while 2
turn = false;
//end of play method
public void computerTurn()
System.out.println("It is now the computers turn...");
System.out.println("The computer is removing: " + compRemove + " from the: " + compPile + " pile...(0 = a, 1 = b, 2 = c ");
while (turn == false)
if (compPile == 0)
if (a == 0)
compPile = num.nextInt(2);
System.err.println("Selected pile is empty... New pile is: " + compPile);
continue;
else if (compRemove > a)
compRemove = num.nextInt(a) + 1;
System.err.println("The amount the computer wanted to remove is to hight... New number to be removed is: " + compRemove);
continue;
else
a = a - compRemove;
if (compPile == 1)
if (b == 0)
compPile = num.nextInt(2);
System.err.println("Selected pile is empty... New pile is: " + compPile);
continue;
else if (compRemove > b)
compRemove = num.nextInt(b) + 1;
System.err.println("The amount the computer wanted to remove is to hight... New number to be removed is: " + compRemove);
continue;
else
b = b - compRemove;
if (compPile == 2)
if (c == 0)
compPile = num.nextInt(2);
System.err.println("Selected pile is empty... New pile is: " + compPile);
continue;
else if (compRemove > c)
compRemove = num.nextInt(c) + 1;
System.err.println("The amount the computer wanted to remove is to hight... New number to be removed is: " + compRemove);
continue;
else
c = c - compRemove;
break;
public void calculate()
if (pile.equals("A"))
a = a - remove;
if (pile.equals("B"))
b = b - remove;
if (pile.equals("C"))
c = c - remove;
//end of calculate method
public void display()
System.out.println("tAtBtC");
System.out.println("t" + a + "t" + b + "t" + c);
java nim-game
I am a college student just learning the basics of programming in Java. I was given the task of recreating the game of NIM playing against a computer. The rules I was given where that there are 3 piles of 10, you can remove any amount from one pile at a time and the one to remove the last point loses the game.
This is what I currently have done, it plays through one turn for the user and one turn for the computer. I know I need to loop this process until the game is over but am not sure how or the best way to do so.
public class Assing3
public static void main(String args)
Nim test = new Nim();
test.playerTurn();
test.calculate();
test.computerTurn();
test.display();
import java.util.Random;
import java.util.Scanner;
public class Nim
Scanner input = new Scanner(System.in);
Random num = new Random();
private String pile;
private int a, b, c, remove;
private boolean turn = true;
private int compPile = num.nextInt(2);
private int compRemove = num.nextInt(9) + 1;
public Nim()
a = 10;
b = 10;
c = 10;
public void playerTurn()
System.out.println("Welsom to the game of NIM");
System.out.println("We will use the misère rules...");
//display the the piles and size
System.out.println("tAtBtC");
System.out.println("t" + a + "t" + b + "t" + c);
//user trun
while (turn == true) //end true while 1
while (turn == true) //select number to remove
System.out.println("How many do you want to remove from the: " + pile + " pile?");
remove = input.nextInt();
if (remove > 0) else
System.err.println("Must ener a number greater then 0...");
//end true while 2
turn = false;
//end of play method
public void computerTurn()
System.out.println("It is now the computers turn...");
System.out.println("The computer is removing: " + compRemove + " from the: " + compPile + " pile...(0 = a, 1 = b, 2 = c ");
while (turn == false)
if (compPile == 0)
if (a == 0)
compPile = num.nextInt(2);
System.err.println("Selected pile is empty... New pile is: " + compPile);
continue;
else if (compRemove > a)
compRemove = num.nextInt(a) + 1;
System.err.println("The amount the computer wanted to remove is to hight... New number to be removed is: " + compRemove);
continue;
else
a = a - compRemove;
if (compPile == 1)
if (b == 0)
compPile = num.nextInt(2);
System.err.println("Selected pile is empty... New pile is: " + compPile);
continue;
else if (compRemove > b)
compRemove = num.nextInt(b) + 1;
System.err.println("The amount the computer wanted to remove is to hight... New number to be removed is: " + compRemove);
continue;
else
b = b - compRemove;
if (compPile == 2)
if (c == 0)
compPile = num.nextInt(2);
System.err.println("Selected pile is empty... New pile is: " + compPile);
continue;
else if (compRemove > c)
compRemove = num.nextInt(c) + 1;
System.err.println("The amount the computer wanted to remove is to hight... New number to be removed is: " + compRemove);
continue;
else
c = c - compRemove;
break;
public void calculate()
if (pile.equals("A"))
a = a - remove;
if (pile.equals("B"))
b = b - remove;
if (pile.equals("C"))
c = c - remove;
//end of calculate method
public void display()
System.out.println("tAtBtC");
System.out.println("t" + a + "t" + b + "t" + c);
java nim-game
java nim-game
edited Nov 16 '18 at 8:08
halfer
14.3k758109
14.3k758109
asked Nov 13 '18 at 1:29
Marcus
6
6
What denotes when the game is over?
– GBlodgett
Nov 13 '18 at 1:31
1
witch plays - too scary for me
– Scary Wombat
Nov 13 '18 at 1:37
1
Well, if you want a loop, you need a loop statement. It looks like you are familiar withwhile
loops, so that would be a good place to start...
– Kevin Anderson
Nov 13 '18 at 2:01
Once the players turn and the computers first turn are done the program simply terminates since there is no loop... and i don't think its as simple as adding another loop because the program separated in method and called to the main method on a different class (the "public class Assing3")
– Marcus
Nov 13 '18 at 3:58
add a comment |
What denotes when the game is over?
– GBlodgett
Nov 13 '18 at 1:31
1
witch plays - too scary for me
– Scary Wombat
Nov 13 '18 at 1:37
1
Well, if you want a loop, you need a loop statement. It looks like you are familiar withwhile
loops, so that would be a good place to start...
– Kevin Anderson
Nov 13 '18 at 2:01
Once the players turn and the computers first turn are done the program simply terminates since there is no loop... and i don't think its as simple as adding another loop because the program separated in method and called to the main method on a different class (the "public class Assing3")
– Marcus
Nov 13 '18 at 3:58
What denotes when the game is over?
– GBlodgett
Nov 13 '18 at 1:31
What denotes when the game is over?
– GBlodgett
Nov 13 '18 at 1:31
1
1
witch plays - too scary for me
– Scary Wombat
Nov 13 '18 at 1:37
witch plays - too scary for me
– Scary Wombat
Nov 13 '18 at 1:37
1
1
Well, if you want a loop, you need a loop statement. It looks like you are familiar with
while
loops, so that would be a good place to start...– Kevin Anderson
Nov 13 '18 at 2:01
Well, if you want a loop, you need a loop statement. It looks like you are familiar with
while
loops, so that would be a good place to start...– Kevin Anderson
Nov 13 '18 at 2:01
Once the players turn and the computers first turn are done the program simply terminates since there is no loop... and i don't think its as simple as adding another loop because the program separated in method and called to the main method on a different class (the "public class Assing3")
– Marcus
Nov 13 '18 at 3:58
Once the players turn and the computers first turn are done the program simply terminates since there is no loop... and i don't think its as simple as adding another loop because the program separated in method and called to the main method on a different class (the "public class Assing3")
– Marcus
Nov 13 '18 at 3:58
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272472%2fhow-to-do-looping-in-this-java-game%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272472%2fhow-to-do-looping-in-this-java-game%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What denotes when the game is over?
– GBlodgett
Nov 13 '18 at 1:31
1
witch plays - too scary for me
– Scary Wombat
Nov 13 '18 at 1:37
1
Well, if you want a loop, you need a loop statement. It looks like you are familiar with
while
loops, so that would be a good place to start...– Kevin Anderson
Nov 13 '18 at 2:01
Once the players turn and the computers first turn are done the program simply terminates since there is no loop... and i don't think its as simple as adding another loop because the program separated in method and called to the main method on a different class (the "public class Assing3")
– Marcus
Nov 13 '18 at 3:58