-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile.py
51 lines (45 loc) · 1.76 KB
/
tile.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
This implements the Tile class.
A Tile should be the data of what numbers and colors the tile possibly is.
"""
import numpy as np
import pandas as pd
import hanabi
from typing import List
class Tile:
def __init__(self, colors:List[int] = None, numbers:List[int] = None) -> None:
# The two arrays indicate the given hints.
# 1 means the tile can be the number/color, and 0 means the tile is not.
if colors is not None:
assert len(colors)==5
self.colors=colors
else:
self.colors = [1]*5
if numbers is not None:
assert len(numbers)==5
self.numbers=numbers
else:
self.numbers = [1]*5
def __repr__(self) -> str:
return f"Tile(colors={self.colors}, numbers={self.numbers})"
def __str__(self) -> str:
return f"""
Colors: {self.colors}.
Numbers: {self.numbers}."""
# return self.possible_colors.__repr__()
def __eq__(self, other) -> bool:
return self.numbers == other.numbers and self.colors == other.colors
def prob_matrix(self, seen:pd.DataFrame) -> pd.DataFrame:
assert seen.shape == (5, 5)
total_num_tiles = pd.DataFrame(
np.array( [3, 2, 2, 2, 1]*5 ).reshape(5, 5),
index=hanabi.colors,
columns=hanabi.numbers,
)
# This is a 5 by 5 matrix of 0 and 1, indicating possible titles.
a=np.array(self.colors).reshape(5, 1)
b=np.array(self.numbers)
# total_num_tiles-seen is a df of the counts of remaining tiles.
# Multiply by the above matrix to see which tile this possibly is.
num_possible = (total_num_tiles - seen) * (a*b)
return num_possible/(num_possible.sum().sum())