This notebook seeks to quantify the value of leaving a certain number of tiles in the bag during the pre-endgame based on a repository of games. We will then implement these values as a pre-endgame heuristic in the Macondo speedy player to improve simulation quality.

Initial questions:

- What is the probability that you will go out first if you make a play leaving N tiles in the bag?
- (slightly harder) What is the expected improvement in end-of-game spread after making a play that leaves N tiles in the bag?

We'll need to make several passes through the log file to obtain the following:

- Final spread of each simulated game
- Delta between pre-endgame and final spread

- We're only analyzing complete games
- The last two rows in the log file for a given game ID are also the last two turns of the game

- Standardize sign convention for spread.
- Start figuring out how to calculate pre-endgame spread

```
In [1]:
```import csv
import pandas as pd
log_folder = '../logs/'
log_file = log_folder + 'log_20200411_short.csv'

```
In [2]:
```second_to_last_move_dict = {}
last_move_dict = {}
spread_dict = {}
p1_minus_p2_spread_dict = {}

```
In [3]:
```n=10000000
with open(log_file,'r') as f:
moveReader = csv.reader(f)
next(moveReader)
for i,row in enumerate(moveReader):
if i<n:
if row[1] in last_move_dict.keys():
second_to_last_move_dict[row[1]] = last_move_dict[row[1]]
last_move_dict[row[1]] = row
if i==n:
break

```
In [4]:
```# whoever made the final move went out first
went_out_first_dict = {game_id:last_move_dict[game_id][0] for game_id in last_move_dict.keys()}
# good sanity check - player 1 should go out a bit more often
print('Analyzing {} games, player 1 went out first {}% of the time'.format(
len(went_out_first_dict),
100*pd.Series(went_out_first_dict).value_counts(normalize=True)[0]))

```
```

```
In [5]:
```for game_id in last_move_dict.keys():
spread_dict[game_id] = int(last_move_dict[game_id][6])-int(second_to_last_move_dict[game_id][6])
p1_minus_p2_spread_dict[game_id] = (int(last_move_dict[game_id][6])-int(second_to_last_move_dict[game_id][6]))*\
-(int(last_move_dict[game_id][0])-int(second_to_last_move_dict[game_id][0]))

```
In [6]:
```print('The person who went out first won by an average of {} points'.format(pd.Series(spread_dict).mean()))
print('Player 1 won by an average of {} points'.format(pd.Series(p1_minus_p2_spread_dict).mean()))

```
```

```
In [ ]:
```

```
In [ ]:
```