0
votes

Title more or less says it all. I got the following code in SWI-PROLOG:

get_card(1):-
    nb_getval(playerOneHand, Hand),
    write("Select which cards you want to play:"), nl,
    read(Input),
    handle_input(Input).

get_card(2):-
    nb_getval(playerTwoHand, Hand),
    write("Select which cards you want to play:"), nl,
    read(Input),
    handle_input(Input).

Expected input-format right now is something like [ 0, 1 ]. to play your first two cards, [ 0 ]. to only play your first card, so on and so forth. While I know this is beyond horrible as far as input formats go, the priority right now is to atleast get it working, then move on to a better UI from there.

This is what happens:

I'm very new to Prolog so bear with me, but this is how I'm looking at it: It seems to be "skipping" over our read and is now expecting a new command to be entered instead (ENTER has been pressed in the above picture).

I feel like the solution should be very simple but unfortunately is nothing I can seem to wrap my head around. Thanks in advance!

EDIT: Example code that can be ran to reproduce the issue, unless it's only happening on my system for some reason:

% Initialize globals
?- nb_setval(playerOneHand, []).
?- nb_setval(playerTwoHand, []).

handle_input(Input, Hand):-
    write("Input is "), write(Input), nl,
    write("Hand is "), write(Hand), nl.

get_card(1):-
    nb_getval(playerOneHand, Hand),
    write("Select which cards you want to play: "), nl,
    read(Input),
    handle_input(Input, Hand).

get_card(2):-
    nb_getval(playerTwoHand, Hand),
    write("Select which cards you want to play: "), nl,
    read(Input),
    handle_input(Input, Hand).

?- write("Player 1: "), get_card(1), nl,
   write("Player 2: "), get_card(2), nl.

Using SWI-Prolog and the given code in a test.pl file, it is simply consulted via the terminal interface (File -> Consult -> test.pl). Trying to give SWI-Prolog ANY kind of input then results in the issue at hand.

1
Can you please provide a minimal reproducible example?Enigmativity
Is there a dot after the right bracket ? I cannot clearly see it... otherwise: it's required as term completion marker.CapelliC
@Enigmativity Will get on that now & update the OP once I got something (but should obviously not take long)!Xariez
@CapelliC There is, but the "?-" from Prolog gets printed the second I hit a key (in this case, [), so right now the issue is something else I feel like.Xariez
Are you on Windows? Which program are you actually running? I think there is some long-standing bug where swipl.exe is broken and cannot do proper input and output, and swipl-win.exe works: stackoverflow.com/questions/23133168/… . Also, please make your description really clear, e.g. "I put the following code into file foo.pl", "I load foo.pl into SWI-Prolog as follows", etc.Isabelle Newbie

1 Answers

0
votes

As mentioned by Isabelle Newbie in the comments, swipl.exe (that is started by default) has a long-running bug associated with proper input/output. Navigating to where Prolog is installed and instead using swipl-win.exe seems to have done the trick.