Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.io.*;
- import java.net.*;
- import java.util.*;
- /**
- * Client class to manage client side connections/ports
- *
- * @author Dillon
- */
- public class Client {
- static String PLAYER_ONE_NAME;
- static String PLAYER_TWO_NAME;
- private Socket clientSocket;
- private PrintStream out;
- private BufferedReader in;
- private Scanner scanner = new Scanner(System.in);
- /**
- * Main method
- *
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- Client client = new Client();
- client.run();
- System.exit(0);
- }
- public void run() {
- try {
- Socket clientSocket = new Socket("localhost", 4000);
- out = new PrintStream(
- clientSocket.getOutputStream(), true);;
- in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- try {
- while (clientSocket.isConnected()) {
- System.out.println("");
- System.out.println("Welcome to Tic Tac Toe!");
- System.out.println("");
- System.out.println("Player one enter your name: ");
- PLAYER_ONE_NAME = scanner.nextLine();
- out.println(PLAYER_ONE_NAME);
- System.out.println("Player two enter your name: ");
- PLAYER_TWO_NAME = scanner.nextLine();
- out.println(PLAYER_TWO_NAME);
- System.out.println("Do you want to play again?");
- while(true) {
- String playAgain = scanner.nextLine();
- if(playAgain.toLowerCase().equals("y") ||
- playAgain.toLowerCase().equals("yes")) {
- break;
- } else if(playAgain.toLowerCase().equals("n") ||
- playAgain.toLowerCase().equals("no")) {
- System.out.println("Exiting....");
- return;
- } else {
- System.out.println("Please enter whether to "
- + " play again or not.");
- }
- }
- // String reply = scanner.nextLine();
- // out.println(reply);
- }
- } finally {
- clientSocket.close();
- System.out.println("Client socket closed....");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement