Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0b59136

Browse files
some functions to play the game on the commandline
1 parent 9fdb001 commit 0b59136

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

‎Game.py‎

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,63 @@
1+
import sys
2+
13
from Engine import Engine
24
from Board import Board
35

46

57
class Game(object):
68
"""Handles the operations of a Game"""
79

8-
def __init__(self):
10+
def __init__(self, position=None):
911
super(Game, self).__init__()
10-
self.board = Board()
11-
self.engine = Engine(self.board, depth=12)
12+
self.board = Board(position)
13+
self.engine = Engine(self.board, depth=7)
1214

1315
def input_move(self):
14-
idx = int(input())
16+
idx = None
17+
while True:
18+
try:
19+
idx = int(input())
20+
except:
21+
print("Invalid Move")
22+
continue
23+
else:
24+
break
25+
if idx == 99:
26+
sys.exit()
1527
return idx
1628

1729
def human_move(self):
1830
moves = self.engine.generate_move_list()
1931
self.board.show()
2032
print("Please choose one move from the list.")
21-
print(' | '.join("%d. %s" % (i, str(move)) for i, move in enumerate(moves)))
33+
print('\n'.join("%d. %s" % (i, str(move)) for i, move in enumerate(moves)))
2234

2335
self.engine._make_move(moves[self.input_move()])
24-
self.engine.make_best_move()
2536

2637

27-
game = Game()
38+
def play():
39+
game = Game()
40+
41+
while not game.board.winner:
42+
game.human_move()
43+
if game.board.winner:
44+
break
45+
game.engine.make_best_move()
46+
47+
game.board.show()
48+
print(game.board.winner)
49+
50+
51+
def play_random_game():
52+
game = Game()
53+
# move_num = 1
54+
while not game.board.winner:
55+
# print(move_num)
56+
# move_num += 1
57+
game.engine.make_best_move()
58+
game.board.show()
59+
60+
return game.board.winner
61+
2862

29-
while not game.board.winner:
30-
game.human_move()
63+
play()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /