473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Nim program.

hello there, im trying to create some java code that spits out a board of 5 rows of sticks, each one with it's corresponding number. (Row 1 = 1 Stick)
Like so:
|
| |
| | |
| | | |
| | | | |

Then it needs to ask Player A to choose a row, and how many sticks to remove from that row ONLY. Then Player B takes a turn.

You can only remove sticks from one row at a time, unlimited amount of sticks.
The object of the game is to be the one to remove the LAST stick.
Then spit out. "You win!"

My code:

import java.util.Scanner;
public class StickGame
{
public static void main (String[]args)
{
Scanner scan = new Scanner(System.in);
{
String a = " | ";
String Row1 = (a);
String Row2 = (a+ " "+a+ " ");
String Row3 = (a+ " "+a+ " "+a+ " ");
String Row4 = (a+ " "+a+ " "+a+ " "+a+ " ");
String Row5 = (a+ " "+a+ " "+a+ " "+a+ " "+a+ " ");

//Initial Board
System.out.println(" 1: " +a+ " ");
System.out.println(" 2: " +a+ " "+a+ " ");
System.out.println(" 3: " +a+ " "+a+ " "+a+ " ");
System.out.println(" 4: " +a+ " "+a+ " "+a+ " "+a+ " ");
System.out.println(" 5: " +a+ " "+a+ " "+a+ " "+a+ " "+a+ " ");

System.out.println();
System.out.println("Player A, Please choose a row.");
int x = scan.nextInt();
System.out.println("How many sticks would you like to remove?");
int y = scan.nextInt();

}
}
}


I don't know how to remove "n" number of sticks from Row "n", and then continue back and forth between players until its finished.

thankz alot for any responses... im getting a headache just thinking about it

PS -- urggh, my uncle just told me I cant use array?? I dont know what that means.. but I have to find a different method..
(Hes my teacher.. im homeschooled) so he wont help me :[
Oct 3 '07 #1
45 5313
r035198x
13,262 8TB
hello there, im trying to create some java code that spits out a board of 5 rows of sticks, each one with it's corresponding number. (Row 1 = 1 Stick)
Like so:
|
| |
| | |
| | | |
| | | | |

Then it needs to ask Player A to choose a row, and how many sticks to remove from that row ONLY. Then Player B takes a turn.

You can only remove sticks from one row at a time, unlimited amount of sticks.
The object of the game is to be the one to remove the LAST stick.
Then spit out. "You win!"

My code:

import java.util.Scanner;
public class StickGame
{
public static void main (String[]args)
{
Scanner scan = new Scanner(System.in);
{
String a = " | ";
String Row1 = (a);
String Row2 = (a+ " "+a+ " ");
String Row3 = (a+ " "+a+ " "+a+ " ");
String Row4 = (a+ " "+a+ " "+a+ " "+a+ " ");
String Row5 = (a+ " "+a+ " "+a+ " "+a+ " "+a+ " ");

//Initial Board
System.out.println(" 1: " +a+ " ");
System.out.println(" 2: " +a+ " "+a+ " ");
System.out.println(" 3: " +a+ " "+a+ " "+a+ " ");
System.out.println(" 4: " +a+ " "+a+ " "+a+ " "+a+ " ");
System.out.println(" 5: " +a+ " "+a+ " "+a+ " "+a+ " "+a+ " ");

System.out.println();
System.out.println("Player A, Please choose a row.");
int x = scan.nextInt();
System.out.println("How many sticks would you like to remove?");
int y = scan.nextInt();

}
}
}


I don't know how to remove "n" number of sticks from Row "n", and then continue back and forth between players until its finished.

thankz alot for any responses... im getting a headache just thinking about it

PS -- urggh, my uncle just told me I cant use array?? I dont know what that means.. but I have to find a different method..
(Hes my teacher.. im homeschooled) so he wont help me :[
1.) Please use code tags (not bold tags) for posting code.
2.) Where is your loop? You need some actions to be repeated.
Oct 3 '07 #2
I don't know what to do next..
that's why I posted here.

I was hoping somebody could either post some code, or describe in somewhat detail of the next steps.

Thank you.
Oct 3 '07 #3
Ganon11
3,652 Expert 2GB
Instead of using strings to represent the rows, I'd use 5 int variables. row1 starts at 1, row2 starts at 2, etc. etc. When a player wishes to remove n sticks from one row, subtract n from the specific row (Make sure the player doesn't try to remove more sticks than are available in that row!)

As for players, hmm...There are only two possibilities: either it's player 1's turn, or it's player 2's turn. Or, in other words, it's either Player 1;s turn, or it's NOT player 1's turn...Sound familiar?
Oct 3 '07 #4
r035198x
13,262 8TB
I don't know what to do next..
that's why I posted here.

I was hoping somebody could either post some code, or describe in somewhat detail of the next steps.

Thank you.
It's unlikely that someone will post some code.
I think you should first decide on how you are going to represent your board.
Oct 3 '07 #5
JosAH
11,448 Expert 8TB
@OP: forget about that 'Nim' (that's its name) game for a moment; do you know
how to print 'n' equal characters in a row? Hin: a for loop can do the job. You
already discovered about System.out.println; there's also a System.out.print
method; go study.

In other words: I give you 'n', a number and 'c', a character; you have to print 'n'
of those 'c's in a row; use that loop and please use a separate little method to
accomplish that little task.

kind regards,

Jos
Oct 3 '07 #6
Wow, these are some good ideas, haha

Okay I'm setting int Row1=1, Row2=2, etc...
and if for example they choose row 5.. and choose to remove 3 sticks.

I don't see how I'm going to print out the actual two sticks " | | "
that will remain in that fifth row.

I know I'm a beginner, so please bare with me.
Oct 3 '07 #7
PS - I just verified I'm not able to use another method.
Only the main method.

As you can see it's quite basic, and yet I'm still having trouble.
Any help at all is greatly appreciated, every bit counts.
Oct 3 '07 #8
I've taken your advice and incorporated a println.. etc
though I can't figure out how to stop my while loop once it's printed out the required sticks.. and how to print out the board after EACH turn, incorporating all
past moves.


[PHP]import java.util.Scanner;
public class StickGameTest
{
public static void main (String[]args)
{
Scanner scan = new Scanner(System.in);
{
int Row1 = 1;
int Row2 = 2;
int Row3 = 3;
int Row4 = 4;
int Row5 = 5;

//Initial Board
System.out.println(" 1: | ");
System.out.println(" 2: | |");
System.out.println(" 3: | | |");
System.out.println(" 4: | | | | ");
System.out.println(" 5: | | | | |");




//Players choose their move.
System.out.println();
System.out.println("Player A, Please choose a row.");
int x = scan.nextInt();
System.out.println("How many sticks would you like to remove?");
int y = scan.nextInt();

int difference = x-y;
int R = difference;
while(difference==R)
{
for (int P =0; P<=R; P++)
{System.out.print("|" + " ");



}
}

}
}
}

[/PHP]
Oct 4 '07 #9
kreagan
153 100+
I've taken your advice and incorporated a println.. etc
though I can't figure out how to stop my while loop once it's printed out the required sticks.. and how to print out the board after EACH turn, incorporating all
past moves.


[PHP]import java.util.Scanner;
public class StickGameTest
{
public static void main (String[]args)
{
int difference = x-y;
int R = difference;
while(difference==R)
{
for (int P =0; P<=R; P++)
{System.out.print("|" + " ");
[/PHP]
Difference is equal to R. R never changes nor does Difference. Therefore, the while loop will never break.

I would suggest using 2 nested for loops. 1 loop to iterate down and the other to iterate across.

To print out the moves each time, just put a loop that terminates when someone wins (there are no sticks).

And what do you mean incorporate all past moves? Are you saying print out all moves taken each time? Store all moves and print them out the same way you did with each single move. I would suggest just printing a statement instead of the sticks for previous moves because the images might get overwhelming.
Oct 4 '07 #10
JosAH
11,448 Expert 8TB
PS -- urggh, my uncle just told me I cant use array?? I dont know what that means.. but I have to find a different method..
(Hes my teacher.. im homeschooled) so he wont help me :[
Would you be so kind to ask him *why* you're not allowed to use arrays? Maybe
he can give you a short list of *what* you are allowed to use?

kind regards,

Jos
Oct 4 '07 #11
He said I can't use arrays since we haven't covered them yet..

basically I can use, for, while, if/else..
println, print... Strings, ints, doubles, etc.. the basics.

Kreagen, you said "I would suggest using 2 nested for loops. 1 loop to iterate down and the other to iterate across. "

What would I put inside the for loops.. because x = the row they want.
y = the # of sticks to remove..

would it be something like:
[PHP]for (x=1; x<=5, x++)
{ }[/PHP]

and then same for y? I'm so confused...
Basically I want it to print the CURRENT board before each player takes a turn.
So if I remove 3 sticks from row 5.... row 5 should now have 2 sticks.
But I don't know how I'd print Row 1, 2,3, 4 unchanged.. and then 5 being changed.. and so on until somebody wins.

Are you not allowed to post the actual code to help me?
it's only my uncle and I only see him on Mondays, so he won't help.. nor care how I finish it. I do know that once I see the correct changes, I'll understand what I should have done.

Thanks everybody.. I appreciate you sharing your knowledge to this point,
if you're able.. please continue to help.
Oct 4 '07 #12
JosAH
11,448 Expert 8TB
He said I can't use arrays since we haven't covered them yet..
Ask your uncle to come up with another assignment; he doesn't want you to talk
crippled and a much obfuscated subset of Java does he?

kimd regards,

Jos
Oct 4 '07 #13
He said it's completely possible to do without using an array.

And it seems possible as you all have continued to support me this far..
I can't get a new assignment, especially seeing how I am almost finished this one.

I need to finish this one, if you don't want to help me, you don't have to.
Oct 4 '07 #14
JosAH
11,448 Expert 8TB
He said it's completely possible to do without using an array.

And it seems possible as you all have continued to support me this far..
I can't get a new assignment, especially seeing how I am almost finished this one.

I need to finish this one, if you don't want to help me, you don't have to.
Wanting to help you or not is not the issue. Of course the entire program can
be implemented without arrays because arrays are just a figment of our own
imagination. If you want you can implemented the entire thing using just String
fiddling on one single String but that's not the issue either.

Your example problem is an ideal example of indexing values and then some.
Forbidding to use that indexing technique (read: arrays or Lists) forbids elegant
implementations for this exercise and cripples your programming style, the way
of thinking about problems; a programming style which you haven't developed
yet, so don't let it be crippled in the near future.

kind regards,

Jos
Oct 5 '07 #15
Alright, I appreciate your clarification,

basically I don't expect to take any other courses in Programming, so I'm just wanting to finish this up.. It doesn't matter if it's the most efficient method at all, just needs to work.

Here's my current code, can somebody post the remaining pieces that I need, either the code.. or a description of what I need to do.. I'm getting quite desperate, i'll admit.. so I really appreciate it.

[PHP]import java.util.Scanner;
public class StickGame
{
public static void main (String[]args)
{
Scanner scan = new Scanner(System.in);
{
int Row1 = 1;
int Row2 = 2;
int Row3 = 3;
int Row4 = 4;
int Row5 = 5;

//Initial Board
System.out.println(" 1: | ");
System.out.println(" 2: | |");
System.out.println(" 3: | | |");
System.out.println(" 4: | | | | ");
System.out.println(" 5: | | | | |");

int count = 0;
String Player = ("Player A");
String PlayerA = ("Player A");
String PlayerB = ("Player B");

while (Row1 != 0 && Row2 !=0 && Row3 !=0 && Row4 != 0 && Row5 != 0)
{

//Players choose their move.
System.out.println();
System.out.println(Player+ " please choose a row.");
int x = scan.nextInt();
System.out.println("How many sticks would you like to remove?");
int y = scan.nextInt();

Switch(x);
{
case 1:
Row1 -= y;
break;
case 2:
Row2 -= y;
break;
case 3:
Row3 -= y;
break;
case 4:
Row4 -= y;
break;
case 5:
Row5 -= y;
break;
}

if (count % 2 == 0)
{Player = PlayerB;
}
else
{Player = PlayerA;
}
count++;
}
}
}
}[/PHP]
Oct 5 '07 #16
jkmyoung
2,057 Expert 2GB
I thought nim ended when all of the sticks were taken?
while (Row1 != 0 && Row2 !=0 && Row3 !=0 && Row4 != 0 && Row5 != 0)

This should probably be an OR.
while (Row1 != 0 || Row2 !=0 || Row3 !=0 || Row4 != 0 || Row5 != 0)

Are you allowed other functions?
I would probably create a display row function: eg

Expand|Select|Wrap|Line Numbers
  1. private static void printRow(int row, int matches){
  2.   System.out.print(row+":");
  3.   for (int i = 0; i < matches; i++){
  4.     System.out.print(" |");
  5.   }    
  6.  System.out.println("");
  7. }
Oct 5 '07 #17
Thanks a lot for your suggestion,
would that be considered a second method though?

The requirements state I can only use a single method (main).. no arrays..

no only the basics.. int, double, String, for, while, if/else, println, print, etc.
Oct 5 '07 #18
Ganon11
3,652 Expert 2GB
The OP has already stated no other functions are allowed.

In order to print out the rows, you will need 1 for loop for each row variable. For example, print 1 "|" for every time the loop executes in Row1, one "|" for every time the loop executes in Row2, etc. This is going to get messy because you will have 5 for...loops at the beginning of your while...loop.
Oct 5 '07 #19
JosAH
11,448 Expert 8TB
The OP has already stated no other functions are allowed.

In order to print out the rows, you will need 1 for loop for each row variable. For example, print 1 "|" for every time the loop executes in Row1, one "|" for every time the loop executes in Row2, etc. This is going to get messy because you will have 5 for...loops at the beginning of your while...loop.
I'd go for a single initial String "|@||@|||@||||@||||@", forget about those five single
int variables and fiddle diddle with the String operators and regexps quite a bit.

kind regards,

Jos

ps, the '@' can be any non-'|' character.
Oct 5 '07 #20
what do you mean by "a single initial String "|@||@|||@||||@||||@"

I'm pretty much a novice in the field of programming..
I felt as if I was nearing completion of this program, but I don't know which suggestion to follow through with.

Right now I have my Rows as follows:
[PHP] int Row1 = 1;
int Row2 = 2;
int Row3 = 3;
int Row4 = 4;
int Row5 = 5;[/PHP]

and I thought I could simply subtract the number of sticks from the associated row.. and then print out the result.

The result has to be printed after EACH turn.. and by result I mean the complete board.
|
||
|||
||||
||||| except with the chosen sticks missing each time this is printed after each
turn. Am I simply overlooking an easy approach?
Is anybody willing to finish my code.. I think I'm pretty close to completion, so you'd only be applying your suggestions in the form of code, as opposed to words.

If nobody is willing, then I'll have to keep trying each suggestion 1 by 1. It just seems everyone has a different view on this.. and I don't know what to do.
Thanks to everyone who has stuck around this far, it means a lot to me and has relieved some much unneeded stress..
Oct 5 '07 #21
PS - Why would I want
while (Row1 != 0 && Row2 !=0 && Row3 !=0 && Row4 != 0 && Row5 != 0)

to be "|| " (Or) instead?

wouldn't that only run the loop until ONE row equals zero?
Even though I want ALL rows to equal zero before the loop stops, and then print "You Win!"
Oct 5 '07 #22
Okay I've made progress..

I'm now able for PlayerA and PlayerB to choose any row, and any amount of sticks to remove.

It prints out the board after each turn, except in the form of numbers..
how do I make it so it prints out the sticks?

That may be the end of it, if I can finish that. :)

[PHP]import java.util.Scanner;
public class StickGame
{
public static void main (String[]args)
{
Scanner scan = new Scanner(System.in);
{
int Row1 = 1;
int Row2 = 2;
int Row3 = 3;
int Row4 = 4;
int Row5 = 5;

//Initial Board
System.out.println(" 1: | ");
System.out.println(" 2: | |");
System.out.println(" 3: | | |");
System.out.println(" 4: | | | | ");
System.out.println(" 5: | | | | |");

int count = 0;
String Player = ("Player A");
String PlayerA = ("Player A");
String PlayerB = ("Player B");

while (Row1 != 0 || Row2 !=0 || Row3 !=0 || Row4 != 0 || Row5 != 0)
{

//Players choose their move.
System.out.println();
System.out.println(Player+ " please choose a row.");
int x = scan.nextInt();
System.out.println("How many sticks would you like to remove?");
int y = scan.nextInt();

switch(x)
{
case 1:
Row1 -= y;
break;
case 2:
Row2 -= y;
break;
case 3:
Row3 -= y;
break;
case 4:
Row4 -= y;
break;
case 5:
Row5 -= y;
}

System.out.println(Row1);
System.out.println(Row2);
System.out.println(Row3);
System.out.println(Row4);
System.out.println(Row5);

if (count % 2 == 0)
{Player = PlayerB;
}
else
{Player = PlayerA;
}
count++;
}
}
}
}

[/PHP]
Oct 5 '07 #23
Ganon11
3,652 Expert 2GB
In order to print out the rows, you will need 1 for loop for each row variable. For example, print 1 "|" for every time the loop executes in Row1, one "|" for every time the loop executes in Row2, etc. This is going to get messy because you will have 5 for...loops at the beginning of your while...loop.
Like I suggested here :D
Oct 5 '07 #24
Like I suggested here :D
I changed it to be

.....
[PHP]int difference = x-y;
switch (x)
{
case 5:
Row5 -= y;
for (Row5=1; Row5 <= difference; Row5++)
{
System.out.print(" | " + " ");

}
[/PHP]
This prints out the right amount of sticks the first time.. for example.
x=5 (Row5) and y=3 (remove 3 sticks)

so it prints out " | | "

but then for Player B's turn.. if they choose Row5.. and to remove only one(1) stick.. it will print out "| | | |" four (4) sticks... not the one stick which I want it to.

I've only tried this for loop on one variable so far (Row5) just to see if it works, then i'll implement it for the other 4 rows.. so what am I doing wrong?
how can I make it "save" the amount of sticks currently in a row, as opposed to resetting it.

Thanks a lot.

My entire code is:

[PHP]import java.util.Scanner;
public class StickGame
{
public static void main (String[]args)
{
Scanner scan = new Scanner(System.in);
{
int Row1 = 1;
int Row2 = 2;
int Row3 = 3;
int Row4 = 4;
int Row5 = 5;

//Initial Board
System.out.println(" 1: | ");
System.out.println(" 2: | |");
System.out.println(" 3: | | |");
System.out.println(" 4: | | | | ");
System.out.println(" 5: | | | | |");

int count = 0;
String Player = ("Player A");
String PlayerA = ("Player A");
String PlayerB = ("Player B");

while (Row1 != 0 || Row2 !=0 || Row3 !=0 || Row4 != 0 || Row5 != 0)
{

//Players choose their move.
System.out.println();
System.out.println(Player+ " please choose a row.");
int x = scan.nextInt();
System.out.println("How many sticks would you like to remove?");
int y = scan.nextInt();

int difference = x-y;
switch(x)
{
case 1:
Row1 -= y;
break;
case 2:
Row2 -= y;
break;
case 3:
Row3 -= y;
break;
case 4:
Row4 -= y;
break;
case 5:
Row5 -= y;
for (Row5=1; Row5 <= difference; Row5++)
{
System.out.print(" | " + " ");

}
}

//System.out.println(Row1);
//System.out.println(Row2);
//System.out.println(Row3);
//System.out.println(Row4);
// System.out.println(Row5);

if (count % 2 == 0)
{Player = PlayerB;
}
else
{Player = PlayerA;
}
count++;
}
}
}
}[/PHP]
Oct 6 '07 #25
Ganon11
3,652 Expert 2GB
No, no, no, your for...loop should not change the Row5 variable! Use a temporary variable called i only inside the for loop, and go from 1 to <= Row5.
Oct 6 '07 #26
JosAH
11,448 Expert 8TB
This is all so bad; even old Fortran/4 or 77 knows about subroutines and arrays.
It shouldn't be implemented this way ... and I'm not even talking fancy OO things here ...

kind regards,

Jos
Oct 6 '07 #27
This is all so bad; even old Fortran/4 or 77 knows about subroutines and arrays.
It shouldn't be implemented this way ... and I'm not even talking fancy OO things here ...

kind regards,

Jos
My main objective is to solve the program, I do not care which way it is implemented as long as it completes the required task.

Efficiency is not an issue, so more beneficial methods are irrelevant. Reading the previous posts would support this.

Thank you to everyone who posted solutions, i'll try them out and report back.
Oct 7 '07 #28
No, no, no, your for...loop should not change the Row5 variable! Use a temporary variable called i only inside the for loop, and go from 1 to <= Row5.

Okay, that makes sense.. I've applied the necessary changes and it seems to do what I want.. although when it prints out the line.. since each one is a print(blah blah)

as opposed to a println( blah blah) .. then the WHOLE board (all rows) are printed onto the same row... but if I switch it to be println(blah blah) then each time it runs through each individual for loop it puts a new stick on a new line..

how do I solve this??
Thanks a lot.
Oct 7 '07 #29
Ganon11
3,652 Expert 2GB
After the row ends, use another output statement to either output a newline character '\n' (if you use print()), or simply put a blank println() statement.
Oct 7 '07 #30
JosAH
11,448 Expert 8TB
Nah, this program (if it ever completes) is as ugly as it is, so better print all rows like this:

Expand|Select|Wrap|Line Numbers
  1. System.out.println("|||||".substring(0, row1));
  2. System.out.println("|||||".substring(0, row2));
  3. System.out.println("|||||".substring(0, row3));
  4. System.out.println("|||||".substring(0, row4));
  5. System.out.println("|||||".substring(0, row5));
  6.  
It would save you all those identical loops.

kind regards,

Jos
Oct 7 '07 #31
Nah, this program (if it ever completes) is as ugly as it is, so better print all rows like this:

Expand|Select|Wrap|Line Numbers
  1. System.out.println("|||||".substring(0, row1));
  2. System.out.println("|||||".substring(0, row2));
  3. System.out.println("|||||".substring(0, row3));
  4. System.out.println("|||||".substring(0, row4));
  5. System.out.println("|||||".substring(0, row5));
  6.  
It would save you all those identical loops.

kind regards,

Jos
Wow, that basically completed the program right there.
The only thing is if I try to remove three(3) sticks from the second row, then it prints out an ugly section of errors.

I'd rather it just simply ignore the choice, and remove the maximum from that row.. Ex: Removing five(5) sticks from the third row would print: " " (nothing).

I also have to see if I'm able to use Substrings, but I should be able to.
Oct 7 '07 #32
JosAH
11,448 Expert 8TB
I also have to see if I'm able to use Substrings, but I should be able to.
Yes you should; you're able to use System.out.println() aren't you? Then you should
be able/allowed to use String.substring() for the same reason.

kind regards,

Jos
Oct 7 '07 #33
JosAH
11,448 Expert 8TB
I'd rather it just simply ignore the choice, and remove the maximum from that row.. Ex: Removing five(5) sticks from the third row would print: " " (nothing).
You're nowhere near are you; when/if a user types something be prepared to
anticipate to all of the following:

1) no input at all
2) nonsense input (like 'fronobulax' when asked for a pile number)
3) incorrect pile number (like -42)
4) incorrect input for the number of sticks to remove (see 1, 2 and 3 again)

and just because you're not allowed to implement your own little methods, nor
are you allowed to use a simple array you have to repeat your solution over and
over again for every single number; have fun.

kind regards,

Jos
Oct 7 '07 #34
You're nowhere near are you; when/if a user types something be prepared to
anticipate to all of the following:

1) no input at all
2) nonsense input (like 'fronobulax' when asked for a pile number)
3) incorrect pile number (like -42)
4) incorrect input for the number of sticks to remove (see 1, 2 and 3 again)

