Guest User

Untitled

a guest
Feb 19th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. DOT = '.'
  2. X = 'X'
  3. O = 'O'
  4.  
  5.  
  6. class Board(object):
  7. def __init__(self, cols=7, rows=6):
  8. self.cols = cols
  9. self.rows = rows
  10. self.board = list([[DOT] * rows for void in range(cols)])
  11.  
  12. def display(self):
  13. print(' ', ' '.join(map(str, range(self.cols))))
  14.  
  15. for y in range(self.rows):
  16. print(y, ' '.join(str(self.board[x][y]) for x in range(self.cols)))
  17.  
  18. def insert(self, column, coin):
  19. col = self.board[column]
  20.  
  21. if col[0] != DOT:
  22. raise Exception('Full column, choose another.')
  23.  
  24. i = int(self.rows) - 1
  25. for row in range(self.rows):
  26. if col[i] == DOT:
  27. col[i] = coin
  28. break
  29. else:
  30. i -= 1
  31.  
  32. self.check_winner()
  33. self.display()
  34. print('\n')
  35.  
  36.  
  37. def check_winner(self):
  38. for row in list(zip(*self.board)):
  39. count = 0
  40. temp = row[0]
  41. for item in row:
  42. if item == temp:
  43. count += 1
  44. temp = item
  45. if count >= 4 and item != '.':
  46. raise Exception('Winner is: ', item)
  47.  
  48. else:
  49. count = 0
  50. temp = item
  51.  
  52. for col in list(self.board):
  53. count = 0
  54. temp = col[0]
  55. for item in col:
  56. if item == temp:
  57. count += 1
  58. temp = item
  59. if count >= 4 and item != '.':
  60. raise Exception('Winner is: ', item)
  61.  
  62. else:
  63. count = 0
  64. temp = item
  65.  
  66.  
  67. if __name__ == '__main__':
  68.  
  69. game = Board(7, 6)
  70. player = X
  71. while True:
  72. game.display()
  73. column = input('Player {} enter column:'.format(X if player == X else O))
  74. game.insert(int(column), player)
  75. player = O if player == X else X
Add Comment
Please, Sign In to add comment