Advertisement
Guest User

Untitled

a guest
May 17th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.56 KB | None | 0 0
  1. package DiceGame2;
  2.  
  3. import java.net.*;
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7.  
  8.  
  9. @SuppressWarnings("serial")
  10. public class ChatClient2 extends javax.swing.JFrame
  11. {
  12. String username, address;
  13. ArrayList<String> users = new ArrayList<String>();
  14. int port = 2222;
  15. int playerScore = 0;
  16. public int rollAmnt = 0;
  17. Boolean isConnected = false;
  18. private Dice2 dice = new Dice2();
  19. public static int status = 0;
  20. private ChatServer2 tellallserver = new ChatServer2();
  21.  
  22. Socket sock;
  23. BufferedReader reader;
  24. PrintWriter writer;
  25.  
  26. //Communication Functions
  27. public void ListenThread()
  28. {
  29. Thread IncomingReader = new Thread(new IncomingReader());
  30. IncomingReader.start();
  31. }
  32.  
  33. public void userAdd(String data)
  34. {
  35. users.add(data);
  36. //tellallserver.tellEveryone("Hello");
  37. }
  38.  
  39. public void userRemove(String data)
  40. {
  41. ta_chat.append(data + " is now offline.\n");
  42. }
  43.  
  44. public void writeUsers()
  45. {
  46. String[] tempList = new String[(users.size())];
  47. users.toArray(tempList);
  48. for (String token:tempList)
  49. {
  50. //users.append(token + "\n");
  51. }
  52. //tellallserver.tellEveryone("Hello");
  53. }
  54.  
  55. public void sendDisconnect()
  56. {
  57. String bye = (username + ": :Disconnect");
  58. try
  59. {
  60. writer.println(bye);
  61. writer.flush();
  62. } catch (Exception e)
  63. {
  64. ta_chat.append("Could not send Disconnect message.\n");
  65. }
  66. }
  67.  
  68. public void Disconnect()
  69. {
  70. try
  71. {
  72. ta_chat.append("Disconnected.\n");
  73. sock.close();
  74. } catch(Exception ex) {
  75. ta_chat.append("Failed to disconnect. \n");
  76. }
  77. isConnected = false;
  78. tf_username.setEditable(true);
  79.  
  80. }
  81. //Creates Chat Client
  82. public ChatClient2()
  83. {
  84. initComponents();
  85. }
  86.  
  87. public class IncomingReader implements Runnable
  88. {
  89. @Override
  90. public void run()
  91. {
  92. String[] data;
  93. String stream, done = "Done", connect = "Connect", disconnect = "Disconnect", chat = "Chat";
  94.  
  95. try
  96. {
  97. while ((stream = reader.readLine()) != null)
  98. {
  99. data = stream.split(":");
  100.  
  101. if (data[2].equals(chat))
  102. {
  103. ta_chat.append(data[0] + ": " + data[1] + "\n");
  104. ta_chat.setCaretPosition(ta_chat.getDocument().getLength());
  105. }
  106. else if (data[2].equals(connect))
  107. {
  108. ta_chat.removeAll();
  109. userAdd(data[0]);
  110. }
  111. else if (data[2].equals(disconnect))
  112. {
  113. userRemove(data[0]);
  114. }
  115. else if (data[2].equals(done))
  116. {
  117. //users.setText("");
  118. writeUsers();
  119. users.clear();
  120. }
  121. }
  122. }catch(Exception ex) { }
  123. }
  124. }
  125.  
  126. //GUI Creation/Implementation
  127. @SuppressWarnings("unchecked")
  128. private void initComponents() {
  129.  
  130. lb_address = new javax.swing.JLabel();
  131. tf_address = new javax.swing.JTextField();
  132. lb_port = new javax.swing.JLabel();
  133. tf_port = new javax.swing.JTextField();
  134. lb_username = new javax.swing.JLabel();
  135. tf_username = new javax.swing.JTextField();
  136. lb_password = new javax.swing.JLabel();
  137. tf_password = new javax.swing.JTextField();
  138. b_connect = new javax.swing.JButton();
  139. b_disconnect = new javax.swing.JButton();
  140. b_anonymous = new javax.swing.JButton();
  141. jScrollPane1 = new javax.swing.JScrollPane();
  142. ta_chat = new javax.swing.JTextArea();
  143. tf_chat = new javax.swing.JTextField();
  144. b_send = new javax.swing.JButton();
  145. b_roll = new javax.swing.JButton();
  146. b_again = new javax.swing.JButton();
  147. lb_name = new javax.swing.JLabel();
  148.  
  149. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  150. setTitle("Chat - Client's frame");
  151. setName("client");
  152. setResizable(false);
  153.  
  154. lb_address.setText("Address : ");
  155. tf_address.setText("");
  156. //address = "192.168.1.3";
  157. address = tf_address.getText();
  158.  
  159. tf_address.addActionListener(new java.awt.event.ActionListener() {
  160. public void actionPerformed(java.awt.event.ActionEvent evt) {
  161. tf_addressActionPerformed(evt);
  162. }
  163. });
  164.  
  165. lb_port.setText("Port :");
  166. //Auto Sets Port to 2222
  167. tf_port.setText("2222");
  168. tf_port.addActionListener(new java.awt.event.ActionListener() {
  169. public void actionPerformed(java.awt.event.ActionEvent evt) {
  170. tf_portActionPerformed(evt);
  171. }
  172. });
  173.  
  174. lb_username.setText("Username :");
  175.  
  176. tf_username.addActionListener(new java.awt.event.ActionListener() {
  177. public void actionPerformed(java.awt.event.ActionEvent evt) {
  178. tf_usernameActionPerformed(evt);
  179. }
  180. });
  181.  
  182. lb_password.setText("Password : ");
  183.  
  184. b_connect.setText("Connect");
  185. b_connect.addActionListener(new java.awt.event.ActionListener() {
  186. public void actionPerformed(java.awt.event.ActionEvent evt) {
  187. b_connectActionPerformed(evt);
  188. }
  189. });
  190.  
  191. b_disconnect.setText("Disconnect");
  192. b_disconnect.addActionListener(new java.awt.event.ActionListener() {
  193. public void actionPerformed(java.awt.event.ActionEvent evt) {
  194. b_disconnectActionPerformed(evt);
  195. }
  196. });
  197.  
  198. b_anonymous.setText("Anonymous Login");
  199. b_anonymous.addActionListener(new java.awt.event.ActionListener() {
  200. public void actionPerformed(java.awt.event.ActionEvent evt) {
  201. b_anonymousActionPerformed(evt);
  202. }
  203. });
  204.  
  205. ta_chat.setColumns(20);
  206. ta_chat.setRows(5);
  207. jScrollPane1.setViewportView(ta_chat);
  208.  
  209. b_send.setText("SEND");
  210. b_send.addActionListener(new java.awt.event.ActionListener() {
  211. public void actionPerformed(java.awt.event.ActionEvent evt) {
  212. b_sendActionPerformed(evt);
  213. }
  214. });
  215.  
  216. b_roll.setText("Roll Dice");
  217. b_roll.addActionListener(new java.awt.event.ActionListener() {
  218. public void actionPerformed(java.awt.event.ActionEvent evt) {
  219. b_rollActionPerformed(evt);
  220. }
  221. });
  222.  
  223. b_again.setText("Play Again");
  224. b_again.addActionListener(new java.awt.event.ActionListener() {
  225. public void actionPerformed(java.awt.event.ActionEvent evt) {
  226. b_againActionPerformed(evt);
  227. }
  228. });
  229.  
  230. lb_name.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  231.  
  232. //Add GUI
  233. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  234. getContentPane().setLayout(layout);
  235. layout.setHorizontalGroup(
  236. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  237. .addGroup(layout.createSequentialGroup()
  238. .addContainerGap()
  239. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  240. .addGroup(layout.createSequentialGroup()
  241. .addComponent(tf_chat, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)
  242. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  243. .addComponent(b_send, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
  244. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  245. .addComponent(b_roll, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
  246. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  247. .addComponent(b_again, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
  248. .addComponent(jScrollPane1)
  249. .addGroup(layout.createSequentialGroup()
  250. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  251. .addComponent(lb_username, javax.swing.GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE)
  252. .addComponent(lb_address, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  253. .addGap(18, 18, 18)
  254. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  255. .addComponent(tf_address, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
  256. .addComponent(tf_username))
  257. .addGap(18, 18, 18)
  258. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  259. .addComponent(lb_password, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  260. .addComponent(lb_port, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  261. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  262. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  263. .addComponent(tf_password)
  264. .addComponent(tf_port, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE))
  265. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  266. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  267. .addGroup(layout.createSequentialGroup()
  268. .addComponent(b_connect)
  269. .addGap(2, 2, 2)
  270. .addComponent(b_disconnect)
  271. .addGap(0, 0, Short.MAX_VALUE))
  272. .addComponent(b_anonymous, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
  273. .addContainerGap())
  274. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  275. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  276. .addComponent(lb_name)
  277. .addGap(201, 201, 201))
  278. );
  279. layout.setVerticalGroup(
  280. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  281. .addGroup(layout.createSequentialGroup()
  282. .addContainerGap()
  283. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  284. .addComponent(lb_address)
  285. .addComponent(tf_address, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  286. .addComponent(lb_port)
  287. .addComponent(tf_port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  288. .addComponent(b_anonymous))
  289. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  290. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  291. .addComponent(tf_username)
  292. .addComponent(tf_password)
  293. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  294. .addComponent(lb_username)
  295. .addComponent(lb_password)
  296. .addComponent(b_connect)
  297. .addComponent(b_disconnect)))
  298. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  299. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
  300. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  301. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  302. .addComponent(tf_chat)
  303. .addComponent(b_send, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
  304. .addComponent(b_roll, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
  305. .addComponent(b_again, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE))
  306. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  307. .addComponent(lb_name))
  308. );
  309.  
  310. pack();
  311. }
  312.  
  313. private void tf_addressActionPerformed(java.awt.event.ActionEvent evt) {
  314.  
  315. }
  316.  
  317. private void tf_portActionPerformed(java.awt.event.ActionEvent evt) {
  318.  
  319. }
  320.  
  321. private void tf_usernameActionPerformed(java.awt.event.ActionEvent evt) {
  322.  
  323. }
  324.  
  325. private void b_connectActionPerformed(java.awt.event.ActionEvent evt) {
  326. if (isConnected == false)
  327. {
  328. username = tf_username.getText();
  329. tf_username.setEditable(false);
  330.  
  331. try
  332. {
  333. address = tf_address.getText();
  334. sock = new Socket(address, port);
  335. InputStreamReader streamreader = new InputStreamReader(sock.getInputStream());
  336. reader = new BufferedReader(streamreader);
  337. writer = new PrintWriter(sock.getOutputStream());
  338. writer.println(username + ":has connected.:Connect");
  339. writer.flush();
  340. isConnected = true;
  341.  
  342. }
  343. catch (Exception ex)
  344. {
  345. ta_chat.append("Cannot Connect! Try Again. \n");
  346. tf_username.setEditable(true);
  347. }
  348.  
  349. ListenThread();
  350.  
  351. } else if (isConnected == true)
  352. {
  353. ta_chat.append("You are already connected. \n");
  354. }
  355. ta_chat.append("Welcome to Dice Poker " + username + "!\nThe rules are simple. Each player will\ncontinue clicking the roll dice button\nuntil both players "
  356. + "are finished and\n a winner is declared.\n Click play again to go another round!\n\n");
  357. }
  358.  
  359. private void b_disconnectActionPerformed(java.awt.event.ActionEvent evt) {
  360. sendDisconnect();
  361. Disconnect();
  362. }
  363.  
  364. private void b_anonymousActionPerformed(java.awt.event.ActionEvent evt) {
  365. tf_username.setText("");
  366. if (isConnected == false)
  367. {
  368. String anon="anon";
  369. Random generator = new Random();
  370. int i = generator.nextInt(999) + 1;
  371. String is=String.valueOf(i);
  372. anon=anon.concat(is);
  373. username=anon;
  374.  
  375. tf_username.setText(anon);
  376. tf_username.setEditable(false);
  377.  
  378. try
  379. {
  380. sock = new Socket(address, port);
  381. InputStreamReader streamreader = new InputStreamReader(sock.getInputStream());
  382. reader = new BufferedReader(streamreader);
  383. writer = new PrintWriter(sock.getOutputStream());
  384. writer.println(anon + ":has connected.:Connect");
  385. writer.flush();
  386. isConnected = true;
  387. }
  388. catch (Exception ex)
  389. {
  390. ta_chat.append("Cannot Connect! Try Again. \n");
  391. tf_username.setEditable(true);
  392. }
  393.  
  394. ListenThread();
  395.  
  396. } else if (isConnected == true)
  397. {
  398. ta_chat.append("You are already connected. \n");
  399. }
  400. }
  401.  
  402. private void b_sendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_sendActionPerformed
  403. String nothing = "";
  404. if ((tf_chat.getText()).equals(nothing)) {
  405. tf_chat.setText("");
  406. tf_chat.requestFocus();
  407. } else {
  408. try {
  409. writer.println(username + ":" + tf_chat.getText() + ":" + "Chat");
  410. writer.flush(); // flushes the buffer
  411. } catch (Exception ex) {
  412. ta_chat.append("Message was not sent. \n");
  413. }
  414. tf_chat.setText("");
  415. tf_chat.requestFocus();
  416. }
  417.  
  418. tf_chat.setText("");
  419. tf_chat.requestFocus();
  420.  
  421. }
  422.  
  423. private void b_rollActionPerformed(java.awt.event.ActionEvent evt)
  424. {
  425. int[] diceCounts = new int [6];
  426. //int playerScore = 0;
  427. //int rollAmnt = 0;
  428.  
  429. rollAmnt = rollAmnt + 1;
  430. ta_chat.append(username + ": ");
  431. for(int i = 0; i < 5; i++)
  432. {
  433. diceCounts[i] = dice.rollDice();
  434. ta_chat.append(diceCounts[i] + " ");
  435. }
  436. //ta_chat.append(Dice.getResult(diceCounts));
  437. if(Dice2.getResult(diceCounts).equals("Five of a kind"))
  438. {
  439. playerScore = playerScore + 20;
  440. if(playerScore >= 40)
  441. {
  442. ta_chat.append("\n" + username + " Finishes the Game in " + rollAmnt + "rolls!");
  443. }
  444.  
  445. }
  446. else if(Dice2.getResult(diceCounts).equals("Four of a kind"))
  447. {
  448. playerScore = playerScore + 10;
  449. if(playerScore >= 40)
  450. {
  451. ta_chat.append("\n" + username + " Finishes the Game in " + rollAmnt + "rolls!");
  452. }
  453.  
  454. }
  455. else if(Dice2.getResult(diceCounts).equals("Three of a kind"))
  456. {
  457. playerScore = playerScore + 7;
  458. if(playerScore >= 40)
  459. {
  460. ta_chat.append("\n" + username + " Finishes the Game in " + rollAmnt + "rolls!");
  461. }
  462.  
  463. }
  464. else if(Dice2.getResult(diceCounts).equals("Full House"))
  465. {
  466. playerScore = playerScore + 15;
  467. if(playerScore >= 40)
  468. {
  469. ta_chat.append("\n" + username + " Finishes the Game in " + rollAmnt + "rolls!");
  470. }
  471.  
  472. }
  473. else if(Dice2.getResult(diceCounts).equals("Two Pairs"))
  474. {
  475. playerScore = playerScore + 5;
  476. if(playerScore >= 40)
  477. {
  478. ta_chat.append("\n" + username + " Finishes the Game in " + rollAmnt + "rolls!");
  479. }
  480.  
  481. }
  482. else if(Dice2.getResult(diceCounts).equals("One Pair"))
  483. {
  484. playerScore = playerScore + 2;
  485. if(playerScore >= 40)
  486. {
  487. ta_chat.append("\n" + username + " Finishes the Game in " + rollAmnt + "rolls!");
  488. }
  489.  
  490. }
  491.  
  492. ta_chat.append("\n" + username + " got " + Dice2.getResult(diceCounts) + "!");
  493. ta_chat.append("\n");
  494.  
  495. if(playerScore < 40)
  496. {
  497. ta_chat.append("Keep Rolling!\n\n");
  498.  
  499. }
  500. else if(playerScore >=40)
  501. {
  502.  
  503. String message = username + " has finished with " + rollAmnt + " rolls!";
  504.  
  505. b_roll.setEnabled(false);
  506. //tellallserver.tellEveryone(username + " has finished with " + rollAmnt + " rolls!");
  507. //tellallserver.tellEveryone(message);
  508. tf_chat.setText(message);
  509. b_send.doClick();
  510. ta_chat.append("Click 'Play again' button to keep playing\n\n");
  511.  
  512.  
  513. }
  514. }
  515.  
  516. private void b_againActionPerformed(java.awt.event.ActionEvent evt)
  517. {
  518. b_roll.setEnabled(true);
  519. rollAmnt = 0;
  520. playerScore = 0;
  521. }
  522.  
  523. //Main
  524. public static void main(String args[])
  525. {
  526. java.awt.EventQueue.invokeLater(new Runnable()
  527. {
  528. @Override
  529. public void run()
  530. {
  531. new ChatClient2().setVisible(true);
  532. }
  533. });
  534. }
  535.  
  536. // Variables declaration
  537. private javax.swing.JButton b_anonymous;
  538. private javax.swing.JButton b_connect;
  539. private javax.swing.JButton b_disconnect;
  540. private javax.swing.JButton b_send;
  541. private javax.swing.JButton b_roll;
  542. private javax.swing.JButton b_again;
  543. private javax.swing.JScrollPane jScrollPane1;
  544. private javax.swing.JLabel lb_address;
  545. private javax.swing.JLabel lb_name;
  546. private javax.swing.JLabel lb_password;
  547. private javax.swing.JLabel lb_port;
  548. private javax.swing.JLabel lb_username;
  549. private javax.swing.JTextArea ta_chat;
  550. private javax.swing.JTextField tf_address;
  551. private javax.swing.JTextField tf_chat;
  552. private javax.swing.JTextField tf_password;
  553. private javax.swing.JTextField tf_port;
  554. private javax.swing.JTextField tf_username;
  555. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement