2017-03-06 2 views
0

ここまではforループの抜粋です。問題はスペースを減らすことですエリキシル剤のループはありません。ボードを作るにはどうすればいいですか?

for i <- 0..(row-1) do 
    for j <- 0..(col-1) do 

    if j == 0 do 
    IO.write"+" 
    end 

IO.write "--+" 
end 
IO.puts "" 
columns= col+1 


for columns <- 1..columns do 
if columns == 1 do 
IO.write "|" 
end 

if spaces < 10 do 
IO.write " " 
IO.write spaces 
IO.write "|" 

end 

end 
spaces = spaces - 1 <- This won't decrement after it goes through the forloop again! 
end 

end 

私はこのようなものを出力するエリクサーでボードゲームを作る必要があります。

+---+---+---+ 
| 12| 11| 10| 
|B | | | 
+---+---+---+ 
| 7| 8| 9| 
| a | S| e | 
+---+---+---+ 
| 6| 5| 4| 
| e | L|Ad | 
+---+---+---+ 
| 1| 2| 3| 
| | | | 
+---+---+---+ 

私は基本的にループのために別の内ループのために使用して、次のコードを使用してJavaでそれをやったが、これは万能薬では不可能です。これにアプローチする方法はありますか?ありがとう。

EDIT:誰かが私のJavaコードを見たい場合は、ここにあります。

public class math { 
static int col = 10; 
static int row = 10; 
static String top= "---+"; 
static String side= "|"; 
static int spaces=col*row; 
static int columns; 
static int dash; 
static String player="A"; 
public static void main(String args[]){ 

    for (int i=0; i < row; i++){ 
     for (int j=0; j < col; j++){ 
      if (j==0) System.out.print("+"); 

      System.out.print(top); 


     } 

     System.out.print("\n"); 
     columns=col+1; 

     while(columns > 1){ 
     if(columns == col+1){ 
      System.out.print("|"); 
     } 
     if(spaces < 10){ 
     System.out.print(" "+spaces+"|");} 
     else if (spaces <100){ 
      System.out.print(" "+spaces+"|"); 
     } 
     else{ 
      System.out.print(""+spaces+"|"); 
     } 
     spaces--; 
     columns--;} 


     System.out.print("\n"); 
     columns=col+1; 
     while(columns >1){ 
      if (columns == col +1) {System.out.print("|");} 
     System.out.print(" |"); 
     columns--;} 
     System.out.print("\n"); 
     if(i == row-1){ 
      int botside = col; 
      while(botside !=0){ 
       if(botside == col){ 
        System.out.print("+"); 
       } 
      System.out.print(top); 
      botside--;} 
      } 
      } 

      } 
+1

あなたの質問を実行したときに、それはあなたの必要な結果を生成示しています。あなたが問題の中核に到達するのを単純化することができれば(ある順序でボードのフィールドにアクセスし、ある州全体を維持しようとしているように)、あなたはおそらく良い答えを得るでしょう。 – nietaki

答えて

1

あなたが好きなものを必要なものを達成するためにリスト内包のためfor special formを使用することができます。

for i <- 0..(row-1) do 
    for j <- 0..(col-1) do 
    # Rest of the code 
    end 
end 
+4

これは動作しますが、私はそれがforループではなく、動作が非常に異なっていることを指摘したいだけです。これは理解と呼ばれます。また、内包物を入れ子にする必要はありません。あなたはこのような何かをすることができます 'i - 0 ..(row-1)、j < - 0 ..(col-1)do ... end' –

+1

私は各ボックスに番号を付けることに問題があります。 spaces = row * colという変数を指定して、2x2ボードにはスペース= 4があり、次にそれぞれの理解ではスペースを表示するように指示し、最後にスペースを1ずつ減らすように指示します。私はこれを行うときに更新します。 –

+1

@bossrevs元の質問をElixirでのコードの試行で更新してください。 – mudasobwa

0

ここに私のソリューションです - あなたはあなたのデータを記入ボードテンプレートに基づいては。

defmodule Main do 

def main() do 

data = 
[ 
    "B ", " ", " ", 
    " a ", " S", " e ", 
    " e ", " L", "Ad ", 
    " ", " ", " ", 
    "" #required terminal 
] 

IO.puts print_board(data) 

end 

defp print_board(data) do 

board = """ 
+---+---+---+ 
| 12| 11| 10| 
| | | | 
+---+---+---+ 
| 7| 8| 9| 
| | | | 
+---+---+---+ 
| 6| 5| 4| 
| | | | 
+---+---+---+ 
| 1| 2| 3| 
| | | | 
+---+---+---+ 
""" 

String.split(board," ") # Splits board string into a list of strings 
|> Enum.zip( data) # interleaves with data - as list of tuples 
|> Enum.map(fn {a,b} -> a <> b end) # maps tuples back to merge the list. 
end 

end 

私のテストでは、それが今であるあなたはエリクサーにJavaでやっている文字列操作を翻訳についてのほとんどがあるとして、あなたはMain.main

+0

問題は、ボードが行と列をどのように置くかによって異なります。この場合は4 x 3ですが、任意のサイズのボードを入力できます。データリストのSやeなどのはしごの配置と同じです。 –

+0

@boss_revs入力=行、列、データ。私はデータが私が書いたような文字列として渡されると仮定できますか?それがどのようにフォーマットされていない場合? – GavinBrelstaff

関連する問題