Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
This is a GameBoard class with methods to help analyze the grid.
Ruby
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.gitignore
Gemfile
LICENSE
README.md
Rakefile
game_board.gemspec

README.md

GameBoard

Authors: MrPowers, sandbochs

This class helps calculate the rows, columns, diagonals, and diagonals of a coordinate for nested array data structures. It is useful for games like Tic-Tac-Toe, Connect Four, Battleship, etc.

Getting Started

You can add GameBoard to your Gemfile with:

gem 'game_board'

Run the bundle command afterwards to install the gem.

Usage

Create a new 4x3 game board:

board = GameBoard.new(4,3)

Populate the game board with numbers:

cells = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
board.set_grid(cells)

Get or set a value at a coordinate (row_index, column_index):

board.get_cell(0,0) => 1
board.set_cell(0,0, 'X')
board.get_cell(0,0) => X 

Get the two diagonals at a coordinate:

board.coordinate_diagonals(2, 2) => [[1, 5, 9], [9, 11]]

Get all diagonals in the grid:

board.diagonals

Get a row or column:

board.row(1) => [4, 5, 6]
board.column(2) => [3, 6, 9, 12]

Get all rows or columns:

board.rows
board.columns

Print the board to console:

# Returns a string: "[1, 2, 3]\n[4, 5, 6]\n[7, 8, 9]\n[10, 11, 12]"
puts board.to_s
Something went wrong with that request. Please try again.