-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
64 lines (62 loc) · 2.43 KB
/
game.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
import board
import player
import random
class Game(object):
"""游戏类"""
def __init__(self):
self.chess_board = board.Board() # 棋盘对象
self.human = player.Player("玩家 1") # 人类玩家对象1
# self.computer = player.Player("玩家 2") # 人类玩家对象2
self.computer = player.AIPlayer("计算机") # 计算机玩家对象
def random_player(self):
"""随机先手玩家
:return: 落子先后顺序的玩家元祖
"""
if random.randint(0,1) == 1:
players = (self.human, self.computer)
else:
players = (self.computer, self.human)
# 设置玩家棋子
players[0].chess = "X"
players[1].chess = "O"
print("根据随机抽取结果 %s 先行" % players[0].name)
return players
def play_round(self):
"""一轮完整对局"""
# 1. 显示棋子落子位置
self.chess_board.show_board(True)
# 2. 随机决定先手
current_player, next_player = self.random_player()
# 3. 两个玩家轮流落子
while True:
# 落子方落子
current_player.move(self.chess_board)
# 显示落子结果
self.chess_board.show_board()
# 是否胜利?
if self.chess_board.is_win(current_player.chess):
print("%s 战胜 %s" % (current_player.name, next_player.name))
current_player.score += 1
break
# 是否平局
if self.chess_board.is_draw():
print("%s 和 %s 战成平局" % (current_player, next_player.name))
break
# 交换落子方
current_player, next_player = next_player, current_player
# 4. 显示比分
print("[%s] 对战 [%s] 比分是 %d : %d" % (self.human.name, self.computer.name, self.human.score, self.computer.score))
def start(self):
"""循环开始对局"""
while True:
# 一轮完整的对局
self.play_round()
# 询问是否继续
is_continue = input("是否再来一盘(Y/N)?").upper()
# 判断玩家输入
if is_continue != "Y":
break
# 重置棋盘数据
self.chess_board.reset_board()
if __name__ == '__main__':
Game().start()