% Utökat spelprogram
play(Pos):- terminal_won_for_us(Pos), !, write('I won.'), nl. play(Pos):- terminal_lost_for_us(Pos), !, write('I lost.'), nl. play(Pos1):- make_move(Pos1,Pos2), play(Pos2). make_move(Pos1,Pos2):- our_move(Pos1), !, find_move(Pos1,Pos2). make_move(Pos1,Pos2):- ask_move(Pos1,Pos2). find_move(Pos1,Pos2):- won_for_us(Pos1,Pos2),!. find_move(Pos1,Pos2):- default_move(Pos1,Pos2). won_for_us(Pos,_):- terminal_won_for_us(Pos). won_for_us(Pos,Pos1):- move(Pos,Pos1), \+ terminal_lost_for_us(Pos1), \+ (move(Pos1,Pos2), \+ won_for_us(Pos2,_)). % Här följer domänspecifika predikat för spelet NIM % där en position representeras av (Spelare,AntalStickor) % starta spelet med tex play((us,7)). Målet är att slippa plocka den sista stickan. % Man får ta en eller två stickor åt gången, (om man inte vill fuska!). terminal_won_for_us((us,0)). terminal_lost_for_us((them,0)). move((Player1,N1),(Player2,N2)):- opponent(Player1,Player2), decrease(N1,N2). opponent(us,them). opponent(them,us). decrease(N1,N2):- N1 > 0, N2 is N1-1. decrease(N1,N2):- N1 > 1, N2 is N1-2. our_move((us,N)). default_move((us,N1),(them,N2)):- N2 is N1-1. ask_move((them,N1),(us,N2)):- write('There are '), write(N1), write(' left.'), nl, write('How many do you want to take?'), read(N3), N2 is N1-N3.