diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-02-24 02:13:59 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-02-24 02:13:59 +0000 |
commit | a6d58bb6052ac8cb01805a48c4ad2f129126116f (patch) | |
tree | dd867a099fcbb263a8009a9fb22695b87855dad6 /doc/scriptexamples/tutorial | |
download | kvirc-a6d58bb6052ac8cb01805a48c4ad2f129126116f.tar.gz kvirc-a6d58bb6052ac8cb01805a48c4ad2f129126116f.zip |
Added KDE3 version of kvirc
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/kvirc@1095341 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'doc/scriptexamples/tutorial')
-rw-r--r-- | doc/scriptexamples/tutorial/Makefile.am | 5 | ||||
-rw-r--r-- | doc/scriptexamples/tutorial/minesweeper1.kvs | 78 | ||||
-rw-r--r-- | doc/scriptexamples/tutorial/minesweeper2.kvs | 93 | ||||
-rw-r--r-- | doc/scriptexamples/tutorial/minesweeper3.kvs | 147 | ||||
-rw-r--r-- | doc/scriptexamples/tutorial/minesweeper4.kvs | 187 | ||||
-rw-r--r-- | doc/scriptexamples/tutorial/minesweeper5.kvs | 201 | ||||
-rw-r--r-- | doc/scriptexamples/tutorial/minesweeper6.kvs | 254 | ||||
-rw-r--r-- | doc/scriptexamples/tutorial/minesweeper7.kvs | 253 |
8 files changed, 1218 insertions, 0 deletions
diff --git a/doc/scriptexamples/tutorial/Makefile.am b/doc/scriptexamples/tutorial/Makefile.am new file mode 100644 index 00000000..914e3823 --- /dev/null +++ b/doc/scriptexamples/tutorial/Makefile.am @@ -0,0 +1,5 @@ +############################################################################### +# KVirc IRC client Makefile - 16.12.98 Szymon Stefanek <stefanek@tin.it> +############################################################################### + +EXTRA_DIST = *.kvs 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 + diff --git a/doc/scriptexamples/tutorial/minesweeper2.kvs b/doc/scriptexamples/tutorial/minesweeper2.kvs new file mode 100644 index 00000000..16eb3172 --- /dev/null +++ b/doc/scriptexamples/tutorial/minesweeper2.kvs @@ -0,0 +1,93 @@ +# The scripts in this tutorial implement the famous minesweeper game +# STEP 2 + +class(minesweepermain,widget) +{ + constructor() + { + $$->$setCaption("KVIrc's Minesweeper (0.1.0)"); + + $$->%rows = 10 + $$->%cols = 10 + $$->%mines = 10 + + $$->%layout = $new(layout,$this) + + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + $$->%label{%i,%j}=$new(label,$this,"%i_%j") + $$->%label{%i,%j}->%row = %i + $$->%label{%i,%j}->%col = %j + $$->%layout->$addWidget($$->%label{%i,%j},%i,%j) + } + } + + $$->$newGame() + } + + newGame() + { + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + %l = $$->%label{%i,%j} + %l->$setFrameStyle(Raised,WinPanel); + %l->%bIsMine = 0 + %l->%numMines = 0 + %l->%bIsDiscovered = 0 + %l->$setText("") + } + } + # Here we drop the mines around: it is a bit complex problem: + # We want to have a fixed number of mines placed randomly in our grid + # + # So .. for each mine that we have to place... + for(%i = 0;%i < $$->%mines;%i++) + { + # Choose a random position for this mine + %row = $rand($($$->%rows - 1)) + %col = $rand($($$->%cols - 1)) + # Ensure that we're not placing this mine over an existing one + while($$->%label{%row,%col}->%bIsMine != 0) + { + # If there was already a mine, then choose the position again + %row = $rand($($$->%rows - 1)) + %col = $rand($($$->%cols - 1)) + } + # Ok.. this is a mine then + $$->%label{%row,%col}->%bIsMine = 1 + + # increase the mine count for the adiacent cells: this is again a bit complex thingie + if(%row > 0) + { + # There is a row over our mine: the cells above must have their mine count updated + # The cell just above us + $$->%label{$(%row - 1),%col}->%numMines++ + # The cell above on the left (if exists) + if(%col > 0)$$->%label{$(%row - 1),$(%col - 1)}->%numMines++ + # The cell above on the right (if exists) + if(%col < ($$->%cols - 1))$$->%label{$(%row - 1),$(%col + 1)}->%numMines++ + } + if(%row < ($$->%rows - 1)) + { + # There is a row below our mine: the cells below must have their mine count updated + # The cell just below us + $$->%label{$(%row + 1),%col}->%numMines++ + # The cell below on the left (if exists) + if(%col > 0)$$->%label{$(%row + 1),$(%col - 1)}->%numMines++ + # The cell below on the right (if exists) + if(%col < ($$->%cols - 1))$$->%label{$(%row + 1),$(%col + 1)}->%numMines++ + } + # Now the cell on the left side (if exists) + if(%col > 0)$$->%label{%row,$(%col - 1)}->%numMines++ + # And on the right side (if exists) + if(%col < ($$->%cols - 1))$$->%label{%row,$(%col + 1)}->%numMines++ + } + } +} + +%m = $new(minesweepermain) +%m->$show() diff --git a/doc/scriptexamples/tutorial/minesweeper3.kvs b/doc/scriptexamples/tutorial/minesweeper3.kvs new file mode 100644 index 00000000..7522c390 --- /dev/null +++ b/doc/scriptexamples/tutorial/minesweeper3.kvs @@ -0,0 +1,147 @@ +# The scripts in this tutorial implement the famous minesweeper game +# STEP 3 + +# We want the labels to report us the clicks thus we will use +# a class derived from label +class(minelabel,label) +{ + # We ovverride the implementation for mousePressEvent() + # and we signal the mouse press to the parent minesweeper widget + mousePressEvent() + { + # We could check that $$->$parent() is a minesweeper in fact... + # but well... let's keep it simple :) + $$->$parent()->$mineLabelPressed($this) + } +} + +class(minesweepermain,widget) +{ + constructor() + { + $$->$setCaption("KVIrc's Minesweeper (0.1.0)"); + + $$->%rows = 10 + $$->%cols = 10 + $$->%mines = 10 + + $$->%layout = $new(layout,$this) + + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + # we change label to minelabel then + #$$->%label{%i,%j}=$new(label,$this,"%i_%j") + $$->%label{%i,%j}=$new(minelabel,$this,"%i_%j") + $$->%label{%i,%j}->%row = %i + $$->%label{%i,%j}->%col = %j + $$->%layout->$addWidget($$->%label{%i,%j},%i,%j) + } + } + + $$->$newGame() + } + + newGame() + { + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + %l = $$->%label{%i,%j} + %l->$setFrameStyle(Raised,WinPanel); + %l->%bIsMine = 0 + %l->%numMines = 0 + %l->%bIsDiscovered = 0 + %l->$setText("") + } + } + # drop the mines + for(%i = 0;%i < $$->%mines;%i++) + { + %row = $rand($($$->%rows - 1)) + %col = $rand($($$->%cols - 1)) + while($$->%label{%row,%col}->%bIsMine != 0) + { + %row = $rand($($$->%rows - 1)) + %col = $rand($($$->%cols - 1)) + } + $$->%label{%row,%col}->%bIsMine = 1 + # increase the mine count for the adiacent cells + if(%row > 0) + { + $$->%label{$(%row - 1),%col}->%numMines++ + if(%col > 0)$$->%label{$(%row - 1),$(%col - 1)}->%numMines++ + if(%col < ($$->%cols - 1))$$->%label{$(%row - 1),$(%col + 1)}->%numMines++ + } + if(%row < ($$->%rows - 1)) + { + $$->%label{$(%row + 1),%col}->%numMines++ + if(%col > 0)$$->%label{$(%row + 1),$(%col - 1)}->%numMines++ + if(%col < ($$->%cols - 1))$$->%label{$(%row + 1),$(%col + 1)}->%numMines++ + } + if(%col > 0)$$->%label{%row,$(%col - 1)}->%numMines++ + if(%col < ($$->%cols - 1))$$->%label{%row,$(%col + 1)}->%numMines++ + } + } + + # A mine label was pressed! + # This function is called by our children minelabel objects + # and the parameter passed is the minelabel reference + mineLabelPressed($0 = mine label object that has been pressed) + { + # Is is a mine ? + if($0->%bIsMine) + { + # Yes , a mine :( + # Game over + $0->$setFrameStyle(WinPanel,Sunken) + $0->$setText("*") + } else { + # Not a mine.. discover the adiacent cells + # We pass the minelabel reference + $$->$discoverCells($0) + } + } + + # This function is recursive: it discovers the adjacent cells + # that are not mines + discoverCells($0 = mine label that has to be discovered) + { + # If it is a mine ,then do not discover it + if($0->%bIsMine)return; + # If it is already discovered , return too + if($0->%bIsDiscovered)return; + # Ok.. this is discovered + $0->%bIsDiscovered = 1 + $0->$setFrameStyle(WinPanel,Sunken) + # If this cell has adjacent mines then show their number and return + if($0->%numMines > 0)$0->$setText($0->%numMines) + else { + # There are no adjacent mines : discover the cells recursively + # This block of code is similar to the one used in dropping the bombs around + if($0->%row > 0) + { + # Discover the cells in the row above + $$->$discoverCells($$->%label{$($0->%row - 1),$0->%col}) + if($0->%col > 0)$$->$discoverCells($$->%label{$($0->%row - 1),$($0->%col - 1)}) + if($0->%col < ($$->%cols - 1))$$->$discoverCells($$->%label{$($0->%row - 1),$($0->%col + 1)}) + } + if($0->%row < ($$->%rows - 1)) + { + # Discover the cells in the row below + $$->$discoverCells($$->%label{$($0->%row + 1),$0->%col}) + if($0->%col > 0)$$->$discoverCells($$->%label{$($0->%row + 1),$($0->%col - 1)}) + if($0->%col < ($$->%cols - 1))$$->$discoverCells($$->%label{$($0->%row + 1),$($0->%col + 1)}) + } + # Discover on the left and right + if($0->%col > 0)$$->$discoverCells($$->%label{$0->%row,$($0->%col - 1)}) + if($0->%col < ($$->%cols - 1))$$->$discoverCells($$->%label{$0->%row,$($0->%col + 1)}) + } + } + +} + +%m = $new(minesweepermain) +%m->$show() diff --git a/doc/scriptexamples/tutorial/minesweeper4.kvs b/doc/scriptexamples/tutorial/minesweeper4.kvs new file mode 100644 index 00000000..0013736b --- /dev/null +++ b/doc/scriptexamples/tutorial/minesweeper4.kvs @@ -0,0 +1,187 @@ +# The scripts in this tutorial implement the famous minesweeper game +# STEP 4 + +class(minelabel,label) +{ + mousePressEvent() + { + $$->$parent()->$mineLabelPressed($this) + } +} + +class(minesweepermain,widget) +{ + constructor() + { + # This is no longer needed here : we move it to the container widget + + $$->%rows = 10 + $$->%cols = 10 + $$->%mines = 10 + # is there a running game ? + $$->%bGameRunning = 0 + + + $$->%layout = $new(layout,$this) + + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + $$->%label{%i,%j}=$new(minelabel,$this,"%i_%j") + $$->%label{%i,%j}->%row = %i + $$->%label{%i,%j}->%col = %j + # Let's set the minimum size of the labels: they look better this way + $$->%label{%i,%j}->$setMinimumWidth(26) + $$->%label{%i,%j}->$setMinimumHeight(26) + $$->%label{%i,%j}->$setAlignment(HCenter , VCenter) + $$->%layout->$addWidget($$->%label{%i,%j},%i,%j) + } + } + # We remove this call, $$->$newGame() will be called from the container widget + #$$->$newGame() + } + + newGame() + { + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + %l = $$->%label{%i,%j} + %l->$setFrameStyle(Raised,WinPanel); + %l->%bIsMine = 0 + %l->%numMines = 0 + %l->%bIsDiscovered = 0 + %l->$setText("") + # Set enabled ! + %l->$setEnabled(1) + } + } + # drop the mines + for(%i = 0;%i < $$->%mines;%i++) + { + %row = $rand($($$->%rows - 1)) + %col = $rand($($$->%cols - 1)) + while($$->%label{%row,%col}->%bIsMine != 0) + { + %row = $rand($($$->%rows - 1)) + %col = $rand($($$->%cols - 1)) + } + $$->%label{%row,%col}->%bIsMine = 1 + # increase the mine count for the adiacent cells + if(%row > 0) + { + $$->%label{$(%row - 1),%col}->%numMines++ + if(%col > 0)$$->%label{$(%row - 1),$(%col - 1)}->%numMines++ + if(%col < ($$->%cols - 1))$$->%label{$(%row - 1),$(%col + 1)}->%numMines++ + } + if(%row < ($$->%rows - 1)) + { + $$->%label{$(%row + 1),%col}->%numMines++ + if(%col > 0)$$->%label{$(%row + 1),$(%col - 1)}->%numMines++ + if(%col < ($$->%cols - 1))$$->%label{$(%row + 1),$(%col + 1)}->%numMines++ + } + if(%col > 0)$$->%label{%row,$(%col - 1)}->%numMines++ + if(%col < ($$->%cols - 1))$$->%label{%row,$(%col + 1)}->%numMines++ + } + # A game is running + $$->%bGameRunning = 1 + } + + mineLabelPressed($0 = mine label object that has been pressed) + { + # When the game isn't running we don't care about the mouse press events + if(!$$->%bGameRunning)return + if($0->%bIsMine) + { + $0->$setFrameStyle(WinPanel,Sunken) + $0->$setImage(82); # 82 is a bomb + # We emit the gameOver signal so the container widget knows + $$->$emit(gameOver) + # And disable all the cells + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + $$->%label{%i,%j}->$setEnabled(0) + } + } + # leave the bomb image enabled :D + $0->$setEnabled(1) + # Game finished :( + $$->%bGameRunning = 0 + } else { + $$->$discoverCells($0) + } + } + + discoverCells($0 = mine label that has to be discovered) + { + if($0->%bIsMine)return; + if($0->%bIsDiscovered)return; + $0->%bIsDiscovered = 1 + $0->$setFrameStyle(WinPanel,Sunken) + if($0->%numMines > 0)$0->$setText($0->%numMines) + else { + if($0->%row > 0) + { + $$->$discoverCells($$->%label{$($0->%row - 1),$0->%col}) + if($0->%col > 0)$$->$discoverCells($$->%label{$($0->%row - 1),$($0->%col - 1)}) + if($0->%col < ($$->%cols - 1))$$->$discoverCells($$->%label{$($0->%row - 1),$($0->%col + 1)}) + } + if($0->%row < ($$->%rows - 1)) + { + $$->$discoverCells($$->%label{$($0->%row + 1),$0->%col}) + if($0->%col > 0)$$->$discoverCells($$->%label{$($0->%row + 1),$($0->%col - 1)}) + if($0->%col < ($$->%cols - 1))$$->$discoverCells($$->%label{$($0->%row + 1),$($0->%col + 1)}) + } + if($0->%col > 0)$$->$discoverCells($$->%label{$0->%row,$($0->%col - 1)}) + if($0->%col < ($$->%cols - 1))$$->$discoverCells($$->%label{$0->%row,$($0->%col + 1)}) + } + } + +} + +# We want a bigger window around our minesweeper widget +# we want to provide some output and user menus... +# Thus we create a container widget class , that will be also our new toplevel widget + +class(minesweeper,widget) +{ + constructor() + { + $$->$setCaption("KVIrc's Minesweeper (0.1.0)"); + + # we need a layout to manage the children + $$->%layout = $new(layout,$$) + + + $$->%gamepool = $new(minesweepermain,$$) + $$->%layout->$addWidget($$->%gamepool,1,0) + connect $$->%gamepool gameOver $$ gameOver + + $$->%output = $new(label,$$) + $$->%output->$setFrameStyle(WinPanel,Sunken) + $$->%layout->$addWidget($$->%output,2,0) + + $$->$newGame() + } + + gameOver() + { + $$->%output->$setText("Game Over :(") + } + + newGame() + { + $$->%gamepool->$newGame() + $$->%output->$setText("Ready") + } +} + + +#We substitute the old creation call with the new container object creation call +#%m = $new(minesweepermain) +%m = $new(minesweeper) +%m->$show() diff --git a/doc/scriptexamples/tutorial/minesweeper5.kvs b/doc/scriptexamples/tutorial/minesweeper5.kvs new file mode 100644 index 00000000..1bda6c83 --- /dev/null +++ b/doc/scriptexamples/tutorial/minesweeper5.kvs @@ -0,0 +1,201 @@ +# The scripts in this tutorial implement the famous minesweeper game +# STEP 5 + + +# First of all we convert all the $$-> to @ : this is faster both to write and for KVIrc to process + +class(minelabel,label) +{ + mousePressEvent() + { + # We also pass the mouse button pressed + @$parent()->$mineLabelPressed($this,$0) + } +} + +class(minesweepermain,widget) +{ + constructor() + { + @%rows = 20 + @%cols = 20 + @%mines = 60 + @%bGameRunning = 0 + + @%layout = $new(layout,$this) + + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + @%label{%i,%j}=$new(minelabel,$this,"%i_%j") + @%label{%i,%j}->%row = %i + @%label{%i,%j}->%col = %j + @%label{%i,%j}->$setMinimumWidth(26) + @%label{%i,%j}->$setMinimumHeight(26) + @%label{%i,%j}->$setAlignment(HCenter , VCenter) + @%layout->$addWidget(@%label{%i,%j},%i,%j) + } + } + } + + newGame() + { + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + %l = @%label{%i,%j} + %l->$setFrameStyle(Raised,WinPanel); + %l->%bIsMine = 0 + %l->%numMines = 0 + %l->%bIsDiscovered = 0 + %l->%iState = 0; # In state 0 it doesn't show anything + %l->$setText("") + %l->$setImage(); # show no image + %l->$setEnabled(1) + } + } + + for(%i = 0;%i < @%mines;%i++) + { + %row = $rand($(@%rows - 1)) + %col = $rand($(@%cols - 1)) + while(@%label{%row,%col}->%bIsMine != 0) + { + %row = $rand($(@%rows - 1)) + %col = $rand($(@%cols - 1)) + } + @%label{%row,%col}->%bIsMine = 1 + if(%row > 0) + { + @%label{$(%row - 1),%col}->%numMines++ + if(%col > 0)@%label{$(%row - 1),$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{$(%row - 1),$(%col + 1)}->%numMines++ + } + if(%row < (@%rows - 1)) + { + @%label{$(%row + 1),%col}->%numMines++ + if(%col > 0)@%label{$(%row + 1),$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{$(%row + 1),$(%col + 1)}->%numMines++ + } + if(%col > 0)@%label{%row,$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{%row,$(%col + 1)}->%numMines++ + } + @%bGameRunning = 1 + } + + mineLabelPressed($0 = mine label object that has been pressed,$1 = the button that has been pressed) + { + if(!@%bGameRunning)return + + # We add the right button handling : the user can mark the cells discovered + if($1 == 1) + { + # Right button was pressed + if($0->%bIsDiscovered)return; # does nothing + # we loop the state thru 0->1->2->0 + switch($0->%iState) + { + case(0): + { + $0->$setImage(58) + $0->%iState = 1 + } + case(1): + { + $0->$setImage(28) + $0->%iState = 2 + } + case(2): + { + $0->$setImage() + $0->%iState = 0 + } + } + return; + } + + if($0->%bIsMine) + { + $0->$setFrameStyle(WinPanel,Sunken) + $0->$setImage(82); # 82 is a bomb + @$emit(gameOver) + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + @%label{%i,%j}->$setEnabled(0) + } + } + $0->$setEnabled(1) + @%bGameRunning = 0 + } else { + @$discoverCells($0) + } + } + + discoverCells($0 = mine label that has to be discovered) + { + if($0->%bIsMine)return; + if($0->%bIsDiscovered)return; + $0->%bIsDiscovered = 1 + $0->$setFrameStyle(WinPanel,Sunken) + if($0->%iState != 0)$0->$setImage(); # ensure that no image is shown + if($0->%numMines > 0)$0->$setText($0->%numMines) + else { + if($0->%row > 0) + { + @$discoverCells(@%label{$($0->%row - 1),$0->%col}) + if($0->%col > 0)@$discoverCells(@%label{$($0->%row - 1),$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$($0->%row - 1),$($0->%col + 1)}) + } + if($0->%row < (@%rows - 1)) + { + @$discoverCells(@%label{$($0->%row + 1),$0->%col}) + if($0->%col > 0)@$discoverCells(@%label{$($0->%row + 1),$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$($0->%row + 1),$($0->%col + 1)}) + } + if($0->%col > 0)@$discoverCells(@%label{$0->%row,$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$0->%row,$($0->%col + 1)}) + } + } + +} + + +class(minesweeper,widget) +{ + constructor() + { + @$setCaption("KVIrc's Minesweeper (0.1.0)"); + + @%layout = $new(layout,$$) + + + @%gamepool = $new(minesweepermain,$$) + @%layout->$addWidget(@%gamepool,1,0) + connect @%gamepool gameOver $$ gameOver + + @%output = $new(label,$$) + @%output->$setFrameStyle(WinPanel,Sunken) + @%layout->$addWidget(@%output,2,0) + + @$newGame() + } + + gameOver() + { + @%output->$setText("Game Over :(") + } + + newGame() + { + @%gamepool->$newGame() + @%output->$setText("Ready") + } +} + + +%m = $new(minesweeper) +%m->$show() diff --git a/doc/scriptexamples/tutorial/minesweeper6.kvs b/doc/scriptexamples/tutorial/minesweeper6.kvs new file mode 100644 index 00000000..f03ffb05 --- /dev/null +++ b/doc/scriptexamples/tutorial/minesweeper6.kvs @@ -0,0 +1,254 @@ +# The scripts in this tutorial implement the famous minesweeper game +# STEP 6 + +class(minelabel,label) +{ + mousePressEvent() + { + # We also pass the mouse button pressed + @$parent()->$mineLabelPressed($this,$0) + } +} + +class(minesweepermain,widget) +{ + # We accept constructor parameters now + constructor($0 = rows,$1 = cols,$2 = num mines) + { + # We should check the parameters passed here, and maybe return 0 + # if the params are not positive numbers + echo $0,$1,$2 + @%rows = $0 + @%cols = $1 + @%mines = $2 + @%bGameRunning = 0 + + @%layout = $new(layout,$this) + + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + @%label{%i,%j}=$new(minelabel,$this,"%i_%j") + @%label{%i,%j}->%row = %i + @%label{%i,%j}->%col = %j + @%label{%i,%j}->$setMinimumWidth(26) + @%label{%i,%j}->$setMinimumHeight(26) + @%label{%i,%j}->$setAlignment(HCenter , VCenter) + @%layout->$addWidget(@%label{%i,%j},%i,%j) + } + } + } + + newGame() + { + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + %l = @%label{%i,%j} + %l->$setFrameStyle(Raised,WinPanel); + %l->%bIsMine = 0 + %l->%numMines = 0 + %l->%bIsDiscovered = 0 + %l->%iState = 0; # In state 0 it doesn't show anything + %l->$setText("") + %l->$setImage(); # show no image + %l->$setEnabled(1) + } + } + + for(%i = 0;%i < @%mines;%i++) + { + %row = $rand($(@%rows - 1)) + %col = $rand($(@%cols - 1)) + while(@%label{%row,%col}->%bIsMine != 0) + { + %row = $rand($(@%rows - 1)) + %col = $rand($(@%cols - 1)) + } + @%label{%row,%col}->%bIsMine = 1 + if(%row > 0) + { + @%label{$(%row - 1),%col}->%numMines++ + if(%col > 0)@%label{$(%row - 1),$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{$(%row - 1),$(%col + 1)}->%numMines++ + } + if(%row < (@%rows - 1)) + { + @%label{$(%row + 1),%col}->%numMines++ + if(%col > 0)@%label{$(%row + 1),$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{$(%row + 1),$(%col + 1)}->%numMines++ + } + if(%col > 0)@%label{%row,$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{%row,$(%col + 1)}->%numMines++ + } + @%bGameRunning = 1 + # We keep track of the number of the discovered cells + @%iTotalNonMineCells = $((@%rows * @%cols) - @%mines) + @%iUndiscoveredNonMineCells = @%iTotalNonMineCells + } + + mineLabelPressed($0 = mine label object that has been pressed,$1 = the button that has been pressed) + { + if(!@%bGameRunning)return + + # We add the right button handling : the user can mark the cells discovered + if($1 == 1) + { + # Right button was pressed + if($0->%bIsDiscovered)return; # does nothing + # we loop the state thru 0->1->2->0 + switch($0->%iState) + { + case(0): + { + $0->$setImage(58) + $0->%iState = 1 + } + case(1): + { + $0->$setImage(28) + $0->%iState = 2 + } + case(2): + { + $0->$setImage() + $0->%iState = 0 + } + } + return; + } + + if($0->%bIsMine) + { + @$gameFailure($0) + } else { + @$discoverCells($0) + if(@%iUndiscoveredNonMineCells == 0) + { + # Finished! + @$gameSuccess() + } else { + @$emit(userMoved) + } + } + } + + discoverAllCells() + { + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + %l = @%label{%i,%j} + if(!%l->%bIsDiscovered) + { + %l->$setFrameStyle(WinPanel,Sunken) + if(%l->%bIsMine)%l->$setImage(82) + else { + %l->$setImage("") + if(%l->%numMines > 0)%l->$setText(%l->%numMines) + else %l->$setText("") + } + } + %l->$setEnabled(0) + } + } + } + + gameFailure($0 = the game cell that had the bomb) + { + @$discoverAllCells() + $0->$setFrameStyle(WinPanel,Sunken) + $0->$setImage(82); # 82 is a bomb + $0->$setEnabled(1) + @%bGameRunning = 0 + @$emit(gameOver) + } + + gameSuccess() + { + @$discoverAllCells() + @%bGameRunning = 0 + @$emit(success) + } + + discoverCells($0 = mine label that has to be discovered) + { + if($0->%bIsMine)return; + if($0->%bIsDiscovered)return; + $0->%bIsDiscovered = 1 + $0->$setFrameStyle(WinPanel,Sunken) + @%iUndiscoveredNonMineCells-- + if($0->%iState != 0)$0->$setImage(); # ensure that no image is shown + if($0->%numMines > 0)$0->$setText($0->%numMines) + else { + if($0->%row > 0) + { + @$discoverCells(@%label{$($0->%row - 1),$0->%col}) + if($0->%col > 0)@$discoverCells(@%label{$($0->%row - 1),$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$($0->%row - 1),$($0->%col + 1)}) + } + if($0->%row < (@%rows - 1)) + { + @$discoverCells(@%label{$($0->%row + 1),$0->%col}) + if($0->%col > 0)@$discoverCells(@%label{$($0->%row + 1),$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$($0->%row + 1),$($0->%col + 1)}) + } + if($0->%col > 0)@$discoverCells(@%label{$0->%row,$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$0->%row,$($0->%col + 1)}) + } + } + +} + + +class(minesweeper,widget) +{ + constructor() + { + @$setCaption("KVIrc's Minesweeper (0.1.0)"); + + @%layout = $new(layout,$$) + + # Create a 10x10 game pool with 10 mines + @%gamepool = $new(minesweepermain,$$,myGamepool,10,10,10) + @%layout->$addWidget(@%gamepool,1,0) + connect @%gamepool gameOver $$ gameOver + # We connect the two new signals + connect @%gamepool userMoved $$ showState + connect @%gamepool success $$ success + @%output = $new(label,$$) + @%output->$setFrameStyle(WinPanel,Sunken) + @%layout->$addWidget(@%output,2,0) + + @$newGame() + } + + showState() + { + @%output->$setText("@%gamepool->%iUndiscoveredNonMineCells to go") + } + + success() + { + @%output->$setText("You did it!") + } + + gameOver() + { + @%output->$setText("Game Over :(") + } + + newGame() + { + @%gamepool->$newGame() + @%output->$setText("Ready") + @$showState() + } +} + + +%m = $new(minesweeper) +%m->$show() diff --git a/doc/scriptexamples/tutorial/minesweeper7.kvs b/doc/scriptexamples/tutorial/minesweeper7.kvs new file mode 100644 index 00000000..701c09d3 --- /dev/null +++ b/doc/scriptexamples/tutorial/minesweeper7.kvs @@ -0,0 +1,253 @@ +# The scripts in this tutorial implement the famous minesweeper game +# STEP 7 + +class(minelabel,label) +{ + mousePressEvent() + { + @$parent()->$mineLabelPressed($this,$0) + } +} + +class(minesweepermain,widget) +{ + constructor($0 = rows,$1 = cols,$2 = num mines) + { + # We should check the parameters passed here, and maybe return 0 + # if the params are not positive numbers + echo $0,$1,$2 + @%rows = $0 + @%cols = $1 + @%mines = $2 + @%bGameRunning = 0 + + @%layout = $new(layout,$this) + + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + @%label{%i,%j}=$new(minelabel,$this,"%i_%j") + @%label{%i,%j}->%row = %i + @%label{%i,%j}->%col = %j + @%label{%i,%j}->$setMinimumWidth(26) + @%label{%i,%j}->$setMinimumHeight(26) + @%label{%i,%j}->$setAlignment(HCenter , VCenter) + @%layout->$addWidget(@%label{%i,%j},%i,%j) + } + } + } + + newGame() + { + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + %l = @%label{%i,%j} + %l->$setFrameStyle(Raised,WinPanel); + %l->%bIsMine = 0 + %l->%numMines = 0 + %l->%bIsDiscovered = 0 + %l->%iState = 0; # In state 0 it doesn't show anything + %l->$setText("") + %l->$setImage(); # show no image + %l->$setEnabled(1) + } + } + + for(%i = 0;%i < @%mines;%i++) + { + %row = $rand($(@%rows - 1)) + %col = $rand($(@%cols - 1)) + while(@%label{%row,%col}->%bIsMine != 0) + { + %row = $rand($(@%rows - 1)) + %col = $rand($(@%cols - 1)) + } + @%label{%row,%col}->%bIsMine = 1 + if(%row > 0) + { + @%label{$(%row - 1),%col}->%numMines++ + if(%col > 0)@%label{$(%row - 1),$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{$(%row - 1),$(%col + 1)}->%numMines++ + } + if(%row < (@%rows - 1)) + { + @%label{$(%row + 1),%col}->%numMines++ + if(%col > 0)@%label{$(%row + 1),$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{$(%row + 1),$(%col + 1)}->%numMines++ + } + if(%col > 0)@%label{%row,$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{%row,$(%col + 1)}->%numMines++ + } + @%bGameRunning = 1 + # We keep track of the number of the discovered cells + @%iTotalNonMineCells = $((@%rows * @%cols) - @%mines) + @%iUndiscoveredNonMineCells = @%iTotalNonMineCells + } + + mineLabelPressed($0 = mine label object that has been pressed,$1 = the button that has been pressed) + { + if(!@%bGameRunning)return + + if($1 == 1) + { + # Right button + if($0->%bIsDiscovered)return; # does nothing + switch($0->%iState) + { + case(0): + { + $0->$setImage(58) + $0->%iState = 1 + } + case(1): + { + $0->$setImage(28) + $0->%iState = 2 + } + case(2): + { + $0->$setImage() + $0->%iState = 0 + } + } + return; + } + + if($0->%bIsMine) + { + @$gameFailure($0) + } else { + @$discoverCells($0) + if(@%iUndiscoveredNonMineCells == 0) + { + # Finished! + @$gameSuccess() + } else { + @$emit(userMoved) + } + } + } + + discoverAllCells() + { + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + %l = @%label{%i,%j} + if(!%l->%bIsDiscovered) + { + %l->$setFrameStyle(WinPanel,Sunken) + if(%l->%bIsMine)%l->$setImage(82) + else { + %l->$setImage("") + if(%l->%numMines > 0)%l->$setText(%l->%numMines) + else %l->$setText("") + } + } + %l->$setEnabled(0) + } + } + } + + gameFailure($0 = the game cell that had the bomb) + { + @$discoverAllCells() + $0->$setFrameStyle(WinPanel,Sunken) + $0->$setImage(82); # 82 is a bomb + $0->$setEnabled(1) + @%bGameRunning = 0 + @$emit(gameOver) + } + + gameSuccess() + { + @$discoverAllCells() + @%bGameRunning = 0 + @$emit(success) + } + + discoverCells($0 = mine label that has to be discovered) + { + if($0->%bIsMine)return; + if($0->%bIsDiscovered)return; + $0->%bIsDiscovered = 1 + $0->$setFrameStyle(WinPanel,Sunken) + @%iUndiscoveredNonMineCells-- + if($0->%iState != 0)$0->$setImage(); # ensure that no image is shown + if($0->%numMines > 0)$0->$setText($0->%numMines) + else { + if($0->%row > 0) + { + @$discoverCells(@%label{$($0->%row - 1),$0->%col}) + if($0->%col > 0)@$discoverCells(@%label{$($0->%row - 1),$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$($0->%row - 1),$($0->%col + 1)}) + } + if($0->%row < (@%rows - 1)) + { + @$discoverCells(@%label{$($0->%row + 1),$0->%col}) + if($0->%col > 0)@$discoverCells(@%label{$($0->%row + 1),$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$($0->%row + 1),$($0->%col + 1)}) + } + if($0->%col > 0)@$discoverCells(@%label{$0->%row,$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$0->%row,$($0->%col + 1)}) + } + } + +} + + +class(minesweeper,window) +{ + constructor() + { + @$setCaption("KVIrc's Minesweeper (0.1.0)"); + + @%layout = $new(layout,$$) + + @%menubar = $new(menubar,$$) + @%menubar->$insertItem(test) + @%layout->$addWidget(@%menubar,0,0) + + @%gamepool = $new(minesweepermain,$$,myGamepool,10,10,10) + @%layout->$addWidget(@%gamepool,1,0) + connect @%gamepool gameOver $$ gameOver + # We connect the two new signals + connect @%gamepool userMoved $$ showState + connect @%gamepool success $$ success + @%output = $new(label,$$) + @%output->$setFrameStyle(WinPanel,Sunken) + @%layout->$addWidget(@%output,2,0) + + @$newGame() + } + + showState() + { + @%output->$setText("@%gamepool->%iUndiscoveredNonMineCells to go") + } + + success() + { + @%output->$setText("You did it!") + } + + gameOver() + { + @%output->$setText("Game Over :(") + } + + newGame() + { + @%gamepool->$newGame() + @%output->$setText("Ready") + @$showState() + } +} + + +%m = $new(minesweeper) +%m->$show() |