#

# 3DTicTacToe.py

#  Written by Coty R Miller on 30Jan18.

#

board = [" ", " ", " ", \

" ", " ", " ", \

" ", " ", " ", \

" ", " ", " ", \

" ", " ", " ", \

" ", " ", " ", \

" ", " ", " ", \

" ", " ", " ", \

" ", " ", " ", ]

game = 1

player = "x"

# This fuction displays the game board layout.

def printGameBoard():

print(board, " | ", board, " | ", board)

print("-------------")

print(board, " | ", board, " | ", board, "a")

print("-------------")

print(board, " | ", board, " | ", board)

print("             ", board, " | ", board, " | ", board)

print("             -------------")

print("             ", board, " | ", board, " | ", board, "b")

print("             -------------")

print("             ", board, " | ", board, " | ", board)

print("             ","             ", board, " | ", board, " | ", board)

print("             ","             ","-------------")

print("             ","             ",board, " | ", board, " | ", board, "c")

print("             ","             ","-------------")

print("             ","             ",board, " | ", board, " | ", board)

#

# This fuction checks for wins.

#

def checkWins(bd):

win = 1

for i in range(0,3):

x = 9 * i

# Check horizontal wins

if(bd[x+0] == bd[x+1] and bd[x+1] == bd[x+2]):

if(bd[x+0]!=" "):

print("Player", bd[x+0], "wins!")

win = 0

if(bd[x+3] == bd[x+4] and bd[x+4] == bd[x+5]):

if(bd[x+3]!=" "):

print("Player", bd[x+3], "wins!")

win = 0

if(bd[x+6] == bd[x+7] and bd[x+7] == bd[x+8]):

if(bd[x+6]!=" "):

print("Player", bd[x+6], "wins!")

win = 0

# Check verticle wins

if(bd[x+0] == bd[x+3] and bd[x+3] == bd[x+6]):

if(bd[x+0]!=" "):

print("Player", bd[x+0], "wins!")

win = 0

if(bd[x+1] == bd[x+4] and bd[x+4] == bd[x+7]):

if(bd[x+1]!=" "):

print("Player", bd[x+1], "wins!")

win = 0

if(bd[x+2] == bd[x+5] and bd[x+5] == bd[x+8]):

if(bd[x+2]!=" "):

print("Player", bd[x+2], "wins!")

win = 0

# Check cris cross wins

if(bd[x+0] == bd[x+4] and bd[x+4] == bd[x+8]):

if(bd[x+0]!=" "):

print("Player", bd[x+0], "wins!")

win = 0

if(bd[x+2] == bd[x+4] and bd[x+4] == bd[x+6]):

if(bd[x+2]!=" "):

print("Player", bd[x+2], "wins!")

win = 0

return win

#

# This fuction "flips the cube" upwards

#

def flipCubeUp(z):

tr = [" "] * 27

for i in range(0,3):

x = 9 * i

y = 3 * i

# Build first row

tr[x+0] = z[y+18]

tr[x+1] = z[y+19]

tr[x+2] = z[y+20]

# Build second row

tr[x+3] = z[y+9]

tr[x+4] = z[y+10]

tr[x+5] = z[y+11]

# Build third row

tr[x+6] = z[y+0]

tr[x+7] = z[y+1]

tr[x+8] = z[y+2]

return tr

#

# This fuction "flips the cube" to the right

#

def flipCubeRight(z):

tr = [" "] * 27

for i in range(0,3):

x = 9 * i

# build first row

tr[x+0] = z[i+18]

tr[x+3] = z[i+21]

tr[x+6] = z[i+24]

# build second row

tr[x+1] = z[i+9]

tr[x+4] = z[i+12]

tr[x+7] = z[i+15]

# build third row

tr[x+2] = z[i+0]

tr[x+5] = z[i+3]

tr[x+8] = z[i+6]

return tr

def check3DCrissCross(z):

# This checks for 4 possable extreme win patterns.

win = 1

if z == z and z == z:

if(z !=" "):

print("Player", z, "wins!")

win = 0

if z == z and z == z:

if(z !=" "):

print("Player", z, "wins!")

win = 0

if z == z and z == z:

if(z !=" "):

print("Player", z, "wins!")

win = 0

if z == z and z == z:

if(z !=" "):

print("Player", z, "wins!")

win = 0

return win

#

# The actual game it's self, the game loop exits with game = 0.

#

while(game == 1):

printGameBoard()

# Get layer that player wishes to use

print("Player", player, "select a layer (a-c)")

c = input(":")

if c == "a":

c = 0

elif c == "b":

c = 9

elif c == "c":

c = 18

else:

print("ERROR! Please select a valid layer (a-c)")

c = 1

if c != 1:

# Get colision that player wants to plase/use/whatever

print("Player", player, "select a square 1-9")

a = input(":")

b = int(a)

if b < 10 and b > 0:

b = b - 1

else:

print("ERROR! Please enter a valid number (1-9)")

if(player == "x"):

if board[b+c] == " ":

board[b+c] = player

player = "o"

else:

else:

if board[b+c] == " ":

board[b+c] = player

player = "x"

else:

#

# Check for wins.

#

game = checkWins(board)

if game == 1:

game = checkWins(flipCubeUp(board))

if game == 1:

game = checkWins(flipCubeRight(board))

if game == 1:

game = check3DCrissCross(board)

# - - -

# print the winning game board.

printGameBoard()