package pdp.scrabble.game.impl;
import java.awt.Color;
import java.awt.Graphics;
import org.jdom.Element;
import pdp.scrabble.game.BoardCase;
import pdp.scrabble.game.BoardCaseIndex;
import pdp.scrabble.game.BoardCaseState;
import pdp.scrabble.game.Letter;
import static pdp.scrabble.Factory.FACTORY;
import static pdp.scrabble.Language.getGameLang;
import static pdp.scrabble.game.BoardCaseIndex.NONE;
import static pdp.scrabble.game.BoardCaseState.FREE;
import static pdp.scrabble.game.BoardCase.CASE_SIZE;
import static pdp.scrabble.game.Board.HORI_DIM;
import static pdp.scrabble.game.Board.VERT_DIM;
public class BoardCaseImpl implements BoardCase {
/** Vertical coordinate. */
private int v = 0;
/** Horizontal coordinate. */
private int h = 0;
/** Multiplicator bonus point on word. */
private int wordMult = 0;
/** Multiplicator bonus point on letter. */
private int letterMult = 0;
/** Checked multiplicator uses (this avoid multi multiplicator). */
private boolean multUsed = false;
/** Letter reference over the case. */
private Letter letter = null;
/** Case color. */
private Color color = null;
/** Case state. */
private BoardCaseState state = null;
/** Case index in word referential. */
private BoardCaseIndex index = null;
/** Checked state (this avoid multi check). */
private boolean checked = false;
/** True to ignore adjacents word (this avoid multi adjacence check). */
private boolean ignoreAdjacent = false;
/** Create a new board case.
* @param letterMult letter multiplicator.
* @param wordMult word multiplicator.
* @param v vertical location.
* @param h horizontal location.
*/
public BoardCaseImpl(int letterMult, int wordMult, int v, int h) {
this.letterMult = letterMult;
this.wordMult = wordMult;
this.multUsed = false;
this.state = FREE;
this.index = NONE;
this.checked = false;
this.ignoreAdjacent = false;
this.v = v;
this.h = h;
// Normal case
if (letterMult == 1 && wordMult == 1) {
this.color = NORMAL_CASE_COLOR;
}
// Letter double and triple
if (letterMult == 2 && wordMult == 1) {
this.color = DOUBLE_LETTER_CASE_COLOR;
}
if (letterMult == 3 && wordMult == 1) {
this.color = TRIPLE_LETTER_CASE_COLOR;
}
// Word double and triple
if (letterMult == 1 && wordMult == 2) {
this.color = DOUBLE_WORD_CASE_COLOR;
}
if (letterMult == 1 && wordMult == 3) {
this.color = TRIPLE_WORD_CASE_COLOR;
}
}
@Override
public void render(Graphics g, int x, int y) {
// Render letter if it exists
if (this.letter != null) {
this.letter.render(g, x, y);
} // Render blank case else
else {
g.setColor(this.color);
g.fill3DRect(x, y, CASE_SIZE, CASE_SIZE, true);
// Draw circle for center case
if (this.getV() == VERT_DIM / 2
&& this.getH() == HORI_DIM / 2) {
g.setColor(Color.BLACK);
g.fillOval(x + CASE_SIZE / 4, y + CASE_SIZE / 4,
CASE_SIZE / 2 - 1, CASE_SIZE / 2 - 1);
}
else {
g.setColor(MULTIPLICATOR_FONT_COLOR);
g.setFont(Letter.FONT_VALUE);
if (this.color == DOUBLE_LETTER_CASE_COLOR) {
g.drawString(getGameLang("Letter"), x + 5, y + 20);
g.drawString("x2", x + 15, y + 30);
}
if (this.color == DOUBLE_WORD_CASE_COLOR) {
g.drawString(getGameLang("Word"), x + 7, y + 20);
g.drawString("x2", x + 15, y + 30);
}
if (this.color == TRIPLE_LETTER_CASE_COLOR) {
g.drawString(getGameLang("Letter"), x + 5, y + 20);
g.drawString("x3", x + 15, y + 30);
}
if (this.color == TRIPLE_WORD_CASE_COLOR) {
g.drawString(getGameLang("Word"), x + 7, y + 20);
g.drawString("x3", x + 15, y + 30);
}
}
}
}
@Override
public void setLetter(Letter letter) {
this.letter = letter;
}
@Override
public Letter getLetter() {
return this.letter;
}
@Override
public void setState(BoardCaseState state) {
this.state = state;
}
@Override
public BoardCaseState getState() {
return this.state;
}
@Override
public void setIndex(BoardCaseIndex index) {
this.index = index;
}
@Override
public BoardCaseIndex getIndex() {
return this.index;
}
@Override
public void setChecked(boolean checked) {
this.checked = checked;
}
@Override
public boolean isChecked() {
return this.checked;
}
@Override
public void setAdjacentIgnorence(boolean ignore) {
this.ignoreAdjacent = ignore;
}
@Override
public boolean getAdjacentIgnorence() {
return this.ignoreAdjacent;
}
@Override
public int getWordMult() {
return this.wordMult;
}
@Override
public int getLetterMult() {
return this.letterMult;
}
@Override
public void setMultUsed(boolean multiplicatorUsed) {
this.multUsed = multiplicatorUsed;
}
@Override
public boolean getMultUsed() {
return this.multUsed;
}
@Override
public int getV() {
return this.v;
}
@Override
public int getH() {
return this.h;
}
@Override
public void save(Element root) {
Element element = new Element("Case");
root.addContent(element);
element.setAttribute("h", String.valueOf(this.getH()));
element.setAttribute("v", String.valueOf(this.getV()));
element.setAttribute("lmult", String.valueOf(this.getLetterMult()));
element.setAttribute("wmult", String.valueOf(this.getWordMult()));
element.setAttribute("state", this.getState().name());
if (this.letter != null) {
this.letter.save(element);
}
}
@Override
public void load(Element root) {
this.h = Integer.parseInt(root.getAttributeValue("h"));
this.v = Integer.parseInt(root.getAttributeValue("v"));
this.letterMult = Integer.parseInt(root.getAttributeValue("lmult"));
this.wordMult = Integer.parseInt(root.getAttributeValue("wmult"));
this.state = BoardCaseState.valueOf(root.getAttributeValue("state"));
Element Eletter = root.getChild("Letter");
if (Eletter != null) {
this.letter = FACTORY.createLetter(' ', 0, 0);
this.letter.load(Eletter);
}
}
@Override
public BoardCase clone() {
BoardCase boardCase = FACTORY.createBoardCase(
this.getLetterMult(), this.getWordMult(),
this.getV(), this.getH());
boardCase.setAdjacentIgnorence(this.ignoreAdjacent);
boardCase.setChecked(this.checked);
boardCase.setIndex(this.getIndex());
boardCase.setMultUsed(this.multUsed);
boardCase.setState(this.getState());
Letter l = this.getLetter();
if (l != null) {
boardCase.setLetter(l.clone());
}
return boardCase;
}
}