In the program hangman.py we learned how to slice a string and indexing in the previous assignments, pig latin and swapping letters.
The Connect 4 program can be written using strings.
Here is a little help:
# Using strings:
connect_board = '''|_|_|_|_|_|_|
|_|_|_|_|_|_|
|_|_|_|_|_|_|
|_|_|_|_|_|_|
|_|_|_|_|_|_|'''
# what do the indices look like?
# | 1 | 3 | 5 | 7 | 9 | 11 |
# | 15 | 17 | 19 | 21 | 23 | 25 |
# | 29 | 31 | 33 | 35 | 37 | 39 |
# | 43 | 45 | 47 | 49 | 51 | 53 |
# | 57 | 59 | 61 | 63 | 65 | 67 |
# | 1 | 2 | 3 | 4 | 5 | 6 |
In the last row you have the column numbers.
print('Empty board: ')
print(connect_board)
print( ' 1 2 3 4 5 6 ')
print('\nThe length of the string is ',len(connect_board))
print('')
yesNo = 'y'
while yesNo == 'y':
column = int(input("Enter the column number "))
offset = 1
if column == 2:
offset = 3
elif column == 3:
offset = 5
elif column == 4:
offset = 7
elif column == 5:
offset = 9
elif column == 6:
offset = 11
empty = True
position = offset + (14 * 4)
while empty:
if connect_board[position] == '_':
connect_board = connect_board[:position] + 'A' + connect_board[position + 1:]
print(connect_board)
print( ' 1 2 3 4 5 6 ')
empty = False
else:
position -= 14
yesNo = input('More? ')
A test run could look like this:
Empty board: |_|_|_|_|_|_| |_|_|_|_|_|_| |_|_|_|_|_|_| |_|_|_|_|_|_| |_|_|_|_|_|_| 1 2 3 4 5 6 Enter the column number 1 |_|_|_|_|_|_| |_|_|_|_|_|_| |_|_|_|_|_|_| |_|_|_|_|_|_| |A|_|_|_|_|_| 1 2 3 4 5 6 More? y Enter the column number 1 |_|_|_|_|_|_| |_|_|_|_|_|_| |_|_|_|_|_|_| |A|_|_|_|_|_| |A|_|_|_|_|_| 1 2 3 4 5 6 More? y Enter the column number 4 |_|_|_|_|_|_| |_|_|_|_|_|_| |_|_|_|_|_|_| |A|_|_|_|_|_| |A|_|_|A|_|_| 1 2 3 4 5 6 More? y Enter the column number 6 |_|_|_|_|_|_| |_|_|_|_|_|_| |_|_|_|_|_|_| |A|_|_|_|_|_| |A|_|_|A|_|A| 1 2 3 4 5 6 More? y Enter the column number 5 |_|_|_|_|_|_| |_|_|_|_|_|_| |_|_|_|_|_|_| |A|_|_|_|_|_| |A|_|_|A|A|A| 1 2 3 4 5 6 More? y Enter the column number 2 |_|_|_|_|_|_| |_|_|_|_|_|_| |_|_|_|_|_|_| |A|_|_|_|_|_| |A|A|_|A|A|A| 1 2 3 4 5 6 More? y Enter the column number 1 |_|_|_|_|_|_| |_|_|_|_|_|_| |A|_|_|_|_|_| |A|_|_|_|_|_| |A|A|_|A|A|A| 1 2 3 4 5 6 More? y Enter the column number 3 |_|_|_|_|_|_| |_|_|_|_|_|_| |A|_|_|_|_|_| |A|_|_|_|_|_| |A|A|A|A|A|A| 1 2 3 4 5 6 More?
Homework:
Write the pseudocode for the “Connect 4” program. Check edmodo.com for a little help on the pseudocode.
