我發現這個工作液:是否有可能檢查井字遊戲的使用jGraphT遊戲的中獎條件?
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;
}
,但我想知道是否有使用圖形邏輯更優雅的解決方案。
更新:我也在考慮用我的知識在3×3板的不同和更大的變種,我相信這種方法並不能很好地擴展美觀。
例如:https://en.wikipedia.org/wiki/Teeko
這似乎很多優雅 - 你還有更多尋找? –
我想投資更多的圖表設置,然後能夠簡單地調用myGraph.isItWon() –
這種方法很好,可以擴展到64個單元格(即8x8)板很容易 –