// FOR THE MENU USE CURSOR KEYS + ENTER // FOR THE GAME ITSELF USE THE MOUSE // Updated 06,08,2005 // // Putbit Graphic Converter by Hermang H. Mansilla Mattos ini_check() grafix() gosub get_level do gosub init give_score(score) first_click = true // wait until the first click to start time check repeat smiley(happy) repeat key$ = inkey$ until(mouseb(key$) = 1 or mouseb(key$) = 2) if first_click = true then start_time = val(mid$(time$, 10)) first_click = false end if smiley(worried) screenx = mousex(key$) screeny = mousey(key$) x = screen_grid(screenx) y = screen_grid(screeny) if mouseb(key$) = 1 then if is_inside(screenx, screeny) = true then if scr$(x, y) <> "f" then event$ = left_click$(x, y) end if end if end if if mouseb(key$) = 2 then if is_inside(screenx, screeny) = true then event$ = right_click$(x, y) end if end if give_score(score) squares_left = empty_count() mistake = misplaced_flag() smiley(happy) until(left$(event$, 8) = "kablammo" or (squares_left = 0 and mistake = false)) finish_time = val(mid$(time$, 10)) time = finish_time - start_time if current_level <> 999 and best_time > time and left$(event$, 8) <> "kablammo" then set_new_record() end if // end game routines if left$(event$, 8) = "kablammo" then wrong() elsif best_time <= time correct() end if poke("textalign"), "lt" text 0, grid_screen(height) + 135, "Press any key to continue..." repeat key$ = inkey$ until(mouseb(key$) = 1) loop end // initialization routines label init // //Load in best times // dim level(2) open #1, "minesweeper.ini", "r" for n = 0 to 2 input #1 level(n) next n close #1 if current_level = 999 then best_time = - 1 else best_time = level(current_level) end if res = 20 // If the grid is too small experiment with this. // The larger the number, the larger the square. flag = 9 // Icons bomb = 10 quest = 11 blank = 12 empty = 13 cross = 14 happy = 15 // Smileys cool = 16 worried = 17 sad = 18 first = true dim grid$(width + 1, height + 1) dim scr$(width + 1, height + 1) dim store$(width + 1, height + 1) clear_arrays() placebombs(width, height, bombs) draw_screen(width, height) fill_numbers(width, height) store_grid() score = bombs // return // sub grafix() local icon, pixel_total local names$, oldbits$, rgbb$, rgbf$ dim graf$(18) restore graphicdata print "Converting graphic:" for icon = 1 to 18 read names$ print names$ read oldbits$, rgbf$, rgbb$ pixel_total = calc_bitmap_size(oldbits$) graf$(icon) = ConvertBit$(oldbits$, rgbf$, rgbb$, pixel_total) next icon end sub // sub ini_check() local stream print "Ini check - "; stream = open("minesweeper.ini", "r") if stream = 0 then stream = 1 print "Failed, creating from new." open #stream, "minesweeper.ini", "w" print #stream, 999999 print #stream, 999999 print #stream, 999999 else print "Ok." end if close #stream end sub // sub ConvertBit$(oldbm$, fg$, bg$, pixel_total) local i, lb, pixel, pos local b$, h$, newbm$ newbm$ = "rgb " pos = instr(oldbm$, ":") newbm$ = newbm$ + mid$(oldbm$, 1, pos) for i = pos + 1 to len(oldbm$) h$ = mid$(oldbm$, i, 1) b$ = bin$( dec( h$ ) ) lb = len(b$) if lb = 3 b$ = "0" + b$ if lb = 2 b$ = "00" + b$ if lb = 1 b$ = "000" + b$ for j = 1 to 4 pixel = pixel + 1 if pixel > pixel_total break if mid$(b$, 5 - j, 1) = "1" then newbm$ = newbm$ + fg$ else newbm$ = newbm$ + bg$ end if next j next i return newbm$ end sub // sub calc_bitmap_size(o$) local num, pixels, x, y local v$(1) num = token(o$, v$(), ":") num = token(v$(1), v$(), ",") x = val(v$(1)) y = val(v$(2)) pixels = x * y return pixels end sub // sub placebombs(w, h, b) local n, rx, ry for n = 1 to b repeat rx = int(ran(w + 1)) ry = int(ran(h + 1)) until(grid$(rx, ry) = "" and rx > 0 and ry > 0 and rx <= w and ry <= h) grid$(rx, ry) = "x" next n return end sub // sub draw_screen(x, y) local n static window_open if window_open = 0 then open window res * x + res, (res * y) + res + 125 window_open = 1 end if clear window for n = 10 to y * res + 10 step res line 10, n, x * res + 10, n next n for n = 10 to x * res + 10 step res line n, 10, n, y * res + 10 next n return end sub // sub fill_numbers(w, h) local bombs, tx1, tx2, ty1, ty2, x, y for y = 1 to h for x = 1 to w if grid$(x, y) = "" then tx1 = x - 1 ty1 = y - 1 tx2 = x + 1 ty2 = y + 1 if tx1 = 0 tx1 = 1 if ty1 = 0 ty1 = 1 if tx2 = w + 1 tx2 = w if ty2 = h + 1 ty2 = h bombs = bombs(tx1, ty1, tx2, ty2) if bombs <> 0 grid$(x, y) = str$(bombs) end if next x next y return end sub // sub bombs(x1, y1, x2, y2) local bombs, x, y for y = y1 to y2 for x = x1 to x2 if grid$(x, y) = "x" bombs = bombs + 1 next x next y return bombs end sub // // end of initialization routines // // convertion routines // sub screen_grid(x) return int((x - 10) / res) + 1 end sub // sub grid_screen(x) return x * res - ((res / 2) - 2) end sub // sub find_icon$(x, y) local n, value local pix$ pix$ = getbit$(grid_screen(x), grid_screen(y), grid_screen(x) + 16, grid_screen(y) + 16) for n = 1 to 13 if pix$ = graf$(n) then value = n break end if next n if value >= 1 and value <= 8 return str$(value) if value = flag return "f" if value = bomb return "x" if value = quest return "?" if value = blank return "b" if value = empty return "e" return end sub // // end of conversion routines // // left click routines // sub left_click$(x, y) if grid$(x, y) = "c" then if scr$(x, y) = "" then floodfill(x, y, blank) return end if if scr$(x, y) = str$(count(x, y, flag)) then if mistakes(x, y) = 0 then floodfill(x, y, val(scr$(x, y))) return else return "kablammo" end if end if show_squares(x, y) return end if // if grid$(x, y) = "x" return "kablammo_left_click" // if val(grid$(x, y)) <> 0 then putbit graf$(val(grid$(x, y))), grid_screen(x), grid_screen(y) scr$(x, y) = grid$(x, y) grid$(x, y) = "c" return end if // if grid$(x, y) = "" then floodfill(x, y, blank) end if // return end sub // sub count(x, y, item) local count, m, n, scrx, scry, tx1, tx3, ty1, ty2 tx1 = x - 1 ty1 = y - 1 tx2 = x + 1 ty2 = y + 1 if tx1 = 0 tx1 = 1 if ty1 = 0 ty1 = 1 if tx2 > width tx2 = width if ty2 > height ty2 = height count = 0 for n = ty1 to ty2 for m = tx1 to tx2 scrx = grid_screen(m) scry = grid_screen(n) pix$ = getbit$(scrx, scry, scrx + 16, scry + 16) if pix$ = graf$(item) then count = count + 1 end if next m next n return count end sub // sub mistakes(x, y) local count, m, n, tx1, tx3, ty1, ty2 tx1 = x - 1 ty1 = y - 1 tx2 = x + 1 ty2 = y + 1 if tx1 = 0 tx1 = 1 if ty1 = 0 ty1 = 1 if tx2 > width tx2 = width if ty2 > height ty2 = height count = 0 for n = ty1 to ty2 for m = tx1 to tx2 if scr$(m, n) = "f" and grid$(m, n) <> "x" then putbit graf$(cross), grid_screen(m), grid_screen(n) count = count + 1 end if if grid$(m, n) = "x" and scr$(m, n) <> "f" then putbit graf$(bomb), grid_screen(m), grid_screen(n) count = count + 1 end if next m next n // return count end sub // sub show_squares(x, y) local count, m, n, x1, x3, y1, y2 local key$ local co(9, 2) x1 = x - 1 y1 = y - 1 x2 = x + 1 y2 = y + 1 if x1 = 0 x1 = 1 if y1 = 0 y1 = 1 if x2 > width x2 = width if y2 > height y2 = height count = 1 for n = y1 to y2 for m = x1 to x2 if grid$(m, n) <> "c" and scr$(m, n) = "" then co(count, 1) = m co(count, 2) = n count = count + 1 end if next m next n // for n = 1 to count - 1 putbit graf$(blank), grid_screen(co(n, 1)), grid_screen(co(n, 2)) next n // repeat key$ = inkey$ until(mouseb(key$) = - 1) // for n = 1 to count - 1 putbit graf$(empty), grid_screen(co(n, 1)), grid_screen(co(n, 2)) next n return end sub // sub floodfill(x, y, graf) if x < 1 or y < 1 return if x > width or y > height return if grid$(x, y) = "" graf = blank if scr$(x, y) = "f" then putbit graf$(flag), grid_screen(x), grid_screen(y) else putbit graf$(graf), grid_screen(x), grid_screen(y) end if grid$(x, y) = "c" scr$(x, y) = find_icon$(x, y) check_numbers(x, y) if grid$(x, y - 1) = "" floodfill(x, y - 1, graf) // North if grid$(x - 1, y) = "" floodfill(x - 1, y, graf) // West if grid$(x + 1, y) = "" floodfill(x + 1, y, graf) // East if grid$(x, y + 1) = "" floodfill(x, y + 1, graf) // South if grid$(x - 1, y - 1) = "" floodfill(x - 1, y - 1, graf) // Nw if grid$(x + 1, y - 1) = "" floodfill(x + 1, y - 1, graf) // Ne if grid$(x - 1, y + 1) = "" floodfill(x - 1, y + 1, graf) // Sw if grid$(x + 1, y + 1) = "" floodfill(x + 1, y + 1, graf) // Se return end sub // sub check_numbers(x, y) local m, n for n = x - 1 to x + 1 for m = y - 1 to y + 1 if scr$(n, m) = "f" putbit graf$(flag), grid_screen(n), grid_screen(m) if val(grid$(n, m)) <> 0 and scr$(n, m) <> "f" then putbit graf$(val(grid$(n, m))), grid_screen(n), grid_screen(m) scr$(n, m) = grid$(n, m) grid$(n, m) = "c" end if next m next n return end sub // // end of left click routines // // right click routine // sub right_click$(x, y) local scrx, scry local icon$ if grid$(x, y) = "c" and (scr$(x, y) <> "f" and scr$(x, y) <> "?" and scr$(x, y) <> "") return // icon$ = scr$(x, y) scrx = grid_screen(x) scry = grid_screen(y) // if icon$ = "" then putbit graf$(flag), scrx, scry scr$(x, y) = "f" score = score - 1 return end if // if icon$ = "f" then putbit graf$(quest), scrx, scry scr$(x, y) = "?" score = score + 1 return end if // if icon$ = "?" then putbit graf$(empty), scrx, scry scr$(x, y) = "" return end if return end sub // // misc // sub give_score(s) local x, y local score$ static tempscore if tempscore = s and first = false return first = false tempscore = s // score$ = str$(s) if len(score$) = 1 score$ = "0" + score$ x = 1 y = grid_screen(height) + res clear_score(x, y) printscore(left$(score$, 1), x, y) x = 56 clear_score(x, y) printscore(right$(score$, 1), x, y) return end sub // sub printscore(s$, x, y) // if s$ = "0" or s$ = "4" or s$ = "5" or s$ = "6" or s$ = "7" or s$ = "8" or s$ = "9" then fill rectangle x, y, x + 6, y + 40 //nw end if if s$ = "0" or s$ = "2" or s$ = "6" or s$ = "8" then fill rectangle x, y + 44, x + 6, y + 84 // sw end if if s$ = "0" or s$ = "2" or s$ = "3" or s$ = "5" or s$ = "6" or s$ = "7" or s$ = "8" or s$ = "9" then fill rectangle x + 10, y, x + 40, y + 6 // n end if if s$ = "2" or s$ = "3" or s$ = "4" or s$ = "5" or s$ = "6" or s$ = "8" or s$ = "9" or s$ = "-" then fill rectangle x + 10, y + 39, x + 40, y + 46 // middle end if // if s$ = "0" or s$ = "2" or s$ = "3" or s$ = "5" or s$ = "6" or s$ = "8" or s$ = "9" then fill rectangle x + 10, y + 78, x + 40, y + 84 // s end if if s$ = "0" or s$ = "1" or s$ = "2" or s$ = "3" or s$ = "4" or s$ = "7" or s$ = "8" or s$ = "9" then fill rectangle x + 44, y, x + 50, y + 40 // ne end if if s$ = "0" or s$ = "1" or s$ = "3" or s$ = "4" or s$ = "5" or s$ = "6" or s$ = "7" or s$ = "8" or s$ = "9" then fill rectangle x + 44, y + 44, x + 50, y + 84 // se end if return end sub // sub empty_count() local count, m, n for n = 1 to height for m = 1 to width if scr$(m, n) = "" count = count + 1 next m next n return count end sub // sub is_inside(x, y) local scrh, scrw scrw = grid_screen(width) + res scrh = grid_screen(height) + res if x >= scrw or y >= scrh or x < 10 or y < 10 return false return true end sub // // sub store_grid() local m, n for n = 1 to height for m = 1 to width store$(m, n) = grid$(m, n) next m next n return end sub // sub clear_arrays() local m, n for n = 0 to height + 1 for m = 0 to width + 1 grid$(m, n) = "" scr$(m, n) = "" next m next n return end sub // sub clear_score(x, y) clear fill rectangle x, y, x + 6, y + 40 clear fill rectangle x, y + 44, x + 6, y + 84 clear fill rectangle x + 10, y, x + 40, y + 6 clear fill rectangle x + 10, y + 39, x + 40, y + 46 clear fill rectangle x + 10, y + 78, x + 40, y + 84 clear fill rectangle x + 44, y, x + 50, y + 40 clear fill rectangle x + 44, y + 44, x + 50, y + 84 return end sub // label get_level gosub setupscreen pix$ = getbit$(x, y, x + 35, y + 35) text x, y, " < " repeat repeat key$ = inkey$(0) randomize = ran(1) until(key$ = "up" or key$ = "down" or key$ = "enter") putbit pix$, x, y ty = y if key$ = "down" y = y + 20 if key$ = "up" y = y - 20 if y < 88 y = 148 if y > 148 y = 88 pix$ = getbit$(x, y, x + 35, y + 35) text x, y, " < " // clear fill box x, ty, x + 20, ty + 20 // text 150, 88, "1: Easy" // text 150, 108, "2: Intermediate" // text 150, 128, "3: Expert" // text 150, 148, "4: Custom" // until(key$ = "enter") if y = 148 then putbit pix$, x, y // clear fill box 150, 190, 250, 210 width = text_input("Width=", 150, 190) // clear fill box 150, 190, 250, 210 height = text_input("Height=", 150, 190) // clear fill box 150, 190, 250, 210 bombs = text_input("Bombs=", 150, 190) current_level = 999 end if text 0,0,"","swiss" close window if y = 128 then width = 30 height = 16 bombs = 99 current_level = 2 end if if y = 108 then width = 16 height = 16 bombs = 40 current_level = 1 end if if y = 88 then width = 8 height = 8 bombs = 10 current_level = 0 end if // return end // // label setupscreen x = 250 y = 88 clear screen poke("textalign"), "lt" open window 383, 256, "swiss10" backcolour 255, 255, 0 clear window cover$ = getbit$(130, 130, 270, 270) // clear fill box 130, 130, 270, 270 text 1, 88, "1: Easy", "modern30" text 1, 108, "2: Intermediate", "modern30" text 1, 128, "3: Expert", "modern30" text 1, 148, "4: Custom", "modern30" putbitmap() colour 0, 0, 0 // return // sub putbitmap() local a, b, c, g, ig, lb, lg, lr, r, scrx, scry, x, y,stream local pix$ stream=open("minesweeper.bmp", "rb") if stream=0 return scrx = dword(18) scry = dword(22) seek stream, 54, "begin" x = 0 y = scry while( ! eof(stream)) lr = r lg = g lb = b b = peek(stream) g = peek(stream) r = peek(stream) pix$ = getbit$(x, y, x, y) if mid$(pix$, 9) <> "000000" then colour r, g, b dot x, y else colour 255, 255, 0 dot x, y end if x = x + 1 c = c + 3 if x = scrx then x = 0 y = y - 1 while(c / 4 <> int(c / 4)) // Nicked from ant :) ig = peek(1) c = c + 1 wend c = 0 end if a = a + 1 wend close #stream end sub // sub dword(pos) local n, v local b$ seek 1, pos, "begin" for n = 1 to 4 v = peek(1) b$ = pad$(v) + b$ next n return dec(b$, 2) end sub // sub pad$(v) local m$, v$ m$ = "00000000" v$ = bin$(v) right$(m$, len(v$)) = v$ return m$ end sub // sub text_input(t$, x, y) local inp$, key$ text x, y, t$ x = x + (len(t$) * 8.2) repeat key$ = inkey$ if key$ <= "9" and key$ >= "0" then inp$ = inp$ + key$ text x, y, inp$ end if until(key$ = "enter") return val(inp$) end sub // sub smiley(emotion) local x, y x = 110 y = grid_screen(height) + (res * 1.5) // clear fill box x,y,x+50,y+50 putbit graf$(emotion), x, y return end sub // // error checking routine // sub misplaced_flag() local m, n // for m = 1 to height for n = 1 to width if scr$(n, m) = "f" and grid$(n, m) <> "x" then return true end if if scr$(n, m) = "?" return true if scr$(n, m) = "" return true next n next m return false end sub // // End routines // // wrong // sub wrong() local m, n, tx, ty smiley(sad) if event$ = "kablammo_left_click" then tx = grid_screen(x) ty = grid_screen(y) for n = 1 to height for m = 1 to width if store$(m, n) = "x" and scr$(m, n) <> "f" putbit graf$(bomb), grid_screen(m), grid_screen(n) if scr$(m, n) = "f" and store$(m, n) <> "x" putbit graf$(cross), grid_screen(m), grid_screen(n) if val(store$(m, n)) <> 0 and val(scr$(m, n)) <> 0 putbit graf$(val(scr$(m, n))), grid_screen(m), grid_screen(n) next m next n end if // if event$ = "kablammo" then for n = 1 to height for m = 1 to width if scr$(m, n) = "f" and grid$(m, n) <> "x" putbit graf$(cross), grid_screen(m), grid_screen(n) if scr$(m, n) <> "f" and grid$(m, n) = "x" putbit graf$(bomb), grid_screen(m), grid_screen(n) next m next n end if return end sub // sub correct() local text$ // smiley(cool) text$ = str$(finish_time - start_time) text 0, grid_screen(height) + 115, "Time taken=" + text$ + " seconds." end sub // //New record time // sub set_new_record() local dx, dy, tmpx, tmpy, x, y local txt$ beep smiley(cool) open #1, "minesweeper.ini", "w" level(current_level) = time for n = 0 to 2 print #1, level(n) next n close #1 txt$ = "New record! " + str$(time) + " seconds!" text x, y, txt$, "lt" end sub // label graphicdata // 1 data "1" data "17,17:ffffffffffffffffff7fff7eff7dfffbfff7ffffefffdfff0effffffffffffffffffffff1", "000000", "ffffff" // 2 data "2" data "17,17:ffffffffffffffffff00ffdfefffdfffbff00ffdfffbfdf708ffffffffffffffffffffff1", "000000", "ffffff" // 3 data "3" data "17,17:ffffffffffffffffff00ffffefffdff18fff7ffffefffdf708ffffffffffffffffffffff1", "000000", "ffffff" // 4 data "4" data "17,17:fffffffffffffffffff9fff5fffdeffddff10fff7ffffefffdffffffffffffffffffffff1", "000000", "ffffff" // 5 data "5" data "17,17:ffffffffffffffffff00ffdfffbfff708fff7ffffefffdf708ffffffffffffffffffffff1", "000000", "ffffff" // 6 data "6" data "17,17:ffffffffffffffffff00ffdfffbfff708ffe7ffdfefbfdf708ffffffffffffffffffffff1", "000000", "ffffff" // 7 data "7" data "17,17:ffffffffffffffffff00ffffefffefffefffefffefffefffefffffffffffffffffffffff1", "000000", "ffffff" // 8 data "8" data "17,17:ffffffffffffffffff00ffdfefbfdf708ffe7ffdfefbfdf708ffffffffffffffffffffff1", "000000", "ffffff" // flag data "flag" data "17,17:0000cfffb0003d1e62c3c48789e0731006efff0008100030006000c000810003000f00000", "ff0000", "ffff00" // bomb data "bomb" data "17,17:0000000020080008b0e9d07f533e342f726ff15efb8ff74fff0cff08ff10ef100f0000000", "000000", "ffff00" // question mark data "question mark" data "17,17:ffffffffffffffffff00ffdfefffeff7eff7ffffefffffffbfffffffffffffffffffffff1", "0000ff", "ff0000" // blank data "blank" data "17,17:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1", "000000", "000000" // empty data "empty" data "17,17:0000000000000000000000000000000000000000000000000000000000000000000000000", "ffff00", "ffff00" // cross data "cross" data "17,17:0000000003081e08383830e8308b300e30083008f008b3083e08383830e03081000000000", "ff0000", "000050" // // emoticons // // happy data "happy" data "51,51:00000ff3000000008700e1000000030000300000060000060000080000004000003000000c000040000000800001000000080004000000008000100000000800800000000040020000000004080000000000404000000000020100e100e10028008f108f1001200cf00cf0001100e700e70088000f300f30044000f000f0002100000000000a00000000000050000000000082000000000004100000000000a00000000000050000000000082000000000004100000000000a0000000000009000000000004400000000000220000000000011000000000080100800040002800040002000180004008000404000200400020400020010080040006060002002000cf000010020000000040002000000001000200000004000060000008100004000000200000c00000c00000081000810000000f00c3000000008ff1000000000000000000", "000000", "ffff00" // cool data "cool" data "51,51:00000ff3000000008700e1000000030000300000060000060000080000004000003000000c00004000000080000100000008000400000000800010000000080080000000004002000000000408000000000040400000000002010eff1eff302808fff0fff20120aff78ff720118cffffff328802eff1eff124480fff0fff022120ff30ff302a800ff00ff0025200f300f300aa000000000006300000000000a00000000000070000000000082000000000004100000000000a0000000000009000000000004400000000000220000000000011000000000080100800040002800040002000180004008000404000200400020400020010080040006060002002000cf000010020000000040002000000001000200000004000060000008100004000000200000c00000c00000081000810000000f00c3000000008ff1000000000000000000", "000000", "ffff00" // worried data "worried" data "51,51:00000ff3000000008700e1000000030000300000060000060000080000004000003000000c00004000000080000100000008000400000000800010000000080080000000004002000000000408000000000040400c300c30020100f300f3002800cf30cf3001200ef10ef1001100ff00ff0088008f708f70044008f108f1002100870087000a00000000000050000000000082000000000004100000000000a00000000000050000000000082000000000004100000000000a00000c300000900000120000440000402000022000020100001100001800008010008040000280000801000018000087000040400000000002040000000008004000000000200200000000010020000000040002000000001000200000004000060000008100004000000200000c00000c00000081000810000000f00c3000000008ff1000000000000000000", "000000", "ffff00" // sad data "sad" data "51,51:00000ff3000000008700e1000000030000300000060000060000080000004000003000000c00004000000080000100000008000400000000800010000000080080000000004002000000000408000000000040400000000002010080008000280004000400012000200020001100ef00ef00880008000800044000400040002100020002000a00000000000050000000000082000000000004100000000000a00000000000050000000000082000000000004100000000000a0000000000009000000000004400000000000220000cf000001100081810008010002001000280008000100018000400800040400010080002040000000008004000000000200200000000010020000000040002000000001000200000004000060000008100004000000200000c00000c00000081000810000000f00c3000000008ff1000000000000000000", "000000", "ffff00" //