Uploaded by Marsel Nasr

# submission (17)

```Contents
1 lab4.py
2
1
1 lab4.py
1
2
3
4
5
6
7
8
9
10
11
NUMBER_OF_ROWS = 5
LENGTH = 4
MATCH = '|'
PLAYER_ONE = 1
PLAYER_TWO = 2
MESSAGE_ROW_NUM_REQ = 'Please enter row number:'
MESSAGE_AMOUNT_REQ = 'Please enter the amount to take from row number {}:'
ERROR_NO_SUCH_ROW = 'Illegal row number'
ERROR_ILLEGAL_AMOUNT = 'Illegal amount chosen'
WINNER_MESSAGE = 'The Winner is ...PLAYER {}!!'
TURN_MESSAGE = 'Player {}, Its your turn'
12
13
14
15
16
def init_board():
my_board = [LENGTH] * NUMBER_OF_ROWS
return my_board
17
18
19
20
def get_next_player(current_player):
return PLAYER_ONE if current_player == PLAYER_TWO else PLAYER_TWO
21
22
23
def print_board(board):
24
25
26
27
for i in range(NUMBER_OF_ROWS):
print(str(i), ':', end='\t')
print(MATCH * board[i])
28
29
30
31
32
33
34
def is_board_empty(board):
for i in range(NUMBER_OF_ROWS):
if board[i] &gt; 0:
return False
return True
35
36
37
38
39
40
41
42
43
44
def get_input(board):
row_number = int(input(MESSAGE_ROW_NUM_REQ))
while not check_row_number_validity(board, row_number):
print(ERROR_NO_SUCH_ROW)
row_number = int(input(MESSAGE_ROW_NUM_REQ))
amount = int(input(MESSAGE_AMOUNT_REQ))
while not check_amount_taken(board, row_number, amount):
print(ERROR_ILLEGAL_AMOUNT)
45
46
amount = int(input(MESSAGE_AMOUNT_REQ))
47
48
return row_number, amount
49
50
51
52
53
def check_row_number_validity(board, row_number):
if board[row_number] != 0:
return 0 &lt;= row_number &lt; NUMBER_OF_ROWS
54
55
56
57
def check_amount_taken(board, row_number, amount):
return 0 &lt; amount &lt;= board[row_number]
58
59
2
60
61
62
def update_board(board, row_number, amount):
board[row_number] -= amount
return board
63
64
65
66
67
68
69
70
71
72
73
74
75
def run_game():
my_board = init_board()
current_player = PLAYER_TWO
while not is_board_empty(my_board):
current_player = get_next_player(current_player)
print(TURN_MESSAGE.format(current_player))
print_board(my_board)
row_number, amount = get_input(my_board)
my_board = update_board(my_board, row_number, amount)
print_board(my_board)
print(WINNER_MESSAGE.format(current_player))
76
77
78
run_game()
79
80
3
```