Class: Player

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, sym, tokens) ⇒ Player



5
6
7
8
9
10
# File 'lib/player.rb', line 5

def initialize(name, sym, tokens)
  @name = name
  @sym = sym
  @tokens = tokens
  @positions = Array.new($game.cols) {Array.new($game.cols, '.')}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/player.rb', line 4

def name
  @name
end

#symObject (readonly)

Returns the value of attribute sym.



4
5
6
# File 'lib/player.rb', line 4

def sym
  @sym
end

#tokensObject (readonly)

Returns the value of attribute tokens.



4
5
6
# File 'lib/player.rb', line 4

def tokens
  @tokens
end

Instance Method Details

#checkWinObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/player.rb', line 21

def checkWin
  #V: Vertical - #H: Horizontal - DR: Diagonal Right - DL: Diagonal Left
  i, j, k, v, h, dr, dl = [0]*7
  matriz = @positions
  matrizSize = matriz.size
  until i >= matrizSize do
    j = 0
    until j >= matrizSize do
      if matriz[j][i] == @sym then v += 1 else v = 0 end
      if matriz[i][j] == @sym then h += 1 else h = 0 end
      if matriz[i][j] == @sym && i < (matrizSize - $game.n + 1) then
        k = 0
        until k >= $game.n do
          if matriz[i+k][j+k] == @sym then dl += 1 else dl = 0 end
          if matriz[i+k][j-k] == @sym then dr += 1 else dr = 0 end
          k += 1
        end
      end
      if ( v == $game.n || h == $game.n || dr == $game.n || dl == $game.n ) then return true end
      j += 1
    end
    i += 1
    h = 0
  end
  return false
end

#restTokenObject



13
14
15
# File 'lib/player.rb', line 13

def restToken
  @tokens -= 1
end

#setPosition(x, y) ⇒ Object



17
18
19
# File 'lib/player.rb', line 17

def setPosition(x,y)
  @positions[y][x-1] = @sym
end