fork(3) download
  1. require 'pp'
  2.  
  3. def check_letter(position)
  4. %w(a b c d e f g h i j).include?(position[0])
  5. end
  6.  
  7. def check_number(position)
  8. %w(1 2 3 4 5 6 7 8 9 10).include?(position[1..-1]) #pull the first character of a string
  9. end
  10.  
  11. def check_is_ship_behind(position)
  12. #TODO realization
  13. end
  14.  
  15.  
  16. board = {
  17. a: [false, false, false, false, false, false, false, false, false, false],
  18. b: [false, false, false, false, false, false, false, false, false, false],
  19. c: [false, false, false, false, false, false, false, false, false, false],
  20. d: [false, false, false, false, false, false, false, false, false, false],
  21. e: [false, false, false, false, false, false, false, false, false, false],
  22. f: [false, false, false, false, false, false, false, false, false, false],
  23. g: [false, false, false, false, false, false, false, false, false, false],
  24. h: [false, false, false, false, false, false, false, false, false, false],
  25. i: [false, false, false, false, false, false, false, false, false, false],
  26. j: [false, false, false, false, false, false, false, false, false, false]
  27. }
  28.  
  29. puts ' 1 2 3 4 5 6 7 8 9 10'
  30.  
  31. board.each do |key, row|
  32. string = ''
  33. row.each do |v|
  34. if v
  35. string += ' S'
  36. else
  37. string += ' .'
  38. end
  39. end
  40.  
  41. puts key.to_s + ' ' + string
  42. end
  43.  
  44. ships = {
  45. s1: 4,
  46. s2: 3,
  47. s3: 2,
  48. s4: 1
  49. }
  50.  
  51. loop do
  52. puts 'Select ship type[s1, s2, s3, s4]:'
  53. puts 'Remaining ships:'
  54. pp ships
  55. ship_type = gets.chop
  56.  
  57. if %w(s1 s2 s3 s4).include?(ship_type)
  58. if ships[ship_type.to_sym] > 0
  59. ships[ship_type.to_sym] -= 1
  60. current_ship = ship_type
  61. break;
  62. else
  63. puts "Don't have more ships of this type."
  64. end
  65. else
  66. puts 'Incorrect ship type.'
  67. end
  68. end
  69.  
  70. loop do
  71. puts 'Enter first position of ship[a1, a2, b4, etc.]:'
  72. first_pos = gets.chop
  73.  
  74. if check_letter(first_pos) && check_number(first_pos)
  75. #TODO: Check that the cell is not occupied, check that next to the cell is not necessary to ship
  76. if in_not_ship_behind(position)
  77. puts 'yeah'
  78. else
  79. puts 'nope'
  80. end
  81. break;
  82. else
  83. puts 'Incorrect position.'
  84. end
  85. end
  86.  
  87.  
  88. loop do
  89. if ship_type.to_sym = 1
  90. to_next_ship #Сheck that the deck of the ship has 1 space
  91. else
  92. gets second_pos
  93. break;
  94. end
  95. end
  96.  
  97.  
  98. loop do
  99. puts 'Enter second position of ship[a1, a2, b4, etc.]:'
  100. second_pos = gets.chop
  101.  
  102. #TODO: Check that first position located near second position
  103. #TODO: Check the validations like in first position
  104. #TODO: Check that the ship cannot be bend (must be in one row horizontally or vertically)
  105. #TODO: Check whether the cells did not end while building the chip type and gets the following cell
  106. break;
  107.  
  108. end
  109.  
  110. def test(postition)
  111. letter = position[0]
  112.  
  113. # 1 2 3
  114. # A O . .
  115. # B x . .
  116. # C . . .
  117.  
  118. keys = board.keys
  119. index = keys.find_index(letter)
  120. prev_key = keys[index - 1] #up-down
  121. unless prev_key.nil?
  122. is_ship_placed = board[prev_key][postition[1]] #left-right
  123. end
  124.  
  125. # 1 2 3
  126. # A O . .
  127. # B . . .
  128. # C . . .
  129.  
  130. end
Runtime error #stdin #stdout #stderr 0.02s 7564KB
stdin
Standard input is empty
stdout
   1 2 3 4 5 6 7 8 9 10
a  . . . . . . . . . .
b  . . . . . . . . . .
c  . . . . . . . . . .
d  . . . . . . . . . .
e  . . . . . . . . . .
f  . . . . . . . . . .
g  . . . . . . . . . .
h  . . . . . . . . . .
i  . . . . . . . . . .
j  . . . . . . . . . .
Select ship type[s1, s2, s3, s4]:
Remaining ships:
{:s1=>4, :s2=>3, :s3=>2, :s4=>1}
stderr
prog.rb:55:in `block in <main>': undefined method `chop' for nil:NilClass (NoMethodError)
	from prog.rb:51:in `loop'
	from prog.rb:51:in `<main>'