File: uminer.py

package info (click to toggle)
far2l 2.7.0~beta%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,304 kB
  • sloc: cpp: 263,566; ansic: 53,886; python: 7,048; sh: 1,516; perl: 410; javascript: 279; xml: 145; makefile: 31
file content (233 lines) | stat: -rw-r--r-- 7,387 bytes parent folder | download | duplicates (3)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import random
import logging
from far2l.plugin import PluginBase
from far2l.fardialogbuilder import (
    TEXT,
    BUTTON,
    USERCONTROL,
    HLine,
    HSizer,
    VSizer,
    DialogBuilder,
)


log = logging.getLogger(__name__)


class Miner:
    def __init__(self, rows, cols, bombs):
        self.score = 0
        self.rows = rows
        self.cols = cols
        self.bombs = bombs
        self.gamemap = [[0 for col in range(self.cols)] for row in range(self.rows)]
        self.usermap = [["-" for col in range(self.cols)] for row in range(self.rows)]
        self.GenerateMap()
        self.gameover = False
        self.gamewon = False

    @property
    def done(self):
        return self.gameover or self.gamewon

    def GenerateMap(self):
        mmap = self.gamemap
        rows = self.rows
        cols = self.cols

        def mark(y, x):
            if 0 <= y < rows and 0 <= x < cols:
                if mmap[y][x] != "X":
                    mmap[y][x] += 1

        for num in range(self.bombs):
            while True:
                y = random.randint(0, rows - 1)
                x = random.randint(0, cols - 1)
                if mmap[y][x] != "X":
                    break
            mmap[y][x] = "X"
            mark(y - 1, x - 1)
            mark(y - 1, x)
            mark(y - 1, x + 1)
            mark(y, x - 1)
            mark(y, x + 1)
            mark(y + 1, x - 1)
            mark(y + 1, x)
            mark(y + 1, x + 1)

    def CheckWon(self):
        bombs = self.bombs
        for row in self.usermap:
            for cell in row:
                if cell == "-":
                    bombs -= 1
        self.gamewon = bombs == 0

    def OpenMap(self, y, x):
        ch = self.gamemap[y][x]
        if ch == "X":
            self.gameover = True
            self.gamewon = False
            return
        pmap = self.usermap
        mmap = self.gamemap
        pmap[y][x] = mmap[y][x]
        self.score += 1
        checked = {}
        stack = [(y, x)]

        def mark(y, x):
            if 0 <= y < self.rows and 0 <= x < self.cols:
                if mmap[y][x] != "X" and pmap[y][x] != mmap[y][x]:
                    self.score += 1
                    pmap[y][x] = mmap[y][x]
                stack.append((y, x))

        while len(stack):
            y, x = stack.pop()
            if checked.get((y, x)) == True:
                continue
            checked[(y, x)] = True
            if mmap[y][x] != 0:
                continue
            pmap[y][x] = 0
            mark(y - 1, x - 1)
            mark(y - 1, x)
            mark(y - 1, x + 1)
            mark(y, x - 1)
            mark(y, x + 1)
            mark(y + 1, x - 1)
            mark(y + 1, x)
            mark(y + 1, x + 1)

        self.CheckWon()


