[gnome-games/gnibbles-clutter] Loading pixmaps in main.c



commit e902b9f3f9547ff77cb5a419c627821ef15608d8
Author: Guillaume Beland <guillaubel svn gnome org>
Date:   Sun May 24 19:12:53 2009 -0400

    Loading pixmaps in main.c
    
    I still have an annoying segfault if I try to delete pixmaps related functions
    in board.c. I'm clueless about this one since I don't even use the code i'm
    removing and it segfault if it's not there.
---
 glchess/src/lib/game.py |  812 -----------------------------------------------
 glchess/src/lib/uci.py  |  185 -----------
 gnibbles/board.c        |  173 +++++-----
 gnibbles/main.c         |  111 +++++++-
 gnibbles/worm-clutter.c |    8 +-
 5 files changed, 201 insertions(+), 1088 deletions(-)

diff --git a/glchess/src/lib/game.py b/glchess/src/lib/game.py
deleted file mode 100644
index 9b50726..0000000
--- a/glchess/src/lib/game.py
+++ /dev/null
@@ -1,812 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
-"""
-
-__author__ = 'Robert Ancell <bob27 users sourceforge net>'
-__license__ = 'GNU General Public License Version 2'
-__copyright__ = 'Copyright 2005-2006  Robert Ancell'
-
-import chess.board
-import chess.san
-
-# Game results
-RESULT_IN_PROGRESS         = '*'
-RESULT_WHITE_WINS          = '1-0'
-RESULT_BLACK_WINS          = '0-1'
-RESULT_DRAW                = '1/2-1/2'
-
-# Reasons for the result
-RULE_CHECKMATE             = 'CHECKMATE'
-RULE_STALEMATE             = 'STALEMATE'
-RULE_TIMEOUT               = 'TIMEOUT'
-RULE_FIFTY_MOVES           = 'FIFTY_MOVES'
-RULE_THREE_FOLD_REPETITION = 'THREE_FOLD_REPETITION'
-RULE_INSUFFICIENT_MATERIAL = 'INSUFFICIENT_MATERIAL'
-RULE_RESIGN                = 'RESIGN'
-RULE_DEATH                 = 'DEATH'
-RULE_AGREEMENT             = 'AGREEMENT'
-RULE_ABANDONMENT           = 'ABANDONMENT'
-
-class ChessMove:
-    """
-    """
-
-    # The move number (game starts at 0)
-    number     = 0
-    
-    # The player and piece that moved
-    player     = None
-    piece      = None
-    
-    # The piece that was promoted to (or None)
-    promotion  = None
-    
-    # The victim piece (or None)
-    victim     = None
-
-    # The start and end position of the move
-    start      = None
-    end        = None
-    
-    # The move in CAN and SAN format
-    canMove    = ''
-    sanMove    = ''
-
-    # The game result after this move
-    opponentInCheck = False
-    opponentCanMove = False
-    
-    # If this move can be used as a resignation
-    fiftyMoveRule = False
-    threeFoldRepetition = False
-    
-    # A comment about this move
-    comment = ''
-
-    # Numeric annotation glyph for move
-    nag     = ''
-
-class ChessPlayer:
-    """
-    """
-
-    def __init__(self, name):
-        """Constructor for a chess player.
-
-        'name' is the name of the player.
-        """
-        self.__name = str(name)
-        self.__game = None
-        self.__readyToMove = False
-        self.isAlive = True
-
-    # Methods to extend
-
-    def onPieceMoved(self, piece, start, end, delete):
-        """Called when a chess piece is moved.
-        
-        'piece' is the piece that has been moved (chess.board.ChessPiece).
-        'start' is the location the piece in LAN format (string) or None if the piece has been created.
-        'end' is the location the piece has moved to in LAN format (string).
-        'delete' is a flag to show if the piece should be deleted when it arrives there (boolean).
-        """
-        pass
-    
-    def onPlayerMoved(self, player, move):
-        """Called when a player has moved.
-        
-        'player' is the player that has moved (ChessPlayer).
-        'move' is the record for this move (ChessMove).
-        """
-        pass
-    
-    def onUndoMove(self):
-        pass
-    
-    def onPlayerStartTurn(self, player):
-        pass
-
-    def onGameEnded(self, game):
-        """Called when a chess game has ended.
-        
-        'game' is the game that has ended (Game).
-        """
-        pass
-    
-    def readyToMove(self):
-        """FIXME
-        """
-        pass
-
-    # Public methods
-
-    def getName(self):
-        """Get the name of this player.
-        
-        Returns the player name (string).
-        """
-        return self.__name
-    
-    def getGame(self):
-        """Get the game this player is in.
-        
-        Returns the game (Game) or None if not in a game.
-        """
-        return self.__game
-    
-    def getRemainingTime(self):
-        """Get the amount of time this player has remaining.
-        
-        Returns the amount of time in milliseconds.
-        """
-        if self is self.__game.getWhite():
-            timer = self.__game.whiteTimer
-        elif self is self.__game.getBlack():
-            timer = self.__game.blackTimer
-        else:
-            return 0
-        
-        if timer is None:
-            return 0
-        else:
-            return timer.controller.getRemaining()
-
-    def isReadyToMove(self):
-        """
-        """
-        return self.__readyToMove
-    
-    def canMove(self, start, end, promotionType = chess.board.QUEEN):
-        """
-        """
-        return self.__game.canMove(self, start, end, promotionType)
-
-    def move(self, move):
-        """Move a piece.
-        
-        'move' is the move to make in Normal/Long/Standard Algebraic format (string).
-        """
-        self.__game.move(self, move)
-        
-    def undo(self):
-        """Undo moves until it is this players turn"""
-        self.__game.undo(self)
-        
-    def endMove(self):
-        """Complete this players turn"""
-        self.__game.endMove(self)
-        
-    def resign(self):
-        """Resign from the game"""
-        self.__game.resign(self)
-        
-    def claimDraw(self):
-        """Claim a draw"""
-        return self.__game.claimDraw()
-
-    def outOfTime(self):
-        """Report this players timer has expired"""
-        self.__game.outOfTime(self)
-        
-    def die(self):
-        """Report this player has died"""
-        self.isAlive = False
-        if self.__game is not None:
-            self.__game.killPlayer(self)
-
-    # Private methods
-    
-    def _setGame(self, game):
-        """
-        """
-        self.__game = game
-        
-    def _setReadyToMove(self, readyToMove):
-        if self.__readyToMove == readyToMove:
-            return
-        self.__readyToMove = readyToMove
-        if readyToMove is True:
-            self.readyToMove()
-
-class ChessGameBoard(chess.board.ChessBoard):
-    """
-    """
-    
-    def __init__(self, game):
-        """
-        """
-        self.__game = game
-        chess.board.ChessBoard.__init__(self)
-
-    def onPieceMoved(self, piece, start, end, delete):
-        """Called by chess.board.ChessBoard"""
-        self.__game._onPieceMoved(piece, start, end, delete)
-
-class ChessGameSANConverter(chess.san.SANConverter):
-    """
-    """
-        
-    __colourToSAN = {chess.board.WHITE: chess.san.SANConverter.WHITE,
-                     chess.board.BLACK: chess.san.SANConverter.BLACK}
-    __sanToColour = {}
-    for (a, b) in __colourToSAN.iteritems():
-        __sanToColour[b] = a
-        
-    __typeToSAN = {chess.board.PAWN:   chess.san.SANConverter.PAWN,
-                   chess.board.KNIGHT: chess.san.SANConverter.KNIGHT,
-                   chess.board.BISHOP: chess.san.SANConverter.BISHOP,
-                   chess.board.ROOK:   chess.san.SANConverter.ROOK,
-                   chess.board.QUEEN:  chess.san.SANConverter.QUEEN,
-                   chess.board.KING:   chess.san.SANConverter.KING}
-    __sanToType = {}
-    for (a, b) in __typeToSAN.iteritems():
-        __sanToType[b] = a
-        
-    def __init__(self, board, moveNumber):
-        self.board = board
-        self.moveNumber = moveNumber
-        chess.san.SANConverter.__init__(self)
-    
-    def decode(self, colour, move):
-        (start, end, result, promotionType) = chess.san.SANConverter.decode(self, self.__colourToSAN[colour], move)
-        return (start, end, self.__sanToType[promotionType])
-    
-    def encode(self, start, end, isTake, promotionType):
-        if promotionType is None:
-            promotion = self.QUEEN
-        else:
-            promotion = self.__typeToSAN[promotionType]
-        return chess.san.SANConverter.encode(self, start, end, isTake, promotion)
-
-    def getPiece(self, location):
-        """Called by chess.san.SANConverter"""
-        piece = self.board.getPiece(location, self.moveNumber)
-        if piece is None:
-            return None
-        return (self.__colourToSAN[piece.getColour()], self.__typeToSAN[piece.getType()])
-    
-    def testMove(self, colour, start, end, promotionType, allowSuicide = False):
-        """Called by chess.san.SANConverter"""
-        move = self.board.testMove(self.__sanToColour[colour], start, end,
-                                     self.__sanToType[promotionType], allowSuicide, self.moveNumber)
-        if move is None:
-            return False
-
-        if move.opponentInCheck:
-            if not move.opponentCanMove:
-                return chess.san.SANConverter.CHECKMATE
-            return chess.san.SANConverter.CHECK
-        return True
-
-class ChessGame:
-    """
-    """    
-
-    def __init__(self):
-        """Game constructor"""
-        self.__players = []
-        self.__spectators = []
-        self.__whitePlayer = None
-        self.__blackPlayer = None
-        self.__currentPlayer = None
-        self.__moves = []
-        self.__inCallback = False
-        self.__queuedCalls = []
-        self.board = ChessGameBoard(self)
-
-        self.__started = False
-        self.result  = RESULT_IN_PROGRESS
-        self.rule    = None    
-        self.whiteTimer = None
-        self.blackTimer = None
-        
-    def getAlivePieces(self, moveNumber = -1):
-        """Get the alive pieces on the board.
-        
-        'moveNumber' is the move to get the pieces from (integer).
-        
-        Returns a dictionary of the alive pieces (board.ChessPiece) keyed by location.
-        Raises an IndexError exception if moveNumber is invalid.
-        """
-        return self.board.getAlivePieces(moveNumber)
-    
-    def getDeadPieces(self, moveNumber = -1):
-        """Get the dead pieces from the game.
-        
-        'moveNumber' is the move to get the pieces from (integer).
-        
-        Returns a list of the pieces (board.ChessPiece) in the order they were killed.
-        Raises an IndexError exception if moveNumber is invalid.
-        """
-        return self.board.getDeadPieces(moveNumber)
-    
-    def setTimers(self, whiteTimer, blackTimer):
-        """
-        """
-        self.whiteTimer = whiteTimer
-        self.blackTimer = blackTimer
-
-    def setWhite(self, player):
-        """Set the white player in the game.
-        
-        'player' is the player to use as white.
-        
-        If the game has started or there is a white player an exception is thrown.
-        """
-        assert(self.__started is False)
-        assert(self.__whitePlayer is None)
-        self.__whitePlayer = player
-        self.__connectPlayer(player)
-
-    def getWhite(self):
-        """Returns the current white player (player.Player)"""
-        return self.__whitePlayer
-    
-    def setBlack(self, player):
-        """Set the black player in the game.
-        
-        'player' is the player to use as black.
-        
-        If the game has started or there is a black player an exception is thrown.
-        """
-        assert(self.__started is False)
-        assert(self.__blackPlayer is None)
-        self.__blackPlayer = player
-        self.__connectPlayer(player)
-        
-    def getBlack(self):
-        """Returns the current white player (player.Player)"""
-        return self.__blackPlayer
-    
-    def getCurrentPlayer(self):
-        """Get the player to move"""
-        return self.__currentPlayer
-    
-    def addSpectator(self, player):
-        """Add a spectator to the game.
-        
-        'player' is the player spectating.
-        
-        This can be called after the game has started.
-        """
-        self.__spectators.append(player)
-        self.__connectPlayer(player)
-
-    def isStarted(self):
-        """Returns True if the game has been started"""
-        return self.__started
-        
-    def start(self, moves = []):
-        """Start the game.
-        
-        'moves' is a list of moves to start with.
-        
-        If there is no white or black player then an exception is raised.
-        """
-        assert(self.__whitePlayer is not None and self.__blackPlayer is not None)
-        
-        # Disabled for now
-        #import network
-        #self.x = network.GameReporter('Test Game', 12345)
-        #print 'Reporting'
-
-        # Load starting moves
-        self.__currentPlayer = self.__whitePlayer
-        for move in moves:
-            self.move(self.__currentPlayer, move)
-            if self.__currentPlayer is self.__whitePlayer:
-                self.__currentPlayer = self.__blackPlayer
-            else:
-                self.__currentPlayer = self.__whitePlayer
-
-        self.__started = True
-
-        # Stop if both players aren't alive
-        if not self.__whitePlayer.isAlive:
-            self.killPlayer(self.__whitePlayer)
-            return
-        if not self.__blackPlayer.isAlive:
-            self.killPlayer(self.__blackPlayer)
-            return
-
-        # Stop if game ended on loaded moves
-        if self.result != RESULT_IN_PROGRESS:
-            self._notifyEndGame()
-            return
-
-        self.startLock()
-        
-        # Inform other players of the result
-        for player in self.__players:
-            player.onPlayerStartTurn(self.__currentPlayer)
-
-        # Get the next player to move
-        self.__currentPlayer._setReadyToMove(True)
-
-        self.endLock()
-
-    def getSquareOwner(self, coord):
-        """TODO
-        """
-        piece = self.board.getPiece(coord)
-        if piece is None:
-            return None
-        
-        colour = piece.getColour()
-        if colour is chess.board.WHITE:
-            return self.__whitePlayer
-        elif colour is chess.board.BLACK:
-            return self.__blackPlayer
-        else:
-            return None
-        
-    def canMove(self, player, start, end, promotionType):
-        """Test if a player can move.
-        
-        'player' is the player making the move.
-        'start' is the location to move from in LAN format (string).
-        'end' is the location to move from in LAN format (string).
-        'promotionType' is the piece type to promote pawns to. FIXME: Make this a property of the player
-        
-        Return True if can move, otherwise False.
-        """
-        if player is not self.__currentPlayer:
-            return False
-        
-        if player is self.__whitePlayer:
-            colour = chess.board.WHITE
-        elif player is self.__blackPlayer:
-            colour = chess.board.BLACK
-        else:
-            assert(False)
-
-        move = self.board.testMove(colour, start, end, promotionType = promotionType)
-
-        return move is not None
-    
-    def move(self, player, move):
-        """Get a player to make a move.
-        
-        'player' is the player making the move.
-        'move' is the move to make in SAN or LAN format (string).
-        """
-        if self.__inCallback:
-            self.__queuedCalls.append((self.move, (player, move)))
-            return
-        
-        self.startLock()
-        
-        if player is not self.__currentPlayer:
-            print 'Player attempted to move out of turn'
-        else:
-            self._move(player, move)
-
-        self.endLock()
-
-    def undo(self, player):
-        if self.__inCallback:
-            self.__queuedCalls.append((self.undo, (player,)))
-            return
-        
-        self.startLock()
-        
-        self.__whitePlayer._setReadyToMove(False)
-        self.__blackPlayer._setReadyToMove(False)
-        
-        # Pretend the current player is the oponent so when endMove() is called
-        # this player will become the active player.
-        # Yes, this IS a big hack...
-        if player is self.__whitePlayer:
-            self.__currentPlayer = self.__blackPlayer
-        else:
-            self.__currentPlayer = self.__whitePlayer
-        
-        # If this player hasn't moved then undo oponents move before their one
-        if len(self.__moves) > 0 and self.__moves[-1].player is not player:
-            count = 2
-        else:
-            count = 1
-            
-        for i in xrange(count):
-            if len(self.__moves) != 0:
-                self.board.undo()
-                self.__moves = self.__moves[:-1]
-                for p in self.__players:
-                    p.onUndoMove()
-        
-        self.endLock()
-        
-    def startLock(self):
-        assert(self.__inCallback is False)
-        self.__inCallback = True
-        
-    def endLock(self):
-        self.__inCallback = False
-        while len(self.__queuedCalls) > 0:
-            (call, args) = self.__queuedCalls[0]
-            self.__queuedCalls = self.__queuedCalls[1:]
-            call(*args)
-
-    def _move(self, player, move):
-        """
-        """
-        if self.result != RESULT_IN_PROGRESS:
-            print 'Game completed'
-            return
-        
-        if self.__currentPlayer is self.__whitePlayer:
-            colour = chess.board.WHITE
-        else:
-            colour = chess.board.BLACK
-
-        # If move is SAN process it as such
-        try:
-            (start, end, _, _, promotionType, _) = chess.lan.decode(colour, move)
-        except chess.lan.DecodeError, e:
-            converter = ChessGameSANConverter(self.board, len(self.__moves))
-            try:
-                (start, end, promotionType) = converter.decode(colour, move)
-            except chess.san.Error, e:
-                print 'Invalid move: ' + move
-                return
-
-        # Only use promotion type if a pawn move to far file
-        piece = self.board.getPiece(start)
-        promotion = None
-        if piece is not None and piece.getType() is chess.board.PAWN:
-            if colour is chess.board.WHITE:
-                if end[1] == '8':
-                    promotion = promotionType
-            else:
-                if end[1] == '1':
-                    promotion = promotionType
-
-        moveResult = self.board.movePiece(colour, start, end, promotionType)
-        if moveResult is None:
-            print 'Illegal move: ' + str(move)
-            return
-        
-        # Re-encode for storing and reporting
-        canMove = chess.lan.encode(colour, start, end, promotionType = promotion)
-        converter = ChessGameSANConverter(self.board, len(self.__moves))
-        try:
-            sanMove = converter.encode(start, end, moveResult.victim != None, promotionType)
-        except chess.san.Error:
-            # If for some reason we couldn't make the SAN move the use the CAN move instead
-            sanMove = canMove
-
-        m = ChessMove()
-        if len(self.__moves) == 0:
-            m.number = 1
-        else:
-            m.number = self.__moves[-1].number + 1
-        m.player              = self.__currentPlayer
-        m.piece               = piece
-        m.victim              = moveResult.victim
-        m.start               = start
-        m.end                 = end
-        m.canMove             = canMove
-        m.sanMove             = sanMove
-        m.opponentInCheck     = moveResult.opponentInCheck
-        m.opponentCanMove     = moveResult.opponentCanMove
-        m.fiftyMoveRule       = moveResult.fiftyMoveRule
-        m.threeFoldRepetition = moveResult.threeFoldRepetition
-        #FIXME: m.comment             = move.comment
-        #FIXME: m.nag                 = move.nag
-
-        self.__moves.append(m)
-
-        # This player has now moved
-        self.__currentPlayer._setReadyToMove(False)
-
-        # Inform other players of the result
-        for player in self.__players:
-            player.onPlayerMoved(self.__currentPlayer, m)
-
-        # Check if the game has ended
-        result = RESULT_IN_PROGRESS
-        if not m.opponentCanMove:
-            if self.__currentPlayer is self.__whitePlayer:
-                result = RESULT_WHITE_WINS
-            else:
-                result = RESULT_BLACK_WINS
-            if m.opponentInCheck:
-                rule = RULE_CHECKMATE
-            else:
-                result = RESULT_DRAW
-                rule = RULE_STALEMATE
-
-        # Check able to complete
-        if not self.board.sufficientMaterial():
-            result = RESULT_DRAW
-            rule = RULE_INSUFFICIENT_MATERIAL
-
-        if result is not RESULT_IN_PROGRESS:
-            self.endGame(result, rule)
-
-    def endMove(self, player):
-        """
-        """
-        if self.__inCallback:
-            self.__queuedCalls.append((self.endMove, (player,)))
-            return
-        
-        if player is not self.__currentPlayer:
-            print 'Player attempted to move out of turn'
-            return
-        if player.move is None:
-            print "Ending move when haven't made one"
-            return
-
-        if self.__currentPlayer is self.__whitePlayer:
-            self.__currentPlayer = self.__blackPlayer
-        else:
-            self.__currentPlayer = self.__whitePlayer
-        
-        self.startLock()
-        
-        # Inform other players of the result
-        for player in self.__players:
-            player.onPlayerStartTurn(self.__currentPlayer)
-
-        # Notify the next player they can move
-        if self.__started is True and self.result == RESULT_IN_PROGRESS:
-            self.__currentPlayer._setReadyToMove(True)
-
-        self.endLock()
-
-    def resign(self, player):
-        """Get a player to resign.
-        
-        'player' is the player resigning.
-        """
-        rule = RULE_RESIGN
-        if player is self.__whitePlayer:
-            self.endGame(RESULT_BLACK_WINS, rule)
-        else:
-            self.endGame(RESULT_WHITE_WINS, rule)
-            
-    def claimDraw(self):
-        """
-        """
-        # TODO: Penalise if make an incorrect attempt
-        try:
-            move = self.__moves[-1]
-        except IndexError:
-            return False
-        else:
-            if move.fiftyMoveRule:
-                rule = RULE_FIFTY_MOVES
-            elif move.threeFoldRepetition:
-                rule = RULE_THREE_FOLD_REPETITION
-            else:
-                return False
-
-        self.endGame(RESULT_DRAW, rule)
-        return True
-
-    def killPlayer(self, player):
-        """Report a player has died
-        
-        'player' is the player that has died.
-        """
-        if player is self.__whitePlayer:
-            result = RESULT_BLACK_WINS
-        elif player is self.__blackPlayer:
-            result = RESULT_WHITE_WINS       
-        self.endGame(result, RULE_DEATH)
-
-    def outOfTime(self, player):
-        """Report a player's timer has expired"""
-        if player is self.__whitePlayer:
-            result = RESULT_BLACK_WINS
-        elif player is self.__blackPlayer:
-            result = RESULT_WHITE_WINS
-        else:
-            assert(False)
-        self.endGame(result, RULE_TIMEOUT)
-        
-    def abandon(self):
-        self.endGame(RESULT_DRAW, RULE_ABANDONMENT)
-
-    def endGame(self, result, rule):
-        if self.result != RESULT_IN_PROGRESS:
-            return
-        self.result = result
-        self.rule = rule
-        if self.isStarted():
-            self._notifyEndGame()
-
-    def _notifyEndGame(self):
-        self.__currentPlayer._setReadyToMove(False)
-        for player in self.__players:
-            player.onGameEnded(self)
-
-    def getMoves(self):
-        """
-        """
-        return self.__moves
-
-    def abort(self):
-        """End the game"""
-        # Inform players
-        for player in self.__players:
-            player.onGameEnded(self)
-
-    # Private methods:
-
-    def __connectPlayer(self, player):
-        """Add a player into the game.
-        
-        'player' is the player to add.
-        
-        The player will be notified of the current state of the board.
-        """
-        self.__players.append(player)
-        player._setGame(self)
-        
-        # Notify the player of the current state
-        # FIXME: Make the board iteratable...
-        for file in '12345678':
-            for rank in 'abcdefgh':
-                coord = rank + file
-                piece = self.board.getPiece(coord)
-                if piece is None:
-                    continue
-
-                # These are moves from nowhere to their current location
-                player.onPieceMoved(piece, None, coord, False)
-
-    def _onPieceMoved(self, piece, start, end, delete):
-        """Called by the chess board"""
-        
-        # Notify all players of creations and deletions
-        # NOTE: Normal moves are done above since the SAN moves are calculated before the move...
-        # FIXME: Change this so the SAN moves are done afterwards...
-        for player in self.__players:
-            player.onPieceMoved(piece, start, end, delete)
-
-class NetworkChessGame(ChessGame):
-    """
-    """
-    
-    def move(self, player, move):
-        """Get a player to make a move.
-        
-        'player' is the player making the move.
-        'move' is the move to make. It can be of the form:
-               A coordinate move in the form ((file0, rank0), (file1, rank1), promotionType) ((int, int), (int, int), chess.board.PIECE_TYPE) or
-               A SAN move (string).
-        """
-        # Send to the server
-        
-            
-if __name__ == '__main__':
-    game = ChessGame()
-    
-    import pgn
-    
-    p = pgn.PGN('black.pgn')
-    g = p.getGame(0)
-
-    class PGNPlayer(ChessPlayer):
-
-        def __init__(self, isWhite):
-            self.__isWhite = isWhite
-            self.__moveNumber = 1
-        
-        def readyToMove(self):
-            if self.__isWhite:
-                move = g.getWhiteMove(self.__moveNumber)
-            else:
-                move = g.getBlackMove(self.__moveNumber)
-            self.__moveNumber += 1
-            self.move(move)
-            
-    white = PGNPlayer(True)
-    black = PGNPlayer(False)
-    
-    game.setWhite(white)
-    game.setBlack(black)
-    
-    game.start()
diff --git a/glchess/src/lib/uci.py b/glchess/src/lib/uci.py
deleted file mode 100644
index 657a019..0000000
--- a/glchess/src/lib/uci.py
+++ /dev/null
@@ -1,185 +0,0 @@
-# -*- coding: utf-8 -*-
-
-class StateMachine:
-    """
-    """
-    
-    STATE_IDLE = 'IDLE'
-    STATE_CONNECTING = 'CONNECTING'
-    
-    def __init__(self):
-        """
-        """
-        self.options = {}
-        self.__queuedCommands = []
-        self.buffer = ''
-        self.__haveMoves = False
-        self.__readyToConfigure = False    
-        self.__ready            = False
-        self.__inCallback       = False
-        self.__queuedCommands   = None
-        self.__positionCommand = 'position startpos'
-        
-    def logText(self, text, style):
-        """
-        """
-        pass
-        
-    def onOutgoingData(self, data):
-        """
-        """
-        pass
-
-    def onMove(self, move):
-        """Called when the AI makes a move.
-        
-        'move' is the move the AI has decided to make (string).
-        """
-        print 'UCI move: ' + move
-        
-    def registerIncomingData(self, data):
-        """
-        """
-        self.__inCallback = True
-        self.buffer += data
-        while True:
-            index = self.buffer.find('\n')
-            if index < 0:
-                break
-            line = self.buffer[:index]
-            self.buffer = self.buffer[index + 1:]
-            self.parseLine(line)
-        self.__inCallback = False
-        
-        if self.__options is not None and self.__readyToConfigure:
-            options = self.__options
-            self.__options = None
-            self.configure(options)
-
-        # Send queued commands once have OK
-        if len(self.__queuedCommands) > 0 and self.__ready:
-            commands = self.__queuedCommands
-            self.__queuedCommands = []
-            for c in commands:
-                self.__sendCommand(c)
-                
-    def __sendCommand(self, command):
-        """
-        """
-        if self.__ready and not self.__inCallback:
-            self.onOutgoingData(command + '\n')
-        else:
-            self.__queuedCommands.append(command)
-
-    def start(self):
-        """
-        """
-        self.onOutgoingData('uci\n')
-        
-    def startGame(self):
-        """
-        """
-        self.__sendCommand('ucinewgame')
-        self.__sendCommand(self.__positionCommand)
-
-    def configure(self, options = []):
-        """
-        """
-        if not self.__readyToConfigure:
-            self.__options = options
-            return
-
-        for option in options:
-            if not hasattr(option, 'name'):
-                print 'Ignoring unnamed UCI option'
-                continue
-            if option.value == '':
-                continue
-            self.onOutgoingData('setoption ' + option.name + ' value ' + option.value + '\n')
-        self.onOutgoingData('isready\n')
-
-    def requestMove(self, whiteTime, blackTime, ownTime):
-        """
-        """
-        # Some AI's don't work unless assigned some time
-        # TODO: which ones? I think Glaurung had issues
-        if whiteTime == 0:
-            whiteTime = 30000
-        if blackTime == 0:
-            blackTime = 30000
-            
-        self.__sendCommand('go wtime %d btime %d' % (whiteTime, blackTime))
-        
-    def undoMove(self):
-        """
-        """
-        self.__sendCommand('stop');
-        (self.__positionCommand, _) = self.__positionCommand.rsplit(" ", 1)
-        if self.__positionCommand.endswith(' moves'):
-            self.__haveMoves = False
-            self.__positionCommand = 'position startpos'
-        self.__sendCommand(self.__positionCommand)        
-
-    def reportMove(self, move, isSelf):
-        """
-        """
-        if not self.__haveMoves:
-            self.__positionCommand += ' moves'
-        self.__haveMoves = True
-        self.__positionCommand += ' ' + move
-        self.__sendCommand(self.__positionCommand)
-
-    def parseLine(self, line):
-        """
-        """
-        words = line.split()
-        
-        while True:
-            if len(words) == 0:
-                self.logText(line + '\n', 'input')
-                return
-            
-            style = self.parseCommand(words[0], words[1:])
-            if style is not None:
-                self.logText(line + '\n', style)
-                return
-
-            print 'WARNING: Unknown command: ' + repr(words[0])
-            words = words[1:]
-
-    def parseCommand(self, command, args):
-        """
-        """
-        if command == 'id':
-            return 'info'
-        
-        elif command == 'uciok':
-            if len(args) != 0:
-                print 'WARNING: Arguments on uciok: ' + str(args)
-            self.__readyToConfigure = True
-            return 'info'
-        
-        elif command == 'readyok':
-            if len(args) != 0:
-                print 'WARNING: Arguments on readyok: ' + str(args)
-            self.__ready = True
-            return 'info'
-        
-        elif command == 'bestmove':
-            if len(args) == 0:
-                print 'WARNING: No move with bestmove'
-                return 'error'
-            else:
-                move = args[0]
-                self.onMove(move)
-                
-                # TODO: Check for additional ponder information
-                return 'move'
-        
-        elif command == 'info':
-            return 'info'
-        
-        elif command == 'option':
-            return 'info'
-        
-        return None
diff --git a/gnibbles/board.c b/gnibbles/board.c
index e676a6d..f851760 100644
--- a/gnibbles/board.c
+++ b/gnibbles/board.c
@@ -35,18 +35,77 @@
 #include "properties.h"
 #include "board.h"
 
