forked from kenjyoung/KHex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
human_vs.py
101 lines (88 loc) · 2.8 KB
/
human_vs.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from program import Program
import sys
import argparse
import threading
import time
from gamestate import gamestate
def make_valid_move(game, agent, color):
move = agent.sendCommand("genmove "+color)
while(True):
if(game.cell_color(move_to_cell(move))==game.PLAYERS["none"]):
agent.sendCommand("valid")
game.play(move_to_cell(move))
break
else:
move = agent.sendCommand("occupied")
return move
def get_human_move(game, human_game, color):
human_game.set_turn(game.PLAYERS[color])
color_str = '@' if color=='black' else 'O'
while(True):
print(human_game)
move = input("Please type a move ("+color_str+") :")
try:
if(game.cell_color(move_to_cell(move))==game.PLAYERS["none"]):
print("valid (move was played)")
game.play(move_to_cell(move))
human_game.play(move_to_cell(move))
break
else:
print("cell occupied (try another move)")
opponent = game.OPPONENT[game.turn()]
human_game.place(opponent, move_to_cell(move))
except (ValueError, IndexError):
print("Invalid input, please try again")
return move
def move_to_cell(move):
x = ord(move[0].lower())-ord('a')
y = int(move[1:])-1
return (x,y)
parser = argparse.ArgumentParser(description="Referee a game of Kriegspiel Hex between a human and an executable agent")
parser.add_argument("program", type=str, help="executable")
parser.add_argument("--first", "-f", dest="first", action='store_const',
const=True, default=False,
help="human plays first (if not present default to second)")
parser.add_argument("--boardsize", "-b", type=int, help="side length of (square) board")
parser.add_argument("--time", "-t", type=int, help="total time allowed for each move in seconds")
args = parser.parse_args()
if args.boardsize:
boardsize = args.boardsize
else:
boardsize = 11
if args.time:
time = args.time
else:
time = 10
agent = Program(args.program, False)
agent.sendCommand("boardsize "+str(boardsize))
agent.sendCommand("set_time "+str(time))
human_game = gamestate(boardsize)
game = gamestate(boardsize)
winner = None
moves = []
while(True):
if(args.first):
moves.append(get_human_move(game, human_game, "black"))
if(game.winner() != game.PLAYERS["none"]):
winner = game.winner()
break
print("waiting for opponent...")
moves.append(make_valid_move(game, agent, "white").strip())
print("done")
if(game.winner() != game.PLAYERS["none"]):
winner = game.winner()
break
else:
print("waiting for opponent...")
moves.append(make_valid_move(game, agent, "black").strip())
print("done")
if(game.winner() != game.PLAYERS["none"]):
winner = game.winner()
break
moves.append(get_human_move(game, human_game, "white"))
if(game.winner() != game.PLAYERS["none"]):
winner = game.winner()
break
print("Game over, " + game.PLAYER_STR[winner]+" wins.")
print(game)
print(" ".join(moves))