Class: TicTacToe::Game

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ui, config = {}, over = false) ⇒ Game

Returns a new instance of Game.



6
7
8
9
10
# File 'lib/ttt/game.rb', line 6

def initialize(ui, config={}, over = false)
  @ui = ui
  game_config(config) if !config.empty?
  @over = over
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



3
4
5
# File 'lib/ttt/game.rb', line 3

def board
  @board
end

#overObject

Returns the value of attribute over.



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

def over
  @over
end

#player_oneObject (readonly)

Returns the value of attribute player_one.



3
4
5
# File 'lib/ttt/game.rb', line 3

def player_one
  @player_one
end

#player_twoObject (readonly)

Returns the value of attribute player_two.



3
4
5
# File 'lib/ttt/game.rb', line 3

def player_two
  @player_two
end

#uiObject (readonly)

Returns the value of attribute ui.



3
4
5
# File 'lib/ttt/game.rb', line 3

def ui
  @ui
end

Class Method Details

.play_game(ui, config, move = "") ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ttt/game.rb', line 12

def self.play_game(ui, config, move="")
  game = self.new(ui, config)
  game.set_spaces(config[:game_board])
  player = game.current_player(config[:game_board])

  if game.board.is_board_empty? && player.class != AI
    game.ui.display_message
    game.ui.print_board(game.board)
    game.place_move(player.mark, move)
  elsif game.is_over?
    game.ui.display_result(game.result)
    game.over = true
  else
    game.ui.ask_move(player)
    move = game.get_move(player) if player.class == AI
    game.place_move(player.mark, move)
    game.ui.print_board(game.board)

    if game.is_over?
      game.ui.display_result(game.result)
      game.over = true
    end
  end
  game
end

Instance Method Details

#again?Boolean

Returns:

  • (Boolean)


129
130
131
132
# File 'lib/ttt/game.rb', line 129

def again?
  start if @ui.again?
  @over = true
end

#ask_move(player) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/ttt/game.rb', line 77

def ask_move(player)
  user_input = @ui.ask_move(player)
  while 0 > user_input || user_input > 9
    @ui.output.puts("Invalid Move, Try Again")
    user_input = @ui.ask_move(player)
  end
  user_input
end

#current_player(board) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ttt/game.rb', line 116

def current_player(board)
  count = 0
  (0..8).each do |space|
    count += 1 if board[space] != "_"
  end

  if count.even? || count.zero?
    return @player_one
  else
    return @player_two
  end
end

#end_gameObject



110
111
112
113
114
# File 'lib/ttt/game.rb', line 110

def end_game
  @ui.print_board(@board)
  @ui.display_result(result)
  again?
end

#game_config(config) ⇒ Object



38
39
40
41
42
# File 'lib/ttt/game.rb', line 38

def game_config(config)
  @player_one = PlayerFactory.create(:type => config[:player_one].to_sym, :mark => "X")
  @player_two = PlayerFactory.create(:type => config[:player_two].to_sym, :mark => "O")
  @board = TicTacToe::Board.new
end

#get_move(player) ⇒ Object



68
69
70
71
# File 'lib/ttt/game.rb', line 68

def get_move(player)
  return @ui.ask_move(player) if player.class == Human
  return player.make_move(@board)
end

#has_winner?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/ttt/game.rb', line 60

def has_winner?
  @board.has_winner?
end

#is_over?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/ttt/game.rb', line 52

def is_over?
  tied_game? || has_winner?
end

#make_moves(moves = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ttt/game.rb', line 86

def make_moves(moves={})
  moves[:player_one] = get_move(@player_one)
  if valid_move?(moves[:player_one])
    place_move(@player_one.mark, moves[:player_one])

    if !is_over?
      @ui.print_board(@board) if @player_two.class == Human
      moves[:player_two] = get_move(@player_two)
      place_move(@player_two.mark, moves[:player_two])
      @ui.print_board(@board)
    end
  end
end

#place_move(mark, move) ⇒ Object



100
101
102
# File 'lib/ttt/game.rb', line 100

def place_move(mark, move)
  @board.place_move(mark, move) if !move.empty? && valid_move?(move)
end

#resultObject



134
135
136
137
# File 'lib/ttt/game.rb', line 134

def result
  return "tie" if tied_game?
  winner
end

#set_spaces(board) ⇒ Object



48
49
50
# File 'lib/ttt/game.rb', line 48

def set_spaces(board)
  @board = Board.parse(board)
end

#startObject



104
105
106
107
108
# File 'lib/ttt/game.rb', line 104

def start
  start_game
  make_moves until is_over?
  end_game
end

#start_gameObject



44
45
46
# File 'lib/ttt/game.rb', line 44

def start_game
  @ui.print_board(@board)
end

#tied_game?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/ttt/game.rb', line 56

def tied_game?
  @board.tied_game?
end

#unique?(spaces) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/ttt/game.rb', line 139

def unique?(spaces)
  spaces.map { |space| @board.spaces[space-1]}.uniq.length == 1
end

#valid_move?(space) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/ttt/game.rb', line 73

def valid_move?(space)
  !@board.is_space_taken?(space)
end

#winnerObject



64
65
66
# File 'lib/ttt/game.rb', line 64

def winner
  @board.winner
end