struct TwentyFortyEight::Game

Overview

The Game struct does all the heavy lifting, a new game with a default size of 4 will be initialized, then two pieces will be inserted in random a #empty position

Defined in:

twenty_forty_eight/game.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(size : Int32 = 4) #

Returns a Game of specified size and inserts two values at random #empty positions


[View source]
def self.new(board : Array(Array(Int32))) #

Returns a Game starting with specified @board of type Array(Array(Int32)), no values will be inserted and #size will be set to @board.size


[View source]

Instance Method Detail

def board : Array(Array(Int32)) #

Returns the current state of the games' Board


[View source]
def changed? : Bool #

Returns a Bool wether the game has #changed? since last move


[View source]
def down #

Returns the Symbol :down if the move #changed? the state the game


[View source]
def empty #

Returns an Array(NamedTuple(x: Int32, y: Int32)) of :x and :y positions

Running the following example:

puts TwentyFortyEight::Game.new.empty.sample[:x]

Will output:

2

[View source]
def insert #

Returns the Int32 value inserted at a random #empty position


[View source]
def left #

Returns the Symbol :left if the move #changed? the state the game


[View source]
def move(direction) #

Returns the resulting Symbol of executed direction if successful (e.g. #changed? => true) or nil


[View source]
def over? #

Returns a Bool wether the game is #over?


[View source]
def right #

Returns the Symbol :right if the move #changed? the state the game


[View source]
def score : Int32 #

Returns an Int32 containing the current score


[View source]
def size : Int32 #

Returns an Int32 containing the size of the current Board


[View source]
def unchanged? #

Returns a Bool - the inverted of #changed?


[View source]
def up #

Returns the Symbol :up if the move #changed? the state of the game


[View source]