Class: Board
- Inherits:
-
Object
- Object
- Board
- Defined in:
- lib/fiveinarow/board.rb
Constant Summary collapse
- CELL_SIZE =
36.55
Instance Attribute Summary collapse
-
#grid ⇒ Object
Returns the value of attribute grid.
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
-
#cell_clicked(mx, my, v) ⇒ Object
return true if the cell has changed.
- #draw(mx, my, cursor) ⇒ Object
-
#initialize(game, size) ⇒ Board
constructor
A new instance of Board.
- #is_winning_move(row, col) ⇒ Object
- #mark_cell(row, col, v) ⇒ Object
- #on(row, col) ⇒ Object
Constructor Details
#initialize(game, size) ⇒ Board
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/fiveinarow/board.rb', line 10 def initialize(game, size) @game = game @size = size @grid = Array.new(size) { Array.new(size) } (0..(@size - 1)).each do |row| (0..(@size - 1)).each do |col| @grid[row][col] = Cell.new(row, col, Cell::EMPTY) end end @last_cell = false @cell_empty = Gosu::Image.new(@game, 'media/cell_white_35_35.png') @cell_a = Gosu::Image.new(@game, 'media/cell_x_35_35.png') @cell_b = Gosu::Image.new(@game, 'media/cell_o_35_35.png') @cell_alpha = Gosu::Image.new(@game, 'media/cell_alpha_35_35.png') @cell_win = Gosu::Image.new(@game, 'media/cell_win_35_35.png') end |
Instance Attribute Details
#grid ⇒ Object
Returns the value of attribute grid.
8 9 10 |
# File 'lib/fiveinarow/board.rb', line 8 def grid @grid end |
#size ⇒ Object
Returns the value of attribute size.
7 8 9 |
# File 'lib/fiveinarow/board.rb', line 7 def size @size end |
Instance Method Details
#cell_clicked(mx, my, v) ⇒ Object
return true if the cell has changed
81 82 83 84 85 |
# File 'lib/fiveinarow/board.rb', line 81 def cell_clicked(mx, my, v) mx2 = ((mx - (CELL_SIZE/2)) / CELL_SIZE).round my2 = ((my - (CELL_SIZE/2)) / CELL_SIZE).round mark_cell(mx2, my2, v) end |
#draw(mx, my, cursor) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/fiveinarow/board.rb', line 87 def draw(mx, my, cursor) if @last_cell != false @cell_alpha.draw(@last_cell[0] * CELL_SIZE, @last_cell[1] * CELL_SIZE, ZOrder::LastCell) end #if @game.stat = :end # TODO: highlight winning sequence #end (0..(@size - 1)).each do |i| (0..(@size - 1)).each do |j| x = i*CELL_SIZE y = j*CELL_SIZE #puts "drawing i=#{i} j=#{j} c=#{@grid[i][j].value}" if @grid[i][j].e? @cell_empty.draw(x, y, ZOrder::PlayerCell) elsif @grid[i][j].a? @cell_a.draw(x, y, ZOrder::PlayerCell) elsif @grid[i][j].b? @cell_b.draw(x, y, ZOrder::PlayerCell) else puts "a ou" end # Gosu::draw_quad(x-size, y-size, 0xffffffff, x+size, y-size, 0xffffffff, x-size, y+size, 0xffffffff, x+size, y+size, 0xffffffff, 0) end end if @game.state != :end mx2 = ((mx - (CELL_SIZE/2)) / CELL_SIZE).round my2 = ((my - (CELL_SIZE/2)) / CELL_SIZE).round if @grid[mx2][my2].e? if cursor == 1 @cell_a.draw(mx2 * CELL_SIZE, my2 * CELL_SIZE, ZOrder::PlayerCell) elsif cursor == 2 @cell_b.draw(mx2 * CELL_SIZE, my2 * CELL_SIZE, ZOrder::PlayerCell) end end end end |
#is_winning_move(row, col) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/fiveinarow/board.rb', line 39 def is_winning_move(row, col) v = @grid[row][col].value [[1,1], [1,0], [-1,1], [0,1]].each do |i| s = 0 rd = i[0] cd = i[1] (1..4).each do |j| break if v != @grid[row + rd*j][col + cd*j].value s += 1 end rd = -rd cd = - cd (1..4).each do |j| break if v != @grid[row + rd*j][col + cd*j].value s += 1 end return true if s >= 4 end return false end |
#mark_cell(row, col, v) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/fiveinarow/board.rb', line 63 def mark_cell(row, col, v) return false if !@grid[row][col].e? @grid[row][col].set(v) puts "marking cell row=#{row} col=#{col} v=#{v}" @last_cell = [row, col] if is_winning_move(row, col) @game.state = :end puts "winning!!" end true end |
#on(row, col) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/fiveinarow/board.rb', line 31 def on(row, col) return 0 if row < 0 return 0 if row >= @size return 0 if col < 0 return 0 if col >= @size @grid[row][col].value end |