python - Ping Pong simulation not correctly triggering function -
i'm new oop , practicing putting little bit more complex programs using various classes , implementing principles inheritance. i've created ping-pong simulation has player class contains probability player win serve. have pingpong class subclass of super class racquetsports. each instance single game, ability change server, , record won, , whether or not shut-out. finally, have simstats class purpose record stats across "n" number of games.
my problem seems play_game
function not correctly firing, when place print statement in there never triggers. current result when running whole program player1 has 10 wins no shutouts, , player2 has 0 of both.
finally, suggestions on better oo practice appreciated. here player class:
from random import random class player(object): def __init__(self, prob_win): self.prob = prob_win self.points = 0 def wins_serve(self): return self.prob >= random() def add_point(self): self.points += 1 def get_score(self): return self.points
my racquetsports class:
from abc import abcmeta, abstractmethod player import player class racquetsport(object): __metaclass__ = abcmeta def __init__(self, prob1, prob2): self.player1 = player(prob1) self.player2 = player(prob2) self.server = self.player1 def play_game(self): while not self.game_over(): self.sim_point() @abstractmethod def type(self): pass def chg_server(self): if self.server == self.player1: self.server = self.player2 else: self.server = self.player1 def sim_point(self): if self.server.wins_serve(): self.server.add_point() else: self.chg_server() @abstractmethod def game_over(self): pass def get_scores(self): return self.player1.get_score(), \ self.player2.get_score() def return_stats(self): p1_score, p2_score = self.get_scores() print(p1_score, p2_score) won = 'p1' if p2_score > p1_score: won = 'p2' return won, self.__shutout(p1_score, p2_score) @staticmethod @abstractmethod def __shutout(score1, score2): pass
my pingpong , simstats classes, calling code:
from racquet import racquetsport class pingpong(racquetsport): def type(self): return 'ping pong' def game_over(self): return self.player1.get_score == 11 or \ self.player2.get_score == 11 @staticmethod def __shutout(score1, score2): return abs(score1 - score2) == 11 class simstats(object): def __init__(self): # first field games won, second shutouts. self.gms_won_p1 = [0] * 2 self.gms_won_p2 = [0] * 2 def update(self, game): won, shutout = game.return_stats() if won == 'p1': self.gms_won_p1[0] += 1 if shutout: self.gms_won_p1[1] += 1 else: self.gms_won_p2[0] += 1 if shutout: self.gms_won_p2[1] += 1 def print_results(self): tot_games = self.gms_won_p1 + self.gms_won_p2 print('wins player 1 = {} shutouts = {}\n' 'wins player 2 = {} shutouts = {}'.format(*tot_games)) if __name__ == '__main__': stats = simstats() x in range(1, 11): game = pingpong(.5, .5) stats.update(game) stats.print_results()
your first problem never call play_game
. guess intend work this:
if __name__ == '__main__': stats = simstats() x in range(1, 11): game = pingpong(.5, .5) game.play_game() stats.update(game) stats.print_results()
next, have bug cause entire game last forever. take @ these lines:
def game_over(self): return self.player1.get_score == 11 or \ self.player2.get_score == 11
get_score
function, need call it:
def game_over(self): return self.player1.get_score() == 11 or \ self.player2.get_score() == 11
Comments
Post a Comment