def __init__(self, master):
     self.highscores = Highscores()
     self.master    = master
     frame          = Frame(master)
     frame.grid(row = 1, column = 2)
     self.frame     = frame
     self.btn_singleplayer  = Button(frame, text ="[SINGLEPLAYER]", command=self.singleplayer)
     self.btn_singleplayer.grid(row = 1, column  = 1)
     self.btn_multiplayer   = Button(frame, text ="[MULTIPLAYER]", command=self.multiplayer)
     self.btn_multiplayer.grid(row = 1, column   = 2)
     self.btn_exit_game     = Button(frame, text ="[Exit Game]", command=self.quit)
     self.btn_exit_game.grid(row = 1, column = 10)
     highscoretext = self.highscores.get_scores()
     self.lbl_high_score = Label(master,text = highscoretext)
     self.lbl_high_score.grid(row = 1, column = 11)
     self.current_player = Label(master, text    = "", fg = "green")
     self.current_player.grid(row = 2, column    = 1)
     self.info      = Label(master, text = "", fg = "red")
     self.info.grid(row = 2, column = 2)
     self.matrix1   = None
     self.matrix2   = None
     self.player_one_name = None
     self.player_two_name = None
     self.mode = 10  #  val av spelmode
     self.current_ship_vertical = False
     self.current_ship_extent = 1
     return
