ruby_ttt 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/ruby_ttt.rb +96 -6
  2. metadata +2 -3
  3. data/lib/game_setup.rb +0 -93
data/lib/ruby_ttt.rb CHANGED
@@ -2,16 +2,38 @@ require 'ai'
2
2
  require 'board'
3
3
  require 'player'
4
4
  require 'ui'
5
- require 'game_setup'
5
+
6
+ EASY_LEVEL = 'easy'
7
+ HARD_LEVEL = 'hard'
8
+ COMPUTER_PLAYER = 'computer'
9
+ HUMAN_PLAYER = 'human'
10
+
6
11
  class Game
7
- attr_accessor :board, :ui, :player_one, :player_two, :ai, :difficulty_level
8
- def initialize(board, ui, player_one, player_two, difficulty_level)
12
+ attr_accessor :board, :player_one, :player_two, :ai, :difficulty_level
13
+ def initialize(board, player_one, player_two)
9
14
  @board = board
10
- @ui = ui
11
15
  @player_one = player_one
12
16
  @player_two = player_two
13
17
  @ai = AI.new
14
- @difficulty_level = difficulty_level
18
+ @difficulty_level = nil
19
+ end
20
+
21
+ def set_opponents
22
+ player_one.opponent = player_two
23
+ player_two.opponent = player_one
24
+ end
25
+
26
+ def computer_player_selected?
27
+ (player_one.player_type == COMPUTER_PLAYER) || (player_two.player_type == COMPUTER_PLAYER)
28
+ end
29
+
30
+ def who_goes_first
31
+ rand(0..1) == 1 ? set_first_turn(player_one) : set_first_turn(player_two)
32
+ end
33
+
34
+ def set_first_turn(player)
35
+ player.turn = 1
36
+ ui.first_move_message(player)
15
37
  end
16
38
 
17
39
  def verify_move(cell)
@@ -48,10 +70,63 @@ class Game
48
70
  def current_player
49
71
  player_one.current_player? ? player_one : player_two
50
72
  end
51
-
52
73
  end
53
74
 
54
75
  class CLIGame < Game
76
+ attr_accessor :ui
77
+
78
+ def initialize(board, player_one, player_two)
79
+ super
80
+ @ui = CLIUI.new(board)
81
+ end
82
+
83
+ def start_cli_game!
84
+ begin
85
+ set_opponents
86
+ get_player_type(player_one)
87
+ get_player_type(player_two)
88
+ level = get_difficulty_level
89
+ who_goes_first
90
+ play!
91
+ rescue Interrupt
92
+ ui.early_exit_message
93
+ exit
94
+ end
95
+ end
96
+
97
+ def get_player_type(player)
98
+ type = ui.request_player_type(player.marker)
99
+ validate_type(type, player) ? set_player_type(type, player) : invalid_type(type, player)
100
+ end
101
+
102
+ def get_difficulty_level
103
+ return nil unless computer_player_selected?
104
+ level = ui.request_difficulty_level
105
+ validate_level(level) ? ui.level_assigned_message(level) : invalid_level(level)
106
+ end
107
+
108
+ def validate_type(type, player)
109
+ (type == HUMAN_PLAYER) || (type == COMPUTER_PLAYER)
110
+ end
111
+
112
+ def set_player_type(type, player)
113
+ player.player_type = type
114
+ ui.type_assigned_message(type, player.marker)
115
+ end
116
+
117
+ def invalid_type(type, player)
118
+ ui.invalid_input_message(type)
119
+ get_player_type(player)
120
+ end
121
+
122
+ def validate_level(level)
123
+ (level == HARD_LEVEL) || (level == EASY_LEVEL)
124
+ end
125
+
126
+ def invalid_level(level)
127
+ ui.invalid_input_message(level)
128
+ get_difficulty_level
129
+ end
55
130
 
56
131
  def play!
57
132
  until board.game_over?
