tictactoe.pl

loading
details
attribute value
description
owner Johannes Waldmann
uploaded 2017-08-17 03:45:08.0
disk size 9.02 KB
downloadable true
type
attribute value
name no_type
processor id 1
description this is the default benchmark type for rejected benchmarks and benchmarks that are not associated with a type.
owning community none
loading contents
%query: play(i,i,o).

%% From: "(Arvind Kumar Bansal)" <arvind@mcs.kent.edu>
%% 
%% Here is a program of tic tac toe which one one my student did for
%% a course assignment in Logic Programming. It uses Alpha Beta 
%% pruning for 3 X 3 board.
%% 
%% -Arvind
%% Mathematics and Computer Science
%% Kent State University
%% Kent, OH 44242

:- entry(play(Game),[ground(Game)]).
:- trust(player(Player),[],[ground(Player)]).
:- trust(abolish0(Player),[],[ground(Player)]).

play(Game) :-
	initialize(Game, Position, Player),
	display_game(Position),
	play(Position, Player, Result).

play(Position, Player, Result) :-
	game_over(Position, Player, Result), !,
	write(Result),
	abolish0(player).
play(Position, Player, Result) :-
	choose(Position, Player, Move),
	move(Move, Player, Position, Position1),
	display_game(Position1),
	next_player(Player, Player1), !,
	play(Position1, Player1, Result).

choose(Position, computer, Move) :-
	write('Computer is thinking...'), nl,
	alpha_beta(computer, Position, -1, 1, Move, Value).
choose(Position, Player, Move) :-
	write(Player),
	write(', enter move (followed by a period and return) --> '),
	read(Move1),
	legal(Position, Player, Move1, Move).

alpha_beta(Player, Position, Alpha, Beta, Move, Value) :-
	Player == computer,
	game_win_o(Position),
	Value is 1.
alpha_beta(Player, Position, Alpha, Beta, Move, Value) :-
	player(Player),
	game_win_x(Position),
	Value is 1.
alpha_beta(Player, Position, Alpha, Beta, Move, Value) :-
	game_tie(Position),
	Value is 0.
alpha_beta(Player, Position, Alpha, Beta, Move, Value) :-
	Player == computer,
	game_win_x(Position),
	Value is -1.
alpha_beta(Player, Position, Alpha, Beta, Move, Value) :-
	player(Player),
	game_win_o(Position),
	Value is -1.
alpha_beta(Player, Position, Alpha, Beta, Move, Value) :-
	setof(Move1, move(Position, Move1), Moves),
	Alpha1 is -Beta,
	Beta1 is -Alpha,
        evaluate(Player, Moves, Position, Alpha1, Beta1, nil, (Move, Value)).

evaluate(Player, [Move | Moves], Position, Alpha, Beta, Move1, Best):-
	move(Move, Player, Position, Position1),
	next_player(Player, Player1),
	alpha_beta(Player1, Position1, Alpha, Beta, MoveX, Value),
	Value1 is -Value,
	cutoff(Player, Move, Value1, Alpha, Beta, Moves, Position, Move1, Best),
	!.
evaluate(Player, [], Position, Alpha, Beta, Move, (Move, Alpha)).

cutoff(Player, Move, Value, Alpha, Beta, Moves, Position, Move1, (Move,Value)):-
	Value >= Beta, !.
cutoff(Player, Move, Value, Alpha, Beta, Moves, Position, Move1, Best) :-
	Alpha < Value,
	Value < Beta, !,
	evaluate(Player, Moves, Position, Value, Beta, Move, Best).
cutoff(Player, Move, Value, Alpha, Beta, Moves, Position, Move1, Best) :-
	Value =< Alpha, !,
	evaluate(Player, Moves, Position, Alpha, Beta, Move1, Best).

initialize(tictactoe, ((' ',' ',' '), (' ',' ',' '), (' ',' ',' ')), Player) :-
	write('Enter player`s name (followed by a period and return) --> '),
	read(Player),
	write(Player),
	write(', you will go first and use the symbol "x".'), nl,
	assert(player(Player)).

display_game(((R1C1, R1C2, R1C3), (R2C1, R2C2, R2C3), (R3C1, R3C2, R3C3))) :-
	nl,
	write(' '), write(R1C1), write(' '), write('|'),
	write(' '), write(R1C2), write(' '), write('|'),
	write(' '), write(R1C3), write(' '), tab(3),
	write('u-l = upper left,  u-c = upper center,  u-r = upper right'), nl,
	write(' --+---+-- '), nl,
popout

content may be truncated. 'popout' for larger text window.

actions get anonymous link download benchmark