1

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

2 Answers 2

3

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);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

So it is saying that <Spot> is missing a 'symbol'. How would I fill this? Thanks. I have never used < > these before
@pewpew <> is java generics, check out this docs.oracle.com/javase/tutorial/java/generics for more information on generics; Spot is a simple class I created to wrap row and col. Have updated my code
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);

1 Comment

Thank you for your input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.