-2

对不起之前的代码。这是计算机的代码。如果用户选择坐标 N1,那么它会随机选择另一个可以阻挡该位置的坐标。这是我试图用java制作的井字游戏的一部分。我无法在坐标上打印 x。如果你能帮助我,我将不胜感激。只是简单的ai。

here is the code:

import java.util.*;
public class compbrain{
public static void main(String [] args)
{
 char x = 'X';
 char [][] ar = new char [3][3];
 System.out.println("Player X: Enter the coordinate where you want an X placed. Example    1 0");
 int choice1 = input.nextInt();
 int choice2 = input.nextInt();
 char N = ar[choice1][choice2] = x;
  char N1 = ar[0][0];
  char N2 = ar[0][1];
  char N3 = ar[0][2];
  char N4 = ar[1][0];
  char N5 = ar[1][1];
  char N6 = ar[1][2];
  char N7 = ar[2][0];
  char N8 = ar[2][1];
  char N9 = ar[2][2];
 int r = (int)(Math.random() * 4);
 char [] arrayN1 = {N4,N7,N2,N3,N5,N9};


 if(N == N1){arrayN1[r] = x;}

 System.out.println("      0     1      2     ");
    System.out.println("   --------------------");
    System.out.println("0  |  "+N1+"  |   "+N4+"  |  "+N7+"  |");
    System.out.println("   --------------------");
    System.out.println("1  |  "+N2+"  |   "+N5+"  |  "+N8+"  |");
    System.out.println("   --------------------");
    System.out.println("2  |  "+N3+"  |   "+N6+"  |  "+N9+"  |");
    System.out.println("   --------------------");
}
}
4

1 回答 1

1

我想我现在知道你的问题是什么了。

这是一个解决方案:

// Setting a random field to 'o'
Random r = new Random();
int a = r.nextInt(3);
int b = r.nextInt(3);

ar[a][b] = o;

// Printing the fields
System.out.println("      0     1      2     ");
System.out.println("   --------------------");
System.out.println("0  |  " + ar[0][0] + "  |  " + ar[1][0] + "  |  " + ar[2][0] + "  |");
// and so on

arrayN1 从未包含任何数组,只是 ar[1][0] 和其他值的副本的副本。

通过更改 arrayN1 中的值,您永远不会更改某些 Nx 字符或 ar[][] 中的值

于 2011-10-22T20:45:13.773 回答