class Plugin(PluginBase):
    label = "Python uminer"
    openFrom = ["PLUGINSMENU"]

    def Redraw(self, dlg, usermap=True):
        # dlg.EnableRedraw(False)
        mapa = self.game.usermap if usermap else self.game.gamemap
        for row in range(self.game.rows):
            for col in range(self.game.cols):
                did = getattr(dlg, "ID_{}_{}".format(row, col))
                ch = str(mapa[row][col])
                dlg.SetText(did, ch)
        dlg.SetText(dlg.ID_bombs, "Bombs: {:2} ".format(self.bombs))
        msg = "Score: {:2}".format(self.game.score)
        if self.game.gameover:
            msg += ", Game Over"
        elif self.game.gamewon:
            msg += ", Game Won"
        dlg.SetText(dlg.ID_status, msg)
        # dlg.EnableRedraw(True)
        # dlg.RedrawDialog()

    def OpenPlugin(self, OpenFrom):
        self.rows = 9
        self.cols = 16
        self.bombs = 10
        self.game = Miner(self.rows, self.cols, self.bombs)
        dlg = None

        def OnButton(hDlg, Msg, Param1, Param2):
            if Param1 == dlg.ID_vshow:
                self.game.gameover = True
                self.Redraw(dlg, False)
            elif Param1 == dlg.ID_vrestart:
                self.game = Miner(self.rows, self.cols, self.bombs)
                self.Redraw(dlg)
            elif Param1 == dlg.ID_vbplus:
                self.game.gameover = True
                if self.bombs < (self.rows * self.cols) // 2:
                    self.bombs += 1
                    self.Redraw(dlg, False)
            elif Param1 == dlg.ID_vbminus:
                self.game.gameover = True
                if self.bombs > 3:
                    self.bombs -= 1
                    self.Redraw(dlg, False)
            elif Param1 == dlg.ID_vcancel:
                return self.Close(dlg.ID_vcancel)
            else:
                return False
            return True

        def OnMouseClick(hDlg, Msg, Param1, Param2):
            idmin = getattr(dlg, "ID_{}_{}".format(0, 0))
            idmax = getattr(dlg, "ID_{}_{}".format(self.rows - 1, self.cols - 1))

            if Param1 < idmin or Param1 > idmax or self.game.done:
                return False
                # return OnButton(hDlg, Msg, Param1, Param2)

            no = Param1 - 1
            row, col = divmod(no, self.game.cols)
            self.game.OpenMap(row, col)
            if self.game.gameover:
                self.game.gamemap[row][col] = "O"
                self.Redraw(dlg, False)
            else:
                self.Redraw(dlg)
            return True

        @self.ffi.callback("FARWINDOWPROC")
        def DialogProc(hDlg, Msg, Param1, Param2):
            if Msg == self.ffic.DN_INITDIALOG:
                self.Redraw(dlg)
            elif Msg == self.ffic.DN_BTNCLICK:
                return OnButton(hDlg, Msg, Param1, Param2)
            elif Msg == self.ffic.DN_MOUSECLICK:
                if not self.game.done:
                    return OnMouseClick(hDlg, Msg, Param1, Param2)
            return self.info.DefDlgProc(hDlg, Msg, Param1, Param2)

        vs = []
        for row in range(self.game.rows):
            hs = []
            for col in range(self.game.cols):
                hs.append(TEXT("{}_{}".format(row, col), " "))
            vs.append(HSizer(*hs, border=(0, 0, 0, 0)))
        vs = VSizer(*vs)

        b = DialogBuilder(
            self,
            DialogProc,
            "Python Miner",
            "miner",
            0,
            VSizer(
                vs,
                HLine(),
                TEXT("status", " " * 25),
                HSizer(
                    BUTTON("vshow", "Show Me", flags=self.ffic.DIF_CENTERGROUP),
                    TEXT(
                        "bombs",
                        "Bombs: {:2} ".format(self.game.bombs),
                        flags=self.ffic.DIF_CENTERGROUP,
                    ),
                    BUTTON("vbplus", "+", flags=self.ffic.DIF_CENTERGROUP),
                    BUTTON("vbminus", "-", flags=self.ffic.DIF_CENTERGROUP),
                ),
                HSizer(
                    BUTTON("vrestart", "Again", flags=self.ffic.DIF_CENTERGROUP),
                    BUTTON("vcancel", "Cancel", flags=self.ffic.DIF_CENTERGROUP),
                ),
            ),
        )
        dlg = b.build(-1, -1)

        res = self.info.DialogRun(dlg.hDlg)
        if res == -1:
            msg = "esc"
        elif res == dlg.ID_vcancel:
            msg = "cancel"
        else:
            msg = "why ?"
        log.debug("rc={} msg={}".format(res, msg))
        self.info.DialogFree(dlg.hDlg)