Random position in string array? Java

社会主义新天地 提交于 2019-12-13 02:01:41

问题


Working on building a tic tac toe game. I'm trying to use as simple of methods as possible. I'm currently working on getting the cpu to randomly choose a spot in the array to place the "O" at. How do I go about doing this? This is what I have so far.

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!                
            }
        }
}

回答1:


You need to find out the available spots on the board for computer to make a move. Then among the available spots you can randomly choose one to make a move.

In the below example, I use a list to store the available spots on the board and use Collections.shuffle() to randomize the list to achieve the purpose of making a random move.

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);
    }
}



回答2:


You can do it this way, generate two numbers between 0-2 and if place is not taken, place it there at index [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);


来源:https://stackoverflow.com/questions/33446884/random-position-in-string-array-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!