// Arup Guha
// 2/24/2026
// Game class to manange my "Pacman" game.

import java.util.*;

public class PacmanGame {

	// For my fruits!
	final public static String[] FRUITNAMES = {"strawberry", "banana", "orange", "kiwi"};
	final public static String[] COLORS = {"red", "yellow", "orange", "green"};
	final public static int[] POINTS = {100, 50, 150, 300};
	
	// Just to start stuff off.
	public static Random rndObj = new Random();
	
	// Parts of my PacmanGame object.
	private Pacman player;
	private int numFoodObj;
	private Piece[] food;
	private int timeStep;
	
	public PacmanGame(String playerName, int numFood) {
		
		// I start at the origin. Yes, the universe centers around PacMan!
		player = new Pacman(0, 0, playerName);
		
		// Allocate space for the food.
		numFoodObj = numFood;
		food = new Piece[numFoodObj];
		
		// Keeps track of how many moves were made.
		timeStep = 0;
		
		// Create random food objects.
		for (int i=0; i<numFoodObj; i++) {
			
			// int x, int y, int dx, int dy, String myName, String myColor, int myPoints
			int x = 1+rndObj.nextInt(10);
			int y = 1+rndObj.nextInt(10);
			int dx = -2 + rndObj.nextInt(5);
			int dy = -2 + rndObj.nextInt(5);
			
			// One out of every 3 things (roughly) will be fruit.
			if (rndObj.nextInt(3) == 0) {
				int fruitIdx = rndObj.nextInt(FRUITNAMES.length);
				food[i] = new Fruit(x, y, dx, dy, FRUITNAMES[fruitIdx], COLORS[fruitIdx], POINTS[fruitIdx]);
			}
			
			// Boring pieces!
			else
				food[i] = new Piece(x, y, dx, dy);
		}
	}
	
	// Returns a String representation of this PacmanGame.
	public String toString() {
		String res = player+"\n---------------------\n";
		res = res + "timestep = "+timeStep+"\n";
		res = res + "food left to eat\n-------------------\n";
		for (int i=0; i<numFoodObj; i++)
			res = res + food[i]+"\n";
		return res;
	}
	
	// Valid moves are 'U', 'D', 'L', 'R' and 'S' (default)
	public void move(char myMove) {
		
		// Update movement vector of player.
		if (myMove == 'U') player.incDY();
		if (myMove == 'D') player.decDY();
		if (myMove == 'L') player.decDX();
		if (myMove == 'R') player.incDX();
		
		// Move the player.
		player.move();
		
		// Move the food.
		for (int i=0; i<numFoodObj; i++)
			food[i].move();
		
		// Figure out how many are eaten.
		int lose = numEaten();
		int newSize = numFoodObj - lose;

		/*** I should detect if all of the Pieces are eaten.
		     A great exercise for students!!!
		***/
		
		Piece[] survived = new Piece[newSize];
		for (int i=0,j=0; i<numFoodObj; i++) {
			
			// Eat me!
			if (player.getLoc().equals(food[i].getLoc())) {
				player.eat(food[i]);
			}
			
			// Copy into survived food.
			else {
				survived[j++] = food[i];
			}
		}
				
		// Update all of this.
		numFoodObj = newSize;
		food = survived;
		timeStep++;
	}
	
	// Returns the number of pieces eaten.
	private int numEaten() {
		int numEaten = 0;
		for (int i=0; i<numFoodObj; i++)
			if (player.getLoc().equals(food[i].getLoc()))
				numEaten++;
		return numEaten;
	}
	
	public static void main(String[] args) {
		
		// Get player name.
		Scanner stdin = new Scanner(System.in);
		System.out.println("What is your name?");
		String name = stdin.next();
		
		// Create a game with 10 objects to eat.
		PacmanGame thisGame = new PacmanGame(name, 10);
		
		for (int i=0; i<20; i++) {
			
			// Print the game.
			System.out.println(thisGame);
			
			// Get move and execute it.
			System.out.println("What move?(U,D,L,R,S)");
			char myMove = stdin.next().charAt(0);
			thisGame.move(myMove);
		}
	}
}