User:Qubodup/Tic Tac Toe

From FreeGameDevWiki

Jump to: navigation, search
Tic Tac Toe
Tic Tac Toe

A simple implementation of a simple game in a simple? language.

Source code

The source code is licensed under the WTFPL.

# The 'main' function
def tictactoe():
	# The welcome message.
	print("Hello and welcome to Tic Tac Toe, the grand battle between X and O!")
	# The players introduce themselves.
	pXname = raw_input("First player (X), what is your name? [Player 1] ")
	# If the player doesn't enter anything, the default name [Player 1] is selected.
	if len(pXname) == 0:
		pXname = "Player 1"
	pOname = raw_input("Second player (O), what about you? [Player 2] ")
	if len(pOname) == 0:
		pOname = "Player 2"
	# This game doesn't allow both players to use the same name. There's no confusion in this dojo.
	if pOname == pXname:
		pOname = "Not " + pXname
	# The fight is on!
	print pXname, "(X) versus", pOname, "(O), Fight!"
	# Quick instructions.
	print "Instructions: Use numbers, as they are positioned on the Numeric keypad, to place your symbols!"
	# Shows which number represents which cell in the field.
	printfield(range(1,10))
	# Creates a new game field.
	field = (init())
	# Prints the empty game field.
	printfield(field)
	# The main game loop, can only break from the inside.
	while True:
		# Player 1 turn.
		field = playerturn("X", pXname, field)
		# Prints the field after Player 1 made his move.
		printfield(field)
		# Checks weather Player 1 has won yet. If he has, pXwin becomse True.
		pXwin = checkwin(field, "X")
		# If there are no more free cells in the field or if Player 1 won, it's time to end the game.
		if freecells(field) == False or pXwin == True:
			break
		# Player 2 turn and so on.
		field = playerturn("O", pOname, field)
		printfield(field)
		pOwin = checkwin(field, "O")
		# We don't need to check if there are any free cells. This is Tic Tac Toe, remember?
		if pOwin == True:
			break
	# End messages, depend on wheater and who won.
	if pXwin:
		print ("Congratulations " + pXname + "! You win!")
	elif pOwin:
		print ("Congratulations " + pOname + "! You win!")
	else:
		print ("Draw!")
 
# Checks weather the input (a field) contains any " "'s and returns True if it does.
def freecells(field):
	for i in range(0,9):
		if field[i] == " ":
			return True
	return False
 
# Checks weather psymbol has scored a win or not.
def checkwin(field, psymbol):
	for i in range(0,3):
		if field[i] == field[i+3] == field[i+6] == psymbol:
			return True
	for i in [0, 3, 6]:
		if field[i] == field[i+1] == field[i+2] == psymbol:
			return True
	if field[0] == field[4] == field[8] == psymbol or field[2] == field[4] == field[6] == psymbol:
		return True
	else:
		return False
 
# Initializes the game field, which is a list containing " " nine times.
def init():
	field = []
	for i in range(0,9):
		field.append(" ")
	return field
 
# Prints a pretty ASCII representation of the input field.
def printfield(field):
	print "\n", field[6], "|", field[7], "|", field[8]
	print "--+---+--"
	print field[3], "|", field[4], "|", field[5]
	print "--+---+--"
	print field[0], "|", field[1], "|", field[2], "\n"
 
# Executes a player turn. The output is the field after the player's turn. The turn won't stop until the player makes a correct move.
def playerturn(psymbol, playername, field):
	cellok = False
	cell = raw_input(playername + ", your turn! ")
	while cellok != True:
		try:
			cell = int(cell)
			if cell >= 1 and cell <= 9:
				if field[cell-1] != " ":
					cell = raw_input("This cell isn't empty! ")
				else:
					field[cell-1] = psymbol
					cellok = True
			else:
				cell = raw_input("What? ")
		except(ValueError):
			cell = raw_input("What? ")
	return field
 
# Starts the game.
tictactoe()
Personal tools