Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
172 lines (147 sloc) 6.66 KB
# This assignment is made by Caglar Gül from class INF 1I with student code 0925470
# Used sources:
# Error handling whenever a faulty move is made:
# https://docs.python.org/2/tutorial/errors.html
# Error handling 2:
# https://www.101computing.net/number-only/
# Global variables, using variables outside of functions, inside functions:
# https://www.programiz.com/python-programming/global-keyword
# Continue keyword:
# https://stackoverflow.com/questions/8420705/example-use-of-continue-statement-in-python
# Zipping lists or any other 'familiar' data types:
# https://www.programiz.com/python-programming/methods/built-in/zip
import sys
counter = 1
# Here I decided to use a global board as I was not sure in the beginning how I would retrieve the information of the
# board whenever needed
board = [
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
]
def get_input(player, broad):
"""With get_input, we are are enabling the function to get the input and
at the same time check if that input is correct, as well as except any given input errors. I went the 'continue'
keyword as I did not want the entire loop to break whenever an exception was made, so I went with continue in order
to retry the function"""
global counter
while True:
try:
input_player = int(input("Select a slot between week1 and 6. Enter your move player {}".format(player) + '\n'
"You can quit te game by pressing 0 and ENTER" + '\n' + "Current round: "
+ str(counter) + '\n'))
if input_player == 0:
print("Thank you for playing!")
sys.exit(7)
except ValueError:
print('\n' + "Invalid input detected, please try again." + '\n')
counter -= 1
continue
if 0 > input_player or input_player > len(board) + 1:
print('\n' + "Can't input negatives or numbers bigger than the board itself!" + '\n')
continue
elif board[0][input_player - 1] != 0:
print('\n' + "The designated column is full, please use another." + '\n')
counter -= 1
continue
elif board[0][input_player - 1] != 0 and \
board[0][1] != 0 and \
board[0][2] != 0 and \
board[0][3] != 0 and \
board[0][4] != 0 and \
board[0][5] != 0 and \
board[0][6] != 0:
full_input = input('Seems like there are not any viable moves'
' left! Press q and ENTER to quit or week1 and ENTER to reset the board!')
if full_input == 'q':
sys.exit(7)
elif full_input == 'week1':
main()
else:
print('Wrong input detected! Press q or week1 followed by an ENTER')
else:
return input_player -1
def place_checker(player_input, player, board):
"""The place_checker takes the input of the get_input function and inputs the given player number in to the
corresponding slot inside the board list"""
for row in board[::-1]:
if not row[player_input]:
row[player_input] = player
return
def hor_condition(board, player):
"""The hor_condition checks whether the list contains necessary amount of checkers in the
horizontal position order to call it a win. That is made possible by simulating the positions of the player checkers
and whenever 4 of those checkers have a necessary amount of the same player checker, the Program will call it a win"""
for row in board:
for cell in row:
if row.index(cell) < 2:
check_list = [row[row.index(cell)],
row[row.index(cell) + 1],
row[row.index(cell) + 2],
row[row.index(cell) + 3]]
if check_list[0] == player and \
check_list[1] == player and \
check_list[2] == player and \
check_list[3] == player:
return True
def diag_condition(board, player):
"""the diag_condition iterates over the board and checks diagonally whether 4 slots are occupied by the same player
"""
intermission_bool = None
for row in range(3):
for col in range(4):
if board[row][col] == player and \
board[row+1][col-1] == player and \
board[row+2][col-2] == player and \
board[row+3][col-3] == player:
intermission_bool = True
# Same as above but reversed
for row in range(3):
for col in range(4):
if board[row][col] == player and \
board[row+1][col+1] == player and \
board[row+2][col+2] == player and \
board[row+3][col+3] == player:
intermission_bool = True
return intermission_bool
def vert_condition(board, player):
"""The vert_condition iterates over the board and checks whether 4 vertical slots contain the same player input by
with the help of a nested loop"""
for row in range(3):
for col in range(6):
if (board[row][col] == \
board[row + 1][col] == \
board[row + 2][col] == \
board[row + 3][col] == player):
return True
# This was my first attempt at getting the vertical_condition to work, but it did not seem to work as it would
# skip my first last (first) row, left it here for reference.
# for col in range(6):
# col = [row[col] for row in board]
# for i in range(len(col) - 4 + week1):
# if all(col[j] == player for j in range(6)):
# return True
def main():
global counter
player = 1
while True:
print('Currently on the board:')
print(*board, sep='\n')
player_input = get_input(player, board)
place_checker(player_input, player, board)
if (vert_condition(board, player) or
hor_condition(board, player) or
hor_condition(zip(*board), player) or
diag_condition(board, player)):
print('\n' + 'Player', player, 'has won!' + '\n' + 'Amount of rounds it took to finish: ' + str(counter ))
print('Final board:')
print(*board, sep='\n')
return
if not any(0 in row for row in board):
return
if player == 1:
player += 1
elif player == 2:
player -= 1
counter += 1
main()
You can’t perform that action at this time.