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

  • SEARCH
  • Home
  • 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 8947283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:43:06+00:00 2026-06-15T12:43:06+00:00

I’m preparing application to play gomoku. I’ve created nice looking GUI, now I’m fighting

  • 0

I’m preparing application to play gomoku.

I’ve created nice looking GUI, now I’m fighting with core engine.

There is a class GameEngine which has synchronized methods to make moves.
There are also classes implementing IClient interface which are “players”.
First thing to do is to implement PlayerClient which is connected to the BoardPanel and moves when MouseListener inBoardPanel (representing gomoku board) sais to. I want my application to be open to implement also computer players. That’s why it’s so complicated.

So… problem:
My GameEngine class looks like this:

public class GameEngine {
    private IClient blackPlayer;
    private IClient whitePlayer;
    private GameState gameState;

    ...

    public void play() {
        blackPlayer.run();
        whitePlayer.run();
    }

    public synchronized void move(Move move) throws IllegalMoveException {
        gameState.move(move);
        if (move.isBlackMove()){
            whitePlayer.notify();
        }else{
            blackPlayer.notify();
        }
    }

    public synchronized void waitForYourTurn(boolean color){
        try {
            while (gameState.isBlackToMove()!=color) {
                wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

and my Client class has run() method:

@Override
    public void run() {
        while (gameState.getWinner() == 0) {
            move = null;
            boolean moved = false;
            while (!moved) {
                gameEngine.waitForYourTurn(black);
                try {
                    //waitForPickedMove();
                    gameEngine.move(move);
                    moved = true;
                } catch (IllegalMoveException e) {
                    e.printStackTrace();
                }
            }
        }
    }

In place, where I wrote “//waitForPickedMove()” Client should wait for user to click proper place on the board. So… there is a MouseListener in my Board class:

private class MoveMouseListener implements MouseListener {
        @Override
        public synchronized void mouseReleased(MouseEvent e) {
            if (gameState == null) {
                return;
            }

            if (currentMove != null) {
                movePicked();
                repaint();
            }
        }
    }

currentMove is of course move picked by user by clicking board. While movePicked() looks like this:

private synchronized void movePicked() {
    if (gameState.isBlackToMove() && blackClient != null) {
        blackClient.notify();
        clearCurrentMove();
    }
    if (!gameState.isBlackToMove() && whiteClient != null) {
        whiteClient.notify();
        clearCurrentMove();
    }
}

I tried to put wait() in many places. I tried to reverse control and put it into Board class, so it invoked methods in Client class (setCurrentMove(Move move)). Unfortunately I can’t reach what I want. I always get IllegalMonitorStateException or my GUI blocks.

I hope I explained my problem enough. I’d be thankful for any help.

EDIT:

public class MockComputerClient implements IClient {
    private GameState gameState;
    private GameEngine gameEngine;
    private boolean black;
    private ExecutorService executorService;
    private boolean myTurn = false;
    private int i = 3;

    public MockComputerClient(GameEngine gameEngine, GameState gameState,
            boolean black) {
        this.gameEngine = gameEngine;
        this.gameState = gameState;
        this.black = black;
        executorService = new ExecutorService();
        executorService.run();
    }

    @Override
    public void enemyMoved() {
        myTurn = true;
        executorService.notify();
    }

    private class ExecutorService implements Runnable {

        @Override
        public void run() {
            while (gameState.getWinner() == 0) {
                try {
                    while (!myTurn) {
                        wait();
                    }
                    // Hardcoded Moves
                    gameEngine.move(new Move(black, 3, i++));
                    myTurn = false;
                } catch (InterruptedException | IllegalMoveException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

I’m pretty sure that I should synchronize sth, but I don’t really know what.

EDIT 2:

@Override
    public void enemyMoved() {
        myTurn = true;
        executorService.move();
    }

    private class ExecutorService implements Runnable {

        @Override
        public void run() {
            while (gameState.getWinner() == 0) {
                System.out.println("Calculating: j= " + j);
                if (j == 3)
                    j = 4;
                else
                    j = 3;

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }

        public void move() {
            while (myTurn) {
                try {
                    gameEngine.move(new Move(black, j, i++));
                    myTurn = false;
                } catch (IllegalMoveException e) {
                    e.printStackTrace();
                }
            }
        }

    }
  • 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-06-15T12:43:08+00:00Added an answer on June 15, 2026 at 12:43 pm

    There is no need for the Game Engine to constantly occupy a thread, blocking it when there’s nothing to do. You should redesign this to be reactive: code executes when there is an event, in the thread where the event handler is executing. Then you won’t need any wait–notify mechanisms.

    If you want to allow a Player implementation to take its time without occupying the calling thread, design the API with asynchronicity in mind: IClient will have a method representing “other player has moved”, which will not return player’s move immediately. The IClient will be expected to call a method on the Game Engine with its move.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.