from sys import exit
from random import randint
import os

class Engine():

    def __init__(self, class_index):
        self.class_index = class_index

    def game(self):
        this = self.class_index.intro()
        outro = self.class_index.this_next('end')

        while this != outro:
            upcoming = this.function()
            this = self.class_index.this_next(upcoming)

        this.function()

class Start():

    def function(self):
        print(22*'-' + "\nWelcome to Battleship!\n" + 22*'-' + "\n1. Start game\n2. Instructions\n3. Assign player names")
        menu = input('> ')
        if menu == '1':
            return 'preparations'
        elif menu == '2':
            return 'instructions'
        elif menu == '3':
            return 'player_names'
        else:
            print("\nInvalid input.\n")
            return 'start'

class Instructions():

    def function(self):
        print("\nINSTRUCTIONS\n\nYou and your opponent will both get to use a board of a 100 tiles total.\nYou'll each assign 5 different kinds of ships to your boards. One of 5 tiles, one of 4, two of 3, and one of 2.\nYou do this by entering the coordinates of your chosen tile, and then you specify which direction it will face in.\nIn the beginning the boards will just look like squares, but as the game progresses the board will change:\na = Aircraft Carrier\nb = Battleship\nc = Cruiser\nd = Destroyer\ns = Submarine\nx = Where the enemy has struck you.\no = Where the enemy's attack landed in the water.\n\nReturning to START.\n")
        return 'start'

class PlayerNames():

    def function(self):
        global p1, p2
        print("\nPLAYER NAMES\n")
        p1 = input("Player 1's name?\n> ")
        print(f"\nPlayer 1's name is now {p1}.")
        p2 = input("\nPlayer 2's name?\n> ")
        print(f"\nPlayer 2's name is now {p2}.\n")
        print("Returning to START.\n")
        return 'start'

class Preparations():

    def function(self):
        print("\nPREPARATIONS\n\nYou have five types of ships to assign:\n* Aircraft Carrier (5)\n* Battleship (4)\n* Cruiser (3)\n* Destroyer (2)\n* Submarine (3)\n")
        for x in range(0,10):
            print(10 *"* " + "\n", end='') #print ■ ?
        return 'game'

class Game():

    def function(self):
        print(p1, p2)
        return 'end'

class End():

    def function(self):
        input()
        return 'end'

class Index():

    classes = {
    'start': Start(),
    'instructions': Instructions(),
    'player_names': PlayerNames(),
    'preparations': Preparations(),
    'game': Game(),
    'end': End(),
    }

    def __init__(self, first):
        self.first = first

    def this_next(self, name):
        tohere = Index.classes.get(name)
        return tohere

    def intro(self):
        return self.this_next(self.first)

index = Index('start')
this_game = Engine(index)
this_game.game()