@@ -71,4 +146,19 @@ class CLIGame < Game
71
146
  ui.io.exit
72
147
  end
73
148
 
149
+ end
150
+
151
+ class WebGame < Game
152
+ attr_accessor :ui
153
+
154
+ def initialize(board, player_one, player_two)
155
+ super
156
+ @ui = WebUI.new(board)
157
+ end
158
+
159
+ def set_player_types(player_one_type, player_two_type)
160
+ player_one.player_type = player_one_type
161
+ player_two.player_type = player_two_type
162
+ end
163
+
74
164
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_ttt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-16 00:00:00.000000000 Z
12
+ date: 2013-12-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -37,7 +37,6 @@ extra_rdoc_files: []
37
37
  files:
38
38
  - lib/ai.rb
39
39
  - lib/board.rb
40
- - lib/game_setup.rb
41
40
  - lib/player.rb
42
41
  - lib/ruby_ttt.rb
43
42
  - lib/ui.rb
data/lib/game_setup.rb DELETED
@@ -1,93 +0,0 @@
1
- EASY_LEVEL = 'easy'
2
- HARD_LEVEL = 'hard'
3
- COMPUTER_PLAYER = 'computer'
4
- HUMAN_PLAYER = 'human'
5
-
6
- class GameSetup
7
- attr_accessor :ui, :board, :player_one, :player_two
8
- def initialize(ui, player_one, player_two)
9
- @board = ui.board
10
- @ui = ui
11
- @player_one = player_one
12
- @player_two = player_two
13
- end
14
-
15
- def set_opponents
16
- player_one.opponent = player_two
17
- player_two.opponent = player_one
18
- end
19
-
20
- def computer_player_selected?
21
- (player_one.player_type == COMPUTER_PLAYER) || (player_two.player_type == COMPUTER_PLAYER)
22
- end
23
-
24
- def who_goes_first
25
- rand(0..1) == 1 ? set_first_turn(player_one) : set_first_turn(player_two)
26
- end
27
-
28
- def set_first_turn(player)
29
- player.turn = 1
30
- ui.first_move_message(player)
31
- end
32
- end
33
-
34
- class CLIGameSetup < GameSetup
35
-
36
- def start_cli_game!
37
- begin
38
- set_opponents
39
- get_player_type(player_one)
40
- get_player_type(player_two)
41
- level = get_difficulty_level
42
- who_goes_first
43
- CLIGame.new(board, ui, player_one, player_two, level).play!
44
- rescue Interrupt
45
- ui.early_exit_message
46
- exit
47
- end
48
- end
49
-
50
- def get_player_type(player)
51
- type = ui.request_player_type(player.marker)
52
- validate_type(type, player) ? set_player_type(type, player) : invalid_type(type, player)
53
- end
54
-
55
- def get_difficulty_level
56
- return nil unless computer_player_selected?
57
- level = ui.request_difficulty_level
58
- validate_level(level) ? ui.level_assigned_message(level) : invalid_level(level)
59
- end
60
-
61
- def validate_type(type, player)
62
- (type == HUMAN_PLAYER) || (type == COMPUTER_PLAYER)
63
- end
64
-
65
- def set_player_type(type, player)
66
- player.player_type = type
67
- ui.type_assigned_message(type, player.marker)
68
- end
69
-
70
- def invalid_type(type, player)
71
- ui.invalid_input_message(type)
72
- get_player_type(player)
73
- end
74
-
75
- def validate_level(level)
76
- (level == HARD_LEVEL) || (level == EASY_LEVEL)
77
- end
78
-
79
- def invalid_level(level)
80
- ui.invalid_input_message(level)
81
- get_difficulty_level
82
- end
83
-
84
- end
85
-
86
- class WebGameSetup < GameSetup
87
-
88
- def set_player_types(player_one_type, player_two_type)
89
- player_one.player_type = player_one_type
90
- player_two.player_type = player_two_type
91
- end
92
-
93
- end