-1

Wanna simulate the next move for my AI in my othello game, but instead of just returning the next move, it makes all the moves on the original board instead of just simulating on a clone and the game ends.

public class GameState implements Cloneable{
private Node[][] board;                 // Game board
private int scorePlayer, scoreAI;       // Player scores ( will start at 0 )
private ArrayList<Node> validNodes;     // List holding all nodes possible to add pieces to 

/**
 * Creates the game state
 */
public GameState(){
    // create size x size board
    this.board = new Node[Setting.BOARD_SIZE][Setting.BOARD_SIZE];
    validNodes = new ArrayList<>();
    scorePlayer = 0;
    scoreAI = 0;

protected GameState clone() {
    return new GameState(this);
}------------------------ CLONE METHOD----------------



public int search(GameState board, Player player, int alpha, int beta, int depth, ScoreEval function) {
    int record = Integer.MIN_VALUE;
    Node maxMove = null;
    int result;

    GameState subBoard = board.clone();
    if (depth <= 0 || board.getValidMoves().size()==0) {
        record = function.evaluate(board, player);
    } else {
    ArrayList<Node> possibleMoves = board.getValidMoves();
        if (!possibleMoves.isEmpty()) {
            for (int i =0; i<possibleMoves.size();i++) {
                Node nod = possibleMoves.get(i);
                subBoard = board.clone();
                subBoard.setPiece(nod.x,nod.y, player.type);
                if(player.type==Setting.TILE_AI){
                    result = -search(subBoard, aiAss1.Controller.pHum, alpha, beta, depth - 1, function);
                }
                else{
                    result = -search(subBoard, aiAss1.Controller.pAI, alpha, beta, depth - 1, function);
                }

            if (result > record) {
                    record = result;
                    maxMove = nod;
                }
            }
        } else {
            record = -search(subBoard, player, alpha, beta, depth - 1, function);
        }
    }
    bestMove = maxMove;
    return record;
}
NosQ
  • 11
  • 4
  • 2
    are you sure you're copying the board's internal state? (all mutable objects) – Reut Sharabani Sep 29 '16 at 16:59
  • 1
    Your `clone()` method looks broken. You're creating a shallow copy most likely. – Kayaman Sep 29 '16 at 17:00
  • Add your implementation of `clone` for `GameState` – QBrute Sep 29 '16 at 17:01
  • @Kayaman is probably right. But do you really need to clone the whole board? Wouldn't it be more performant to reverse moves? – maraca Sep 29 '16 at 17:09
  • Tried with ---- protected GameState clone() throws CloneNotSupportedException { GameState state = (GameState)super.clone(); return state; } But dint work either – NosQ Sep 29 '16 at 17:12
  • @maraca Unless the board is 10000x10000, it's unlikely to matter. Besides, first you make it work correctly, then you make it work fast (if necessary). – Kayaman Sep 29 '16 at 17:13
  • @Kayaman I see your point about making it work first. But for alpha-beta pruning reversing is much better in general in my opinion. Usually you make a move evaluate another one etc. unless it's the last one or you prune. So there are not that many reverses, but there would be a lot of clones. – maraca Sep 29 '16 at 17:16
  • added the class i wanted to clone – NosQ Sep 29 '16 at 18:04
  • You need to add the copy constructor of GameState too or we don't see anything. Do you clone board and validNodes there? – maraca Sep 30 '16 at 01:40

1 Answers1

0

Try breaking down you logic into parts, putting each in a separate method. Then write a unit test for each part, and check that each part does what you want.

This is how you should write software.

David Roussel
  • 5,788
  • 1
  • 30
  • 35
  • Im just sharing bits of code that is relevant to my question, the problem seems to be that the clone only returns a shallow copy, – NosQ Sep 29 '16 at 18:08
  • The semantics of clone are not clear, and it is best avoided. If you want a deep clone, write a deepClone() method. – David Roussel Sep 29 '16 at 18:11