如何在Nim游戏中反转输出?

huangapple go评论57阅读模式
英文:

How to invert the output in Nim Game?

问题

这是我的代码

System.out.print("");
for(int i = 0; i < 4; i++) {
    for(int j = 0; j < numbers[i];j++) {
        System.out.print("     XXX|XXX     ");
    }
    System.out.println(" ");
}

它会打印出以下样式:


     XXX|XXX     
     XXX|XXX     XXX|XXX     
     XXX|XXX     XXX|XXX     XXX|XXX     
     XXX|XXX     XXX|XXX     XXX|XXX     XXX|XXX
英文:

So I have the NIM Game Program. Basically, there are four piles and the two players have to keep taking stones from the piles until one of them lose. Right now my piles look like this in the output.

Board: [1, 2, 3, 4]

XXX|XXX represents one stone and one column of them represent a pile
but I need to modify it so it will look like this

                        XXX|XXX
                XXX|XXX XXX|XXX
        XXX|XXX XXX|XXX XXX|XXX
XXX|XXX XXX|XXX XXX|XXX XXX|XXX

This is my code

System.out.print(&quot;&quot;);
for(int i = 0; i &lt; 4; i++) {
    //System.out.println(i);
    for(int j = 0; j &lt; numbers[i];j++) {
        //System.out.println(j);
        System.out.print(&quot; XXX|XXX &quot;);
    }
    System.out.println(&quot; &quot;);
}

it prints this

XXX|XXX

XXX|XXX XXX|XXX

XXX|XXX XXX|XXX XXX|XXX

XXX|XXX XXX|XXX XXX|XXX XXX|XXX

suggestons?

答案1

得分: 2

String[][] output = {
    {"       ", "       ", "       ", "XXX|XXX"},
    {"       ", "       ", "XXX|XXX", "XXX|XXX"},
    {"       ", "XXX|XXX", "XXX|XXX", "XXX|XXX"},
    {"XXX|XXX", "XXX|XXX", "XXX|XXX", "XXX|XXX"}
};

int tallestPileLevel = Arrays.stream(numbers).max().getAsInt();

for (int level = tallestPileLevel; level > 0; level--) {
    for (int i = 0; i < numbers.length; i++) {
        if (numbers[i] < level) {
            System.out.print(air);
        } else {
            System.out.print(stone);
        }
    }
    System.out.println();
}
英文:

First, imagine your output as a virtual 2D String array, as hinted by @gmdev in his answer:

String[][] output = {
    {&quot;       &quot;, &quot;       &quot;, &quot;       &quot;, &quot;XXX|XXX&quot;},
    {&quot;       &quot;, &quot;       &quot;, &quot;XXX|XXX&quot;, &quot;XXX|XXX&quot;},
    {&quot;       &quot;, &quot;XXX|XXX&quot;, &quot;XXX|XXX&quot;, &quot;XXX|XXX&quot;},
    {&quot;XXX|XXX&quot;, &quot;XXX|XXX&quot;, &quot;XXX|XXX&quot;, &quot;XXX|XXX&quot;}
};

Next, you need to somehow find out how tall your highest pile is. Here's one way to do so in Java 8 or later:

int tallestPileLevel = Arrays.stream(numbers).max().getAsInt();

Then you render your virtual 2D array one line at a time.

The outer loop descends down your pile levels and the inner loop prints:

  • air (&quot; &quot;) --> if the i-th pile doesn't reach the current level
  • stone (&quot;XXX|XXX&quot;) --> otherwise

Here's a working example:

import java.util.*;
public class PrintNim {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4};
        int tallestPileLevel = Arrays.stream(numbers).max().getAsInt();
        
        String stone = &quot; XXX|XXX &quot;;
        String air   = &quot;         &quot;;
        
        for (int level = tallestPileLevel; level &gt; 0; level--) {
            for(int i = 0; i &lt; numbers.length; i++) {
                if (numbers[i] &lt; level) {
                    System.out.print(air);
                }
                else {
                    System.out.print(stone);
                }
            }
            System.out.println();
        }
    }
}

答案2

得分: 2

使用另一个 for 循环来设置您需要堆叠石头的空格数。我创建了一个与1块石头长度相同的字符串。这个循环将这个字符串重复3次,然后两次,依此类推。

我添加了代码注释,以帮助解释。

public static void main(String[] args) {
    System.out.print("");
    int[] numbers = {1, 2, 3, 4};
    int[] spaceNum = {3, 2, 1, 0}; //要堆叠x所需的空格数,先是3个空格,然后是2个,依此类推。
    String spaces = "         "; //长度为1的石头,即 " XXX|XXX "
    
    for(int i = 0; i < 4; i++) {
        //此循环使用spaceNum来决定在下一个循环写入石头之前需要缩进多远
        for(int k = spaceNum[i]; k > 0; k--) {
            System.out.print(spaces);
        }
        for(int j = 0; j < numbers[i]; j++) {
            System.out.print(" XXX|XXX ");
        }
        System.out.println(" ");
    }
}

输出:

                                XXX|XXX  
                       XXX|XXX  XXX|XXX  
              XXX|XXX  XXX|XXX  XXX|XXX  
     XXX|XXX  XXX|XXX  XXX|XXX  XXX|XXX  
英文:

Use another for loop to set however many spaces you need to stack the stones. I made a string that is the same length as 1 stone. The for loop repeats this string 3 times, then twice, etc.

I added code comments to try to help with the explanation.

public static void main(String[] args) {
	System.out.print(&quot;&quot;);
    int[] numbers = {1, 2, 3, 4};
    int[] spaceNum = {3, 2, 1, 0}; //to stack the x&#39;s you need 3 spaces, then 2, etc.
    String spaces = &quot;         &quot;; //length of 1 stone, i.e &quot; XXX|XXX &quot;
    
	for(int i = 0; i &lt; 4; i++) {
		//this loop uses sapceNum to decide how far to indent before the next loop writes the stones
	    for(int k = spaceNum[i]; k &gt; 0; k--) {
    		System.out.print(spaces);
	    }
	    for(int j = 0; j &lt; numbers[i]; j++) {
	        System.out.print(&quot; XXX|XXX &quot;);
	    }
	    System.out.println(&quot; &quot;);
	}
}

output:

                            XXX|XXX  
                   XXX|XXX  XXX|XXX  
          XXX|XXX  XXX|XXX  XXX|XXX  
 XXX|XXX  XXX|XXX  XXX|XXX  XXX|XXX  

huangapple
  • 本文由 发表于 2020年10月16日 03:24:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64378418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定