Class: Boggle::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/boggle/board.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Board

Returns a new instance of Board.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/boggle/board.rb', line 4

def initialize(opts={})
  @size = opts[:size]
  if opts[:board].empty?
    letters = Boggle::Board.distributions(@size, opts[:variant]).sort_by{ rand }.map { |d| d.sample }
  else
    letters = letters_from_string opts[:board]
  end

  @board = []
  @size.times do
    @board << letters.pop(@size)
  end
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



2
3
4
# File 'lib/boggle/board.rb', line 2

def size
  @size
end

Class Method Details

.distributions(size, variant) ⇒ Object

4,0 gives standard distrubtion



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/boggle/board.rb', line 44

def self.distributions( size, variant ) # 4,0 gives standard distrubtion

  distros = [[
    # http://everything2.com/title/Boggle
    %w{
      ASPFFK NUIHMQ OBJOAB LNHNRZ
      AHSPCO RYVDEL IOTMUC LREIXD
      TERWHV TSTIYD WNGEEH ERTTYL
      OWTOAT AEANEG EIUNES TOESSI
    },

    # http://www.boardgamegeek.com/thread/300565/review-from-a-boggle-veteran-and-beware-differen
    %w{
      AAEEGN ELRTTY AOOTTW ABBJOO
      EHRTVW CIMOTV DISTTY EIOSST
      DELRVY ACHOPS HIMNQU EEINSU
      EEGHNW AFFKPS HLNNRZ DEILRX
    },

    %w{
      AACIOT AHMORS EGKLUY ABILTY
      ACDEMP EGINTV GILRUW ELPSTU
      DENOSW ACELRS ABJMOQ EEFHIY
      EHINPS DKNOTU ADENVZ BIFORX
    }
  ],[

    # http://boardgamegeek.com/thread/300883/letter-distribution
    %w{
      aaafrs aaeeee aafirs adennn aeeeem
      aeegmu aegmnn afirsy bjkqxz ccenst
      ceiilt ceilpt ceipst ddhnot dhhlor
      dhlnor dhlnor eiiitt emottt ensssu
      fiprsy gorrvw iprrry nootuw ooottu
    }.map(&:upcase),

    %w{
      AAAFRS	AAEEEE	AAFIRS	ADENNN	AEEEEM
      AEEGMU	AEGMNN	AFIRSY	BJKQXZ	CCNSTW
      CEIILT	CEILPT	CEIPST	DHHNOT	DHHLOR
      DHLNOR	DDLNOR	EIIITT	EMOTTT	ENSSSU
      FIPRSY	GORRVW	HIPRRY	NOOTUW	OOOTTU
    }
  ]]

  min_size = 4
  distros[size-min_size].map { |dist|
    dist.map { |die|
      die.split(//).map { |letter|
        # our distrubutions return Qu, not Q's
        letter == 'Q' ? 'Qu' : letter
      }
    }
  }[variant]
end

Instance Method Details

#[](row, col) ⇒ Object



31
32
33
# File 'lib/boggle/board.rb', line 31

def [](row, col)
  ( (row < 0) || (col < 0) || (row >= @size) || (col >= @size) ) ? nil : @board[row][col]
end

#[]=(row, col, val) ⇒ Object

deepcopy first



36
37
38
# File 'lib/boggle/board.rb', line 36

def []=(row, col, val)
  @board[row][col]=val
end

#deepcopyObject



40
41
42
# File 'lib/boggle/board.rb', line 40

def deepcopy
  Marshal.load( Marshal.dump(self) )
end

#to_sObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/boggle/board.rb', line 18

def to_s
  s = ""
  @size.times { s<<"+"; s<<"-"*3 }; s<<"+\n"
  @size .times do |row|
    s << "|"
    @size.times do |col|
      (l=@board[row][col]) == "Qu" ? s << " #{l}|" : s << " #{l} |"
    end
    s<<"\n"; @size.times { s<<"+"; s<<"-"*3 }; s<<"+\n"
  end
  s
end