Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 15th, 2010 | Syntax: Python | Size: 6.95 KB | Hits: 40 | Expires: Never
Copy text to clipboard
  1. print ">>> Connect 4! <<<"
  2.  
  3. global EMPTY, BLACK, WHITE, a, b, c, d, e, f, g
  4. EMPTY = " "
  5. BLACK = "X"
  6. WHITE = "O"
  7. a = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]
  8. b = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]
  9. c = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]
  10. d = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]
  11. e = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]
  12. f = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]
  13. g = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]
  14.  
  15. # >>> Combo Database Update Routine
  16. COMBOS = lambda: [
  17.         (a[0],a[1],a[2],a[3]),
  18.         (a[1],a[2],a[3],a[4]),
  19.         (a[2],a[3],a[4],a[5]),
  20.         (b[0],b[1],b[2],b[3]),
  21.         (b[1],b[2],b[3],b[4]),
  22.         (b[2],b[3],b[4],b[5]),
  23.         (c[0],c[1],c[2],c[3]),
  24.         (c[1],c[2],c[3],c[4]),
  25.         (c[2],c[3],c[4],c[5]),
  26.         (d[0],d[1],d[2],d[3]),
  27.         (d[1],d[2],d[3],d[4]),
  28.         (d[2],d[3],d[4],d[5]),
  29.         (e[0],e[1],e[2],e[3]),
  30.         (e[1],e[2],e[3],e[4]),
  31.         (e[2],e[3],e[4],e[5]),
  32.         (f[0],f[1],f[2],f[3]),
  33.         (f[1],f[2],f[3],f[4]),
  34.         (f[2],f[3],f[4],f[5]),
  35.         (g[0],g[1],g[2],g[3]),
  36.         (g[1],g[2],g[3],g[4]),
  37.         (g[2],g[3],g[4],g[5]),
  38.         (a[1],b[1],c[1],d[1]),
  39.         (b[1],c[1],d[1],e[1]),
  40.         (c[1],d[1],e[1],f[1]),
  41.         (a[2],b[2],c[2],d[2]),
  42.         (b[2],c[2],d[2],e[2]),
  43.         (c[2],d[2],e[2],f[2]),
  44.         (a[3],b[3],c[3],d[3]),
  45.         (b[3],c[3],d[3],e[3]),
  46.         (c[3],d[3],e[3],f[3]),
  47.         (a[4],b[4],c[4],d[4]),
  48.         (b[4],c[4],d[4],e[4]),
  49.         (c[4],d[4],e[4],f[4]),
  50.         (a[5],b[5],c[5],d[5]),
  51.         (b[5],c[5],d[5],e[5]),
  52.         (c[5],d[5],e[5],f[5]),
  53.         (a[0],b[1],c[2],d[3]),
  54.         (b[0],c[1],d[2],e[3]),
  55.         (c[0],d[1],e[2],f[3]),
  56.         (d[0],e[1],f[2],g[3]),
  57.         (a[1],b[2],c[3],d[4]),
  58.         (b[1],c[2],d[3],e[4]),
  59.         (c[1],d[2],e[3],f[4]),
  60.         (d[1],e[2],f[3],g[4]),
  61.         (a[2],b[3],c[4],d[5]),
  62.         (b[2],c[3],d[4],e[5]),
  63.         (c[2],d[3],e[4],f[5]),
  64.         (d[2],e[3],f[4],g[5]),
  65.         (g[0],f[1],e[2],d[3]),
  66.         (f[0],e[1],d[2],c[3]),
  67.         (e[0],d[1],c[2],b[3]),
  68.         (d[0],c[1],b[2],a[3]),
  69.         (g[1],f[2],e[3],d[4]),
  70.         (f[1],e[2],d[3],c[4]),
  71.         (e[1],d[2],c[3],b[4]),
  72.         (d[1],c[2],b[3],a[4]),
  73.         (g[2],f[3],e[4],d[5]),
  74.         (f[2],e[3],d[4],c[5]),
  75.         (e[2],d[3],c[4],b[5]),
  76.         (d[2],c[3],b[4],a[5]),
  77.     ]
  78.  
  79.  
  80. # >>> Definitions
  81. def main():
  82.     global COMP,HUMN
  83.     response = raw_input("\nWould you like to go first [y/N]?: ").lower()
  84.     print "At any time, just type 'exit' or 'quit' to leave the game...\n"
  85.     if response[0] != "y":
  86.         COMP = WHITE
  87.         HUMN = BLACK
  88.         comp_move()
  89.     else:
  90.         COMP = BLACK
  91.         HUMN = WHITE
  92.         draw_board()
  93.         human_move()
  94.     # Instead of putting things in a loop, I've set it so that the AI/HUMN functions run their course, and then call each other.
  95.     # The AI function calls the main algorithm which is expected to not only interpret possible AI moves, but to be aware of the
  96.     # game STATE, as I like to call it. (In a way, it is a loop, but there is more control over it...)
  97.  
  98. def draw_board():
  99.     # This relies completely on globals. There is an easier way to code this, but for right now,
  100.     # it helps to be able to visualize the board when coding.
  101.     print "6 | %s | %s | %s | %s | %s | %s | %s |" % (a[5],b[5],c[5],d[5],e[5],f[5],g[5])
  102.     print "5 | %s | %s | %s | %s | %s | %s | %s |" % (a[4],b[4],c[4],d[4],e[4],f[4],g[4])
  103.     print "4 | %s | %s | %s | %s | %s | %s | %s |" % (a[3],b[3],c[3],d[3],e[3],f[3],g[3])
  104.     print "3 | %s | %s | %s | %s | %s | %s | %s |" % (a[2],b[2],c[2],d[2],e[2],f[2],g[2])
  105.     print "2 | %s | %s | %s | %s | %s | %s | %s |" % (a[1],b[1],c[1],d[1],e[1],f[1],g[1])
  106.     print "1 | %s | %s | %s | %s | %s | %s | %s |" % (a[0],b[0],c[0],d[0],e[0],f[0],g[0])
  107.     print "--| a | b | c | d | e | f | g |"
  108.  
  109.  
  110. def quit(message, draw=True):
  111.     if draw == True:
  112.         draw_board()
  113.     print message
  114.     exit()
  115.    
  116. def check_state():
  117.     # I recently realized that even though the COMBOS array was global, it would not provide absolute references to the a,b,c,d,e,f,g arrays.
  118.     # But lambda solves this problem very elegantly.
  119.     combos = COMBOS()
  120.     # Check if game is over.
  121.     for win in combos:
  122.         if win[0]+win[1]+win[2]+win[3] == COMP*4:
  123.             quit("The computer has won.")
  124.         if win[0]+win[1]+win[2]+win[3] == HUMN*4:
  125.             quit("Human has won.")
  126.         # Insert super-awesome algorithm here. If a move has not been determined, then continue. Else, make it.
  127.  
  128.         # Base : Find a win in combos that is within gravitational restraints and brute force into making that win.
  129.             # If Case 1 occurs OR if a necessary location in Base is taken by human, reload Base and try again.
  130.                 # It may occur that Base will be reloaded to the SAME Base. This is OK.
  131.         # Case 1. If there is a possible human victory in the next move, block it. If computer is first, skip to #2.
  132.         # Case 2. If there is a possible computer victory in the next move, make it. If there is a gravity error;
  133.             # a) find another winning move
  134.             # b) find a different Base
  135.         # Regardless of Base or STATE status, once a move is determined, run the according set of evaled column, row assignments.
  136.             # If there is a computer win and a human win in the next move, make the computer win.
  137.            
  138.  
  139. def help_text(flargh):
  140.     print "Your error involves",flargh
  141.     print "An example of a correct input (locator) would be: a,1"
  142.     human_move()
  143.  
  144. def human_move():
  145.     coords = raw_input("\nConnect4 [Option] <column>,<row>: ").lower()
  146.     quitting = ['exit','quit']
  147.     if coords in quitting:
  148.         quit("Game Over...", False)
  149.     # Petty error handling.
  150.     yAlpha = ['a','b','c','d','e','f','g']
  151.     if len(coords) != 3 or coords[1] != ',':
  152.         help_text("a misformed locator.")
  153.     if coords[0] not in yAlpha:
  154.         help_text(coords[0]+" being out of the acceptable range [a-g].")
  155.     if eval(coords[2]) not in range(1,6):
  156.         help_text(coords[2]+" being out of the acceptable range [1-6].")
  157.     # Works brilliantly. Much easier to implement than I thought as well!
  158.     if eval(coords[0])[eval(coords[2])-1] == EMPTY:
  159.         if eval(coords[2])-2 < 0:
  160.             pass
  161.         else:
  162.             if eval(coords[0])[eval(coords[2])-2] == EMPTY:
  163.                 print "Illegal Move: Gravity Error."
  164.                 human_move()
  165.         eval(coords[0])[eval(coords[2])-1] = HUMN
  166.         print "HUMAN:"
  167.         draw_board()
  168.         comp_move()
  169.     else:
  170.         print "Illegal Move: That spot has been taken."
  171.         human_move()
  172.  
  173. def comp_move():
  174.     check_state()
  175.     print "COMPUTER:"
  176.     # This is done so that if additional difficulties are added, the general display routine would not be interrupted.
  177.     # (I don't want draw_board() or such to be in check_state().)
  178.     draw_board()
  179.     human_move()
  180.  
  181. # Ta-daaaa!!!
  182. main()