import random
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, LSTM

# Define the RNN model
model = Sequential()
model.add(LSTM(64, input_shape=(1, 1)))
model.add(Dense(1, activation='linear'))
model.compile(optimizer='adam', loss='mean_squared_error')

balance = 100
bet = 1

sim_numbers = []

while True:
    cup = random.randint(1, 3)

    game = input("Choice ('simulate x' or 'play cups'): ")

    if game.startswith("simulate"):
        input_list = game.split(" ")
        num = int(input_list[1])
        if num >= 100000:
             print("Please wait a few minutes, numbers above 100.000 take longer for the model to simulate...")
        sim_numbers += [random.randint(1, 3) for _ in range(num)]
        print(sim_numbers)
        sim_numbers_arr = np.array(sim_numbers)
        sim_numbers_arr = sim_numbers_arr.reshape(sim_numbers_arr.shape[0], 1, 1)
        model.fit(sim_numbers_arr[:-1], sim_numbers_arr[1:], epochs=10, verbose=0)
        predicted_number = model.predict(sim_numbers_arr[-1].reshape(1, 1, 1))
        print(f'Predicted next number: {predicted_number[0][0]:.0f}')

    elif game == "play cups" or game == "play":
            choice = input("What cup do you want to choose? 1, 2 or 3: ")
            bet_choice = input(f'How much do you want to bet on cup {choice}?: ')
            bet = bet_choice

            if int(choice) == cup:
                balance += int(bet)
                print(f'You won ${bet}! Total balance is now ${balance}!')
            elif int(choice) != cup:
                balance -= int(bet)
                print(f'You lost ${bet}. Total balance is now ${balance}. Correct cup was cup {cup}.')
            else:
                print("Please input a valid number")
                continue
    else:
        print("Please input a valid choice...")
