#!/usr/bin/env python

import numpy as np

class board():
    def __init__(self):
        self.init_board = np.zeros([6,7]).astype(str)
        self.init_board[self.init_board == "0.0"] = " "
        self.player = 0
        self.current_board = self.init_board
        
    def drop_piece(self, column):
        if self.current_board[0, column] != " ":
            return "Invalid move"
        row = 0
        pos = " "
        while (pos == " "):
            if row == 6:
                row += 1
                break
            pos = self.current_board[row, column]
            row += 1
        if self.player == 0:
            self.current_board[row-2, column] = "O"
            self.player = 1
        elif self.player == 1:
            self.current_board[row-2, column] = "X"
            self.player = 0
    
    def check_winner(self):
        for row in range(6):
            for col in range(7):
                if self.player == 0:
                    if self.current_board[row, col] != " ":
                        # rows
                        try:
                            if self.current_board[row, col] == "X" and self.current_board[row + 1, col] == "X" and \
                                self.current_board[row + 2, col] == "X" and self.current_board[row + 3, col] == "X":
                                return True
                        except IndexError:
                            next
                        # columns
                        try:
                            if self.current_board[row, col] == "X" and self.current_board[row, col + 1] == "X" and \
                                self.current_board[row, col + 2] == "X" and self.current_board[row, col + 3] == "X":
                                return True
                        except IndexError:
                            next
                        # \ diagonal
                        try:
                            if self.current_board[row, col] == "X" and self.current_board[row + 1, col + 1] == "X" and \
                                self.current_board[row + 2, col + 2] == "X" and self.current_board[row + 3, col + 3] == "X":
                                return True
                        except IndexError:
                            next
                        # / diagonal
                        try:
                            if (
                                self.current_board[row, col] == "X"
                                and self.current_board[row + 1, col - 1] == "X"
                                and self.current_board[row + 2, col - 2] == "X"
                                and self.current_board[row + 3, col - 3] == "X"
                                and col >= 3
                            ):
                                return True
                        except IndexError:
                            next

                elif self.player == 1:
                    if self.current_board[row, col] != " ":
                        # rows
                        try:
                            if self.current_board[row, col] == "O" and self.current_board[row + 1, col] == "O" and \
                                self.current_board[row + 2, col] == "O" and self.current_board[row + 3, col] == "O":
                                #print("row")
                                return True
                        except IndexError:
                            next
                        # columns
                        try:
                            if self.current_board[row, col] == "O" and self.current_board[row, col + 1] == "O" and \
                                self.current_board[row, col + 2] == "O" and self.current_board[row, col + 3] == "O":
                                #print("col")
                                return True
                        except IndexError:
                            next
                        # \ diagonal
                        try:
                            if self.current_board[row, col] == "O" and self.current_board[row + 1, col + 1] == "O" and \
                                self.current_board[row + 2, col + 2] == "O" and self.current_board[row + 3, col + 3] == "O":
                                #print("\\")
                                return True
                        except IndexError:
                            next
                        # / diagonal
                        try:
                            if (
                                self.current_board[row, col] == "O"
                                and self.current_board[row + 1, col - 1] == "O"
                                and self.current_board[row + 2, col - 2] == "O"
                                and self.current_board[row + 3, col - 3] == "O"
                                and col >= 3
                            ):
                                #print("/")
                                return True
                        except IndexError:
                            next
    def actions(self): # returns all possible moves
        return [col for col in range(7) if self.current_board[0, col] == " "]
            