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

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

How to invert the output in Nim Game?

问题

这是我的代码

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

它会打印出以下样式:

  1. XXX|XXX
  2. XXX|XXX XXX|XXX
  3. XXX|XXX XXX|XXX XXX|XXX
  4. 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

  1. XXX|XXX
  2. XXX|XXX XXX|XXX
  3. XXX|XXX XXX|XXX XXX|XXX
  4. XXX|XXX XXX|XXX XXX|XXX XXX|XXX

This is my code

  1. System.out.print(&quot;&quot;);
  2. for(int i = 0; i &lt; 4; i++) {
  3. //System.out.println(i);
  4. for(int j = 0; j &lt; numbers[i];j++) {
  5. //System.out.println(j);
  6. System.out.print(&quot; XXX|XXX &quot;);
  7. }
  8. System.out.println(&quot; &quot;);
  9. }

it prints this

  1. XXX|XXX
  2. XXX|XXX XXX|XXX
  3. XXX|XXX XXX|XXX XXX|XXX
  4. XXX|XXX XXX|XXX XXX|XXX XXX|XXX

suggestons?

答案1

得分: 2

  1. String[][] output = {
  2. {" ", " ", " ", "XXX|XXX"},
  3. {" ", " ", "XXX|XXX", "XXX|XXX"},
  4. {" ", "XXX|XXX", "XXX|XXX", "XXX|XXX"},
  5. {"XXX|XXX", "XXX|XXX", "XXX|XXX", "XXX|XXX"}
  6. };
  7. int tallestPileLevel = Arrays.stream(numbers).max().getAsInt();
  8. for (int level = tallestPileLevel; level > 0; level--) {
  9. for (int i = 0; i < numbers.length; i++) {
  10. if (numbers[i] < level) {
  11. System.out.print(air);
  12. } else {
  13. System.out.print(stone);
  14. }
  15. }
  16. System.out.println();
  17. }
英文:

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

  1. String[][] output = {
  2. {&quot; &quot;, &quot; &quot;, &quot; &quot;, &quot;XXX|XXX&quot;},
  3. {&quot; &quot;, &quot; &quot;, &quot;XXX|XXX&quot;, &quot;XXX|XXX&quot;},
  4. {&quot; &quot;, &quot;XXX|XXX&quot;, &quot;XXX|XXX&quot;, &quot;XXX|XXX&quot;},
  5. {&quot;XXX|XXX&quot;, &quot;XXX|XXX&quot;, &quot;XXX|XXX&quot;, &quot;XXX|XXX&quot;}
  6. };

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:

  1. 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:

  1. import java.util.*;
  2. public class PrintNim {
  3. public static void main(String[] args) {
  4. int[] numbers = {1, 2, 3, 4};
  5. int tallestPileLevel = Arrays.stream(numbers).max().getAsInt();
  6. String stone = &quot; XXX|XXX &quot;;
  7. String air = &quot; &quot;;
  8. for (int level = tallestPileLevel; level &gt; 0; level--) {
  9. for(int i = 0; i &lt; numbers.length; i++) {
  10. if (numbers[i] &lt; level) {
  11. System.out.print(air);
  12. }
  13. else {
  14. System.out.print(stone);
  15. }
  16. }
  17. System.out.println();
  18. }
  19. }
  20. }

答案2

得分: 2

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

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

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

输出:

  1. XXX|XXX
  2. XXX|XXX XXX|XXX
  3. XXX|XXX XXX|XXX XXX|XXX
  4. 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.

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

output:

  1. XXX|XXX
  2. XXX|XXX XXX|XXX
  3. XXX|XXX XXX|XXX XXX|XXX
  4. 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:

确定