Forum Archive

Tic tac toe

janAaron

I've (with a lot of help) made this basic tic tac toe game. Right now when you tap on one of the clocks it turns into an "X", but I want to make it so that every other click the clock turns into an "O"; that way two people can play. How would I do that?

Here's the code:

#coding: utf-8
import ui

def pressed (sender):
    sender.tint_color = 1.00, 0.00, 0.00
    button1.background_image = ui.Image.named('ionicons-close-24')

def jumbo (sender):
    button2.background_image = ui.Image.named('ionicons-close-round-24')

def pretzel (sender):
    button3.background_image = ui.Image.named('ionicons-close-24')

def turkey (sender):
    button4.background_image = ui.Image.named('ionicons-close-round-24')

def yoohoo (sender):
    button5.background_image = ui.Image.named('ionicons-close-24')

def domino (sender):
    button6.background_image = ui.Image.named('ionicons-close-24')

def tubs (sender):
    button7.background_image = ui.Image.named('ionicons-close-24')

def oops (sender):
    button8.background_image = ui.Image.named('ionicons-close-24')

def ager (sender):
    button9.background_image = ui.Image.named('ionicons-close-24')


# view established
view = ui.View()
view.name = "tic tac toe"
view.background_color = 0.90, 0.90, 0.90
view.present("sheet")
view.background_color = 0.00, 1.00, 1.00


# first button
button1 = ui.Button()
button1.background_image = ui.Image.named('ionicons-clock-24')
button1.flex = "LRTB"
button1.action = pressed
view.add_subview(button1)
button1.center = (view.width * 0.4, view.height * 0.1)
button1.size_to_fit()


#second button
button2 = ui.Button()
button2.background_image = ui.Image.named('ionicons-clock-24')
button2.flex = "LRTB"
button2.action = jumbo
view.add_subview(button2)
button2.center = (view.width * 0.7, view.height * 0.1)
button2.size_to_fit()

# third button
button3 = ui.Button()
button3.background_image = ui.Image.named('ionicons-clock-24')
button3.flex = "LRTB"
button3.action = pretzel
view.add_subview(button3)
button3.center = (view.width * 0.2, view.height * 0.1)
button3.size_to_fit()

# fourth button
button4 = ui.Button()
button4.background_image = ui.Image.named('ionicons-clock-24')
button4.flex = "LRTB"
button4.action = turkey
view.add_subview(button4)
button4.center = (view.width * 0.2, view.height * 0.4)
button4.size_to_fit()

# fifth button
button5 = ui.Button()
button5.background_image = ui.Image.named('ionicons-clock-24')
button5.flex = "LRTB"
button5.action = yoohoo
view.add_subview(button5)
button5.center = (view.width * 0.4, view.height * 0.4)
button5.size_to_fit()

# sixth button
button6 = ui.Button()
button6.background_image = ui.Image.named('ionicons-clock-24')
button6.flex = "LRTB"
button6.action = domino
view.add_subview(button6)
button6.center = (view.width * 0.7, view.height * 0.4)
button6.size_to_fit()

# seventh button 
button7 = ui.Button()
button7.background_image = ui.Image.named('ionicons-clock-24')
button7.flex = "LRTB"
button7.action = tubs
view.add_subview(button7)
button7.center = (view.width * 0.2, view.height * 0.7)
button7.size_to_fit()

# eighth button
button8 = ui.Button()
button8.background_image = ui.Image.named('ionicons-clock-24')
button8.flex = "LRTB"
button8.action = oops
view.add_subview(button8)
button8.center = (view.width * 0.4, view.height * 0.7)
button8.size_to_fit()

# ninth button
button9 = ui.Button()
button9.background_image = ui.Image.named('ionicons-clock-24')
button9.flex = "LRTB"
button9.action = ager
view.add_subview(button9)
button9.center = (view.width * 0.7, view.height * 0.7)
button9.size_to_fit()
ccc
images = { 'X' : ui.Image.named('ionicons-close-256'),
           'O' : ui.Image.named('ionicons-ios7-circle-outline-256') }

def pressed(sender):
    sender.tint_color = 1.00, 0.00, 0.00
    if sender.background_image == images['X']:
        sender.background_image = images['O']
    else:
        sender.background_image = images['X']

This version of pressed() uses sender so it could be the action assigned to all nine squares.

For other hints, see: http://omz-forums.appspot.com/pythonista/post/5788189516103680

janAaron

Thanks!

Sebastian

I made a Tic-tac-toe game a while back. I uploaded it as a gist now, you can take a look ;)