CCP2410: Logic Programming | My Assignment Tutor

/*————————————————————————– CCP2410: Logic Programming Practical Title: Implementation of a two player game in Prolog Game: Deploy-Destroy – One Dimension Authors: Petros Kefalas and Ioanna Stamatopoulou ————————————————————————–*/ /*————————————————————————– Initial armies (lists of 1 to 8) ————————————————————————–*/ black_army([1,2,3,4,5,6,7,8]). white_army([1,2,3,4,5,6,7,8]). /*————————————————————————– Initial Board (all positions empty) ————————————————————————–*/ board([ (1,-,-),(2,-,-),(3,-,-),(4,-,-), (5,-,-),(6,-,-),(7,-,-),(8,-,-), (9,-,-),(10,-,-),(11,-,-),(12,-,-), (13,-,-),(14,-,-),(15,-,-),(16,-,-) ]). /*————————————————————————– game/0: The main predicate that starts the program. Finds the initial available armies and initial board and calls the two phases of the game, plus the winner announcement phase. ————————————————————————–*/ game:- black_army(BA), white_army(WA), board(B), deploy(BA,WA,B,InterB), destroy(InterB,FinalB), winner(FinalB). /*————————————————————————– deploy/4: +Black Army, +White Army, +Board, -Final Board after deploy phase ————————————————————————–*/ deploy([],[],B,B):-!. deploy(BA,WA,Board,FinalBoard):- deploy_computer(b,BA,Board,NewBA,TempBoard),!, pp(NewBA,WA,TempBoard),!, deploy_human(w,WA,TempBoard,NewWA,NewBoard),!, pp(NewBA,NewWA,NewBoard),!, deploy(NewBA,NewWA,NewBoard,FinalBoard). /*————————————————————————– deploy_computer/5: +Colour (normally b for computer), +Army for that colour +Current Board, -New available army after move, – New Board after move ————————————————————————–*/ % Ex.3 /*————————————————————————– deploy_human/5: +Colour (normally w for human), +Army for that colour +Current Board, -New available army after move, – New Board after move ————————————————————————–*/ % Ex.3 /*————————————————————————– destroy/2: +Current Board, -Final Board (where no one can destroy more armies) Destroy calls the computer and the human to destroy an army. If no one can destroy any more armies then the Final Board is returned. ————————————————————————–*/ destroy(Board,FinalBoard):- destroy_computer(b,Board,TempBoard),!, pp([],[],TempBoard), get0(_),!, destroy_human(w,TempBoard,NewBoard),!, pp([],[],NewBoard), get0(_),!, (NewBoard = Board -> destroy(NewBoard,FinalBoard); FinalBoard = Board). /*————————————————————————– destroy_human/3: +Colour (normally w for human), +Current Board -Board after one destroy move ————————————————————————–*/ % Ex.4 destroy_human(C,Board,NewBoard):- other_colour(C,OC), findall( (X,OC,N), (member((X,OC,N), Board), threaten_by(C,(X,OC,N),Board)), Options),!, writelist([C,’ can destroy ‘,Options,nl]),get0(_), (Options = [] -> ( select_a_piece_to_destroy_human(Options,(X,OC,Num)),!, writelist([C,’ selects: ‘,(X,OC,Num),nl]),get0(_), replace((X,-,-),Board,NewBoard),! ); ( writelist([C,’ cannot play anymore ‘,nl]),get0(_), NewBoard=Board) ),!. /*————————————————————————– destroy_computer/3: +Colour (normally b for computer), +Current Board -Board after one destroy move ————————————————————————–*/ % Ex.6 /*————————————————————————– select_a_piece_to_destroy_human/2: +Options available to destroy, -Selected number of the opponent to destroy After human input and the appropriate checks, a number is selected to be removed from the board during the current move. ————————————————————————–*/ select_a_piece_to_destroy_human(Options,(X,OC,N)):- repeat, writelist([‘Please select a number of the opponent to destroy :’,nl]), read(N), (member((X,OC,N),Options)-> ! ; ( writelist([N,’ cannot be destroyed. Try again!’,nl]), fail ) ). /*————————————————————————– select_a_piece/2: +Army available, -Selected number The predicate randomly selects an element out of the given list. Computer uses this predicate to randomly select (i) a number to be put on the board during a deploy phase move, or (ii) the position on which a number will be placed during a deploy phase move. ————————————————————————–*/ select_a_piece([N],N):-!. select_a_piece(Army,N):- length(Army,Pieces), random(1,Pieces,NthPiece), in_position(NthPiece,Army,N). /*————————————————————————– select_a_position/2: +Current Board, -Selected Position Computer randomly selects a position on the board. ————————————————————————–*/ select_a_position(Board,X):- findall(Free,member((Free,-,-),Board),Options), select_a_piece(Options,X). /*————————————————————————– winner/1: +Board It counts number of armies left in the Board and announces the winner (only “who has more armies than the other player left on the board”) ————————————————————————–*/ winner(B):- count(b,B,BC), count(w,B,WC), announce(BC,WC,B),!. count(_,[],0). count(C,[(_,C,_)|R],X):- count(C,R,Y),!, X is Y+1. count(C,[(_,_,_)|R],X):- count(C,R,X). /*————————————————————————– announce/3: announces the winner ————————————————————————–*/ announce(BC,WC,_):- BC > WC,!, Score is BC-WC, writelist([‘the winner is black by ‘,Score,nl]),!. announce(BC,WC,_):- BC < WC,!, Score is WC-BC, writelist([‘the winner is white by’,Score, nl]),!. % Ex. 10 Extending the winning rule % announce(C,C,B):- /*————————————————————————– next/2: +Coordinate, -Neighbouring Coordinate ————————————————————————–*/ % Ex.2 /*————————————————————————– other_colour/2: +Colour (b or w), -Colour (w or b) Returns the colour of the opponent. ————————————————————————–*/ other_colour(w,b). other_colour(b,w). /*———————————————————————— writelist/1: +List Prints all the elements of the input list ————————————————————————*/ writelist([]):-!. writelist([nl|R]):- nl,!, writelist(R). writelist([H|T]):- write(H),!, writelist(T). /*————————————————————————– sum/2: +List of Positions, -Sum of numbers in these positions ————————————————————————–*/ sum([],0). sum([(_,_,H)|T],R):- sum(T,L), R is L+H. /*————————————————————————– replace/3: +Move (X,C,N), +Current Board, -New Board Replaces the move that a player does thus updating the board. ————————————————————————–*/ replace((X,C,N),[(X,_,_)|Rest],[(X,C,N)|Rest]):-!. replace(T,[P|RestBoard],[P|NewBoard]):- replace(T,RestBoard,NewBoard). /*————————————————————————– in_position/3: +Nth, +List, -Selected Item From a list of items selects the one in the Nth position ————————————————————————–*/ in_position(1,[H|_],H):-!. in_position(N,[_|T],X):- N > 1, N1 is N-1, in_position(N1,T,X). /*————————————————————————– threaten_by/3: +Colour, +Position, +Board Checks whether Position has a number of the opponent which can be destroyed by Colour ————————————————————————–*/ % Ex.5 /*———————————————————————— cls/0: clears the screen ————————————————————————*/ cls :- put(27), put(“[“), put(“2”), put(“J”). /*————————————————————————– pp/3: +Black Army List, +White Army List, +Board ————————————————————————–*/ %Ex.1 pp(BA,WA,Board):- cls, writelist([‘black army: ‘, BA, nl]), writelist([‘white army: ‘, WA, nl]), nl,write(‘| 1| 2| 3| 4| 5| 6| 7| 8| 9|10|11|12|13|14|15|16|’), nl,write(‘+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+’),nl,write(‘|’), member(X,[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]), member((X,C,N),Board), display_army(C,N), fail. pp(_,_,_):- nl,write(‘+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+’),nl,!. % To diplay — as empty squares while all others normally display_army(-,-):-!, writelist([‘ ‘,’ ‘,’|’]),!. display_army(C,N):-!, writelist([C,N,’|’]),!.

QUALITY: 100% ORIGINAL PAPER – NO PLAGIARISM – CUSTOM PAPER

Leave a Reply

Your email address will not be published. Required fields are marked *