tic_tac_toe_jchavarria 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9488d46ca4331333c83b90f8e3838806b85c26f6
4
+ data.tar.gz: cdda21d2e70d6f6fb0e879d06ebee4d6bdd999af
5
+ SHA512:
6
+ metadata.gz: 84855cfdad4d0f6db94ab433b5780be951642696017fc0075a9ec9929d6423b9a4fae72435e8a0f8d1364306c4fef4a0d0a8d1cf5ebc15d8f9d4d7489db1d8f2
7
+ data.tar.gz: 97c67861d657f656b50b587873fb73b647720238a3f40de457fe18284c9727bd8469afe91b470bca666b535ff1ac9584fdb321f0caa72bc67703fdd950024426
data/bin/tic_tac_toe ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative "../lib/tic_tac_toe.rb"
@@ -0,0 +1,12 @@
1
+
2
+ require_relative "./tic_tac_toe/tictactoe.rb"
3
+
4
+
5
+ =begin
6
+ require_relative "./tic_tac_toe/Board.rb"
7
+ require_relative "./tic_tac_toe/Players.rb"
8
+ require_relative "./tic_tac_toe/Display.rb"
9
+ require_relative "./tic_tac_toe/Human.rb"
10
+ require_relative "./tic_tac_toe/Machine.rb"
11
+ require_relative "./tic_tac_toe/Game.rb"
12
+ =end
@@ -0,0 +1,121 @@
1
+ =begin
2
+ Class Tictactoe
3
+ Jorge Chavarria Rodriguez
4
+ =end
5
+
6
+ EMPTY_BOX = "-"
7
+ MAX_NUMBER_OF_PIECES_IN_TICTACTOE = 9
8
+
9
+ class Tictactoe
10
+
11
+ attr_accessor :board
12
+
13
+ def initialize
14
+ @board = [["-","-","-"],["-","-","-"],["-","-","-"]]
15
+ @game_winner = :no_player
16
+ end
17
+
18
+
19
+ def put_piece_at_game(row_position, column_position, game_piece)
20
+ if @board[row_position][column_position] == EMPTY_BOX
21
+ @board[row_position][column_position] = game_piece
22
+ end
23
+ end
24
+
25
+
26
+ def is_there_a_winner?
27
+ is_there_a_winner_in_row? or is_there_a_winner_in_column? or is_there_a_winner_in_diagonal_1? or is_there_a_winner_in_diagonal_2?
28
+ end
29
+
30
+
31
+ def who_is_the_winner?
32
+ @game_winner
33
+ end
34
+
35
+
36
+ def is_there_a_tie?
37
+ not is_there_a_winner? and the_board_is_full?
38
+ end
39
+
40
+
41
+ def clean_the_board
42
+ for actual_row in (0..2)
43
+ for actual_column in (0..2)
44
+ @board[actual_row][actual_column] = EMPTY_BOX
45
+ end
46
+ end
47
+ end
48
+
49
+ # ***** These are private functions *****
50
+
51
+ private
52
+
53
+ def is_there_a_winner_in_row?
54
+ for actual_row in (0..2)
55
+ if all_elements_are_equals?(@board[actual_row]) and not is_there_an_empty_box?(@board[actual_row])
56
+ @game_winner = @board[actual_row][0]
57
+ return true
58
+ end
59
+ end
60
+ false
61
+ end
62
+
63
+
64
+ def is_there_a_winner_in_column?
65
+ for actual_column in (0..2)
66
+ actual_column_values = [@board[0][actual_column], @board[1][actual_column], @board[2][actual_column]]
67
+
68
+ if all_elements_are_equals?(actual_column_values) and not is_there_an_empty_box?(actual_column_values)
69
+ @game_winner = @board[0][actual_column]
70
+ return true
71
+ end
72
+ end
73
+ false
74
+ end
75
+
76
+
77
+ def is_there_a_winner_in_diagonal_1?
78
+ diagonal_1_values = [@board[0][0], @board[1][1], @board[2][2]]
79
+
80
+ if all_elements_are_equals?(diagonal_1_values) and not is_there_an_empty_box?(diagonal_1_values)
81
+ @game_winner = @board[0][0]
82
+ return true
83
+ end
84
+ false
85
+ end
86
+
87
+
88
+ def is_there_a_winner_in_diagonal_2?
89
+ diagonal_2_values = [@board[0][2], @board[1][1], @board[2][0]]
90
+
91
+ if all_elements_are_equals?(diagonal_2_values) and not is_there_an_empty_box?(diagonal_2_values)
92
+ @game_winner = @board[0][2]
93
+ return true
94
+ end
95
+ false
96
+ end
97
+
98
+
99
+ def all_elements_are_equals?(array)
100
+ array.uniq.length == 1
101
+ end
102
+
103
+
104
+ def is_there_an_empty_box?(array)
105
+ array.include?(EMPTY_BOX)
106
+ end
107
+
108
+
109
+ def the_board_is_full?
110
+ pieces_in_game = 0
111
+ for current_row in (0..2)
112
+ for current_column in (0..2)
113
+ if @board[current_row][current_column] != EMPTY_BOX
114
+ pieces_in_game += 1
115
+ end
116
+ end
117
+ end
118
+ pieces_in_game == MAX_NUMBER_OF_PIECES_IN_TICTACTOE
119
+ end
120
+ end
121
+
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tic_tac_toe_jchavarria
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jorge Chavarria
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Console Tic Tac Toe game
14
+ email: jchavarria@pernix-solutions.com
15
+ executables:
16
+ - tic_tac_toe
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/tic_tac_toe.rb
21
+ - lib/tic_tac_toe/tictactoe.rb
22
+ - bin/tic_tac_toe
23
+ homepage: ''
24
+ licenses: []
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.0.3
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Tic tac toe console game
46
+ test_files: []