and just because you're not allowed to implement your own little methods, nor
are you allowed to use a simple array you have to repeat your solution over and
over again for every single number; have fun.

kind regards,

Jos

It doesn't matter what they type.. if it is irrelevant to the actual purpose of the program.. it can simply be ignored. There does *not* need to be any code written for such as the 4 exceptions you've posted.

It just simply needs to ignore it..and continue on.

The program would ask: Player A Which row would you like to use? Answer: 7
How many sticks would you like to remove? Answer: 62

Then it should simply ignore this.. and ask again
Player A Which row would you like to use?

until they choose a number that *is* relevant.
So I am pretty much finished the program..aren't I?

How do I simply make it ignore this.. something like this maybe?

if (x > 5 || y > 5 || y>x)
{
ignore/continue; <--- that's not right.. so what could I do to have this effect.
}
Thank you so much.
My code is :

[PHP]import java.util.Scanner;
public class StickGame
{
public static void main (String[]args)
{
Scanner scan = new Scanner(System.in);
{
int Row1 = 1;
int Row2 = 2;
int Row3 = 3;
int Row4 = 4;
int Row5 = 5;

//Initial Board
System.out.println(" 1: | ");
System.out.println(" 2: | |");
System.out.println(" 3: | | |");
System.out.println(" 4: | | | | ");
System.out.println(" 5: | | | | |");

int count = 0;
String Player = ("Player A");
String PlayerA = ("Player A");
String PlayerB = ("Player B");

while (Row1 != 0 || Row2 !=0 || Row3 !=0 || Row4 != 0 || Row5 != 0)
{

//Players choose their move.
System.out.println();
System.out.println(Player+ " please choose a row.");
int x = scan.nextInt();
System.out.println("How many sticks would you like to remove?");
int y = scan.nextInt();

int a= Row1;
int b= Row2;
int c= Row3;
int d= Row4;
int e= Row5;
switch(x)
{
case 1:
Row1 -= y;
break;
case 2:
Row2 -= y;
break;
case 3:
Row3 -= y;
break;
case 4:
Row4 -= y;
break;
case 5:
Row5 -= y;
}

if(y <= Row1 || y <= Row2 || y <= Row3 || y <= Row4 || y <= Row5)
{
System.out.println("|||||".substring(0, Row1));

System.out.println("|||||".substring(0, Row2));

System.out.println("|||||".substring(0, Row3));

System.out.println("|||||".substring(0, Row4));

System.out.println("|||||".substring(0, Row5));
}
else
{
return;}

if(Row1 == 0 && Row2 ==0 && Row3 ==0 && Row4 == 0 && Row5 == 0)
{
System.out.print(Player+ " wins!");
}

if (count % 2 == 0)
{Player = PlayerB;
}
else
{Player = PlayerA;
}
count++;

}

}
}
}
[/PHP]
Oct 7 '07 #35
Ganon11
3,652 Expert 2GB
The correct statement you want there is the "continue" statement.
Oct 7 '07 #36
The correct statement you want there is the "continue" statement.
Thank you so much, that basically did what I wanted it to..

