Building docker's Nginx environment I was addicted to the swamp and didn't study anything, so I made Othello for a change this time. Basically, it doesn't deviate from the scope of the tutorial, so I thought it was perfect for reviewing grammar. As for the structure of this section, we will first introduce the ● × game, and then introduce the coding of Othello.
Create Othello ・ Creation of board ・ Stone placement ・ Turn the stone over ・ Repeat I think it will work if only the four items of ⬆️ can be done
ruby: 2.5.3
Since it is vertically long, some parts have been changed.
line1 = "123" line2 = "456" line3 = "789" n = 1
puts line1
puts line2 ⇨ 3 * 3 squares are created. This part is abbreviated as (*) thereafter.
puts line3
puts "#{n}Turn: Player 1's turn"
input = gets.to_i
unless input >10 ⇨ Play other than 1-9 at this branch
if 1 <= input && input <= 3
line1.gsub!("#{input}", "×")⇨ Character replacement method gsub
elsif 4 <= input && input <= 6
line2.gsub!("#{input}", "×")
else
line3.gsub!("#{input}", "×")
end
else
puts "Illegal value. Penalty: Skip"
end
(*) n+= 1
while n <10 ⇨ Since it can only be repeated an even number of times, the first one was removed from while.
puts "#{n}Turn 2 Player 2's turn"
input = gets.to_i
unless input > 10
if 1 <= input && input <= 3
line1.gsub!("#{input}", "●")
elsif 4 <= input && input <= 6
line2.gsub!("#{input}", "●")
else
line3.gsub!("#{input}", "●")
end
else puts "Illegal value. Penalty: Skip"
end
(*) n += 1
puts "#{n}Turn: Player 1's turn"
input = gets.to_i
unless input > 10
if 1 <= input && input <= 3
line1.gsub!("#{input}", "×")
elsif 4 <= input && input <= 6
line2.gsub!("#{input}", "×")
else
line3.gsub!("#{input}", "×")
end
else puts "Illegal value. Penalty: Skip"
end
(*) n += 1
end
Like this. It is composed of intuitive sentences. However, if you do this in Othello, you will end up with redundant sentences, so you can use arrays to create images like mathematical matrices and quadratic functions.
BL = "●" WH = "○" wall = "+" blank = "*" field = []
m_row = 10 m_col = 10
#Initialization
m_row.times do ☆ Entered from 0 to 99 at this stage
row = []The array is complete. However, still
m_col.times do At this stage, an 8 * 8 board is completed
row <<Since there is no blank, perform the processing.
end
field << row
end
#Wall creation
0.upto(m_col - 1) do |i|☆ Make walls in the 0th and 9th rows
field[0][i] = wall field[m_row - 1 ][i] = wall
end
0.upto(m_row - 1 ) do |i|☆ Make walls on the 0th and 9th lines
field[i][0] = wall field[i][m_col - 1] = wall
end
#Initial position ⇨ With the upper left as the origin
field[4][4] = WH field[5][5] = WH
field[4][5] = BL field[5][4] = BL
def print_field(field, m_row)⇨ Method to prepare on the board
print " 123456789\n"⇨ Here\n If you don't put it in, it will shift
for i in 0..m_row -1 ⇨ Iterative method
print i.to_s ☆ At this time\Without n
row = field[i] +++++++++++********++********...
row.each do |stone|It is displayed like
print stone
end
print "\n"
end
end
#Inside out method(Black ⇨ white)
def reverse_black(bl_row, bl_col, field)
direcrions = [[-1,0], [-1,1], [0,1], [1,1], [1,0], [0,-1], [-1,-1]]
direcrions.each do |direction|☆ Judge adjacent stones one by one clockwise
reverse_pos = []⇨ If you find an enemy stone, store it
reverse_row = bl_row + direction[0].to_i ⇨ Confirmation of absolute position
reverse_col = bl_col + direction[1].to_i
if field[reverse_row][reverse_col] != "●"⇨ If it is not a friend's stone, proceed with the processing as it is
next
end
reverse_flag =false ⇨ Determining whether it is an enemy
reverse_pos << [reverse_row, reverse_col]⇨ Store enemy placement
while true ☆ At the tip of the stone that extends from there
reverse_row += direction[0]If there is a ally stone, store it there
reverse_col += direction[1]
if field[reverse_row][reverse_col] == "●"
reverse_pos << [reverse_row, reverse_col]
elsif field[reverse_row][reverse_col] == "○"
reverse_flag = true
break
else
break
end
end
if reverse_flag Transforms the stored stone into a ally stone
reverse_pos.each do |pos|
field[pos[0]][pos[1]] = "○"
end end end end
#Inside out method(White ⇨ black)
def reverse_white(wh_row, wh_col, field)
direcrions = [[-1,0], [-1,1], [0,1], [1,1], [1,0], [0,-1], [-1,-1]]
direcrions.each do |direction|
reverse_pos = []
reverse_row = wh_row + direction[0].to_i
reverse_col = wh_col + direction[1].to_i
if field[reverse_row][reverse_col] != "○"
next
end
reverse_flag = false
reverse_pos << [reverse_row, reverse_col]
while true
reverse_row += direction[0]
reverse_col += direction[1]
if field[reverse_row][reverse_col] == "○"
reverse_pos << [reverse_row, reverse_col]
elsif field[reverse_row][reverse_col] == "●"
reverse_flag = true
break
else
break
end
end
if reverse_flag
reverse_pos.each do |pos|
field[pos[0]][pos[1]] = "●"
end end end end
#Initial position completed
print_field(field, m_row)
while true do
#Put a stone
puts "It's black turn Input example: 2(Vertical),3(side) "
puts "Vertical and horizontal(comma)Enter p for paths separated by"
bl_side = gets
unless bl_side == "p"
bl_side = bl_side.split(",")⇨ Since it is a string type, it is an integer
bl_row = bl_side[0].to_Firmly changed to i
bl_col = bl_side[1].to_i
field[bl_row][bl_col] =BL ⇨ Place a stone in an absolute position from the origin
reverse_white( bl_row, bl_col, field)
print_field(field, m_row)
end
puts "It's white turn Input example: 2(Vertical),3(side) "
puts = "Vertical and horizontal(comma)Enter p for paths separated by"
wh_side = gets
unless wh_side == "p"
wh_side = wh_side.split(",")
wh_row = wh_side[0].to_i
wh_col = wh_side[1].to_i
field[wh_row][wh_col] = WH
reverse_black( wh_row, wh_col, field)
print_field(field, m_row)
end
end
Please edit and use as appropriate.
What could not be implemented in Othello ・ Functions ・ Undo function ・ A function to redirect places that cannot be placed
I wonder if there is a high degree of perfection if this area is created. I think I can play chess.
.. .. .. Shogi is impossible with the current ability www
Recommended Posts