nicktroyanov_riverbattle 0.9.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 +7 -0
- data/lib/nicktroyanov_riverbattle.rb +4 -0
- data/lib/nicktroyanov_riverbattle/computer.rb +25 -0
- data/lib/nicktroyanov_riverbattle/game.rb +59 -0
- data/lib/nicktroyanov_riverbattle/human.rb +62 -0
- data/lib/nicktroyanov_riverbattle/player.rb +19 -0
- data/lib/nicktroyanov_riverbattle/player_and_field.rb +29 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d163fbb307e1b61ff68079d7d9b7129dd866ed37
|
4
|
+
data.tar.gz: 2cafde5d0799aa7d367df1f5c1851353c9df6ab7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c4932835123e278ffb0121241d39f097c2a421404c5f9126a17abbed189a07a86f984709a2f362e73718062a534266cb10ce64ee6590d57ea038d5dd48c2eaa7
|
7
|
+
data.tar.gz: 53ada716acf1f3e91e6ccbdb4823591909463d63fc7291f64317c3b31a2625778cafb75fe7854098625f860f5610c8aff40372fc71fa1e6aa8261b10913c45f7
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Computer < Player
|
2
|
+
|
3
|
+
attr_accessor :positions_of_ships, :already_shot_positions
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@positions_of_ships = []
|
7
|
+
@already_shot_positions = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def place_ships field, number_of_ships = 5
|
11
|
+
positions_of_ships = (0..9).to_a.sample(number_of_ships)
|
12
|
+
positions_of_ships.each { |p| field[p] = 1 }
|
13
|
+
end
|
14
|
+
|
15
|
+
def make_turn oponent_field, field
|
16
|
+
shoot_position_index = ((0..9).to_a - already_shot_positions).sample
|
17
|
+
oponent_field[shoot_position_index] += 2
|
18
|
+
already_shot_positions << shoot_position_index
|
19
|
+
end
|
20
|
+
|
21
|
+
def congratulate
|
22
|
+
puts "What a shame! You managed to loose to a computer!!"
|
23
|
+
puts "If you want to play again type Game.new.play"
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require_relative 'player.rb'
|
2
|
+
require_relative 'human.rb'
|
3
|
+
require_relative 'computer.rb'
|
4
|
+
require_relative 'player_and_field.rb'
|
5
|
+
|
6
|
+
class Game
|
7
|
+
|
8
|
+
attr_accessor :player_and_field1, :player_and_field2
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
puts <<-TEXT
|
12
|
+
Choose whether you want to make turn first or second.
|
13
|
+
If you want to be first, enter 1. Otherwise - enter 2.
|
14
|
+
TEXT
|
15
|
+
loop do
|
16
|
+
turn_position = gets.chomp.to_i
|
17
|
+
unless [1,2].include? turn_position
|
18
|
+
puts "incorrect position"
|
19
|
+
redo
|
20
|
+
end
|
21
|
+
if turn_position == 1
|
22
|
+
player1 = Human.new
|
23
|
+
@player_and_field1 = PlayerAndField.new player1
|
24
|
+
player_and_field1.player_place_ships
|
25
|
+
player2 = Computer.new
|
26
|
+
@player_and_field2 = PlayerAndField.new player2
|
27
|
+
player_and_field2.player_place_ships
|
28
|
+
break
|
29
|
+
else
|
30
|
+
player1 = Computer.new
|
31
|
+
@player_and_field1 = PlayerAndField.new player1
|
32
|
+
player_and_field1.player_place_ships
|
33
|
+
player2 = Human.new
|
34
|
+
@player_and_field2 = PlayerAndField.new player2
|
35
|
+
player_and_field2.player_place_ships
|
36
|
+
break
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def play
|
42
|
+
loop do
|
43
|
+
player_and_field1.player_show_field player_and_field2.field
|
44
|
+
player_and_field1.player_make_turn player_and_field2.field
|
45
|
+
if player_and_field2.player_win_condition?
|
46
|
+
player_and_field1.player_congratulate
|
47
|
+
break
|
48
|
+
end
|
49
|
+
player_and_field2.player_show_field player_and_field1.field
|
50
|
+
player_and_field2.player_make_turn player_and_field1.field
|
51
|
+
if player_and_field1.player_win_condition?
|
52
|
+
player_and_field2.player_congratulate
|
53
|
+
break
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
class Human < Player
|
2
|
+
|
3
|
+
attr_accessor :already_shot_positions, :positions_of_ships
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@already_shot_positions = []
|
7
|
+
@positions_of_ships = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def place_ships field, number_of_ships = 5
|
11
|
+
puts "enter #{number_of_ships} positions of your ships(numbers from 0 to 9)"
|
12
|
+
1.upto(number_of_ships) do
|
13
|
+
position = gets.chomp.to_i
|
14
|
+
if ((0..9).to_a - positions_of_ships).include? position
|
15
|
+
positions_of_ships << position
|
16
|
+
else
|
17
|
+
puts "incorrect position"
|
18
|
+
redo
|
19
|
+
end
|
20
|
+
end
|
21
|
+
positions_of_ships.each { |p| field[p] = 1 }
|
22
|
+
end
|
23
|
+
|
24
|
+
def make_turn oponent_field, field
|
25
|
+
puts "enter position on field where you want to shoot"
|
26
|
+
shoot_position = gets.chomp
|
27
|
+
if shoot_position_is_correct? shoot_position then
|
28
|
+
oponent_field[shoot_position.to_i] += 2
|
29
|
+
already_shot_positions << shoot_position.to_i
|
30
|
+
else
|
31
|
+
show_field field, oponent_field
|
32
|
+
puts "incorrect shoot position"
|
33
|
+
make_turn oponent_field, field
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def show_field field, oponent_field
|
38
|
+
system "clear"
|
39
|
+
puts "0 - cell on a board, that hasn't been shoot yet, without ship"
|
40
|
+
puts "1 - cell on a board, that hasn't been shoot yet, with ship"
|
41
|
+
puts "2 - cell on a board, that has been shoot, without ship"
|
42
|
+
puts "3 - cell on a board, that has been shoot, with ship"
|
43
|
+
puts "* - cell on a board, that you can't see(sorry it's oponent field ;])"
|
44
|
+
puts "Your field looks like:"
|
45
|
+
p field
|
46
|
+
oponent_field_to_show = ["*"]*10
|
47
|
+
already_shot_positions.each { |i| oponent_field_to_show[i] = oponent_field[i] }
|
48
|
+
puts "Your oponent's field looks like:"
|
49
|
+
p oponent_field_to_show
|
50
|
+
end
|
51
|
+
|
52
|
+
def shoot_position_is_correct? shoot_position
|
53
|
+
shoot_position.size == 1 and shoot_position.match(/\d/) and
|
54
|
+
((0..9).to_a - already_shot_positions).include? shoot_position.to_i
|
55
|
+
end
|
56
|
+
|
57
|
+
def congratulate
|
58
|
+
puts "Congratulations! You win!!"
|
59
|
+
puts "If you want to play again type Game.new.play"
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class PlayerAndField
|
2
|
+
|
3
|
+
attr_accessor :player, :field
|
4
|
+
|
5
|
+
def initialize player
|
6
|
+
@player = player
|
7
|
+
@field = [0]*10
|
8
|
+
end
|
9
|
+
|
10
|
+
def player_place_ships number_of_ships = 5
|
11
|
+
player.place_ships field, number_of_ships
|
12
|
+
end
|
13
|
+
|
14
|
+
def player_make_turn oponent_field
|
15
|
+
player.make_turn oponent_field, field
|
16
|
+
end
|
17
|
+
|
18
|
+
def player_win_condition?
|
19
|
+
player.win_condition? field
|
20
|
+
end
|
21
|
+
|
22
|
+
def player_congratulate
|
23
|
+
player.congratulate
|
24
|
+
end
|
25
|
+
|
26
|
+
def player_show_field oponent_field
|
27
|
+
player.show_field field, oponent_field
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nicktroyanov_riverbattle
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mykyta Troianov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-19 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple riverbattle console game
|
14
|
+
email: nictroyanov@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/nicktroyanov_riverbattle.rb
|
20
|
+
- lib/nicktroyanov_riverbattle/computer.rb
|
21
|
+
- lib/nicktroyanov_riverbattle/game.rb
|
22
|
+
- lib/nicktroyanov_riverbattle/human.rb
|
23
|
+
- lib/nicktroyanov_riverbattle/player.rb
|
24
|
+
- lib/nicktroyanov_riverbattle/player_and_field.rb
|
25
|
+
homepage: http://rubygems.org/gems/nicktroyanov_riverbattle
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.4.3
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: This is gem contains a simple console riverbattle game. You can play it
|
49
|
+
if you type gem install nicktroyanov_riverbattle and Game.new.play
|
50
|
+
test_files: []
|