class Game

Attributes

cols[R]
n[R]
rows[R]
tokens[R]
winner[R]
x[R]

Public Class Methods

new(n, players) click to toggle source
# 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

Public Instance Methods

createPlayers() click to toggle source
# File lib/main.rb, line 23
def createPlayers
  i = 0
  $symbols = ['.']
  until i >= @players
    $stdout.print "> Enter your name player #{i+1}: "
    $stdout.flush
    name = gets
    sym = getPlayerSym(name)
    $symbols.push(sym)
    player = Player.new(name, sym, @tokens)
    @playersArray.push(player)    
    i += 1
  end
end
getPlayerSym(name) click to toggle source
# File lib/main.rb, line 38
def getPlayerSym(name)
  $stdout.print "> Enter your symbol #{name.chomp}: "
  $stdout.flush
  sym = gets
  sym = sym.chr
  if valSym(sym) == true then getPlayerSym(name) else return sym end
end
initGame() click to toggle source
# File lib/main.rb, line 17
def initGame
  createPlayers()
  $board = Board.new(@rows, @cols)
  turns()
end
turns() click to toggle source
# File lib/main.rb, line 51
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
valSym(sym) click to toggle source
# File lib/main.rb, line 46
def valSym(sym)
  result = $symbols.include?(sym)
  return result
end