connect_four_2211 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f51984b37042cd4df0ea53577b40c0603d9ea2aa7dd4a660c1abf5c3e876118
4
- data.tar.gz: 64070f1fb2c0dc3187a679ee5c5bc37f50813505e915520d60a6728d75d6f148
3
+ metadata.gz: 0b6aec7fc17bec0d0d1920bb1e533634578dd458c163a2874dc2d27890f58f49
4
+ data.tar.gz: 48e20778ce8f913447c59387fae90b782a11ca4ce4abb0479dd85c29b2f22932
5
5
  SHA512:
6
- metadata.gz: 171a3061be109cbdad8e3fe66b5b5b84ae47b381a740c1e717b89428d9567a36209e15afc4ceb824c99bff97e607531d3da6df86311676f527a083841d24d0bc
7
- data.tar.gz: 3f0b4fc7e37a3033440c45831b7444beec3c5222e2934119606eb166ffe48a97797f76f2fddbbcce24d547ab6e1a51061eddf7362c8d18d42c763eaf37c70b05
6
+ metadata.gz: 0d53a0ae6e3fe8eee70a152230b4ed7e532684a9317e1fdc7f79c3d888c55bf262ee769f2020736f161d5ab955c25cc17789c541d94f4ca8ef56e298be6ab947
7
+ data.tar.gz: 4e810c18c355ca0b99fccb3859cd6aa72b0649d6307b02f6faea6eba359ab8ac17ff001adfa7c4c6cba0166943aa622fee9e6ec89ac4ed2f1a4c0912b4c6700f
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require './lib/player'
4
- require './lib/computer'
5
- require './lib/board'
6
- require './lib/turn'
7
- require './lib/game'
3
+ require 'connect_four_2211/player'
4
+ require 'connect_four_2211/computer'
5
+ require 'connect_four_2211/board'
6
+ require 'connect_four_2211/turn'
7
+ require 'connect_four_2211/game'
8
8
 
9
9
  game = Game.new
10
10
  game.start
@@ -1,12 +1,35 @@
1
1
  class Board
2
- attr_accessor :board
3
- attr_reader :computer, :player, :rows
2
+ attr_accessor :board,
3
+ :l_block_index,
4
+ :r_block_index,
5
+ :l_win_index,
6
+ :r_win_index
7
+ attr_reader :computer,
8
+ :player,
9
+ :rows,
10
+ :temp_array,
11
+ :column_block,
12
+ :column_win,
13
+ :all_arrays
4
14
 
5
15
  def initialize(player, computer)
6
16
  @player = player
7
17
  @rows = []
8
18
  @computer = computer
9
19
  @temp_array = []
20
+ @column_block = []
21
+ @column_win = []
22
+ @all_arrays = []
23
+ @l_block_index = nil
24
+ @r_block_index = nil
25
+ @d_set_1 = [0, 2, 8]
26
+ @d_set_2 = [1, 3, 5]
27
+ @d_set_3 = [7, 10, 11]
28
+ @d_set_4 = [4, 9]
29
+ @r_set_1 = [0, 4, 8, 12, 16, 20]
30
+ @r_set_2 = [1, 5, 9, 13, 17, 21]
31
+ @r_set_3 = [2, 6, 10, 14, 18, 22]
32
+ @r_set_4 = [3, 7, 11, 15, 19, 23]
10
33
  end
11
34
 
12
35
  def create_board
@@ -143,4 +166,226 @@ class Board
143
166
  game_winner.compact!
144
167
  return game_winner[0]
145
168
  end
