# Put your code here, using additional cells if necessary.

import random
import math

def initialize_list(array_size=32, randseed=8675309):
    '''
    This function optionally takes in an array size and random seed
    and returns the initial neighborhood that we're going to start 
    from - a string of zeros and ones.  If no arguments are given, it
    defaults to the values specified.
    '''

    random.seed(randseed)
    
    initial_list = []

    for i in range(array_size):
        initial_list.append(random.randint(0,1))

    return initial_list

def neighborhood_print(neighborhood, note=''):
    '''
    This is a convenience function to take our neighborhood list,
    make a string of stars and zeros out of it, and print the string
    plus optional text at the end.  It's not necessary but it looks pretty.  
    '''
    
    neighborstring=''

    for i in range(len(neighborhood)):
        if(neighborhood[i]) > 0:
            neighborstring += '*'
        else:
            neighborstring += '0'
    
    # make sure optional text is a string
    if type(note)!=str:
        note = str(note)
    
    # add an extra space to make it look nice!
    if note != '':
        note = ' ' + note
        
    neighborstring += note
    
    print(neighborstring)
   

my_board = initialize_list()

neighborhood_print(my_board)


"""
Explanation: Getting ready to implement the Schelling model
Goal for this assignment
The goal of this assignment is to finish up the two functions that you started in class on the first day of this project, to ensure that you're ready to hit the ground running when you get back to together with your group.  
You are welcome to work with your group on this pre-class assignment - just make sure to list who you worked with below.  Also, everybody needs to turn in their own solutions!
Your name
SOLUTIONS
Function 1:  Creating a game board
Function 1:  Write a function that creates a one-dimensional game board composed of agents of two different types (0 and 1, X and O, stars and pluses... whatever you want), where the agents are assigned to spots randomly with a 50% chance of being either type.  As arguments to the function, take in (1) the number of spots in the game board (setting the default to 32) and (2) a random seed that you will use to initialize the board (again with some default number), and return your game board.  (Hint: which makes more sense to describe the game board, a list or a Numpy array?  What are the tradeoffs?)  Show that your function is behaving correctly by printing out the returned game board.
End of explanation
"""


# Put your code here, using additional cells if necessary.



def is_happy(my_list, my_value, my_index):
    '''
    This function assumes that my_list has a value (my_value)
    popped out of it already, and checkes to see if my_value
    would be happy in my_list at index my_index.  It returns
    'True' if happy and 'False' if unhappy under those circumstances.
    '''

    # do some error-checking (is the index within the allowed range?)
    if my_index < 0 or my_index > len(my_list):
        print("you've made an indexing error!", my_index)
        
    start = my_index-4 # start 4 to the left
    end = my_index+4   # end 3 to the right b/c we count the value at my_index too
    
    # if the starting value is out of bounds, fix it
    if start < 0:
        start = 0
    
    # if the ending value is out of bounds, fix it.  note that we want to go to 
    # len(list), not len(list)-1, because range() goes to 1 before the end of 
    # the range!
    if end > len(my_list):
        end = len(my_list)

    # keep track of the neighbors that are like me
    neighbors_like_me = 0
    
    # keep track of total neighbors
    total_neighbors = 0
    
    # loop over the specified range
    for i in range(start,end):
        if my_list[i] == my_value:  # if this neighbor is like me, keep track of that
            neighbors_like_me += 1
        total_neighbors+=1  # also keep track of total neighbors
    
    # happy if at least half are like me, unhappy otherwise
    # note: it's *at least* half because we're not double-counting our
    # own value
    if neighbors_like_me/total_neighbors >= 0.5:
        return True
    else:
        return False

    

my_board = initialize_list()

neighborhood_print(my_board)

for i in range(len(my_board)):
    agent = my_board.pop(i)
    
    am_i_happy = is_happy(my_board, agent, i)
    
    my_board.insert(i, agent)
    
    if am_i_happy==True:
        print("agent {} at position {} is HAPPY!  :-)".format(agent,i))
    else:
        print("agent {} at position {} is UNHAPPY!  :-(".format(agent,i))

"""
Explanation: Function 2: deciding if an agent is happy
Write a function that takes the game board generated by the function you wrote above and determines whether an agent at position i in the game board of a specified type is happy for a game board of any size and a neighborhood of size N (i.e., from position i-N to i+N), and returns that information.  Make sure to check that position i is actually inside the game board (i.e., make sure the request makes sense), and ensure that it behaves correctly for agents near the edges of the game board.  Show that your function is behaving correctly by giving having it check every position in the game board you generated previously, and decide whether the agent in each spot is happy or not.  Verify by eye that it's behaving correctly.  (Hint: You're going to use this later, when you're trying to decide where to put an agent.  Should you write the function assuming that the agent is already in the board, or that you're testing to see whether or not you've trying to decide whether to put it there?)
End of explanation
"""


from IPython.display import HTML
HTML(
"""
<iframe 
	src="https://goo.gl/forms/M7YCyE1OLzyOK7gH3?embedded=true" 
	width="80%" 
	height="1200px" 
	frameborder="0" 
	marginheight="0" 
	marginwidth="0">
	Loading...
</iframe>
"""
)

"""
Explanation: Assignment wrapup
Please fill out the form that appears when you run the code below.  You must completely fill this out in order to receive credit for the assignment!
End of explanation
"""