For example I choose the fifth row, to remove 6 sticks.. and it prints out "please choose a correct number"..

although since my code is
[PHP] if (x > 5 || y > 5 || y > x)
{
System.out.println("Please enter a correct number");
continue;
}[/PHP]

once you remove say 3 sticks.. from the fifth, you're left with two remaining.. and then that if statement doesn't apply anymore.. since 2 !> (not greater) than 5.. so it prints out this:


[PHP]StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at StickGame.main(StickGame.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)[/PHP]

where the out of range: -1 is the difference from the remaining sticks and the amount you wanted to remove.. ( in this case removing 3 sticks from the remaining two).

Urgh, I truly appreciate all this help.. I would be NOWHERE near completion without all of you.. but I can't figure this out.

Please report any suggestions.
Oct 8 '07 #37
Ganon11
3,652 Expert 2GB
When you're checking if the amount to remove is greater than the amount of sticks in the pile, are you checking against a constant, hard-coded number, or against a variable?
Oct 8 '07 #38
I'm using "5" .. so a hardcoded number.

I tested out using something like this.

[PHP]
int a = Row1;
int b = Row2;
int c = Row3;
int d = Row4;
int e = Row5;


if (x > a,b,c,d,e || y > a,b,c,d,e || y>x)
{
System.out.println("Please enter a correct number");
continue;
}[/PHP]