class Gameboard:
    def __init__(self, master):
        self.highscores = Highscores()
        self.master    = master
        frame          = Frame(master)
        frame.grid(row = 1, column = 2)
        self.frame     = frame
        self.btn_singleplayer  = Button(frame, text ="[SINGLEPLAYER]", command=self.singleplayer)
        self.btn_singleplayer.grid(row = 1, column  = 1)
        self.btn_multiplayer   = Button(frame, text ="[MULTIPLAYER]", command=self.multiplayer)
        self.btn_multiplayer.grid(row = 1, column   = 2)
        self.btn_exit_game     = Button(frame, text ="[Exit Game]", command=self.quit)
        self.btn_exit_game.grid(row = 1, column = 10)
        highscoretext = self.highscores.get_scores()
        self.lbl_high_score = Label(master,text = highscoretext)
        self.lbl_high_score.grid(row = 1, column = 11)
        self.current_player = Label(master, text    = "", fg = "green")
        self.current_player.grid(row = 2, column    = 1)
        self.info      = Label(master, text = "", fg = "red")
        self.info.grid(row = 2, column = 2)
        self.matrix1   = None
        self.matrix2   = None
        self.player_one_name = None
        self.player_two_name = None
        self.mode = 10  #  val av spelmode
        self.current_ship_vertical = False
        self.current_ship_extent = 1
        return

    def current_turn(self):
        if self.current == 1:
            self.current_player["text"] = "Player " + self.player_one_name + "s Turn"
        else:
            self.current_player["text"] = "Player " + self.player_two_name + "s Turn"
        return

    def whos_turn_is_it(self):
        return self.current

    def change_player(self):
        if self.current == 1:
            self.current = 2
        else:
            self.current = 1
        self.current_turn()
        self.clear_info()
        return

    def quit(self):
        root.destroy()

    def set_info(self,text):
        self.info["text"] = text

    def clear_info(self):
        self.info["text"] = ""
        return

    def win(self,matrix):
        if matrix == self.matrix2:
            winning_matrix = self.matrix1
            losing_matrix  = self.matrix2
        else:
            winning_matrix = self.matrix2
            losing_matrix  = self.matrix1
        score = losing_matrix.calculate_score()
        self.info["text"] = "Congratulations " + winning_matrix.name + " Won with score " + str(score)
        self.highscores.add_score(winning_matrix.name,score)
        highscoretext = self.highscores.get_scores()
        self.lbl_high_score["text"] = highscoretext
        self.mode = 40
        return

    def show_hide_ship_player1(self):
         self.matrix1.show_hide_ships()
         return

    def show_hide_ship_player2(self):
        self.matrix2.show_hide_ships()
        return

    def computer_move(self):
        valid_move = False
        while not valid_move:
            x = self.get_random_coordinate(10)
            y = self.get_random_coordinate(10)
            valid_move = self.matrix2.try_move(x,y)
        return

    def get_random_coordinate(self, x):
        return random.randint(1, x)

    def create_5_ship(self):
        self.current_ship_extent = 5
        self.set_ship_button_highlight()
        return

    def create_4_ship(self):
        self.current_ship_extent = 4
        self.set_ship_button_highlight()
        return

    def create_3_ship(self):
        self.current_ship_extent = 3
        self.set_ship_button_highlight()
        return

    def create_2_ship(self):
        self.current_ship_extent = 2
        self.set_ship_button_highlight()
        return

    def create_1_ship(self):
        self.current_ship_extent = 1
        self.set_ship_button_highlight()
        return

    def create_vertical_on_off(self):
        if self.current_ship_vertical:
            self.current_ship_vertical = False
            self.btn_vertical_on_off["fg"] = "black"
        else:
            self.current_ship_vertical = True
            self.btn_vertical_on_off["fg"] = "red"
        return

    def set_ship_button_highlight(self):
        self.btn_1_ship["state"] = NORMAL
        self.btn_2_ship["state"] = NORMAL
        self.btn_3_ship["fg"] = "black"
        self.btn_4_ship["fg"] = "black"
        self.btn_5_ship["fg"] = "black"
        if self.current_ship_extent == 1:
            self.btn_1_ship.flash()
        elif self.current_ship_extent == 2:
            self.btn_2_ship.flash()
        elif self.current_ship_extent == 3:
            self.btn_3_ship["fg"] = "red"
        elif self.current_ship_extent == 4:
            self.btn_4_ship["fg"] = "red"
        elif self.current_ship_extent == 5:
            self.btn_5_ship["fg"] = "red"
        return

    def start_game(self):

        if self.singleplayer_mode:
            self.matrix1.auto_place_ships(self.matrix2)
            self.matrix2.hide_ships()

        else:
            self.matrix1.hide_ships()
            self.matrix2.hide_ships()

        if self.can_we_start():
            self.btn_1_ship.grid_forget()
            self.btn_2_ship.grid_forget()
            self.btn_3_ship.grid_forget()
            self.btn_4_ship.grid_forget()
            self.btn_5_ship.grid_forget()
            self.btn_vertical_on_off.grid_forget()
            self.btn_start_game.grid_forget()
            self.btn_reset_ships.grid_forget()
            # destroy other shipplacer mode = 20 buttons
            self.mode = 30
            self.current   = 1
            self.set_info("")
        else:
            self.info["text"] = "Not equal amount of ships between players"

        return

    def can_we_start(self):
        result = True
        if (self.matrix1.nr_of_ships == 0):
            result = False
        if self.matrix1.nr_of_1_ships != self.matrix2.nr_of_1_ships:
            result = False
        elif self.matrix1.nr_of_2_ships != self.matrix2.nr_of_2_ships:
            result = False
        elif self.matrix1.nr_of_3_ships != self.matrix2.nr_of_3_ships:
            result = False
        elif self.matrix1.nr_of_4_ships != self.matrix2.nr_of_4_ships:
            result = False
        elif self.matrix1.nr_of_5_ships != self.matrix2.nr_of_5_ships:
            result = False
        return result

    def singleplayer(self):
        self.singleplayer_mode = True
        dialog = MyDialog(self.frame, "Ange ditt namn", "Namn")
        self.frame.wait_window(dialog.top)
        self.player_one_name = "CPU"
        self.player_two_name = str(dialog.value)
        self.matrix2 = Matrix(self.master, self, 2, False, self.player_two_name)
        self.matrix1 = Matrix(self.master, self, 1, True, self.player_one_name)
        self.start_placing_ships()
        return

    def multiplayer(self):
        self.singleplayer_mode = False
        dialog = MyDialog(self.frame, "Ange första spelarns namn", "Namn")
        self.frame.wait_window(dialog.top)
        self.player_one_name = dialog.value
        dialog2 = MyDialog(self.frame, "Ange andra spelarns namn", "Namn")
        self.frame.wait_window(dialog2.top)
        self.player_two_name = dialog2.value
        self.matrix2 = Matrix(self.master, self, 1, False, self.player_two_name)
        self.matrix1 = Matrix(self.master, self, 2, False, self.player_one_name)
        self.start_placing_ships()
        return


    def reset_ships(self):
        self.matrix1.delete_ships()
        self.matrix2.delete_ships()
        self.current = 1

        return

    def start_placing_ships(self):
        self.btn_reset_ships = Button(self.frame, text = "[Reset Ships]", command = self.reset_ships)
        self.btn_reset_ships.grid(row = 10, column = 10)
        self.btn_5_ship = Button(self.frame, text = "Creat Carrier(5)", command = self.create_5_ship)
        self.btn_5_ship.grid(row = 2, column = 1)
        self.btn_4_ship = Button(self.frame, text = "Creat Battleship(4)", command = self.create_4_ship)
        self.btn_4_ship.grid(row = 3, column = 1)
        self.btn_3_ship = Button(self.frame, text = "Creat Submarine(3)", command = self.create_3_ship)
        self.btn_3_ship.grid(row = 4, column = 1)
        self.btn_2_ship = Button(self.frame, text = "Creat Cruiser(2)", command   = self.create_2_ship)
        self.btn_2_ship.grid(row = 5, column = 1)
        self.btn_1_ship = Button(self.frame, text = "Creat Destroyer(1)", command = self.create_1_ship)
        self.btn_1_ship.grid(row = 6, column = 1)
        self.btn_vertical_on_off = Button(self.frame, text = "Vertical On", command = self.create_vertical_on_off)
        self.btn_vertical_on_off.grid(row = 7, column = 1)
        self.btn_start_game = Button(self.frame, text ="Start Game", command = self.start_game)
        self.btn_start_game.grid(row = 5, column = 5)
        self.btn_singleplayer.grid_forget()
        self.btn_multiplayer.grid_forget()
        self.matrix1.show_hide_ships()
        self.matrix2.show_hide_ships()


        self.btn_show_player2_ships = Button(self.frame, text ="[Show/Hide Player2 Ships]", command = self.show_hide_ship_player1)
        self.btn_show_player2_ships.grid(row = 2, column = 8)
        self.btn_show_player1_ships = Button(self.frame, text ="[Show/Hide Player1 Ships]", command = self.show_hide_ship_player2)
        self.btn_show_player1_ships.grid(row = 2, column = 9)
        self.btn_show_player2_ships["text"] = "Show/Hide " + self.player_one_name +"s" + " Ships"
        self.btn_show_player1_ships["text"] = "Show/Hide " + self.player_two_name +"s Ships"
        self.set_info("Player "+ self.player_two_name +"s turn to place ships")
        self.mode = 20
        return