Class: EatTheOcean::MediumBattleship

Inherits:
Battleship
  • Object
show all
Defined in:
lib/battleship_hardmedium.rb,
lib/eat_the_ocean/battleship_hardmedium.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMediumBattleship

Returns a new instance of MediumBattleship.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/battleship_hardmedium.rb', line 7

def initialize
	@user_ship_1x2 = Ship.new([], 6)
	@user_ship_1x3 = Ship.new([], 6)
	@user_ship_1x4 = Ship.new([], 6)

	@opponent_ship_1x2 = Ship.new(nil, 6)
	@opponent_ship_1x2.random_1x2
   @opponent_ship_1x3 = Ship.new(@opponent_ship_1x2.coordinates, 6)
	@opponent_ship_1x3.random_1xSize(3)
	@opponent_ship_1x4 = Ship.new(@opponent_ship_1x2.coordinates + @opponent_ship_1x3.coordinates, 6)
	@opponent_ship_1x4.random_1xSize(4)

	@user_map = Map.new(6)
	@opponent_map = Map.new(6)

	@user_evaluator = Evaluator.new(@opponent_ship_1x2, @opponent_ship_1x3, @opponent_ship_1x4, nil, @opponent_map)
	@opponent_evaluator = Evaluator.new(@user_ship_1x2, @user_ship_1x3, @user_ship_1x4, nil, @user_map)

	@first_time = true
	@second_time = false
	@third_time = false
end

Instance Attribute Details

#first_timeObject

Returns the value of attribute first_time.



5
6
7
# File 'lib/battleship_hardmedium.rb', line 5

def first_time
  @first_time
end

#opponent_mapObject (readonly)

Returns the value of attribute opponent_map.



4
5
6
# File 'lib/battleship_hardmedium.rb', line 4

def opponent_map
  @opponent_map
end

#opponent_ship_1x2Object

Returns the value of attribute opponent_ship_1x2.



5
6
7
# File 'lib/battleship_hardmedium.rb', line 5

def opponent_ship_1x2
  @opponent_ship_1x2
end

#opponent_ship_1x3Object

Returns the value of attribute opponent_ship_1x3.



5
6
7
# File 'lib/battleship_hardmedium.rb', line 5

def opponent_ship_1x3
  @opponent_ship_1x3
end

#user_evaluatorObject (readonly)

Returns the value of attribute user_evaluator.



4
5
6
# File 'lib/battleship_hardmedium.rb', line 4

def user_evaluator
  @user_evaluator
end

#user_mapObject (readonly)

Returns the value of attribute user_map.



4
5
6
# File 'lib/battleship_hardmedium.rb', line 4

def user_map
  @user_map
end

Instance Method Details

#computer_guessObject



175
176
177
178
179
180
# File 'lib/battleship_hardmedium.rb', line 175

def computer_guess
	computer_coordinate = ["A", "B", "C", "D", "E", "F"].sample + rand(1..6).to_s
	unless self.already_guessed(computer_coordinate, @opponent_evaluator)
		self.guess(computer_coordinate, @opponent_evaluator)
	end
end

#guess(aGuess, evaluator) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/battleship_hardmedium.rb', line 182

def guess(aGuess, evaluator)
	hit_or_not = evaluator.hit(aGuess)
	if evaluator == @user_evaluator
		hit_or_not ?  puts("\n" + Printer.user_guess_right) : puts("\n" + Printer.user_guess_wrong)
		self.show_opponent_map
		puts "\n"
		self.computer_guess
		self.show_user_map
		puts "\n"
		if @opponent_ship_1x2.sunk + @opponent_ship_1x3.sunk + @opponent_ship_1x4.sunk == 3
			self.win_game
		elsif @user_ship_1x2.sunk + @user_ship_1x3.sunk + @user_ship_1x4.sunk == 3
			self.lose_game
		else
			self.prompt_user
		end
	else
		hit_or_not ? puts(Printer.comp_guess_right) : puts(Printer.comp_guess_wrong + aGuess + ".")
	end
end

#mark_initial_ship_position_on_mapObject



158
159
160
161
162
163
# File 'lib/battleship_hardmedium.rb', line 158

def mark_initial_ship_position_on_map
	super
	@user_ship_1x4.coordinates.each do |coordinate|
		@user_map.grid_mark(coordinate, "🐳")
	end
end

#prompt_userObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/battleship_hardmedium.rb', line 30

