Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Connect 4 Game
- import pprint
- import time
- import re
- def init():
- global theBoard
- print('Welcome to Connect Four!\n')
- time.sleep(.5)
- theBoard = {'C1R1': ' ', 'C1R2': ' ', 'C1R3': ' ', 'C1R4': ' ', 'C1R5': ' ', 'C2R1': ' ', 'C2R2': ' ', \
- 'C2R3': ' ', 'C2R4': ' ', 'C2R5': ' ', 'C3R1': ' ', 'C3R2': ' ', 'C3R3': ' ', 'C3R4': ' ', \
- 'C3R5': ' ', 'C4R1': ' ', 'C4R2': ' ', 'C4R3': ' ', 'C4R4': ' ', 'C4R5': ' ', 'C5R1': ' ', \
- 'C5R2': ' ', 'C5R3': ' ', 'C5R4': ' ', 'C5R5': ' ', 'C6R1': ' ', 'C6R2': ' ', 'C6R3': ' ', \
- 'C6R4': ' ', 'C6R5': ' ',}
- def checkcolumn():
- if selectedColumn == '1' \
- and theBoard['C1R1'] == ' ' \
- and theBoard['C1R2'] == ' ' \
- and theBoard['C1R3'] == ' ' \
- and theBoard['C1R4'] == ' ' \
- and theBoard['C1R5'] == ' ':
- theBoard['C1R5'] = Turn
- elif selectedColumn == '1' \
- and theBoard['C1R1'] == ' ' \
- and theBoard['C1R2'] == ' ' \
- and theBoard['C1R3'] == ' ' \
- and theBoard['C1R4'] == ' ' \
- and theBoard['C1R5'] != ' ':
- theBoard['C1R4'] = Turn
- elif selectedColumn == '1' \
- and theBoard['C1R1'] == ' ' \
- and theBoard['C1R2'] == ' ' \
- and theBoard['C1R3'] == ' ' \
- and theBoard['C1R4'] != ' ':
- theBoard['C1R3'] = Turn
- elif selectedColumn == '1' \
- and theBoard['C1R1'] == ' ' \
- and theBoard['C1R2'] == ' ' \
- and theBoard['C1R3'] != ' ':
- theBoard['C1R2'] = Turn
- elif selectedColumn == '1' \
- and theBoard['C1R1'] == ' ' \
- and theBoard['C1R2'] != ' ':
- theBoard['C1R1'] = Turn
- elif selectedColumn == '1' \
- and theBoard['C1R1'] != ' ':
- print('Error: Column Full\n')
- if selectedColumn == '2' \
- and theBoard['C2R1'] == ' ' \
- and theBoard['C2R2'] == ' ' \
- and theBoard['C2R3'] == ' ' \
- and theBoard['C2R4'] == ' ' \
- and theBoard['C2R5'] == ' ':
- theBoard['C2R5'] = Turn
- elif selectedColumn == '2' \
- and theBoard['C2R1'] == ' ' \
- and theBoard['C2R2'] == ' ' \
- and theBoard['C2R3'] == ' ' \
- and theBoard['C2R4'] == ' ' \
- and theBoard['C2R5'] != ' ':
- theBoard['C2R4'] = Turn
- elif selectedColumn == '2' \
- and theBoard['C2R1'] == ' ' \
- and theBoard['C2R2'] == ' ' \
- and theBoard['C2R3'] == ' ' \
- and theBoard['C2R4'] != ' ':
- theBoard['C2R3'] = Turn
- elif selectedColumn == '2' \
- and theBoard['C2R1'] == ' ' \
- and theBoard['C2R2'] == ' ' \
- and theBoard['C2R3'] != ' ':
- theBoard['C2R2'] = Turn
- elif selectedColumn == '2' \
- and theBoard['C2R1'] == ' ' \
- and theBoard['C2R2'] != ' ':
- theBoard['C2R1'] = Turn
- elif selectedColumn == '2' \
- and theBoard['C2R1'] != ' ':
- print('Error: Column Full\n')
- if selectedColumn == '3' \
- and theBoard['C3R1'] == ' ' \
- and theBoard['C3R2'] == ' ' \
- and theBoard['C3R3'] == ' ' \
- and theBoard['C3R4'] == ' ' \
- and theBoard['C3R5'] == ' ':
- theBoard['C3R5'] = Turn
- elif selectedColumn == '3' \
- and theBoard['C3R1'] == ' ' \
- and theBoard['C3R2'] == ' ' \
- and theBoard['C3R3'] == ' ' \
- and theBoard['C3R4'] == ' ' \
- and theBoard['C3R5'] != ' ':
- theBoard['C3R4'] = Turn
- elif selectedColumn == '3' \
- and theBoard['C3R1'] == ' ' \
- and theBoard['C3R2'] == ' ' \
- and theBoard['C3R3'] == ' ' \
- and theBoard['C3R4'] != ' ':
- theBoard['C3R3'] = Turn
- elif selectedColumn == '3' \
- and theBoard['C3R1'] == ' ' \
- and theBoard['C3R2'] == ' ' \
- and theBoard['C3R3'] != ' ':
- theBoard['C3R2'] = Turn
- elif selectedColumn == '3' \
- and theBoard['C3R1'] == ' ' \
- and theBoard['C3R2'] != ' ':
- theBoard['C3R1'] = Turn
- elif selectedColumn == '3' \
- and theBoard['C3R1'] != ' ':
- print('Error: Column Full\n')
- if selectedColumn == '4' \
- and theBoard['C4R1'] == ' ' \
- and theBoard['C4R2'] == ' ' \
- and theBoard['C4R3'] == ' ' \
- and theBoard['C4R4'] == ' ' \
- and theBoard['C4R5'] == ' ':
- theBoard['C4R5'] = Turn
- elif selectedColumn == '4' \
- and theBoard['C4R1'] == ' ' \
- and theBoard['C4R2'] == ' ' \
- and theBoard['C4R3'] == ' ' \
- and theBoard['C4R4'] == ' ' \
- and theBoard['C4R5'] != ' ':
- theBoard['C4R4'] = Turn
- elif selectedColumn == '4' \
- and theBoard['C4R1'] == ' ' \
- and theBoard['C4R2'] == ' ' \
- and theBoard['C4R3'] == ' ' \
- and theBoard['C4R4'] != ' ':
- theBoard['C4R3'] = Turn
- elif selectedColumn == '4' \
- and theBoard['C4R1'] == ' ' \
- and theBoard['C4R2'] == ' ' \
- and theBoard['C4R3'] != ' ':
- theBoard['C4R2'] = Turn
- elif selectedColumn == '4' \
- and theBoard['C4R1'] == ' ' \
- and theBoard['C4R2'] != ' ':
- theBoard['C4R1'] = Turn
- elif selectedColumn == '4' \
- and theBoard['C4R1'] != ' ':
- print('Error: Column Full\n')
- if selectedColumn == '5' \
- and theBoard['C5R1'] == ' ' \
- and theBoard['C5R2'] == ' ' \
- and theBoard['C5R3'] == ' ' \
- and theBoard['C5R4'] == ' ' \
- and theBoard['C5R5'] == ' ':
- theBoard['C5R5'] = Turn
- elif selectedColumn == '5' \
- and theBoard['C5R1'] == ' ' \
- and theBoard['C5R2'] == ' ' \
- and theBoard['C5R3'] == ' ' \
- and theBoard['C5R4'] == ' ' \
- and theBoard['C5R5'] != ' ':
- theBoard['C5R4'] = Turn
- elif selectedColumn == '5' \
- and theBoard['C5R1'] == ' ' \
- and theBoard['C5R2'] == ' ' \
- and theBoard['C5R3'] == ' ' \
- and theBoard['C5R4'] != ' ':
- theBoard['C5R3'] = Turn
- elif selectedColumn == '5' \
- and theBoard['C5R1'] == ' ' \
- and theBoard['C5R2'] == ' ' \
- and theBoard['C5R3'] != ' ':
- theBoard['C5R2'] = Turn
- elif selectedColumn == '5' \
- and theBoard['C5R1'] == ' ' \
- and theBoard['C5R2'] != ' ':
- theBoard['C5R1'] = Turn
- elif selectedColumn == '5' \
- and theBoard['C5R1'] != ' ':
- print('Error: Column Full\n')
- if selectedColumn == '6' \
- and theBoard['C6R1'] == ' ' \
- and theBoard['C6R2'] == ' ' \
- and theBoard['C6R3'] == ' ' \
- and theBoard['C6R4'] == ' ' \
- and theBoard['C6R5'] == ' ':
- theBoard['C6R5'] = Turn
- elif selectedColumn == '6' \
- and theBoard['C6R1'] == ' ' \
- and theBoard['C6R2'] == ' ' \
- and theBoard['C6R3'] == ' ' \
- and theBoard['C6R4'] == ' ' \
- and theBoard['C6R5'] != ' ':
- theBoard['C6R4'] = Turn
- elif selectedColumn == '6' \
- and theBoard['C6R1'] == ' ' \
- and theBoard['C6R2'] == ' ' \
- and theBoard['C6R3'] == ' ' \
- and theBoard['C6R4'] != ' ':
- theBoard['C6R3'] = Turn
- elif selectedColumn == '6' \
- and theBoard['C6R1'] == ' ' \
- and theBoard['C6R2'] == ' ' \
- and theBoard['C6R3'] != ' ':
- theBoard['C6R2'] = Turn
- elif selectedColumn == '6' \
- and theBoard['C6R1'] == ' ' \
- and theBoard['C6R2'] != ' ':
- theBoard['C6R1'] = Turn
- elif selectedColumn == '6' \
- and theBoard['C6R1'] != ' ':
- print('Error: Column Full\n')
- def printBoard(board):
- print('\n|' + (board['C1R1'] + '|' + board['C2R1'] + '|' + board['C3R1'] + '|' + board['C4R1'] + '|' + \
- board['C5R1'] + '|' + board['C6R1'] + '|')) #Print Row 1
- print('|' + (board['C1R2'] + '|' + board['C2R2'] + '|' + board['C3R2'] + '|' + board['C4R2'] + '|' + \
- board['C5R2'] + '|' + board['C6R2'] + '|')) #Print Row 2
- print('|' + (board['C1R3'] + '|' + board['C2R3'] + '|' + board['C3R3'] + '|' + board['C4R3'] + '|' + \
- board['C5R3'] + '|' + board['C6R3'] + '|')) #Print Row 3
- print('|' + (board['C1R4'] + '|' + board['C2R4'] + '|' + board['C3R4'] + '|' + board['C4R4'] + '|' + \
- board['C5R4'] + '|' + board['C6R4'] + '|')) #Print Row 4
- print('|' + (board['C1R5'] + '|' + board['C2R5'] + '|' + board['C3R5'] + '|' + board['C4R5'] + '|' + \
- board['C5R5'] + '|' + board['C6R5'] + '|')) #Print Row 5
- print(' 1 2 3 4 5 6 ')
- def checkwin():
- #Check possible win conditions (All Vertical and Horizontal possibilities done - Diagnal options to do)
- if theBoard['C1R5'] == Turn and theBoard['C1R4'] == Turn and theBoard['C1R3'] == Turn and theBoard['C1R2'] == Turn or \
- theBoard['C1R4'] == Turn and theBoard['C1R3'] == Turn and theBoard['C1R2'] == Turn and theBoard['C1R1'] == Turn or \
- theBoard['C2R5'] == Turn and theBoard['C2R4'] == Turn and theBoard['C2R3'] == Turn and theBoard['C2R2'] == Turn or \
- theBoard['C2R4'] == Turn and theBoard['C2R3'] == Turn and theBoard['C2R2'] == Turn and theBoard['C2R1'] == Turn or \
- theBoard['C3R5'] == Turn and theBoard['C3R4'] == Turn and theBoard['C3R3'] == Turn and theBoard['C3R2'] == Turn or \
- theBoard['C3R4'] == Turn and theBoard['C3R3'] == Turn and theBoard['C3R2'] == Turn and theBoard['C3R1'] == Turn or \
- theBoard['C4R5'] == Turn and theBoard['C4R4'] == Turn and theBoard['C4R3'] == Turn and theBoard['C4R2'] == Turn or \
- theBoard['C4R4'] == Turn and theBoard['C4R3'] == Turn and theBoard['C4R2'] == Turn and theBoard['C4R1'] == Turn or \
- theBoard['C5R5'] == Turn and theBoard['C5R4'] == Turn and theBoard['C5R3'] == Turn and theBoard['C5R2'] == Turn or \
- theBoard['C5R4'] == Turn and theBoard['C5R3'] == Turn and theBoard['C5R2'] == Turn and theBoard['C5R1'] == Turn or \
- theBoard['C6R5'] == Turn and theBoard['C6R4'] == Turn and theBoard['C6R3'] == Turn and theBoard['C6R2'] == Turn or \
- theBoard['C6R4'] == Turn and theBoard['C6R3'] == Turn and theBoard['C6R2'] == Turn and theBoard['C6R1'] == Turn or \
- theBoard['C1R5'] == Turn and theBoard['C2R5'] == Turn and theBoard['C3R5'] == Turn and theBoard['C4R5'] == Turn or \
- theBoard['C2R5'] == Turn and theBoard['C3R5'] == Turn and theBoard['C4R5'] == Turn and theBoard['C5R5'] == Turn or \
- theBoard['C3R5'] == Turn and theBoard['C4R5'] == Turn and theBoard['C5R5'] == Turn and theBoard['C6R5'] == Turn or \
- theBoard['C1R4'] == Turn and theBoard['C2R4'] == Turn and theBoard['C3R4'] == Turn and theBoard['C4R4'] == Turn or \
- theBoard['C2R4'] == Turn and theBoard['C3R4'] == Turn and theBoard['C4R4'] == Turn and theBoard['C5R4'] == Turn or \
- theBoard['C3R4'] == Turn and theBoard['C4R4'] == Turn and theBoard['C5R4'] == Turn and theBoard['C6R4'] == Turn or \
- theBoard['C1R3'] == Turn and theBoard['C2R3'] == Turn and theBoard['C3R3'] == Turn and theBoard['C4R3'] == Turn or \
- theBoard['C2R3'] == Turn and theBoard['C3R3'] == Turn and theBoard['C4R3'] == Turn and theBoard['C5R3'] == Turn or \
- theBoard['C3R3'] == Turn and theBoard['C4R3'] == Turn and theBoard['C5R3'] == Turn and theBoard['C6R3'] == Turn or \
- theBoard['C1R2'] == Turn and theBoard['C2R2'] == Turn and theBoard['C3R2'] == Turn and theBoard['C4R2'] == Turn or \
- theBoard['C2R2'] == Turn and theBoard['C3R2'] == Turn and theBoard['C4R2'] == Turn and theBoard['C5R2'] == Turn or \
- theBoard['C3R2'] == Turn and theBoard['C4R2'] == Turn and theBoard['C5R2'] == Turn and theBoard['C6R2'] == Turn or \
- theBoard['C1R1'] == Turn and theBoard['C2R1'] == Turn and theBoard['C3R1'] == Turn and theBoard['C4R1'] == Turn or \
- theBoard['C2R1'] == Turn and theBoard['C3R1'] == Turn and theBoard['C4R1'] == Turn and theBoard['C5R1'] == Turn or \
- theBoard['C3R1'] == Turn and theBoard['C4R1'] == Turn and theBoard['C5R1'] == Turn and theBoard['C6R1'] == Turn:
- printBoard(theBoard)
- print('\nWINNER!!!\n\n\n\n')
- time.sleep(3)
- init()
- init()
- Turn = 'X'
- for i in range(30):
- printBoard(theBoard)
- selectedColumn = input('\nWhat is your move player ' + Turn + '?\n\n')
- time.sleep(.2)
- checkcolumn()
- checkwin()
- if Turn == 'X':
- Turn = 'O'
- else:
- Turn = 'X'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement