Skip to content

Instantly share code, notes, and snippets.

@msyvr
Last active September 2, 2021 19:45
Show Gist options
  • Save msyvr/3773cf7659f691d20402bb41f3e7e6f3 to your computer and use it in GitHub Desktop.
Save msyvr/3773cf7659f691d20402bb41f3e7e6f3 to your computer and use it in GitHub Desktop.
import random
rowIndex = [1,2,3]
colIndex = [1,2,3]
diagset=[(1,1), (2,2), (3,3)]
antidiagset=[(1,3), (2,2), (3,1)]
availablePositions=[]
playerPositions=[]
computerPositions=[]
pDiagCount=0
pAntiDiagCount=0
cDiagCount=0
cAntiDiagCount=0
row=[]
pRowIndices=[]
col=[]
pColIndices=[]
cRow=[]
cRowIndices=[]
cCol=[]
cColIndices=[]
winner=False
# create the playing board and set available positions
# (the board setup is for a randomly sized game, but the rest is just for regular 3x3 ttt)
for i in range(len(rowIndex)):
for j in range(len(colIndex)):
availablePositions.append((rowIndex[i],colIndex[j]))
#check that there is no winner yet, and that there are available spots on the board
while (winner!=True & len(availablePositions) > 0):
# for each round of play print available positions
print(f'The available positions are: {availablePositions}')
# ask player to pick a spot, validate input format
while True:
try:
print(f'Make your play as row index, column index: ')
(n, m) = tuple(map(int, input().split(",")))
while n>3 or m>3 or (n, m) not in availablePositions:
print(f'Please select an available position from the list above: ')
(n, m) = tuple(map(int, input().split(",")))
except ValueError:
print("Enter as format: integer, integer")
continue
else:
break
# check if player won
pRowIndices.append(n)
pColIndices.append(m)
playerPositions.append((n,m))
print(f'Player positions: {playerPositions}')
if (n,m) in diagset:
pDiagCount+=1
if (n,m) in antidiagset:
pAntiDiagCount+=1
if pDiagCount>2 or pAntiDiagCount>2:
print('You win with a diagonal!')
winner=True
# if player has at least 3 positions selected, check if row or col filled
if len(playerPositions)>2:
# check if player wins, which requires either all of either
# row or col indices the same OR if no row (col) indices repeat
pRowsSame = max(pRowIndices.count(1), pRowIndices.count(2), pRowIndices.count(3))
if pRowsSame >= 3:
print('You win with a row!')
winner=True
else:
pColsSame = max(pColIndices.count(1), pColIndices.count(2), pColIndices.count(3))
if pColsSame>=3:
print('You win with a column!')
winner=True
#update availablePositions
availablePositions.remove((n,m))
# if player didn't win, check if there are any available positions
# if so, assign one randomly to the automated/computer opponent
if winner==True:
break
if len(availablePositions)>0:
compChoice=random.choice(availablePositions)
cRowIndices.append(compChoice[0])
cColIndices.append(compChoice[1])
computerPositions.append(compChoice)
print(f'Computer plays: {computerPositions}')
if (compChoice[0],compChoice[1]) in diagset:
cDiagCount+=1
if (compChoice[0],compChoice[1]) in antidiagset:
cAntiDiagCount+=1
if cDiagCount>2 or cAntiDiagCount>2:
print('Computer wins with a diagonal!')
winner=True
# if computer has at least 3 positions selected, check if row or col filled
if len(computerPositions)>2:
# check if player wins, which requires either all of either
# row or col indices the same OR if no row (col) indices repeat
cRowsSame = max(cRowIndices.count(1), cRowIndices.count(2), cRowIndices.count(3))
if cRowsSame >= 3:
print('Computer wins with a row!')
winner=True
else:
cColsSame = max(cColIndices.count(1), cColIndices.count(2), cColIndices.count(3))
if cColsSame>=3:
print('Computer wins with a column!')
winner=True
#update availablePositions
availablePositions.remove(compChoice)
if winner!=True:
print('No winner this time')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment