/
generator.py
33 lines (26 loc) · 862 Bytes
/
generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import numpy as np
from random import random
from tic_tac_toe import TicTacToe
class TicTacToeGenerator:
'''
various methods of generating plausable tic tac toe boards
'''
def generate_naive(self):
tiles = [-1, 0, 1]
return np.random.choice(tiles, 9)
def generate_weighted(self):
tiles = [-1, 0, 1]
p_blank = random() / 3 + 0.5
p_tile = (1 - p_blank) / 2
return np.random.choice(tiles, 9, p=[p_tile, p_blank, p_tile])
def generate_valid(self):
tile = [1, -1]
turn = 0
board = np.zeros(9)
while(np.any(board == 0)):
index = np.random.choice(np.nonzero(board == 0)[0], 1)
board[index] = tile[turn % 2]
turn += 1
if TicTacToe.is_solved(board) or random() > 0.75:
break
return board