You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
4.5 KiB
125 lines
4.5 KiB
from common.game.gameData import GameData
|
|
from enums.category import *
|
|
|
|
|
|
class RuleManager(object):
|
|
def __init__(self, game_data: GameData):
|
|
self.__game_data = game_data
|
|
self.__should_hit = []
|
|
self.need_reset_cue_ball = False
|
|
self.settlement = False
|
|
self.round = 1
|
|
|
|
def judge(self):
|
|
self.print_log()
|
|
self.do_reset()
|
|
self.cal_score()
|
|
self.try_turn_round()
|
|
self.settlement = False
|
|
|
|
def print_log(self):
|
|
print('round:' + str(self.round))
|
|
print('collide: ' + str(self.__game_data.collide_log))
|
|
print('collision_flag: ' + str(self.collision_legal()))
|
|
print('goal: ' + str(self.__game_data.goal_balls))
|
|
print('goal_flag: ' + str(self.goal_legal()))
|
|
|
|
def check_legality(self):
|
|
return self.collision_legal() and self.goal_legal() and self.cue_legal()
|
|
|
|
def do_reset(self):
|
|
if not self.goal_legal():
|
|
for _ in self.__game_data.balls:
|
|
if _.category in COLORED_BALLS and _.category in self.__game_data.goal_balls:
|
|
_.reset()
|
|
if not self.cue_legal():
|
|
self.need_reset_cue_ball = True
|
|
for _ in self.__game_data.balls:
|
|
if _.category == RED_BALL and _.activity:
|
|
for __ in self.__game_data.balls:
|
|
if __.category in COLORED_BALLS and __.category in self.__game_data.goal_balls:
|
|
__.reset()
|
|
break
|
|
|
|
def get_legality(self):
|
|
legality = []
|
|
if not self.collision_legal():
|
|
legality.append(legality)
|
|
if not self.goal_legal():
|
|
legality.append(legality)
|
|
if not self.cue_legal():
|
|
legality.append(legality)
|
|
return legality
|
|
|
|
def try_turn_round(self):
|
|
if len(self.__game_data.goal_balls) != 0 and self.check_legality():
|
|
return
|
|
self.__game_data.goal_balls.clear()
|
|
self.__game_data.last_goal_balls.clear()
|
|
self.round *= -1
|
|
|
|
def get_should_hit(self):
|
|
self.__should_hit.clear()
|
|
have_red_ball = False
|
|
for _ in self.__game_data.balls:
|
|
if _.category == RED_BALL and _.activity:
|
|
have_red_ball = True
|
|
break
|
|
if have_red_ball:
|
|
if len(self.__game_data.last_goal_balls) == 0:
|
|
return self.__should_hit.append(RED_BALL)
|
|
if self.__game_data.last_goal_balls[0] in COLORED_BALLS:
|
|
return self.__should_hit.append(RED_BALL)
|
|
self.__should_hit = [_ for _ in COLORED_BALLS]
|
|
return
|
|
should_hit = 7
|
|
for _ in self.__game_data.balls:
|
|
if _.category < should_hit and _.activity:
|
|
should_hit = _.category
|
|
return self.__should_hit.append(should_hit)
|
|
|
|
def collision_legal(self):
|
|
if len(self.__game_data.collide_log) == 0:
|
|
return False
|
|
if self.__game_data.collide_log[0] in self.__should_hit:
|
|
return True
|
|
return False
|
|
|
|
def goal_legal(self):
|
|
without_cue_list = [_ for _ in self.__game_data.goal_balls]
|
|
if CUE_BALL in without_cue_list:
|
|
without_cue_list.remove(CUE_BALL)
|
|
if len(without_cue_list) == 0:
|
|
return True
|
|
if len(list(set(without_cue_list) & set(self.__should_hit))) == 0:
|
|
return False
|
|
if RED_BALL not in self.__should_hit and len(list(set(without_cue_list) & set(self.__should_hit))) != 1:
|
|
return True
|
|
return True
|
|
|
|
def cue_legal(self):
|
|
return CUE_BALL not in self.__game_data.goal_balls
|
|
|
|
def cal_score(self):
|
|
if self.round == 1:
|
|
if self.check_legality():
|
|
for _ in self.__game_data.goal_balls:
|
|
self.__game_data.left_score += _
|
|
return
|
|
if len(self.__game_data.collide_log) == 0:
|
|
self.__game_data.right_score += 4
|
|
return
|
|
first_collide = self.__game_data.collide_log[0]
|
|
self.__game_data.right_score += first_collide if first_collide > 4 else 4
|
|
else:
|
|
if self.check_legality():
|
|
for _ in self.__game_data.goal_balls:
|
|
self.__game_data.right_score += _
|
|
return
|
|
if len(self.__game_data.collide_log) == 0:
|
|
self.__game_data.left_score += 4
|
|
return
|
|
first_collide = self.__game_data.collide_log[0]
|
|
self.__game_data.left_score += first_collide if first_collide > 4 else 4
|
|
pass
|