Search

Friday, November 16, 2012

Day 99: Extending

This is just an idea, but, extending from a stone is something usefull to create a wall and to escape from a contact stone.

I know this method will change a lot but let's begin with a simple idea. If a stone contacts a limiting stone of my color, I will add a couple of points to this one.

For example:

       X
     E0

I am 0 and E is the move I am evaluating. I will add a couple of points to the evaluated move. Maybe a random between 1 and 3.

This is the first approach:

/** * If someone puts a stone that quits a liberty maybe it needs to extend. * @param move The move * @param board The Actual board * @param color The color of the move * @return */ public int extend(int move, Board board, int color){ //this will make that any contact move makes the player extend. boolean trueAdjacent=false; int points=0; Random rand=new Random(); if(board.getMove(move-1)==color){ trueAdjacent=searchAdjacent(move-1,board,1,color); if(trueAdjacent==true){ points=rand.nextInt(4); trueAdjacent=false; } } if(board.getMove(move+1)==color){ trueAdjacent=searchAdjacent(move+1,board,1,color); if(trueAdjacent==true){ points=rand.nextInt(4); trueAdjacent=false; } } if(board.getMove(move+board.getBoardSize()+1)==color){ trueAdjacent=searchAdjacent(move+board.getBoardSize()+1,board,1,color); if(trueAdjacent==true){ points=rand.nextInt(4); trueAdjacent=false; } } if(board.getMove(move-(board.getBoardSize()+1))==color){ trueAdjacent=searchAdjacent(move-(board.getBoardSize()+1),board,1,color); if(trueAdjacent==true){ points=rand.nextInt(4); trueAdjacent=false; } } return points; }
I need to create the searchAdjacent method, so this is what I thought:

/** * Search for x stones adjacents to one stone. * @param move The move * @param board the actual board * @param number The number of stones that are searched * @param color The color of the stone analized * @return returns true is number=number of stones ajacents */ public boolean searchAdjacent(int move, Board board,int number, int color){ boolean adjacents=false; int counter=0; int opponentColor; if(color==1){ opponentColor=2; }else{ opponentColor=1; } if(board.getMove(move+1)==opponentColor){ counter++; } if(board.getMove(move-1)==opponentColor){ counter++; } if(board.getMove(move+board.getBoardSize()+1)==opponentColor){ counter++; } if(board.getMove(move-(board.getBoardSize()+1))==opponentColor){ counter++; } if(counter==number){ adjacents=true; } return adjacents; }
Of course I still did not test this... maybe later.

2 comments:

  1. You're going to be using code like "move-(board.getBoardSize()+1)" a lot. I'd really recommend writing methods "int up(move, board)" and similarly for left, right, and down. The less time you have to rewrite this code, the less opportunities there are for errors to creep in. If you put these methods in the board object's class, it can simply be board.up(move).

    ReplyDelete
  2. Thanks a lot! Next post is about your idea :)

    ReplyDelete