def __init__(self, team, bowling_team): self.batsmen = [] for player in team['players']: self.batsmen.append(Batsman(player)) self.bowler = [] bowling_team['players'].reverse() for player in bowling_team['players']: if player['role'] == 'Bowler': self.bowler.append(Bowler(player)) if len(self.bowler) == 6: break for player in bowling_team['players']: if player['role'] == 'All Rounder': self.bowler.append(Bowler(player)) if len(self.bowler) == 6: break bowling_team['players'].reverse() self.name = team['name'] self.opponent_name = bowling_team['name'] self.onstrike_batsman = 0 self.offstrike_batsman = 1 self.score = 0 self.wickets = 0 self.run_rate = 0 self.balls = 0 self.sixes = 0 self.fours = 0 self.extras = 0 self.current_bowler = 0 self.bowling_pair = (0, 1) self.bowler_index = 0
def createBowler(self, players, bowlerMapping): '''param: players - List of players containing name''' bowlers = [] for player in players: if player[0] in bowlerMapping: bowler = Bowler(player[0], bowlerMapping[player[0]]) bowlers.append(bowler) return bowlers
def add_bowler(self): name = self.player_name.text() if name: self.player_list.addItem(name) bowler = Bowler(name) self.bowler_dict[self.player_num] = bowler self.player_num += 1 self.player_name.clear()
def main(): team = [] inText = input("Enter a name and a score, enter to quit. ") while inText != "": inText = inText.split() team.append(Bowler(inText[0], int(inText[1]))) inText = input("Enter a name and a score, enter to quit. ") printAsEntered(team) sortedScores = printByScores(team) printByNames(team) printSummary(sortedScores) outFile = open("game_results.txt", "w") printAsEntered(team, outFile) printByScores(team, outFile) printByNames(team, outFile) printSummary(sortedScores, outFile) outFile.close()
def setupUi(self, StartGame): StartGame.setObjectName("StartGame") StartGame.resize(400, 300) self.horizontalLayoutWidget_2 = QtGui.QWidget(StartGame) self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(9, 10, 381, 41)) self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2") self.horizontalLayout_2 = QtGui.QHBoxLayout( self.horizontalLayoutWidget_2) self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0) self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.welcome = QtGui.QLabel(self.horizontalLayoutWidget_2) self.welcome.setAlignment(QtCore.Qt.AlignCenter) self.welcome.setObjectName("welcome") self.horizontalLayout_2.addWidget(self.welcome) self.horizontalLayoutWidget_3 = QtGui.QWidget(StartGame) self.horizontalLayoutWidget_3.setGeometry( QtCore.QRect(40, 240, 321, 51)) self.horizontalLayoutWidget_3.setObjectName("horizontalLayoutWidget_3") self.horizontalLayout_3 = QtGui.QHBoxLayout( self.horizontalLayoutWidget_3) self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0) self.horizontalLayout_3.setObjectName("horizontalLayout_3") self.button_start_game = QtGui.QPushButton( self.horizontalLayoutWidget_3) self.button_start_game.setObjectName("button_start_game") self.horizontalLayout_3.addWidget(self.button_start_game) self.verticalLayoutWidget = QtGui.QWidget(StartGame) self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 60, 361, 171)) self.verticalLayoutWidget.setObjectName("verticalLayoutWidget") self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget) self.verticalLayout.setContentsMargins(0, 0, 0, 0) self.verticalLayout.setObjectName("verticalLayout") self.label_add_player = QtGui.QLabel(self.verticalLayoutWidget) self.label_add_player.setObjectName("label_add_player") self.verticalLayout.addWidget(self.label_add_player) self.horizontalLayout_6 = QtGui.QHBoxLayout() self.horizontalLayout_6.setSizeConstraint(QtGui.QLayout.SetFixedSize) self.horizontalLayout_6.setContentsMargins(5, 5, 5, 5) self.horizontalLayout_6.setObjectName("horizontalLayout_6") self.player_name = QtGui.QLineEdit(self.verticalLayoutWidget) self.player_name.setObjectName("player_name") self.horizontalLayout_6.addWidget(self.player_name) spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) self.horizontalLayout_6.addItem(spacerItem) self.button_add_new_bowler = QtGui.QPushButton( self.verticalLayoutWidget) self.button_add_new_bowler.setMinimumSize(QtCore.QSize(20, 20)) self.button_add_new_bowler.setObjectName("button_add_new_bowler") self.horizontalLayout_6.addWidget(self.button_add_new_bowler) self.verticalLayout.addLayout(self.horizontalLayout_6) self.player_list = QtGui.QListWidget(self.verticalLayoutWidget) self.player_list.setObjectName("player_list") self.verticalLayout.addWidget(self.player_list) spacerItem1 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem1) self.retranslateUi(StartGame) QtCore.QMetaObject.connectSlotsByName(StartGame) QtGui.QWidget.connect(self.button_add_new_bowler, QtCore.SIGNAL("pressed()"), self.add_bowler) QtGui.QWidget.connect(self.button_start_game, QtCore.SIGNAL("pressed()"), self.launch_mainapp) QtGui.QWidget.connect(mainapp.ui.button_bowl, QtCore.SIGNAL("released()"), self.bowl) self.player_list.addItem('Matt') matt = Bowler('Matt') self.bowler_dict[0] = matt self.player_num += 1
class ControlMainWindow(QtGui.QMainWindow): bowlers = {} next_bowler = Bowler('') def __init__(self, parent=None): super(ControlMainWindow, self).__init__(parent) self.ui = mainwindow.Ui_MainWindow() self.ui.setupUi(self) """ Gets current bowler from name, rolls, and updates score Decides whether to move to next player (e.g. a strike, a spare, or an open frame) """ def bowl(self, bowlers, name): # Initialize if not self.next_bowler.get_name(): self.next_bowler = bowlers[0] if not self.bowlers: self.bowlers = bowlers current_bowler_index = 0 for key in self.bowlers.keys(): bowler = self.bowlers[key] if bowler.get_name() == name: current_bowler_index = key break current_bowler = self.bowlers[current_bowler_index] if current_bowler.get_name() == 'Matt': skill_level = 0 else: skill_level = 10 # Simulates lane control hardware if len(current_bowler.get_current_frame()) == 0: pins_left = random.randint(0, skill_level) else: pins_left = random.randint( 0, 10 - current_bowler.get_current_frame()[0]) strike_spare_or_open = self.next_bowler.bowl(pins_left) self.update_score(current_bowler_index) if current_bowler_index == len(bowlers) - 1 and current_bowler.current_frame == 12 and len(bowlers) != 1\ or len(bowlers) == 1 and current_bowler.current_frame > 13: winning_score = 0 winning_bowler = '' for bowler_index, bowler in self.bowlers.iteritems(): if bowler.get_total_score() > winning_score: winning_score = bowler.get_total_score() winning_bowler = bowler.get_name() winner.ui.label_2.setText('Congratulations, ' + str(winning_bowler) + '!!! You\ve won the game!') winner.show() mainapp.hide() if strike_spare_or_open: self.next_bowler = self.get_next_bowler(current_bowler_index) self.ui.current_bowler.setText(self.next_bowler.get_name()) """ Updates UI elements (individual rolls) with new rolls, accounting for strikes and spares """ def update_score(self, bowler_index): # So ugly... Please forgive me bowler = self.bowlers[bowler_index] player_widget = mainapp.ui.verticalLayout.itemAt(bowler_index).widget() frame = len(bowler.frames) if not bowler.frames[frame]: frame -= 1 if bowler.frames[frame]: frame_finished = len(bowler.frames[frame]) > 1 throw1 = str(bowler.frames[frame][0]) if throw1 == '10': throw1 = 'X' elif frame_finished: throw2 = str(bowler.frames[frame][1]) if sum(bowler.frames[frame]) == 10: throw2 = '/' if frame == 1: player_widget.ui.throw_1.setText(throw1) if frame_finished: player_widget.ui.throw_2.setText(throw2) if frame == 2: player_widget.ui.throw_3.setText(throw1) if frame_finished: player_widget.ui.throw_4.setText(throw2) if frame == 3: player_widget.ui.throw_5.setText(throw1) if frame_finished: player_widget.ui.throw_6.setText(throw2) if frame == 4: player_widget.ui.throw_7.setText(throw1) if frame_finished: player_widget.ui.throw_8.setText(throw2) if frame == 5: player_widget.ui.throw_9.setText(throw1) if frame_finished: player_widget.ui.throw_10.setText(throw2) if frame == 6: player_widget.ui.throw_11.setText(throw1) if frame_finished: player_widget.ui.throw_12.setText(throw2) if frame == 7: player_widget.ui.throw_13.setText(throw1) if frame_finished: player_widget.ui.throw_14.setText(throw2) if frame == 8: player_widget.ui.throw_15.setText(throw1) if frame_finished: player_widget.ui.throw_16.setText(throw2) if frame == 9: player_widget.ui.throw_17.setText(throw1) if frame_finished: player_widget.ui.throw_18.setText(throw2) if frame == 10: player_widget.ui.throw_19.setText(throw1) if frame_finished: player_widget.ui.throw_20.setText(throw2) if frame == 11: if len(bowler.frames[frame - 1]) > 1: player_widget.ui.throw_21.setText(throw1) else: player_widget.ui.throw_20.setText(throw1) if len(bowler.frames[frame] ) == 2 and bowler.frames[10] == [10]: player_widget.ui.throw_21.setText(throw2) if frame == 12: player_widget.ui.throw_21.setText(throw1) ControlMainWindow.update_frame_scores(player_widget, bowler) player_widget.ui.cum_score.setText(str(bowler.get_total_score())) """ Updates frame scores """ @staticmethod def update_frame_scores(player_widget, bowler): if len(bowler.score) >= 1: player_widget.ui.score_1.setText(str(bowler.score[0])) if len(bowler.score) >= 2: player_widget.ui.score_2.setText(str(bowler.score[1])) if len(bowler.score) >= 3: player_widget.ui.score_3.setText(str(bowler.score[2])) if len(bowler.score) >= 4: player_widget.ui.score_4.setText(str(bowler.score[3])) if len(bowler.score) >= 5: player_widget.ui.score_5.setText(str(bowler.score[4])) if len(bowler.score) >= 6: player_widget.ui.score_6.setText(str(bowler.score[5])) if len(bowler.score) >= 7: player_widget.ui.score_7.setText(str(bowler.score[6])) if len(bowler.score) >= 8: player_widget.ui.score_8.setText(str(bowler.score[7])) if len(bowler.score) >= 9: player_widget.ui.score_9.setText(str(bowler.score[8])) if len(bowler.score) >= 10: final_frame_score = bowler.score[9] if len(bowler.score) > 10: final_frame_score += bowler.score[10] if len(bowler.score) == 12: final_frame_score += bowler.score[11] player_widget.ui.score_10.setText(str(final_frame_score)) def get_next_bowler(self, bowler_index): if bowler_index == len(self.bowlers) - 1: bowler_index = 0 else: bowler_index += 1 return self.bowlers[bowler_index]