-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollers.py
66 lines (44 loc) · 1.48 KB
/
controllers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from models import Game
import views as view
"""
The controller handles all user input.
It delegates all game logic to the model.
All display content lives in the view.
"""
class GameController:
def __init__(self):
self.game = Game()
def run(self):
print(view.intro())
while not self.game.is_over():
first = self.first_input()
if not first: continue
second = self.second_input(first)
if not second: continue
self.game.roll(first)
self.game.roll(second)
self.print_board()
print(view.game_over())
def first_input(self):
first = input(view.enter_first())
if not first.isdigit() or int(first) not in range(0, 11):
print(view.error_invalid_input())
return False
first = int(first)
# skip second roll for strikes and the fill ball
if first == 10 or len(self.game.results()) == 10:
self.game.roll(first)
self.print_board()
return False
return first
def second_input(self, first):
second = input(view.enter_second())
if not second.isdigit() or int(second) not in range(0, 11 - first):
print(view.error_invalid_second(10 - first))
return False
return int(second)
def print_board(self):
results = self.game.results()
print(view.score_board(results))