% Hangman Game

% List of words for the game
word_list(['prolog', 'hangman', 'computer', 'programming', 'artificial', 'intelligence', 'algorithm']).

% Predicate to choose a random word from the list
random_word(Word) :-
    word_list(Words),
    length(Words, Length),
    random(1, Length, Index),
    nth1(Index, Words, Word).

% Predicate to initialize the game
start_game :-
    writeln('Welcome to Hangman!'),
    random_word(Word),
    string_chars(Word, WordChars),
    length(WordChars, WordLength),
    initial_state(State, WordLength),
    play_game(WordChars, State).

% Predicate to represent the initial state of the game
initial_state(state([], [], 0), WordLength) :-
    WordLength > 0,
    initial_state(State, Remaining),
    append(['_'], Remaining, NewGuessed),
    NextLength is WordLength - 1,
    initial_state(state(NewGuessed, ['_'], NextLength), NextLength).
initial_state(state(Guessed, Display, ), 0) :- Guessed = ['' | ], Display = ['' | _].

% Predicate to display the current state of the game
display_game(state(Guessed, Display, IncorrectGuesses)) :-
    nl,
    write('Incorrect Guesses: '), write(IncorrectGuesses), nl,
    write('Guessed: '), write(Guessed), nl,
    write('Word: '), write(Display), nl.

% Predicate to play the game
play_game(WordChars, State) :-
    display_game(State),
    read_guess(Guess),
    update_state(WordChars, State, Guess, NewState),
    game_status(NewState, WordChars).

% Predicate to read a valid guess from the user
read_guess(Guess) :-
    write('Enter your guess (a single letter): '),
    read_line_to_string(user_input, StringGuess),
    string_chars(StringGuess, [Guess]),
    char_type(Guess, alpha),
    !.
read_guess(_) :-
    writeln('Invalid input. Please enter a single letter.'),
    read_guess(Guess).

% Predicate to update the game state based on the guess
update_state(WordChars, state(Guessed, Display, IncorrectGuesses), Guess, NewState) :-
    (member(Guess, Guessed) ->
        writeln('You already guessed that letter!'),
        NewState = state(Guessed, Display, IncorrectGuesses)
    ;
    (member(Guess, WordChars) ->
        writeln('Good guess!'),
        update_display(WordChars, Guessed, Display, Guess, NewDisplay),
        NewState = state([Guess | Guessed], NewDisplay, IncorrectGuesses)
    ;
        writeln('Incorrect guess!'),
        IncorrectGuesses1 is IncorrectGuesses + 1,
        NewState = state([Guess | Guessed], Display, IncorrectGuesses1)
    )).

% Predicate to update the display with correctly guessed letters
update_display([], _, [], _, []).
update_display([WordChar | RestWord], Guessed, [DisplayChar | RestDisplay], Guess, [DisplayChar | NewRestDisplay]) :-
    (WordChar = Guess -> NewDisplayChar = Guess ; NewDisplayChar = DisplayChar),
    update_display(RestWord, Guessed, RestDisplay, Guess, NewRestDisplay).

% Predicate to determine the current status of the game
game_status(state(Guessed, Display, IncorrectGuesses), WordChars) :-
    (IncorrectGuesses >= 6 ->
        display_game(state(Guessed, WordChars, IncorrectGuesses)),
        writeln('Sorry, you lose! The word was: '), writeln(WordChars)
    ;
    (WordChars = Display ->
        display_game(state(Guessed, Display, IncorrectGuesses)),
        writeln('Congratulations, you guessed the word!')
    ;
        play_game(WordChars, state(Guessed, Display, IncorrectGuesses))
    )).

% Run the game
:- start_game.






 

Prolog online compiler

Write, Run & Share Prolog code online using OneCompiler’s Prolog online compiler for free. It’s a simple and intuitive platform to experiment with logic programming in Prolog. OneCompiler supports standard Prolog syntax, great for learning, prototyping, and practicing logic-based problems.

About Prolog

Prolog (Programming in Logic) is a logic programming language associated with artificial intelligence and computational linguistics. It works through facts, rules, and queries, using a form of symbolic reasoning known as backward chaining. Prolog is declarative, meaning you describe what you want instead of how to compute it.

Sample Code

The following is a simple Prolog program that prints a greeting:

:- initialization(main).

main :-
    write('Hello, World!').

Syntax Basics

Facts

Facts represent basic assertions about the world.

likes(alice, pizza).
likes(bob, pasta).

Rules

Rules define logical relationships using facts.

friends(X, Y) :- likes(X, Z), likes(Y, Z).

Queries

Queries are used to find information based on facts and rules.

?- likes(alice, What).

Operators

OperatorDescription
:-Rule definition
,Logical AND
;Logical OR
=Unification

Lists

member(X, [X|_]).
member(X, [_|T]) :- member(X, T).

Recursion

Prolog heavily relies on recursion.

factorial(0, 1).
factorial(N, F) :-
  N > 0,
  N1 is N - 1,
  factorial(N1, F1),
  F is N * F1.

This guide provides a quick reference to Prolog programming syntax and features. Start writing Prolog code using OneCompiler’s Prolog online compiler today!