Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.20 KB | None | 0 0
  1.  
  2. import random
  3. import time
  4.  
  5.  
  6. def print_board(board):
  7. # board is a list of current pieces? or whatever
  8.  
  9. top = u'\u2554' + (u'\u2550\u2550\u2550\u2564' * 6) +\
  10. u'\u2550\u2550\u2550\u2557'
  11.  
  12. bottom = u'\u2569' + (u'\u2550\u2550\u2550\u2567' * 6) +\
  13. u'\u2550\u2550\u2550\u2569'
  14.  
  15. upright = ' ' + u'\u2502' + ' '
  16.  
  17. row_six = u'\u2551 ' + board[36] + upright + board[37] + upright + \
  18. board[38] + upright + board[39] + upright + board[40] + upright + \
  19. board[41] + upright + board[42] + u' \u2551'
  20.  
  21. row_five = u'\u2551 ' + board[29] + upright + board[30] + upright + \
  22. board[31] + upright + board[32] + upright + board[33] + upright + \
  23. board[34] + upright + board[35] + u' \u2551'
  24.  
  25. row_four = u'\u2551 ' + board[22] + upright + board[23] + upright + \
  26. board[24] + upright + board[25] + upright + board[26] + upright + \
  27. board[27] + upright + board[28] + u' \u2551'
  28.  
  29. row_three = u'\u2551 ' + board[15] + upright + board[16] + upright + \
  30. board[17] + upright + board[18] + upright + board[19] + upright + \
  31. board[20] + upright + board[21] + u' \u2551'
  32.  
  33. row_two = u'\u2551 ' + board[8] + upright + board[9] + upright + \
  34. board[10] + upright + board[11] + upright + board[12] + upright + \
  35. board[13] + upright + board[14] + u' \u2551'
  36.  
  37. row_one = u'\u2551 ' + board[1] + upright + board[2] + upright + \
  38. board[3] + upright + board[4] + upright + board[5] + upright + \
  39. board[6] + upright + board[7] + u' \u2551'
  40.  
  41. row_divider = u'\u255f' + (u'\u2500\u2500\u2500\u253c' * 6) +\
  42. u'\u2500\u2500\u2500\u2562'
  43.  
  44. banner = chr(27) + '[31m' + 'C ' + chr(27) + '[33m' + 'O ' + \
  45. chr(27) + '[31m' + 'N ' + chr(27) + '[33m' + 'N ' + \
  46. chr(27) + '[31m' + 'E ' + chr(27) + '[33m' + 'C ' + \
  47. chr(27) + '[31m' + 'T' + ' ' + chr(27) + '[33m' + 'F ' + \
  48. chr(27) + '[31m' + 'O ' + chr(27) + '[33m' + 'U ' + \
  49. chr(27) + '[31m' + 'R ' + chr(27) + '[0m'
  50.  
  51. # make this text red and yellow
  52. print ' ' + banner
  53. print ""
  54. print " 1 2 3 4 5 6 7"
  55. print chr(27) + '[34m' + ' ' + top
  56. print ' ' + row_six
  57. print ' ' + row_divider
  58. print ' ' + row_five
  59. print ' ' + row_divider
  60. print ' ' + row_four
  61. print ' ' + row_divider
  62. print ' ' + row_three
  63. print ' ' + row_divider
  64. print ' ' + row_two
  65. print ' ' + row_divider
  66. print ' ' + row_one
  67. print ' ' + u'\u2550' + bottom + u'\u2550' + chr(27) + '[0m'
  68. print ""
  69. #print "Pick a row to drop your game piece."
  70.  
  71.  
  72. def blank_and_draw():
  73. # blank screen and re-draw board
  74. print chr(27) + "[2J"
  75. print_board(board)
  76.  
  77.  
  78. def choose_color():
  79. # ask player which color they want to be
  80. # return player_color
  81. color = ""
  82. while color == "":
  83. blank_and_draw()
  84. print ""
  85. print "Which color do you want to be? (red or yellow)"
  86. color = raw_input("> ")
  87. if color.lower().startswith('r'):
  88. return 'red'
  89. elif color.lower().startswith('y'):
  90. return 'yellow'
  91. else:
  92. color = ""
  93.  
  94.  
  95. def goes_first():
  96. if random.randint(0, 1) == 0:
  97. return 'player'
  98. else:
  99. return 'computer'
  100.  
  101.  
  102. def is_space_open(board, column):
  103. # checks if there is room in a column to drop a piece
  104. # returns index where piece will end up
  105. # or not, if full
  106. index = column
  107. while True:
  108. if index > (column + 35):
  109. return 'full'
  110.  
  111. if board[index] == u'\u25ef':
  112. return index
  113. else:
  114. index += 7
  115. continue
  116.  
  117.  
  118. def get_player_move():
  119. """
  120. Get move, check validity, return index piece ends up in
  121. """
  122. column = 'x'
  123. while str(column) not in "1 2 3 4 5 6 7".split():
  124. blank_and_draw()
  125. print ""
  126. print ""
  127. print "Select a column to drop into. (1-7)"
  128. column = raw_input("> ")
  129. if column not in "1 2 3 4 5 6 7".split():
  130. blank_and_draw()
  131. print ""
  132. print ""
  133. print "Please enter a number between 1 and 7."
  134. continue
  135. else:
  136. if is_space_open(board, int(column)) == 'full':
  137. blank_and_draw()
  138. print ""
  139. print ""
  140. print "Column full! Pick another."
  141. column = 'x'
  142. time.sleep(1.5)
  143. continue
  144. else:
  145. break
  146.  
  147. return is_space_open(board, int(column))
  148.  
  149.  
  150. def get_computer_move():
  151. # make this random for now
  152. # return index of computer's piece
  153. while True:
  154. move = random.randint(1, 7)
  155. if is_space_open(board, move) == 'full':
  156. continue
  157. else:
  158. return is_space_open(board, move)
  159.  
  160.  
  161. def check_up(each, positions):
  162. # check if position + 3 above it make a win
  163. # each -- int index of place we're checking
  164. # positions -- list of places where a player's pieces are (int)
  165. if (each + 7) in positions and (each + 14) in positions and \
  166. (each + 21) in positions:
  167. print "won upwards of %d" % each
  168. return True
  169. else:
  170. return False
  171.  
  172.  
  173. def check_right(each, positions):
  174. # check if position + 3 to its right make a win
  175. # each -- int index of place we're checking
  176. # positions -- list of places where a player's pieces are (int)
  177. if (each + 1) in positions and (each + 2) in positions and \
  178. (each + 3) in positions:
  179. print "won to the right of %d" % each
  180. return True
  181. else:
  182. return False
  183.  
  184.  
  185. def check_up_right(each, positions):
  186. # stuff
  187. if (each + 8) in positions and (each + 16) in positions and \
  188. (each + 24) in positions:
  189. print "won up and to the right of %d" % each
  190. return True
  191. else:
  192. return False
  193.  
  194.  
  195. def check_up_left(each, positions):
  196. # stuff
  197. if (each + 6) in positions and (each + 12) in positions and \
  198. (each + 18) in positions:
  199. print "won up and to the left of %d" % each
  200. return True
  201. else:
  202. return False
  203.  
  204.  
  205. def see_if_won(board, player_color):
  206. # check player's positions for a win
  207. # return boolean
  208. # first, find all of player's pieces
  209.  
  210. index = 0 # instead of '0', actually '0'
  211. #print "board: ", board
  212. #print '%r' % red
  213. #print '%r' % player_color
  214. positions = []
  215. for space in board:
  216. if space == player_color:
  217. #print "player pieces: ", index
  218. positions.append(index)
  219. index += 1
  220. else:
  221. index += 1
  222.  
  223. #print "positions: ", positions
  224. for each in positions: # each: integer index of piece
  225. for way in possible_wins[each]:
  226. if way == 'above':
  227. if check_up(each, positions):
  228. return True
  229. elif way == 'right':
  230. if check_right(each, positions):
  231. return True
  232. elif way == 'up_right':
  233. if check_up_right(each, positions):
  234. return True
  235. elif way == 'up_left':
  236. if check_up_left(each, positions):
  237. return True
  238. else:
  239. continue
  240.  
  241. return False
  242.  
  243.  
  244. def play_again():
  245. while True:
  246. blank_and_draw()
  247. print ""
  248. print "Do you wish to play again? (yes or no)"
  249. again = raw_input("> ")
  250. if again.lower().startswith('y'):
  251. return True
  252. elif again.lower().startswith('n'):
  253. return False
  254. else:
  255. continue
  256.  
  257.  
  258. def flip_anim():
  259. for x in range(1, 4):
  260. blank_and_draw()
  261. print ""
  262. print ""
  263. print "Flipping a coin to see who goes first" + ('.' * x)
  264. time.sleep(0.8)
  265. return
  266.  
  267.  
  268. # AI routine below
  269. def go_for_win(board, computer_color):
  270. # play as computer, test drop into each slot and pick the first one
  271. # that leads to a win
  272.  
  273. for slot in range(1, 8): # columns 1-7
  274. test_board = []
  275. test_board = test_board + board # duplicate the board
  276.  
  277. if is_space_open(test_board, slot) == 'full':
  278. continue
  279. else:
  280. move = is_space_open(test_board, slot)
  281. test_board[move] = computer_color
  282.  
  283. # now test if it's a win:
  284. if see_if_won(test_board, computer_color):
  285. return move
  286. else:
  287. continue
  288.  
  289. # no winning moves found
  290. return False
  291.  
  292.  
  293. def block_player(board, player_color):
  294. # play as player, test drop into each slot and pick the first one
  295. # that leads to a win, essentially blocking the player
  296.  
  297. for slot in range(1, 8): # columns 1-7
  298. test_board = []
  299. test_board = test_board + board # duplicate the board
  300.  
  301. if is_space_open(test_board, slot) == 'full':
  302. continue
  303. else:
  304. move = is_space_open(test_board, slot)
  305. test_board[move] = player_color
  306.  
  307. # now test if it's a win:
  308. if see_if_won(test_board, player_color):
  309. return move
  310. else:
  311. continue
  312.  
  313. # no blocking moves found
  314. return False
  315.  
  316.  
  317. def dont_screw_it(board, player_color, computer_color):
  318. # play a piece in each slot, then playing the player's color in
  319. # the same, if it leads to a win for the player, dont play that slot
  320. # in other words, avoid throwing the game
  321.  
  322. bad_moves = []
  323. # first, test drop computer pieces
  324. for slot in range(1, 8): # columns 1-7
  325. test_board = []
  326. test_board = test_board + board # duplicate the board
  327.  
  328. if is_space_open(test_board, slot) == 'full':
  329. continue
  330. else:
  331. comp_move = is_space_open(test_board, slot)
  332. test_board[comp_move] = computer_color
  333.  
  334. # now drop player piece in the same slot
  335. if is_space_open(test_board, slot) == 'full':
  336. continue
  337. else:
  338. move = is_space_open(test_board, slot)
  339. test_board[move] = player_color
  340.  
  341. # now test if it's a win, then don't play it:
  342. if see_if_won(test_board, player_color):
  343. bad_moves.append(comp_move)
  344. else:
  345. continue
  346.  
  347. print "bad moves: ", bad_moves
  348. loop_count = 1 # avoid infinite loops
  349. while True: # need test if empty or not
  350. if loop_count > 7:
  351. return get_computer_move() # give up and pick one at random
  352. else:
  353. new_move = random.randint(1, 7)
  354. if is_space_open(board, new_move) in bad_moves:
  355. loop_count += 1
  356. continue
  357. elif is_space_open(board, new_move) == 'full':
  358. loop_count += 1
  359. continue
  360. else:
  361. return is_space_open(board, new_move)
  362.  
  363. ################################# setup #######################################
  364.  
  365. # {position:[ways you can connect 4 from numbered position]
  366. possible_wins = {1:['above', 'right', 'up_right'], \
  367. 2:['above', 'right', 'up_right'], 3:['above', 'right', 'up_right'], \
  368. 4:['above', 'right', 'up_left', 'up_right'], 5:['above', 'up_left'], \
  369. 6:['above', 'up_left'], 7:['above', 'up_left'], \
  370. 8:['above', 'right', 'up_right'], 9:['above', 'right', 'up_right'], \
  371. 10:['above', 'right', 'up_right'], \
  372. 11:['above', 'right', 'up_left', 'up_right'], 12:['above', 'up_left'], \
  373. 13:['above', 'up_left'], 14:['above', 'up_left'], \
  374. 15:['above', 'right', 'up_right'], 16:['above', 'right', 'up_right'], \
  375. 17:['above', 'right', 'up_right'], \
  376. 18:['above', 'right', 'up_left', 'up_right'], 19:['above', 'up_left'], \
  377. 20:['above', 'up_left'], 21:['above', 'up_left'], 22:['right'], 23:['right'], \
  378. 24:['right'], 25:['right'], 29:['right'], 30:['right'], 31:['right'], \
  379. 32:['right'], 36:['right'], 37:['right'], 38:['right'], 39:['right'], \
  380. 26:['null'], 27:['null'], 28:['null'], 33:['null'], 34:['null'], \
  381. 35:['null'], 40:['null'], 41:['null'], 42:['null'], }
  382.  
  383. red = chr(27) + '[31m' + u'\u25cf' + chr(27) + '[34m'
  384. yellow = chr(27) + '[33m' + u'\u25cf' + chr(27) + '[34m'
  385.  
  386. ############################## main game loop #################################
  387.  
  388. while True:
  389. board = (u'\u25ef ' * 43).split() # reset board
  390.  
  391. print chr(27) + "[2J"
  392. print " W E L C O M E T O "
  393. print ""
  394.  
  395. print_board(board)
  396. print ""
  397.  
  398. time.sleep(2)
  399.  
  400. if choose_color() == 'red':
  401. blank_and_draw()
  402. print "OK. Player will be " + chr(27) + '[31m' + "red" + chr(27) + \
  403. '[0m' + ','
  404. print "and Computer will be " + chr(27) + '[33m' + "yellow" + \
  405. chr(27) + '[0m' + '.'
  406. player_color = red
  407. computer_color = yellow
  408. else:
  409. blank_and_draw()
  410. print "OK. Player will be " + chr(27) + '[33m' + "yellow" + chr(27) + \
  411. '[0m' + ','
  412. print "and Computer will be " + chr(27) + '[31m' + "red" + chr(27) + \
  413. '[0m' + '.'
  414. player_color = yellow
  415. computer_color = red
  416.  
  417. time.sleep(3)
  418.  
  419. flip_anim() # show progress while 'coin flip' happens
  420.  
  421. blank_and_draw()
  422. print ""
  423. print ""
  424. if goes_first() == 'player':
  425. print "The Player has first move."
  426. next_move = 'player'
  427. time.sleep(2.5)
  428. else:
  429. print "The Computer has first move."
  430. next_move = 'computer'
  431. time.sleep(2.5)
  432.  
  433. game_won = False
  434. turn = 1
  435. # play loop
  436. while game_won == False:
  437. if turn == 42: # out of moves
  438. blank_and_draw()
  439. print ""
  440. print "No more moves! It's a draw."
  441. break
  442.  
  443. if next_move == 'player':
  444. move = get_player_move()
  445. board[move] = player_color
  446. #print board
  447. # check for win
  448. if see_if_won(board, player_color) == True:
  449. blank_and_draw()
  450. print ""
  451. print "Four in a row. You win!"
  452. time.sleep(3.5)
  453. game_won = True
  454. else:
  455. #print "not yet."
  456. next_move = 'computer'
  457. turn += 1
  458. continue
  459. else: # computer's move
  460. # change this to check if board[4] is empty, or not, idk
  461. if turn == 1: # first turn
  462. move = 4 # take the middle
  463. else:
  464. if go_for_win(board, computer_color) == False:
  465. if block_player(board, player_color) == False:
  466. move = dont_screw_it(board, player_color, \
  467. computer_color)
  468. print "dont screw it move: ", move
  469. else:
  470. move = block_player(board, player_color)
  471. print "block move: ", move
  472. else:
  473. move = go_for_win(board, computer_color)
  474.  
  475. board[move] = computer_color
  476. # check for win
  477. if see_if_won(board, computer_color) == True:
  478. blank_and_draw()
  479. print ""
  480. print "Four in a row. Computer wins!"
  481. time.sleep(3.5)
  482. game_won = True
  483. else:
  484. #print "not yet."
  485. next_move = 'player'
  486. turn += 1
  487. continue
  488.  
  489. # game_won = True
  490. if play_again():
  491. continue
  492. else:
  493. break
  494.  
  495. blank_and_draw()
  496. print ""
  497. print "Thanks for playing!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement