Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 33 additions & 26 deletions board.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
import os
class Board():
def CheckForWinner(self, winCondList, moveCount, thereIsWinner):
for list in winCondList:
if list[0] == list[1] and list[0] == list[2]:
if list[0] == "X":
print("You are the winner!")
thereIsWinner = True
exit()
elif list[0] == "O":
print("Get wrecked nerd!")
thereIsWinner = True
exit()
else:
if moveCount == 9 and thereIsWinner == False:
print("It's a draw")
exit()
return winCondList

def CheckForWinner(winCond, win, moveCount):
winCond = list(map(int, str(winCond)))
winCond = sorted(winCond)
topRow = [1,2,3]
midRow = [4,5,6]
bottomRow = [7,8,9]
leftCol = [1,4,7]
midCol = [2,5,8]
rightCol = [3,6,9]
diagnal1 = [1,5,9]
diagnal2 = [3,5,7]
winCondList = [topRow, midRow, bottomRow, leftCol, midCol, rightCol, diagnal1, diagnal2]
if winCond is not None:
result = all(elem in winCondList for elem in winCond)
if result:
print(result)
win = 1
return win
else:
print("no win")

def PrintBoard(self, positions):
print(positions[1] + "|" + positions[2] + "|" + positions[3])
print("-----")
print(positions[4] + "|" + positions[5] + "|" + positions[6])
print("-----")
print(positions[7] + "|" + positions[8] + "|" + positions[9])
print()

def ClearBoard(self):
clear = lambda : os.system('tput reset')


def PrintBoard(positions):
print(positions[1] + "|" + positions[2] + "|" + positions[3])
print("-----")
print(positions[4] + "|" + positions[5] + "|" + positions[6])
print("-----")
print(positions[7] + "|" + positions[8] + "|" + positions[9])
print()

def ClearBoard():
clear = lambda : os.system('tput reset')
102 changes: 50 additions & 52 deletions computer.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,55 @@
import board
import random
class Computer():
#picks a 'random' move when no block or win move is available
#the middle space has the highest weight followed by the four corners
def ComputerStarterMoves (self, positions, winCondList):
priorityMoveAvailable = False
priorityMoves = [5,1,3,7,9]
for value in priorityMoves:
if positions[value] != "X" and positions[value] != "O":
compMove = value
positions[compMove] = "O"
priorityMoveAvailable = True
break
if priorityMoveAvailable == False:
def ComputerStarterMoves (positions, winCond):
priorityMoveAvailable = False
priorityMoves = [5,1,3,7,9]
for value in priorityMoves:
if positions[value] != "X" and positions[value] != "O":
compMove = value
positions[compMove] = "O"
priorityMoveAvailable = True
break
if priorityMoveAvailable == False:
compMove = random.choice(list(positions))
while positions[compMove] == "O" or positions[compMove] == "X":
compMove = random.choice(list(positions))
while positions[compMove] == "O" or positions[compMove] == "X":
compMove = random.choice(list(positions))
positions[compMove] = "O"
winCondList = [list(map(lambda y: y if y != compMove else 'O', i)) for i in winCondList]
return winCondList

def ComputerMoveWinOrBlock (self, positions, noBestMove, userTurn, winCondList):
for list in winCondList:
xCounter = 0
oCounter = 0
if userTurn == True:
break
for value in list:
if value == "O":
oCounter += 1
if oCounter == 2:
for index, item in enumerate(list):
if list[index] != "O" and list[index] != "X":
compMove = list[index]
positions[list[index]] = "O"
noBestMove = False
userTurn = True
for list in winCondList:
xCounter = 0
oCounter = 0
if userTurn == True:
break
for value in list:
if value == "X":
xCounter += 1
if xCounter == 2:
for index, item in enumerate(list):
if list[index] != "O" and list[index] != "X":
compMove = list[index]
positions[list[index]] = "O"
noBestMove = False
userTurn = True
if noBestMove == False:
winCondList = [list(map(lambda y: y if y != compMove else 'O', i)) for i in winCondList]
return winCondList

positions[compMove] = "O"
winCondComp = list(map(int, str(compMove)))
return winCondComp

def ComputerMoveWinOrBlock (positions, noBestMove, userTurn, winCond, winCondList):
for list in winCondList:
xCounter = 0
oCounter = 0
if userTurn == True:
break
for value in list:
if value == "O":
oCounter += 1
if oCounter == 2:
for index, item in enumerate(list):
if list[index] != "O" and list[index] != "X":
compMove = list[index]
positions[list[index]] = "O"
noBestMove = False
userTurn = True
for list in winCondLitxs:
xCounter = 0
oCounter = 0
if userTurn == True:
break
for value in list:
if value == "X":
xCounter += 1
if xCounter == 2:
for index, item in enumerate(list):
if list[index] != "O" and list[index] != "X":
compMove = list[index]
positions[list[index]] = "O"
noBestMove = False
userTurn = True
if noBestMove == False:
winCond.append(compMove)
return winCond
8 changes: 4 additions & 4 deletions player.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import board
class Player():
def GamePrep (self):
def GamePrep(self):
PlayerName = input("Please enter your name: ")
PlayersToken = input("Which piece would you like to plays, X or O?")

def PlayerMove (self, positions, winCondList):
def PlayerMove(self, positions, winCondPlayer):
gettingInput = True
while gettingInput:
try:
Expand All @@ -19,5 +19,5 @@ def PlayerMove (self, positions, winCondList):
selection = int(input("that move is invalid please choose a legal move: "))
else:
positions[selection] = "X"
winCondList = [list(map(lambda x: x if x != selection else 'X', i)) for i in winCondList]
return winCondList
winCondPlayer.append(selection)
return winCondPlayer
42 changes: 24 additions & 18 deletions ttt.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import board
import player
import computer
import random
from board import CheckForWinner, PrintBoard, ClearBoard
from computer import ComputerStarterMoves, ComputerMoveWinOrBlock


thereIsWinner = False
#make board
positions = {1:"1",2:"2",3:"3",4:"4",5:"5",6:"6",7:"7",8:"8",9:"9"}

#lists for 3 consecutive moves
winCond = []
topRow = [1,2,3]
midRow = [4,5,6]
bottomRow = [7,8,9]
Expand All @@ -18,31 +17,38 @@
diagnal1 = [1,5,9]
diagnal2 = [3,5,7]
winCondList = [topRow, midRow, bottomRow, leftCol, midCol, rightCol, diagnal1, diagnal2]
win = 0

#start game cycle
board.Board().PrintBoard(positions)
player.Player().GamePrep()
PrintBoard(positions)
userTurn = True
moveCount = 0


while moveCount < 10:
while win == 0:
if userTurn == True:
player.Player().PlayerMove(positions, winCondList)
board.Board().PrintBoard(positions)
winCondPlayer = player.Player().PlayerMove(positions, winCond)
PrintBoard(positions)
print(winCondPlayer)
moveCount += 1
board.Board().CheckForWinner(winCondList, moveCount, thereIsWinner)
winCond = winCondPlayer
try:
win = CheckForWinner(win, moveCount, winCond)
except:
pass
userTurn = False

else:
noBestMove = True
computer.Computer().ComputerMoveWinOrBlock(positions, noBestMove, userTurn, winCondList)


if noBestMove == True:
computer.Computer().ComputerStarterMoves(positions, winCondList)
winCondComp = ComputerStarterMoves(positions)
noBestMove = False
board.Board().PrintBoard(positions)
board.Board().CheckForWinner(winCondList, moveCount, thereIsWinner)
else: winCondComp = ComputerMoveWinOrBlock(positions, noBestMove, userTurn, winCond, winCondList)
print(winCondComp)
PrintBoard(positions)
winCond = winCondComp
try:
win = CheckForWinner(win, moveCount, winCond)
except:
pass
userTurn = True
moveCount += 1