Guest User

Untitled

a guest
Mar 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. ROWS = 6
  2. COLUMNS = 7
  3.  
  4. FOUR = 4
  5.  
  6. INDEX_FIRST_ROW = 0
  7. INDEX_FIRST_COLUMN = 0
  8.  
  9. RED = "R"
  10. YELLOW = "Y"
  11. EMPTY = "."
  12.  
  13. FIRST_PLAYER = RED
  14. PLAYER_NAMES = {RED: "red", YELLOW: "yellow"}
  15.  
  16. grid = [([EMPTY] * COLUMNS) for i in xrange(0, ROWS)]
  17.  
  18.  
  19. class ColumnFullError(Exception):
  20. pass
  21.  
  22.  
  23. def get_player_name(player):
  24. return PLAYER_NAMES[player]
  25.  
  26.  
  27. def print_grid():
  28. print("".join(["{} ".format(i) for i in xrange(0, COLUMNS)]))
  29. for row in grid:
  30. print("".join(["{} ".format(cell) for cell in row]))
  31.  
  32.  
  33. def insert(colour, column):
  34. if grid[INDEX_FIRST_ROW][column] != EMPTY:
  35. raise ColumnFullError()
  36. row = INDEX_FIRST_ROW
  37.  
  38. row_try = INDEX_FIRST_ROW
  39. while (row_try < ROWS and grid[row_try][column] == EMPTY):
  40. row = row_try
  41. row_try += 1
  42.  
  43. grid[row][column] = colour
  44. return (row, column)
  45.  
  46.  
  47. def is_column_input_valid(input):
  48. try:
  49. column = int(input)
  50. return INDEX_FIRST_COLUMN <= column and column < COLUMNS
  51. except ValueError:
  52. return False
  53.  
  54.  
  55. def get_column_input(player):
  56. column_str = raw_input("{} player, pick a column: ".format(get_player_name(player).title()))
  57. while not is_column_input_valid(column_str):
  58. print("That isn't a column number!")
  59. column_str = raw_input("{} player, pick a column between {} and {}: ".format(INDEX_FIRST_COLUMN, COLUMNS - 1, get_player_name(player).title()))
  60. return int(column_str)
  61.  
  62.  
  63. def is_grid_full():
  64. for row in grid:
  65. for cell in row:
  66. if cell == EMPTY:
  67. return False
  68. return True
  69.  
  70.  
  71. def check_four_vertical(player, row, column):
  72. # downwards
  73. count = 0
  74. while row < ROWS and grid[row][column] == player:
  75. count += 1
  76. if count >= 4:
  77. return True
  78. row += 1
  79. return False
  80.  
  81.  
  82.  
  83. def check_four_horizontal(player, row, column):
  84. count_left = 0
  85. column_l = column - 1
  86. while column_l >= INDEX_FIRST_COLUMN and grid[row][column_l] == player:
  87. count_left += 1
  88. column_l -= 1
  89.  
  90. count_right = 0
  91. column_r = column + 1
  92. while column_r < COLUMNS and grid[row][column_r] == player:
  93. count_right += 1
  94. column_r += 1
  95.  
  96. return count_left + count_right + 1 >= 4
  97.  
  98.  
  99. def check_four_posdiag(player, row, column):
  100. count_left = 0
  101. column_l = column - 1
  102. row_l = row - 1
  103. while column_l >= INDEX_FIRST_COLUMN and row_l >= INDEX_FIRST_ROW and grid[row_l][column_l] == player:
  104. count_left += 1
  105. column_l -= 1
  106. row_l -= 1
  107.  
  108. count_right = 0
  109. column_r = column + 1
  110. row_r = row + 1
  111. while column_r < COLUMNS and row_r < ROWS and grid[row][column_r] == player:
  112. count_right += 1
  113. column_r += 1
  114. row_r += 1
  115.  
  116. return count_left + count_right + 1 >= 4
  117.  
  118.  
  119. def check_four_negdiag(player, row, column):
  120. count_left = 0
  121. column_l = column - 1
  122. row_l = row + 1
  123. while column_l >= INDEX_FIRST_COLUMN and row_l < ROWS and grid[row_l][column_l] == player:
  124. count_left += 1
  125. column_l -= 1
  126. row_l += 1
  127.  
  128. count_right = 0
  129. column_r = column + 1
  130. row_r = row - 1
  131. while column_r < COLUMNS and row_r >= INDEX_FIRST_ROW and grid[row][column_r] == player:
  132. count_right += 1
  133. column_r += 1
  134. row_r -= 1
  135.  
  136. return count_left + count_right + 1 >= 4
  137.  
  138.  
  139. def has_won(player, row, column):
  140. 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)
  141.  
  142.  
  143. def main():
  144. player = FIRST_PLAYER
  145.  
  146. while True:
  147. if is_grid_full():
  148. print("It's a draw!")
  149. return
  150.  
  151. column = get_column_input(player)
  152. try:
  153. (row, column) = insert(player, column)
  154. print("")
  155. print_grid()
  156. print("")
  157.  
  158. if has_won(player, row, column):
  159. print("{} has won!".format(get_player_name(player).title()))
  160. return
  161.  
  162.  
  163. except ColumnFullError:
  164. print("Column {} is full! Pick a different column.".format(column))
  165. continue
  166. player = RED if player == YELLOW else YELLOW
  167.  
  168.  
  169. main()
Add Comment
Please, Sign In to add comment