I want to learn how to connect computers so I created a game for 2 players that was played on the same computer, now I want it to be played on a network. I have been trying to do this with sockets.
I have a server class:
public class WordsGameServer extends JFrame {
private LetterBank lBank;
private UsedWordsBank uWBank;
private WordsBank wBank;
private JTextArea output;
private Player players[];
private ServerSocket server;
public WordsGameServer()
{
super( "Words-Game-Server Server" );
lBank = new LetterBank();
uWBank = new UsedWordsBank();
wBank = new WordsBank();
players = new Player[2];
// set up ServerSocket
try {
server = new ServerSocket( 5000, 2 );
}
catch( IOException e ) {
e.printStackTrace();
System.exit( 1 );
}
output = new JTextArea();
getContentPane().add( output, BorderLayout.CENTER );
output.setText( "Server awaiting connections\n" );
setSize( 300, 300 );
show();
}
// wait for two connections so game can be played
public void execute()
{
for ( int i = 0; i < players.length; i++ ) {
try {
players[ i ] =
new Player( server.accept(), this, i );
players[ i ].start();
}
catch( IOException e ) {
e.printStackTrace();
System.exit( 1 );
}
}
synchronized ( players[ 0 ] ) {
players[ 0 ].threadSuspended = false;
players[ 0 ].notify();
}
}
public boolean isWordValid(String word, int player) {
if (!uWBank.isWordUsed(word) && wBank.doesWordExist(word))
{
if (!lBank.isWordValid(word))
{
if (player == 0)
{
InformationPane pane = new InformationPane("Player 1 LOSES!");
}
else
{
InformationPane pane = new InformationPane("Player 2 LOSES!");
}
}
else
{
if (player == 0)
players[0].updateScore();
else
players[1].updateScore();
uWBank.addNewWord(word);
}
}
return true;
}
public boolean gameOver()
{
// Place code here to test for a winner of the game
return false;
}
public static void main( String args[] )
{
WordsGameServer game = new WordsGameServer();
game.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
game.execute();
}
}
And a client class:
public class WordsGame extends javax.swing.JFrame implements Runnable{
private DataInputStream input;
private DataOutputStream output;
private Socket connection;
private short p1Score = 0, p2Score = 0;
/** Creates new form WordsGame */
public WordsGame() throws IOException {
initComponents();
try {
connection = new Socket(
InetAddress.getByName( "127.0.0.1" ), 5000 );
input = new DataInputStream(
connection.getInputStream() );
output = new DataOutputStream(
connection.getOutputStream() );
}
catch ( IOException e ) {
e.printStackTrace();
}
timer.start();
new Thread(this).start();
}
public static void main(String[] args) throws IOException {
new WordsGame().setVisible(true);
}
public void run ()
{
while (true)
{
try
{
String s = input.readUTF();
System.out.print(s);
if (s.equals("Player 1"))
jLabel3.setText("Player 1: " + ++p1Score);
else
jLabel4.setText("Player 2: " + ++p2Score);
output.writeUTF(s);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@Action
public void validateWord1() {
String word = jTextField1.getText();
try
{
output.writeUTF(word);
}
catch( IOException e )
{
e.printStackTrace();
}
}
}
There are labels, buttons, and a text box in this class, I just eliminated them to make the code smaller (I used netbeans). The problem is that for some reason the score of the players is not updated on both windows. The logic goes like this:
The server is ran, the connections are made, the server creates the players, a player types something in his/her text box and clicks submit word, the word is passed to a player class via an object of type DataOutputStream (output.write(word)) the player receives the word and uses control.isWordValid(word, player) [control is the name of the object that was created from the server class], this sees if the word is valid, if it is then the updateScore method from the player is called and this calls this: output.write("Player 1") if it was player 1, this is received by the client but for some reason just the window from which the word was typed is updated.