Package pdp.scrabble.ia.impl

Source Code of pdp.scrabble.ia.impl.ClassicMoveModel

package pdp.scrabble.ia.impl;

import java.util.ArrayList;
import java.util.List;

import pdp.scrabble.game.Bag;
import pdp.scrabble.game.Board;
import pdp.scrabble.game.Letter;
import pdp.scrabble.game.Location;
import pdp.scrabble.game.Player;
import pdp.scrabble.game.impl.LocationImpl;
import pdp.scrabble.ia.Direction;
import pdp.scrabble.ia.MoveModel;

/** Classic implementation of a move model, as used in articles
*
* @author alexandre
*
*/
public class ClassicMoveModel implements MoveModel {

    private String word;
    private Location first;
    private Direction dir;
    private String leave;

    public ClassicMoveModel(String word, Location first, Direction dir, String leave) {
  this.word = word;
  this.first = new LocationImpl(first.getV(), first.getH());
  this.dir = dir;
  this.leave = leave;
    }

    @Override
    public String getWord() {
  return word;
    }

    @Override
    public Location getLocation() {
  return first;
    }

    @Override
    public String getRackLeave() {
  return leave;
    }

    public String toString() {
  return "("+getWord()+", "+getLocation()+getDirection()+", "+getRackLeave()+")";
    }

    @Override
    public Direction getDirection() {
  return this.dir;
    }

    @Override
    public void play(Board board, Player p) {
  Location currentLoc = this.first.clone();
  for (int i=0 ; i<this.word.length() ; i++) {
      char c = this.word.charAt(i);
      if (board.isFree(currentLoc.getH(), currentLoc.getV())) {
    Letter currentLetter = null;
    if (Character.isLowerCase(c)) {
        currentLetter = p.getRack().getLetter(Bag.JOKER);
        p.getRack().removeLetter(currentLetter);
        currentLetter.setJokerChar(Character.toUpperCase(c));
    }
    else {
        currentLetter = p.getRack().getLetter(c);
        p.getRack().removeLetter(currentLetter);
    }
    board.setCaseLetter(currentLoc.getV(), currentLoc.getH(), currentLetter, true);
      }
      this.dir.applyTo(currentLoc);
  }
    }

    @Override
    public int computePoints(Board b, Player p) {
  this.play(b, p);
  int points = b.getWordPoints();
  b.cancel(p);
  return points;
    }

}
TOP

Related Classes of pdp.scrabble.ia.impl.ClassicMoveModel

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.