Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 468691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:42:26+00:00 2026-05-12T23:42:26+00:00

The goal of the assignment that I’m currently working on for my Data Structures

  • 0

The goal of the assignment that I’m currently working on for my Data Structures class is to create a of Quantum Tic Tac Toe with an AI that plays to win.

Currently, I’m having a bit of trouble finding the most efficient way to represent states.

Overview of current Structure:

AbstractGame

  • Has and manages AbstractPlayers (game.nextPlayer() returns next player by int ID)
  • Has and intializes AbstractBoard at the beginning of the game
  • Has a GameTree (Complete if called in initialization, incomplete otherwise)

AbstractBoard

  • Has a State, a Dimension, and a Parent Game
  • Is a mediator between Player and State, (Translates States from collections of rows to a Point representation
  • Is a StateConsumer

AbstractPlayer

  • Is a State Producer
  • Has a ConcreteEvaluationStrategy to evaluate the current board

StateTransveralPool

  • Precomputes possible transversals of “3-states”.
  • Stores them in a HashMap, where the Set contains nextStates for a given “3-state”

State

  • Contains 3 Sets — a Set of X-Moves, O-Moves, and the Board
  • Each Integer in the set is a Row. These Integer values can be used to get the next row-state from the StateTransversalPool

SO, the principle is
Each row can be represented by the binary numbers 000-111, where 0 implies an open space and 1 implies a closed space.

So, for an incomplete TTT board:

From the Set<Integer> board perspective:
X_X  R1 might be: 101
OO_  R2 might be: 110
X_X  R3 might be: 101, where 1 is an open space, and 0 is a closed space

From the Set<Integer> xMoves perspective:
X_X  R1 might be: 101
OO_  R2 might be: 000
X_X  R3 might be: 101, where 1 is an X and 0 is not

From the Set<Integer> oMoves perspective:
X_X  R1 might be: 000
OO_  R2 might be: 110
X_X  R3 might be: 000, where 1 is an O and 0 is not

Then we see that x{R1,R2,R3} & o{R1,R2,R3} => board{R1,R2,R3}

The problem is quickly generating next states for the GameTree. If I have player Max (x) with board{R1,R2,R3}, then getting the next row-states for R1, R2, and R3 is simple..

Set<Integer> R1nextStates = StateTransversalPool.get(R1); 

The problem is that I have to combine each one of those states with R1 and R2.

Is there a better data structure besides Set that I could use? Is there a more efficient approach in general? I’ve also found Point<->State mediation cumbersome. Is there another approach that I could try there?

Thanks!

Here is the code for my ConcretePlayer class. It might help explain how players produce new states via moves, using the StateProducer (which might need to become StateFactory or StateBuilder).

public class ConcretePlayerGeneric extends AbstractPlayer {

 @Override
 public BinaryState makeMove() {
  // Given a move and the current state, produce a new state
  Point playerMove = super.strategy.evaluate(this);
  BinaryState currentState = super.getInGame().getBoard().getState();


  return StateProducer.getState(this, playerMove, currentState);
 }
}

EDIT: I’m starting with normal TTT and moving to Quantum TTT. Given the framework, it should be as simple as creating several new Concrete classes and tweaking some things.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:42:27+00:00Added an answer on May 12, 2026 at 11:42 pm

    My suggestion:

    • Consider representing individual squares rather than rows, whereby +1 == O, -1 == X and 0 implies an empty square. This allows you to detect an end state by checking whether the sum of a horizontal, vertical or diagonal row equals +3 or -3.
    • Secondly “flatten” this 2D 3×3 matrix into a single array whereby elements[0-2] represent the first row, elements[3-5] represent the second row and elements[6-8] represent the third row.
    • Use either recursion or an iterative approach to generate subsequent game states given the current state of the board.

    EDIT

    I got bored and so decided to write some “toy code” to implement the game board, including methods to determine if it is in a terminal state and to generate the set of board states after the next move is made. It should generalise to any size board although I haven’t tried. Enjoy …

    Sample Output

    $ java Board
    Creating board:
    
    ---
    ---
    ---
    
    Initialising board:
    
    -OX
    O--
    XO-
    Terminal state: false
    
    
    Generating next move states:
    
    XOX
    O--
    XO-
    
    -OX
    OX-
    XO-
    
    -OX
    O-X
    XO-
    
    -OX
    O--
    XOX
    

    Code

    import java.util.List;
    import java.util.LinkedList;
    import java.util.Random;
    
    public class Board {
        private final int[] squares;
    
        public Board() {
        this.squares = new int[9];
        }
    
        protected Board(int[] squares) {
        this.squares = squares;
        }
    
        public void init() {
        Random rnd = new Random();
        int turn = 1; // 'O' always goes first.
    
            for (int i=0; i<squares.length; ++i) {
            double d = rnd.nextDouble();
    
            if (d < 0.75) {
            squares[i] = turn;
            turn = turn == 1 ? -1 : 1; // Flip to other player's turn.
            } else {
            squares[i] = 0; // Empty square.
            }
    
            if (isTerminalState()) {
            break;
            }
        }
        }
    
        public boolean isTerminalState() {
        boolean ret = false;
    
        boolean foundEmpty = false;
        int hSum = 0;
        int[] vSum = new int[3];
    
        for (int i=0; i<squares.length; ++i) {
            hSum += squares[i];
    
            if (isWinningRow(hSum)) {
            ret = true;
            break;
            } else if (i == 2 || i == 5) {
            hSum = 0;
            }
    
            int col = i % 3;
            vSum[col] += squares[i];
    
            if (isWinningRow(vSum[col])) {
            ret = true;
            break;
            }
    
            if (squares[i] == 0) {
            foundEmpty = true;
            }
        }
    
        if (!ret) {
            if (!foundEmpty) {
            ret = true;
            } else {
            int diag1 = 0;
            int diag2 = 0;
            int rowSz = (int)Math.sqrt(squares.length);
    
            for (int i=0; i<squares.length; ++i) {
                if (i % (rowSz + 1) == 0) {
                diag1 += squares[i];
    
                if (isWinningRow(diag1)) {
                    ret = true;
                    break;
                }
                }
    
                if (i > 0 && i % (rowSz - 1) == 0) {
                diag2 += squares[i];
    
                if (isWinningRow(diag2)) {
                    ret = true;
                    break;
                }
                }
            }
            }
        }
    
        return ret;
        }
    
        private boolean isWinningRow(int rowSum) {
        return rowSum == 3 || rowSum == -3;
        }
    
        public List<Board> getNextStates() {
        List<Board> ret = new LinkedList<Board>();
    
        int tmp = 0;
        for (int i=0; i<squares.length; ++i) {
            tmp += squares[i];
        }
    
        // Next turn is 'O' (i.e. +1) if the board sums to 0.
        // Otherwise it's 'X's turn.
        int turn = tmp == 0 ? 1 : -1;
    
        if (!isTerminalState()) {
            for (int i=0; i<squares.length; ++i) {
            if (squares[i] == 0) { // Empty square          
                int[] squaresA = new int[squares.length];
                System.arraycopy(squares, 0, squaresA, 0, squares.length);
                squaresA[i] = turn;
                ret.add(new Board(squaresA));
            }
            }
        }
    
        return ret;
        }
    
        public String toString() {
        StringBuilder sb = new StringBuilder();
    
        for (int i=0; i<squares.length; ++i) {
            if (squares[i] == 1) {
            sb.append('O');
            } else if (squares[i] == -1) {
            sb.append('X');
            } else {
            assert squares[i] == 0;
            sb.append('-');
            }
    
            if (i == 2 || i == 5) {
            sb.append('\n');
            }
        }
    
        return sb.toString();
        }
    
        public static void main(String[] args) {
        System.err.println("Creating board:\n");
        Board bd = new Board();
        System.err.println(bd);
    
        System.err.println("\nInitialising board:\n");
        bd.init();
        System.err.println(bd);
        System.err.println("Terminal state: " + bd.isTerminalState() + '\n');
    
        System.err.println("\nGenerating next move states:\n");
        List<Board> nextStates = bd.getNextStates();
    
        for (Board bd1 : nextStates) {
            System.err.println(bd1.toString() + '\n');
        }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I'm trying to do is create a class that I can quickly attach
Goal: Create Photomosaics programmatically using .NET and C#. Main reason I'd like to do
Goal: to programmatically determine the sizes (in bytes) of the fields of a class.
My goal here is to create a very simple template language. At the moment,
my goal is to write a stored proc that can collect all field values
The goal: To create a .NET dll i can reference from inside SQL Server
I have three questions regarding a homework assignment for C++. The goal was to
I've this question from an assignment to create a Store which rent out books,
Hey all, i've been working on this small project in XNA the goal is
Have a homework assignment in which I'm supposed to create a vector of pointers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.