import sg.edu.nus.comp.cs3243.pipedream.*;

import java.util.Random;
import java.io.*;

/** MyPlayer is a sample class that extends the Player abstract class.
 * You'll have to write your own class that will extend the class in a non-interactive way.
 * <P>
 * In this sample class, the play() method prompts the user for input and executes the action.
 * A very intelligent agent, it just passes the work of playing the game to the human manning standard input.
 * Your agent will have to do it without the help of a person.
 * <P>
 * $Log: MyPlayer.java,v $
 * Revision 1.1  2004/01/15 10:05:48  rpnlpir
 * Initial revision
 * 
 * @author	Min-Yen Kan 
 * @version	$Id: MyPlayer.java,v 1.1 2004/01/15 10:05:48 rpnlpir Exp rpnlpir $
 * @since 1.0 
 */
class MyPlayer extends Player {
    // constants that are used for the public accessor functions required by the Player abstract class
    static final String MATRIC_NUMBER = "AB1234567C";
    static final String NAME = "KAN Min-Yen";
    static final String EMAIL = "dcskmy@nus.edu.sg";
    static final String TAGLINE = "Game playing is fun!  Making agents to play for me is even more fun!";

    // empty constructor, no need for this sample class to have anything initialized
    MyPlayer () { ; }

    // publically-available accessor functions
    public String getMatric () { return (MATRIC_NUMBER); }
    public String getName () { return (NAME); }
    public String getEmail () { return (EMAIL); }
    public String getTagLine () { return (TAGLINE); }

    // the play method, this version passes the work to the input operator
    public Action play (Driver d) {
	Action a = new Action();				      // create a new Action object
	char[][] map = new char [7][7];				      // pointer for storing the board
	char[] queue = new char [5];				      // pointer for storing the queue

	map = d.getBoard();					      // read the contents of the board
	queue = d.getQueue();					      // read the contents of the queue
	
	d.printQueue();						      // print the queue so we can see what's going on
	d.printBoard();						      // same with the board

	// print some other diagnostics before setting the action.
	// Note that you have to use the methods through the Driver class
	System.out.print ("Score of agent on turn " + d.getTime() + ": " + d.getScore() + "\n");
	System.out.print ("Ooze time: " + d.getTimeToNextOoze() + " at (" + 
			  d.getNextOozeSpotX() + "," + d.getNextOozeSpotY() + ")\n");

	// read action from the standard input, in three lines 
	try {
	    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	    System.out.print("action (p,w,d)>  ");
	    a.setAction(in.readLine().charAt(0));
	    if (a.getAction() != 'w') { 
		System.out.print("x (0-6)>  ");
		a.setX(Integer.parseInt(in.readLine()));
		System.out.print("y (0-6)>  ");
		a.setY(Integer.parseInt(in.readLine()));
	    }
	} catch (IOException e) {
   	    return (null);					      // oops, yuck
	}

	return (a);						      // got the action, return it
    }

    // Here's another version of the play() method that randomly performs an action.
    // It plays quite badly.  You can swap its name with the method above to see it at work.
    public Action play2 (Driver d) {
	Random r = new Random();
	Action a = new Action();

	int actionType;						      // choose a rand action
	actionType = r.nextInt(3);
	
	if (actionType == 0) {					      // set the Action type appropriately
	    a.setAction(Constants.WAIT_ACTION);
	} else if (actionType == 1) {
	    a.setAction(Constants.DESTROY_ACTION);
	} else if (actionType == 2) { 
	    a.setAction(Constants.PLACE_ACTION);
	}

	a.setX(r.nextInt(7));					      // set the X coordinate randomly
	a.setY(r.nextInt(7));					      // set the Y coordinate randomly
	
	return a;						      // return the completely specified action
    }

    // Here's the driver for the entire game.  You can use the same in your program
    // When testing and grading your program, we'll use a variant of this main() method.
    public static void main (String [] args) {
	Driver d = new Driver();				      // create a game driver
	MyPlayer p = new MyPlayer();				      // create my game-playing agent
	boolean status = true;					      // initially, the game hasn't ended

	System.out.print ("This is Strategic Pipe Dreams, game playing agent by " + p.getName() + "(" + 
			  p.getMatric() + ")\n<" + p.getEmail() + "> who says: \"" + p.getTagLine() + "\"\n");
	for (; status == true;) {				      // while the game isn't over...
	    Action a = p.play(d);				      // ...get an action from the agent
	    status = d.processTurn(a);				      // ...and process it
	}

	// Game's over, what's my score?
	System.out.print ("My final score for this game was: " + d.getScore() + "\n");
    }
}
