Class: BoardgameEngine::Game

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

Overview

Class representing the game loop. It contains the tutorial sequence and information about the core gameplay loop

Direct Known Subclasses

Chess::Game, Connect4::Game

Constant Summary collapse

PLAY_INSTRUCTIONS =
''
GAME_NAME =
'Boardgame'
NUM_PLAYERS =
2

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(do_onboarding: true) ⇒ void

This method returns an undefined value.

Begins a round of the Game

onboarding

Parameters:

  • do_onboarding (Boolean) (defaults to: true)

    optional argument on whether to do



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/boardgame_engine/boardgame.rb', line 40

def self.start(do_onboarding: true)
  names = []
  self::NUM_PLAYERS.times do |i|
    puts "What is Player #{i}'s name?"
    names.push(gets.chomp)
  end

  @game = new(names)

  puts "Welcome to #{@game}!"
  @game.onboarding if do_onboarding
  puts "Starting #{@game}..."
  @game.play
end

Instance Method Details

#onboardingvoid

This method returns an undefined value.

Execute onboarding sequence where the player is asked if they want a tutorial



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/boardgame_engine/boardgame.rb', line 59

def onboarding
  puts "Would you like a tutorial on how to play on this program? \n(y, n)"

  case gets.chomp
  when 'y'
    tutorial
  when 'n'
    puts 'Skipping Tutorial'
  else
    puts 'Please answer either "y" or "n"'
    onboarding
  end
end

#play(turn = ) ⇒ void

This method returns an undefined value.

Play the game

Parameters:

  • turn (Player) (defaults to: )

    the player who is going first



78
79
80
81
82
83
84
85
86
87
# File 'lib/boardgame_engine/boardgame.rb', line 78

def play(turn = @players[0])
  @turn = turn
  @board.display
  until @winner
    play_turn
    @board.display
    change_turn
  end
  puts "#{@winner} wins!"
end

#to_sString

String representation of the game

Returns:

  • (String)

    <description>



92
93
94
# File 'lib/boardgame_engine/boardgame.rb', line 92

def to_s
  "#{self.class::GAME_NAME} between #{@players.join(', ')}"
end