// Computer Science 367, Section 3, Fall 1997
// Instructor: Michael Siff (siff@cs.wisc.edu)
//
// Programming Assignment Two
//
// <student name & login here>
//
//
// FILE: play.C
//
//
// Function provided: play, a read-evalutaion loop for the aMAZEd game. 
//
// Calls the getNextAction function to get the next user move. The loop
// continues until either the exit is found (WIN), too many moves have
// occurred (LOSE), or the user chooses q (QUIT) as the action.
// 


#include "game.h"

// you might want to add some other includes here...

void play(const Maze& M, Player& p)
{
  action act;
  direction dir=EAST, lastDir;
  unsigned moves=0;

  bool movement=false;

  do {
    lastDir = dir;

    // display nearby (relative to player position) parts of the maze
    M.displayNeighbors(p.getY(), p.getX());
    // update the message bar with the number of moves played
    messageBar("aMAZEd", moves);

    movement=false;
    act = getNextAction();
    moves++;

    if (moves==MAX_MOVES)
      act = LOSE;

    switch(act) {
    case GO_EAST:
      dir = EAST;
      movement = true;
      break;
    case GO_WEST:
      movement = true;
      dir = WEST;
      break;
    case GO_NORTH:
      movement = true;
      dir = NORTH;
      break;
    case GO_SOUTH:
      movement = true;
      dir = SOUTH;
      break;
    case GO_BACK:
      dir = reverse(lastDir);
      break;
    case CLEAR:
      // implement this!
      break;
    case RESET:
      // implement this!
      break;
    case WIN:
    case LOSE:
    case QUIT:
      break;
    }

    // if the action was to move in a specific direction and that direction
    // is not blocked by a wall
    if (movement && M.isValidDirection(p.getY(), p.getX(), dir)) {
      // if the direction is the opposite of the last direction moved in,
      // don't count either of last two moves on maps:
      // implement this!

      // otherwise do this:
	p.go(dir);
    }

    if (M.isExit(p.getY(), p.getX()))
      act = WIN;
      
  } while (act != QUIT && act != LOSE && act != WIN);

  switch(act) {
  case LOSE:
    messageBar("You LOSE! Use the quit button to quit.",0); 
    while (getNextAction()!=QUIT);
    break;
  case WIN:
    messageBar("You WIN! Press q to quit or other action key to see route to exit.",0);
    while(getNextAction()!=QUIT) {
      // implement this! (showing the route from start to exit)
    }
    break;
  default:
    break;
  }
}