-static GdkPixbuf*  load_pixmap_file (const gchar * pixmap,
-			                                      gint xsize, gint ysize);
-static void load_pixmap ();
+extern GnibblesProperties *properties;
+
+extern GdkPixbuf *wall_pixmaps[];
 
-GdkPixbuf *wall_pixmaps[19] = { NULL, NULL, NULL, NULL, NULL,
+GdkPixbuf *walls_pixmaps[19] = { NULL, NULL, NULL, NULL, NULL,
   NULL, NULL, NULL, NULL, NULL,
   NULL, NULL, NULL, NULL, NULL,
   NULL, NULL, NULL, NULL
 };
 
-extern GnibblesProperties *properties;
+static GdkPixbuf *
+board_load_pixmap_file (const gchar * pixmap, gint xsize, gint ysize)
+{
+  GdkPixbuf *image;
+  gchar *filename;
+  const char *dirname;
+
+  dirname = games_runtime_get_directory (GAMES_RUNTIME_GAME_PIXMAP_DIRECTORY);
+  filename = g_build_filename (dirname, pixmap, NULL);
+
+  if (!filename) {
+    char *message =
+      g_strdup_printf (_("Nibbles couldn't find pixmap file:\n%s\n\n"
+			 "Please check your Nibbles installation"), pixmap);
+    //gnibbles_error (window, message;
+    g_free(message);
+  }
 
+  image = gdk_pixbuf_new_from_file_at_size (filename, xsize, ysize, NULL);
+  g_free (filename);
+
+  return image;
+}
+
+static void 
+board_load_pixmap ()
+{
+
+  gchar *small_files[] = {
+    "snake-red.svg",
+    "snake-green.svg",
+    "snake-blue.svg",
+    "snake-yellow.svg",
+    "snake-cyan.svg",
+    "snake-magenta.svg",
+    "snake-grey.svg",
+    "wall-empty.svg",
+    "wall-straight-up.svg",
+    "wall-straight-side.svg",
+    "wall-corner-bottom-left.svg",
+    "wall-corner-bottom-right.svg",
+    "wall-corner-top-left.svg",
+    "wall-corner-top-right.svg",
+    "wall-tee-up.svg",
+    "wall-tee-right.svg",
+    "wall-tee-left.svg",
+    "wall-tee-down.svg",
+    "wall-cross.svg"
+  };
+
+  int i;
+
+  for (i = 0; i < 19; i++) {
+    if (walls_pixmaps[i])
+      g_object_unref (walls_pixmaps[i]);
+      
+    walls_pixmaps[i] = board_load_pixmap_file (small_files[i],
+		  		                              4 * properties->tilesize,
+                           						  4 * properties->tilesize);
+  }
+}
 
 GnibblesBoard *
 gnibbles_board_new (gint t_w, gint t_h) 
@@ -57,12 +116,12 @@ gnibbles_board_new (gint t_w, gint t_h)
   board->width = t_w;
   board->height = t_h;
   board->level = NULL;
-  board->surface =NULL;
+  board->surface = NULL;
   board->clutter_widget = gtk_clutter_embed_new ();
 
   ClutterActor *stage;
 
-  load_pixmap ();
+  board_load_pixmap ();
 
   stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (board->clutter_widget));
   clutter_stage_set_color (CLUTTER_STAGE(stage), &stage_color);
