我写了一个游戏井字棋在Java中,和我目前确定的游戏帐号年底的游戏是在接下来可能出现的情况的方法:
I've written a game of tic-tac-toe in Java, and my current method of determining the end of the game accounts for the following possible scenarios for the game being over:
董事会已满,没有赢家尚未宣布:游戏是平局。 十字赢得了。 在圈赢得。不幸的是,这样做时,它读取通过一个predefined设定的从表这些场景。这不一定是坏事考虑到有一个董事会只有9位,因此该表是有点小,但有确定的比赛已经结束了一个更好的算法的方法吗?中是否有人赢了还是没有确定是问题的肉,因为检查,如果9位已满实在是微不足道。
Unfortunately, to do so, it reads through a predefined set of these scenarios from a table. This isn't necessarily bad considering that there are only 9 spaces on a board, and thus the table is somewhat small, but is there a better algorithmic way of determining if the game is over? The determination of whether someone has won or not is the meat of the problem, since checking if 9 spaces are full is trivial.
的表的方法可能是溶液,但如果不是,是什么?此外,如果董事会没有大小为n = 9?如果它被一个更大的板,说n = 16的每组25,等等,造成连续放置的物品的数量来赢得为x = 4中,x = 5等?一般的算法,用于所有的n = {9,16,25,36 ...}?
The table method might be the solution, but if not, what is? Also, what if the board were not size n=9? What if it were a much larger board, say n=16, n=25, and so on, causing the number of consecutively placed items to win to be x=4, x=5, etc? A general algorithm to use for all n = { 9, 16, 25, 36 ... }?
您知道一个成功的举动只能发生十大之后或O-取得了他们最近的举动,所以你只能搜索行中包含/列具有可选的诊断在此举试图确定一个成功的董事会时,以限制搜索空间。此外,由于也有以平局井字棋游戏的移动固定数量,一旦最后一步是由如果它不是一个成功的举动是在默认情况下平局的比赛。
You know a winning move can only happen after X or O has made their most recent move, so you can only search row/column with optional diag that are contained in that move to limit your search space when trying to determine a winning board. Also since there are a fixed number of moves in a draw tic-tac-toe game once the last move is made if it wasn't a winning move it's by default a draw game.
编辑:这code是一个由N主板具有n个一排取胜(3×3板requries 3连胜,等等)
edit: this code is for an n by n board with n in a row to win (3x3 board requries 3 in a row, etc)
编辑:增加了code,检查防诊断,我想不出一个非循环的方式来确定的一点是对反诊断所以这就是为什么这一步是缺少
edit: added code to check anti diag, I couldn't figure out a non loop way to determine if the point was on the anti diag so thats why that step is missing
public class TripleT {
enum State{Blank, X, O};
int n = 3;
State[][] board = new State[n][n];
int moveCount;
void Move(int x, int y, State s){
if(board[x][y] == State.Blank){
board[x][y] = s;
}
moveCount++;
//check end conditions
//check col
for(int i = 0; i < n; i++){
if(board[x][i] != s)
break;
if(i == n-1){
//report win for s
}
}
//check row
for(int i = 0; i < n; i++){
if(board[i][y] != s)
break;
if(i == n-1){
//report win for s
}
}
//check diag
if(x == y){
//we're on a diagonal
for(int i = 0; i < n; i++){
if(board[i][i] != s)
break;
if(i == n-1){
//report win for s
}
}
}
//check anti diag (thanks rampion)
for(int i = 0;i<n;i++){
if(board[i][(n-1)-i] != s)
break;
if(i == n-1){
//report win for s
}
}
//check draw
if(moveCount == (n^2 - 1)){
//report draw
}
}
}