Results 1 to 2 of 2
  1. #1
    Greghan is offline Member
    Join Date
    Sep 2015
    Posts
    1
    Rep Power
    0

    Default Java multithreaded tic tac toe endGame button

    Hi,
    I'm new to this and need a little help. I'm creating a multithreaded tic tac toe program for Java. The whole thing works except for if the player O pushes the end game button. The boards reset, but I think it is getting locked because no matter who clicks on their boards there is no response. It works fine if player X pushes the button, and a new game starts. My game works to start a new round , winner gets to change its piece, and X can do end game and a new client can start. I will try to add the parts of my code that are relevant.
    This is the client's side when then endGame button is pushed to send message to the server:
    Java Code:
    case "End Game":    output.format("%d\n", 100);
                                        output.flush();
                                        closeConnection();
                                        break;
    This is the server side, what happens after button is pushed:
    Java Code:
    else if ( location == 100 ) // end game
            { 
                if ( currentPlayer == 0 )
                {
                    roundOver = true;
                    displayMessage("\nPlayer X has ended the game.");
                    players[1].sendMessage("Player X has ended the game.");
                    players[1].sendMessage("Please wait for new player to connect.");
                    //The server should then wait for a connection from another client so that a new game can begin.               
                    players[(currentPlayer+1)%2].sendMessage("restart");
                    players[currentPlayer].sendMessage("restart");
                    for(int x = 0;x<board.length;x++)
                    {
                        board[x] = "";
                    }// end for loop
     
                    try // wait for connection, create Player, start runnable
                    {
                        players[ 0 ] = new Player( server.accept(), 0 );
                        Player temp = players[0];
                        displayMessage("\n NEW EXECUTE Player Created!!: "+0);
                        displayMessage("\n NEW EXECUTE Player Number!!: "+temp.playerNumber);
                        displayMessage("\n NEW EXECUTE Player Mark!!: "+temp.mark);
                        runGame.execute( players[0] ); // execute player runnable
                    } // end try
                    catch ( IOException ioException ) 
                    {
                        ioException.printStackTrace();
                        System.exit( 1 );
                    } // end catch
     
                   // gameLock.lock();
                    try // allow x to go first
                    {
                        players[ PLAYER_X ].setSuspended( false ); // resume player X
                        otherPlayerConnected.signal(); // wake up player X's thread
                    } // end try
                    finally
                    {
                        gameLock.unlock(); // unlock game after signalling player X
                    } // end finally
     
                    gameOver = false;
                } // end if
     
                else
                {
                    roundOver = true;
                    displayMessage("\nPlayer O has ended the game.");
                    players[0].sendMessage("Player O has ended the game.");
                    players[0].sendMessage("Please wait for new player to connect.");
                    // The server should then wait for a connection from another client so that a new game can begin.
                    players[(currentPlayer+1)%2].sendMessage("restart");
                    players[currentPlayer].sendMessage("restart"); 
                    for(int x = 0;x<board.length;x++)
                    {
                        board[x] = "";
                    }// end for loop
     
                    try // wait for connection, create Player, start runnable
                    {
                        players[ 1 ] = new Player( server.accept(), 1 );
                        Player temp = players[1];
                        displayMessage("\n NEW EXECUTE Player Created!!: "+1);
                        displayMessage("\n NEW EXECUTE Player Number!!: "+temp.playerNumber);
                        displayMessage("\n NEW EXECUTE Player Mark!!: "+temp.mark);
                        runGame.execute( players[ 1 ] ); // execute player runnable
                    } // end try
                    catch ( IOException ioException ) 
                    {
                        ioException.printStackTrace();
                        System.exit( 1 );
                    } // end catch
     
                    //gameLock.lock();
                    try // allow x to go first
                    {
                        displayMessage("Entered Player O reset");
                        players[ PLAYER_X ].setSuspended( false ); // resume player X
                        otherPlayerConnected.signal(); // wake up player X's thread
                    } // end try
                    finally
                    {
                        gameLock.unlock(); // unlock game after signalling player X
                    } // end finally
     
                    gameOver = false;
                }// end else
                return true;
            }// end else if
    	else // move was not valid
                return false; // notify player that move was invalid
    roundOver is to stand for obviously the round and not the game
    The "restart" resets the board on the clients' side
    The small for loop resets the board on the server side
    The lock for x works but the one for o does nothing. Can anyone one give me any pointers. I've been trying to change my code around for days and cannot figure out why it will work for x but not for o. And everything else works, the game play, starting a new round, changing playing pieces, and x's endGame button press.
    Also sorry if I didn't post this right. I'm new at this.
    Thanks for any help.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    13

Similar Threads

  1. Replies: 0
    Last Post: 11-21-2012, 04:35 PM
  2. A Multithreaded HTTP Server using Sockets in Java
    By zameer in forum Reviews / Advertising
    Replies: 1
    Last Post: 05-30-2011, 06:51 PM
  3. How to debug multithreaded applications
    By nxhoaf in forum Eclipse
    Replies: 1
    Last Post: 03-25-2011, 04:41 AM
  4. Multithreaded search
    By xjx in forum Threads and Synchronization
    Replies: 2
    Last Post: 01-13-2010, 12:02 AM
  5. Generating Fibonacci Series with a Multithreaded Java Program
    By firesauce in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-20-2009, 07:26 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •