/usr/share/gcompris/python/lightsoff.py is in gcompris-data 15.02-1.1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 | # gcompris - lightsoff.py
#
# Copyright (C) 2010 Bruno and Clement Coudoin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
# lightsoff Board module
import gtk
import gtk.gdk
import gcompris
import gcompris.utils
import gcompris.skin
import gcompris.bonus
import goocanvas
from gcompris import gcompris_gettext as _
class Gcompris_lightsoff:
"""Empty gcompris python class"""
def __init__(self, gcomprisBoard):
# Save the gcomprisBoard, it defines everything we need
# to know from the core
self.gcomprisBoard = gcomprisBoard
self.rootitem = None
# These are used to let us restart only after the bonus is displayed.
# When the bonus is displayed, it call us first with pause(1) and then with pause(0)
self.board_paused = False;
self.gamewon = False;
self.data = [
[[0,0,0,0,0],
[0,0,1,0,0],
[0,1,1,1,0],
[0,0,1,0,0],
[0,0,0,0,0]],
[[1,1,0,0,0],
[1,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]],
[[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,1,0,0],
[0,1,1,1,0]],
[[1,1,0,1,1],
[1,0,0,0,1],
[0,0,0,0,0],
[0,0,0,1,0],
[0,0,1,1,1]],
[[0,1,1,1,0],
[1,0,1,0,1],
[1,1,0,1,1],
[1,0,1,0,1],
[0,1,1,1,0]],
[[1,1,0,1,1],
[1,0,1,0,1],
[0,1,1,1,0],
[1,0,1,0,1],
[1,1,0,1,1]],
[[0,1,0,1,0],
[1,1,0,1,1],
[0,1,0,1,0],
[1,0,1,0,1],
[1,0,1,0,1]],
[[1,0,0,0,1],
[1,1,0,1,1],
[0,0,1,0,0],
[1,0,1,0,0],
[1,0,1,1,0]],
[[1,1,0,1,1],
[0,0,0,0,0],
[1,1,0,1,1],
[0,0,0,0,1],
[1,1,0,0,0]],
# 5
[[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1]],
# 6
[[0,0,0,1,1],
[0,0,0,1,1],
[0,0,0,0,0],
[1,1,0,0,0],
[1,1,0,0,0]],
# 7
[[0,0,0,0,0],
[0,1,1,1,0],
[1,1,1,1,1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[1,1,1,0,0],
[0,0,1,0,0]],
# 39
[[0,0,1,1,1],
[1,0,1,1,1],
[1,1,1,0,0],
[0,0,1,0,0],
[1,1,0,1,1]],
# 40
[[0,1,1,1,1],
[0,0,1,1,1],
[0,0,1,1,1],
[1,1,1,1,0],
[0,0,0,1,0]],
# 41
[[1,1,1,1,1],
[1,0,0,0,0],
[0,1,0,0,1],
[0,0,0,1,1],
[1,1,1,1,1]],
# 42
[[1,1,1,1,1],
[1,0,0,0,0],
[0,0,1,0,0],
[0,1,1,1,0],
[0,1,0,0,1]],
# 43
[[0,0,0,0,0],
[0,0,0,1,0],
[1,1,0,1,1],
[0,1,1,0,0],
[1,1,1,1,1]],
# 44
[[0,0,0,1,1],
[0,1,1,0,0],
[0,1,0,0,0],
[1,1,1,1,0],
[1,1,1,1,0]],
# 45
[[0,0,0,1,0],
[1,1,1,1,1],
[0,0,0,0,0],
[0,0,1,0,0],
[1,1,1,1,0]],
# 46
[[0,1,0,1,0],
[0,0,0,1,0],
[0,1,0,1,0],
[0,0,1,0,0],
[0,1,1,1,0]],
# 47
[[1,0,0,1,0],
[0,0,0,0,1],
[0,1,0,0,0],
[0,0,0,0,0],
[1,0,1,0,0]],
# 48
[[1,1,0,0,1],
[0,1,0,0,1],
[0,1,1,1,1],
[0,1,0,1,0],
[1,1,1,1,1]],
# 49
[[1,1,1,1,1],
[0,0,1,0,0],
[0,1,1,0,0],
[0,0,1,1,0],
[1,1,1,0,1]],
# 50
[[1,0,1,0,1],
[1,0,1,0,0],
[0,0,0,1,1],
[0,1,0,1,0],
[1,0,0,1,0]],
# 51
[[0,1,0,1,0],
[1,0,0,1,0],
[0,1,1,1,1],
[1,0,1,0,0],
[0,1,1,0,0]],
# 52
[[1,1,1,1,1],
[1,1,0,0,0],
[0,0,0,1,1],
[0,1,1,1,0],
[0,0,1,0,0]],
]
def start(self):
self.gcomprisBoard.level=1
self.gcomprisBoard.maxlevel=len(self.data)
self.gcomprisBoard.sublevel=1
self.gcomprisBoard.number_of_sublevel=1
# Create our rootitem. We put each canvas item in it so at the end we
# only have to kill it. The canvas deletes all the items it contains
# automaticaly.
self.backroot = goocanvas.Group(parent = \
self.gcomprisBoard.canvas.get_root_item())
# A color changing background
self.background = goocanvas.Rect(
parent = self.backroot,
x = 0,
y = 0,
width = gcompris.BOARD_WIDTH,
height = 200,
fill_color_rgba = 0xFFFFFFFFL
)
svghandle = gcompris.utils.load_svg("lightsoff/back.svgz")
goocanvas.Svg(
parent = self.backroot,
svg_handle = svghandle,
svg_id = "#BACKGROUND",
pointer_events = goocanvas.EVENTS_NONE
)
# The Sun
self.sunitem = goocanvas.Svg(
parent = self.backroot,
svg_handle = svghandle,
svg_id = "#SUN",
)
self.sunitem_offset = 0
# TUX
self.tuxitem = goocanvas.Svg(
parent = self.backroot,
svg_handle = svghandle,
svg_id = "#TUX",
)
self.tuxitem.connect("button_press_event", self.solve_event)
gcompris.utils.item_focus_init(self.tuxitem, None)
# The Buble
self.bubbleitem = goocanvas.Svg(
parent = self.backroot,
svg_handle = svghandle,
svg_id = "#BUBBLE1",
)
self.textitem = goocanvas.Text(
parent = self.backroot,
x = gcompris.BOARD_WIDTH/2 + 80,
y = gcompris.BOARD_HEIGHT - 80,
width = 400,
font = gcompris.skin.get_font("gcompris/content"),
text = _("Switch off all the lights, I have to go to sleep.\n"
"If you need help, click on me."),
fill_color = "black",
anchor = gtk.ANCHOR_CENTER
)
# The Tipi
self.tipiitem = goocanvas.Svg(
parent = self.backroot,
svg_handle = svghandle,
svg_id = "#TIPI",
)
# The image foreground
goocanvas.Svg(
parent = self.backroot,
svg_handle = svghandle,
svg_id = "#FOREGROUND",
pointer_events = goocanvas.EVENTS_NONE
)
# Set the buttons we want in the bar
gcompris.bar_set(gcompris.BAR_LEVEL|gcompris.BAR_REPEAT)
gcompris.bar_location(gcompris.BOARD_WIDTH/2 - 90, -1, 0.6)
gcompris.bar_set_level(self.gcomprisBoard)
self.display_game()
def end(self):
self.backroot.remove()
self.backroot = None
self.rootitem = None
def ok(self):
print("lightsoff ok.")
def repeat(self):
self.display_game()
def config(self):
print("lightsoff config.")
def key_press(self, keyval, commit_str, preedit_str):
pass
def pause(self, pause):
self.board_paused = pause
# When the bonus is displayed, it call us first
# with pause(1) and then with pause(0)
# the game is won
if(self.gamewon == True and pause == False):
self.gamewon = False
if(self.increment_level()):
self.display_game()
def set_level(self, level):
self.gcomprisBoard.level = level
gcompris.bar_set_level(self.gcomprisBoard)
self.display_game()
# Code that increments the sublevel and level
# And bail out if no more levels are available
# return 1 if continue, 0 if bail out
def increment_level(self):
self.gcomprisBoard.sublevel += 1
if(self.gcomprisBoard.sublevel>self.gcomprisBoard.number_of_sublevel):
# Try the next level
self.gcomprisBoard.sublevel=1
self.gcomprisBoard.level += 1
gcompris.bar_set_level(self.gcomprisBoard)
if(self.gcomprisBoard.level>self.gcomprisBoard.maxlevel):
self.gcomprisBoard.level = self.gcomprisBoard.maxlevel
return 1
def create_empty_list(self):
items = []
for x in range(5):
items.append(range(5))
for y in range(len(items)):
for x in range(len(items[0])):
items[y][x] = 0
return items
# Display the board game
def display_game(self):
# The grid we display
# It contains all the graphic items
self.items = self.create_empty_list()
# The grid of hints items
self.hints = self.create_empty_list()
# Do we display the hints
self.hints_mode = False
if self.rootitem:
self.rootitem.remove()
self.tipiitem.props.visibility = goocanvas.ITEM_INVISIBLE
self.textitem.props.visibility = goocanvas.ITEM_VISIBLE
self.tuxitem.props.visibility = goocanvas.ITEM_VISIBLE
self.bubbleitem.props.visibility = goocanvas.ITEM_VISIBLE
# Create our rootitem. We put each canvas item in it so at the end we
# only have to kill it. The canvas deletes all the items it contains
# automaticaly.
self.rootitem = \
goocanvas.Group(parent = self.backroot)
svghandle = gcompris.utils.load_svg("lightsoff/onoff.svgz")
iwidth = svghandle.props.width
iheight = svghandle.props.height
gap = 10
x_start = (gcompris.BOARD_WIDTH - len(self.items) * (iwidth + gap) ) / 2
y_start = (gcompris.BOARD_HEIGHT - len(self.items[0]) \
* (iheight + gap) ) / 2 - 40
goocanvas.Rect(
parent = self.rootitem,
x = x_start - gap,
y = y_start - gap,
width = len(self.items) * (iwidth + gap) + gap,
height = len(self.items[0]) * (iheight + gap) + gap,
fill_color_rgba = 0x445533AAL,
stroke_color_rgba = 0xC0C0C0AAL,
radius_x = 10,
radius_y = 10,
line_width = 2
)
data = self.data[self.gcomprisBoard.level - 1]
for y in range(len(self.items)):
for x in range(len(self.items[0])):
item = goocanvas.Rect(
parent = self.rootitem,
x = x_start + (iwidth + gap) * x - gap/2,
y = y_start + (iheight + gap) * y - gap/2,
width = iwidth + gap,
height = iheight + gap,
stroke_color_rgba = 0xC0C0C0FAL,
fill_color_rgba = 0x5050509AL,
line_width = 2,
radius_x = 10,
radius_y = 10,
)
self.hints[y][x] = item
item.props.visibility = goocanvas.ITEM_INVISIBLE
state = data[y][x]
svg_id = "#off"
if state:
svg_id = "#on"
item = goocanvas.Svg(
parent = self.rootitem,
svg_handle = svghandle,
svg_id = svg_id,
)
item.set_data("state", state)
item.translate(x_start + (iwidth + gap) * x,
y_start + (iheight + gap) * y)
item.connect("button_press_event", self.button_press_event, [y,x])
self.items[y][x] = item
self.solve_it()
def is_on(self, item):
return item.get_data("state")
def switch(self, item):
if not item:
return
mystate = self.is_on(item)
if mystate == False:
item.set_properties(svg_id = "#on")
else:
item.set_properties(svg_id = "#off")
item.set_data("state", not mystate)
def get_item_up(self, y, x):
if y == 0:
return None
return self.items[y-1][x]
def get_item_down(self, y, x):
if y == len(self.items[0])-1:
return None
return self.items[y+1][x]
def get_item_left(self, y, x):
if x == 0:
return None
return self.items[y][x-1]
def get_item_right(self, y, x):
if x == len(self.items)-1:
return None
return self.items[y][x+1]
# Returns True when complete
def is_done(self):
for y in range(len(self.items)):
for x in range(len(self.items[0])):
if self.is_on(self.items[y][x]):
return False
return True
def button_press_event(self, widget, target, event, spot):
self.switch(target)
self.switch(self.get_item_up(spot[0], spot[1]))
self.switch(self.get_item_left(spot[0], spot[1]))
self.switch(self.get_item_right(spot[0], spot[1]))
self.switch(self.get_item_down(spot[0], spot[1]))
self.solve_it()
if self.is_done():
self.tipiitem.props.visibility = goocanvas.ITEM_VISIBLE
self.textitem.props.visibility = goocanvas.ITEM_INVISIBLE
self.tuxitem.props.visibility = goocanvas.ITEM_INVISIBLE
self.bubbleitem.props.visibility = goocanvas.ITEM_INVISIBLE
self.gamewon = True
gcompris.bonus.display(gcompris.bonus.WIN, gcompris.bonus.FLOWER)
def solution_switch(self, items, clicks, y, x):
items[y][x] = not items[y][x]
clicks[y][x] = not clicks[y][x]
if y >= 1:
items[y-1][x] = not items[y-1][x]
if y <= 3:
items[y+1][x] = not items[y+1][x]
if x >= 1:
items[y][x-1] = not items[y][x-1]
if x <= 3:
items[y][x+1] = not items[y][x+1]
def chase_light(self, items, clicks):
for y in range(1, len(items)):
for x in range(len(items[0])):
if items[y-1][x]:
self.solution_switch(items, clicks, y, x)
def is_solution_pattern(self, s, a, b, c, d, e):
if s[4][0] == a and \
s[4][1] == b and \
s[4][2] == c and \
s[4][3] == d and \
s[4][4] == e:
return True
return False
# Return False if the is no solution
def solution_wrap(self, solution, clicks):
if self.is_solution_pattern(solution, 1, 0, 0 , 0, 1):
self.solution_switch(solution, clicks, 0, 0)
self.solution_switch(solution, clicks, 0, 1)
elif self.is_solution_pattern(solution, 0, 1, 0, 1, 0):
self.solution_switch(solution, clicks, 0, 0)
self.solution_switch(solution, clicks, 0, 3)
elif self.is_solution_pattern(solution, 1, 1, 1, 0, 0):
self.solution_switch(solution, clicks, 0, 1)
elif self.is_solution_pattern(solution, 0, 0, 1, 1 , 1):
self.solution_switch(solution, clicks, 0, 3)
elif self.is_solution_pattern(solution, 1, 0, 1, 1, 0):
self.solution_switch(solution, clicks, 0, 4)
elif self.is_solution_pattern(solution, 0, 1, 1, 0, 1):
self.solution_switch(solution, clicks, 0, 0)
elif self.is_solution_pattern(solution, 1, 1, 0, 1, 1):
self.solution_switch(solution, clicks, 0, 2)
else:
return False
return True
def items2list(self, items):
list = []
for y in range(len(items[0])):
line = []
for x in range(len(items)):
if self.is_on(items[y][x]):
line.append(1)
else:
line.append(0)
list.append(line)
return list
# We check only the last line
def solution_found(self, solution):
for x in range(len(solution[0])):
if solution[4][x]:
return False
return True
def solution_length(self, clicks):
click = 0
for y in range(0, len(clicks)):
for x in range(len(clicks[0])):
if clicks[y][x]:
click += 1
return click
def solve_one(self, solution, clicks):
found = False
for index in range(0, 5):
self.chase_light( solution, clicks )
if self.solution_found(solution):
found = True
break
if not self.solution_wrap(solution, clicks):
break
if found:
return clicks
else:
return None
# Solving algorithm is the one described here:
# http://www.haar.clara.co.uk/Lights/solving.html To begin, you turn
# out all the lights on the top row, by pressing the buttons on the
# second row that are directly underneath any lit buttons on the top
# row. The top row will then have all it's lights off. Repeat this
# step for the second, third and fourth row. (i.e. chase the lights
# all the way down to the bottom row). This may have solved the
# puzzle already ( click here for an example of this ), but is more
# likely that there will now be some lights left on in the bottom
# row. If so, there are only 7 posible configurations. Depending on
# which configuration you are left with, you need to press some
# buttons in the top row. You can determine which buttons you need
# to press from the following table.
# Light on bottom row Press on this on top row
# 10001 11000
# 01010 10010
# 11100 01000
# 00111 00010
# 10110 00001
# 01101 10000
# 11011 00100
def solve_it(self):
clicks = None
# Our solving algorithm does not find the shortest solution. We
# don't really care but we'd like to keep the proposed solution
# stable (not propose a complete new solution when one light
# changes). To achieve this (closely), we test here all the
# combination of the first line, trying to find the shortest
# solution.
for x in range(64):
solution = self.items2list(self.items)
clicks2 = self.create_empty_list()
if x & 1:
self.solution_switch(solution, clicks2, 0, 0)
if x & 2:
self.solution_switch(solution, clicks2, 0, 1)
if x & 4:
self.solution_switch(solution, clicks2, 0, 2)
if x & 8:
self.solution_switch(solution, clicks2, 0, 3)
if x & 16:
self.solution_switch(solution, clicks2, 0, 4)
clicks2 = self.solve_one(solution, clicks2)
if clicks == None and clicks2:
clicks = clicks2
elif clicks2 and \
self.solution_length(clicks2) < self.solution_length(clicks):
clicks = clicks2
if self.hints_mode:
self.show_hints(clicks)
self.update_background(clicks)
def solve_event(self, widget, target, event):
clicks = self.create_empty_list()
self.hints_mode = not self.hints_mode
if not self.hints_mode:
self.show_hints(clicks)
else:
self.solve_it()
def update_background(self, clicks):
length = self.solution_length(clicks)
c = int(length * 0xFF / 18.0)
color = 0X33 << 24 | 0x11 << 16 | c << 8 | 0xFFL
self.background.set_properties(fill_color_rgba = color)
self.sunitem.translate(0, self.sunitem_offset)
self.sunitem_offset = length * 10
self.sunitem.translate(0, self.sunitem_offset * -1)
def show_hints(self, clicks):
for y in range(len(clicks)):
for x in range(len(clicks[0])):
if clicks[y][x]:
self.hints[y][x].props.visibility = goocanvas.ITEM_VISIBLE
else:
self.hints[y][x].props.visibility = goocanvas.ITEM_INVISIBLE
def print_sol(self, clicks):
for y in range(len(clicks)):
s = ""
for x in range(len(clicks[0])):
if clicks[y][x]:
s += "1"
else:
s += "0"
print s
print ""
|