Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hangman.UI;
- public class Start {
- public static void main(String[] arg) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- JoinGame jg = new JoinGame();
- }
- });
- }
- }
- ====================================================
- ====================================================
- package hangman.UI;
- import hangman.Client.JoinClient;
- import hangman.server.GameServer;
- import java.awt.Image;
- import java.awt.Toolkit;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- import java.util.ArrayList;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- public class JoinGame extends JFrame implements MouseListener {
- JFrame jf;
- JButton server;
- JButton join;
- JLabel imgIcn;
- JTextField name;
- JLabel nameJL;
- String userName;
- public JoinGame() {
- Image icon = Toolkit.getDefaultToolkit().getImage("img0.png");
- jf = new JFrame();
- jf.setSize(230, 130);
- jf.setLayout(null);
- jf.setTitle("Hangman server");
- jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- jf.setResizable(false);
- jf.setLocationRelativeTo(this);
- jf.setVisible(true);
- jf.setIconImage(icon);
- server = new JButton("Start a new server");
- server.addMouseListener(this);
- server.setSize(150, 30);
- server.setLocation(5, 5);
- server.setVisible(true);
- server.setToolTipText("<html>   By clicking this button<br>you can start a new server </html>");
- join = new JButton("Join to a server");
- join.addMouseListener(this);
- join.setSize(150, 30);
- join.setLocation(5, 37);
- join.setVisible(true);
- join.setToolTipText("<html>You can join a game if you<br> know it's server IP address </html>");
- ImageIcon ii = new ImageIcon("iconImg.png");
- imgIcn = new JLabel("");
- imgIcn.setIcon(ii);
- imgIcn.setLocation(157, 5);
- imgIcn.setSize(60, 68);
- imgIcn.setVisible(true);
- nameJL = new JLabel("YOUR USERNAME:");
- nameJL.setSize(130, 10);
- nameJL.setLocation(5, 78);
- nameJL.setVisible(true);
- name = new JTextField();
- name.setSize(110, 18);
- name.setLocation(110, 75);
- name.setVisible(true);
- jf.add(server);
- jf.add(join);
- jf.add(imgIcn);
- jf.add(nameJL);
- jf.add(name);
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- if (e.getSource() == server) {
- try {
- setUsername(InetAddress.getLocalHost());
- } catch (UnknownHostException e1) {
- }
- } else if (e.getSource() == join) {
- setUsername();
- }
- }
- private void setUsername() {
- if (!name.getText().equals("")) {
- this.userName = name.getText();
- JoinClient jc = new JoinClient(this.userName);
- jf.dispose();
- } else {
- Toolkit.getDefaultToolkit().beep();
- Object[] options = { "try again", "cancel" };
- int answer = JOptionPane.showOptionDialog(null,
- "Please enter your user name", "SET USERNAME",
- JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
- null, options, options[0]);
- }
- }
- private void setUsername(InetAddress address) {
- if (!name.getText().equals("")) {
- this.userName = name.getText();
- JoinClient jc = new JoinClient(address, this.userName);
- jf.dispose();
- } else {
- Toolkit.getDefaultToolkit().beep();
- Object[] options = { "try again", "cancel" };
- int answer = JOptionPane.showOptionDialog(null,
- "Please enter your user name", "SET USERNAME",
- JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
- null, options, options[0]);
- }
- }
- @Override
- public void mousePressed(MouseEvent e) {
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- }
- =======================================================
- =======================================================
- package hangman.Client;
- import hangman.server.GameServer;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.io.IOException;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- public class JoinClient extends JFrame implements MouseListener {
- InetAddress serverInetAdd = null;
- String userName;
- JFrame jf;
- JTextField ipAddress;
- JLabel ipJL;
- JButton connect;// for the constructor which get inet from user
- public JoinClient(String userName) {
- this.userName = userName;
- jf = new JFrame();
- jf.setSize(260, 105);
- jf.setLayout(null);
- jf.setTitle("Join server");
- jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- jf.setResizable(false);
- jf.setLocationRelativeTo(this);
- jf.setVisible(true);
- ipJL = new JLabel("SERVER IP ADDRESS:");
- ipJL.setSize(160, 10);
- ipJL.setLocation(5, 10);
- ipJL.setVisible(true);
- ipAddress = new JTextField();
- ipAddress.setSize(103, 18);
- ipAddress.setLocation(135, 7);
- ipAddress.setVisible(true);
- connect = new JButton("connect");
- connect.setBounds(60, 50, 135, 22);
- connect.setVisible(true);
- connect.addMouseListener(this);
- jf.add(ipJL);
- jf.add(ipAddress);
- jf.add(connect);
- }
- public JoinClient(InetAddress iAdd, String userName) {
- GameServer gs = new GameServer(userName);
- try {
- Socket s = new Socket(iAdd, 4444);
- } catch (IOException e) {
- }
- this.serverInetAdd = iAdd;
- this.userName = userName;
- }
- private InetAddress getUserAddressInput() {
- InetAddress address = null;
- try {
- address = InetAddress.getByName(ipAddress.getText());
- } catch (UnknownHostException e) {
- JOptionPane.showMessageDialog(this,
- "Please enter a valid IP address", "WRONG IP", 0);
- ipAddress.setText(null);
- }
- return address;
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- if (e.getSource() == connect) {
- jf.dispose();
- this.serverInetAdd = getUserAddressInput();
- // Client c = new Client(serverInetAdd, 1);
- try {
- Socket s = new Socket(serverInetAdd, 4444);
- } catch (IOException e2) {
- }
- }
- }
- @Override
- public void mousePressed(MouseEvent e) {
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- }
- =========================================================
- =========================================================
- package hangman.Client;
- import java.awt.Button;
- import java.awt.TextField;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.Scanner;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- public class NetworkThread implements Runnable, ActionListener {
- Socket newClient;
- String userName;
- int id;
- Scanner sc = null;
- PrintWriter pw = null;
- JFrame jf;
- TextField object;
- Button[] chars;
- final int MESSAGE_LENGTH = 2;
- public NetworkThread(Socket newClient, String userName, int id) {
- this.userName = userName;
- this.newClient = newClient;
- this.id = id;
- jf = new JFrame(userName);
- jf.setSize(460, 350);
- jf.setLayout(null);
- jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- jf.setResizable(false);
- jf.setLocationRelativeTo(null);
- jf.setVisible(true);
- try {
- pw = new PrintWriter(newClient.getOutputStream());
- sc = new Scanner(newClient.getInputStream());
- } catch (IOException e) {
- }
- ConstructFrame();
- }
- @Override
- public void run() {
- if (id == 1)
- changeTurnAsAGuesser();
- else
- startConnectionForSecondPlayer();
- }
- private void ConstructFrame() {
- chars = new Button[26];
- int a = 'A';
- int j = 1;
- for (int i = 0; i < chars.length; i++) {
- if (i == 9)
- j++;
- else if (i == 18)
- j++;
- chars[i] = new Button(String.valueOf((char) a));
- chars[i].setBounds(21 * (i % 9) + j * 8, 21 * j, 20, 20);
- chars[i].setVisible(true);
- chars[i].setActionCommand(String.valueOf((char) a));
- chars[i].addActionListener(this);
- jf.add(chars[i]);
- a++;
- }
- }
- private void startConnectionForSecondPlayer() {
- new Thread() {
- public void run() {
- while (true) {
- try {
- String taken = sc.nextLine();
- jf.setTitle(taken);
- } catch (Exception exc) {
- }
- }
- }
- }.start();
- }
- private void changeTurnAsAGuesser() {
- String word;
- String passing;
- word = JOptionPane.showInputDialog(null, "Enter Word",
- "input" + userName, JOptionPane.QUESTION_MESSAGE).toUpperCase();
- passing = "~:";
- for (int i = 0; i < word.length(); i++)
- passing += "_ ";
- try {
- int wrongGuesses = 0;
- pw.println(passing);
- pw.flush();
- do {
- char x = sc.nextLine().charAt(0);
- System.err.println("Received char: " + x);
- boolean thereItIs = false;
- for (int i = 0; i < word.length(); i++) {
- if (word.charAt(i) == x) {
- String temp1 = passing.substring(0, 2 * i
- + MESSAGE_LENGTH);
- String temp2 = passing.substring(2 * i + 1
- + MESSAGE_LENGTH);
- passing = temp1 + x + temp2;
- System.out.println(passing);
- thereItIs = true;
- }
- }
- if (thereItIs == false) {
- wrongGuesses++;
- }
- pw.println(passing);
- pw.flush();
- } while (checkWord(word, passing) == false && wrongGuesses < 7);
- if (wrongGuesses == 6) {
- lose();
- } else {
- win();
- }
- } catch (Exception e) {
- }
- }
- private void win() {
- // TODO Auto-generated method stub
- System.out.println("win");
- pw.println("nextRound");
- pw.flush();
- changeTurnAsAGuesser();
- }
- private void lose() {
- // TODO Auto-generated method stub
- System.out.println("lose");
- }
- private boolean checkWord(String word, String Passing) {
- boolean ans = false;
- int length = 0;
- for (int i = 0; i < word.length(); i++) {
- if (word.charAt(i) == Passing.charAt(2 * i + MESSAGE_LENGTH)) {
- length++;
- }
- }
- if (length == word.length()) {
- ans = true;
- }
- return ans;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- }
- }
- ===============================================================
- ===============================================================
- package hangman.server;
- import hangman.Client.NetworkThread;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.UnknownHostException;
- public class GameServer {
- static int i = 0;
- ServerSocket gameServer = null;
- String userName;
- NetworkThread[] nt = new NetworkThread[51];
- Thread[] th = new Thread[51];
- public GameServer(String username) {
- this.userName = username;
- new Thread() {
- public void run() {
- try {
- gameServer = new ServerSocket(4444);
- } catch (IOException e) {
- }
- while (true) {
- try {
- i++;
- Socket newClient = gameServer.accept();
- System.out.println(i);
- Thread th = new Thread(new NetworkThread(newClient,
- userName, i));
- th.start();
- } catch (UnknownHostException e) {
- } catch (IOException e) {
- }
- }
- }
- }.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement