summaryrefslogtreecommitdiffstats
path: root/doc/scriptexamples/tutorial/minesweeper1.kvs
diff options
context:
space:
mode:
Diffstat (limited to 'doc/scriptexamples/tutorial/minesweeper1.kvs')
-rw-r--r--doc/scriptexamples/tutorial/minesweeper1.kvs78
1 files changed, 78 insertions, 0 deletions
diff --git a/doc/scriptexamples/tutorial/minesweeper1.kvs b/doc/scriptexamples/tutorial/minesweeper1.kvs
new file mode 100644
index 00000000..f4f5f5ee
--- /dev/null
+++ b/doc/scriptexamples/tutorial/minesweeper1.kvs
@@ -0,0 +1,78 @@
+# The scripts in this tutorial implement the famous minesweeper game
+# STEP 1
+
+
+
+# First of all we create the main game widget
+# The minesweeper widget inherits from the widget class
+class(minesweepermain,widget)
+{
+ # The constructor sets the basic widget properties
+ # and creates the child widgets
+ constructor()
+ {
+ # Set the widget caption
+ $$->$setCaption("KVIrc's Minesweeper (0.1.0)");
+
+ # We will have a variable number of rows , columns and mines
+ # For now we hardcorde it , later they might become user definable parameters
+ $$->%rows = 10
+ $$->%cols = 10
+ $$->%mines = 10
+
+ # The child labels will be put in a layout that will manage automatically their geometries
+ $$->%layout = $new(layout,$this)
+
+ # Time to create the child labels
+ for(%i = 0;%i < $$->%rows;%i++)
+ {
+ for(%j = 0;%j < $$->%cols;%j++)
+ {
+ # We use a dictionary to simulate a two dimensional array
+ # The label references are stored in the dictionary associated to a key
+ # that is build from the row and column index
+ $$->%label{%i,%j}=$new(label,$this,"%i_%j")
+ # Each label must remember its position
+ $$->%label{%i,%j}->%row = %i
+ $$->%label{%i,%j}->%col = %j
+ # We add the labels to the layout grid
+ $$->%layout->$addWidget($$->%label{%i,%j},%i,%j)
+ }
+ }
+
+ # Time to initialize a new game
+ $$->$newGame();
+ }
+
+ # We need no destructor for now : the child widgets and the layout will be
+ # destroyed when the user will close the main widget
+
+
+ # Here we start a new game
+ newGame()
+ {
+ # We set the labels
+ for(%i = 0;%i < $$->%rows;%i++)
+ {
+ for(%j = 0;%j < $$->%cols;%j++)
+ {
+ # KVIrc is parsed on-the-fly so we use the following line as optimisation.
+ # parsing %l is really faster than parsing a $$->%label{%i,%j}
+ %l = $$->%label{%i,%j}
+ %l->$setFrameStyle(Raised,WinPanel); # raised labels are "unpressed" buttons
+ %l->%bIsMine = 0 # for now it is NOT a mine
+ %l->%numMines = 0 # number of adiacent mines , for now 0
+ %l->%bIsDiscovered = 0 # this label has been pressed ?
+ %l->$setText("") # set the text to an empty string
+ }
+ }
+ }
+}
+
+# Create an instance of the minesweepermain object
+
+%m = $new(minesweepermain)
+%m->$show()
+
+# /parse this file
+