Advertisement
sabbasizadeh

Untitled

Apr 10th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.36 KB | None | 0 0
  1. package hangman.UI;
  2.  
  3. public class Start {
  4. public static void main(String[] arg) {
  5. java.awt.EventQueue.invokeLater(new Runnable() {
  6. public void run() {
  7. JoinGame jg = new JoinGame();
  8. }
  9. });
  10. }
  11. }
  12. ====================================================
  13. ====================================================
  14. package hangman.UI;
  15.  
  16. import hangman.Client.JoinClient;
  17. import hangman.server.GameServer;
  18.  
  19. import java.awt.Image;
  20. import java.awt.Toolkit;
  21. import java.awt.event.MouseEvent;
  22. import java.awt.event.MouseListener;
  23.  
  24. import java.net.InetAddress;
  25. import java.net.UnknownHostException;
  26. import java.util.ArrayList;
  27.  
  28. import javax.swing.ImageIcon;
  29. import javax.swing.JButton;
  30. import javax.swing.JFrame;
  31. import javax.swing.JLabel;
  32. import javax.swing.JOptionPane;
  33. import javax.swing.JTextField;
  34.  
  35. public class JoinGame extends JFrame implements MouseListener {
  36.  
  37. JFrame jf;
  38. JButton server;
  39. JButton join;
  40. JLabel imgIcn;
  41. JTextField name;
  42. JLabel nameJL;
  43.  
  44. String userName;
  45.  
  46. public JoinGame() {
  47.  
  48. Image icon = Toolkit.getDefaultToolkit().getImage("img0.png");
  49.  
  50. jf = new JFrame();
  51. jf.setSize(230, 130);
  52. jf.setLayout(null);
  53. jf.setTitle("Hangman server");
  54. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55. jf.setResizable(false);
  56. jf.setLocationRelativeTo(this);
  57. jf.setVisible(true);
  58. jf.setIconImage(icon);
  59.  
  60. server = new JButton("Start a new server");
  61. server.addMouseListener(this);
  62. server.setSize(150, 30);
  63. server.setLocation(5, 5);
  64. server.setVisible(true);
  65. server.setToolTipText("<html>&#160;&#160;&#160;By clicking this button<br>you can start a new server </html>");
  66.  
  67. join = new JButton("Join to a server");
  68. join.addMouseListener(this);
  69. join.setSize(150, 30);
  70. join.setLocation(5, 37);
  71. join.setVisible(true);
  72. join.setToolTipText("<html>You can join a game if you<br> know it's server IP address </html>");
  73.  
  74. ImageIcon ii = new ImageIcon("iconImg.png");
  75. imgIcn = new JLabel("");
  76. imgIcn.setIcon(ii);
  77. imgIcn.setLocation(157, 5);
  78. imgIcn.setSize(60, 68);
  79. imgIcn.setVisible(true);
  80.  
  81. nameJL = new JLabel("YOUR USERNAME:");
  82. nameJL.setSize(130, 10);
  83. nameJL.setLocation(5, 78);
  84. nameJL.setVisible(true);
  85.  
  86. name = new JTextField();
  87. name.setSize(110, 18);
  88. name.setLocation(110, 75);
  89. name.setVisible(true);
  90.  
  91. jf.add(server);
  92. jf.add(join);
  93. jf.add(imgIcn);
  94. jf.add(nameJL);
  95. jf.add(name);
  96.  
  97. }
  98.  
  99. @Override
  100. public void mouseClicked(MouseEvent e) {
  101. if (e.getSource() == server) {
  102. try {
  103. setUsername(InetAddress.getLocalHost());
  104. } catch (UnknownHostException e1) {
  105. }
  106. } else if (e.getSource() == join) {
  107. setUsername();
  108. }
  109. }
  110.  
  111. private void setUsername() {
  112.  
  113. if (!name.getText().equals("")) {
  114. this.userName = name.getText();
  115. JoinClient jc = new JoinClient(this.userName);
  116. jf.dispose();
  117. } else {
  118. Toolkit.getDefaultToolkit().beep();
  119. Object[] options = { "try again", "cancel" };
  120. int answer = JOptionPane.showOptionDialog(null,
  121. "Please enter your user name", "SET USERNAME",
  122. JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
  123. null, options, options[0]);
  124. }
  125. }
  126.  
  127. private void setUsername(InetAddress address) {
  128. if (!name.getText().equals("")) {
  129. this.userName = name.getText();
  130. JoinClient jc = new JoinClient(address, this.userName);
  131. jf.dispose();
  132.  
  133. } else {
  134. Toolkit.getDefaultToolkit().beep();
  135. Object[] options = { "try again", "cancel" };
  136. int answer = JOptionPane.showOptionDialog(null,
  137. "Please enter your user name", "SET USERNAME",
  138. JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
  139. null, options, options[0]);
  140. }
  141. }
  142.  
  143. @Override
  144. public void mousePressed(MouseEvent e) {
  145. }
  146.  
  147. @Override
  148. public void mouseReleased(MouseEvent e) {
  149. }
  150.  
  151. @Override
  152. public void mouseEntered(MouseEvent e) {
  153. }
  154.  
  155. @Override
  156. public void mouseExited(MouseEvent e) {
  157. }
  158. }
  159. =======================================================
  160. =======================================================
  161. package hangman.Client;
  162.  
  163. import hangman.server.GameServer;
  164.  
  165. import java.awt.event.MouseEvent;
  166. import java.awt.event.MouseListener;
  167. import java.io.IOException;
  168. import java.net.InetAddress;
  169. import java.net.Socket;
  170. import java.net.UnknownHostException;
  171.  
  172. import javax.swing.JButton;
  173. import javax.swing.JFrame;
  174. import javax.swing.JLabel;
  175. import javax.swing.JOptionPane;
  176. import javax.swing.JTextField;
  177.  
  178. public class JoinClient extends JFrame implements MouseListener {
  179.  
  180. InetAddress serverInetAdd = null;
  181. String userName;
  182.  
  183. JFrame jf;
  184. JTextField ipAddress;
  185. JLabel ipJL;
  186. JButton connect;// for the constructor which get inet from user
  187.  
  188. public JoinClient(String userName) {
  189.  
  190. this.userName = userName;
  191.  
  192. jf = new JFrame();
  193. jf.setSize(260, 105);
  194. jf.setLayout(null);
  195. jf.setTitle("Join server");
  196. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  197. jf.setResizable(false);
  198. jf.setLocationRelativeTo(this);
  199. jf.setVisible(true);
  200.  
  201. ipJL = new JLabel("SERVER IP ADDRESS:");
  202. ipJL.setSize(160, 10);
  203. ipJL.setLocation(5, 10);
  204. ipJL.setVisible(true);
  205.  
  206. ipAddress = new JTextField();
  207. ipAddress.setSize(103, 18);
  208. ipAddress.setLocation(135, 7);
  209. ipAddress.setVisible(true);
  210.  
  211. connect = new JButton("connect");
  212. connect.setBounds(60, 50, 135, 22);
  213. connect.setVisible(true);
  214. connect.addMouseListener(this);
  215.  
  216. jf.add(ipJL);
  217. jf.add(ipAddress);
  218. jf.add(connect);
  219.  
  220. }
  221.  
  222. public JoinClient(InetAddress iAdd, String userName) {
  223. GameServer gs = new GameServer(userName);
  224. try {
  225. Socket s = new Socket(iAdd, 4444);
  226. } catch (IOException e) {
  227. }
  228.  
  229. this.serverInetAdd = iAdd;
  230. this.userName = userName;
  231. }
  232.  
  233. private InetAddress getUserAddressInput() {
  234. InetAddress address = null;
  235.  
  236. try {
  237. address = InetAddress.getByName(ipAddress.getText());
  238. } catch (UnknownHostException e) {
  239. JOptionPane.showMessageDialog(this,
  240. "Please enter a valid IP address", "WRONG IP", 0);
  241. ipAddress.setText(null);
  242. }
  243.  
  244. return address;
  245. }
  246.  
  247. @Override
  248. public void mouseClicked(MouseEvent e) {
  249.  
  250. if (e.getSource() == connect) {
  251. jf.dispose();
  252. this.serverInetAdd = getUserAddressInput();
  253. // Client c = new Client(serverInetAdd, 1);
  254. try {
  255. Socket s = new Socket(serverInetAdd, 4444);
  256. } catch (IOException e2) {
  257. }
  258.  
  259. }
  260. }
  261.  
  262. @Override
  263. public void mousePressed(MouseEvent e) {
  264. }
  265.  
  266. @Override
  267. public void mouseReleased(MouseEvent e) {
  268. }
  269.  
  270. @Override
  271. public void mouseEntered(MouseEvent e) {
  272. }
  273.  
  274. @Override
  275. public void mouseExited(MouseEvent e) {
  276. }
  277. }
  278. =========================================================
  279. =========================================================
  280. package hangman.Client;
  281.  
  282. import java.awt.Button;
  283. import java.awt.TextField;
  284. import java.awt.event.ActionEvent;
  285. import java.awt.event.ActionListener;
  286. import java.io.IOException;
  287. import java.io.PrintWriter;
  288. import java.net.Socket;
  289. import java.util.Scanner;
  290.  
  291. import javax.swing.JFrame;
  292. import javax.swing.JOptionPane;
  293.  
  294. public class NetworkThread implements Runnable, ActionListener {
  295.  
  296. Socket newClient;
  297.  
  298. String userName;
  299.  
  300. int id;
  301.  
  302. Scanner sc = null;
  303. PrintWriter pw = null;
  304.  
  305. JFrame jf;
  306. TextField object;
  307. Button[] chars;
  308.  
  309. final int MESSAGE_LENGTH = 2;
  310.  
  311. public NetworkThread(Socket newClient, String userName, int id) {
  312. this.userName = userName;
  313. this.newClient = newClient;
  314. this.id = id;
  315.  
  316. jf = new JFrame(userName);
  317. jf.setSize(460, 350);
  318. jf.setLayout(null);
  319. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  320. jf.setResizable(false);
  321. jf.setLocationRelativeTo(null);
  322. jf.setVisible(true);
  323.  
  324. try {
  325. pw = new PrintWriter(newClient.getOutputStream());
  326. sc = new Scanner(newClient.getInputStream());
  327. } catch (IOException e) {
  328. }
  329.  
  330. ConstructFrame();
  331. }
  332.  
  333. @Override
  334. public void run() {
  335. if (id == 1)
  336. changeTurnAsAGuesser();
  337. else
  338. startConnectionForSecondPlayer();
  339.  
  340. }
  341.  
  342. private void ConstructFrame() {
  343. chars = new Button[26];
  344. int a = 'A';
  345. int j = 1;
  346. for (int i = 0; i < chars.length; i++) {
  347. if (i == 9)
  348. j++;
  349. else if (i == 18)
  350. j++;
  351.  
  352. chars[i] = new Button(String.valueOf((char) a));
  353. chars[i].setBounds(21 * (i % 9) + j * 8, 21 * j, 20, 20);
  354. chars[i].setVisible(true);
  355. chars[i].setActionCommand(String.valueOf((char) a));
  356. chars[i].addActionListener(this);
  357. jf.add(chars[i]);
  358. a++;
  359. }
  360.  
  361. }
  362.  
  363. private void startConnectionForSecondPlayer() {
  364. new Thread() {
  365. public void run() {
  366. while (true) {
  367. try {
  368. String taken = sc.nextLine();
  369. jf.setTitle(taken);
  370. } catch (Exception exc) {
  371. }
  372. }
  373. }
  374. }.start();
  375. }
  376.  
  377. private void changeTurnAsAGuesser() {
  378. String word;
  379. String passing;
  380.  
  381. word = JOptionPane.showInputDialog(null, "Enter Word",
  382. "input" + userName, JOptionPane.QUESTION_MESSAGE).toUpperCase();
  383. passing = "~:";
  384. for (int i = 0; i < word.length(); i++)
  385. passing += "_ ";
  386. try {
  387. int wrongGuesses = 0;
  388. pw.println(passing);
  389. pw.flush();
  390.  
  391. do {
  392. char x = sc.nextLine().charAt(0);
  393. System.err.println("Received char: " + x);
  394. boolean thereItIs = false;
  395. for (int i = 0; i < word.length(); i++) {
  396. if (word.charAt(i) == x) {
  397. String temp1 = passing.substring(0, 2 * i
  398. + MESSAGE_LENGTH);
  399. String temp2 = passing.substring(2 * i + 1
  400. + MESSAGE_LENGTH);
  401. passing = temp1 + x + temp2;
  402. System.out.println(passing);
  403. thereItIs = true;
  404. }
  405. }
  406. if (thereItIs == false) {
  407. wrongGuesses++;
  408. }
  409. pw.println(passing);
  410. pw.flush();
  411.  
  412. } while (checkWord(word, passing) == false && wrongGuesses < 7);
  413.  
  414. if (wrongGuesses == 6) {
  415. lose();
  416. } else {
  417. win();
  418. }
  419.  
  420. } catch (Exception e) {
  421. }
  422. }
  423.  
  424. private void win() {
  425. // TODO Auto-generated method stub
  426. System.out.println("win");
  427. pw.println("nextRound");
  428. pw.flush();
  429. changeTurnAsAGuesser();
  430. }
  431.  
  432. private void lose() {
  433. // TODO Auto-generated method stub
  434. System.out.println("lose");
  435. }
  436.  
  437. private boolean checkWord(String word, String Passing) {
  438. boolean ans = false;
  439. int length = 0;
  440. for (int i = 0; i < word.length(); i++) {
  441. if (word.charAt(i) == Passing.charAt(2 * i + MESSAGE_LENGTH)) {
  442. length++;
  443. }
  444. }
  445. if (length == word.length()) {
  446. ans = true;
  447. }
  448. return ans;
  449. }
  450.  
  451. @Override
  452. public void actionPerformed(ActionEvent e) {
  453.  
  454. }
  455.  
  456. }
  457. ===============================================================
  458. ===============================================================
  459. package hangman.server;
  460.  
  461. import hangman.Client.NetworkThread;
  462.  
  463. import java.io.IOException;
  464. import java.net.ServerSocket;
  465. import java.net.Socket;
  466. import java.net.UnknownHostException;
  467.  
  468. public class GameServer {
  469.  
  470. static int i = 0;
  471. ServerSocket gameServer = null;
  472.  
  473. String userName;
  474.  
  475. NetworkThread[] nt = new NetworkThread[51];
  476. Thread[] th = new Thread[51];
  477.  
  478. public GameServer(String username) {
  479. this.userName = username;
  480. new Thread() {
  481. public void run() {
  482. try {
  483. gameServer = new ServerSocket(4444);
  484. } catch (IOException e) {
  485. }
  486. while (true) {
  487. try {
  488. i++;
  489. Socket newClient = gameServer.accept();
  490. System.out.println(i);
  491. Thread th = new Thread(new NetworkThread(newClient,
  492. userName, i));
  493. th.start();
  494.  
  495. } catch (UnknownHostException e) {
  496. } catch (IOException e) {
  497. }
  498. }
  499. }
  500. }.start();
  501. }
  502.  
  503. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement