summaryrefslogtreecommitdiffstats
path: root/doc/scriptexamples/tutorial/minesweeper1.kvs
blob: f4f5f5ee175fa8c9297c30c93ddc6761624a4017 (plain)
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
# 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