*
The moose likes Beginning Java and the fly likes write the code to check for winner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "write the code to check for winner" Watch "write the code to check for winner" New topic
Author

write the code to check for winner

Andres John
Ranch Hand

Joined: Jun 07, 2012
Posts: 34


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: 11150
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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: 2271
    
  28

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: 34
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: 6109
    
    6

William P O'Sullivan wrote:This is probably the code they used behind the scenes in "War Games"!

WP



Hmm... "WP" ... "WOPR" ... Coincidence?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: write the code to check for winner
 
Similar Threads
JButtons fail to change
getGraphics returnning null pointer exception
Loops
StringBuffer class
Help Change Class Please