import numpy as np
import pylab as plt
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.visualization import plot_histogram, plot_bloch_multivector

damage = 0  # damage to the enemy
def attack(damage):
    damage = min ( (damage +1/3.), 1 )
    return damage

damage = attack(damage)
print (damage)

%matplotlib inline
def plot_satevector(qc):
    backend_sv = Aer.get_backend('statevector_simulator')
    sv = execute(qc, backend=backend_sv).result().get_statevector()
    fig = plot_bloch_multivector(sv)
    return fig

qcc = QuantumCircuit(1,1)
qcc.x(0)
fig = plot_satevector(qcc)
fig







damage_qc = QuantumCircuit(1,1)

def attack(damage_qc):
    damage_qc.rx(np.pi/3,0)
    fig = plot_satevector(damage_qc)
    fig
    return fig

meas = QuantumCircuit(1,1)

meas.measure(0,0)

fig = attack(damage_qc)
fig

#qc = damage_qc+meas
#counts = execute(qc, Aer.get_backend('qasm_simulator'), shots=1000).result().get_counts()

#print(counts)
#qc.draw('mpl')

def get_damage(damage_qc):
    meas = QuantumCircuit(1,1)
    meas.measure(0,0)
    attack(damage_qc)
    Nshots=1000
    counts = execute(damage_qc+meas, backend=Aer.get_backend('qasm_simulator'), shots=Nshots).result().get_counts()
    
    damage = counts['1']/Nshots
    return damage

damage_qc = QuantumCircuit(1,1)
damage = get_damage(damage_qc)
print(damage)

#damage = attack(damage_qc)
#get_damage(damage)
#damage_qc.draw()

meas = QuantumCircuit(1,1)
meas.measure(0,0)

damage_qc = attack(damage_qc)

qc = damage_qc+meas
    
counts = execute(qc, Aer.get_backend('qasm_simulator'), shots=1000).result().get_counts()

print(counts)

qc.draw()






#some tests

qc = QuantumCircuit(1,1)

backend = Aer.get_backend('qasm_simulator')
counts = execute(qc, backend=Aer.get_backend('qasm_simulator'), shots=1000).result().get_counts()
counts

!git clone https://github.com/quantumjim/jupyter-widget-game-engine/
!mv jupyter-widget-game-engine/jupyter_widget_engine.py jupyter_widget_engine.py
!mv jupyter-widget-game-engine/jupyter_widget_game.ipynb jupyter_widget_game.ipynb
!rm -r jupyter-widget-game-engine


from jupyter_widget_engine import jupyter_widget_engine

L = 8
    
def start(engine):
    pass

def next_frame(engine):
    pass
        
engine = jupyter_widget_engine(start, next_frame, L=L)

def get_terrain(x,y):
    
    
    return 'grass'
    

L = 8
    
def start(engine):
    engine.get_terrain = get_terrain
    engine.next_frame(engine)
    

def next_frame(engine):
    for x in range(L):
        for y in range(L):
            #terrain = engine.get_terrain(x,y)
            if engine.get_terrain(x,y) == 'grass':
                engine.screen[x,y].button_style = 'success'
            
        
engine = jupyter_widget_engine(start, next_frame, L=L)

def get_terrain(x,y):

    qc = QuantumCircuit(1,1) # make a circuit
    
    # perform rotations, whose angles depend on x and y
    qc.rx( (np.pi/16)*(x+y) ,0)
    qc.rx( (np.pi/16)*(x-y) ,0)
    # calculate probability for outcome 1
    qc.measure(0,0)
    counts = execute(qc, Aer.get_backend('qasm_simulator'), shots=1000).result().get_counts()

    if '1' in counts:
        h = counts['1']/1000
    else:
        h = 0
        
    # return terrain depending on this probability
    # the chosen values here are fairly arbitrarily
    if h<0.3:
        terrain = 'sea'
    elif h<0.7:
        terrain = 'sand'
    else:
        terrain = 'grass'
        
    return terrain

L = 8
    
def start(engine):
    engine.get_terrain = get_terrain
    engine.next_frame(engine)
    
def next_frame(engine):
    for x in range(L):
        for y in range(L):
            terrain = engine.get_terrain(x,y)
            if terrain == 'grass':
                engine.screen[x,y].button_style = 'success'
            elif terrain == 'sand':
                engine.screen[x,y].button_style = 'warning'
            else:
                engine.screen[x,y].button_style = 'info'

        
engine = jupyter_widget_engine(start, next_frame, L=L)

L = 8
    
def start(engine):
    engine.get_terrain = get_terrain
    
    engine.p_x = 4
    engine.p_y = 4
    
    engine.next_frame(engine)
    
def next_frame(engine):

        
    if engine.controller['up'].value:
        engine.p_y -= 1
    if engine.controller['down'].value:
        engine.p_y += 1
    if engine.controller['left'].value:
        engine.p_x -= 1
    if engine.controller['right'].value:
        engine.p_x += 1
    
    s_x = np.floor(engine.p_x/L)
    s_y = np.floor(engine.p_y/L)

    for x in range(L):
        for y in range(L):
            terrain = engine.get_terrain(L*s_x+x,L*s_y+y)
            if terrain == 'grass':
                engine.screen[x,y].button_style = 'success'
            elif terrain == 'sand':
                engine.screen[x,y].button_style = 'warning'
            else:
                engine.screen[x,y].button_style = 'info'
                       
    engine.screen[engine.p_x%L, engine.p_y%L].button_style = 'danger'
        
engine = jupyter_widget_engine(start, next_frame, L=L)

