Placement of code within an application
I have an assignment due for my Introduction to Java for which I have written the majority of the foundational code. The assignment is to write the code for text version of a popular logic game. I have written the code for the playing board, populated it with numbers and so on. My question is regarding a reset method which would reset the playing board returning all of values populating the board to their default values. I have written the code for this reset method and it works as a time procedure but I can't figure out where it should be placed within my application and how it redirect the application to start a new game. This reset method is a simple switch statement prompting the user to enter the letter "R" to prompt the resetting of the game. This works fine, displaying a "new" board but ends there not allowing another game to begin. I hope I have provided sufficient information to get a response... Thanks in advance.
Re: Placement of code within an application
I'm not entirely sure how to answer. I sort of understand, but without code or a picture or something to give us an idea of exactly what you have and what you are wanting to have, there is only so much help that can be provided.
Re: Placement of code within an application
Quote:
Originally Posted by
Deprogrammer
I have an assignment due for my Introduction to Java for which I have written the majority of the foundational code. The assignment is to write the code for text version of a popular logic game. I have written the code for the playing board, populated it with numbers and so on. My question is regarding a reset method which would reset the playing board returning all of values populating the board to their default values. I have written the code for this reset method and it works as a time procedure but I can't figure out where it should be placed within my application and how it redirect the application to start a new game. This reset method is a simple switch statement prompting the user to enter the letter "R" to prompt the resetting of the game. This works fine, displaying a "new" board but ends there not allowing another game to begin. I hope I have provided sufficient information to get a response... Thanks in advance.
Assuming you have all the values in an array or something, just use a for loop
for (int x =0 ; x < array.length; x++)
{
array[x] = " ";
}
As for the not ending, try using a boolean and a while loop together.
char c = 'c';
boolean isDone = false;
while (isDone == false)
{
code for game goes here
c = console.next().charAt(0);
switch (c)
{
case ('R')
isDone = false;
reset();
break;
default
isDone = true;
break;
}
}
Personally, you could have it much better without the switch structure by having
if (c =='R' || c == 'r')
{
isDone = false;
reset();
}
else
isDone = true;
Re: Placement of code within an application
Also, if this happens to be due soon, it may or may not be fully answered in time.
Also, I don't know what data types you are storing. int, float, double, long, short.
If the board size never changes, try storing the default values in an array of your data type.
If you don't know what an array is:
it basically is
dataType[] arrayName = new dataType[arraySize];
arraySize must be >= 1.
Array indexes start at 0 and go to index arraySize - 1.
the statement that declares the array above only makes a new array, it does not set any of the values at the indexes in the array. You have to do that yourself.
Its size cannot be decreased or increased once set. You have to copy all the data into another array of a greater size.
output and input processes when you actually run it go here.
Re: Placement of code within an application
Thank you for your replies, they were very prompt and helpful. I figured out a very simple solution that had been right under my nose. Thanks!