<jupyter_start><jupyter_text>tick tack toe<jupyter_code>import numpy as np
import pickle
# human interface
# input a number to put a chessman
# | q | w | e |
# | a | s | d |
# | z | x | c |
BOARD_ROWS = 3
BOARD_COLS = 3
BOARD_SIZE = BOARD_ROWS * BOARD_COLS
class State:
    def __init__(self):
        # the board is represented by an n * n array,
        # 1 represents a chessman of the player who moves first,
        # -1 represents a chessman of another player
        # 0 represents an empty position
        self.data = np.zeros((BOARD_ROWS, BOARD_COLS))
        self.winner =  None
        self.hash_val = None
        self.end = None
        
        
    # compute the hash value for one state, its unique
    def hash(self):
        if self.hash_value is None:
            self.hash_val = 0
            for i in np.nditer(self.data):
                self.hash_val = self.hash_val * 3 + i + 1
        return self.hash_val
    
    # check whether a player has won he game, or it's  tie
    def is_end(self):
        if self.end is not None:
            return self.end
        results = []
        
        # check row 
        for i in range(BOARD_ROWS):
            results.append(np.sum(self.data[i, :]))
        
        # check for columns
        for i in range(BOARD_COLS):
            results.append(np.sum(self.data[:, i]))
            
        # check diagonal
        trace = 0
        reverse_trace = 0
        for i in range(BOARD_ROWS):
            trace += self.data[i,i]
            reverse_trace += self.data[i, BOARD_ROWS - 1 - i]
        results.append(trace)
        results.append(reverse_trace)
        
        for result in results:
            if result == 3:
                self.winner = 1
                self.end = True
                return self.end
            if result == -3:
                self.winner = -1
                self.end = True
                return self.end
            
        # wheter it's a tie
        sum_values = np.sum(np.abs(self.data))
        if sum_values == BOARD_SIZE:
            self.winner = 0
            self.end = True
            return self.end
        
        # game is still going
        self.end = False
        return self.end
        
        # @symbol: 1 or -1
        # put chessman symbol in position (i,j)
        def next_state(self, i, j, symbol):
            new_state = State()
            new_state.data = np.copy(self.data)
            new_state.data[i,j] = symbol
            return new_state
        
        # print the board
        def print_state(self):
            for i in range(BOARD_ROWS):
                print('--------------')
                out = '|'
                for j in range(BOARD_COLS):
                    if self.data[i,j] == 1:
                        token = '*'
                    elif self.data[i,j] == -1:
                        token = 'x'
                    else:
                        token = '0'
                    out += token + '|'
                print(out)
            print('--------------')
            
def get_all_states_impl(current_state, current_symbol, all_states):
    for i in range(BOARD_ROWS):
        for j in range(BOARD_COLS):
            if current_state.data[i][j] == 0:
                new_state = current_state.next_state(i, j, current_symbol)
                new_hash = new_state.hash()
                if new_hash not in all_states:
                    is_end = new_state.is_end()
                    all_states[new_hash] = (new_state, is_end)
                    if not is_end:
                        get_all_states_impl(new_state, -current_symbol, all_states)

def get_all_states():
    current_symbol = 1
    current_state = State()
    all_state = dict()
    all_states[current_state.hash()] = (current_state, current_state.is_end())
    get_all_states_impl(current_state, current_symbol, all_states)
    return all_states

# all possible board configuration
all_states = get_all_states()


class Judge:
    # @player1: the  player who will move first, its chessman will be 1
    # @player2: another player with aa chessman -1
    def __init__(self, player1, player2):
        self.p1 = player1
        self.p2 = player2
        self.current_player = None
        self.p1_symbol = 1
        self.p2_symbol = -1
        
import numpy as np
x = np.random.normal(size=12)
for i in np.nditer(x):
    np = x*3+i+1
x
np
for i in range(0,1):
    x = i+2
x
from numpy import random

y = random.randint(100, size=(1,10))

y
trace = [][]
for i in y:
    trace += y[i,i]
sum_values = np.sum(np.abs(self.data))
if sum_values == BOARD_SIZE:
    self.winner = 0
    self.end = True
    return self.end
# wheter it's a tie
sum_values = np.sum(n)
data = np.zeros((BOARD_ROWS, BOARD_COLS))
data
import numpy as np
hash_value = 0

for i in np.nditer(data):
    hash_value = hash_value * 3 + i + 1

hash_value
def is_end(self):
    if self.end is not None:
        return self.end
    results = []
    # check now
    for i in range(BOARD_ROWS):
        results.append(np.sum(self.data[i,:]))
    for i in range(BOARD_COLS):
        results.append(np.sum(self.data[:])
import IPython
print(IPython.sys_info())
!pip install jupyter-kite
!jupyter labextension install "@kiteco/jupyterlab-kite"<jupyter_output>Requirement already satisfied: jupyter-kite in c:\users\vigne\anaconda3\lib\site-packages (1.2.0)
Requirement already satisfied: setuptools in c:\users\vigne\anaconda3\lib\site-packages (from jupyter-kite) (49.2.0.post20200714)
Requirement already satisfied: tornado>=5.1.0 in c:\users\vigne\anaconda3\lib\site-packages (from jupyter-kite) (6.0.4)
Requirement already satisfied: notebook>=4.3.1 in c:\users\vigne\anaconda3\lib\site-packages (from jupyter-kite) (6.0.3)
Requirement already satisfied: jinja2 in c:\users\vigne\anaconda3\lib\site-packages (from notebook>=4.3.1->jupyter-kite) (2.11.2)
Requirement already satisfied: pyzmq>=17 in c:\users\vigne\anaconda3\lib\site-packages (from notebook>=4.3.1->jupyter-kite) (19.0.1)
Requirement already satisfied: traitlets>=4.2.1 in c:\users\vigne\anaconda3\lib\site-packages (from notebook>=4.3.1->jupyter-kite) (4.3.3)
Requirement already satisfied: Send2Trash in c:\users\vigne\anaconda3\lib\site-packages (from notebook>=4.3.1->jupyter-kite) (1.5[...]
{'fraction_non_alphanumeric': 0.10940744290049513, 'fraction_numerical': 0.019325986264175053, 'mean_word_length': 1.9330210772833725, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '54155', 'license': 'MIT', 'n_tokens_mistral': 2131, 'n_tokens_neox': 2054, 'n_words': 528, 'path': 'Untitled.ipynb', 'repo_name': 'vigneshyaadav27/tick_tack_toe', 'size': '6261'}