Re: tictactoe
- From: "Rhino" <no.offline.contact.please@xxxxxxxxxx>
- Date: Sun, 30 Oct 2005 00:59:52 -0400
"CPSCmajor" <bmerritt1987@xxxxxxxxx> wrote in message
news:1130642933.974303.182720@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> Rhino wrote:
> > "CPSCmajor" <bmerritt1987@xxxxxxxxx> wrote in message
> > news:1130615466.252620.216950@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> > > I have no idea where to even start on this...
> > > Any suggestions?
> > >
> > > In this problem, you will modify the TicTacToe class from the
textbook.
> > > Add a method flipVertical that flips the board position along the
> > > vertical axis. For example, the position
> > > x x o
> > > o
> > > x
> > > is flipped to
> > > o x x
> > > o
> > > x
> > > This is not useful for playing the game, but it can be useful for
> > > recognizing a winning strategy in a database of strategies.
> > >
> > > Also supply a flipHorizontal method that would flip the original
> > > position to
> > > x
> > > o
> > > x x o
> > > (Hint: If you are clever and understand how two-dimensional arrays are
> > > implemented as "arrays of arrays", this method can be much simpler
than
> > > the vertical flip.)
> > >
> > > What is your implementation of the flipVertical and flipHorizontal
> > > methods?
> > >
> > I'm sorry but that is just so simple that I can't believe you don't know
how
> > to do that. It seems a lot more likely that you are simply asking us to
> > write your homework for you. Surely even a novice Java programmer can
create
> > a new array based on an existing array where the new one has x's where
the
> > old one had o's (and vice versa) and where the spaces are still spaces.
If
> > not, you need some serious one-on-one tutoring with someone who can
review
> > the basics with you.
> >
> > Rhino
>
> Ok if I thought it was that easy then I would do it, obviously! I'm not
> trying to get people to do my homework, I just asked for any
> suggestions, not to be insulted.
>
I'd be more inclined to believe you if you'd shown even a single line of
code representing an attempt to solve the problem. But maybe you're just
really discouraged by hard Java seems to be right now. I'll give you the
benefit of the doubt - for now.
Anyway, I wasn't trying to insult you; I was trying to give you a vote of
confidence: you can do this! It's simply not that difficult.
Consider a simpler problem. Start with one dimensional array that has a mix
of "x", "o", and blank elements. Can you make another array whose elements
are exactly the same?
Here's your original array:
String[] myArray = {"x", "o", " "}; //original array
You want the second array to have the same number of elements as the
original array. How do you do it? Let's assume that we want code that will
work for any number of elements in the original array so that I can 'flip'
ANY size of array. The code to create a new empty array of the same size as
the original one is this:
String[] secondArray = new String[myArray.length];
[Every array has a variable called 'length' which contains the size of the
array; you use the expression 'myArray.length' to determine the number of
elements in the array called myArray.]
Now, to put the same elements in the second array as the first, you use a
loop, usually a 'for' loop, as opposed to a 'do' loop or 'while' loop:
for (int ix=0; ix < myArray.length; ix++) {
if (myArray[ix].equals("x")) flippedArray[ix] = "x";
else if (myArray[ix].equals("o")) flippedArray[ix] = "y";
else if (myArray[ix].equals(" ")) flippedArray[ix] = " ";
else {
System.err.println("Discovered element that is not x, o, or blank.");
}
}
The loop examines each element of the original array one at a time, from the
first to the last. Remember, array elements are numbered started at 0 in
Java.
For each element of the original array, the 'if' statements inspect the
value in the original array. Since the values in the arrays are Strings, we
have to use the equals() method, not an equal sign! The 'if/else' statements
look at a given element in the original array: if the value is an "x", we
write an "x" into the corresponding element of the second array; same thing
for "o" and " " (blank). If any OTHER value is found in the original array,
we display an error message. (In a professional program, we'd do something
snazzier but let's walk before we run.)
Now, to check our work, we need a loop to display the original array and
another loop to display the second array. This is the code for the first
array:
System.out.println("Original array:");
for (int ix=0; ix < myArray.length; ix++) {
System.out.println("[" + ix + "]: " + myArray[ix]);
}
I'll let you write the corresponding code for the second array; it's
virtually identical except that you change the text that you print before
the loop and the name of the array used throughout the loop.
Now, EXECUTE the code that we have developed. What happens? It works, right?
If not, try to figure out what went wrong. If you can't figure it out on
your own, post again and describe the problem clearly, making sure that you
post the code that isn't working so other people can see the problem and
help you.
Okay, let's assume you finally have this simpler code working. You're almost
done with your 'flip' logic!
In order to accomplish the original objective of the problem, all you have
to do is add a second dimension to the original array and modify your for
loops to handle two dimensions instead of one. The other thing you have to
do is make sure that the second array has an "o" written to it wherever the
original array had an "x" and an "x" where the original array had an "o".
That's it! You've solved the whole problem!
Okay, now give this all a try and post again if you're having problems.
Somebody will probably help IF you show some effort. Don't expect much help
though if you just throw up your hands in despair and say you don't know
what to do. Ask a concrete question that will help you solve the problem and
convince us that you are trying by showing us the code that doesn't work. If
you don't do that, no one will believe that you are actually trying.
You can do this stuff; we all learned it at some point and it was a struggle
for many of us. Those who succeeded were those who made a steady, rational
effort to figure out what we were doing wrong. If you don't have it in you
to work at learning Java, then drop the course and take something else.
Rhino
.
- References:
- tictactoe
- From: CPSCmajor
- Re: tictactoe
- From: CPSCmajor
- tictactoe
- Prev by Date: Re: help please
- Next by Date: Re: Converting int to string
- Previous by thread: Re: tictactoe
- Next by thread: Re: tictactoe
- Index(es):
Relevant Pages
|
|