Class: Player
- Inherits:
-
Object
- Object
- Player
- Defined in:
- lib/player.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#sym ⇒ Object
readonly
Returns the value of attribute sym.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
- #checkWin ⇒ Object
-
#initialize(name, sym, tokens) ⇒ Player
constructor
A new instance of Player.
- #restToken ⇒ Object
- #setPosition(x, y) ⇒ Object
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/player.rb', line 4 def name @name end |
#sym ⇒ Object (readonly)
Returns the value of attribute sym.
4 5 6 |
# File 'lib/player.rb', line 4 def sym @sym end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
4 5 6 |
# File 'lib/player.rb', line 4 def tokens @tokens end |
Instance Method Details
#checkWin ⇒ Object
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 |
#restToken ⇒ Object
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 |