Python style questions

François Granger francois.granger at free.fr
Sat Mar 17 11:20:43 EST 2001


Alex Martelli <aleaxit at yahoo.com> wrote:

> If you give us a specific, similarly-small example that
> you dream to be able to code with a switch (and are
> presumably now coding with if/elif), we can help you
> translate this into some form of dispatching...

Thanks for the proposal. And let expose my realy bad coding style :-)
(I am not a programmer, and most if not all I do in Python is at home,
by night, and for my pleasure)

In order to exercise some Neural Network and Genetic Algorithm that I am
currently trying to code, I wrote a simple TicTacToe game. The idea is
that I want to have the following possibilities:
- one player can choose a token between 'X' and 'O'
- games can be human against one of NN or GA or NN against GA or even
two different NN or GA against each other. These NN or GA are handled by
the player class wich in turn call the corresponding class.
- one of them is the starter of the game.

To call a non human player, I have a simple class 'player' with a
methode play() wich return the move as a tuple of integer between 0 & 2.
It has also to handle the feed back about win or lost to allow for NN or
GA to "learn".

I am currently on the following dispatch issue wich is far from corect:

def game():
    print 'Starting game.'
    a = tictactoe() # create a new game
    ans = raw_input('Who start (h/c) : ')
    if  (ans[0]=='q' or ans[0]=='Q'):
        return
    human = userinput   # proc to handle human interaction
    c = player('X') # create a machine player
    computer = c.play
    if (ans[0]=='h' or ans[0]=='H'):
        first, second = human, computer
    else:
        first, second = computer, human
    while 1:
        if a.turn % 2:
            move, token = second(a)
        else:
            move, token = first(a)
        a.valid(move, token)
        a.display()
        win = a.won()
        if win == 'tie':
            print 'tie'
            # call player with tie
            # exit game
        elif win == 'X':
            print 'X'
            # call player with X
            # exit game
        elif win == 'O':
            print 'O'
            # call player with O
            # exit game
        else:
            continue



-- 
Une faq de frj: http://faq.jardin.free.fr/, près de l'encyclopédie
http://nature.jardin.free.fr/
MacPython beginners http://francois.granger.free.fr/MacPython/faq/



More information about the Python-list mailing list