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
|
class(minelabel,label)
{
mousePressEvent()
{
$$->$parent()->$mineLabelPressed($this)
}
}
class(minesweeper,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(minelabel,$this,"%i_%j")
$$->%label{%i,%j}->%row = %i
$$->%label{%i,%j}->%col = %j
$$->%layout->$addWidget($$->%label{%i,%j},%i,%j)
}
}
$$->$newGame()
}
destructor()
{
}
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++
}
}
mineLabelPressed($0 = mine label object that has been pressed)
{
#echo "MINE LABEL PRESSED $0"
if($0->%bIsMine)
{
#echo "IS MINE!"
$0->$setFrameStyle(WinPanel,Sunken)
$0->$setText("*")
} else {
#echo "IS NOT MINE"
$$->$discoverCells($0)
}
}
discoverCells($0 = mine label that has to be discovered)
{
#echo "Discover cells $0"
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)})
}
}
}
alias(mines)
{
%m = $new(minesweeper)
%m->$show()
}
|