Forum Archive

How to reset dictionary of button images

techteej

Working on a Tic Tac Toe game, and would like to implement a clear board function, instead of closing and re-opening the view again. Any suggestions are appreciated.

# coding: utf-8

import ui
import random

global buttons
buttons = ['square1', 'square2', 'square3', 'square4', 'square5', 'square6', 'square7', 'square8','square9']

def clear_board(sender):
    # game outcome function here (TO-DO)
    buttons.image = None

def computer_pick(sender):
    try:
        global buttons
        computer = random.choice(buttons)
        computer1 = sender.superview[computer]
        computer1.image = ui.Image.named('ionicons-ios7-circle-outline-32')
        buttons.remove(computer)
    except IndexError: # for there being no more picks left
        clear_board(sender)

def button_tapped(sender):
    try:
        global buttons
        sender.image = ui.Image.named('ionicons-close-32')
        buttons.remove(sender.name)
        computer_pick(sender)
    except ValueError: # so the user does not pick the same as computer
        clear_board(sender)

v = ui.load_view('tic-tac-toe')
v.present('sheet')
ccc

It is difficult to make headway without your .pyui file.

buttons = ['square{}'.format(i) for i in xrange(1,10)]

The test of any good tic-tac-toe program should be that it never loses. Draws should be common, wins should be rare, but losses should never happen... http://en.m.wikipedia.org/wiki/Tic-tac-toe#Strategy

techteej

Thanks. I updated the repo with my .pyui file. Hopefully still works the same.