Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ROWS = 6
- COLUMNS = 7
- FOUR = 4
- INDEX_FIRST_ROW = 0
- INDEX_FIRST_COLUMN = 0
- RED = "R"
- YELLOW = "Y"
- EMPTY = "."
- FIRST_PLAYER = RED
- PLAYER_NAMES = {RED: "red", YELLOW: "yellow"}
- grid = [([EMPTY] * COLUMNS) for i in xrange(0, ROWS)]
- class ColumnFullError(Exception):
- pass
- def get_player_name(player):
- return PLAYER_NAMES[player]
- def print_grid():
- print("".join(["{} ".format(i) for i in xrange(0, COLUMNS)]))
- for row in grid:
- print("".join(["{} ".format(cell) for cell in row]))
- def insert(colour, column):
- if grid[INDEX_FIRST_ROW][column] != EMPTY:
- raise ColumnFullError()
- row = INDEX_FIRST_ROW
- row_try = INDEX_FIRST_ROW
- while (row_try < ROWS and grid[row_try][column] == EMPTY):
- row = row_try
- row_try += 1
- grid[row][column] = colour
- return (row, column)
- def is_column_input_valid(input):
- try:
- column = int(input)
- return INDEX_FIRST_COLUMN <= column and column < COLUMNS
- except ValueError:
- return False
- def get_column_input(player):
- column_str = raw_input("{} player, pick a column: ".format(get_player_name(player).title()))
- while not is_column_input_valid(column_str):
- print("That isn't a column number!")
- column_str = raw_input("{} player, pick a column between {} and {}: ".format(INDEX_FIRST_COLUMN, COLUMNS - 1, get_player_name(player).title()))
- return int(column_str)
- def is_grid_full():
- for row in grid:
- for cell in row:
- if cell == EMPTY:
- return False
- return True
- def check_four_vertical(player, row, column):
- # downwards
- count = 0
- while row < ROWS and grid[row][column] == player:
- count += 1
- if count >= 4:
- return True
- row += 1
- return False
- def check_four_horizontal(player, row, column):
- count_left = 0
- column_l = column - 1
- while column_l >= INDEX_FIRST_COLUMN and grid[row][column_l] == player:
- count_left += 1
- column_l -= 1
- count_right = 0
- column_r = column + 1
- while column_r < COLUMNS and grid[row][column_r] == player:
- count_right += 1
- column_r += 1
- return count_left + count_right + 1 >= 4
- def check_four_posdiag(player, row, column):
- count_left = 0
- column_l = column - 1
- row_l = row - 1
- while column_l >= INDEX_FIRST_COLUMN and row_l >= INDEX_FIRST_ROW and grid[row_l][column_l] == player:
- count_left += 1
- column_l -= 1
- row_l -= 1
- count_right = 0
- column_r = column + 1
- row_r = row + 1
- while column_r < COLUMNS and row_r < ROWS and grid[row][column_r] == player:
- count_right += 1
- column_r += 1
- row_r += 1
- return count_left + count_right + 1 >= 4
- def check_four_negdiag(player, row, column):
- count_left = 0
- column_l = column - 1
- row_l = row + 1
- while column_l >= INDEX_FIRST_COLUMN and row_l < ROWS and grid[row_l][column_l] == player:
- count_left += 1
- column_l -= 1
- row_l += 1
- count_right = 0
- column_r = column + 1
- row_r = row - 1
- while column_r < COLUMNS and row_r >= INDEX_FIRST_ROW and grid[row][column_r] == player:
- count_right += 1
- column_r += 1
- row_r -= 1
- return count_left + count_right + 1 >= 4
- def has_won(player, row, column):
- return check_four_vertical(player, row, column) or check_four_horizontal(player, row, column) or check_four_posdiag(player, row, column) or check_four_negdiag(player, row, column)
- def main():
- player = FIRST_PLAYER
- while True:
- if is_grid_full():
- print("It's a draw!")
- return
- column = get_column_input(player)
- try:
- (row, column) = insert(player, column)
- print("")
- print_grid()
- print("")
- if has_won(player, row, column):
- print("{} has won!".format(get_player_name(player).title()))
- return
- except ColumnFullError:
- print("Column {} is full! Pick a different column.".format(column))
- continue
- player = RED if player == YELLOW else YELLOW
- main()
Add Comment
Please, Sign In to add comment