With ruby ● × Game and Othello (basic review)

Recent report

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.

Today's topic

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

environment

ruby: 2.5.3

● × game

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.

Othello

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.

in conclusion

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

With ruby ● × Game and Othello (basic review)
Feel the basic type and reference type easily with ruby
Feel the basic type and reference type easily with ruby 2
Ruby methods and classes (basic)
Review of Ruby basic grammar
Make a typing game with ruby
Ruby Review 2
Making a Cee-lo game with Ruby Final improvement after receiving a review
Ruby Review 1
Introduction to Ruby basic grammar with yakiniku
Memorandum (Ruby: Basic Grammar: Classes and Instances)
Convert JSON to TSV and TSV to JSON with Ruby
Ruby basic terms
[Ruby] Arguments with keywords and default values of arguments
I implemented Ruby with Ruby (and C) (I played with builtin)
Try to link Ruby and Java with Dapr
Create jupyter notebook with Docker and run ruby
Solving with Ruby and Crystal AtCoder ABC 129 D
A simple rock-paper-scissors game with JavaFX and SceneBuilder
Ruby and Gem
[Review] Reading and writing files with java (JDK6)
[Ruby] Basic code list. Keep the basics with examples
Solving with Ruby and Java AtCoder ABC129 D 2D array
[Ruby] Exclude and replace specific patterns with regular expressions
Solving with Ruby, Perl and Java AtCoder ABC 128 C
Design and implement a breakout game with a clean architecture
[Beginner] Create a competitive game with basic Java knowledge
Creating a Cee-lo game with Ruby 4th Creating a game progress process
Design and implement a breakout game with a clean architecture
[Ruby] I made a crawler with anemone and nokogiri.
Install rbenv with apt on ubuntu and put ruby
[Ruby] Classes and instances
Install Ruby 3.0.0 with asdf
Symbols and Destructive Ruby
Ruby learning points (basic)
[Ruby] Big Decimal and DECIMAL
ruby basic syntax memo
Getting Started with Ruby
Ruby classes and instances
Ruby inheritance and delegation
Ruby variables and methods
Ruby cherry book review
ruby memorandum (basic edition)
11th, Classification with Ruby
Evolve Eevee with Ruby
Basic operators and operations
Review the basic knowledge of ruby that is often forgotten
Install Ruby 3.0.0 Preview 1 with a combination of Homebrew and rbenv
The difference between programming with Ruby classes and programming without it
How to deal with different versions of rbenv and Ruby
Write DiscordBot to Spreadsheets Write in Ruby and run with Docker
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)
Sample code for basic mocking and testing with Mockito 3 + JUnit 5