Thread overview
first try
Mar 17, 2017
Philip Miess
Mar 17, 2017
Ali Çehreli
Mar 18, 2017
Philip Miess
Mar 17, 2017
Jordan Wilson
Mar 18, 2017
Philip Miess
Mar 17, 2017
Andrea Fontana
Mar 17, 2017
Andrea Fontana
Mar 18, 2017
Philip Miess
Mar 17, 2017
XavierAP
Mar 18, 2017
Philip Miess
March 17, 2017
This is my first 100+ line D program.
https://gitlab.com/pmiess/101gamesDlangComputerGames/blob/master/
aceyducy.d
Its a translation/refactor of aceyducy from 101 basic programs.
Could someone look over it and see if I've made any glaring mistakes.
Suggestions are welcome also.

Thanks,
Phil

March 16, 2017
I remember this game from many many years ago. :)

On 03/16/2017 05:35 PM, Philip Miess wrote:
> This is my first 100+ line D program.
> https://gitlab.com/pmiess/101gamesDlangComputerGames/blob/master/
> aceyducy.d
> Its a translation/refactor of aceyducy from 101 basic programs.
> Could someone look over it and see if I've made any glaring mistakes.
> Suggestions are welcome also.
>
> Thanks,
> Phil
>

It looks pretty good to me.

- All capital letters? Yuck! :)

- Typo: DOLLERS

- It's better to declare and define variables at the same time:

  int bet = inputInt( "\nWHAT IS YOUR BET");
  int card1 = uniform(2, 15, rnd);

That way, you can make the variables const if you care:

  const bet = inputInt( "\nWHAT IS YOUR BET");
  const card1 = uniform(2, 15, rnd);

- I liked the functional style of programming like stakes goes into aceyducyHand and then comes out as return value.

- I've also learned something: It was interesting how getBet() and aceyducyGames() are handled recursively.

Ali

March 17, 2017
On Friday, 17 March 2017 at 00:35:32 UTC, Philip Miess wrote:
> This is my first 100+ line D program.
> https://gitlab.com/pmiess/101gamesDlangComputerGames/blob/master/
> aceyducy.d
> Its a translation/refactor of aceyducy from 101 basic programs.
> Could someone look over it and see if I've made any glaring mistakes.
> Suggestions are welcome also.
>
> Thanks,
> Phil

Hello Phil,

I think there might be an issue with this function here:
int inputInt ( string prompt){
  int val ;
  writeln( prompt ~ "? ");
  string line;
  bool found = false;
  while ( !found ) {
    try {
      readf(" %d", &val);
      readln();
      found = true;
    } catch (Exception e) {
      writeln( "? " );
    }
  }
  return val;
}

I get an infinate loop if I put in something that's not convertible to an integer. I think what happens is that you catch the exception, the input still remains (presumably the buffer/range it's reading from hasn't had the chance to iterate through because of the thrown exception), and readf will always try and convert the input again.

I'd probably rewrite it as this:

int inputInt ( string prompt ){
  import std.range : popBack;
  writeln( prompt ~ "? ");

  string line;
  while ((line = readln) !is null){
    line.popBack; // remove line terminator
    try {
        return line.to!int;
    } catch (Exception e) {
        writeln ("? ");
    }
  }
  assert(0);
}

Thanks,
Jordan
March 17, 2017
On Friday, 17 March 2017 at 00:35:32 UTC, Philip Miess wrote:
> This is my first 100+ line D program.
> https://gitlab.com/pmiess/101gamesDlangComputerGames/blob/master/
> aceyducy.d
> Its a translation/refactor of aceyducy from 101 basic programs.
> Could someone look over it and see if I've made any glaring mistakes.
> Suggestions are welcome also.
>
> Thanks,
> Phil

// Probably you mean > rather than >=
if ( card1 >= card2 ) {
    swap( card1, card2);
}


// This is an old way to avoid not-intended assigment
// but in D problem doesn't exists (and yoda notation doesn't work well with overloads, i think)
if ( 0 == bet )

Another problem: you doesn't check if bet is negative. So if i bet -1000$ and I lose my bet, i actually become richer.

Andrea
March 17, 2017
On Friday, 17 March 2017 at 09:04:18 UTC, Andrea Fontana wrote:
> On Friday, 17 March 2017 at 00:35:32 UTC, Philip Miess wrote:
>> This is my first 100+ line D program.
>> https://gitlab.com/pmiess/101gamesDlangComputerGames/blob/master/
>> aceyducy.d
>> Its a translation/refactor of aceyducy from 101 basic programs.
>> Could someone look over it and see if I've made any glaring mistakes.
>> Suggestions are welcome also.
>>
>> Thanks,
>> Phil
>
> // Probably you mean > rather than >=
> if ( card1 >= card2 ) {
>     swap( card1, card2);
> }
>
>
> // This is an old way to avoid not-intended assigment
> // but in D problem doesn't exists (and yoda notation doesn't work well with overloads, i think)
> if ( 0 == bet )
>
> Another problem: you doesn't check if bet is negative. So if i bet -1000$ and I lose my bet, i actually become richer.
>
> Andrea

Anyway, on original .bas code I read:
330 IF A>=B THEN 270

That means that card1 and card2 can't be equals, but in your code this could happen.

Andrea
March 17, 2017
On Friday, 17 March 2017 at 00:35:32 UTC, Philip Miess wrote:
> 
> https://gitlab.com/pmiess/101gamesDlangComputerGames/blob/master/
> aceyducy.d

You don't need string literals to be verbatim (r"") in order to insert newlines as in the code (without escape sequences). All string literals behave this way in D -- this is different from C# for example.
March 18, 2017
On Thu, 16 Mar 2017 19:53:08 -0700, Ali Çehreli wrote:
> It looks pretty good to me.
> 
> - All capital letters? Yuck! :)
> 
> - Typo: DOLLERS
> 
> - It's better to declare and define variables at the same time:
> 
>    int bet = inputInt( "\nWHAT IS YOUR BET"); int card1 = uniform(2, 15,
>    rnd);
> 
> That way, you can make the variables const if you care:
> 
>    const bet = inputInt( "\nWHAT IS YOUR BET");
>    const card1 = uniform(2, 15, rnd);
> 
> - I liked the functional style of programming like stakes goes into aceyducyHand and then comes out as return value.
> 
> - I've also learned something: It was interesting how getBet() and
> aceyducyGames() are handled recursively.
> 
> Ali

Ali,
   I appreciate your taking the time to look.
It was all caps in the original, but your right that looks awful so I've
lower cased the messages and fixed the typo.
I also merged the declarations into the first usage and made some of the
variables constants.

Thank you,
Phil

March 18, 2017
On Fri, 17 Mar 2017 03:55:26 +0000, Jordan Wilson wrote:
> Hello Phil,
> 
> I think there might be an issue with this function here:
snip
> I get an infinate loop if I put in something that's not convertible to an integer. I think what happens is that you catch the exception, the input still remains (presumably the buffer/range it's reading from hasn't had the chance to iterate through because of the thrown exception), and readf will always try and convert the input again.
> 
> I'd probably rewrite it as this:
> 
> int inputInt ( string prompt ){
>    import std.range : popBack; writeln( prompt ~ "? ");
> 
>    string line;
>    while ((line = readln) !is null){
>      line.popBack; // remove line terminator try {
>          return line.to!int;
>      } catch (Exception e) {
>          writeln ("? ");
>      }
>    }
>    assert(0);
> }
> 
> Thanks,
> Jordan

Jordan,
  I don't see an infinite loop, but I do see one failure and prompt
printed for each character on the line that doesn't parse.
I added a readline to the catch so that the input should be emptied when
the parse fails. Does that fix your infinite loop?
I tried your proposed fix and it works the same as adding the readline
for me.

Thanks for taking the time to help,
Phil

March 18, 2017
On Fri, 17 Mar 2017 09:44:02 +0000, Andrea Fontana wrote:
>> // Probably you mean > rather than >=
>> if ( card1 >= card2 ) {
>>     swap( card1, card2);
>> }
>>
>>
>> // This is an old way to avoid not-intended assigment // but in D
>> problem doesn't exists (and yoda notation doesn't work well with
>> overloads, i think)
>> if ( 0 == bet )
>>
>> Another problem: you doesn't check if bet is negative. So if i bet -1000$ and I lose my bet, i actually become richer.
>>
>> Andrea
> 
> Anyway, on original .bas code I read:
> 330 IF A>=B THEN 270
> 
> That means that card1 and card2 can't be equals, but in your code this could happen.
> 
> Andrea
Andrea,
	your right I accidentally remove the protection from duplicate
cards during translation. I fixed that, and reacted to negative bets with
Chicken!!.

Thanks for your help,
Phil

March 18, 2017
On Fri, 17 Mar 2017 23:58:59 +0000, XavierAP wrote:

> On Friday, 17 March 2017 at 00:35:32 UTC, Philip Miess wrote:
>> 
>> https://gitlab.com/pmiess/101gamesDlangComputerGames/blob/master/ aceyducy.d
> 
> You don't need string literals to be verbatim (r"") in order to insert
> newlines as in the code (without escape sequences). All string literals
> behave this way in D -- this is different from C#
> for example.

XavierAP,
  your right it works fine without.
Thanks,
  Phil