# #!/usr/bin/env Y if turn == R else

#!/usr/bin/env python2

# -*- coding: utf-8 -*-

“””

Created on Wed Nov 8 15:05:00 2017

“””

def D_Neg (table, columns, rows):

“””Showing negative diagonals, going from top-left to bottom-right.”””

for di in ((j, i – columns + j + 1) for j in range(columns) for i in range(columns + rows – 1)):

yield tableij for i, j in di if i >= 0 and j >= 0 and i < columns and j < rows def D_Pos (table, columns, rows): """Showing positive diagonals, going from bottom-left to top-right.""" for di in ((j, i - j) for j in range(columns) for i in range(columns + rows -1)): yield tableij for i, j in di if i >= 0 and j >= 0 and i < columns and j < rows """Specifications.""" R = 'R'; Y = 'Y'; P = '=' class Connect4: def __init__ (C4, columns = 7, rows = 6, win = 4): """Create a new Connect4.""" C4.columns = columns C4.rows = rows C4.win = win C4.game = P * rows for _ in range(columns) def insert (C4, column, color): """Getting the color in the given column.""" c = C4.gamecolumn if c1 != P: raise Exception('Column is full') i = -1 while ci != P: i -= 1 ci = color C4.Win() def Win (C4): """Checking for game winner.""" w = C4.winner() if w: C4.showGame() raise Exception(w + ' won!') def winner (C4): """Getting the game winner.""" lns = ( C4.game, # columns zip(*C4.game), # rows D_Pos(C4.game, C4.columns, C4.rows), # positive diagonals D_Neg(C4.game, C4.columns, C4.rows) # negative diagonals ) for ln in chain(*lns): for color, group in groupby(ln): if color != P and len(list(group)) >= C4.win:

return color

def showGame (C4):

“””Printing the game.”””

print(‘ ‘.join(map(str, range(C4.columns))))

for y_axis in range(C4.rows):

print(‘ ‘.join(str(C4.gamex_axisy_axis) for x_axis in range(C4.columns)))

print()

from itertools import groupby, chain

if __name__ == ‘__main__’:

g = Connect4()

turn = R

while True:

g.showGame()

row = input(‘{}’s turn: ‘.format(‘R’ if turn == R else ‘Y’))

g.insert(int(row), turn)

turn = Y if turn == R else R

Go Top