Advertisement
jcr4990

Connect Four

Mar 23rd, 2019
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.23 KB | None | 0 0
  1. ### Connect 4 Game
  2. import pprint
  3. import time
  4. import re
  5.  
  6. def init():
  7.     global theBoard
  8.     print('Welcome to Connect Four!\n')
  9.     time.sleep(.5)
  10.     theBoard = {'C1R1': ' ', 'C1R2': ' ', 'C1R3': ' ', 'C1R4': ' ', 'C1R5': ' ', 'C2R1': ' ', 'C2R2': ' ', \
  11.                 'C2R3': ' ', 'C2R4': ' ', 'C2R5': ' ', 'C3R1': ' ', 'C3R2': ' ', 'C3R3': ' ', 'C3R4': ' ', \
  12.                 'C3R5': ' ', 'C4R1': ' ', 'C4R2': ' ', 'C4R3': ' ', 'C4R4': ' ', 'C4R5': ' ', 'C5R1': ' ', \
  13.                 'C5R2': ' ', 'C5R3': ' ', 'C5R4': ' ', 'C5R5': ' ', 'C6R1': ' ', 'C6R2': ' ', 'C6R3': ' ', \
  14.                 'C6R4': ' ', 'C6R5': ' ',}
  15.  
  16.  
  17. def checkcolumn():
  18.     if selectedColumn == '1' \
  19.        and theBoard['C1R1'] == ' ' \
  20.        and theBoard['C1R2'] == ' ' \
  21.        and theBoard['C1R3'] == ' ' \
  22.        and theBoard['C1R4'] == ' ' \
  23.        and theBoard['C1R5'] == ' ':
  24.         theBoard['C1R5'] = Turn
  25.     elif selectedColumn == '1' \
  26.        and theBoard['C1R1'] == ' ' \
  27.        and theBoard['C1R2'] == ' ' \
  28.        and theBoard['C1R3'] == ' ' \
  29.        and theBoard['C1R4'] == ' ' \
  30.        and theBoard['C1R5'] != ' ':
  31.         theBoard['C1R4'] = Turn
  32.     elif selectedColumn == '1' \
  33.        and theBoard['C1R1'] == ' ' \
  34.        and theBoard['C1R2'] == ' ' \
  35.        and theBoard['C1R3'] == ' ' \
  36.        and theBoard['C1R4'] != ' ':
  37.         theBoard['C1R3'] = Turn
  38.     elif selectedColumn == '1' \
  39.        and theBoard['C1R1'] == ' ' \
  40.        and theBoard['C1R2'] == ' ' \
  41.        and theBoard['C1R3'] != ' ':
  42.         theBoard['C1R2'] = Turn
  43.     elif selectedColumn == '1' \
  44.        and theBoard['C1R1'] == ' ' \
  45.        and theBoard['C1R2'] != ' ':
  46.         theBoard['C1R1'] = Turn
  47.     elif selectedColumn == '1' \
  48.        and theBoard['C1R1'] != ' ':
  49.         print('Error: Column Full\n')
  50.  
  51.  
  52.     if selectedColumn == '2' \
  53.        and theBoard['C2R1'] == ' ' \
  54.        and theBoard['C2R2'] == ' ' \
  55.        and theBoard['C2R3'] == ' ' \
  56.        and theBoard['C2R4'] == ' ' \
  57.        and theBoard['C2R5'] == ' ':
  58.         theBoard['C2R5'] = Turn
  59.     elif selectedColumn == '2' \
  60.        and theBoard['C2R1'] == ' ' \
  61.        and theBoard['C2R2'] == ' ' \
  62.        and theBoard['C2R3'] == ' ' \
  63.        and theBoard['C2R4'] == ' ' \
  64.        and theBoard['C2R5'] != ' ':
  65.         theBoard['C2R4'] = Turn
  66.     elif selectedColumn == '2' \
  67.        and theBoard['C2R1'] == ' ' \
  68.        and theBoard['C2R2'] == ' ' \
  69.        and theBoard['C2R3'] == ' ' \
  70.        and theBoard['C2R4'] != ' ':
  71.         theBoard['C2R3'] = Turn
  72.     elif selectedColumn == '2' \
  73.        and theBoard['C2R1'] == ' ' \
  74.        and theBoard['C2R2'] == ' ' \
  75.        and theBoard['C2R3'] != ' ':
  76.         theBoard['C2R2'] = Turn
  77.     elif selectedColumn == '2' \
  78.        and theBoard['C2R1'] == ' ' \
  79.        and theBoard['C2R2'] != ' ':
  80.         theBoard['C2R1'] = Turn
  81.     elif selectedColumn == '2' \
  82.        and theBoard['C2R1'] != ' ':
  83.         print('Error: Column Full\n')
  84.  
  85.        
  86.     if selectedColumn == '3' \
  87.        and theBoard['C3R1'] == ' ' \
  88.        and theBoard['C3R2'] == ' ' \
  89.        and theBoard['C3R3'] == ' ' \
  90.        and theBoard['C3R4'] == ' ' \
  91.        and theBoard['C3R5'] == ' ':
  92.         theBoard['C3R5'] = Turn
  93.     elif selectedColumn == '3' \
  94.        and theBoard['C3R1'] == ' ' \
  95.        and theBoard['C3R2'] == ' ' \
  96.        and theBoard['C3R3'] == ' ' \
  97.        and theBoard['C3R4'] == ' ' \
  98.        and theBoard['C3R5'] != ' ':
  99.         theBoard['C3R4'] = Turn
  100.     elif selectedColumn == '3' \
  101.        and theBoard['C3R1'] == ' ' \
  102.        and theBoard['C3R2'] == ' ' \
  103.        and theBoard['C3R3'] == ' ' \
  104.        and theBoard['C3R4'] != ' ':
  105.         theBoard['C3R3'] = Turn
  106.     elif selectedColumn == '3' \
  107.        and theBoard['C3R1'] == ' ' \
  108.        and theBoard['C3R2'] == ' ' \
  109.        and theBoard['C3R3'] != ' ':
  110.         theBoard['C3R2'] = Turn
  111.     elif selectedColumn == '3' \
  112.        and theBoard['C3R1'] == ' ' \
  113.        and theBoard['C3R2'] != ' ':
  114.         theBoard['C3R1'] = Turn
  115.     elif selectedColumn == '3' \
  116.        and theBoard['C3R1'] != ' ':
  117.         print('Error: Column Full\n')
  118.  
  119.  
  120.     if selectedColumn == '4' \
  121.        and theBoard['C4R1'] == ' ' \
  122.        and theBoard['C4R2'] == ' ' \
  123.        and theBoard['C4R3'] == ' ' \
  124.        and theBoard['C4R4'] == ' ' \
  125.        and theBoard['C4R5'] == ' ':
  126.         theBoard['C4R5'] = Turn
  127.     elif selectedColumn == '4' \
  128.        and theBoard['C4R1'] == ' ' \
  129.        and theBoard['C4R2'] == ' ' \
  130.        and theBoard['C4R3'] == ' ' \
  131.        and theBoard['C4R4'] == ' ' \
  132.        and theBoard['C4R5'] != ' ':
  133.         theBoard['C4R4'] = Turn
  134.     elif selectedColumn == '4' \
  135.        and theBoard['C4R1'] == ' ' \
  136.        and theBoard['C4R2'] == ' ' \
  137.        and theBoard['C4R3'] == ' ' \
  138.        and theBoard['C4R4'] != ' ':
  139.         theBoard['C4R3'] = Turn
  140.     elif selectedColumn == '4' \
  141.        and theBoard['C4R1'] == ' ' \
  142.        and theBoard['C4R2'] == ' ' \
  143.        and theBoard['C4R3'] != ' ':
  144.         theBoard['C4R2'] = Turn
  145.     elif selectedColumn == '4' \
  146.        and theBoard['C4R1'] == ' ' \
  147.        and theBoard['C4R2'] != ' ':
  148.         theBoard['C4R1'] = Turn
  149.     elif selectedColumn == '4' \
  150.        and theBoard['C4R1'] != ' ':
  151.         print('Error: Column Full\n')
  152.  
  153.  
  154.     if selectedColumn == '5' \
  155.        and theBoard['C5R1'] == ' ' \
  156.        and theBoard['C5R2'] == ' ' \
  157.        and theBoard['C5R3'] == ' ' \
  158.        and theBoard['C5R4'] == ' ' \
  159.        and theBoard['C5R5'] == ' ':
  160.         theBoard['C5R5'] = Turn
  161.     elif selectedColumn == '5' \
  162.        and theBoard['C5R1'] == ' ' \
  163.        and theBoard['C5R2'] == ' ' \
  164.        and theBoard['C5R3'] == ' ' \
  165.        and theBoard['C5R4'] == ' ' \
  166.        and theBoard['C5R5'] != ' ':
  167.         theBoard['C5R4'] = Turn
  168.     elif selectedColumn == '5' \
  169.        and theBoard['C5R1'] == ' ' \
  170.        and theBoard['C5R2'] == ' ' \
  171.        and theBoard['C5R3'] == ' ' \
  172.        and theBoard['C5R4'] != ' ':
  173.         theBoard['C5R3'] = Turn
  174.     elif selectedColumn == '5' \
  175.        and theBoard['C5R1'] == ' ' \
  176.        and theBoard['C5R2'] == ' ' \
  177.        and theBoard['C5R3'] != ' ':
  178.         theBoard['C5R2'] = Turn
  179.     elif selectedColumn == '5' \
  180.        and theBoard['C5R1'] == ' ' \
  181.        and theBoard['C5R2'] != ' ':
  182.         theBoard['C5R1'] = Turn
  183.     elif selectedColumn == '5' \
  184.        and theBoard['C5R1'] != ' ':
  185.         print('Error: Column Full\n')
  186.  
  187.  
  188.     if selectedColumn == '6' \
  189.        and theBoard['C6R1'] == ' ' \
  190.        and theBoard['C6R2'] == ' ' \
  191.        and theBoard['C6R3'] == ' ' \
  192.        and theBoard['C6R4'] == ' ' \
  193.        and theBoard['C6R5'] == ' ':
  194.         theBoard['C6R5'] = Turn
  195.     elif selectedColumn == '6' \
  196.        and theBoard['C6R1'] == ' ' \
  197.        and theBoard['C6R2'] == ' ' \
  198.        and theBoard['C6R3'] == ' ' \
  199.        and theBoard['C6R4'] == ' ' \
  200.        and theBoard['C6R5'] != ' ':
  201.         theBoard['C6R4'] = Turn
  202.     elif selectedColumn == '6' \
  203.        and theBoard['C6R1'] == ' ' \
  204.        and theBoard['C6R2'] == ' ' \
  205.        and theBoard['C6R3'] == ' ' \
  206.        and theBoard['C6R4'] != ' ':
  207.         theBoard['C6R3'] = Turn
  208.     elif selectedColumn == '6' \
  209.        and theBoard['C6R1'] == ' ' \
  210.        and theBoard['C6R2'] == ' ' \
  211.        and theBoard['C6R3'] != ' ':
  212.         theBoard['C6R2'] = Turn
  213.     elif selectedColumn == '6' \
  214.        and theBoard['C6R1'] == ' ' \
  215.        and theBoard['C6R2'] != ' ':
  216.         theBoard['C6R1'] = Turn
  217.     elif selectedColumn == '6' \
  218.        and theBoard['C6R1'] != ' ':
  219.         print('Error: Column Full\n')
  220.  
  221.  
  222. def printBoard(board):
  223.     print('\n|' + (board['C1R1'] + '|' + board['C2R1'] + '|' + board['C3R1'] + '|' + board['C4R1'] + '|' + \
  224.           board['C5R1'] + '|' + board['C6R1'] + '|')) #Print Row 1
  225.     print('|' + (board['C1R2'] + '|' + board['C2R2'] + '|' + board['C3R2'] + '|' + board['C4R2'] + '|' + \
  226.           board['C5R2'] + '|' + board['C6R2'] + '|')) #Print Row 2
  227.     print('|' + (board['C1R3'] + '|' + board['C2R3'] + '|' + board['C3R3'] + '|' + board['C4R3'] + '|' + \
  228.           board['C5R3'] + '|' + board['C6R3'] + '|')) #Print Row 3
  229.     print('|' + (board['C1R4'] + '|' + board['C2R4'] + '|' + board['C3R4'] + '|' + board['C4R4'] + '|' + \
  230.           board['C5R4'] + '|' + board['C6R4'] + '|')) #Print Row 4
  231.     print('|' + (board['C1R5'] + '|' + board['C2R5'] + '|' + board['C3R5'] + '|' + board['C4R5'] + '|' + \
  232.           board['C5R5'] + '|' + board['C6R5'] + '|')) #Print Row 5
  233.     print(' 1 2 3 4 5 6 ')
  234.    
  235. def checkwin():
  236. #Check possible win conditions (All Vertical and Horizontal possibilities done - Diagnal options to do)
  237.     if theBoard['C1R5'] == Turn and theBoard['C1R4'] == Turn and theBoard['C1R3'] == Turn and theBoard['C1R2'] == Turn or \
  238.        theBoard['C1R4'] == Turn and theBoard['C1R3'] == Turn and theBoard['C1R2'] == Turn and theBoard['C1R1'] == Turn or \
  239.        theBoard['C2R5'] == Turn and theBoard['C2R4'] == Turn and theBoard['C2R3'] == Turn and theBoard['C2R2'] == Turn or \
  240.        theBoard['C2R4'] == Turn and theBoard['C2R3'] == Turn and theBoard['C2R2'] == Turn and theBoard['C2R1'] == Turn or \
  241.        theBoard['C3R5'] == Turn and theBoard['C3R4'] == Turn and theBoard['C3R3'] == Turn and theBoard['C3R2'] == Turn or \
  242.        theBoard['C3R4'] == Turn and theBoard['C3R3'] == Turn and theBoard['C3R2'] == Turn and theBoard['C3R1'] == Turn or \
  243.        theBoard['C4R5'] == Turn and theBoard['C4R4'] == Turn and theBoard['C4R3'] == Turn and theBoard['C4R2'] == Turn or \
  244.        theBoard['C4R4'] == Turn and theBoard['C4R3'] == Turn and theBoard['C4R2'] == Turn and theBoard['C4R1'] == Turn or \
  245.        theBoard['C5R5'] == Turn and theBoard['C5R4'] == Turn and theBoard['C5R3'] == Turn and theBoard['C5R2'] == Turn or \
  246.        theBoard['C5R4'] == Turn and theBoard['C5R3'] == Turn and theBoard['C5R2'] == Turn and theBoard['C5R1'] == Turn or \
  247.        theBoard['C6R5'] == Turn and theBoard['C6R4'] == Turn and theBoard['C6R3'] == Turn and theBoard['C6R2'] == Turn or \
  248.        theBoard['C6R4'] == Turn and theBoard['C6R3'] == Turn and theBoard['C6R2'] == Turn and theBoard['C6R1'] == Turn or \
  249.        theBoard['C1R5'] == Turn and theBoard['C2R5'] == Turn and theBoard['C3R5'] == Turn and theBoard['C4R5'] == Turn or \
  250.        theBoard['C2R5'] == Turn and theBoard['C3R5'] == Turn and theBoard['C4R5'] == Turn and theBoard['C5R5'] == Turn or \
  251.        theBoard['C3R5'] == Turn and theBoard['C4R5'] == Turn and theBoard['C5R5'] == Turn and theBoard['C6R5'] == Turn or \
  252.        theBoard['C1R4'] == Turn and theBoard['C2R4'] == Turn and theBoard['C3R4'] == Turn and theBoard['C4R4'] == Turn or \
  253.        theBoard['C2R4'] == Turn and theBoard['C3R4'] == Turn and theBoard['C4R4'] == Turn and theBoard['C5R4'] == Turn or \
  254.        theBoard['C3R4'] == Turn and theBoard['C4R4'] == Turn and theBoard['C5R4'] == Turn and theBoard['C6R4'] == Turn or \
  255.        theBoard['C1R3'] == Turn and theBoard['C2R3'] == Turn and theBoard['C3R3'] == Turn and theBoard['C4R3'] == Turn or \
  256.        theBoard['C2R3'] == Turn and theBoard['C3R3'] == Turn and theBoard['C4R3'] == Turn and theBoard['C5R3'] == Turn or \
  257.        theBoard['C3R3'] == Turn and theBoard['C4R3'] == Turn and theBoard['C5R3'] == Turn and theBoard['C6R3'] == Turn or \
  258.        theBoard['C1R2'] == Turn and theBoard['C2R2'] == Turn and theBoard['C3R2'] == Turn and theBoard['C4R2'] == Turn or \
  259.        theBoard['C2R2'] == Turn and theBoard['C3R2'] == Turn and theBoard['C4R2'] == Turn and theBoard['C5R2'] == Turn or \
  260.        theBoard['C3R2'] == Turn and theBoard['C4R2'] == Turn and theBoard['C5R2'] == Turn and theBoard['C6R2'] == Turn or \
  261.        theBoard['C1R1'] == Turn and theBoard['C2R1'] == Turn and theBoard['C3R1'] == Turn and theBoard['C4R1'] == Turn or \
  262.        theBoard['C2R1'] == Turn and theBoard['C3R1'] == Turn and theBoard['C4R1'] == Turn and theBoard['C5R1'] == Turn or \
  263.        theBoard['C3R1'] == Turn and theBoard['C4R1'] == Turn and theBoard['C5R1'] == Turn and theBoard['C6R1'] == Turn:
  264.  
  265.  
  266.  
  267.         printBoard(theBoard)
  268.         print('\nWINNER!!!\n\n\n\n')
  269.         time.sleep(3)
  270.         init()
  271.        
  272.  
  273.        
  274.        
  275.    
  276. init()
  277.  
  278. Turn = 'X'
  279.  
  280.  
  281. for i in range(30):
  282.     printBoard(theBoard)
  283.     selectedColumn = input('\nWhat is your move player ' + Turn + '?\n\n')
  284.     time.sleep(.2)
  285.     checkcolumn()
  286.     checkwin()
  287.  
  288.     if Turn == 'X':
  289.         Turn = 'O'
  290.     else:
  291.         Turn = 'X'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement