Skip to content

Commit

Permalink
restructured - added main runner
Browse files Browse the repository at this point in the history
  • Loading branch information
BedirT committed May 19, 2021
1 parent 0df513d commit 7002c93
Show file tree
Hide file tree
Showing 15 changed files with 431 additions and 180 deletions.
28 changes: 16 additions & 12 deletions Projects/base/game/darkHex.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ def __init__(self, BOARD_SIZE=[3, 3], verbose=True,

self.BOARDS = {C_PLAYER2: deepcopy(self.BOARD), C_PLAYER1: deepcopy(self.BOARD)}

if custom_board_C_PLAYER1:
self.BOARDS[C_PLAYER1] = custom_board_C_PLAYER1
if custom_board_C_PLAYER2:
self.BOARDS[C_PLAYER2] = custom_board_C_PLAYER2
self.__set_board()
self.set_board(custom_board_C_PLAYER1, custom_board_C_PLAYER2)
self.num_moves = {C_PLAYER2: 0, C_PLAYER1: 0}
self.num_opp_known = {C_PLAYER2: 0, C_PLAYER1: 0}

Expand All @@ -49,13 +45,21 @@ def rewind(self, action):
self.BOARDS[C_PLAYER2][action] = '.'
self.valid_moves_colors[C_PLAYER2].append(action)

def __set_board(self):
for i, c in enumerate(self.BOARDS[C_PLAYER1]):
if c == C_PLAYER1:
self.BOARD[i] = c
for i, c in enumerate(self.BOARDS[C_PLAYER2]):
if c == C_PLAYER2:
self.BOARD[i] = c
def set_board(self, custom_board_C_PLAYER1, custom_board_C_PLAYER2):
if custom_board_C_PLAYER1:
self.BOARDS[C_PLAYER1] = custom_board_C_PLAYER1
if custom_board_C_PLAYER2:
self.BOARDS[C_PLAYER2] = custom_board_C_PLAYER2
for i, (c1, c2) in enumerate(zip(self.BOARDS[C_PLAYER1], self.BOARDS[C_PLAYER2])):
if c1 != c2 and not (c1 == '.' or c2 == '.'):
# print('Invalid custom board sequence.')
return False
if c1 == C_PLAYER1:
self.BOARD[i] = c1
if c2 == C_PLAYER2:
self.BOARD[i] = c2
return True #success


def print_information_set(self, player):
if not self.verbose:
Expand Down
24 changes: 23 additions & 1 deletion Projects/base/game/hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,26 @@ def check_early_win(self, color):
res = self.game_status()
self.BOARD[c] = temp; self.legality_check = True
if res == color:
return True
return True

def customBoard_print(board, num_cols, num_rows):
'''
Method for printing the board in a nice format.
'''
num_cells = num_cols * num_rows
print(colors.C_PLAYER1 + ' ' + '{0: <3}'.format(C_PLAYER1) * num_cols + colors.ENDC)
print(colors.BOLD + colors.C_PLAYER1 + ' ' + '-' * (num_cols * 3 +1) + colors.ENDC)
for cell in range(num_cells):
if cell % num_cols == 0: # first col
print(colors.BOLD + colors.C_PLAYER2 + 'W\ ' + colors.ENDC, end= '')
if board[cell] == C_PLAYER1:
clr = colors.C_PLAYER1
elif board[cell] == C_PLAYER2:
clr = colors.C_PLAYER2
else:
clr = colors.NEUTRAL
print(clr + '{0: <3}'.format(board[cell]) + colors.ENDC, end='')
if cell % num_cols == num_cols-1: # last col
print(colors.BOLD + colors.C_PLAYER2 + '\W\n' + (' ' * (cell//num_cols)) + colors.ENDC, end = ' ')
print(colors.BOLD + colors.C_PLAYER1 + ' ' + '-' * (num_cols * 3 +1) + colors.ENDC)
print(colors.BOLD + colors.C_PLAYER1 + ' ' * (num_rows+4) + '{0: <3}'.format(C_PLAYER1) * num_cols + colors.ENDC)
9 changes: 8 additions & 1 deletion Projects/base/util/cPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import pickle

def heat_map(results, num_cols, num_rows):
def heat_map(in_file):
with open(in_file, 'rb') as f:
dct = pickle.load(f)

results = dct['results']
num_cols = dct['num_cols']
num_rows = dct['num_rows']
all_total = 0; new_data_b = []; new_data_tot = []
y_size=range(num_rows)
x_size=range(num_cols)
Expand Down
9 changes: 6 additions & 3 deletions Projects/base/util/colors.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

class colors:
TITLE = '\033[35m'
MIDTEXT ='\033[32m'
QUESTIONS = '\033[94m'
C_PLAYER1 = '\033[96m'
C_PLAYER2 = '\033[91m'
C_PLAYER2 = '\033[35m'
NEUTRAL = '\033[93m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
UNDERLINE = '\033[4m'
WARNING = '\033[31m'
24 changes: 24 additions & 0 deletions Projects/base/util/drive.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
# https://stackoverflow.com/a/39225272/15269364

from pathlib import Path
import requests

def missing_in_file():
print('Please pick a pre-run game data to work on;')
print('\t1) 3x3 First Player')
print('\t2) 4x3 First Player (Long edge)')
game_type = -1
while game_type <= 0 or game_type > 2:
try:
game_type = int(input('Please enter the item number only (i.e. 1): ').strip())
except KeyboardInterrupt:
exit()
except:
game_type = -1
print('Invalid input, please try again.')
if game_type == 1:
Path('Exp_Results/pONE/3x3/').mkdir(parents=True, exist_ok=True)
in_file = 'Exp_Results/pONE/3x3/default_file.pkl'
download_file_from_google_drive('1oNl4UZAB6SxjA-aUi0M9oUiqlpIdXnwk', in_file)
elif game_type == 2:
Path('Exp_Results/pONE/4x3/').mkdir(parents=True, exist_ok=True)
in_file = 'Exp_Results/pONE/4x3/default_file.pkl'
download_file_from_google_drive('1MpMn8Gf0a8tCWeb8Ag276FlLGX6tBCD9', in_file)
return in_file

def download_file_from_google_drive(id, destination):
URL = "https://docs.google.com/uc?export=download"

Expand Down
20 changes: 20 additions & 0 deletions Projects/base/util/print_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from textwrap import wrap
from Projects.base.util.colors import colors

def_width = 50

def wrap_it(s, clr, w=def_width, end='\n'):
for i in wrap(s, w):
print(clr + i, end=end)

def question_cont(s, f=str):
while True:
try:
wrap_it(s, colors.QUESTIONS, end=' ');
val = f(input().strip())
break
except KeyboardInterrupt:
exit()
except:
wrap_it('The input is invalid.', colors.WARNING)
return val
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# loads pickle data on given path
import pickle
import argparse
from Projects.base.util import cPlot

Expand All @@ -8,7 +7,4 @@
help="File path to load the results from")
args = parser.parse_args()

with open(args.in_file, 'rb') as f:
dct = pickle.load(f)

cPlot.heat_map(dct['results'], dct['num_cols'], dct['num_rows'])
cPlot.heat_map(args.in_file)
11 changes: 11 additions & 0 deletions Projects/pONE/manual_play.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# loads pickle data on given path

import argparse
from Projects.pONE.user_tools.play import play_pONE

parser = argparse.ArgumentParser()
parser.add_argument("--in_file", "-if", type=str,
help="File path to load the results from")
args = parser.parse_args()

play_pONE(args.in_file)
17 changes: 17 additions & 0 deletions Projects/pONE/manual_train.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'''
Sample runner for PONE.
'''
import argparse
from Projects.pONE.user_tools.trainer import train_pONE

parser = argparse.ArgumentParser()
parser.add_argument("--num_of_rows", "-nr", default=3, type=int,
help="Number of rows on the board.")
parser.add_argument("--num_of_cols", "-nc", default=3, type=int,
help="Number of columns on the board.")
parser.add_argument("--out_file", "-f", default="default_file", type=str,
help="File path to save the resulting list on. Please do not add any extension to file.\
here is an example usage:\n\t'filepath/filename'")
args = parser.parse_args()

train_pONE(args.out_file, args.num_of_rows, args.num_of_cols)
122 changes: 0 additions & 122 deletions Projects/pONE/pONE_player.py

This file was deleted.

Loading

0 comments on commit 7002c93

Please sign in to comment.