169
+
170
+ # Start of intelligent computer
171
+ def computer_column_block
172
+ board.each do |key, value|
173
+ board[key].each_cons(4) do |element|
174
+ @column_block << key if element == ['.','X', 'X', 'X']
175
+ end
176
+ end
177
+ computer.input = @column_block.first if !@column_block.empty?
178
+ end
179
+
180
+ def computer_column_win
181
+ board.each do |key, value|
182
+ board[key].each_cons(4) do |element|
183
+ @column_win << key if element == ['.','O', 'O', 'O']
184
+ end
185
+ end
186
+ computer.input = @column_win.first if !@column_win.empty?
187
+ end
188
+
189
+ def groups_of_four
190
+ @rows.shift(6)
191
+ n = 0
192
+ @rows.reverse.map {|n| n.each_cons(4) {|element| @all_arrays.push(element)}}
193
+ @all_arrays.concat(@temp_array.first(24))
194
+ end
195
+
196
+ def left_block_check
197
+ @all_arrays.find {|section| @l_block_index = @all_arrays.index(section) if section == ['.', 'X', 'X', 'X']}
198
+ if (0..23).include?(@l_block_index)
199
+ row_l_block
200
+ elsif (24..36).include?(@l_block_index)
201
+ d_l_block
202
+ elsif (36..47).include?(@l_block_index)
203
+ rd_l_block
204
+ end
205
+ end
206
+
207
+ def row_l_block
208
+ if (@r_set_1).include?(@l_block_index)
209
+ computer.input = 'A'
210
+ elsif (@r_set_2).include?(@l_block_index)
211
+ computer.input = 'B'
212
+ elsif (@r_set_3).include?(@l_block_index)
213
+ computer.input = 'C'
214
+ elsif (@r_set_4).include?(@l_block_index)
215
+ computer.input = 'D'
216
+ end
217
+ end
218
+
219
+ def d_l_block
220
+ d_l_block = (@l_block_index) - 24
221
+ if @d_set_1.include?(d_l_block)
222
+ computer.input = 'A'
223
+ elsif @d_set_2.include?(d_l_block)
224
+ computer.input = 'B'
225
+ elsif @d_set_3.include?(d_l_block)
226
+ computer.input = 'D'
227
+ elsif @d_set_4.include?(d_l_block) || d_l_block == 6
228
+ computer.input = 'C'
229
+ end
230
+ end
231
+
232
+ def rd_l_block
233
+ rd_l_block = (@l_block_index) - 36
234
+ if @d_set_1.include?(rd_l_block)
235
+ computer.input = 'A'
236
+ elsif @d_set_2.include?(rd_l_block)
237
+ computer.input = 'B'
238
+ elsif @d_set_3.include?(rd_l_block)
239
+ computer.input = 'D'
240
+ elsif @d_set_4.include?(rd_l_block) || rd_l_block == 6
241
+ computer.input = 'C'
242
+ end
243
+ end
244
+
245
+ def right_block_check
246
+ @all_arrays.find {|section| @r_block_index = all_arrays.index(section) if section == ['X', 'X', 'X', '.']}
247
+ if (0..23).include?(@r_block_index)
248
+ row_r_block
249
+ elsif (24..35).include?(@r_block_index)
250
+ d_r_block
251
+ elsif (36..47).include?(@r_block_index)
252
+ rd_r_block
253
+ end
254
+ end
255
+
256
+ def row_r_block
257
+ if (@r_set_1).include?(@r_block_index)
258
+ computer.input = 'D'
259
+ elsif (@r_set_2).include?(@r_block_index)
260
+ computer.input = 'E'
261
+ elsif (@r_set_3).include?(@r_block_index)
262
+ computer.input = 'F'
263
+ elsif (@r_set_4).include?(@r_block_index)
264
+ computer.input = 'G'
265
+ end
266
+ end
267
+
268
+ def d_r_block
269
+ d_r_block = (@r_block_index) - 24
270
+ if @d_set_1.include?(d_r_block)
271
+ computer.input = 'D'
272
+ elsif @d_set_2.include?(d_r_block)
273
+ computer.input = 'E'
274
+ elsif @d_set_3.include?(d_r_block) || d_r_block == 6
275
+ computer.input = 'G'
276
+ elsif @d_set_4.include?(d_r_block)
277
+ computer.input = 'F'
278
+ end
279
+ end
280
+
281
+ def rd_r_block
282
+ rd_r_block = (@r_block_index) - 36
283
+ if @d_set_1.include?(rd_r_block)
284
+ computer.input = 'D'
285
+ elsif @d_set_2.include?(rd_r_block)
286
+ computer.input = 'E'
287
+ elsif @d_set_3.include?(rd_r_block)
288
+ computer.input = 'G'
289
+ elsif @d_set_4.include?(rd_r_block) | rd_r_block == 6
290
+ computer.input = 'F'
291
+ end
292
+ end
293
+
294
+ def left_win_check
295
+ @all_arrays.find {|section| @l_win_index = @all_arrays.index(section) if section == ['.', 'O', 'O', 'O']}
296
+ if (0..23).include?(@l_win_index)
297
+ row_l_win
298
+ elsif (24..36).include?(@l_win_index)
299
+ d_l_win
300
+ elsif (36..47).include?(@l_win_index)
301
+ rd_l_win
302
+ end
303
+ end
304
+
305
+ def row_l_win
306
+ if (@r_set_1).include?(@l_win_index)
307
+ computer.input = 'A'
308
+ elsif (@r_set_2).include?(@l_win_index)
309
+ computer.input = 'B'
310
+ elsif (@r_set_3).include?(@l_win_index)
311
+ computer.input = 'C'
312
+ elsif (@r.set_4).include?(@l_win_index)
313
+ computer.input = 'D'
314
+ end
315
+ end
316
+
317
+ def d_l_win
318
+ d_l_win = (@l_win_index) - 24
319
+ if @d_set_1.include?(d_l_win)
320
+ computer.input = 'A'
321
+ elsif @d_set_2.include?(d_l_win)
322
+ computer.input = 'B'
323
+ elsif @d_set_3.include?(d_l_win)
324
+ computer.input = 'D'
325
+ elsif @d_set_4.include?(d_l_win) || d_l_win == 6
326
+ computer.input = 'C'
327
+ end
328
+ end
329
+
330
+ def rd_l_win
331
+ rd_l_win = (@l_win_index) - 36
332
+ if @d_set_1.include?(rd_l_win)
333
+ computer.input = 'A'
334
+ elsif @d_set_2.include?(rd_l_win)
335
+ computer.input = 'B'
336
+ elsif @d_set_3.include?(rd_l_win)
337
+ computer.input = 'D'
338
+ elsif @d_set_4.include?(rd_l_win) || rd_l_win == 6
339
+ computer.input = 'C'
340
+ end
341
+ end
342
+
343
+ def right_win_check
344
+ @all_arrays.find {|section| @r_win_index = all_arrays.index(section) if section == ['O', 'O', 'O', '.']}
345
+ if (0..23).include?(@r_win_index)
346
+ row_r_win
347
+ elsif (24..35).include?(@r_win_index)
348
+ d_r_win
349
+ elsif (36..47).include?(@r_win_index)
350
+ rd_r_win
351
+ end
352
+ end
353
+
354
+ def row_r_win
355
+ if (@r_set_1).include?(@r_win_index)
356
+ computer.input = 'D'
357
+ elsif (@r_set_2).include?(@r_win_index)
358
+ computer.input = 'E'
359
+ elsif (@r_set_3).include?(@r_win_index)
360
+ computer.input = 'F'
361
+ elsif (@r_set_4).include?(@r_win_index)
362
+ computer.input = 'G'
363
+ end
364
+ end
365
+
366
+ def d_r_win
367
+ d_r_win = (@r_win_index) - 24
368
+ if @d_set_1.include?(d_r_win)
369
+ computer.input = 'D'
370
+ elsif @d_set_2.include?(d_r_win)
371
+ computer.input = 'E'
372
+ elsif @d_set_3.include?(d_r_win) || d_r_win == 6
373
+ computer.input = 'G'
374
+ elsif @d_set_4.include?(d_r_win)
375
+ computer.input = 'F'
376
+ end
377
+ end
378
+
379
+ def rd_r_win
380
+ rd_r_win = (@r_win_index) - 36
381
+ if @d_set_1.include?(rd_r_win)
382
+ computer.input = 'D'
383
+ elsif @d_set_2.include?(rd_r_win)
384
+ computer.input = 'E'
385
+ elsif @d_set_3.include?(rd_r_win)
386
+ computer.input = 'G'
387
+ elsif @d_set_4.include?(rd_r_win) | rd_r_win == 6
388
+ computer.input = 'F'
389
+ end
390
+ end
146
391
  end
