0

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.

Xariez
  • 759
  • 7
  • 24
  • Can you please provide a [mcve]? – Enigmativity Sep 02 '20 at 02:29
  • Is there a dot after the right bracket ? I cannot clearly see it... otherwise: it's required as term completion marker. – CapelliC Sep 02 '20 at 05:49
  • @Enigmativity Will get on that now & update the OP once I got something (but should obviously not take long)! – Xariez Sep 02 '20 at 16:50
  • @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 Sep 02 '20 at 16:51
  • 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: https://stackoverflow.com/questions/23133168/after-the-first-answer-prolog-shows-the-error-char-code-2-cannot-represent-du . 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 Sep 02 '20 at 21:29
  • @Xariez - We need a [mcve]. That means we need to have the full text of the code you're running along with the query you're running. Can you please either provide it or explain why you haven't? – Enigmativity Sep 02 '20 at 22:08
  • @IsabelleNewbie Using Windows and SWI-Prolog version 8.0.3. That said, after testing your theory with `swipl.exe` and `swipl-win.exe` with the "test program" I added in the OP, it does seem to be working. Can't say if it will hold up or not, but if you add that as an actual answer I'll accept it and call it case closed for now. – Xariez Sep 03 '20 at 01:32
  • @Enigmativity What I added is the "relevant" part of the code as to reproduce the issue. This will also be added in the OP, but: 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. (However, it now seems like the issue is resolved. Mostly didn't want to leave any comments unanswered). – Xariez Sep 03 '20 at 01:37
  • @Xariez - When I run the code it seems to work fine for me. – Enigmativity Sep 03 '20 at 04:31
  • Of interest: SWI-Prolog [Getting started quickly](https://www.swi-prolog.org/pldoc/man?section=quickstart) – Guy Coder Sep 03 '20 at 10:43
  • @Enigmativity If you have a look at the comments between me and IsabelleNewbie, you'll see it seems to be up to the used executable more than anything else. Still, thanks for your input! – Xariez Sep 03 '20 at 15:43

1 Answers1

0

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.

Xariez
  • 759
  • 7
  • 24