I kind of want a variable to be global, except you can't do that in Java. I tried making it static instead, but it's still not working when I do that....

Here's the situation:

I am making a TicTacToe game that operates by creating a server and receiving requests from clients to play the game.

All player information (number of wins, number of losses, name, symbol, etc) is written to a file, for every player who plays the game.

The first thing I do when a client connects is ask him if he's a new or existing player.

Now, imagine this situation:

Two clients connect, and the server establishes a game for these two players. One of the client tells the server that he is an existing player. However, it turns out that there is someone else in a different Tic Tac Toe game who is currently playing as that player.

Obviously, then, I can't let this other guy connect and also be playing simultaneously under the same name.

The problem is this: how do I check that? The key here is that this is happening across games, and not among two players in the same game.


What I did was create an array of Strings called "playersInUse". Everytime someone starts a game, I add their name to this array. When they finish, I remove their name from the array.

The problem, however, is that this array is a data member of what? ....the Server class. Even though I only create one server socket, I create many instances of the Server class, since each instance is itself a game. Thus, each game will have its own copy of "playersInUse", and it won't work across games.

My solution was to make the data member static, but it still doesn't keep all the information - the array gets reset for each instance of the game.

Why is this happening??





And on a totally unrelated note, how do you delete a topic on this site? I tried to do it before when my question became moot, but all I could do was edit it....