#!/usr/bin/env python from sugar.activity import activity import logging import pygtk pygtk.require('2.0') import gtk import pango import sys, os import random rows = 12 cols = 12 x_score = 0 o_score = 0 str_text = "X" class fiveinarowActivity(activity.Activity): def __init__(self, handle): print "running activity init", handle activity.Activity.__init__(self, handle) print "activity running" toolbox = activity.ActivityToolbox(self) self.set_toolbox(toolbox) toolbox.show() self.connect("ran-num", self.ran_Num) self.connect("roll-dice", self.roll_dice) self.connect("isTopLeft", self.isTopLeft) self.connect("isTopCenter", self.isTopCenter) self.connect("isTopRight", self.isTopRight) self.connect("isBottom", self.isBottom) self.connect("enable-button", self.enable_button) self.connect("disable_button", self.disable_button) self.connect("clear-button", self.clear_button) self.connect("change-text", self.change_text) self.connect("check-horiztontalLeft", self.check_horizontalLeft) self.connect("check-horizontal", self.check_horizontal) self.connect("check-verticle", self.check_verticle) self.connect("check-verticleUp", self.check_verticleUp) self.connect("check-diagonalUpRight", self.check_diagonalUpRight) self.connect("check-diagonalRight", self.check_diagonalRight) self.connect("check-diagonalLeft", self.check_diagonalLeft) self.connect("check-diagonalUp", self.check_diagonalUp) main_vbox = gtk.VBox(False, 1) main_vbox.set_border_width(1) self.set_canvas(main_vbox) main_vbox.show() main_hbox = gtk.HBox(False, 1) main_vbox.pack_start(main_hbox, False, True, 0) main_hbox.show() vbox_left = gtk.VBox(False, 1) hbox_child = gtk.HBox(False, 1) roll_button = gtk.Button() label = gtk.Label('Roll') image = gtk.Image() image.set_from_file("red04.gif") box1 = gtk.HBox(False, 0) box1.pack_start(image, False, False, 3) box1.pack_start(label, False, False, 3) image.show() label.show() roll_button.add(box1) box1.show() roll_button.show() hbox_child.pack_start(roll_button, False, True, 0) new_button = gtk.Button() label = gtk.Label('New Game') image = gtk.Image() image.set_from_file("arrow.jpg") box1 = gtk.HBox(False, 0) box1.pack_start(image, False, False, 3) box1.pack_start(label, False, False, 3) image.show() label.show() new_button.add(box1) new_button.set_sensitive(0) box1.show() new_button.show() hbox_child.pack_start(new_button, False, True, 0) vbox_left.pack_start(hbox_child, False, False, 0) separator = gtk.HSeparator() vbox_left.pack_start(separator, False, True, 5) separator.show() hbox_child2 = gtk.HBox(False, 1) labelX = gtk.Label("Player X") labelXScore = gtk.Label("") hbox_child2.pack_start(labelX, True, False, 0) hbox_child2.pack_start(labelXScore, True, False, 0) vbox_left.pack_start(hbox_child2, False, False, 0) hbox_child3 = gtk.HBox(False, 1) labelO = gtk.Label("Player O") labelOScore = gtk.Label("") hbox_child3.pack_start(labelO, True, False, 0) hbox_child3.pack_start(labelOScore, True, False, 0) vbox_left.pack_start(hbox_child3, False, False, 0) hbox_child4 = gtk.HBox(False, 1) labelStart = gtk.Label("") hbox_child4.pack_start(labelStart, True, False, 0) vbox_left.pack_start(hbox_child4, False, False, 0) separator = gtk.HSeparator() vbox_left.pack_start(separator, False, True, 5) separator.show() label_player1 = gtk.Label("Player X's Score:") vbox_left.pack_start(label_player1, False, False, 0) entry_player1 = gtk.Entry() entry_player1.set_editable(False) entry_player1.set_alignment(0.5) vbox_left.pack_start(entry_player1, False, False, 0) label_player2 = gtk.Label("Player O's Score:") vbox_left.pack_start(label_player2, False, False, 0) entry_player2 = gtk.Entry() entry_player2.set_editable(False) entry_player2.set_alignment(0.5) vbox_left.pack_start(entry_player2, False, False, 0) frame = gtk.Frame("The Winner") label_winner = gtk.Label("") label_winner.set_justify(gtk.JUSTIFY_LEFT) frame.add(label_winner) vbox_left.pack_start(frame, False, False, 0) im = gtk.Image() pixbuf = gtk.gdk.pixbuf_new_from_file("Phnompenh38.jpg") scaled_buf = pixbuf.scale_simple(170,100,gtk.gdk.INTERP_BILINEAR) im.set_from_pixbuf(scaled_buf) im.show() vbox_left.pack_start(im) main_hbox.pack_start(vbox_left, False, False, 0) separator = gtk.VSeparator() main_hbox.pack_start(separator, False, True, 5) separator.show() vbox_right = gtk.VBox(False, 1) frame = gtk.Frame("FIVE IN A ROW") table = gtk.Table(12, 12, True) table.set_row_spacings(0) table.set_col_spacings(0) frame.add(table) table.show() vbox_right.pack_start(frame, False, False, 0) button = map(lambda i:gtk.Button(" "), range(rows*cols)) for i in range(rows*cols): y,x = divmod(i, cols) table.attach(button[i],x, x+1, y, y+1) button[i].connect("clicked", self.change_text, button, label_winner, entry_player1, entry_player2, i) button[i].show() self.disable_button(button) roll_button.connect("clicked", self.roll_dice, button, new_button, labelXScore, labelOScore, labelStart) new_button.connect("clicked", self.clear_button, button, roll_button, label_winner, labelXScore, labelOScore, labelStart ) main_hbox.pack_start(vbox_right, False, False, 0) separator = gtk.HSeparator() main_vbox.pack_start(separator, False, True, 5) separator.show() quitbox = gtk.HBox(False, 0) button = gtk.Button("Quit") button.connect_object("clicked", lambda w: w.destroy(), window) button.set_flags(gtk.CAN_DEFAULT) quitbox.pack_end(button, False, False, 0) main_vbox.pack_start(quitbox, False, False, 0) button.grab_default() button.show() self.show_all() # Obligatory basic callback def print_hello(self, w, data=None): logging.info('Five in a Row activity') def ran_Num(self): return random.randint(1,6) def roll_dice(self, w, b_list, w2, label1, label2, label3, data=None): global str_text Xnum = self.ran_Num() + self.ran_Num() Onum = self.ran_Num() + self.ran_Num() label1.set_text(str(Xnum)) label2.set_text(str(Onum)) if Xnum > Onum: str_text = "X" else: str_text = "O" self.enable_button(b_list) label3.set_text("Player " + str_text + " starts!") w.set_state(gtk.STATE_INSENSITIVE) w2.set_sensitive(1) def isTopLeft(self, row, col, index, b_list, b_label): start_point = [] num = index % 12 for i in range(num+1): start_point.append(index-i) if self.check_horizontalLeft(start_point, b_list, b_label): return True else: start_point = [] num = index / 12 if num > 4: num = 4 for i in range(num+1): start_point.append(index-i*12) if self.check_verticle(start_point, b_list, b_label): return True else: start_point = [] num = index % 12 for i in range(num+1): start_point.append(index-i*13) if self.check_diagonalLeft(start_point, b_list, b_label): return True if row >= 4: start_point = [] num = index % 12 for i in range(num+1): start_point.append(index + i*11) if self.check_diagonalUp(start_point, b_list, b_label): return True return False def isTopCenter(self, row, col, index, b_list, b_label): start_point = [] for i in range(5): start_point.append(index + i) if self.check_horizontal(start_point, b_list, b_label): return True else: start_point = [] num = index / 12 if num > 4: num = 4 for i in range(num+1): start_point.append(index - i*12) if self.check_verticle(start_point, b_list, b_label): return True else: start_point = [] for i in range(num+1): start_point.append(index - i*11) if self.check_diagonalRight(start_point, b_list, b_label): return True else: start_point = [] for i in range(num+1): start_point.append(index - i*13) if self.check_diagonalLeft(start_point, b_list, b_label): return True return False def isTopRight(self, row, col, index, b_list, b_label): start_point = [] num = 12 - (index%12) for i in range(num): start_point.append(index+i) if self.check_horizontal(start_point, b_list, b_label): return True else: start_point = [] num = index/12 if num > 4: num = 4 for i in range(num+1): start_point.append(index - i*12) if self.check_verticle(start_point, b_list, b_label): return True else: start_point = [] num = 12 - (index%12) for i in range(num): start_point.append(index - i*11) if self.check_diagonalRight(start_point, b_list, b_label): return True else: if row >= 4: start_point = [] for i in range(num): start_point.append(index + i*13) if self.check_diagonalUpRight(start_point, b_list, b_label): return True return False def isBottom(self, row, col, index, b_list, b_label): start_point = [] num = 12 - (index / 12) for i in range(num): start_point.append(index + i*12) if self.check_verticleUp(start_point, b_list, b_label): return True if col < 4: start_point = [] num = index % 12 for i in range(num+1): start_point.append(index-i) if self.check_horizontalLeft(start_point, b_list, b_label): return True else: start_point = [] if index in (111, 122,123,133,134,135): num = 12 - (index/12) for i in range(num): start_point.append(index + i*11) else: num = index % 12 for i in range (num+1): start_point.append(index + i*11) if self.check_diagonalUp(start_point, b_list, b_label): return True elif 4 <= col <= 7: start_point = [] for i in range(5): start_point.append(index + i) if self.check_horizontal(start_point, b_list, b_label): return True else: start_point = [] num = 12- (index / 12) for i in range(num): start_point.append(index + i*11) if self.check_diagonalUp(start_point, b_list, b_label): return True else: start_point = [] for i in range(num): start_point.append(index + i*13) if self.check_diagonalUpRight(start_point, b_list, b_label): return True else: start_point = [] num = 12 - (index%12) for i in range(num): start_point.append(index+i) if self.check_horizontal(start_point, b_list, b_label): return True else: start_point = [] if index in (116,128,129,140,141,142): num = 12 - (index/12) else: num = 12 - (index%12) for i in range(num): start_point.append(index + i*13) if self.check_diagonalUpRight(start_point, b_list, b_label): return True return False def enable_button(self, bList): for i in range(len(bList)): bList[i].set_sensitive(1) bList[i].set_label(" ") def disable_button(self, bList): for i in range(len(bList)): bList[i].set_state(gtk.STATE_INSENSITIVE) def clear_button(self, w, bu_list, w2, label, l1, l2, l3): self.enable_button(bu_list) self.disable_button(bu_list) w.set_sensitive(0) w2.set_sensitive(1) label.set_text("") l1.set_text("") l2.set_text("") l3.set_text("") def change_text(self, w, button_list, label, e1, e2, data=None): global str_text, x_score, o_score if w.get_label() != "X" and w.get_label() != "O": w.set_label(str_text) w.set_state(gtk.STATE_INSENSITIVE) if str_text == "X": str_text = "O" else: str_text = "X" row_idx = data/12 col_idx = data%12 winning = False if row_idx <= 7: if col_idx <= 4: if self.isTopLeft(row_idx, col_idx, data, button_list, w.get_label()): winning = True elif 4 <= col_idx <=7: if self.isTopCenter(row_idx, col_idx, data, button_list, w.get_label()): winning = True else: if self.isTopRight(row_idx, col_idx, data, button_list, w.get_label()): winning = True else: if self.isBottom(row_idx, col_idx, data, button_list, w.get_label()): winning = True if winning: if w.get_label() == 'X': x_score = x_score + 5 else: o_score = o_score + 5 self.disable_button(button_list) e1.set_text(str(x_score)) e2.set_text(str(o_score)) # e1.modify_font(pango.FontDescription("sans 16")) # e2.modify_font(pango.FontDescription("sans 16")) label.set_text("Player " + w.get_label()) # label.modify_font(pango.FontDescription("sans 16")) def check_horizontalLeft(self, point, bList, text): for i in range(len(point)): indx = point[i] if bList[indx].get_label() == bList[indx+1].get_label() == bList[indx+2].get_label()== bList[indx+3].get_label()== bList[indx+4].get_label() == text: return True return False def check_horizontal(self, point, bList, text): for i in range(len(point)): indx = point[i] if bList[indx].get_label() == bList[indx-1].get_label() == bList[indx-2].get_label()== bList[indx-3].get_label()== bList[indx-4].get_label() == text: return True return False def check_verticle(self, point, bList, text): for i in range(len(point)): indx = point[i] if bList[indx].get_label() == bList[indx+12].get_label() == bList[indx+24].get_label()== bList[indx+36].get_label()== bList[indx+48].get_label() == text: return True return False def check_verticleUp(self, point, bList, text): for i in range(len(point)): indx = point[i] if bList[indx].get_label() == bList[indx-12].get_label() == bList[indx-24].get_label()== bList[indx-36].get_label()== bList[indx-48].get_label() == text: return True return False def check_diagonalUpRight(self, point, bList, text): for i in range(len(point)): indx = point[i] if bList[indx].get_label() == bList[indx-13].get_label() == bList[indx-26].get_label()== bList[indx-39].get_label()== bList[indx-52].get_label() == text: return True return False def check_diagonalRight(self, point, bList, text): for i in range(len(point)): indx = point[i] if bList[indx].get_label() == bList[indx+11].get_label() == bList[indx+22].get_label()== bList[indx+33].get_label()== bList[indx+44].get_label() == text: return True return False def check_diagonalLeft(self, point, bList, text): for i in range(len(point)): indx = point[i] if bList[indx].get_label() == bList[indx+13].get_label() == bList[indx+26].get_label()== bList[indx+39].get_label()== bList[indx+52].get_label() == text: return True return False def check_diagonalUp(self, point, bList, text): for i in range(len(point)): indx = point[i] if bList[indx].get_label() == bList[indx-11].get_label() == bList[indx-22].get_label()== bList[indx-33].get_label()== bList[indx-44].get_label() == text: return True return False