Back to index

gcompris  8.2.2
Public Member Functions | Public Attributes
connect4.Gcompris_connect4 Class Reference

List of all members.

Public Member Functions

def __init__
def start
def end
def ok
def repeat
def config
def key_press
def pause
def set_level
def newGame
def columnItemEvent
def profItemEvent
def machinePlay
def machinePlayed
def refreshScreen
def play
def drawBoard
def animTimer
def winner

Public Attributes

 gcomprisBoard
 boardSize
 nbColumns
 nbLines
 stoneSize
 maxVictory
 maxLevel
 firstPlayer
 boardItem
 timerAnim
 timerMachine
 humanVictory
 machineHasPlayed
 endAnimCallback
 winnercall
 rootitem
 prof
 timericon
 player1
 player2
 board
 gamewon
 winLine
 stone
 countAnim
 redLine

Detailed Description

Connect 4 stones

Definition at line 45 of file connect4.py.


Constructor & Destructor Documentation

def connect4.Gcompris_connect4.__init__ (   self,
  gcomprisBoard 
)

Definition at line 49 of file connect4.py.

00049 
00050   def __init__(self, gcomprisBoard):
00051     self.gcomprisBoard = gcomprisBoard


Member Function Documentation

Definition at line 255 of file connect4.py.

00255 
00256   def animTimer(self):
00257     self.countAnim -= 1
00258     if self.countAnim > 0:
00259       y= self.stone.get_property('y')
00260       self.stone.set_property('y', y + (self.boardSize/self.nbColumns))
00261       self.timerAnim = gtk.timeout_add(200, self.animTimer)
00262     else:
00263       if self.winnercall:
00264         self.winnercall()
00265         self.winnercall=None
00266         self.endAnimCallback=None
00267       if self.endAnimCallback:
00268         self.endAnimCallback()

Here is the call graph for this function:

Here is the caller graph for this function:

def connect4.Gcompris_connect4.columnItemEvent (   self,
  widget,
  event,
  column 
)

Definition at line 184 of file connect4.py.

