omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    How to reset dictionary of button images

    Pythonista
    game
    2
    3
    2197
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • techteej
      techteej last edited by 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')
      
      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by

        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

        1 Reply Last reply Reply Quote 0
        • techteej
          techteej last edited by

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

          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Powered by NodeBB Forums | Contributors