Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. import java.io.*;
  8. import java.net.*;
  9. import java.util.*;
  10.  
  11. /**
  12. * Client class to manage client side connections/ports
  13. *
  14. * @author Dillon
  15. */
  16. public class Client {
  17.  
  18. static String PLAYER_ONE_NAME;
  19. static String PLAYER_TWO_NAME;
  20. private Socket clientSocket;
  21. private PrintStream out;
  22. private BufferedReader in;
  23. private Scanner scanner = new Scanner(System.in);
  24.  
  25. /**
  26. * Main method
  27. *
  28. * @param args
  29. * @throws IOException
  30. */
  31. public static void main(String[] args) throws IOException {
  32. Client client = new Client();
  33. client.run();
  34.  
  35. System.exit(0);
  36. }
  37.  
  38. public void run() {
  39. try {
  40. Socket clientSocket = new Socket("localhost", 4000);
  41. out = new PrintStream(
  42. clientSocket.getOutputStream(), true);;
  43. in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  44.  
  45. try {
  46.  
  47. while (clientSocket.isConnected()) {
  48.  
  49. System.out.println("");
  50. System.out.println("Welcome to Tic Tac Toe!");
  51. System.out.println("");
  52. System.out.println("Player one enter your name: ");
  53. PLAYER_ONE_NAME = scanner.nextLine();
  54. out.println(PLAYER_ONE_NAME);
  55. System.out.println("Player two enter your name: ");
  56. PLAYER_TWO_NAME = scanner.nextLine();
  57. out.println(PLAYER_TWO_NAME);
  58.  
  59. System.out.println("Do you want to play again?");
  60. while(true) {
  61. String playAgain = scanner.nextLine();
  62. if(playAgain.toLowerCase().equals("y") ||
  63. playAgain.toLowerCase().equals("yes")) {
  64. break;
  65. } else if(playAgain.toLowerCase().equals("n") ||
  66. playAgain.toLowerCase().equals("no")) {
  67. System.out.println("Exiting....");
  68. return;
  69. } else {
  70. System.out.println("Please enter whether to "
  71. + " play again or not.");
  72. }
  73. }
  74. // String reply = scanner.nextLine();
  75. // out.println(reply);
  76. }
  77. } finally {
  78. clientSocket.close();
  79. System.out.println("Client socket closed....");
  80. }
  81. } catch (IOException e) {
  82. e.printStackTrace();
  83. }
  84.  
  85. }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement