import random
import math
import numpy as np


def mynumbers():
    final_number = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    first_number = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    second_number = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    others = []

    for i in range(0, 10):
        #Target number:
        #A random number in the range 51-99, not including those that end with 0
        candidate_final_number = 0
        while candidate_final_number % 10 == 0:
            candidate_final_number = random.sample(range(51, 99), 1)[0]
        final_number[i] = candidate_final_number

        #First number:
        #A random number that is bigger or equal to 9, not end with 0, last digit different from target number
        candidate_first_number = 0
        while candidate_first_number % 10 == 0 or candidate_first_number % 10 == final_number[i] % 10:
            #print((candidate_first_number % 10 == 0), (candidate_first_number % 10 == final_number[i] % 10))
            candidate_first_number = random.sample(range(9, final_number[i] - 9), 1)[0]
        first_number[i] = candidate_first_number

        #Second number:
        second_number[i] = final_number[i] - first_number[i]

        #Other numbers:
        #Random numbers that are bigger or equal to 9, not end with 0, any two of other numbers do not add up to target, no duplicates:
        others.append(np.ones(7, dtype=int))
        toFill = 7
        while toFill > 0:
            candidate = random.sample(range(9, final_number[i] - 9), 1)[0]
            if min(abs(candidate + others[i] - final_number[i])) > 0 and candidate != first_number[i] \
                    and candidate != second_number[i] and candidate % 10 != 0 and min(abs(candidate - others[i])) != 0:
                print(min(abs(candidate - others[i])))
                toFill = toFill-1
                others[i][toFill-7] = candidate
    return np.array([final_number, first_number, second_number, others])


x = mynumbers()

print(x)


#Check if there are two equal numbers:
temp = x
addsUppp = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

for h in range(0, 10):
    full_board = np.array([temp[1][h], temp[2][h], temp[3][h][0], temp[3][h][1], temp[3][h][2], temp[3][h][3],
                           temp[3][h][4], temp[3][h][5], temp[3][h][6]])
    final = temp[0][h]


    for k in range(0, 8):
        for l in range(min(k+1, 8), 9):
            if full_board[k] == full_board[l]:
                addsUppp[h] = addsUppp[h] + 1

print(addsUppp)



#Check if there's only one solution:
temp = x
addsUp = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

for h in range(0, 10):
    full_board = np.array([temp[1][h], temp[2][h], temp[3][h][0], temp[3][h][1], temp[3][h][2], temp[3][h][3],
                           temp[3][h][4], temp[3][h][5], temp[3][h][6]])
    final = temp[0][h]
    comboCount = 0

    for k in range(0, 8):
        for l in range(min(k+1, 8), 9):
            comboCount = comboCount + 1
            if full_board[k]+full_board[l] == final:
                addsUp[h] = addsUp[h] + 1

print(addsUp)








# Order of numbers in the game:
y = []
for h in range(0, 10):
    y = y+[list(np.append(x[0:3, h], x[:, h][3]))]

y_new = y[:]

for h in range(0, 10):
    final = y[h][0]
    first = y[h][1]
    second = y[h][2]
    others = y[h][3:10]
    place1 = random.randint(0, 7)
    place2 = random.randint(0, 8)
    part1 = others[:place1]
    part2 = others[place1:]
    temp1 = part1 + [first] + part2
    part11 = temp1[:place2]
    part22 = temp1[place2:]
    y1_new = [final] + part11 + [second] + part22
    y_new[h] = y1_new

print(y_new)
