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 4626948
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:29:43+00:00 2026-05-22T03:29:43+00:00

I am making a complex tic tac toe program that has a variable grid

  • 0

I am making a complex tic tac toe program that has a variable grid size and player amount. But one of my friends commented that it is a little slow respond after they make a move in a 64 by 64 space. I looked into it and I have found the issue, my program checks every button in the grid to see if it was clicked, then it checks every player to see who made the move. Once it has found both out, it moves on with life :D. But that can take some time when working with a larger grid. so I made an attempt at fixing it by putting in some “breaks;”, but they didn’t help to find it faster, only to stop looking faster.

public void actionPerformed(ActionEvent gridButtonClicked) {
        for (int i = 0; i < gridButton.length; i++) {
            if (gridButtonClicked.getSource() == gridButton[i]) {
                for(int a = 0;a < amountOfPlayers;a++){
                    if(turn == a) {
                        gridButtonOwner[i] = a + 1;
                        gridButton[i].setBackground(playerColors[a]);
                        gridButton[i].setEnabled(false);
                        System.out.println("Grid " + i + ": " + gridButtonOwner[i]);
                        break;
                    }
                }
                break;
            }
        }
    }

What I want to know is that if I can get the array number of the button clicked. Like if gridButtonClicked = gridButton[1], it would return the number 1, or if it equaled gridButton[2] it would return 2, etc.

  • gridButtonOwner is a int array,
  • gridButton is a jbutton array.
  • 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-22T03:29:44+00:00Added an answer on May 22, 2026 at 3:29 am

    I must say i don’t understand the need for the loops. In the first for-loop you’re getting the grid-button – but you know what this is because it’s the event source… You can just store a map of GridButton onto Integer to get the place in your array. Why loop to find it? In the second loop you’re looping until a == turn… Which means you already know what a is because it == turn. You should be able to remove the loops completely:

    // earlier on: myMap = new HashMap<GridButton, Integer>();
    public void actionPerformed(ActionEvent gridButtonClicked) {
        GridButton gridButton = gridButtonClicked.getSource();
        int i = myMap.get(gridButton);
        gridButtonOwner[i] = turn + 1;
        gridButton.setBackground(playerColors[turn]);
        gridButton.setEnabled(false);
        System.out.println("Grid " + i + ": " + gridButtonOwner[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a C++ program that has a large number of classes. In my
I'm making a WinForms app with a ListView set to detail so that several
I'm making a complex drawing using quartz based on passed in information. The only
I'm working on making a complex object designer compliant and was going to utilize
I am making a movement engine for my top-down 2D game, but I got
Making echo of a question around the web: Is the syntax for svn:ignore patterns
When making changes using SubmitChanges() , LINQ sometimes dies with a ChangeConflictException exception with
After making some changes in my models (eg. new field in a model and
After making a few modifications to a rails app I am tinkering on, railroad
Im making a small python script to upload files on the net. The script

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.