I thought this was a good idea, but it gave me errors.
Is this the correct process, but I'm implementing it wrong?
If so, what's the correct way.
I'm close.. I can tell
Oct 8 '07 #39
r035198x
13,262 8TB
I'm using "5" .. so a hardcoded number.

I tested out using something like this.

[PHP]
int a = Row1;
int b = Row2;
int c = Row3;
int d = Row4;
int e = Row5;


if (x > a,b,c,d,e || y > a,b,c,d,e || y>x)
{
System.out.println("Please enter a correct number");
continue;
}[/PHP]

I thought this was a good idea, but it gave me errors.
Is this the correct process, but I'm implementing it wrong?
If so, what's the correct way.
I'm close.. I can tell
No that is not correct Java syntax. What are you trying to do with those statements.

P.S It helps to go through a Java tutorial.
Oct 8 '07 #40
No that is not correct Java syntax. What are you trying to do with those statements.

P.S It helps to go through a Java tutorial.

I'm trying to make it so.. IF somebody inputs an incorrect number (irrelevant to the program's main purpose)

Ex: chose the 7th row.. or chose to remove 50 sticks..

I just want it to ignore all these cases and continue.. that's why I set each Row equal to a different variable.. so I thought I had the right process.. just obviously in an incorrect syntax.

Can somebody please post the correct way to do this.. so that it will work for my program?