@@ -130,61 +189,61 @@ gnibbles_board_load_level (GnibblesBoard *board, GnibblesLevel *level)
   gint i,j;
   gint x_pos, y_pos;
   ClutterActor *tmp;  
-  gboolean wall = TRUE;
+  gboolean is_wall = TRUE;
 
   if (board->level)
     g_object_unref (board->level);
 
   board->level = clutter_group_new ();
 
-  /* Load walls onto the surface*/
+  /* Load wall_pixmaps onto the surface*/
   for (i = 0; i < BOARDHEIGHT; i++) {
     y_pos = i * properties->tilesize;
     for (j = 0; j < BOARDWIDTH; j++) {
-      wall = TRUE;
+      is_wall = TRUE;
       switch (level->walls[j][i]) {
         case 'a': // empty space
-          wall = FALSE;
+          is_wall = FALSE;
           break; // break right away
         case 'b': // straight up
-          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[1]);
+          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[0]);
           break;
         case 'c': // straight side
-          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[2]);
+          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[1]);
           break;
         case 'd': // corner bottom left
-          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[3]);
+          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[2]);
           break;
         case 'e': // corner bottom right
-          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[4]);
+          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[3]);
           break;
-        case 'f': // corner up left
-          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[5]);
+          case 'f': // corner up left
+          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[4]);
           break;
         case 'g': // corner up right
