2013-04-21 2 views
0

Я пытаюсь преобразовать алгоритм 3x3 tic tac toe в алгоритм 5x5 tic tac toe. Я искал множество алгоритмов, но каждый раз, когда я модифицировал компьютер кода, он начинается с первой строки и продолжается из каждого столбца в этой строке.Преобразование 3x3 tictactoe в 5x5 tictactoe

Это код, который я нашел в Интернете

общественного класса TicTacToeAI {

/* the board */ 
private int board[][]; 
/* empty */ 
public static final int EMPTY = 0; 
/* player one */ 
public static final int ONE = 1; 
/* player two */ 
    public static final int TWO = 2; 

public TicTacToeAI() { 
    board = new int[3][3]; 
} 

/* get the board value for position (i,j) */ 
public int getBoardValue(int i,int j) { 
    if(i < 0 || i >= 3) return EMPTY; 
    if(j < 0 || j >= 3) return EMPTY; 
    return board[i][j]; 
    } 

/* set the board value for position (i,j) */ 
public void setBoardValue(int i,int j,int token) { 
    if(i < 0 || i >= 3) return; 
    if(j < 0 || j >= 3) return; 
    board[i][j] = token; 
    } 

/* calculate the winning move for current token */ 
public int []nextWinningMove(int token) { 

    for(int i=0;i<3;i++) 
    for(int j=0;j<3;j++) 
    if(getBoardValue(i, j)==EMPTY) { 
    board[i][j] = token; 
    boolean win = isWin(token); 
    board[i][j] = EMPTY; 
    if(win) return new int[]{i,j}; 
    } 

    return null; 
    } 

    public int inverse(int token) { 
    return token==ONE ? TWO : ONE; 
} 

    /* calculate the best move for current token */ 
    public int []nextMove(int token) { 

     /* lucky position in the center of board*/ 
     if(getBoardValue(1, 1)==EMPTY) return new int[]{1,1}; 

     /* if we can move on the next turn */ 
     int winMove[] = nextWinningMove(token); 
     if(winMove!=null) return winMove; 

     /* choose the move that prevent enemy to win */ 
     for(int i=0;i<3;i++) 
      for(int j=0;j<3;j++) 
       if(getBoardValue(i, j)==EMPTY) 
       { 
        board[i][j] = token; 
       boolean ok = nextWinningMove(inverse(token)) == null; 
        board[i][j] = EMPTY; 
        if(ok) return new int[]{i,j}; 
       } 

     /* choose available move */ 
     for(int i=0;i<3;i++) 
      for(int j=0;j<3;j++) 
       if(getBoardValue(i, j)==EMPTY) 
        return new int[]{i,j}; 

     /* no move is available */ 
     return null; 
    } 

/* determine if current token is win or not win */ 
public boolean isWin(int token) { 
     final int DI[]={-1,0,1,1}; 
     final int DJ[]={1,1,1,0}; 

     for(int i=0;i<3;i++) 
      for(int j=0;j<3;j++) { 

      /* we skip if the token in position(i,j) not equal current token */ 
      if(getBoardValue(i, j)!=token) continue; 

       for(int k=0;k<4;k++) { 
        int ctr = 0; 
           while(getBoardValue(i+DI[k]*ctr, j+DJ[k]*ctr)==token) ctr++; 

        if(ctr==3) return true; 
      } 
     } 
     return false; 
    } 

}

И это код, который я изменил:

/* the board */ 
private int board[][]; 
/* empty */ 
public static final int EMPTY = 0; 
/* player one */ 
public static final int ONE = 1; 
/* player two */ 
    public static final int TWO = 2; 

public TicTacToeAI() { 
    board = new int[5][5]; 
} 

/* get the board value for position (i,j) */ 
public int getBoardValue(int i,int j) { 
    if(i < 0 || i >= 5) return EMPTY; 
    if(j < 0 || j >= 5) return EMPTY; 
    return board[i][j]; 
    } 

/* set the board value for position (i,j) */ 
public void setBoardValue(int i,int j,int token) { 
    if(i < 0 || i >= 5) return; 
    if(j < 0 || j >= 5) return; 
    board[i][j] = token; 
    } 

/* calculate the winning move for current token */ 
public int []nextWinningMove(int token) { 

    for(int i=0;i<5;i++) 
    for(int j=0;j<5;j++) 
    if(getBoardValue(i, j)==EMPTY) { 
    board[i][j] = token; 
    boolean win = isWin(token); 
    board[i][j] = EMPTY; 
    if(win) return new int[]{j,i}; 
    } 

    return null; 
    } 

    public int inverse(int token) { 
    return token==ONE ? TWO : ONE; 
} 

    /* calculate the best move for current token */ 
    public int []nextMove(int token) { 

     /* lucky position in the center of board*/ 
     if(getBoardValue(2, 2)==EMPTY) return new int[]{2,2}; 

     /* if we can move on the next turn */ 
     int winMove[] = nextWinningMove(token); 
     if(winMove!=null) return winMove; 

     /* choose the move that prevent enemy to win */ 
     for(int i=0;i<5;i++) 
      for(int j=0;j<5;j++) 
       if(getBoardValue(i, j)==EMPTY) 
       { 
        board[i][j] = token; 
       boolean ok = nextWinningMove(inverse(token)) == null; 
        board[i][j] = EMPTY; 
        if(ok) return new int[]{i,j}; 
       } 

     for(int i=1;i<4;i++) 
      for(int j=1;j<4;j++) 
       if(getBoardValue(i, j)==EMPTY) 
        return new int[]{i,j}; 
       /* choose available move */ 
       else{ 
        for(i=0;i<5;i++) 
         for(j=0;j<5;j++) 
          if(getBoardValue(i, j)==EMPTY) 
           return new int[]{i,j}; 

       } 
     /* no move is available */ 
     return null; 
    } 

/* determine if current token is win or not win */ 
public boolean isWin(int token) { 
     final int DI[]={-1,0,1,1,1,0}; 
     final int DJ[]={1,1,1,0,-1,0}; 

     for(int i=0;i<5;i++) 
      for(int j=0;j<5;j++) { 

      // we skip if the token in position(i,j) not equal current token 
      if(getBoardValue(i, j)!=token) continue; 

       for(int k=0;k<5;k++) { 
        int ctr = 0; 
           while(getBoardValue(i+DI[k]*ctr, j+DJ[k]*ctr)==token) ctr++; 

        if(ctr==4) return true; 
      } 
     } 
     return false; 
    } 

}

Спасибо за помощь

+1

Это не вопрос. – djechlin

ответ

2

Стратегия, используемая исходной программой, применима только к 3x3 tic-tac-toe - она ​​не смотрит вперед за пределы своего следующего шага, поэтому она не может завершить цепочку с пятью движениями. (Он может закончить цепочку, если ей как-то удастся получить четыре подряд, и она заблокирует строку противником, но не может сделать ничего более сложного.) Вам нужно будет разработать новую стратегию, чтобы сделать это Работа.

+0

for (int i = 1; i <4; i ++) for (int j = 1; j <4; j ++) if (getBoardValue (i, j) == EMPTY) return new int [] {i, J}; /* выберите доступный ход */ else { для (i = 0; i <5; i ++) для (j = 0; j <5; j ++) if (getBoardValue (i, j) == EMPTY) return new int [] {i, j}; } /* Отсутствует движение */ return null; } – Surinovi

+0

Я использовал этот элемент управления, но он не работал – Surinovi

+0

Спасибо за ответ! Так как я пытаюсь создать игру, которая будет более сложной, я могу использовать эту проверку наилучшего перемещения или мне нужно использовать такой алгоритм, как MINIMAX? – Surinovi