Finding the first empty index in an array and adding a value...

I am trying to write code for a simple Noughts and Crosses game. I have the client side with GUI running fine. My problem now is once i receive a message from the client side as to which play square has been selected i add it to an array as so;

if(clientMessage.equals("1"))

{

gameBoard[0] ='X';

generateMove();

}

I am then using generateMove()

to process a move from the server. Simply all i wish it to do is find the first index in the gameBoard array and add a 'O', then send the index value of the new addition to the array back to the client side so that the GUI can be updated and show the move just implemented. After checking if the gameBoard is not full and there is no winner yet the server side will then wait for the next message However my mind has gone blank. my incorrect none value returning code is as follows;

privateint generateMove()

{

for(int i = 0; i<9; i++)

{

if(gameBoard[i] ==' ')

{

gameBoard[i] ='O';

if(!isWinner('O') && !isFullBoard())

{

toClient.println(i);

}

}

}

}

am i right in presuming that...

for(int i = 0; i<9; i++)

{

if(gameBoard[i] ==' ')

{

//do stuff

}

returnint of some sort

}

..will check for the next empty index?

Or should i set up an instance variable of an integer at the start of the for loop and have it set as the value of i then use this in the if loop as the index, then again have it as the returned value at the end of the loops? If i do have it as a returned value then how do i catch this integer that will be floating around? i would presume it would just go into the previous code which calls generateMove()?

Thanks in advance

[2780 byte] By [ianpwelch85a] at [2007-11-27 4:41:45]
# 1

I think we probably need to see the other lines to do with gameBoard[], ie the declaration of it, and initialization.

Have you checked to make sure each entry in the array is set by default (and reset) to ' '? I'm guessing you would get a null pointer exception if you hadn't, but you might be catching this and doing nothing.

If you have, try debugging it by reading each character in turn from the array, casting it as an integer, and then printing out this integer to make sure it matches the number for ' ', which I think is 32.

abu5ea at 2007-7-12 9:53:11 > top of Java-index,Java Essentials,Java Programming...
# 2

import java.net.*;

import java.io.*;

public class GameSession implements Runnable

