Terminal Game: Tic Tac Toe

#TICTACTOE GAME
print("""











        // WRITTEN BY TOMAS HOLMESTRAND //












########################################################
# Welcome to my first terminal game of Tic Tac Toe!    #
# To start your turn, type in the coordinates for      #
# your desired space (starting with the number,        #
# followed by the letter). Then hit enter.             #
########################################################





""")

numbered_coordinates = ' '.join(["  ", "1", "2", "3"])

l1 = ["a|", "▢", "▢", "▢"]
l2 = ["b|", "▢", "▢", "▢"]
l3 = ["c|", "▢", "▢", "▢"]


valid_input = ["1a", "2a", "3a", "1b", "2b", "3b", "1c", "2c", "3c"]

player = " "
false_input = 999
win = 111




def interaction(player):
	user_input = input('Coordinates:')

	if user_input in valid_input:
		if user_input == "1a":
			l1[1] = player
			valid_input.remove("1a")

		elif user_input == "2a":
			l1[2] = player
			valid_input.remove("2a")

		elif user_input == "3a":
			l1[3] = player
			valid_input.remove("3a")

		elif user_input == "1b":
			l2[1] = player
			valid_input.remove("1b")

		elif user_input == "2b":
			l2[2] = player
			valid_input.remove("2b")

		elif user_input == "3b":
			l2[3] = player
			valid_input.remove("3b")

		elif user_input == "1c":
			l3[1] = player
			valid_input.remove("1c")

		elif user_input == "2c":
			l3[2] = player
			valid_input.remove("2c")

		elif user_input == "3c":
			l3[3] = player
			valid_input.remove("3c")

	else: 
		return interaction(player)


def gameboard(l1p, l2p, l3p, player):
	print("It's {}'s turn!".format(player))
	print("""
	{nc}
	{l1p}
	{l2p}
	{l3p}






		""".format(nc = numbered_coordinates, l1p = l1p, l2p = l2p, l3p = l3p), end ='\r')

def check_win(l1, l2, l3, player):
	if l1[1] == player and l1[2] == player and l1[3] == player:
		return win
		#"{} WON!".format(player)
	if player in l2[1] and player in l2[2] and player in l2[3]:
		return win
	if player in l3[1] and player in l3[2] and player in l3[3]:
		return win
	if player in l1[1] and player in l2[1] and player in l3[1]:
		return win
	if player in l1[2] and player in l2[2] and player in l3[2]:
		return win
	if player in l1[3] and player in l2[3] and player in l3[3]:
		return win
	if player in l1[1] and player in l2[2] and player in l3[3]:
		return win
	if player in l1[3] and player in l2[2] and player in l3[1]:
		return win
	return

	
def playgame():
	turn_count = 1

	for i in range(1,10):
		
		if i % 2 != 0:
			player = "X"
		else: player = "O"

		l1p = ' '.join(l1)
		l2p = ' '.join(l2)
		l3p = ' '.join(l3)

		gameboard(l1p, l2p, l3p, player)

		
		interaction(player)

		l1p = ' '.join(l1)
		l2p = ' '.join(l2)
		l3p = ' '.join(l3)
		
		if check_win(l1, l2, l3, player) == win:
			print("""
	{nc}
	{l1p}
	{l2p}
	{l3p}
	""".format(nc = numbered_coordinates, l1p = l1p, l2p = l2p, l3p = l3p))
			print("GAME OVER, {player} WON!".format(player = player))
			exit()
		
		turn_count += 1
	print("""
	{nc}
	{l1p}
	{l2p}
	{l3p}
	""".format(nc = numbered_coordinates, l1p = l1p, l2p = l2p, l3p = l3p))
	print("IT'S A TIE!")


playgame()

Hi Thomas,

I got the following error when running your game, any prior setup needed?

Traceback (most recent call last):
File “main.py”, line 178, in
playgame()
File “main.py”, line 152, in playgame
interaction(player)
File “main.py”, line 57, in interaction
user_input = input(‘Coordinates:’)
EOFError: EOF when reading a line

Best,
César

1 Like

I can’t seem to figure it out myself either. A workaround is to copy the code into a text file, to run it locally through your desired console :slight_smile:

The Codebyte console is not interactive, so your code won’t run in it. The EOFError is thrown because there’s no way for the console to accept input. I’ll edit your post to include the code, but not in a Codebyte, so others won’t try to run it here, but can still copy it, and run it locally on their own machines.

1 Like