Class: Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n, players) ⇒ Game

Returns a new instance of Game.



7
8
9
10
11
12
13
14
15
# File 'lib/main.rb', line 7

def initialize(n, players)
  @cols = 2*n+1
  @rows = 2*n-1
  @n = n
  @players = players
  @tokens = ((2*n-1)*(2*n-2)/players)
  @playersArray = Array.new()
  @winner = false
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols.



6
7
8
# File 'lib/main.rb', line 6

def cols
  @cols
end

#nObject (readonly)

Returns the value of attribute n.



6
7
8
# File 'lib/main.rb', line 6

def n
  @n
end

#rowsObject (readonly)

Returns the value of attribute rows.



6
7
8
# File 'lib/main.rb', line 6

def rows
  @rows
end

#tokensObject (readonly)

Returns the value of attribute tokens.



6
7
8
# File 'lib/main.rb', line 6

def tokens
  @tokens
end

#winnerObject (readonly)

Returns the value of attribute winner.



6
7
8
# File 'lib/main.rb', line 6

def winner
  @winner
end

#xObject (readonly)

Returns the value of attribute x.



6
7
8
# File 'lib/main.rb', line 6

def x
  @x
end

Instance Method Details

#createPlayersObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/main.rb', line 23

def createPlayers
  i = 0
  symbols = Array.new()
  until i >= @players
    $stdout.print "> Enter your name player #{i+1}: "
    $stdout.flush
    name = gets
    $stdout.print "> Enter your symbol #{name.chomp}: "
    $stdout.flush
    sym = gets
    player = Player.new(name, sym.chop!, @tokens)
    @playersArray.push(player)
    i += 1
  end
end

#initGameObject



17
18
19
20
21
# File 'lib/main.rb', line 17

def initGame
  createPlayers()
  $board = Board.new(@rows, @cols)
  turns()
end

#turnsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/main.rb', line 39

def turns
  while @winner == false do
    player = @playersArray.shift
    if player.tokens != 0
      $stdout.print "> Enter the column that you like play "
      $stdout.print "(#{player.name.chomp} :: #{player.sym} :: #{player.tokens}): "
      $stdout.flush
      x = gets.to_i
      y = $board.setPoint(x, player.sym)
      player.setPosition(x,y)
      player.restToken()
      @playersArray.push(player)
    else
      raise NoMoreTokens
    end
    if player.checkWin == true then
      @winner = true
      puts "Congrats #{player.name.chomp}! You won on nraya game!"
      raise WeHaveWin
    end
  end
end