-          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[6]);
+          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[5]);
           break;
         case 'h': // tee up
-          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[7]);
+          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[6]);
           break;
         case 'i': // tee right
-          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[8]);
+          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[7]);
           break;
         case 'j': // tee left
-          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[9]);
+          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[8]);
           break;
         case 'k': // tee down
-          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[10]);
+          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[9]);
           break;
         case 'l': // cross
-          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[11]);
+          tmp = gtk_clutter_texture_new_from_pixbuf (wall_pixmaps[10]);
           break;
         default:
-          wall = FALSE;
+          is_wall = FALSE;
           break;
       }
 
-      if (wall == TRUE) {
+      if (is_wall) {
         x_pos = j * properties->tilesize;
 
         clutter_actor_set_size (CLUTTER_ACTOR(tmp),
@@ -236,67 +295,3 @@ gnibbles_board_resize (GnibblesBoard *board, gint newtile)
     clutter_actor_set_size (tmp ,newtile, newtile);
   }
 }
-
-static void 
-load_pixmap ()
-{
-
-  gchar *small_files[] = {
-    "wall-empty.svg",
-    "wall-straight-up.svg",
-    "wall-straight-side.svg",
-    "wall-corner-bottom-left.svg",
-    "wall-corner-bottom-right.svg",
-    "wall-corner-top-left.svg",
-    "wall-corner-top-right.svg",
-    "wall-tee-up.svg",
-    "wall-tee-right.svg",
-    "wall-tee-left.svg",
-    "wall-tee-down.svg",
-    "wall-cross.svg",
-    "snake-red.svg",
-    "snake-green.svg",
-    "snake-blue.svg",
-    "snake-yellow.svg",
-    "snake-cyan.svg",
-    "snake-magenta.svg",
-    "snake-grey.svg"
-  };
-
-  int i;
-
-  for (i = 0; i < 19; i++) {
-    if (wall_pixmaps[i])
-      g_object_unref (wall_pixmaps[i]);
-      
-    wall_pixmaps[i] = load_pixmap_file (small_files[i],
-		  		                              4 * properties->tilesize,
-                           						  4 * properties->tilesize);
-  }
-}
-
-static GdkPixbuf *
-load_pixmap_file (const gchar * pixmap, gint xsize, gint ysize)
-{
-  GdkPixbuf *image;
-  gchar *filename;
-  const char *dirname;
-
-  dirname = games_runtime_get_directory (GAMES_RUNTIME_GAME_PIXMAP_DIRECTORY);
-  filename = g_build_filename (dirname, pixmap, NULL);
-
-  if (!filename) {
-    char *message =
-      g_strdup_printf (_("Nibbles couldn't find pixmap file:\n%s\n\n"
-			 "Please check your Nibbles installation"), pixmap);
-    //gnibbles_error (window, message;
-    g_free(message);
-  }
-
-  image = gdk_pixbuf_new_from_file_at_size (filename, xsize, ysize, NULL);
-  g_free (filename);
-
-  return image;
-}
-
-
diff --git a/gnibbles/main.c b/gnibbles/main.c
index e550c36..cda42c6 100644
--- a/gnibbles/main.c
+++ b/gnibbles/main.c
@@ -50,6 +50,7 @@
 #include <clutter/clutter.h>
 
 #include "board.h"
+#include "worm-clutter.h"
 
 #ifdef GGZ_CLIENT
 #include <libgames-support/games-dlg-chat.h>
@@ -87,6 +88,19 @@ GnibblesProperties *properties;
 
 GnibblesScoreboard *scoreboard;
 
+GdkPixbuf *wall_pixmaps[11] = { NULL, NULL, NULL, NULL, NULL,
+  NULL, NULL, NULL, NULL, NULL,
+  NULL
+};
+
+GdkPixbuf *worm_pixmaps[7] = { NULL, NULL, NULL, NULL, NULL,
+  NULL, NULL
+};
+
+GdkPixbuf *boni_pixmaps[9] = { NULL, NULL, NULL, NULL, NULL,
+  NULL, NULL, NULL, NULL
+};
+
 extern GnibblesBoni *boni;
 
 gchar board[BOARDWIDTH][BOARDHEIGHT];
@@ -124,6 +138,98 @@ static GtkAction *scores_action;
 static GtkAction *fullscreen_action;
 static GtkAction *leave_fullscreen_action;
 
+static GdkPixbuf *
+load_pixmap_file (const gchar * pixmap, gint xsize, gint ysize)
+{
+  GdkPixbuf *image;
+  gchar *filename;
+  const char *dirname;
+
+  dirname = games_runtime_get_directory (GAMES_RUNTIME_GAME_PIXMAP_DIRECTORY);
+  filename = g_build_filename (dirname, pixmap, NULL);
+
+  if (!filename) {
+    char *message =
+      g_strdup_printf (_("Nibbles couldn't find pixmap file:\n%s\n\n"
+			 "Please check your Nibbles installation"), pixmap);
+    //gnibbles_error (window, message;
+    g_free(message);
+  }
+
+  image = gdk_pixbuf_new_from_file_at_size (filename, xsize, ysize, NULL);
+  g_free (filename);
+
+  return image;
+}
+
+static void 
+load_pixmap ()
+{
+  gchar *bonus_files[] = {
+    "blank.svg",
+    "diamond.svg",
+    "bonus1.svg",
+    "bonus2.svg",
+    "life.svg",
+    "bonus3.svg",
+    "bonus4.svg",
+    "bonus5.svg",
+    "questionmark.svg"
+  };
+
+  gchar *small_files[] = {
+    "wall-straight-up.svg",
+    "wall-straight-side.svg",
+    "wall-corner-bottom-left.svg",
+    "wall-corner-bottom-right.svg",
+    "wall-corner-top-left.svg",
+    "wall-corner-top-right.svg",
+    "wall-tee-up.svg",
+    "wall-tee-right.svg",
+    "wall-tee-left.svg",
+    "wall-tee-down.svg",
+    "wall-cross.svg"
+  };
+  
+  gchar *worm_files[] = {
+    "snake-red.svg",
+    "snake-green.svg",
+    "snake-blue.svg",
+    "snake-yellow.svg",
+    "snake-cyan.svg",
+    "snake-magenta.svg",
+    "snake-grey.svg"
+  };
+
+  int i;
+
+  for (i = 0; i < 9; i++) {
+    if (boni_pixmaps[i])
+      g_object_unref (boni_pixmaps[i]);
+    boni_pixmaps[i] = load_pixmap_file (bonus_files[i],
+						  4 * properties->tilesize,
+						  4 * properties->tilesize);
+  }
+
+  for (i = 0; i < 11; i++) {
+    if (wall_pixmaps[i])
+      g_object_unref (wall_pixmaps[i]);
+      
+    wall_pixmaps[i] = load_pixmap_file (small_files[i],
+		  		                              4 * properties->tilesize,
+                           						  4 * properties->tilesize);
+  }
+
+  for (i = 0; i < 7; i++) {
+    if (worm_pixmaps[i])
+      g_object_unref (worm_pixmaps[i]);
+
+    worm_pixmaps[i] = load_pixmap_file (worm_files[i],
+                                        4 * properties->tilesize,
+                                        4 * properties->tilesize);
+  }
+}
+
 static void
 hide_cursor (void)
 {
@@ -1209,13 +1315,16 @@ main (int argc, char **argv)
   gtk_action_set_visible (new_game_action, !ggz_network_mode);
   gtk_action_set_visible (player_list_action, ggz_network_mode);
 
+  load_pixmap ();
 
   // clutter fun
   gtk_clutter_init (&argc, &argv);
   GnibblesBoard *board = gnibbles_board_new (BOARDWIDTH, BOARDHEIGHT);
   setup_window_clutter (board);
   
-  gnibbles_board_load_level (board, gnibbles_level_new (1));
+  gnibbles_board_load_level (board, gnibbles_level_new (16));
+
+  //GnibblesCWorm *cworm = gnibbles_cworm_new (1,10,10);
 
   //render_logo_clutter (board);
 
diff --git a/gnibbles/worm-clutter.c b/gnibbles/worm-clutter.c
index 3328f04..22283f6 100644
--- a/gnibbles/worm-clutter.c
+++ b/gnibbles/worm-clutter.c
@@ -19,13 +19,19 @@
  *   along with this program; if not, write to the Free Software
  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
-
+#include <config.h>
+#include <glib/gi18n.h>
+#include <gdk/gdk.h>
+#include <stdlib.h>
+#include <libgames-support/games-runtime.h>
+
+#include "main.h"
+#include "gnibbles.h"
 #include "properties.h"
 #include "worm-clutter.h"
 
 extern GnibblesProperties *properties;
 
-
 GnibblesCWorm*
 gnibbles_cworm_new (guint number, gint x_s, gint y_s)
 {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]