wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Check winner diagonal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Check winner diagonal " Watch "Check winner diagonal " New topic
Author

Check winner diagonal

Andres John
Ranch Hand

Joined: Jun 07, 2012
Posts: 34
this is just part of the ttt program that i need to know how i could check the four main diagonals line for the winner. this what i got
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
one way might be to set up another array of 'lines'
e.g. say you have a button array button[9] so they are indexed 0 to 8
JButton[][] lines = {{button[0],button[1],button[2]},
then
3,4,5 (row)
6,7,8 (row)
0,3,6 (column)
1,4,7 (column)
2,5,8 (column)
0,4,8 (diagonal)
2,4,6 (diagonal)

and check each 'line' to see if they're all the same i.e. winner
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Andres John wrote:this is just part of the ttt program that i need to know how i could check the four main diagonals line for the winner.

Four? Must be a new version of TTT I haven't heard of. And if this is for a 3D TTT game, there will be a lot more than 4, because there will be 2 for each plane, plus 4 "3D" diagonals (and you should probably come up with a name for them to differentiate them from a "normal" diagonal).

My suggestion: Write methods that return you rows, columns, and diagonals for each plane and "apex to apex" lines as arrays of your TTT cells. That way, you can program the game for any size of board.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Andres John
Ranch Hand

Joined: Jun 07, 2012
Posts: 34
Winston Gutkowski wrote:
Andres John wrote:this is just part of the ttt program that i need to know how i could check the four main diagonals line for the winner.

Four? Must be a new version of TTT I haven't heard of. And if this is for a 3D TTT game, there will be a lot more than 4, because there will be 2 for each plane, plus 4 "3D" diagonals (and you should probably come up with a name for them to differentiate them from a "normal" diagonal).

My suggestion: Write methods that return you rows, columns, and diagonals for each plane and "apex to apex" lines as arrays of your TTT cells. That way, you can program the game for any size of board.

Winston



i already did that which is this
using For Loop

i just need to find all the possible diagonal. I think i'm missing few of them.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Andres John wrote:i already did that which is this
...
using For Loop

which isn't creating methods.

i just need to find all the possible diagonal. I think i'm missing few of them.

Which is probably because you haven't broken the problem down enough. Monolithic code like that, especially when it involves multi-layered loops, can become incrediby difficult to follow, so my suggestion would be something like this:
1. You know how to find a winning line on a 2D board. Well, in an NxNxN cube there are 3N "planes" or "sections" - N for each of the x, y and z dimensions - each of which is the equivalent of a 2D TTT board. Write a method that returns you a single plane of NxN squares (the x dimension planes are dirt simple; the y and z ones only marginally more difficult).
2. Run the method for each plane in your cube, and check what it returns just like you would an ordinary TTT board.
3. The only "lines" left to process after that are your 4 "apex-to-apex" diagonals.

HIH

Winston
Andres John
Ranch Hand

Joined: Jun 07, 2012
Posts: 34
Winston Gutkowski wrote:
Andres John wrote:i already did that which is this
...
using For Loop

which isn't creating methods.

i just need to find all the possible diagonal. I think i'm missing few of them.

Which is probably because you haven't broken the problem down enough. Monolithic code like that, especially when it involves multi-layered loops, can become incrediby difficult to follow, so my suggestion would be something like this:
1. You know how to find a winning line on a 2D board. Well, in an NxNxN cube there are 3N "planes" or "sections" - N for each of the x, y and z dimensions - each of which is the equivalent of a 2D TTT board. Write a method that returns you a single plane of NxN squares (the x dimension planes are dirt simple; the y and z ones only marginally more difficult).
2. Run the method for each plane in your cube, and check what it returns just like you would an ordinary TTT board.
3. The only "lines" left to process after that are your 4 "apex-to-apex" diagonals.

HIH
Winston

k will try to figure this out
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Check winner diagonal