00184 
00185   def columnItemEvent(self, widget, event, column):
00186     if event.type == gtk.gdk.BUTTON_PRESS:
00187       if event.button == 1 and self.gamewon == False and self.machineHasPlayed:
00188         column = int((event.x - (gcompris.BOARD_WIDTH-self.boardSize)/2.0) // self.stoneSize)
00189         if not (column < 0 or column > (self.nbColumns-1)):
00190           gcompris.bar_hide(True)
00191           if self.play(self.player1, 1, column):
00192             if self.winnercall == None:
00193               self.timericon.gnomecanvas.show()
00194               self.endAnimCallback = self.machinePlay
00195               self.machineHasPlayed = False
00196 
00197     return False

Here is the call graph for this function:

Definition at line 138 of file connect4.py.

00138 
00139   def config(self):
00140     pass
00141 

def connect4.Gcompris_connect4.drawBoard (   self,
  board 
)

Definition at line 238 of file connect4.py.

00238 
00239   def drawBoard(self, board):
00240     stone = self.board.state[self.board.last_move][-1]
00241     x = self.board.last_move
00242     y = len(self.board.state[self.board.last_move])-1
00243     file = "connect4/stone_%d.png" % stone
00244 
00245     self.stone = self.boardItem.add(
00246       gnomecanvas.CanvasPixbuf,
00247        pixbuf = gcompris.utils.load_pixmap(file),
00248        x=x*(self.boardSize/self.nbColumns),
00249        #y=(self.boardSize/self.nbColumns)*(self.nbLines-1-y)
00250        y=0
00251        )
00252     self.stone.connect("event", self.columnItemEvent, 0)
00253     self.countAnim = self.nbLines-y
00254     self.timerAnim = gtk.timeout_add(200, self.animTimer)

Here is the caller graph for this function:

Definition at line 112 of file connect4.py.

00112 
00113   def end(self):
00114     if self.timerAnim:
00115       gtk.timeout_remove(self.timerAnim)
00116     if self.timerMachine:
00117       gtk.timeout_remove(self.timerMachine)
00118 
00119     del self.timericon
00120 
00121     # Remove the root item removes all the others inside it
00122     self.rootitem.destroy()
00123     self.boardItem.destroy()
00124 

def connect4.Gcompris_connect4.key_press (   self,
  keyval,
  commit_str,
  preedit_str 
)

Definition at line 142 of file connect4.py.

00142 
00143   def key_press(self, keyval, commit_str, preedit_str):
00144     self.timericon.gnomecanvas.hide()
00145     return False

Definition at line 204 of file connect4.py.

00204 
00205   def machinePlay(self):
00206     # ai starts
00207     self.play(self.player2, 2, 0)
00208     # ai ends
00209     self.timericon.gnomecanvas.hide()
00210     self.prof.hide()
00211     self.endAnimCallback = self.machinePlayed

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 212 of file connect4.py.

00212 
00213   def machinePlayed(self):
00214     self.machineHasPlayed = True
00215     gcompris.bar_hide(False)

Here is the caller graph for this function:

Definition at line 163 of file connect4.py.

00163 
00164   def newGame(self):
00165     if self.boardItem != None:
00166       self.boardItem.destroy()
00167     self.boardItem = self.gcomprisBoard.canvas.root().add(
00168       gnomecanvas.CanvasGroup,
00169       x=(gcompris.BOARD_WIDTH-self.boardSize)/2.0,
00170       y=50.0
00171       )
00172     self.player1 = human.Human(self.gcomprisBoard.level)
00173     self.player2 = minmax.MinMax(self.gcomprisBoard.level, self.refreshScreen)
00174     self.board = board.Board()
00175     self.gamewon = False
00176     self.machineHasPlayed = True
00177     self.winLine = None
00178     try:
00179       del self.redLine
00180     except:
00181       pass
00182     self.firstPlayer = False
00183     self.prof.show()

Here is the caller graph for this function:

Definition at line 125 of file connect4.py.

00125 
00126   def ok(self):
00127     pass

def connect4.Gcompris_connect4.pause (   self,
  pause 
)

Definition at line 146 of file connect4.py.

00146 
00147   def pause(self, pause):
00148     pass

def connect4.Gcompris_connect4.play (   self,
  player,
  numPlayer,
  column 
)

Definition at line 220 of file connect4.py.

00220 
00221   def play(self, player, numPlayer, column):
00222     move = player.doMove(self.board, numPlayer, column)
00223 
00224     if type(move) is types.IntType and rules.isMoveLegal(self.board, move):
00225 #      self.firstPlayer = True
00226       self.board.move(move, numPlayer)
00227       self.drawBoard(self.board)
00228       self.winLine = rules.isWinner(self.board, numPlayer)
00229 
00230       if self.winLine:
00231         self.winnercall = lambda : self.winner(numPlayer)
00232       elif rules.isBoardFull(self.board):
00233         self.winnercall = lambda : self.winner(0)
00234       return True
00235 
00236     gcompris.bar_hide(False)
00237     return False

Here is the call graph for this function:

Here is the caller graph for this function:

def connect4.Gcompris_connect4.profItemEvent (   self,
  widget,
  event,
  column 
)

Definition at line 198 of file connect4.py.

00198 
00199   def profItemEvent(self, widget, event, column):
00200     #if event.type == gtk.gdk.BUTTON_PRESS and self.firstPlayer == False:
00201     if event.type == gtk.gdk.BUTTON_PRESS:
00202       self.firstPlayer = True
00203       self.machinePlay()

Here is the call graph for this function:

Definition at line 216 of file connect4.py.

00216 
00217   def refreshScreen(self):
00218     gtk.main_iteration(block=False)
00219     pass

Definition at line 128 of file connect4.py.

00128 
00129   def repeat(self):
00130     if self.humanVictory >= self.maxVictory:
00131       if self.gcomprisBoard.level < self.maxLevel:
00132         self.set_level(self.gcomprisBoard.level+1)
00133       else:
00134         self.end()
00135         gcompris.end_board()
00136     else:
00137       self.newGame()

Here is the call graph for this function:

Here is the caller graph for this function:

def connect4.Gcompris_connect4.set_level (   self,
  level 
)

Definition at line 150 of file connect4.py.

00150 
00151   def set_level(self, level):
00152     if level > self.maxLevel: level = 1
00153     self.gcomprisBoard.level=level
00154     self.gcomprisBoard.sublevel=1
00155     gcompris.bar_set_level(self.gcomprisBoard)
00156     self.player1.setDifficulty(level)
00157     self.player2.setDifficulty(level)
00158     self.humanVictory = 0
00159     self.newGame()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 52 of file connect4.py.

00052 
00053   def start(self):
00054     self.boardSize = 490.0
00055     self.nbColumns = 7
00056     self.nbLines = 6
00057     self.stoneSize = 70.0
00058     self.maxVictory = 3
00059     self.maxLevel = 4
00060     self.firstPlayer = False
00061     self.boardItem = None
00062     self.timerAnim = 0
00063     self.timerMachine = 0
00064     self.humanVictory = 0
00065     self.machineHasPlayed = True
00066     self.endAnimCallback = None
00067     self.gcomprisBoard.level=1
00068     self.gcomprisBoard.maxlevel=9
00069     self.gcomprisBoard.sublevel=1
00070     self.gcomprisBoard.number_of_sublevel=1
00071     self.winnercall = None
00072     gcompris.bar_set(gcompris.BAR_LEVEL)
00073     gcompris.bar_set_level(self.gcomprisBoard)
00074 
00075     # Create our rootitem. We put each canvas item in it so at the end we
00076     # only have to kill it. The canvas deletes all the items it contains automaticaly.
00077     self.rootitem = self.gcomprisBoard.canvas.root().add(
00078       gnomecanvas.CanvasGroup,
00079       x=0.0,
00080       y=0.0
00081       )
00082 
00083     pixmap = gcompris.utils.load_pixmap(gcompris.skin.image_to_skin("button_reload.png"))
00084     if(pixmap):
00085       gcompris.bar_set_repeat_icon(pixmap)
00086       gcompris.bar_set(gcompris.BAR_LEVEL|gcompris.BAR_REPEAT_ICON)
00087     else:
00088       gcompris.bar_set(gcompris.BAR_LEVEL|gcompris.BAR_REPEAT)
00089 
00090     selector = self.rootitem.add(
00091      gnomecanvas.CanvasPixbuf,
00092       pixbuf = gcompris.utils.load_pixmap("connect4/back.png"),
00093       x=0.0,
00094       y=0.0
00095       )
00096     selector.connect("event", self.columnItemEvent, 0)
00097 
00098     self.prof = self.rootitem.add(
00099      gnomecanvas.CanvasPixbuf,
00100       pixbuf = gcompris.utils.load_pixmap("connect4/tux-teacher.png"),
00101       x=10,
00102       y=350.0
00103       )
00104     self.prof.connect("event", self.profItemEvent, 0)
00105     self.prof.connect("event", gcompris.utils.item_event_focus)
00106 
00107     self.timericon = gcompris.anim.CanvasItem( gcompris.anim.Animation("connect4/sablier.txt"),
00108                                                self.rootitem )
00109     self.timericon.gnomecanvas.hide()
00110 
00111     self.newGame()

def connect4.Gcompris_connect4.winner (   self,
  player 
)

Definition at line 269 of file connect4.py.

00269 
00270   def winner(self, player):
00271     self.gamewon = True
00272 
00273     # It's a draw, no line to draw
00274     if player == 0:
00275       gcompris.bonus.display(gcompris.bonus.DRAW, gcompris.bonus.FLOWER)
00276       return
00277 
00278     # Display the winner line
00279     if ((self.firstPlayer and (player==2)) or
00280         ((not self.firstPlayer) and (player==1))):
00281       self.humanVictory += 1
00282     else:
00283       self.humanVictory = 0
00284     points = ( self.winLine[0][0]*(self.boardSize/self.nbColumns)+self.stoneSize/2,
00285                (self.boardSize/self.nbColumns)*(self.nbLines-1-self.winLine[0][1])+self.stoneSize/2,
00286                self.winLine[1][0]*(self.boardSize/self.nbColumns)+self.stoneSize/2,
00287                (self.boardSize/self.nbColumns)*(self.nbLines-1-self.winLine[1][1])+self.stoneSize/2
00288                )
00289 
00290     self.redLine = self.boardItem.add(
00291       gnomecanvas.CanvasLine,
00292        fill_color_rgba=0xFF0000FFL,
00293        points=points,
00294        width_pixels = 8
00295        )
00296     self.redLine.set_property("cap-style", gtk.gdk.CAP_ROUND)
00297     if player == 1:
00298       gcompris.bonus.display(gcompris.bonus.WIN, gcompris.bonus.FLOWER)
00299     elif player == 2:
00300       gcompris.bonus.display(gcompris.bonus.LOOSE, gcompris.bonus.FLOWER)
00301 

Here is the caller graph for this function:


Member Data Documentation

Definition at line 173 of file connect4.py.

Definition at line 60 of file connect4.py.

Definition at line 53 of file connect4.py.

Definition at line 252 of file connect4.py.

Definition at line 65 of file connect4.py.

Definition at line 59 of file connect4.py.

Definition at line 174 of file connect4.py.

Definition at line 50 of file connect4.py.

Definition at line 63 of file connect4.py.

Definition at line 64 of file connect4.py.

Definition at line 58 of file connect4.py.

Definition at line 57 of file connect4.py.

Definition at line 54 of file connect4.py.

Definition at line 55 of file connect4.py.

Definition at line 171 of file connect4.py.

Definition at line 172 of file connect4.py.

Definition at line 97 of file connect4.py.

Definition at line 289 of file connect4.py.

Definition at line 76 of file connect4.py.

Definition at line 244 of file connect4.py.

Definition at line 56 of file connect4.py.

Definition at line 61 of file connect4.py.

Definition at line 106 of file connect4.py.

Definition at line 62 of file connect4.py.

Definition at line 176 of file connect4.py.

Definition at line 70 of file connect4.py.


The documentation for this class was generated from the following file: