Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 23rd, 2005, 6:05 PM   #11
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Since we're doing TicTacToe, I think i'll give my one (and hopefully only, Jython runs 10 times slower than Java) frollic with Jython - TicTacToe with a Swing GUI.

import javax.swing as swing
import java.awt as awt
import java.lang

# Subclass the Swing JFrame
class tttwindow(swing.JFrame):

    def __init__(self):
        self.title = "Tic-Tac-Toe: Jython style."
        self.size = 200, 200
        # Use layouts to make a pretty GUI.
        self.contentPane.layout = awt.FlowLayout()
        # Set the current marker (X or O)
        self.currentMarker = "X"
        # Once the window closes kill the program (call self.exitprogram)
        self.windowClosing = self.exitProgram

        # Add all the buttons.
        self.buttons = [self.createButton(name) for name in range(1, 10)]
        for button in self.buttons:
            # Add the buttons to the window
            self.contentPane.add(button)
        self.show()
    def createButton(self, name):
        # We can set widget properties in the constructor
        b = swing.JButton(`name`, name=`name`, preferredSize=(50, 50))
        # On click call buttonPressed.
        b.actionPerformed = self.buttonPressed
        b.text = ""
        return b

    def buttonPressed(self, event):
        # - 1 because arrays start at 0 einstein.
        self.buttons[int(event.source.name) - 1].text = self.currentMarker
        # Disable the button.
        self.buttons[int(event.source.name) - 1].setEnabled(0)
        # Is there a win?
        self.checkWin(self.currentMarker)

        if self.currentMarker == "X":
            self.currentMarker = "O"
        else:
            self.currentMarker = "X"

    def checkWin(self, cur):
        # Create a string representation of the board,
        # i.e X-XX--OXX
        s = "".join([self.setValue(i) for i in range(0, 9)])
        print s

        # Check for top, middle and bottom row.
        if s[0:3] == (cur*3) or s[3:6] == (cur*3) or s[6:] == (cur*3):
            self.win(cur)
        # Check for left, center and right row.
        for blah in range(0, 3):
            pos = s.find(cur)
            # We'll get an error if we search out of range.
            # Just ignore it.
            try:
                if s[pos + 3] == cur and s[pos + 6] == cur:
                    self.win(cur)
            except:
                pass
        # Check for diagnols.
        if (s[0] == cur and s[4] == cur and s[8] == cur) or \
            (s[2] == cur and s[4] == cur and s[6] == cur):
            self.win(cur)

    def win(self, marker):
        again = swing.JOptionPane.showConfirmDialog(None, "Team " + marker + " win!\nPlay again?", "Tic-Tac-Toe", swing.JOptionPane.YES_NO_OPTION)
        # Don't play again..
        if again == 1:
            java.lang.System.exit(0)
        for button in self.buttons:
            button.text = ""
            button.setEnabled(1)

    def setValue(self, i):
        # Check if the value of i is null. If so, return "-", else return i.
        if self.buttons[i].text:
            return self.buttons[i].text
        return "-"


    def exitProgram(self, event):
        java.lang.System.exit(0)

tttwindow()
Cerulean is offline   Reply With Quote
Old Jun 24th, 2005, 8:45 AM   #12
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,827
Rep Power: 5 Sane will become famous soon enough
Wow, I've been looking for a way to make buttons (PyGTK doesn't work, or I don't know how to install it properly). Care to share how with Jython?
Sane is offline   Reply With Quote
Old Jun 25th, 2005, 5:49 AM   #13
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
Wow, I've been looking for a way to make buttons (PyGTK doesn't work, or I don't know how to install it properly). Care to share how with Jython?
I've already explained what you need to install for pyGTK. As for Jython.. I wouldn't really recommend it (for the speed reason) and because you can't use any extensions written in C/C++, so you lose access to a lot of the nicer Python modules out there. As for creating the button anyway, just look at the "createButton" method in the tttwindow class I created in that source submission.
If you really want to create buttons and graphical user interfaces (text boxes, check boxes, etc) then I would recommend wxPython. It's a simple download and installation, with fairly decent documentation.
Cerulean is offline   Reply With Quote
Old Jul 6th, 2005, 3:36 PM   #14
SaturN
Programmer
 
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4 SaturN is on a distinguished road
Just one comment on the ASCII Msn thing, why not just a code like
x = """
..########..
.#########.
.###.##.###.
.#########.	 <----- pic there!! <i ain't good at em, so excuse them please :D>
..###..###.. 
...##..##...  
...######...
	"""
x = "".join(x)
x = x.replace('.',':@')
x = x.replace('#',':)')
print x

Or does that not do the job??
__________________
while me is alive:
	make(life,simple)

Last edited by SaturN; Jul 6th, 2005 at 3:43 PM. Reason: boo boo!! :$
SaturN is offline   Reply With Quote
Old Jul 6th, 2005, 4:30 PM   #15
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,827
Rep Power: 5 Sane will become famous soon enough
Like I said, I don't know commands so I make up my way of doing them. lol. You think I read a tutorial for this language? hahahah Besides, that was a long time ago, in my like first week of python.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:18 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC