HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyMinor

Burning logs - really simple text game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simplereallylogstextburninggame

Problem

What I have

I am a bit advanced PHP developer, but now I'm going to college where we are learning Ruby. We got the first homework and I did it. But I wanna make great code - so I looked for standards and found pretty much nothing. I only realized, that variables aren't named with camelCase but_like_this. I edited some things, but still I don't think this is great. Can someone familiar with Ruby tell me, how to improve this code? I am interested in details, because I wanna learn the best practice at the beginning.

Specific questions

  • Are functions without parameters bad? RubyMine highlights them and tells me "Parentheses around empty arguments list" but I am not sure, what does it mean.



  • How would be this code divided into more files (what would go where)? // It was task of the homework to make only one file



Long story short - the task

There is someone who lives in 1526. He has chunks of wood, which he can burn to make heat. Then he has logs, which he can split into chunks. Day by day he has four options.

  • Cut a tree (and get rand(2..4) logs )



  • Split rand(2..5) logs into chunks and get two chunks for each log



  • Go to sleep



  • Die



When he does something the day ends and he burns one chunk. Every day I must write what day it is and how many logs and chunks we have.

My code

```
def print_status()
puts "It's day #{$day}. You have #{$logs} logs and #{$chunks} of chunks."
end

def print_action()
puts "\nWhat will you do? \n 1. Cut a tree \n 2. Split logs into chunks \n 3. Go to sleep \n 4. Die and end the simulation"
puts "\nYour choice:"
end

def print_delimiter()
puts "\n------------------------------------------------------------"
end

def user_interaction()
print_delimiter()
print_status()
print_action()
end

def end_day()
puts "\nDay has passed and you burned a single chunk."
$day += 1
$chunks -= 1
end

$day = 1
$logs = 0
$chunks = 3

puts 'You are a logger in 1526. Try to survive.'

while $chunks > 0 do

user_interaction()

Solution

Ruby is a departure from PHP. It has a different syntax. Parentheses are often optionally when calling functions. Say you have the following function:

def add(x, y)
    x + y
end


The following are equivalent:

puts(add(3, 5))
puts add(3, 5)
puts add 3, 5


Similarly, these parentheses can be omitted, but only when the arguments are empty. Thus, the following are equivalent:

def hello()
    puts "world"
end
def foo
    puts "bar"
end

hello
foo()


So, as for one of your code examples:

def user_interaction()
  print_delimiter()
  print_status()
  print_action()
end


Can become:

def user_interaction
  print_delimiter
  print_status
  print_action
end


def print_status()
  puts "It's day #{$day}. You have #{$logs} logs and #{$chunks} of chunks."
end


Minor English quibble: it should be #{$chunks} chunks.

if logs_to_chop > $logs
    logs_to_chop = $logs
  end


You can use a neat feature of ruby's syntax: a single-statement if block shaped like this:

if c
    d
end


Can become this:

d if c


Accordingly, this line could become:

logs_to_chop = $logs if logs_to_chop > $logs


However, this change is to your discretion.

You should move your global variables ($logs, $day, $chunks) to the beginning of your code.

$chunks += logs_to_chop*2


You should always put a space on either side of binary operators like * and +. So, it should become this:

$chunks += logs_to_chop * 2


Your code ends with an exit statement; this is unnecessary, since the program terminates at the end of the program anyhow.

Other than these suggestions, I think your code is really readable. Great job! In larger programs, you may wish to add comments. I don't think they're as crucial here, since the task itself is simple enough.

Code Snippets

def add(x, y)
    x + y
end
puts(add(3, 5))
puts add(3, 5)
puts add 3, 5
def hello()
    puts "world"
end
def foo
    puts "bar"
end

hello
foo()
def user_interaction()
  print_delimiter()
  print_status()
  print_action()
end
def user_interaction
  print_delimiter
  print_status
  print_action
end

Context

StackExchange Code Review Q#144927, answer score: 2

Revisions (0)

No revisions yet.