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

Printing a Tic-Tac-Toe board in Ruby

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

Problem

I have two implementations of a function that prints out a basic board for Tic-Tac-Toe.

Which of these should I include and why?

def display_board(board)
  for i in 0...9
    print " #{ board[i] }"
    if (i+1)%3 != 0
      print " |"
    elsif i != 9
       print " \n#{'-'*11}\n"
    end
  end
end


def display_board(board)
  puts " #{board[0]} | #{board[1]} | #{board[2]} "
  puts "-----------"
  puts " #{board[3]} | #{board[4]} | #{board[5]} "
  puts "-----------"
  puts " #{board[6]} | #{board[7]} | #{board[8]} "
end

Solution

Surely version two.

-
It makes no use of control structures such as for or if, it has very low Kolmogorov complexity because of that, and it is good, as it is directly related to difficulty in understanding.

-
It lets you see in a single glance what the output will look like.

Context

StackExchange Code Review Q#108196, answer score: 4

Revisions (0)

No revisions yet.