Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Multithreading and Game

807580Apr 16 2010 — edited Apr 16 2010
Hi guys,

I am in my first year of Java programming I have sort of hit a roadblock with an assignment we were given.

The assignment is about networking and the game that the players will play is Snakes and Ladders. I have successfully made the snakes and ladders game to work in the class Main.

I then created 2 more classes to allow the multiple players to connect and play the game. The class Server and the class Client.

Unfortunately at the moment I can only get 1 client to connect to the Server and I don't know how to get multiple connections.
I tried to re-draw the Gui for every new connection but I am not quite sure how to get each player to play the game and see the moves of the other players. Any ideas?

Class Main:_
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.image.*;

public class Main implements ActionListener{

    boolean buttonPressed = false;
    SnakesAndLaddersGUI gui;
    int[] pos;
    JButton go = new JButton("Roll Dice");

    public Main() {

        gui = new SnakesAndLaddersGUI();
        pos = new int[5];

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(450, 450);

        JPanel controlPanel = new JPanel();
        controlPanel.setSize(450, 100);

        go.addActionListener(this);
        controlPanel.add(go);

        f.getContentPane().add(gui, BorderLayout.CENTER);
        f.getContentPane().add(controlPanel, BorderLayout.SOUTH);

        f.setVisible(true);


        gui.setNumberOfPlayers(5);

        for (int i = 0; i < 5; i++) {
            pos[i] = 0;
            gui.setPosition(i, 0);
        }

        while (true) {
            if (buttonPressed) {
                move();
                buttonPressed = false;
            }
        }

    }

    public void actionPerformed(ActionEvent e) {
        buttonPressed = true;

    }

    public int PositionTest(int pos) {
        //the below conditions will lead to climbing ladders
        //or being eaten by snakes
        if (pos == 6) {
            return 16;
        } else if (pos == 11) {
            return 49;
        } else if (pos == 14) {
            return 4;
        } else if (pos == 24) {
            return 87;
        } else if (pos == 31) {
            return 9;
        } else if (pos == 35) {
            return 54;
        } else if (pos == 21) {
            return 60;
        } else if (pos == 44) {
            return 26;
        } else if (pos == 51) {
            return 67;
        } else if (pos == 56) {
            return 53;
        } else if (pos == 62) {
            return 19;
        } else if (pos == 64) {
            return 42;
        } else if (pos == 6) {
            return 16;
        } else if (pos == 73) {
            return 92;
        } else if (pos == 84) {
            return 28;
        } else if (pos == 78) {
            return 100;
        } else if (pos == 98) {
            return 80;
        } else if (pos == 95) {
            return 75;
        } else if (pos == 91) {
            return 71;
        } else {
            return pos;
        }

    }

    public void move() {
        try {
            int dice;
           
              Thread.sleep(500);
            for (int i = 0; i < 5; i++) {
                dice = (int) ((Math.random() * 10) % 6) + 1;
                if (pos[i] + dice < 100) {
                    while (dice > 0) {
                        System.out.print(dice);

                        System.out.println("\t" + i + "," + pos);

gui.setPosition(i, pos[i]++);
dice--;
}
Thread.sleep(100);//wait a tenth of second before climbing
//a ladder or being eaten by a snake
if (dice == 0) {//when dice is zero, the pieces have completed
//their move. we'll now test the current position
pos[i] = PositionTest(pos[i]);//set the current position
//to the position where it should be moved to
gui.setPosition(i, pos[i]);
}
Thread.sleep(100);
} else if (pos[i] + dice == 100) {
gui.setPosition(i, 101);
System.out.println("Player " + i + " won ");
} else if (pos[i] == 100) {
gui.setPosition(i, 101);
System.out.println("Player " + i + " won");
}
}

} catch (Exception evt) {
System.out.println(evt.getMessage());
}
}

public static void main(String[] a) {
new Main();
}
}
_*Class Server:*_
import java.net.*;
import javax.swing.*;

public class Server extends Thread {

private final int PORT = 3000; //Port number
private ServerThread[] playerNumber = new ServerThread[5]; // number of people allowed to connect
private String threadName;

public Server()//Constructor
{


try {

ServerSocket ss = new ServerSocket(PORT);

int i = 0;
//wait for connections
while (true) {

Socket soc = ss.accept(); //Waits for client to connect
System.out.println("Connected Player " + i);
ServerThread s = new ServerThread(soc);
playerNumber[i] = s;
s.start();
threadName=s.getName();
System.out.print(threadName);
i++;
}


} catch (Exception e) {
e.printStackTrace(); //Displays Error if things go wrong....
}

}

public static void main(String args[]) {
new Server(); //Makes object (calls constructor)
}
}
*_Class ServerThread_:*
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Lord Ra
*/
class ServerThread extends Thread{

private PrintWriter out=null;
private BufferedReader in=null;
private Socket Soc;


public ServerThread(Socket soc) {
Soc=soc;
// Set up Buffered reader,writer

try {
out = new PrintWriter(Soc.getOutputStream(), true);
in=new BufferedReader(new InputStreamReader(Soc.getInputStream()));
communicateThread();
} catch (IOException ex) {
Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
}

}

public void communicateThread()
{
while(true)
{
try{

String s=in.readLine();
System.out.println(s);
out.println("Your are succssefully connected to server");
}
catch(IOException r)
{

}
}
}
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 14 2010
Added on Apr 16 2010
6 comments
1,301 views