// class_eg4.cpp - an implementation of simple_game using classes.
// Shows the use of private and public access to data and methods.
// In general practice, it is best to make as much data + methods
// private as possible. Often class functions are used to allow access to 
// private variables, like isLoc() does here.
#include <iostream>
using namespace std;

class Player {
private:  // all these private variables can only be accessed within Player functions.
	int xp;
	int yp;
	char name;
public:
	Player(int x, int y, char n);
	~Player();
	void move(char dir);
	void print();
	bool isLoc(int x, int y); // introducing a function that allows main() to check the position
							  // of the Player.
};

Player::Player(int x, int y, char n) {
	xp = x;
	yp = y;
	name = n;
}

Player::~Player() {
}

void Player::move(char dir) {
	if (dir == 'n') yp--;
	else if (dir == 's') yp++;
	else if (dir == 'e') xp++;
	else if (dir == 'w') xp--;
}

void Player::print() {
  cout << "  " << name << "  ";
}

// This function takes in a coordinate pair and compares it to the Player's
// x and y positions defined in our Player class (xp and yp). It returns a
// boolean value (true or false) depending on this.
bool Player::isLoc(int x, int y) {
	if (xp == x && yp == y) return true;
	else return false;
}

int main() {

  char direction;
  char whichPlayer;

  Player myGuy = Player(2,3,'a');
  Player myGuy2 = Player(3,2,'b');
  Player *whichGuy;

  while(direction != 'q') {

    for(int y=0;y<5;y++) {
      for(int x=0;x<5;x++) {
        if (myGuy.isLoc(x,y)) // using member function to access private vars xp and yp.
          myGuy.print();
        else if (myGuy2.isLoc(x,y))  // using member function to access private vars xp and yp.
          myGuy2.print();
        else
          cout << '(' << x << "," << y << ") ";
      }
      cout << endl;
    }

	cout << "\nEnter in a player (a or b): ";
    cin >> whichPlayer;
    cout << "Enter in a direction (q to quit): ";
    cin >> direction;

	if (whichPlayer == 'a') whichGuy = &myGuy;
	if (whichPlayer == 'b') whichGuy = &myGuy2;
	(*whichGuy).move(direction);

  }

}

