コード例 #1
0
    def test_generate_commentory(self):
        test_message_string = "3.2 Test Player scores 2 run"
        test_out_message_string = "3.2 Test Player out"

        message_string = Commentary.generate_commentary(current_over=3, current_ball=2, name='Test Player', run=2)
        message_out_string = Commentary.generate_commentary(current_over=3, current_ball=2, name='Test Player', run=None)

        self.assertEqual(test_message_string,message_string)
        self.assertEqual(test_out_message_string, message_out_string)

        print("Generate Commentory test case executed successfully", end='\n\n')
コード例 #2
0
    def play(self, overs, balls_per_over, target):
        self.total_overs = overs
        self.balls_per_over = balls_per_over

        # set state of a team to batting
        self.set_state(1)

        # set active players
        self.set_active_players()

        runs_to_win = target - self.runs_scored

        current_over = 0
        while current_over < overs and runs_to_win >= 0:
            if target:
                Commentary.generate_commentary_over_wise(
                    self.total_overs - current_over, runs_to_win)
            else:
                Commentary.generate_commentary_over_wise(self.total_overs -
                                                         current_over)

            current_ball = 1
            while current_ball <= balls_per_over and runs_to_win >= 0:

                self.total_balls_played += 1

                self.current_player = self.active_players[
                    self.current_active_player_index]

                if self.current_player.get_position_on_pitch() == 0:
                    self.current_player.set_position_on_pitch(1)

                runs = self.current_player.play(Game.available_runs)

                # Generate ball by ball commentary
                Commentary.generate_commentary(current_over, current_ball,
                                               self.current_player.name, runs)

                # updating total runs of a team
                if runs is not None:
                    self.set_runs(runs)
                    runs_to_win = target - self.runs_scored

                if runs == 1 or runs == 3 or runs == 5:
                    self.change_positions()

                # when a player is out
                if runs is None:
                    # setting current player state to out i.e. 0
                    self.current_player.set_state(0)

                    # changing active players and setting it to next player
                    next_player = self.get_next_player()
                    if next_player:
                        self.active_players[
                            self.current_active_player_index] = next_player
                    else:
                        return self.runs_scored

                current_ball += 1

            current_over += 1

            self.total_overs_played += 1

            # change position after every over
            self.change_positions()

        # set state of the team again
        self.set_state(1)

        return self.runs_scored