-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
80 lines (73 loc) · 2.06 KB
/
game.rb
1
2
3
4
5
6
7
8
9
10
11
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require_relative './player.rb'
require_relative './content.rb'
require_relative './game_rules.rb'
require_relative './moves.rb'
require_relative './spot.rb'
require_relative './marker.rb'
require_relative './menu_option.rb'
require_relative './get_input.rb'
class Game
include Moves
include GameRules
attr_accessor :board, :player1, :player2
def initialize(board = Board.new, player1 = nil, player2 = nil)
@board = board
@player1 = player1
@player2 = player2
end
def user_input
case GetInput.get_input
when '1'
@player1 = Player::Human.new(Marker::X)
@player2 = Player::Human.new(Marker::O)
when '2'
select_player_order
else
puts "\nInvalid choice. Select #{MenuOption::ONE} or #{MenuOption::TWO} to start game:"
user_input
end
end
def turn
puts "\nIt's now #{current_player.marker}'s turn."
game_clone = clone
input = current_player.move(game_clone).to_i
if valid_move?(@board, input.to_s)
@board.update_board(input, current_player)
@board.display_board
elsif input.between?(Spot::FIRST_SPOT, Spot::LAST_SPOT) == false
puts "\nThis move is not valid. Try again."
turn
elsif taken?(@board, input)
puts "\nLooks like that position is taken. Try again."
turn
end
end
def play
user_input
@board.reset!
@board.display_board
turn until over?(@board)
if draw?(@board)
puts "\nGame Draws."
elsif won?(@board, current_player)
puts "\nGame Over. Winner is #{winner(board)}."
end
end
def current_player
turn_count(@board.cells).even? ? @player1 : @player2
end
def select_player_order
puts "\n Choose 1 to play first, or 2 to play second: "
case GetInput.get_input
when '1'
@player1 = Player::Human.new(Marker::X)
@player2 = Player::Ai.new(Marker::O)
when '2'
@player1 = Player::Ai.new(Marker::X)
@player2 = Player::Human.new(Marker::O)
else
puts "\nInvalid choice. Select #{MenuOption::ONE} or #{MenuOption::TWO} to start game:"
user_input
end
end
end