import matplotlib.pyplot as plt
%matplotlib notebook
plt.figure(1)

values = []
for i in xrange(0,100000):
    values.append(Card().absolute_value)
    # values.append(random.randint(1,10))

plt.title('Test; Each draw from the deck results in a value between 1 and 10 (uniformly distributed)')
plt.hist(values)
  
         # , c='g', s=20, alpha=0.25, label='true positive')
plt.show()
plt.savefig("#1-test1.png")

# from IPython.display import Image
# Image(url="test.png")

"""
Explanation: Test; Each draw from the deck results in a value between 1 and 10 (uniformly distributed)
End of explanation
"""


import matplotlib.pyplot as plt
%matplotlib notebook
plt.figure(2)

values = []
for i in xrange(0,100000):
    if (Card().is_black):
        values.append(0.6666666)
    else:
        values.append(0.3333333)

plt.title('Test; red (probability 1/3) or black (probability 2/3)')
plt.hist(values)
  
         # , c='g', s=20, alpha=0.25, label='true positive')
plt.show()
plt.savefig("#1-test2.png")

"""
Explanation: Each draw from the deck results in a colour of red (probability 1/3) or black (probability 2/3).
End of explanation
"""


def play_test_player_bust():
    s = State(Card(True),Card(True))
    a = Actions.hit
    
    while not s.term:
        s, r = step(s, a)
        # print ("state = %s, %s, %s" % (s.player, s.dealer, s.term))
    return s, r

import matplotlib.pyplot as plt
%matplotlib notebook
plt.figure(3)

values = []
for i in xrange(0,100000):
    s, r = play_test_player_bust()
    if s.player > 21:
        values.append(1)
    elif s.player < 1:
        values.append(1)
    else:
        values.append(-1)
        print "error!!!!"
    

plt.title('Test; player busts > 21 or <1')
plt.hist(values)
  
         # , c='g', s=20, alpha=0.25, label='true positive')
plt.show()
plt.savefig("#1-test3.png")

"""
Explanation: Test: If the player’s sum exceeds 21, or becomes less than 1, then she “goes bust” and loses the game (reward -1)
End of explanation
"""


def play_test_player_stick():
    s = State(Card(True),Card(True))
    a = Actions.hit
    
    a = Actions.stick
    while not s.term:
        s, r = step(s, a)
        # print ("state = %s, %s, %s" % (s.player, s.dealer, s.term))
    return s, r

import matplotlib.pyplot as plt
%matplotlib notebook
plt.figure(4)

values = []
for i in xrange(0,100000):
    s, r = play_test_player_stick()
    if s.dealer > 21 or s.dealer < 1:
        if r == 1:
            values.append(1)
        else:
            print "error, player should have won"
            print ("state = %s, %s, %s. result = %s" % (s.player, s.dealer, s.term, r))
            values.append(-1)
    elif s.player == s.dealer:
        if r == 0:
            values.append(1)
        else:
            print "error, player should have drawn"
            print ("state = %s, %s, %s. result = %s" % (s.player, s.dealer, s.term, r))
            values.append(-2)
    elif s.player > s.dealer:
        if r == 1:
            values.append(1)
        else:
            print "error, player should have won"
            print ("state = %s, %s, %s. result = %s" % (s.player, s.dealer, s.term, r))
            values.append(-3)
    elif s.player < s.dealer:
        if r == -1:
            values.append(1)
        else:
            print "error, player should have lost"
            print ("state = %s, %s, %s. result = %s" % (s.player, s.dealer, s.term, r))
            values.append(-4)
    else:
        print "all cases should have been dealt with"
        print ("state = %s, %s, %s. result = %s" % (s.player, s.dealer, s.term, r))
        values.append(-5)

    

plt.title('Test; player sticks')
plt.hist(values)
plt.show()
plt.savefig("#1-test4.png")

"""
Explanation: Test: If the player sticks then the dealer starts taking turns. The dealer always sticks on any sum of 17 or greater, and hits otherwise. If the dealer goes bust, then the player wins; otherwise, the outcome – win (reward +1), lose (reward -1), or draw (reward 0) – is the player with the largest sum.
End of explanation
"""