I know you guys know how to do it... please please help me.
I know you have all helped a lot to this point, but I'm a beginner (big time) and just would really like to get this over with.
Thank you so much.
Oct 8 '07 #41
Ganon11
3,652 Expert 2GB
I would check each thing one at a time. For instance, right after the user enters the row number, check if it's valid. If not, use the continue statement. If it is valid, move on to check how many sticks they want to remove. Then check if it's valid - depending on the row variable entered, your check will vary.
Oct 8 '07 #42
I would check each thing one at a time. For instance, right after the user enters the row number, check if it's valid. If not, use the continue statement. If it is valid, move on to check how many sticks they want to remove. Then check if it's valid - depending on the row variable entered, your check will vary.

Is it because I have

[PHP]System.out.println("|||||".substring(0, Row1));

System.out.println("|||||".substring(0, Row2));

System.out.println("|||||".substring(0, Row3));

System.out.println("|||||".substring(0, Row4));


that I get an error whenever I try to remove less than zero (0) sticks...
can simply placing a few if/else statements throughout the code bypass this?

I've been trying tediously since my program is suppose to be due tonight.. to get past this.. but I continuously get the same error:


[PHP]StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.substring(Unknown Source)
at StickGame.main(StickGame.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
System.out.println("|||||".substring(0, Row5));[/PHP][/PHP]

could you perhaps give me an example of what I'd place inside the if statement.. should I be comparing x and y to a,b,c,d,e? (where those are equal to their corresponding row)
Oct 8 '07 #43
r035198x
13,262 8TB
What you failed to realize from Jos' instructions above is that ignoring meaningless input and continuing actually is handling an exceptional condition. Now you are again faced with a problem of handling another "exceptional" condition.
Tip: Whenever you see the words "input from user", you'll always need to handle ridiculuos input because users input ridiculous things.
Oct 8 '07 #44
What you failed to realize from Jos' instructions above is that ignoring meaningless input and continuing actually is handling an exceptional condition. Now you are again faced with a problem of handling another "exceptional" condition.
Tip: Whenever you see the words "input from user", you'll always need to handle ridiculuos input because users input ridiculous things.

What do you mean by exceptional condition?

How do I solve this?
I keep being told what the problem is.. and then left to dwell over it..

should I just put in a for loop? if so.. what should be in the for loop?
I don't need to know why.. once I see the code.. or an example then I'll be able to figure it out.

anybody?
Oct 8 '07 #45
JosAH
11,448 Expert 8TB
Can somebody please post the correct way to do this.. so that it will work for my program?
No we can't; at least I can't because you or your uncle or whoever had forbidden
to use the most elementary features present in Java to solve such a little problem.
The result of the (if possible) solution to this little problem will be just an ugly
beast; accepting wrong things, overlooking incorrect input and what else besides
a lot of repeating code. At least all those loops where removed in showing the
current board; I won't show you other uglyness in order to get rid of all those
other silly loops because it ain't worth it. Give it a try yourself because you
haven't accomplished much yourself (yet).

kind regards,

Jos
Oct 8 '07 #46

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
22
by: edgrsprj | last post by:
PROPOSED EARTHQUAKE FORECASTING COMPUTER PROGRAM DEVELOPMENT EFFORT Posted July 11, 2005 My main earthquake forecasting Web page is: http://www.freewebz.com/eq-forecasting/Data.html ...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
11
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >>...
1
by: Eric Whittaker | last post by:
hi all, im trying to write my first c++ program. a success, but i can't get the window to stay open after user enters input. it just automatically closes. right now the end of my program looks...
9
by: Hemal | last post by:
Hi All, I need to know the memory required by a c program. Is there any tool/utility which can give me the memory usage in terms of DATA segment, TEXT segment, BSS segment etc. I am working...
7
by: ibtc209 | last post by:
I just started programming in C, and I need some help with this problem. Your program will read the information about one MiniPoker hand, namely the rank and suit of the hand’s first card, and...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
0
amitpatel66
by: amitpatel66 | last post by:
There is always a requirement that in Oracle Applications, the Concurrent Program need to be execute programatically based on certain conditions/validations: Concurrent programs can be executed...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.