paiza.ioでelixir その163

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

# 練習問題
オセロを完成させよ。

# 方針
パスを実装する。

# サンプルコード

```

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 cls() do
		Agent.update(__MODULE__, fn v ->
	        0
		end)
	end
	def set() do
		Agent.update(__MODULE__, fn v ->
	        1
		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 Isi do
	use Agent
	def start_link() do
		Agent.start_link(fn -> 
			[]
		end, name: __MODULE__)
	end
	def get() do
		Agent.get(__MODULE__, fn v ->
			v
		end)
	end
	def len() do
		Agent.get(__MODULE__, fn v ->
			Enum.reduce(v, 0, fn x, n ->
                n + 1
            end)
		end)
	end
	def cls() do
		Agent.update(__MODULE__, fn v ->
			[]
		end)
	end
	def put(x) do
		Agent.update(__MODULE__, fn v ->
			v ++ [x]
		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) 
	    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
        Isi.cls()
        Isi.put(p)
	    p = p + x
	    if check(p, x) && Ban.get(p) == 0 do
	        Isi.put(p)
            p = p + x
            cond do
	        check(p, x) && Ban.get(p) == 0 ->
                Isi.put(p)
                p = p + x
                cond do
	            check(p, x) && Ban.get(p) == 0 ->
	                Isi.put(p)
                    p = p + x
                    cond do
	                check(p, x) && Ban.get(p) == 0 ->
	                    Isi.put(p)
                        p = p + x
                        cond do
	                    check(p, x) && Ban.get(p) == 0 ->
	                        Isi.put(p)
                            p = p + x
                            cond do
	                        check(p, x) && Ban.get(p) == 0 ->
	                            true
	                        check(p, x) && Ban.get(p) == 2 ->
                                #IO.puts "ok5"
                                Vb.set()
                                Enum.map(Isi.get(), fn y ->
                                    Ban.set(y, 2)
                                end)
        		            true -> 
                                true
    		                end
    		            check(p, x) && Ban.get(p) == 2 ->
                            #IO.puts "ok4"
                            Vb.set()
                            Enum.map(Isi.get(), fn y ->
                                Ban.set(y, 2)
                            end)
        		        true -> 
                            true
    		            end
	                check(p, x) && Ban.get(p) == 2 ->
                        #IO.puts "ok3"
                        Vb.set()
                        Enum.map(Isi.get(), fn y ->
                            Ban.set(y, 2)
                        end)
        		    true -> 
                        true
    		        end
    		    true
	            check(p, x) && Ban.get(p) == 2 ->
	                #IO.puts "ok2"
	                Vb.set()
                    Enum.map(Isi.get(), fn y ->
                        Ban.set(y, 2)
                    end)
        		true -> 
                    true
    		    end
	        check(p, x) && Ban.get(p) == 2 ->
	            #IO.puts "ok1"
	            Vb.set()
                Enum.map(Isi.get(), fn y ->
                    Ban.set(y, 2)
                end)
    		true -> 
                true
		    end
        end
    end
	def oku(x, y) do
        p = y * 8 + x - 9
        if Ban.get(p) == 1 do
            IO.puts "kuro #{x} #{y}"
            [-9, 9, -7, 7, -1, 1, -8, 8]
            |> Enum.map(fn x ->
                k = kuro(p, x)
            end)
        end
    end
    def siro(p, x) do
        Isi.cls()
        Isi.put(p)
	    p = p + x
	    if check(p, x) && Ban.get(p) == 2 do
	        Isi.put(p)
            p = p + x
            cond do
	        check(p, x) && Ban.get(p) == 2 ->
                Isi.put(p)
                p = p + x
                cond do
	            check(p, x) && Ban.get(p) == 2 ->
	                Isi.put(p)
                    p = p + x
                    cond do
	                check(p, x) && Ban.get(p) == 2 ->
	                    Isi.put(p)
                        p = p + x
                        cond do
	                    check(p, x) && Ban.get(p) == 2 ->
	                        Isi.put(p)
                            p = p + x
                            cond do
	                        check(p, x) && Ban.get(p) == 2 ->
	                            true
	                        check(p, x) && Ban.get(p) == 0 ->
                                #IO.puts "ok5 #{x}"
                                Vb.set()
                                Enum.map(Isi.get(), fn y ->
                                    Ban.set(y, 0)
                                end)
        		            true -> 
                                true
    		                end
    		            check(p, x) && Ban.get(p) == 0 ->
                            #IO.puts "ok4 #{p} #{x}"
                            Vb.set()
                            Enum.map(Isi.get(), fn y ->
                                Ban.set(y, 0)
                            end)
        		        true -> 
                            true
    		            end
	                check(p, x) && Ban.get(p) == 0 ->
                        #IO.puts "ok3 #{p} #{x}"
                        Vb.set()
                        Enum.map(Isi.get(), fn y ->
                            Ban.set(y, 0)
                        end)
        		    true -> 
                        true
    		        end
    		    true
	            check(p, x) && Ban.get(p) == 0 ->
	                #IO.puts "ok2 #{x}"
	                Vb.set()
                    Enum.map(Isi.get(), fn y ->
                        Ban.set(y, 0)
                    end)
        		true -> 
                    true
    		    end
	        check(p, x) && Ban.get(p) == 0 ->
	            #IO.puts "ok1 #{x}"
	            Vb.set()
                Enum.map(Isi.get(), fn y ->
                    Ban.set(y, 0)
                end)
    		true -> 
                true
		    end
        end
    end
	def sasu() do
	    Vb.cls()
        [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 Ban.get(p) == 1 do
                if Vb.get == 0 do
                    #IO.puts p
                    [-9, 9, -7, 7, -1, 1, -8, 8]
                    |> Enum.map(fn x ->
                        if check(p, x) do
                            k = siro(p, x)
                        end
                    end)
                end
            end
        end)
        if Vb.get == 0 do
            IO.puts "pass"
        end
    end
end
Isi.start_link
Ban.start_link
Vb.start_link
Main.ban
Main.oku(4, 3)
#IO.puts "kuro 4 3"
Main.ban
Main.sasu
Main.ban
Main.oku(3, 4)
#IO.puts "kuro 3 4"
Main.ban
Main.sasu
Main.ban
Main.oku(3, 2)
#IO.puts "kuro 3 2"
Main.ban
Main.sasu
Main.ban
Main.oku(6, 3)
#IO.puts "kuro 6 3"
Main.ban
Main.sasu
Main.ban
Main.oku(6, 4)
#IO.puts "kuro 6 4"
Main.ban
Main.sasu
Main.ban
Main.oku(3, 1)
#IO.puts "kuro 3 1"
Main.ban
Main.sasu
Main.ban
Main.oku(5, 1)
#IO.puts "kuro 5 1"
Main.ban
Main.sasu
Main.ban
Main.oku(5, 6)
#IO.puts "kuro 5 6"
Main.ban
Main.sasu
Main.ban
Main.oku(2, 4)
#IO.puts "kuro 2 4"
Main.ban
Main.sasu
Main.ban
Main.oku(1, 6)
#IO.puts "kuro 1 6"
Main.ban
Main.sasu
Main.ban
#IO.puts Main.check(47, -7)
Main.oku(1, 5)
Main.ban
Main.sasu
Main.ban
Main.oku(1, 3)
Main.ban
Main.sasu
Main.ban
Main.oku(2, 3)
Main.ban
Main.sasu
Main.ban
Main.oku(2, 6)
Main.ban
Main.sasu
Main.ban
Main.oku(6, 5)
Main.ban
Main.sasu
Main.ban
Main.oku(1, 1)
Main.ban
Main.sasu
Main.ban
Main.oku(2, 2)
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                
kuro 4 3
   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                
kuro 3 4
   1 2 3 4 5 6 7 8
 1                
 2                
 3     o x        
 4     x x x      
 5       x o      
 6                
 7                
 8                
   1 2 3 4 5 6 7 8
 1                
 2                
 3     o o o      
 4     x x o      
 5       x o      
 6                
 7                
 8                
kuro 3 2
   1 2 3 4 5 6 7 8
 1                
 2     x          
 3     x o o      
 4     x x o      
 5       x o      
 6                
 7                
 8                
   1 2 3 4 5 6 7 8
 1                
 2     x          
 3     x o o      
 4     x x o      
 5       o o      
 6     o          
 7                
 8                
kuro 6 3
   1 2 3 4 5 6 7 8
 1                
 2     x          
 3     x x x x    
 4     x x o      
 5       o o      
 6     o          
 7                
 8                
   1 2 3 4 5 6 7 8
 1                
 2     x o        
 3     x o x x    
 4     x o o      
 5       o o      
 6     o          
 7                
 8                
kuro 6 4
   1 2 3 4 5 6 7 8
 1                
 2     x o        
 3     x o x x    
 4     x x x x    
 5       o o      
 6     o          
 7                
 8                
   1 2 3 4 5 6 7 8
 1                
 2     x o o      
 3     x o o x    
 4     x x o x    
 5       o o      
 6     o          
 7                
 8                
kuro 3 1
   1 2 3 4 5 6 7 8
 1     x          
 2     x x o      
 3     x o x x    
 4     x x o x    
 5       o o      
 6     o          
 7                
 8                
   1 2 3 4 5 6 7 8
 1     x o        
 2     x o o      
 3     x o x x    
 4     x x o x    
 5       o o      
 6     o          
 7                
 8                
kuro 5 1
   1 2 3 4 5 6 7 8
 1     x x x      
 2     x x x      
 3     x o x x    
 4     x x o x    
 5       o o      
 6     o          
 7                
 8                
   1 2 3 4 5 6 7 8
 1     x x x o    
 2     x x o      
 3     x o x x    
 4     x x o x    
 5       o o      
 6     o          
 7                
 8                
kuro 5 6
   1 2 3 4 5 6 7 8
 1     x x x o    
 2     x x o      
 3     x o x x    
 4     x x x x    
 5       x x      
 6     o   x      
 7                
 8                
   1 2 3 4 5 6 7 8
 1     x x x o    
 2     x x o      
 3     x o x x    
 4     o x x x    
 5   o   x x      
 6     o   x      
 7                
 8                
kuro 2 4
   1 2 3 4 5 6 7 8
 1     x x x o    
 2     x x o      
 3     x o x x    
 4   x x x x x    
 5   o   x x      
 6     o   x      
 7                
 8                
   1 2 3 4 5 6 7 8
 1     x x x o    
 2     x x o      
 3     x o x o    
 4   x x x x x o  
 5   o   x x      
 6     o   x      
 7                
 8                
kuro 1 6
   1 2 3 4 5 6 7 8
 1     x x x o    
 2     x x o      
 3     x o x o    
 4   x x x x x o  
 5   x   x x      
 6 x   o   x      
 7                
 8                
   1 2 3 4 5 6 7 8
 1     x x x o    
 2     x x o      
 3     x o x o    
 4 o o o o o o o  
 5   o   x x      
 6 x   o   x      
 7                
 8                
kuro 1 5
   1 2 3 4 5 6 7 8
 1     x x x o    
 2     x x o      
 3     x o x o    
 4 o x o o o o o  
 5 x o   x x      
 6 x   o   x      
 7                
 8                
   1 2 3 4 5 6 7 8
 1     x x x o    
 2     x x o      
 3     x o x o    
 4 o x o o o o o  
 5 x o   x o      
 6 x   o   x o    
 7                
 8                
kuro 1 3
   1 2 3 4 5 6 7 8
 1     x x x o    
 2     x x o      
 3 x   x o x o    
 4 x x o o o o o  
 5 x o   x o      
 6 x   o   x o    
 7                
 8                
   1 2 3 4 5 6 7 8
 1     x x x o    
 2     x x o      
 3 x   x o x o    
 4 x x o o o o o  
 5 x o o o o      
 6 x   o   x o    
 7                
 8                
kuro 2 3
   1 2 3 4 5 6 7 8
 1     x x x o    
 2     x x o      
 3 x x x o x o    
 4 x x x o o o o  
 5 x o o x o      
 6 x   o   x o    
 7                
 8                
   1 2 3 4 5 6 7 8
 1     x x x o    
 2     x x o      
 3 x x x o x o    
 4 x x x o o o o  
 5 x o o o o      
 6 x   o o o o    
 7                
 8                
kuro 2 6
   1 2 3 4 5 6 7 8
 1     x x x o    
 2     x x o      
 3 x x x o x o    
 4 x x x x o o o  
 5 x x x o o      
 6 x x o o o o    
 7                
 8                
   1 2 3 4 5 6 7 8
 1   o o o o o    
 2     o x o      
 3 x x x o x o    
 4 x x x x o o o  
 5 x x x o o      
 6 x x o o o o    
 7                
 8                
kuro 6 5
   1 2 3 4 5 6 7 8
 1   o o o o o    
 2     o x o      
 3 x x x o x o    
 4 x x x x o o o  
 5 x x x x x x    
 6 x x o o o o    
 7                
 8                
   1 2 3 4 5 6 7 8
 1   o o o o o    
 2 o   o x o      
 3 x o x o x o    
 4 x x o x o o o  
 5 x x x o x x    
 6 x x o o o o    
 7                
 8                
kuro 1 1
   1 2 3 4 5 6 7 8
 1 x o o o o o    
 2 x   o x o      
 3 x o x o x o    
 4 x x o x o o o  
 5 x x x o x x    
 6 x x o o o o    
 7                
 8                
   1 2 3 4 5 6 7 8
 1 x o o o o o    
 2 x   o x o      
 3 x o x o x o    
 4 x x o x o o o  
 5 x x x o o o o  
 6 x x o o o o    
 7                
 8                
kuro 2 2
   1 2 3 4 5 6 7 8
 1 x o o o o o    
 2 x x x x o      
 3 x x x o x o    
 4 x x o x o o o  
 5 x x x o o o o  
 6 x x o o o o    
 7                
 8                
pass
   1 2 3 4 5 6 7 8
 1 x o o o o o    
 2 x x x x o      
 3 x x x o x o    
 4 x x o x o o o  
 5 x x x o o o o  
 6 x x o o o o    
 7                
 8                
```

# 成果物

https://paiza.io/projects/0L0BmxRKAdbOShRFNXSamA

以上。