{

private boolean DEBUG = true; //used to control debugging output

private int squaresLeft;//how many unplayed squares are there?

private String clientMessage; //the last message the client sent

private boolean clientQuit;//has the player quit?

// Use the following for streams based communication

private PrintWriter toClient;

private BufferedReader fromClient;

private String outcome;//win, lose or draw for the client

// Store the state of the board of 3x3 squares in a 1 x 9 char array

// Each square contains player ID (X or O) if taken

private char[] gameBoard;

private boolean gameOver;//is the gameOver?

private ServerSocket ss;

private Socket socket;

//Streams for connections

private InputStream is;

private OutputStream os;

static final int PORT_NUMBER = 3000;

/*

* write a constructor that receives a reference to the client's socket

* and initialises input and output streams

*/

public GameSession(Socket sc)

{

char[] gameBoard = {' ',' ',' ',' ',' ',' ',' ',' ',' '};

socket = sc;

try{

fromClient

= new BufferedReader(new InputStreamReader(socket.getInputStream()));

toClient = new PrintWriter(socket.getOutputStream(), true);

}

catch (IOException e)

{

System.out.println("and Again ...." + e);

}

}

//send initial message to client

public void sendInitialMessage()

{

System.out.println("sending start messages");

toClient.println("Start");

}

//get a message from the player

private void setClientMessage()

{

try

{

clientMessage = fromClient.readLine();

if(DEBUG)

{

System.out.println("got message " + clientMessage);

}

processMessage();

}

catch (IOException e)

{

System.out.println("Bugger " + e);

}

}

//process the last message received from a client

private void processMessage()

{

if(clientMessage.equals("Play"))

{

toClient.println("Turn");

messageToClient("Turn");

}

if(clientMessage.equals("Exit"))

{

clientQuit = true;

doQuit();

}

if(clientMessage.equals("1"))

{

gameBoard[0] = 'X';

// generateMove();

}

if(clientMessage.equals("2"))

{

gameBoard[1] = 'X';

// generateMove();

}

}

//process a request to Quit

private void doQuit()

{

try

{

toClient.close();

os.close();

}

catch (IOException e)

{

System.out.println("Oh no " + e);

}

}

//close the client's connections

public void closeClientComms()

{

}

//process a move from the client

private void doTurn()

{

}

//work out the client's move and update the state of the game

private void doClientMove()

{

int imove = -1;

if(DEBUG)

{

System.out.println("move converted to integer is " + imove);

}

if (DEBUG)

{

displayBoard();

}

}

//see if the player with ID playerID ('X' or 'O') has won, or if it is a draw;

//otherwise the outcome is unchanged. The outcome reflects the client status

private void setOutcome(char playerID)

{

}

private void doServerMove()

{

if (DEBUG)

{

displayBoard();

}

}

//the server makes a move - dummy implementation

private int generateMove()

{

//This is where i am having lots of problems

}

//a method to send a message to the client

public void messageToClient(String message)

{

toClient.println(message);

if(DEBUG)

{

System.out.println("sending " + message);

}

if(DEBUG)

{

System.out.println("sent " + message);

}

}

//are there any squares left to play?

private boolean isFullBoard()

{

if(gameBoard.length == 9)

{

return true;

}

else

{

return false;

}

}

/************************** PROVIDED METHODS *********************************/

// The run method is provided to show you the game logic

// and is used to start the processing of a game

// can't throw an IOException if implementing Runnable

public void run()

{

sendInitialMessage();

//the following style of loop is used when you want to execute

//the body of the loop at least once

do

{

setClientMessage();

processMessage();

}

while (!clientQuit && !gameOver);

System.out.println("Game over"); // end of game

while(!clientQuit) //game over but client hasn't pressed quit

{

//client can still press QUIT

setClientMessage();

processMessage();

}

closeClientComms();

} // end method run

//method to check for an endgame position -

//We assume that the player ID char ('X' or 'O') is stored whenever a move is played

private boolean isWinner(char playerID)

{

boolean win = false;

int winVal = 3*playerID;

if (gameBoard[0] + gameBoard[1] + gameBoard[2] == winVal)

win = true;

else if (gameBoard[0] + gameBoard[4] + gameBoard[8] == winVal)

win = true;

else if (gameBoard[0] + gameBoard[3] + gameBoard[6] == winVal)

win = true;

else if (gameBoard[6] + gameBoard[4] + gameBoard[2] == winVal)

win = true;

else if (gameBoard[6] + gameBoard[7] + gameBoard[8] == winVal)

win = true;

else if (gameBoard[2] + gameBoard[5] + gameBoard[8] == winVal)

win = true;

else if (gameBoard[1] + gameBoard[4] + gameBoard[7] == winVal)

win = true;

else if (gameBoard[3] + gameBoard[4] + gameBoard[5] == winVal)

win = true;

if(DEBUG)

{

System.out.println("win for " + playerID + " is " + win);

}

return win;

}

//method to display the game board, for debugging purposes.

//This method should not be called during normal play.

public void displayBoard()

{

for(int i = 0; i < gameBoard.length; i++)

{

System.out.print(gameBoard[i] + " ");

if((i+1) % 3 == 0)

{

System.out.println();

}

}

}

ianpwelch85a at 2007-7-12 9:53:11 > top of Java-index,Java Essentials,Java Programming...
# 3

I think the problem is here:

private char[] gameBoard;

Declares a global variable gameBoard,

then:

public GameSession(Socket sc)

{

char[] gameBoard = {' ',' ',' ',' ',' ',' ',' ',' ',' '};

declares a DIFFERENT char array, and then initializes it.

Because of this, all other methods referring to gameBoard are referring to the one that hasn't been initialized.

Replace the above with:

public GameSession(Socket sc)

{

gameBoard = {' ',' ',' ',' ',' ',' ',' ',' ',' '};

and that *should* solve the issue.

abu5ea at 2007-7-12 9:53:11 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks for the advice, to some extent this was the problem but i also found several other areas that didn't do precisly what i intended.
ianpwelch85a at 2007-7-12 9:53:11 > top of Java-index,Java Essentials,Java Programming...