digitalmars.D.learn - first try
- Philip Miess (8/8) Mar 16 2017 This is my first 100+ line D program.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (16/24) Mar 16 2017 It looks pretty good to me.
- Philip Miess (9/32) Mar 17 2017 Ali,
- Jordan Wilson (42/51) Mar 16 2017 Hello Phil,
- Philip Miess (11/38) Mar 18 2017 Jordan,
- Andrea Fontana (12/21) Mar 17 2017 // Probably you mean > rather than >=
- Andrea Fontana (6/28) Mar 17 2017 Anyway, on original .bas code I read:
- Philip Miess (7/30) Mar 18 2017 Andrea,
- XavierAP (5/8) Mar 17 2017 You don't need string literals to be verbatim (r"") in order to
- Philip Miess (5/14) Mar 18 2017 XavierAP,
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
Mar 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, PhilIt 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
Mar 16 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. AliAli, 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
Mar 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, PhilHello 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
Mar 16 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:snipI 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, JordanJordan, 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
Mar 18 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
Mar 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: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. AndreaThis 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
Mar 17 2017
On Fri, 17 Mar 2017 09:44:02 +0000, Andrea Fontana wrote: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// 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. AndreaAnyway, 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
Mar 18 2017
On Friday, 17 March 2017 at 00:35:32 UTC, Philip Miess wrote:https://gitlab.com/pmiess/101gamesDlangComputerGames/blob/master/ aceyducy.dYou don't need string literals to be verbatim (r"") in order to insert newlines as in the code (without escape sequences). All for example.
Mar 17 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:XavierAP, your right it works fine without. Thanks, Philhttps://gitlab.com/pmiess/101gamesDlangComputerGames/blob/master/ aceyducy.dYou don't need string literals to be verbatim (r"") in order to insert newlines as in the code (without escape sequences). All string literals for example.
Mar 18 2017