@@ -1,5 +1,6 @@
1
1
  class Computer
2
- attr_reader :input, :piece
2
+ attr_accessor :input
3
+ attr_reader :piece
3
4
 
4
5
  def initialize
5
6
  @input = nil
@@ -45,9 +45,12 @@ class Game
45
45
  end
46
46
 
47
47
  def play_game
48
- until @board.winner == @computer || @board.winner == @player || @board.full? == true
48
+ game_winner = @board.winner
49
+ until game_winner == @computer || game_winner == @player || @board.full? == true
50
+ @board.rows.clear
49
51
  player_move
50
52
  computer_move
53
+ game_winner = @board.winner
51
54
  end
52
55
  end_game
53
56
  end
@@ -79,7 +82,8 @@ class Game
79
82
 
80
83
  def computer_move
81
84
  p "Computer's move"
82
- @board.computer.give_input
85
+ @board.groups_of_four
86
+ @turn.intelligent_computer_move
83
87
  input = @board.computer.input
84
88
  until @turn.column_space_check(input) == true
85
89
  @board.computer.give_input
@@ -96,9 +100,24 @@ class Game
96
100
  elsif @board.full? == true
97
101
  p "Draw!"
98
102
  end
103
+ clear_all
99
104
  play_again
100
105
  end
101
106
 
107
+ def clear_all
108
+ @turn.computer_win.clear
109
+ @turn.computer_block.clear
110
+ @board.all_arrays.clear
111
+ @board.column_block.clear
112
+ @board.column_win.clear
113
+ @board.rows.clear
114
+ @board.temp_array.clear
115
+ @board.l_block_index = nil
116
+ @board.r_block_index = nil
117
+ @board.l_win_index = nil
118
+ @board.r_win_index = nil
119
+ end
120
+
102
121
  def play_again
103
122
  p "Would you like to play again? Type 'yes' or 'no'."
104
123
  answer = 'test'
@@ -1,9 +1,14 @@
1
1
  class Turn
2
- attr_reader :board, :input
2
+ attr_reader :board,
3
+ :input,
4
+ :computer_win,
5
+ :computer_block
3
6
 
4
7
  def initialize (board)
5
8
  @board = board
6
9
  @input = nil
10
+ @computer_win = []
11
+ @computer_block = []
7
12
  end
8
13
 
9
14
  def column_space_check(input)
@@ -57,4 +62,44 @@ class Turn
57
62
  puts '------------------------------------------------------'
58
63
  return 'End of computer turn.'
59
64
  end
65
+
66
+ # Start of intelligent computer
67
+ def intelligent_computer_win
68
+ @computer_win.push(board.computer_column_win)
69
+ @computer_win.push(board.left_win_check)
70
+ @computer_win.push(board.right_win_check)
71
+ @computer_win.compact!
72
+ end
73
+
74
+ def intelligent_computer_block
75
+ @computer_block.push(board.computer_column_block)
76
+ @computer_block.push(board.left_block_check)
77
+ @computer_block.push(board.right_block_check)
78
+ @computer_block.compact!
79
+ end
80
+
81
+ def intelligent_computer_move
82
+ intelligent_computer_win
83
+ intelligent_computer_block
84
+ if !@computer_win.empty?
85
+ board.computer.input = @computer_win.first
86
+ elsif !@computer_block.empty?
87
+ board.computer.input = @computer_block.first
88
+ else
89
+ board.computer.give_input
90
+ end
91
+ clear_computer_turn
92
+ end
93
+
94
+ def clear_computer_turn
95
+ @computer_win.clear
96
+ @computer_block.clear
97
+ board.all_arrays.clear
98
+ board.column_block.clear
99
+ board.column_win.clear
100
+ board.l_block_index = nil
101
+ board.r_block_index = nil
102
+ board.l_win_index = nil
103
+ board.r_win_index = nil
104
+ end
60
105
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: connect_four_2211
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Axel De La Guardia
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-12-12 00:00:00.000000000 Z
12
+ date: 2022-12-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec