字符串数组中的随机位置?JAVA
正在制作一个tic-tac-toe游戏。我正在尝试使用尽可能简单的方法。我目前正在努力让cpu在阵列中随机选择一个点来放置“O”。我该怎么做呢?这就是我目前所拥有的字符串数组中的随机位置?JAVA,java,arrays,2d,Java,Arrays,2d,正在制作一个tic-tac-toe游戏。我正在尝试使用尽可能简单的方法。我目前正在努力让cpu在阵列中随机选择一个点来放置“O”。我该怎么做呢?这就是我目前所拥有的 import java.util.Scanner; import java.util.Random; public class Player { String player = "X"; String cpu = "O"; //private int[][] theBoard= new int [3][3]
import java.util.Scanner;
import java.util.Random;
public class Player
{
String player = "X";
String cpu = "O";
//private int[][] theBoard= new int [3][3] ;
private String[][] theBoard= {{" "," "," "}, {" "," "," "}, {" ", " "," "}};;
Random cpuInput = new Random();
private Board board1;
public static Scanner scan = new Scanner(System.in);
public Player(Board board, String inBoard )
{
theBoard = theBoard;
}
public void computerMove()
{
String spacing = " ";
for (int i = 0; i < theBoard.length; i++)
{
for (int j = 0; j < theBoard[i].length; j++)
{
//int random = cpuInput.nextInt(theBoard[i][j]);
theBoard[2][2] = (cpu); //STUCK HERE!
}
}
}
import java.util.Scanner;
导入java.util.Random;
公开课选手
{
字符串播放器=“X”;
字符串cpu=“O”;
//私有int[][]董事会=新int[3][3];
私有字符串[][]电路板={{,'','},{,'','},{,'','},{,'','};;
Random cpuInput=新随机();
私人董事会1;
公共静态扫描仪扫描=新扫描仪(System.in);
公共播放器(棋盘、内弦)
{
theBoard=theBoard;
}
公共空间移动()
{
字符串间距=”;
for(int i=0;i
您可以这样做,在0-2之间生成两个数字,如果未进行替换,则将其放置在索引[x][y]处
Random generator = new Random();
import java.util.*;
int x = generator.nextInt(3);
int y = generator.nextInt(3);
if place is not taken:
theBoard[x][y];
if place is taken generate again:
int x = generator.nextInt(3);
int y = generator.nextInt(3);
您可以这样做,在0-2之间生成两个数字,如果未进行替换,则将其放置在索引[x][y]处
Random generator = new Random();
import java.util.*;
int x = generator.nextInt(3);
int y = generator.nextInt(3);
if place is not taken:
theBoard[x][y];
if place is taken generate again:
int x = generator.nextInt(3);
int y = generator.nextInt(3);
你需要找出棋盘上的可用点,以便计算机进行移动。然后在可用点中,你可以随机选择一个进行移动 在下面的示例中,我使用一个列表来存储棋盘上的可用点,并使用Collections.shuffle()来随机化列表,以实现随机移动的目的
class Spot {
int row;
int col;
public Spot(int row, int col) {
this.row = row;
this.col = col;
}
}
public void computerMove() {
String spacing = " ";
// firstly finding out the available moves for the computer
List<Spot> availableSpots = new ArrayList<>();
for (int i = 0; i < theBoard.length; ++i) {
for (int j = 0; j < theBoard[i].length; ++j) {
if (theBoard[i][j].equals(spacing)) {
availableSpots.add(new Spot(i, j));
}
}
}
// if no available moves, do nothing, game has ended
if (availableSpots.isEmpty()) {
System.out.println("No more spots available on the board.");
return;
} else {
// shuffle the list so that it becomes randomly orderedd
Collections.shuffle(availableSpots);
// get the first one of the random list
Spot spot = availableSpots.get(0);
theBoard[spot.row][spot.col] = (cpu);
}
}
课堂现场{
int行;
int col;
公共点(国际行、国际列){
this.row=行;
this.col=col;
}
}
公共空间移动(){
字符串间距=”;
//首先找出计算机可用的移动方式
List availableSpots=new ArrayList();
对于(int i=0;i
您需要找出板上的可用点,以便计算机移动。然后在可用点中,您可以随机选择一个进行移动
在下面的示例中,我使用一个列表来存储棋盘上的可用点,并使用Collections.shuffle()来随机化列表,以实现随机移动的目的
class Spot {
int row;
int col;
public Spot(int row, int col) {
this.row = row;
this.col = col;
}
}
public void computerMove() {
String spacing = " ";
// firstly finding out the available moves for the computer
List<Spot> availableSpots = new ArrayList<>();
for (int i = 0; i < theBoard.length; ++i) {
for (int j = 0; j < theBoard[i].length; ++j) {
if (theBoard[i][j].equals(spacing)) {
availableSpots.add(new Spot(i, j));
}
}
}
// if no available moves, do nothing, game has ended
if (availableSpots.isEmpty()) {
System.out.println("No more spots available on the board.");
return;
} else {
// shuffle the list so that it becomes randomly orderedd
Collections.shuffle(availableSpots);
// get the first one of the random list
Spot spot = availableSpots.get(0);
theBoard[spot.row][spot.col] = (cpu);
}
}
课堂现场{
int行;
int col;
公共点(国际行、国际列){
this.row=行;
this.col=col;
}
}
公共空间移动(){
字符串间距=”;
//首先找出计算机可用的移动方式
List availableSpots=new ArrayList();
对于(int i=0;i
这是说缺少了一个“符号”。我该如何填写它?谢谢。我从来没有用过这些before@pewpew是java泛型,请查看这篇文章,了解有关泛型的更多信息;Spot是我创建的一个简单类,用于包装行和列。我更新了代码,所以它说缺少一个“符号”。我该如何填写它?谢谢。我有e从未使用过这些before@pewpew是java泛型,请查看本文以了解有关泛型的更多信息;Spot是我创建的一个简单类,用于包装行和列。更新了我的代码谢谢您的输入谢谢您的输入