// Copyright 1996, Marimba Inc. All Rights Reserved.
// @(#)Rack.java, 1.2, 11/18/96

package wordsmith;

import java.net.*;
import java.io.*;
import java.awt.*;
import java.util.*;

import marimba.channel.*;
import marimba.gui.*;
import marimba.io.*;
import marimba.desktop.*;

/**
 * Comment here
 *
 * @author      Carl W. Haynes III
 * @version     1.2, 11/18/96
 */


public class Rack {

    static final int NUM_LETTERS = 10;

    char letters[] = new char[10];
    int originX[] = new int[10];
    int originY[] = new int[10];
    int nextAvailable = 0;

    public Rack() {
	reset();
    }

    public void reset() {
	clear();
    }

    public void clear() {
	for (int i = 0 ; i < NUM_LETTERS ; i++) {
	    letters[i] = (char)-1;
	}
	nextAvailable = 0;
    }

    public char[] getLetters() {
	return(letters);
    }

    public char getLetter(int i) {
	return(letters[i]);
    }

    public int getNextAvailable() {
	return nextAvailable;
    }

    public int getOriginX(int i) {
	return(originX[i]);
    }

    public int getOriginY(int i) {
	return(originY[i]);
    }


    public void addTile(int x, int y, char letter) {
	if (nextAvailable == -1)
	    return;
	originX[nextAvailable] = x;
	originY[nextAvailable] = y;

	letters[nextAvailable++] = letter;
	if (nextAvailable >= NUM_LETTERS)
	    nextAvailable = (char)-1;
    }

    /** always assumes tile to be removed is last one avaliable */
    public void removeTile(int tile) {
	letters[tile] = (char)-1;
	nextAvailable = tile;
    }

    /** always assumes tile to be removed is last one avaliable */
    public void removeLastAvailableTile() {
	if (nextAvailable == 0) {
	    return;
	}

	if (nextAvailable == -1) {
	    removeTile(NUM_LETTERS-1);
	    nextAvailable = NUM_LETTERS-1;
	    return;
	}

	removeTile(--nextAvailable);
    }

    /** i want to move this tile, can i? */
    public boolean tileAvailableForMoving(int tile) {
	if (letters[tile] == (char)-1)  {
	    return(false);
	}

	if (tile == NUM_LETTERS-1) {
	    return(true);
	}

	if (letters[tile+1] == (char)-1)  {
	    return(true);
	}

	return(false);


    }

    /** how many slots filled? */
    public int numTilesUsed() {
	if (nextAvailable == (char)-1) {
	    return(NUM_LETTERS);
	}
	return(nextAvailable);
    }

    public String getWord() {
	if (nextAvailable == 0) {
	    return("");
	}

	//DebugStr("numTilesUsed: " + numTilesUsed());
	String s = "";
	for (int i = 0 ; i < numTilesUsed() ; i++) {
	    s += (char)letters[i];
	}
	return(s);
    }

    /**
     * for debugging
     */
    public void DebugStr(String s) {
        System.out.println(this.getClass().getName() + ": " + s);
	System.out.flush();
    }
}
