![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
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() |
|
|
|
|
|
#12 |
|
Programming Guru
![]() |
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?
|
|
|
|
|
|
#13 | |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
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. |
|
|
|
|
|
|
#14 |
|
Programmer
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4
![]() |
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 xOr 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!! :$ |
|
|
|
|
|
#15 |
|
Programming Guru
![]() |
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. ![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|