Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7566713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:29:03+00:00 2026-05-30T14:29:03+00:00

I need to convert a string (64 character representation of a checker board): game_state

  • 0

I need to convert a string (64 character representation of a checker board):

game_state = "r_____r__r___r_rr_r___r__________________b_b______b_b____b_b___b"

into a two dimensional, 8 X 8, array of checker objects:

[ [red_checker, nil, nil, nil, nil....and so on times 8], ]

This is my way of doing it, which is ugly and not very ruby like with the increment near the end.

def game_state_string_to_board(game_state)
    board = Board.new
    game_board = board.create_test_board

    game_state_array = []
    game_state.each_char { |char| game_state_array << char}
    row_index = 0
    game_state_array.each_slice(8) do |row|
      row.each_with_index do |square, col_index|
        unless square == '_'
          game_board[row_index][col_index] = create_checker(square, row_index, col_index)
        end
      end
      row_index += 1
    end
    game_board
  end

Does anyone have a cleaner, simpler, more ruby-ish way? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:29:04+00:00Added an answer on May 30, 2026 at 2:29 pm

    Something like this ought to work (Ruby 1.9.x):

    game_state = "r_____r__r___r_rr_r___r__________________b_b______b_b____b_b___b"
    
    game_state.split( '' ).each_with_index.map do |square, idx|
      square == '_' ? nil : create_checker( square, idx / 8, idx % 8 )
    end.each_slice( 8 ).to_a
    

    Output:

    [ [ #<RedChecker(0,0)>, nil, nil, nil, nil, nil, #<RedChecker(0,6)>, nil],
      [ nil, #<RedChecker(1,1)>, nil, nil, nil, #<RedChecker(1,5)>, nil, #<RedChecker(1,7)>],
      [ #<RedChecker(2,0)>, nil, #<RedChecker(2,2)>, nil, nil, nil, #<RedChecker(2,6)>, nil],
      [ nil, nil, nil, nil, nil, nil, nil, nil],
      [ nil, nil, nil, nil, nil, nil, nil, nil],
      [ nil, #<BlackChecker(5,1)>, nil, #<BlackChecker(5,3)>, nil, nil, nil, nil],
      [ nil, nil, #<BlackChecker(6,2)>, nil, #<BlackChecker(6,4)>, nil, nil, nil],
      [ nil, #<BlackChecker(7,1)>, nil, #<BlackChecker(7,3)>, nil, nil, nil, #<BlackChecker(7,7) ]
    ]
    

    Since it’s a bit dense I’ll break it down:

    game_state.
      # change the string into an Array like [ "r", "_", "_", ...]
      split( '' ).
    
      # `each_with_index` gives us an Enumerable that passes the index of each element...
      each_with_index.
    
      # ...so we can access the index in `map`
      map do |square, idx|
        square == "_" ?
          # output nil if the input is "_"
          nil :
          # otherwise call create_checker
          create_checker( square,
            idx / 8,  # the row can be derived with integer division
            idx % 8   # and the column is the modulus (remainder)
          )
      end.
    
      # take the result of `map` and slice it up into arrays of 8 elements each
      each_slice( 8 ).to_a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to convert a string of text containing a long url into the
I need to convert a string like this: A &#039;quote&#039; is <b>bold</b> into: A
I need to convert a StringBuffer Array to String Array for sorting. Is there
How can I convert this string: This string contains the Unicode character Pi(π) into
I am trying to convert UTF-8 string into UCS-2 string. I need to get
I need to convert a string to write it into a registry.reg_binary key. I
For a problem at school, I need to convert a ASCII string of character
I need to convert a string of numbers into a string of letters to
I'm parsing command sequence strings and need to convert each string into a string[]
I have a need to convert one string to another string (4 character always)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.