In this video we’ll add the logic to our tic tac toe game with Kivy and Python.

In the last video we built out the gui and basic functionality of our tic tac toe game.

In this video we’ll determine if a person won, or if there was a tie.

We’ll change the button colors for wins, and tinker with the reset function a bit.

#kivy #codemy

Python Code: toe.py
GitHub Code: toe.py

from kivy.lang import Builder
from kivymd.app import MDApp

class MainApp(MDApp):
	def build(self):
		self.theme_cls.theme_style = "Dark"
		self.theme_cls.primary_palette = "BlueGray"
		return Builder.load_file('toe.kv')
	
	# Define Who's turn it is
	turn = "X"
	# Keep Track of win or lose
	winner = False

	# No Winner
	def no_winner(self):
		if self.winner == False and \
		self.root.ids.btn1.disabled == True and \
		self.root.ids.btn2.disabled == True and \
		self.root.ids.btn3.disabled == True and \
		self.root.ids.btn4.disabled == True and \
		self.root.ids.btn5.disabled == True and \
		self.root.ids.btn6.disabled == True and \
		self.root.ids.btn7.disabled == True and \
		self.root.ids.btn8.disabled == True and \
		self.root.ids.btn9.disabled == True:
			self.root.ids.score.text = "IT'S A TIE!!"

	# End The Game
	def end_game(self, a,b,c):
		self.winner = True
		a.color = "red"
		b.color = "red"
		c.color = "red"

		# Disable the buttons
		self.disable_all_buttons()

		# Set Label for winner
		self.root.ids.score.text = f"{a.text} Wins!"

	def disable_all_buttons(self):
		# Disable The Buttons
		self.root.ids.btn1.disabled = True
		self.root.ids.btn2.disabled = True
		self.root.ids.btn3.disabled = True
		self.root.ids.btn4.disabled = True
		self.root.ids.btn5.disabled = True
		self.root.ids.btn6.disabled = True
		self.root.ids.btn7.disabled = True
		self.root.ids.btn8.disabled = True
		self.root.ids.btn9.disabled = True

	def win(self):
		# Across
		if self.root.ids.btn1.text != "" and self.root.ids.btn1.text == self.root.ids.btn2.text and self.root.ids.btn2.text == self.root.ids.btn3.text:
			self.end_game(self.root.ids.btn1, self.root.ids.btn2, self.root.ids.btn3)

		if self.root.ids.btn4.text != "" and self.root.ids.btn4.text == self.root.ids.btn5.text and self.root.ids.btn5.text == self.root.ids.btn6.text:
			self.end_game(self.root.ids.btn4, self.root.ids.btn5, self.root.ids.btn6)

		if self.root.ids.btn7.text != "" and self.root.ids.btn7.text == self.root.ids.btn8.text and self.root.ids.btn8.text == self.root.ids.btn9.text:
			self.end_game(self.root.ids.btn7, self.root.ids.btn8, self.root.ids.btn9)
		# Down
		if self.root.ids.btn1.text != "" and self.root.ids.btn1.text == self.root.ids.btn4.text and self.root.ids.btn4.text == self.root.ids.btn7.text:
			self.end_game(self.root.ids.btn1, self.root.ids.btn4, self.root.ids.btn7)

		if self.root.ids.btn2.text != "" and self.root.ids.btn2.text == self.root.ids.btn5.text and self.root.ids.btn5.text == self.root.ids.btn8.text:
			self.end_game(self.root.ids.btn2, self.root.ids.btn5, self.root.ids.btn8)

		if self.root.ids.btn3.text != "" and self.root.ids.btn3.text == self.root.ids.btn6.text and self.root.ids.btn6.text == self.root.ids.btn9.text:
			self.end_game(self.root.ids.btn3, self.root.ids.btn6, self.root.ids.btn9)

		# Diagonal 
		if self.root.ids.btn1.text != "" and self.root.ids.btn1.text == self.root.ids.btn5.text and self.root.ids.btn5.text == self.root.ids.btn9.text:
			self.end_game(self.root.ids.btn1, self.root.ids.btn5, self.root.ids.btn9)

		if self.root.ids.btn3.text != "" and self.root.ids.btn3.text == self.root.ids.btn5.text and self.root.ids.btn5.text == self.root.ids.btn7.text:
			self.end_game(self.root.ids.btn3, self.root.ids.btn5, self.root.ids.btn7)

		self.no_winner()

	def presser(self, btn):
		if self.turn == 'X':
			btn.text = "X"
			btn.disabled = True
			self.root.ids.score.text = "O's Turn!"
			self.turn = "O"
		else:
			btn.text = "O"
			btn.disabled = True
			self.root.ids.score.text = "X's Turn!"
			self.turn = "X"

		# Check To See if won
		self.win()

	def restart(self):
		# Reset Who's Turn It Is
		self.turn = "X"

		# Enable The Buttons
		self.root.ids.btn1.disabled = False
		self.root.ids.btn2.disabled = False
		self.root.ids.btn3.disabled = False
		self.root.ids.btn4.disabled = False
		self.root.ids.btn5.disabled = False
		self.root.ids.btn6.disabled = False
		self.root.ids.btn7.disabled = False
		self.root.ids.btn8.disabled = False
		self.root.ids.btn9.disabled = False

		# Clear The Buttons
		self.root.ids.btn1.text = ""
		self.root.ids.btn2.text = ""
		self.root.ids.btn3.text = ""
		self.root.ids.btn4.text = ""
		self.root.ids.btn5.text = ""
		self.root.ids.btn6.text = ""
		self.root.ids.btn7.text = ""
		self.root.ids.btn8.text = ""
		self.root.ids.btn9.text = ""

		# Reset The Button Colors
		self.root.ids.btn1.color = "green"
		self.root.ids.btn2.color = "green"
		self.root.ids.btn3.color = "green"
		self.root.ids.btn4.color = "green"
		self.root.ids.btn5.color = "green"
		self.root.ids.btn6.color = "green"
		self.root.ids.btn7.color = "green"
		self.root.ids.btn8.color = "green"
		self.root.ids.btn9.color = "green"

		# Reset The Score Label
		self.root.ids.score.text = "X GOES FIRST!"

		# Reset The Winner Variable
		self.winner = False

	
MainApp().run()

Kivy Design Code: toe.kv
GitHub Code: toe.kv

MDFloatLayout:

	MDGridLayout:
		size_hint: .5, .5
		pos_hint: {'center_x': .5, 'center_y': .7}
		cols: 3
		rows: 3

		Button:
			id: btn1
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn1)

		Button:
			id: btn2
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn2)

		Button:
			id: btn3
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn3)

		Button:
			id: btn4
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn4)

		Button:
			id: btn5
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn5)

		Button:
			id: btn6
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn6)

		Button:
			id: btn7
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn7)

		Button:
			id: btn8
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn8)

		Button:
			id: btn9
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn9)

	MDLabel:
		id: score
		font_size: "32sp"
		text: "X GOES FIRST!"
		halign: "center"
		pos_hint: {"center_y": .3}

	MDRaisedButton:
		id: restart
		text: "Restart The Game"
		pos_hint: {'center_x': .5, 'center_y': .15}
		on_release: app.restart()

		

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

Your email address will not be published. Required fields are marked *

John Elder