eat_the_ocean 1.0.0

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: 24c70a1744eb54e9931bf3bbb22743e84c463651
4
+ data.tar.gz: b97b072cac3de6046fb43839473c37b02ababcd0
5
+ SHA512:
6
+ metadata.gz: 06907a736ac247ccc806a228dbec7d1afe6c76528b3df102aadfd5d486a73ad9d1f9b4e0fe959b0aa5e1dcb3a6117cba633e4cdfcbfb35a28c12c76416ad8388
7
+ data.tar.gz: 26135eb66ea6408abb2f2cb539bae43c272605b63c6b1a4ce84234155005b3d1efeff5e9ccc9ed00ddcc21e3853b3f7c74f4b28f65f330557bf4a1cd8979c408
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) <year> <copyright holders>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1 @@
1
+ A marine animal-skinned version of battleship. You can initialize 4x4, 6x6, and 8x8 gameboards.
data/bin/battleship.rb ADDED
@@ -0,0 +1,213 @@
1
+ require "./lib/evaluator"
2
+ require "./lib/printer"
3
+ require "./lib/ship"
4
+ require "./lib/map"
5
+ require "./lib/battleship_hardmedium"
6
+
7
+ module EatTheOcean
8
+ class Battleship
9
+ attr_reader :user_map, :opponent_map, :user_evaluator
10
+ attr_accessor :first_time, :opponent_ship_1x2, :opponent_ship_1x3
11
+
12
+ def initialize
13
+ @user_ship_1x2 = Ship.new
14
+ @user_ship_1x3 = Ship.new
15
+
16
+ @opponent_ship_1x2 = Ship.new
17
+ @opponent_ship_1x2.random_1x2
18
+ @opponent_ship_1x3 = Ship.new(@opponent_ship_1x2.coordinates)
19
+ @opponent_ship_1x3.random_1xSize(3)
20
+
21
+ @user_map = Map.new(4)
22
+ @opponent_map = Map.new(4)
23
+
24
+ @user_evaluator = Evaluator.new(@opponent_ship_1x2, @opponent_ship_1x3, nil, nil, @opponent_map)
25
+ @opponent_evaluator = Evaluator.new(@user_ship_1x2, @user_ship_1x3, nil, nil, @user_map)
26
+
27
+ @first_time = true
28
+ @second_time = false
29
+ end
30
+
31
+ def prompt_user
32
+ if @first_time == true
33
+ puts Printer.first_boat_loop
34
+ @first_time = false
35
+ @second_time = true
36
+ self.user_input_first_boat
37
+ elsif @second_time == true
38
+ puts Printer.second_boat_loop
39
+ @second_time = false
40
+ self.user_input_second_boat
41
+ else
42
+ puts Printer.guess_opponent_coordinate
43
+ self.user_guess
44
+ end
45
+ end
46
+
47
+ def validate_input(aString)
48
+ upcased = aString.upcase
49
+ if upcased[/[ABCD][1234]/] && upcased.length == 2
50
+ return upcased[0..1]
51
+ elsif aString == "q"
52
+ $user_choice = "q"
53
+ else
54
+ input_invalid = true
55
+ while input_invalid
56
+ puts Printer.invalid_input
57
+ upcased = gets.chomp.upcase
58
+ if upcased[/[ABCD][1234]/]
59
+ input_invalid=false
60
+ end
61
+ end
62
+ end
63
+ upcased[0..1]
64
+ end
65
+
66
+ def user_input_first_boat
67
+ to_validate = gets.chomp
68
+ validated = self.validate_input(to_validate)
69
+ first_coordinate = validated
70
+
71
+ puts Printer.next_coordinate
72
+ to_validate_2 = gets.chomp
73
+ validated_2 = self.validate_input(to_validate_2)
74
+ second_coordinate = validated_2
75
+
76
+ @user_ship_1x2.coordinates[0] = first_coordinate
77
+ @user_ship_1x2.coordinates[1] = second_coordinate
78
+
79
+ unless @user_ship_1x2.straight?(@user_ship_1x2.coordinates)
80
+ @first_time = true
81
+ @second_time = false
82
+ puts Printer.not_in_line
83
+ end
84
+ self.prompt_user
85
+ end
86
+
87
+ def user_input_second_boat
88
+ to_validate = gets.chomp
89
+ validated = self.validate_input(to_validate)
90
+ first_coordinate = validated
91
+
92
+ puts Printer.next_coordinate
93
+ to_validate_2 = gets.chomp
94
+ validated_2 = self.validate_input(to_validate_2)
95
+ second_coordinate = validated_2
96
+
97
+ puts Printer.next_coordinate
98
+ to_validate_3 = gets.chomp
99
+ validated_3 = self.validate_input(to_validate_3)
100
+ third_coordinate = validated_3
101
+
102
+ @user_ship_1x3.coordinates[0] = first_coordinate
103
+ @user_ship_1x3.coordinates[1] = second_coordinate
104
+ @user_ship_1x3.coordinates[2] = third_coordinate
105
+
106
+ unless @user_ship_1x3.straight?(@user_ship_1x3.coordinates)
107
+ @first_time = false
108
+ @second_time = true
109
+ puts Printer.not_in_line
110
+ self.prompt_user
111
+ end
112
+
113
+ @user_ship_1x3.other_ship_array << @user_ship_1x2.coordinates
114
+ @user_ship_1x3.other_ship_array.flatten!
115
+
116
+ if @user_ship_1x3.blocked?(@user_ship_1x3.coordinates)
117
+ @first_time = false
118
+ @second_time = true
119
+ puts Printer.blocked
120
+ self.prompt_user
121
+ end
122
+
123
+ self.mark_initial_ship_position_on_map
124
+ self.show_user_map
125
+ puts Printer.prompt_first_guess
126
+ self.prompt_user
127
+ end
128
+
129
+ def mark_initial_ship_position_on_map
130
+ @user_ship_1x2.coordinates.each do |coordinate|
131
+ @user_map.grid_mark(coordinate, "🐙")
132
+ end
133
+
134
+ @user_ship_1x3.coordinates.each do |coordinate|
135
+ @user_map.grid_mark(coordinate, "🐬")
136
+ end
137
+ end
138
+
139
+ def show_user_map
140
+ puts Printer.user_map
141
+ puts @user_map.grid_array
142
+ @user_ship_1x2.sunk == 1 ? puts(Printer.comp_one_by_two_sunk) : nil
143
+ @user_ship_1x3.sunk == 1 ? puts(Printer.comp_one_by_three_sunk) : nil
144
+ end
145
+
146
+ def show_opponent_map
147
+ puts Printer.opponent_map
148
+ puts @opponent_map.grid_array
149
+ @opponent_ship_1x2.sunk == 1 ? puts(Printer.one_by_two_sunk) : nil
150
+ @opponent_ship_1x3.sunk == 1 ? puts(Printer.one_by_three_sunk) : nil
151
+ end
152
+
153
+ def already_guessed(coordinate, evaluator)
154
+ #begin janky
155
+ looped = false
156
+ if evaluator.guess_record.include?(coordinate)
157
+ evaluator == @user_evaluator ? puts(Printer.already_guessed) : nil
158
+ looped = true
159
+ self.send(caller[0][/`.*'/][1..-2].to_sym)
160
+ end
161
+ looped
162
+ #end janky
163
+ end
164
+
165
+ def user_guess
166
+ user_coordinate = gets.chomp
167
+ validated_coordinate = self.validate_input(user_coordinate)
168
+ self.already_guessed(validated_coordinate, @user_evaluator)
169
+ self.guess(validated_coordinate, @user_evaluator)
170
+ end
171
+
172
+ def computer_guess
173
+ computer_coordinate = ["A", "B", "C", "D"].sample + rand(1..4).to_s
174
+ unless self.already_guessed(computer_coordinate, @opponent_evaluator)
175
+ self.guess(computer_coordinate, @opponent_evaluator)
176
+ end
177
+ end
178
+
179
+ def guess(aGuess, evaluator)
180
+ hit_or_not = evaluator.hit(aGuess)
181
+ if evaluator == @user_evaluator
182
+ hit_or_not ? puts("\n" + Printer.user_guess_right) : puts("\n" + Printer.user_guess_wrong)
183
+ self.show_opponent_map
184
+ puts "\n"
185
+ self.computer_guess
186
+ self.show_user_map
187
+ puts "\n"
188
+ if @opponent_ship_1x2.sunk + @opponent_ship_1x3.sunk == 2
189
+ self.win_game
190
+ elsif @user_ship_1x2.sunk + @user_ship_1x3.sunk == 2
191
+ self.lose_game
192
+ else
193
+ self.prompt_user
194
+ end
195
+ else
196
+ hit_or_not ? puts(Printer.comp_guess_right) : puts(Printer.comp_guess_wrong + aGuess + ".")
197
+ end
198
+ end
199
+
200
+ def win_game
201
+ puts "\n" + "You win this (unethical) game! You defeated the computer in #{@user_evaluator.guess_record.length} moves." + "\n\n"
202
+ $user_choice = "q"
203
+ end
204
+
205
+ def lose_game
206
+ puts "\n" + "You lose. The computer demoralized you in #{@opponent_evaluator.guess_record.length} moves." + "\n\n"
207
+ # puts "Play again? (y\\n)"
208
+ # answer = gets.chomp
209
+ # if answer == "y"
210
+ $user_choice = "q"
211
+ end
212
+ end
213
+ end
data/bin/eat_the_ocean ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+ require "./lib/evaluator"
3
+ require "./lib/printer"
4
+ require "./lib/ship"
5
+ require "./lib/map"
6
+ require "./lib/battleship_hardmedium"
7
+
8
+ puts EatTheOcean::Printer.title
9
+ puts EatTheOcean::Printer.welcome
10
+ $user_choice
11
+ until $user_choice == "q"
12
+ $user_choice = gets.chomp
13
+ if $user_choice == "e"
14
+ game = nil
15
+ while game.nil?
16
+ begin
17
+ game = EatTheOcean::Battleship.new
18
+ rescue SystemStackError
19
+ end
20
+ end
21
+ game.prompt_user
22
+ elsif $user_choice == "m"
23
+ game = nil
24
+ while game.nil?
25
+ begin
26
+ game = EatTheOcean::MediumBattleship.new
27
+ rescue SystemStackError
28
+ end
29
+ end
30
+ game.prompt_user
31
+ elsif $user_choice == "h"
32
+ game = nil
33
+ while game.nil?
34
+ begin
35
+ game = EatTheOcean::HardBattleship.new
36
+ rescue SystemStackError
37
+ end
38
+ end
39
+ game.prompt_user
40
+ elsif $user_choice == "i"
41
+ puts EatTheOcean::Printer.instructions
42
+ elsif $user_choice == "q"
43
+ puts "Goodbye!"
44
+ else
45
+ puts "Please enter a valid character."
46
+ end
47
+ end