/* * SlidingTilePuzzle.java * A CLUI-based version of the game. * To compile: javac -classpath nplayer.jar:. SlidingTilePuzzle.java * To run: java -classpath nplayer.jar:. SlidingTilePuzzle */public class SlidingTilePuzzle extends ComputerGame implements CLUIPlayableGame, GUIPlayableGame {    private char puzzle[] = {'R','R','R',' ','L','L','L'};    private String solution = "LLL RRR";    private boolean userQuits = false;    private int blankAt = 3;    public SlidingTilePuzzle() {        super(1);    }    public SlidingTilePuzzle (Player p) {           super(1);        addPlayer(p);    }    /** Overridden method from TwoPlayerGame */    public String getRules() {           return "\n*** The Rules of SlidingTilePuzzle  ***\n" +        "The goal is to change the puzzle  into [LLL RRR]\n" +        "  by sliding tiles L(eft) or R(ight).\n" +        "Tiles labeled R can only move right.\n" +        "Tiles labeled L can only move left.\n" +        "A tile can be moved into the blank space.\n" +        "A tile can jump over 1 other tile.\n";    } //getRules()    public boolean gameOver() {  // Returns true if puzzle solved        StringBuffer sb = new StringBuffer();        sb.append(puzzle);        return sb.toString().equals(solution);        } // gameOver()    public String getWinner() {           if (gameOver())            return "\nYou did it! Very Nice!\n";        else return "\nGood try. Try again!\n";    } // getWinner()    public void play(UserInterface ui) {        ui.report(getRules());        ui.report(listPlayers());        ui.report(reportGameState());	while(!gameOver() && !userQuits) {              ui.prompt(getGamePrompt());            ui.report(submitUserMove(ui.getUserInput()));            ui.report("0123456\n" + reportGameState());        } // while        ui.report(getWinner());    }    /** Implementation of method from CLUIPlayable */    public String reportGameState() {           StringBuffer sb = new StringBuffer();        sb.append(puzzle);        return sb.toString();    } // report()    /** Implementation of method from CLUIPlayable */    public String getGamePrompt() {            return "\nWhat tile do you want to move, 0..6 or -1 to give up? ";    } //prompt()    /** Implementation of method from CLUIPlayable */    public String submitUserMove(String s) {           int tile = Integer.parseInt(s);        if (tile < -1 || tile > 6)            return "That is an illegal move.\n";        else if (tile == -1) {            userQuits = true;            return "The user gives up.\n";	} else if (tile == blankAt) {            return "That's an illegal move.\n";	} else {            char ch = puzzle[tile];            if (ch == 'L' && (blankAt == tile-1 || blankAt == tile-2))		swapTiles(tile,blankAt);            else if (ch == 'R' && (blankAt == tile+1 || blankAt == tile+2))		swapTiles(tile,blankAt);            else 		return "That's an illegal move.\n";	}        return "That move is legal.\n";    } //move()    private void swapTiles(int ti, int bl) {        char ch = puzzle[ti];        puzzle[ti] = puzzle[bl];        puzzle[bl] = ch;        blankAt = ti;   // Reset the blank    }    public static void main(String args[])  {  	KeyboardReader kb = new KeyboardReader();        SlidingTilePuzzle puzzle  = new SlidingTilePuzzle(new SlidingTilePlayer());        puzzle.play(kb);    } //main()    } //SlidingTilePuzzle