(************** Content-type: application/mathematica ************** CreatedBy='Mathematica 4.2' Mathematica-Compatible Notebook This notebook can be used with any Mathematica-compatible application, such as Mathematica, MathReader or Publicon. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. *******************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 20420, 515]*) (*NotebookOutlinePosition[ 21223, 544]*) (* CellTagsIndexPosition[ 21148, 538]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Code", "Section", InitializationCell->True], Cell["\<\ This is a convenience variable that specifies the pattern many of \ the below functions take when they are expecting a \"GameBoard\" \ argument.\ \>", "Text"], Cell[BoxData[ \(\(GameBoard = Table[True | False, {5}, {5}];\)\)], "Input", InitializationCell->True], Cell["\<\ Create a new clear game board with all lights initially set to \ false.\ \>", "Text"], Cell[BoxData[ \(\(ClearBoard[] := Table[False, {5}, {5}];\)\)], "Input", InitializationCell->True], Cell["\<\ Adjust the given game board after a move has been made. Toggle the \ light that was pressed, as well as the lights immediately above, below, to \ the left, and to the right of the light that was pressed.\ \>", "Text"], Cell[BoxData[ \(\(AdjustBoard[board : GameBoard, x_Integer, y_Integer] := \[IndentingNewLine]Module[{b = board}, \[IndentingNewLine]b[\([y, x]\)] = \(! b[\([y, x]\)]\); \[IndentingNewLine]If[x > 1, b[\([y, x - 1]\)] = \(! b[\([y, x - 1]\)]\)]; \[IndentingNewLine]If[x < 5, b[\([y, x + 1]\)] = \(! b[\([y, x + 1]\)]\)]; \[IndentingNewLine]If[y > 1, b[\([y - 1, x]\)] = \(! b[\([y - 1, x]\)]\)]; \[IndentingNewLine]If[ y < 5, b[\([y + 1, x]\)] = \(! b[\([y + 1, x]\)]\)]; \[IndentingNewLine]b\[IndentingNewLine]];\)\)], \ "Input", InitializationCell->True], Cell["\<\ Create a new game board with the given number of moves made in \ reverse as its initial state. Ideally, the more moves made, the harder the \ board would be to solve. But for this particular game that's not quite the \ case, since many moves can cancel each other out...\ \>", "Text"], Cell[BoxData[ \(\(NewBoard[ iter_Integer] := \[IndentingNewLine]Module[{b = ClearBoard[]}, \[IndentingNewLine]Do[ b = AdjustBoard[ b, \[IndentingNewLine]Random[ Integer, {1, 5}], \[IndentingNewLine]Random[ Integer, {1, 5}]\[IndentingNewLine]], {iter}]; \[IndentingNewLine]b\ \[IndentingNewLine]];\)\)], "Input", InitializationCell->True], Cell["\<\ Set the current game board in the notebook window to the given game \ board state. If a board already exists, find it and overwrite it, otherwise \ create a new game board.\ \>", "Text"], Cell[BoxData[ \(\(SetBoard[ board : GameBoard] := \[IndentingNewLine]Module[{nb = ButtonNotebook[]}, \[IndentingNewLine]\(If[ nb =!= $Failed && NotebookFind[nb, "\", All, CellTags] =!= $Failed, \[IndentingNewLine]NotebookWrite[ nb, Cell[BoxData[MakeGrid[board]], CellTags -> "\", Editable \[Rule] False], All], \[IndentingNewLine]CellPrint[ Cell[BoxData[MakeGrid[board]], CellTags -> "\", Editable \[Rule] False]]\[IndentingNewLine]];\)\[IndentingNewLine]];\)\)], \ "Input", InitializationCell->True], Cell["\<\ Take appropriate action when one of the lights is clicked. First \ adjust the board as necessary. Next check to see if the player won (if all \ the lights are off or False) and do a little animation if they did.\ \>", \ "Text"], Cell[BoxData[ \(\(ClickBoard[board : GameBoard, x_Integer, y_Integer] := \[IndentingNewLine]Module[{b = board, xx, yy}, \[IndentingNewLine]b = AdjustBoard[board, x, y]; \[IndentingNewLine]If[ FreeQ[b, True], \[IndentingNewLine]Do[\[IndentingNewLine]b = ClearBoard[]; \[IndentingNewLine]b[\([yy, xx]\)] = True; \[IndentingNewLine]SetBoard[b], {yy, 1, 5}, {xx, 1, 5}]; \[IndentingNewLine]b = ClearBoard[]\[IndentingNewLine]]; \[IndentingNewLine]SetBoard[ b];\[IndentingNewLine]];\)\)], "Input", InitializationCell->True], Cell["\<\ Create a button box to represent a given light on the game board. \ It's color will indicate its state, and the button function will do the \ appropriate action to toggle the light.\ \>", "Text"], Cell[BoxData[ \(\(MakeButton[board : GameBoard, x_Integer, y_Integer] := \[IndentingNewLine]ButtonBox["\< \>", \ \[IndentingNewLine]Active \[Rule] True, \[IndentingNewLine]ButtonEvaluator \[Rule] Automatic, \[IndentingNewLine]ButtonFunction \[RuleDelayed] ClickBoard[board, x, y], \[IndentingNewLine]ButtonMargins \[Rule] 10, \[IndentingNewLine]Background \[Rule] If[board[\([y, x]\)], RGBColor[1, 0, 0], RGBColor[0, 0, 1]]\[IndentingNewLine]];\)\)], "Input", InitializationCell->True], Cell["\<\ Create a grid box containing the appropriate buttons to represent a \ given game board.\ \>", "Text"], Cell[BoxData[ \(\(MakeGrid[ board : GameBoard] \ := \[IndentingNewLine]GridBox[\[IndentingNewLine]Table[ Table[MakeButton[board, x, y], {x, 1, 5}], {y, 1, 5}], \[IndentingNewLine]RowSpacings \[Rule] 0.1, ColumnSpacings \[Rule] 0.1\[IndentingNewLine]];\)\)], "Input", InitializationCell->True] }, Closed]], Cell[CellGroupData[{ Cell["Game", "Section"], Cell["\<\ Each of the buttons in the grid below represents a light. The red \ buttons indicate a light that is on, while the blue buttons indicate a light \ that is off. The purpose of this game is to get all of the lights of. Each \ time you press a light the state of that light will change, as will the state \ of the lights immediately above, below, to the left, and to the right of the \ light you pressed.\ \>", "Text"], Cell[BoxData[GridBox[{ { ButtonBox[\(Easy\ Game\), ButtonFunction:>SetBoard[ NewBoard[ Random[ Integer, {4, 8}]]], ButtonEvaluator->Automatic, Active->True], ButtonBox[\(Medium\ Game\), ButtonFunction:>SetBoard[ NewBoard[ Random[ Integer, {8, 24}]]], ButtonEvaluator->Automatic, Active->True], ButtonBox[\(Difficult\ Game\), ButtonFunction:>SetBoard[ NewBoard[ Random[ Integer, {24, 64}]]], ButtonEvaluator->Automatic, Active->True]} }]], "Input"], Cell[BoxData[GridBox[{ { ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 1, 1], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[1, 0, 0]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 2, 1], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 3, 1], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 4, 1], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 5, 1], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]]}, { ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 1, 2], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 2, 2], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[1, 0, 0]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 3, 2], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 4, 2], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 5, 2], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]]}, { ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 1, 3], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 2, 3], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 3, 3], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[1, 0, 0]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 4, 3], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 5, 3], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]]}, { ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 1, 4], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 2, 4], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 3, 4], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 4, 4], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[1, 0, 0]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 5, 4], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]]}, { ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 1, 5], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 2, 5], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 3, 5], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 4, 5], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[0, 0, 1]], ButtonBox[" ", ButtonFunction:> ClickBoard[ {{True, False, False, False, False}, {False, True, False, False, False}, {False, False, True, False, False}, { False, False, False, True, False}, {False, False, False, False, True}}, 5, 5], ButtonEvaluator->Automatic, Active->True, ButtonMargins->10, Background->RGBColor[1, 0, 0]]} }, RowSpacings->0.1, ColumnSpacings->0.1]], Editable->False, CellTags->"GameBoard"] }, Open ]] }, FrontEndVersion->"4.2 for Macintosh", ScreenRectangle->{{0, 1246}, {0, 832}}, AutoGeneratedPackage->None, WindowSize->{559, 500}, WindowMargins->{{280, Automatic}, {Automatic, 64}} ] (******************************************************************* Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. *******************************************************************) (*CellTagsOutline CellTagsIndex->{ "GameBoard"->{ Cell[8363, 228, 12041, 284, 165, InheritFromParent, CellTags->"GameBoard"]} } *) (*CellTagsIndex CellTagsIndex->{ {"GameBoard", 21036, 531} } *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1776, 53, 51, 1, 56, "Section", InitializationCell->True], Cell[1830, 56, 168, 4, 50, "Text"], Cell[2001, 62, 107, 2, 27, "Input", InitializationCell->True], Cell[2111, 66, 95, 3, 32, "Text"], Cell[2209, 71, 104, 2, 27, "Input", InitializationCell->True], Cell[2316, 75, 228, 4, 50, "Text"], Cell[2547, 81, 755, 15, 155, "Input", InitializationCell->True], Cell[3305, 98, 296, 5, 68, "Text"], Cell[3604, 105, 453, 10, 139, "Input", InitializationCell->True], Cell[4060, 117, 197, 4, 50, "Text"], Cell[4260, 123, 727, 14, 187, "Input", InitializationCell->True], Cell[4990, 139, 239, 5, 50, "Text"], Cell[5232, 146, 666, 12, 203, "Input", InitializationCell->True], Cell[5901, 160, 206, 4, 50, "Text"], Cell[6110, 166, 582, 10, 155, "Input", InitializationCell->True], Cell[6695, 178, 111, 3, 32, "Text"], Cell[6809, 183, 346, 7, 107, "Input", InitializationCell->True] }, Closed]], Cell[CellGroupData[{ Cell[7192, 195, 23, 0, 36, "Section"], Cell[7218, 197, 428, 7, 86, "Text"], Cell[7649, 206, 711, 20, 33, "Input"], Cell[8363, 228, 12041, 284, 165, InheritFromParent, CellTags->"GameBoard"] }, Open ]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)