// Student: C. H.
// NimGame Project
// Computer Science 1

// Implementation of a Nim game variant. Select coins to remove (at least 1, but
// no more than 5) from one of the two rows, or the same number from both rows.
//
// Tap a coin on a row to remove it and all the coins to the right (so tap no more
// than 5 coins from the right end).
//
// Double-tap wil remove the same number of coins from both rows.
//
// The winner is the player who leaves the bard empty.

package edu.compsci;

import cs1.android.*;

import java.util.Random;

public class NimGameApp extends AndroidApp
{
	final int MAX_COINS=20;
	final int MIN_COINS=10;
	final int MAX_MOVE=5; 

	//draw a row of n touching circles each of the given radius
	//the left-most circle is centered at the given coordinators (x,y)
	void drawCirclesRow(double x,double y,double radius,int n)
	{
		double curX=x;
		double curY=y;
		int count=0;
		int value=1;

		while(count<n)
		{
			canvas.drawCircle(curX,curY,radius, "tomato");
			canvas.drawText(curX,curY,value,"white");
			curX=curX+2*radius;
			count=count+1;
			value=value+1;
		}

	}

	//draw two horizontal rows of touching circles
	void drawGameBoard(int topCoins,int botCoins,double radius)
	{
		canvas.clear();
		int curY=canvas.getHeight()/2;
		drawCirclesRow(radius,curY,radius,topCoins);
		drawCirclesRow(radius,curY+2*radius,radius,botCoins);

	}

	int computeRowSelection(int x, int y, double radius)
	{
		double y1=canvas.getHeight()/2;
		double y2=canvas.getHeight()/2 + 2*radius;

		if (Math.abs(y - y1) < radius) {
			return 1;
		}
		else if (Math.abs(y - y2) < radius) {
			return 2;
		}
		else {
			return -1;
		}
	}

	//display the game board at each turn and start the game 
	//with the most updated numbers of coins from both rows
	void playNimGame()
	{
		// Scanner input=new Scanner(System.in);

		int middleX=canvas.getWidth()/2;
		int middleY=canvas.getHeight()/2;

		int topCoinsNumber=randomInteger(MIN_COINS,MAX_COINS);
		int botCoinsNumber=randomInteger(MIN_COINS,MAX_COINS);
		double  radius=calculateCoinRadius(topCoinsNumber,botCoinsNumber);
		int turnsCounter=1;

		drawGameBoard(topCoinsNumber,botCoinsNumber,radius);

		while(topCoinsNumber!=0 || botCoinsNumber!=0)
		{
			if(turnsCounter%2==0)
			{
				canvas.drawText(middleX,middleY/2,"BLUE player's move");
			}
			else
			{
				canvas.drawText(middleX,middleY/2,"RED player's move");
			}

			/*
			 * Old code for desktop app
			 *
			 * System.out.println("How many coins to remove?");
			 * int coinsRemoved=input.nextInt();

			 * System.out.println("Which row to remove from?");
			 * System.out.println("(1.2.3)");
			 * System.out.println("1 for first row;2 for second row;3 for both rows");
			 * int whichRow=input.nextInt();

			 * if (whichRow == 1) {
			 *    topCoins = topCoins - coinsRemoved;
			 * }
			 * else if (whichRow == 2) {
			 *    botCoins = botCoins - coinsRemoved;
			 * }
			 * else if (whichRow == 3) {
			 *    topCoins = topCoins - coinsRemoved;
			 *    botCoins = botCoins - coinsRemoved;
			 * }
			 *
			 */

			/*
			 * New code for Android App + new method computeRowSelection(x, y, radius);
			 */
			Touch touch = canvas.waitForTouch();
			int x = touch.getX();
			int y = touch.getY();
			int taps = touch.getTaps();
			int coinTapped = (int)(x / (2*radius)) + 1;
			int whichRow = computeRowSelection(x, y, radius);

			if( whichRow==1 )
			{
				int coinsRemoved = topCoinsNumber - coinTapped + 1;
				topCoinsNumber=topCoinsNumber-coinsRemoved;
				if (taps == 2) {
					botCoinsNumber=botCoinsNumber-coinsRemoved;
				}
			}
			else if( whichRow==2 )
			{
				int coinsRemoved = botCoinsNumber - coinTapped + 1;
				botCoinsNumber=botCoinsNumber-coinsRemoved;
				if (taps == 2) {
					topCoinsNumber=topCoinsNumber-coinsRemoved;
				}
			}
			/*
			 * End New code for Android App
			 */


			drawGameBoard(topCoinsNumber,botCoinsNumber,radius);

			turnsCounter=turnsCounter+1;
		}

		if(turnsCounter%2!=0)
		{
			canvas.drawText(middleX,middleY/2,"Congratulations!!Blue player won!!");
		}
		else
		{
			canvas.drawText(middleX,middleY/2,"Congratulations!!Red player won!");
		}
	}

	//to generate the numbers of coins per row randomly
	int randomInteger(int low,int high)
	{
		Random random=new Random();
		int result=random.nextInt(high-low+1)+low;
		return result;
	}

	//to calculate the radii of the coins based on the number of coins in each row
	double calculateCoinRadius(int topCoins,int botCoins)
	{
		int width=canvas.getWidth();

		if(topCoins>=botCoins)
		{
			double radius=width/topCoins/2;
			return radius;
		}
		else
		{
			double radius=width/botCoins/2;
			return radius;
		}
	}


	public void run()
	{
		//test cases for drawCirclesRow
		//drawCirclesRow(20,40,20,5);
		//drawCirclesRow(50,40,20,8);

		//testcases for drawGameBoard
		//drawGameBoard(10,15,20);
		//drawGameBoard(5,8,40);

		//test cases for playNimGame
		playNimGame();

		//test cases for randomInteger
		//randomInteger(20,30);

		//tast cases for calculateCoinRadius
		//calculateCoinRadius(14,17);
	}
}
