/******************************* FILE HEADER **********************************
    Program:          Tic Tac Toe, version 1
    File:             Player.java
    Main File:        TicTacToe1.java
    
    Author:           James D. Skrentny
                      copyright 2000, all rights reserved
    Course:           CS 302, Fall 2000, Lectures 1 & 2
    
    Compiler:         CodeWarrior IDE 4.0 (JDK 1.2)
    Platform:         Windows NT 4.0
 **************************** 80 columns wide *********************************/

import java.io.*;       // required for console input

/**
 * The Player class represents a player of the game Tic Tac Toe.
 *
 * Bugs: none known
 **/

/*
 * The following concepts are illustrated:
 *     - console input (see import, stdin declaration, getMove and getName)
 *     - try/catch statement (see getMove and getName)
 *     - returning a locally constructed object (see getMove)
 */

public class Player {

    private Mark   mark;        // player's mark, assumed to be 1 char.
    private String name;        // player's actual name, ok if many chars.
    
    // used for console input that is discussed during week 12
    private static BufferedReader stdin = new BufferedReader (
                                   new InputStreamReader(System.in));
    
    /**         
    * Constructs a Player object with an inputted name and specified mark.
    *
    * @param specifiedMark player's specified mark, assumed to be 1 char.
    **/
    public Player (String specifiedMark) {
    
        mark = new Mark(specifiedMark);

        // this code gets user input via the console window without
        // using javabook classes, this will be discussed during weeks
        // 12 and 13 of class
        try {
            System.out.print("Player " + mark + ", please enter name: ");
            name = stdin.readLine();
        } catch (IOException oops) {
            System.out.println("\n\nInput Error.  BYE!");
            System.exit(1);
        }
    }
    
    /**         
    * Gets a player's move from input.
    * @return a Move object of the player's chosen move
    **/
    public Move getMove () {
    
        int row = 0, col = 0;   // default move coordinates
        
        // this code gets user input via the console window without
        // using javabook classes, this will be discussed during weeks
        // 12 and 13 of class
        try {
            System.out.println("\n\n" + name + ", " + mark + " moves next.");
            System.out.print("\nPlease enter row:    ");
            row = Integer.parseInt(stdin.readLine());
            System.out.print("Please enter column: ");
            col = Integer.parseInt(stdin.readLine());
        } catch (IOException oops) {
            System.out.println("\n\nInput Error.  BYE!");
            System.exit(1);
        } catch (NumberFormatException oops) {
            System.out.println("\n\nNumber Format Error.  BYE!");
            System.exit(1);
        }
        
        //construct and then return the new Move object
        return new Move(row,col);
    }
    
    /**         
    * Gets a player's mark.
    *
    * @return the player's mark
    **/
    public Mark getMark() {
        return mark;
    }
    
}
