import pygame
import math
import random


print "This program performs Monte Carlo simulations.\n"

print "AIM: To calculate value of Pi\n"

simulations = int(raw_input("Enter no of simulations to be performed:\n"))

print "Generating ", str(simulations), "random coordinate points within (1,1)....\n"

n = simulations
insideCircle = 0

gen_pi = 0

pygame.init()

screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Monte Carlo")

white = (255, 255, 255)
black = (0, 0, 0)
blue  = (0, 0, 255)
red   = (255, 0, 0)
green  = (0, 255, 0)

clock = pygame.time.Clock()
done = False

screen.fill(white)
while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gen_pi = 4.0 * (float(insideCircle)/float(simulations))
            print "The value of pi generated by Monte Carlo simulations:\n"
            print "Pi = ", str(gen_pi) 
            done = True
    
    pygame.draw.circle(screen, blue, (320, 240), 100, 2)
    pygame.draw.polygon(screen, black, [(320, 240), (420, 240), (420, 140), (320, 140)], 2)

    if(n != 0):
        x = float(random.randint(1, 101))
        y = float(random.randint(1, 101))

        if ((math.pow(x, 2) + math.pow(y, 2)) <= 10000.0):
            pygame.draw.circle(screen, green, (320+int(x), 240-int(y)), 1,0)
            insideCircle = insideCircle + 1
        else:
            pygame.draw.circle(screen, red, (320+int(x), 240-int(y)), 1,0)

        n = n - 1

    pygame.display.update()
    clock.tick(100)