Package pdp.scrabble.ia

Source Code of pdp.scrabble.ia.AIBoardModel

package pdp.scrabble.ia;

import pdp.scrabble.game.Board;
import pdp.scrabble.game.Letter;
import pdp.scrabble.game.Location;
import pdp.scrabble.game.impl.LocationImpl;
import pdp.scrabble.utility.CrossCheck;

/** Board modelisation retaining only characters
* should make algorithms faster
* also contains crossChecks
* @author alexandre
*
*/
public class AIBoardModel implements Cloneable {

    private char[][] charTab = new char[Board.HORI_DIM][Board.VERT_DIM];
   
    public AIBoardModel(Board board) {
  for (int i=0 ; i<board.VERT_DIM ; i++)
      for (int j=0 ; j<board.HORI_DIM ; j++) {
    Letter tmp = board.getCase(i, j).getLetter();
    if (tmp == null)
        charTab[i][j] = ' ';
    else
        charTab[i][j] = board.getCase(i, j).getLetter().getName();
      }
    }
   
    public boolean isFree(int x, int y) {
  return charTab[x][y] == ' ';
    }
   
    public boolean isFree(Location loc) {
  return charTab[loc.getV()][loc.getH()] == ' ';
    }
   
    public char getCharAt(int x, int y)
    {
        return charTab[x][y];
    }

    public char getCharAt(Location loc)
    {
  return charTab[loc.getV()][loc.getH()];
    }
   
    public boolean hasNeighbours(Location square, Direction dir) {
  Location prev=dir.applyReverseTo(square.clone()), next=dir.applyTo(square.clone());
  return (contains(prev) && !isFree(prev)) ||
    (contains(next) && !isFree(next));
    }
   
    public boolean isAnchorSquare(int x, int y) {
  if (!isFree(x, y))
      return false;
  boolean result = false;
  if (y-1>=0) result |= !isFree(x, y-1);
  if (x-1>=0) result |= !isFree(x-1, y);
  if (x+1<Board.VERT_DIM) result |= !isFree(x+1, y);
  if (y+1<Board.HORI_DIM) result |= !isFree(x, y+1);
  return result;
    }
   
    public boolean contains(Location square) {
  return (square.getH() >= 0 &&
    square.getH() < Board.HORI_DIM &&
    square.getV() >= 0 &&
    square.getV() < Board.VERT_DIM);
    }
   
    public void update(Board board) {
  for (int i=0 ; i<Board.VERT_DIM ; i++)
      for (int j=0 ; j<Board.HORI_DIM ; j++) {
    Letter tmp = board.getCase(i, j).getLetter();
    if (tmp == null) {
        if (charTab[i][j] != ' ')
      charTab[i][j] = ' ';
    }
    else if (this.charTab[i][j] != tmp.getName())
        charTab[i][j] = tmp.getName();
     
    }
   
    public String toString() {
  StringBuilder s = new StringBuilder();
  for (int i=0 ; i<charTab.length ; i++) {
    s.append(charTab[i]);
    s.append('\n');
  }
  //s.trimToSize()
  return s.toString();
    }
   
    public AIBoardModel clone() {
  AIBoardModel clone;
  try {
      clone = (AIBoardModel) super.clone();
  } catch (CloneNotSupportedException e) {
      // TODO Auto-generated catch block
      return null;
  }
  for (int i=0 ; i<charTab.length ; i++)
      clone.charTab[i] = this.charTab[i].clone();
  return clone;
    }
}
TOP

Related Classes of pdp.scrabble.ia.AIBoardModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.