paiza.ioでelixir その142

# 概要
paiza.ioでelixirやってみた。
練習問題やってみた。


# 練習問題
オセロを書け。

# サンプルコード

```
defmodule Va do
	use Agent
	def start_link() do
		Agent.start_link(fn -> 
            0
		end, name: __MODULE__)
	end
	def get() do
		Agent.get(__MODULE__, fn v ->
			v
		end)
	end
	def set(x) do
		Agent.update(__MODULE__, fn v ->
	        x
		end)
	end
end
defmodule Vb do
	use Agent
	def start_link() do
		Agent.start_link(fn -> 
            0
		end, name: __MODULE__)
	end
	def get() do
		Agent.get(__MODULE__, fn v ->
			v
		end)
	end
	def set(x) do
		Agent.update(__MODULE__, fn v ->
	        x
		end)
	end
end
defmodule Ban do
	use Agent
	def start_link() do
		Agent.start_link(fn -> 
			[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 1, 1, 1, 1, 1, 1, 2, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
		end, name: __MODULE__)
	end
	def get() do
		Agent.get(__MODULE__, fn v ->
			v
		end)
	end
	def get(p) do
		Agent.get(__MODULE__, fn v ->
			Enum.at(v, p)
		end)
	end
	def set(p, x) do
		Agent.update(__MODULE__, fn v ->
			List.update_at(v, p, fn _ -> 
	            x
            end)
		end)
	end
end

defmodule Main do
	def ban() do
	    IO.puts "   1 2 3 4 5 6 7 8"
		Ban.get
		|> Enum.reduce(1, fn x, n ->
	    	if rem(n, 8) == 1 do
		        IO.write " #{floor(n / 8) + 1}"
		    end
		    case x do
		    0 ->
			    IO.write " o"
			2 ->
			    IO.write " x"
			_ ->
		        IO.write "  "
			end
		    if rem(n, 8) == 0 do
		        IO.puts ""
		    end
		    n + 1
		end)
	end
	def check(p, d) do
		x = rem(p, 8)
		y = floor(p / 8) + 1
	    cond do
		(x == 0 && (d == -9 || d == -1 || d ==  7)) ->
			false
		(x == 7 && (d == -7 || d ==  1 || d ==  9)) ->
			false
		(y == 0 && (d == -9 || d == -8 || d == -7)) ->
			false
		(y == 7 && (d ==  7 || d ==  8 || d ==  9)) ->
			false
		true -> 
            true
		end
	end
	def kuro(p, x) do
	    p = p + x
	    Va.set(0)
	    if check(p, x) && Ban.get(p) == 0 do
            p = p + x
	        if check(p, x) && Ban.get(p) != 1 do
	            Va.set(1)
	            p = p + x
	            if check(p, x) do
                    p = p + x
	                if check(p, x) do
    	                p = p + x
	                    if check(p, x) do            
	                        #IO.puts p
                        end
                    end
                end
            end
        end
        Va.get
    end
	def kuro2(p, x) do
	    Ban.set(p, 2)
	    p = p + x
	    if check(p, x) && Ban.get(p) == 0 do
            Ban.set(p, 2)
            p = p + x
	        if check(p, x) && Ban.get(p) != 1 do
                Ban.set(p, 2)
	            p = p + x
	            if check(p, x) && Ban.get(p) != 1 do
	                Ban.set(p, 2)
	                p = p + x
	                if check(p, x) && Ban.get(p) != 1 do
                        Ban.set(p, 2)
	                    p = p + x
	                    if check(p, x) do            
	                        Ban.set(p, 2)
                        end
                    end
                end
            end
        end
    end
	def oku(x, y) do
        p = y * 8 + x - 9
        [-9, 9, -7, 7, -1, 1, -8, 8]
        |> Enum.map(fn x ->
            k = kuro(p, x)
            if k == 1 do
                kuro2(p, x)
            end
        end)
    end
    def siro(p, x) do
	    p = p + x
	    Va.set(0)
	    if check(p, x) && Ban.get(p) == 2 do
            p = p + x
	        if check(p, x) && Ban.get(p) != 1 do
	            Va.set(1)
	            p = p + x
	            if check(p, x) do
                    p = p + x
	                if check(p, x) do
    	                p = p + x
	                    if check(p, x) do            
	                        #IO.puts p
                        end
                    end
                end
            end
        end
        Va.get
    end
	def siro2(p, x) do
	    Ban.set(p, 0)
	    p = p + x
	    if check(p, x) && Ban.get(p) == 2 do
            Ban.set(p, 0)
            p = p + x
	        if check(p, x) && Ban.get(p) != 1 do
                Ban.set(p, 0)
	            p = p + x
	            if check(p, x) && Ban.get(p) != 1 do
	                Ban.set(p, 0)
	                p = p + x
	                if check(p, x) && Ban.get(p) != 1 do
                        Ban.set(p, 0)
	                    p = p + x
	                    if check(p, x) do            
	                        Ban.set(p, 0)
                        end
                    end
                end
            end
        end
    end
	def sasu() do
        [0, 7, 56, 63, 18, 21, 42, 45, 2, 16, 5, 23, 40, 58, 47, 61, 3, 4, 11, 12, 19, 20, 24, 25, 26, 32, 33, 34, 29, 30, 31, 37, 38, 39, 43, 44, 51, 52, 59, 60, 1, 8, 9, 10, 17, 6, 13, 14, 15, 22, 41, 48, 49, 50, 57, 46, 53, 54, 55, 62]
        |> Enum.map(fn p -> 
            if Vb.get == 0 do
                [-9, 9, -7, 7, -1, 1, -8, 8]
                |> Enum.map(fn x ->
                    k = siro(p, x)
                    if k == 1 do
                        siro2(p, x)
                        Vb.set(1)
                    end
                end)
            end
        end)
    end
end

Ban.start_link
Va.start_link
Vb.start_link
Main.ban
Main.oku(4, 3)
Main.ban
Main.sasu
Main.ban




```

# 実行結果

```
  1 2 3 4 5 6 7 8
 1                
 2                
 3                
 4       o x      
 5       x o      
 6                
 7                
 8                
   1 2 3 4 5 6 7 8
 1                
 2                
 3       x        
 4       x x      
 5       x o      
 6                
 7                
 8                
   1 2 3 4 5 6 7 8
 1                
 2                
 3     o x        
 4       o x      
 5       x o      
 6                
 7                
 8     
```

# 成果物

https://paiza.io/projects/Ejs90zuS219tBZlIRYCWew


以上。