Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- from copy import deepcopy
- def printBoard(board):
- rowCounter = 1
- print("-" * 33)
- for i in range(8):
- row = board[i]
- row = " | ".join(row)
- print("| " +row + " | " + str(rowCounter))
- rowCounter += 1
- if rowCounter <= 8:
- print("|" + "-" * 31 + "|")
- if rowCounter == 9:
- print("-" * 33)
- print(" A B C D E F G H")
- def choosePlayer():
- player = None
- while player != "X" or player != "O":
- player = input("What letter do you pick? (X or O): ").upper()
- if player == "X":
- computer = "O"
- return player, computer
- elif player == "O":
- computer = "X"
- return player, computer
- else:
- print("Invalid input. Pick either X or O.")
- def whoStarts(player, computer):
- participantList = [player, computer]
- randInt = randint(0, 1)
- return participantList[randInt]
- def isColumnFull(board, column):
- counter = 0
- for i in range(8):
- if "_" not in board[i][column]: #Goes vertically downa single column to count absences of "_"
- counter += 1
- if counter == 8: #If eight absences of "_" were found, the column must be full
- return True
- else:
- return False
- def pickColumn(board, column, participant):
- for i in reversed(range(8)): #Reverses all rows. Bottom ones go first
- if board[i][column] == "_": #So soon as an empty row/column combination is found (column remains constant), returns true
- board[i][column] = participant
- break
- def isWinner(board):
- for i in range(8):
- row = board[i] #Extracts a single row from the list
- for x in range(5):
- if row[x] == row[x+1] and row[x] == row[x+2] and row[x] == row[x+3] and row[x] != "_": #Horizontal comparison (row)
- return True
- for i in range(5):
- rowCounter = 0
- while rowCounter <= 7:
- if board[i][rowCounter] == board[i+1][rowCounter] and board[i][rowCounter] == board[i+2][rowCounter] and board[i][rowCounter] == board[i+3][rowCounter] and board[i][rowCounter] != "_": #Vertical comparison (column)
- return True
- rowCounter += 1
- for i in range(5):
- rowCounter = 0
- while rowCounter <= 4:
- if board[i][rowCounter] == board[i+1][rowCounter+1] and board[i][rowCounter] == board[i+2][rowCounter+2] and board[i][rowCounter] == board[i+3][rowCounter+3] and board[i][rowCounter] != "_": #Diagonal comparison (top left to bottom right)
- return True
- rowCounter += 1
- for i in range(3, 8):
- rowCounter = 0
- while rowCounter <= 4:
- if board[i][rowCounter] == board[i-1][rowCounter+1] and board[i][rowCounter] == board[i-2][rowCounter+2] and board[i][rowCounter] == board[i-3][rowCounter+3] and board[i][rowCounter] != "_": #Diagonal comparison (bottom left to top right)
- return True
- rowCounter += 1
- return False
- def dupBoard(board):
- duplicate = deepcopy(board)
- return duplicate
- #duplicate = []
- #for row in board:
- # duplicate.append(row) #Fancy way of duplicating. Could use duplicate = board, return duplicate
- #return duplicate
- def isBoardFull(board):
- for i in range(8):
- for x in range(8): #Checks all possible spots for "_"s. Returns false so soon as one is found
- if board[i][x] == "_":
- return False
- return True
- def getComputerChoice(board, computer, player):
- #Can I win? Return that
- for i in range(8):
- duplicate = dupBoard(board)
- if not isColumnFull(duplicate, i):
- pickColumn(duplicate, i, computer)
- if isWinner(duplicate):
- return i
- #Can player win? Return that
- for i in range(8):
- duplicate = dupBoard(board)
- if not isColumnFull(duplicate, i):
- pickColumn(duplicate, i, player)
- if isWinner(duplicate):
- return i
- #Pick any other column (lol stupid AI)
- duplicate = dupBoard(board)
- while True:
- choice = randint(0, 7)
- if not isColumnFull(duplicate, choice):
- return choice
- def getPlayerChoice(board):
- while True:
- choice = input("Which column do you want to picK? (A-H): ").upper()
- try:
- choice = ord(choice) - 65 #"A" = 0, "H" = 7
- if choice >= 0 and choice <=7:
- if not isColumnFull(board, choice):
- return choice
- else:
- print("That column is full.")
- except:
- print("That input is invalid.")
- playAgain = True
- while playAgain == True:
- board = [["_"] * 8 for i in range(8)]
- gameOver = False
- player, computer = choosePlayer()
- turn = whoStarts(player, computer)
- while gameOver == False:
- if turn == player:
- printBoard(board)
- choice = getPlayerChoice(board)
- pickColumn(board, choice, player)
- if isWinner(board):
- printBoard(board)
- print("Congratulations! You have won!")
- gameOver = True
- elif isBoardFull(board):
- printBoard(board)
- print("Alas. It is a tie.")
- gameOver = True
- else:
- turn = computer
- if turn == computer:
- choice = getComputerChoice(board, computer, player)
- pickColumn(board, choice, computer)
- if isWinner(board):
- printBoard(board)
- print("You have failed!")
- gameOver = True
- elif isBoardFull(board):
- printBoard(board)
- print("Alas. It is a tie.")
- gameOver = True
- else:
- turn = player
- playAgain = input("Do you want to play again? (yes or no): ")
- if playAgain.lower().startswith("y"):
- playAgain = True
- else:
- playAgain = False
Advertisement
Add Comment
Please, Sign In to add comment