def prompt_user
	if @first_time == true
		puts Printer.first_boat_loop
		@first_time = false
		@second_time = true
		self.user_input_first_boat
	elsif @second_time == true
		puts Printer.second_boat_loop
		@second_time = false
		@third_time = true
		self.user_input_second_boat
	elsif @third_time == true
		puts Printer.third_boat_loop
		@third_time = false
		self.user_input_third_boat
	else
		puts Printer.guess_opponent_coordinate
		self.user_guess
	end
end

#show_opponent_mapObject



170
171
172
173
# File 'lib/battleship_hardmedium.rb', line 170

def show_opponent_map
	super
	@opponent_ship_1x4.sunk == 1 ? puts(Printer.one_by_four_sunk) : nil
end

#show_user_mapObject



165
166
167
168
# File 'lib/battleship_hardmedium.rb', line 165

def show_user_map
	super
	@user_ship_1x4.sunk == 1 ? puts(Printer.comp_one_by_four_sunk) : nil
end

#user_input_second_boatObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/battleship_hardmedium.rb', line 70

def user_input_second_boat
	to_validate = gets.chomp
	validated = self.validate_input(to_validate)
	first_coordinate = validated

	puts Printer.next_coordinate
	to_validate_2 = gets.chomp
	validated_2 = self.validate_input(to_validate_2)
	second_coordinate = validated_2

	puts Printer.next_coordinate
	to_validate_3 = gets.chomp
	validated_3 = self.validate_input(to_validate_3)
	third_coordinate = validated_3

	@user_ship_1x3.coordinates[0] = first_coordinate
	@user_ship_1x3.coordinates[1] = second_coordinate
	@user_ship_1x3.coordinates[2] = third_coordinate

	unless @user_ship_1x3.straight?(@user_ship_1x3.coordinates)
		@first_time = false
		@second_time = true
		puts Printer.not_in_line
		self.prompt_user
	end

	@user_ship_1x3.other_ship_array << @user_ship_1x2.coordinates
	@user_ship_1x3.other_ship_array.flatten!

	if @user_ship_1x3.blocked?(@user_ship_1x3.coordinates)
		@first_time = false
		@second_time = true
		puts Printer.blocked
		self.prompt_user
	end

	self.prompt_user
end

#user_input_third_boatObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/battleship_hardmedium.rb', line 109

def user_input_third_boat
	to_validate = gets.chomp
	validated = self.validate_input(to_validate)
	first_coordinate = validated

	puts Printer.next_coordinate
	to_validate_2 = gets.chomp
	validated_2 = self.validate_input(to_validate_2)
	second_coordinate = validated_2

	puts Printer.next_coordinate
	to_validate_3 = gets.chomp
	validated_3 = self.validate_input(to_validate_3)
	third_coordinate = validated_3

	puts Printer.next_coordinate
	to_validate_4 = gets.chomp
	validated_4 = self.validate_input(to_validate_4)
	fourth_coordinate = validated_4

	@user_ship_1x4.coordinates[0] = first_coordinate
	@user_ship_1x4.coordinates[1] = second_coordinate
	@user_ship_1x4.coordinates[2] = third_coordinate
	@user_ship_1x4.coordinates[3] = fourth_coordinate

	unless @user_ship_1x4.straight?(@user_ship_1x4.coordinates)
		@second_time = false
		@third_time = true
		puts Printer.not_in_line
		self.prompt_user
	end

	@user_ship_1x4.other_ship_array << @user_ship_1x2.coordinates
	@user_ship_1x4.other_ship_array << @user_ship_1x3.coordinates
	@user_ship_1x4.other_ship_array.flatten!

	if @user_ship_1x4.blocked?(@user_ship_1x4.coordinates)
		@second_time = false
		@third_time = true
		puts Printer.blocked
		self.prompt_user
	end

	self.mark_initial_ship_position_on_map
	self.show_user_map
	puts Printer.prompt_first_guess
	self.prompt_user
end

#validate_input(aString) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/battleship_hardmedium.rb', line 51

def validate_input(aString)
	upcased = aString.upcase
	if upcased[/[ABCDEF][123456]/] && upcased.length == 2
		return upcased[0..1]
	elsif aString == "q"
		$user_choice = "q"
	else
		input_invalid = true
		while input_invalid
			puts Printer.invalid_input
			upcased = gets.chomp.upcase
			if upcased[/[ABCDEF][123456]/]
				input_invalid=false
			end
		end
	end
	upcased[0..1]
end