JavaRanch » Java Forums »
Java »
Beginning Java
| Author |
write the code to check for winner
|
Andres John
Ranch Hand
Joined: Jun 07, 2012
Posts: 33
|
|
i just want to know which method i should add for winner and explain me how this code work. i'm not asking you give me solution.
i know that to check for winner i need to use the matrix form which is w=[1 0 0 0 0], w=[0 1 0 0 0], w=[0 0 1 0 0], w=[0 0 0 1 0], w=[0 0 0 0 1], and so on
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9952
|
|
I would be seriously amazed if anyone took the time to try and figure out your 450 lines of code that has exactly 8 comments.
Even if it were well commented, that is too much code to expect someone to go through.
finally, your question is too vague.
i just want to know which method i should add for winner and explain me how this code work.
questions like this can only be answered with:
First, figure out by hand what you need to do. How do you determine if someone is a winner? Then refine the steps and break it down into simple steps. Then, code that.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Kemal Sokolovic
Bartender
Joined: Jun 19, 2010
Posts: 792
|
|
Wow, it's been a while since I've seen n-level nested loops (n -> Infinity) like these...
I would be seriously amazed if anyone took the time to try and figure out your 450 lines of code that has exactly 8 comments.
I would bet now that even OP won't be able to figure out how the code works in, like, 2 weeks...
|
The quieter you are, the more you are able to hear.
|
 |
Jayesh A Lalwani
Bartender
Joined: Jan 17, 2008
Posts: 1272
|
|
What is this code? TicTacTOe? I am assuming that the function takes the token of the player ("X" or "O") and needs to check the board to see if "X" or "O" won.
One approach is to brute force it. There are 8 possible winning patterns in TicTacToe. Just check if the board matches one of those 8 patterns.
If you don;t want to brute force it, you should really keep a "score" for every row column and diagonal, and change the score everytime a move is made. So, let's say, an X mark does a +1 on ever row, col and diagonal that the cell belongs to, and O mark does a -1. You just check the scores for all the rows, columns and diagonals, if any of them is 3, X won.. if any of them is -3 O won. Bada Bing Bada Boom
|
 |
William P O'Sullivan
Ranch Hand
Joined: Mar 28, 2012
Posts: 860
|
|
This is probably the code they used behind the scenes in "War Games"!
WP
|
 |
Andres John
Ranch Hand
Joined: Jun 07, 2012
Posts: 33
|
|
Jayesh A Lalwani wrote:What is this code? TicTacTOe? I am assuming that the function takes the token of the player ("X" or "O") and needs to check the board to see if "X" or "O" won.
One approach is to brute force it. There are 8 possible winning patterns in TicTacToe. Just check if the board matches one of those 8 patterns.
If you don;t want to brute force it, you should really keep a "score" for every row column and diagonal, and change the score everytime a move is made. So, let's say, an X mark does a +1 on ever row, col and diagonal that the cell belongs to, and O mark does a -1. You just check the scores for all the rows, columns and diagonals, if any of them is 3, X won.. if any of them is -3 O won. Bada Bing Bada Boom
k now i'm getting the ideas thanks
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5891
|
|
William P O'Sullivan wrote:This is probably the code they used behind the scenes in "War Games"!
WP
Hmm... "WP" ... "WOPR" ... Coincidence?
|
 |
 |
|
|
subject: write the code to check for winner
|
|
|
|