I found this working solution:
private int[] winningPatterns = { 0b111000000, 0b000111000, 0b000000111, // rows
0b100100100, 0b010010010, 0b001001001, // cols
0b100010001, 0b001010100 // diagonals
};
/** Returns true if thePlayer wins */
private boolean hasWon(int thePlayer) {
int pattern = 0b000000000; // 9-bit pattern for the 9 cells
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
if (cells[row][col].content == thePlayer) {
pattern |= (1 << (row * 3 + col));
}
}
}
for (int winningPattern : winningPatterns) {
if ((pattern & winningPattern) == winningPattern)
return true;
}
return false;
}
but I would like to know if there is a more elegant solution using graph logic.
Update: I am also looking into using my knowledge at different and bigger variants of the 3x3 board and I believe this approach does not scale well aesthetically.
For example: https://en.wikipedia.org/wiki/Teeko