#! /usr/bin/pl -q -s /* CN-Röj, inspirerat av minröjningsspel i allehanda former. Skal från GraphViewer-demon i XPCE, därav texten nedan. Egentligen skrivet av Carl Nettelblad 2006, cnettel@stp.ling.uu.se. GPL-licensen gäller givetvis fortfarande. Heavily modified version of the graph viewer sample, which is what the text below describes. Part of XPCE --- The SWI-Prolog GUI toolkit Author: Jan Wielemaker and Anjo Anjewierden E-mail: jan@swi.psy.uva.nl Copyright (C): 1985-2002, University of Amsterdam This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA As a special exception, if you link this library with other files, compiled with a Free Software compiler, to produce an executable, this library does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License. */ :- module(minesweeper, [ minesweeper/0 ]). :- use_module(library(pce)). :- dynamic ismine/2. :- dynamic filled/2. :- dynamic filledcount/1. :- dynamic totsize/1. :- dynamic rowcount/1. :- dynamic ended/0. minesweeper :- new(GV, minesweeper), game_dialog(GV). height(16). width(16). :- pce_begin_class(minesweeper, frame). variable(timer, timer, get, "Timer."). variable(time, int, get, "Tid."). variable(filledcount, int, get, "Fyllda."). initialise(GV) :-> "Inled spel":: send(GV, send_super, initialise, 'CN-Röj'). free(GV) :-> send(GV, send_super, free), halt. /* Visa förberedande dialogruta med fråga om spelplanens egenskaper */ game_dialog(GV) :- new(D, dialog('Inställningar')), string_to_atom(WStr, "Bredd"), send(D, append, new(ColCount, text_item(WStr))), string_to_atom(HStr, "Höjd"), send(D, append, new(RowCount, text_item(HStr))), string_to_atom(MStr, "Minor"), send(D, append, new(Mines, text_item(MStr))), send(Mines, type, int), send(RowCount, type, int), send(ColCount, type, int), string_to_atom(PlStr, "Spela"), send(D, append, new(Play, button(play, message(GV, play, Mines?selection, RowCount?selection, ColCount?selection, D)))), send(Play, label, PlStr), send(D, default_button, play), send(D, open). play(GV, Mines, RowCount, ColCount, OldD) :-> "Starta spel med angivna inställningar, stäng inställningsdialogen":: send(OldD, free), retractall(ismine(_, _)), retractall(filled(_, _)), retractall(totsize(_)), retractall(ended), retractall(rowcount(_)), TotSize is (RowCount * ColCount) - Mines, assert(totsize(TotSize)), assert(rowcount(RowCount)), send(GV, append, new(D, dialog)), make_row(GV, D, 0, RowCount, ColCount), fill(Mines, RowCount, ColCount), height(H), width(W), string_to_atom(S, "Spela!"), new(Status, text(S)), send(Status, name, status), send(D, append, Status), Y is (H + 1) * RowCount, send(Status, auto_align, off), send(D, display, Status, point(0, Y)), string_to_atom(Sc, "0"), new(Score, text(Sc, right)), send(Score, name, score), send(D, append, Score), X is W * ColCount, send(Score, auto_align, off), send(D, display, Score, point(X, Y)), send(GV, slot, timer, new(T, timer(1, message(GV, newtime, D)))), send(GV, slot, time, 0), send(GV, slot, filledcount, 0), send(T, start), send(GV, open). /* Räkna antal minor på viss ruta, 1 eller 0, A in, B ut */ countmine(X, Y, A, B) :- X2 is X, Y2 is Y, (ismine(X2, Y2) *-> B is A + 1; B is A). /* Räkna antal grannar med minor till angiven koordinat */ countneighbors(X, Y, N) :- countmine(X-1, Y-1, 0, A), countmine(X, Y-1, A, B), countmine(X+1, Y-1, B, C), countmine(X-1, Y, C, D), countmine(X+1, Y, D, E), countmine(X-1, Y+1, E, F), countmine(X, Y+1, F, G), countmine(X+1, Y+1, G, N). /* Basfall för rekursion som sätter ut minor */ fill(0, _, _). /* Sätt ut minor inom angiven spelplan, undvik att sätta dubbelt */ fill(Mines, RowCount, ColCount) :- X is random(ColCount), Y is random(RowCount), ( ismine(X, Y) *-> Mines2 is Mines ; (Mines2 is Mines - 1, assert(ismine(X, Y)))), fill(Mines2, RowCount, ColCount). /* Rekursionsslut */ make_row(_, _, X, X, _). /* Skapa spelplan */ make_row(GV, D, RowNo, RowCount, ColCount) :- make_cell(GV, D, RowNo, 0, ColCount), NewRowNo is RowNo + 1, make_row(GV, D, NewRowNo, RowCount, ColCount). /* Basfall */ make_cell(_, _, _, X, X). /* Skapa cell och högerliggande celler */ make_cell(GV, D, RowNo, ColNo, ColCount) :- NewColNo is ColNo + 1, height(H), width(W), new(B, box(W, H)), atom_codes(Name, [RowNo, ColNo]), send(B, name, Name), send(D, append, B), Y is H * RowNo, X is W * ColNo, send(B, size, size(W, H)), send(B, auto_align, off), new(Handler, handler_group( new(click_gesture(right, '', single, message(GV, rightclick, D, ColNo, RowNo))), new(click_gesture(left, '', single, message(GV, click, D, ColNo, RowNo))))), send(B, recogniser, Handler), send(D, display, B, point(X, Y)), make_cell(GV, D, RowNo, NewColNo, ColCount). click(GV, D, ColNo, RowNo) :-> "Användaren har klickat i en cell.":: \+ ended, (ismine(ColNo, RowNo) *-> endtext(GV, D, "Förlust!") ; ( floodfill(GV, D, ColNo, RowNo), !, get(GV, filledcount, N), totsize(N), endtext(GV, D, "Seger!"))). rightclick(_GV, D, ColNo, RowNo) :-> "Användaren har högerklickat i en cell":: \+ ended, (filled(ColNo, RowNo) *-> unmark(D, ColNo, RowNo) ; mark(D, ColNo, RowNo)). /* Markera förmodad minförekomst i en cell */ mark(Frame, ColNo, RowNo) :- ( atom_codes(Name, [RowNo, ColNo]), get(Frame, member, Name, B), !, assert(filled(ColNo, RowNo)), string_to_atom(Text, "*"), atom_codes(Name2, [RowNo, ColNo, 1]), new(T, text(Text, center)), send(T, name, Name2), send(Frame, display, T), send(T, center, B?center)) ; true. /* Avmarkera förmodad minförekomst i en cell */ unmark(Frame, ColNo, RowNo) :- ( atom_codes(Name, [RowNo, ColNo, 1]), get(Frame, member, Name, T), send(Frame, delete, T), retract(filled(ColNo, RowNo)) ) ; true. /* Räkna tid */ newtime(GV, D) :-> get(GV, time, Time), Time2 is Time + 1, send(GV, slot, time, Time2), string_to_atom(TimeStr, Time2), get(D, member, score, Score), send(Score, string, TimeStr). /* Avsluta spelet, visa verkliga minförekomster, stoppa klockan */ endtext(GV, Frame, Text) :- assert(ended), get(Frame, member, status, Status), string_to_atom(String, Text), send(Status, string, String), send(GV?timer, stop), !, ((filled(ColNo, RowNo), unmark(Frame, ColNo, RowNo), fail) ; true), !, (ismine(ColNo, RowNo), mark(Frame, ColNo, RowNo), fail) ; true. /* Fyll runt en cell, spridning vid nollor är poängen */ floodfill(GV, Frame, ColNo2, RowNo2) :- ColNo is ColNo2, RowNo is RowNo2, ( (filled(ColNo, RowNo); ColNo < 0; RowNo < 0) ; ( atom_codes(Name, [RowNo, ColNo]), get(Frame, member, Name, B), \+ ismine(ColNo, RowNo), !, assert(filled(ColNo, RowNo)), get(GV, filledcount, Count), Count2 is Count + 1, send(GV, slot, filledcount, Count2), countneighbors(ColNo, RowNo, N), string_to_atom(Text, N), new(T, text(Text, center)), send(Frame, display, T), send(T, center, B?center), !, ( N == 0 *-> ( floodfill(GV, Frame, ColNo, RowNo - 1), floodfill(GV, Frame, ColNo - 1, RowNo - 1), floodfill(GV, Frame, ColNo + 1, RowNo - 1), floodfill(GV, Frame, ColNo - 1, RowNo), floodfill(GV, Frame, ColNo + 1, RowNo), floodfill(GV, Frame, ColNo, RowNo + 1), floodfill(GV, Frame, ColNo - 1, RowNo + 1), floodfill(GV, Frame, ColNo + 1, RowNo + 1)) ; true))) ; true. :- pce_end_class. :- minesweeper.