# Skeleton Program for Advaspire Mock Examination 1
# International GCSE Computer Science (9210) Paper 1 Programming
# Developed using Python 3
# To be released to candidates before the examination

import random

GRID_SIZE = 8
NUMBER_OF_TREASURES = 5
NUMBER_OF_ROCKS = 3
MAX_DIGS = 20
POINTS_PER_TREASURE = 10
SAND = "~"
HOLE = "o"
TREASURE = "T"
FOUND = "$"
ROCK = "#"
PRACTICE_ISLAND = ["~~~~~~~~",
                   "~T~~~~#~",
                   "~~~~~~~~",
                   "~~#~T~~~",
                   "~~~~~~~~",
                   "T~~~~~~~",
                   "~~~~~#~T",
                   "~~~T~~~~"]

def CreateIsland():
    island = []
    for row in range(GRID_SIZE):
        newRow = []
        for column in range(GRID_SIZE):
            newRow.append(SAND)
        island.append(newRow)
    return island

def HideItems(island, item, numberOfItems):
    itemsHidden = 0
    while itemsHidden < numberOfItems:
        row = random.randint(0, GRID_SIZE - 1)
        column = random.randint(0, GRID_SIZE - 1)
        if island[row][column] == SAND:
            island[row][column] = item
            itemsHidden = itemsHidden + 1

def CreateRandomIsland():
    island = CreateIsland()
    HideItems(island, ROCK, NUMBER_OF_ROCKS)
    HideItems(island, TREASURE, NUMBER_OF_TREASURES)
    return island

def LoadPracticeIsland():
    island = []
    for row in range(GRID_SIZE):
        line = PRACTICE_ISLAND[row]
        newRow = []
        for column in range(GRID_SIZE):
            newRow.append(line[column])
        island.append(newRow)
    return island

def MakeGridReference(row, column):
    reference = chr(ord("A") + column) + str(row + 1)
    return reference

def DisplayIsland(island):
    print()
    header = "   "
    for column in range(GRID_SIZE):
        header = header + " " + chr(ord("A") + column)
    print(header)
    for row in range(GRID_SIZE):
        line = str(row + 1) + "  "
        for column in range(GRID_SIZE):
            symbol = island[row][column]
            if symbol == TREASURE:
                symbol = SAND
            line = line + " " + symbol
        print(line)
    print()

def ValidateGridReference(reference):
    if len(reference) != 2:
        return False
    lastColumnLetter = chr(ord("A") + GRID_SIZE - 1)
    if reference[0] < "A" or reference[0] > lastColumnLetter:
        return False
    if not reference[1].isdigit():
        return False
    rowNumber = int(reference[1])
    if rowNumber < 1 or rowNumber > GRID_SIZE:
        return False
    return True

def GetGridReference():
    reference = input("Enter a grid reference to dig (for example C5): ").upper()
    while not ValidateGridReference(reference):
        print("That is not a valid grid reference")
        reference = input("Enter a grid reference to dig (for example C5): ").upper()
    return reference

def ConvertGridReference(reference):
    column = ord(reference[0]) - ord("A")
    row = int(reference[1:]) - 1
    return row, column

def CountNearbyTreasure(island, row, column):
    count = 0
    for checkRow in range(row - 1, row + 2):
        for checkColumn in range(column - 1, column + 2):
            if checkRow >= 0 and checkRow < GRID_SIZE and checkColumn >= 0 and checkColumn < GRID_SIZE:
                if island[checkRow][checkColumn] == TREASURE:
                    count = count + 1
    return count

def Dig(island, row, column):
    square = island[row][column]
    treasureFound = False
    if square == TREASURE:
        island[row][column] = FOUND
        print("You found treasure!")
        treasureFound = True
    elif square == SAND:
        island[row][column] = HOLE
        print("Nothing here. Treasure nearby: " + str(CountNearbyTreasure(island, row, column)))
    elif square == ROCK:
        print("You hit a rock")
    else:
        print("You have already dug there")
    return treasureFound

def ShowResult(treasureFound, digsLeft):
    print()
    if treasureFound == NUMBER_OF_TREASURES:
        print("Well done - you found all the treasure!")
    else:
        print("Out of digs!")
    score = treasureFound * POINTS_PER_TREASURE
    print("Treasure found: " + str(treasureFound) + " of " + str(NUMBER_OF_TREASURES))
    print("Score: " + str(score))

def PlayGame(island):
    digsLeft = MAX_DIGS
    treasureFound = 0
    while digsLeft > 0 and treasureFound < NUMBER_OF_TREASURES:
        DisplayIsland(island)
        print("Digs left: " + str(digsLeft))
        reference = GetGridReference()
        row, column = ConvertGridReference(reference)
        if Dig(island, row, column):
            treasureFound = treasureFound + 1
        digsLeft = digsLeft - 1
    DisplayIsland(island)
    ShowResult(treasureFound, digsLeft)

def DisplayMenu():
    print("TREASURE ISLAND")
    print("1. Play a new game")
    print("2. Play the practice island")
    print("Q. Quit")

def Main():
    choice = ""
    while choice != "Q":
        DisplayMenu()
        choice = input("Enter your choice: ").upper()
        if choice == "1":
            island = CreateRandomIsland()
            PlayGame(island)
        elif choice == "2":
            island = LoadPracticeIsland()
            PlayGame(island)
        elif choice == "Q":
            print("Thanks for playing Treasure Island")
        print()

if __name__ == "__main__":
    Main()
