Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# write the code to check for winner

Andres John
Ranch Hand
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
Posts: 12185
34
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.

Kemal Sokolovic
Bartender
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...

Jayesh A Lalwani
Rancher
Posts: 2756
32
• 1
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
Posts: 859
This is probably the code they used behind the scenes in "War Games"!

WP

Andres John
Ranch Hand
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
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?