def game_result(): form_data = request.form player_1 = Player(form_data['player_1_name'], form_data['player_1_choice']) if form_data['computer'] == '1': player_2 = Player("Computer", "") player_2.computer() else: player_2 = Player(form_data['player_2_name'], form_data['player_2_choice']) r_p_s = Game("Rock Paper Scissors", player_1, player_2) r_p_s.play() return render_template('result.html', game=r_p_s)
class GameTest(unittest.TestCase): def setUp(self): self.game_1 = Game(Player("Hal", "Rock"), Player("Dave", "Scissors")) self.game_2 = Game(Player("Rick", "Rock"), Player("Morty", "Scissors")) self.game_3 = Game(Player("Bill", "Paper"), Player("Ted", "Paper")) self.game_4 = Game(Player("Merry", "Rock"), Player("Pippin", "Paper")) self.game_5 = Game(Player("Bob", "Rock"), ) def test_game_has_player_1(self): self.assertEqual("Hal", self.game_1.player_1.name) def test_game_has_player_2(self): self.assertEqual(3, self.game_1.player_2.choice) def test_game_can_play__player_1_win(self): self.assertEqual("Rick", self.game_2.play().name) def test_game_can_play__player_2_win(self): self.assertEqual("Paper", self.game_4.play().input_choice) def test_game_can_play__tied_game(self): self.assertEqual(None, self.game_3.play()) def test_show_results__winner(self): self.game_1.play() self.assertEqual("Hal wins by playing Rock!", self.game_1.show_results()) def test_show_results__draw(self): self.game_3.play() self.assertEqual("It's a draw!", self.game_3.show_results()) def test_game_gets_computer_player(self): self.assertEqual("Computer", self.game_5.player_2.name)
def play(): player_name = request.form['name'] player_choice = request.form['choice'] player = Player(player_name, player_choice) new_game = Game() new_game.play(player) print(request.form) if player.choice == new_game.player_2.choice: return render_template( 'game-play.html', page='OUTCOME', outcome= f"Both {player.name} and Computer have chosen {player.choice.upper()} so it's a draw! Try again!" ) else: return render_template( 'game-play.html', page='OUTCOME', outcome= f"Player {player.name} has chosen {player.choice.upper()}, and Computer has chosen {new_game.player_2.choice.upper()} and the winner is {new_game.winner}!" )
def solo_play(): player_1 = request.form['player_1'] choice_1 = request.form['choice_1'] game = Game(Player(player_1, choice_1), ) winning_player = game.play() result = game.show_results() return render_template("results.html", title="Results", winning_player=winning_player, game_result=result)
def index(player_1_name, player_1_guess, player_2_name, player_2_guess): # # player_1 player_1 = Player(player_1_name, player_1_guess) player_2 = Player(player_2_name, player_2_guess) the_game = Game() result = the_game.play(player_1, player_2) return render_template("index.html", result=result, title="Skies") #@app.route('/rock/rock') # so flask returns rock/rock and the return function to the server? #def draw(): # return "Its a draw" #@app.route('/rock/paper') #test inputs are made in html as python code? #def rock_wins(): # return "rock wins"
def test_game_result(player_1_choice, player_2_choice): player_1 = Player("Boris the blade", player_1_choice) player_2 = Player("Batman", player_2_choice) r_p_s = Game("Rock Paper Scissors", player_1, player_2) r_p_s.play() return render_template('result.html', game=r_p_s)
class TestGame(unittest.TestCase): pass def setUp(self): self.player_1 = Player("Sauron", "rock") self.player_2 = Player("Gandalf", "paper") self.game = Game("Our first game", self.player_1, self.player_2) # @unittest.skip("Delete this line to run the test") def test_game_being_created(self): self.assertEqual(self.game.name, "Our first game") self.assertEqual(self.game.player_1.name, "Sauron") self.assertEqual(self.game.player_2.name, "Gandalf") def test_game_play(self): self.assertEqual(self.game._result, "") self.game.play() self.assertEqual(self.game._result, "Gandalf won the game") def test_game_tie(self): self.game1 = Game("Our first game",\ Player("Sauron", "rock"),\ Player("Gandalf", "rock")) self.game1.play() self.assertEqual(self.game1._result, "None won the game") self.game2 = Game("Our first game",\ Player("Sauron", "paper"),\ Player("Gandalf", "paper")) self.game2.play() self.assertEqual(self.game2._result, "None won the game") self.game3 = Game("Our first game",\ Player("Sauron", "scissors"),\ Player("Gandalf", "scissors")) self.game3.play() self.assertEqual(self.game3._result, "None won the game") def test_game_player1_win(self): self.game1 = Game("Our first game",\ Player("Sauron", "rock"),\ Player("Gandalf", "scissors")) self.game1.play() self.assertEqual(self.game1._result, "Sauron won the game") self.game2 = Game("Our first game",\ Player("Sauron", "paper"),\ Player("Gandalf", "rock")) self.game2.play() self.assertEqual(self.game2._result, "Sauron won the game") self.game3 = Game("Our first game",\ Player("Sauron", "scissors"),\ Player("Gandalf", "paper")) self.game3.play() self.assertEqual(self.game3._result, "Sauron won the game") def test_game_player2_win(self): self.game1 = Game("Our first game",\ Player("Sauron", "scissors"),\ Player("Gandalf", "rock")) self.game1.play() self.assertEqual(self.game1._result, "Gandalf won the game") self.game2 = Game("Our first game",\ Player("Sauron", "rock"),\ Player("Gandalf", "paper")) self.game2.play() self.assertEqual(self.game2._result, "Gandalf won the game") self.game3 = Game("Our first game",\ Player("Sauron", "paper"),\ Player("Gandalf", "scissors")) self.game3.play() self.assertEqual(self.game3._result, "Gandalf won the game")