Jump to content

User talk:Ffivaz

From Rosetta Code

Go examples/solutions

Welcome to RosettaCode and thanks for adding some missing Go example/solutions.

I made some changes to the Flipping bits game#Go that I'd thought I'd explain here FYI. Some of these are very minor and I wouldn't have bothered to change them if I wasn't already making other changes.

  • Ran go fmt.
  • Used <lang go> vs <lang Go>, I'm not sure if the wiki cares, but "officially" the syntax tag for Go is the former.
  • Removed leading and trailing blank line from the lang block (unlike pre if you do <lang go>\npackage foo"… it leaves a blank line).
  • Used the {{out}} template instead of "Example:" or "Output:".
  • Since you made a type I turned several of the types into methods instead including adding a String method so that the type implements fmt.String. Since the type is an array I also made (most) of these use a *matrix rather than matrix to avoid copying the array (for small arrays of 3×3 it's a wash but for anything larger passing a reference is better IMO).
  • "Fixed" setting current:
    • not calling rand.Intn(100) each time through the loop
    • making sure the start position is never the target position as required by the task (this is the whole reason I started making changes at all; the very first time I tried the original code it said "You won!" followed by the prompt of what to flip :))
    • including row 3 and column 3 in the selection of what to randomly flip (in hind sight I should have used len(current) and len(current[0]) rather than a literal 3 here)
    • not re-seeding each time randMatrix is called and removing the unnecessary call to time.UTC.
  • Combined the initial and per-move printing of the boards to avoid code duplication as well as eliminating the break by moving the condition into the for loop.
  • Removed use of bufio.Scanner. It's inappropriate to make a new scanner each time through the loop and ignore Scanner.Err (in particular, it's documented as "the reader may have advanced arbitrarily far past the last token."). (Using a simple fmt.Scanf still isn't great but is fine for the purpose of RosettaCode).
  • Removed unnecisary braces from the switch block.
  • Used range in many places instead of explicit hard coded 3. At a minimum it should have been len(m) or len(m[i]) (which for arrays is a constant and the compiler optimizes it as such) but range is more idiomatic.
  • Replace conditions in flip* with the bitwise XOR assignment operator (^=) to flip the least significant bit.

Thanks again for adding the solution, don't let my changes discourage you from adding more missing solutions. —dchapes (talk | contribs) 20:26, 14 November 2014 (UTC)

Thank you. I'll dig into your changes to learn! --Ffivaz (talk) 20:44, 14 November 2014 (UTC)
Changes done! Now the code works for an NxN board. I had to reverse some of your changes since using slices and not a array. Thanks again. --Ffivaz (talk) 08:10, 17 November 2014 (UTC)
Cookies help us deliver our services. By using our services, you agree to our use of cookies.