Skip to main content
Engineering LibreTexts

15.9: Playing One Row Nim Over the Network

  • Page ID
    59289
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In the previous section we developed and tested a generic echo service. It is based on a common root class, ClientServer, which is a subclass of Thread. Both EchoServer and EchoClient extend the root class, and each implements its own version of run(). In this section, we will generalize this design so that it can support a wide range of services. To illustrate the effectiveness of the design, we will use it as the basis for a program that plays One Row Nim over the Internet.

    In order to generalize our design, we begin by identifying those elements that are common to all servers and clients and what is particular to the echo service and client. Clearly, the general server and client protocols that are defined here in their respective run() methods, are something that all servers and clients have in common. What differs from one application to another is the particular service provided and requested, as detailed in their respective provideService() and requestService() methods. In this example, the service that is provided will be One Row Nim. The clients that use this service will be (human) players of the game.

    Therefore, the way to generalize this application is to define the method in the generic Server and Client classes. The overall design of the One Row Nim service will now consist of five classes organized into the hierarchy shown in Figure [fig-p858f1]. At the root of the hierarchy is the ClientServer class, which contains nothing but I/O methods used by both clients and servers. The abstract Server and Client classes contain implementations of the Thread.run() method, which defines the basic protocols for servers and clients, respectively. The details of the particular service are encoded in the provideService() and requestService() methods. Because the run() methods defined in Client and Server call provideService() and requestService(), respectively, these methods must be declared as abstract methods in the Server and Client classes. Any class that contains an method must itself be declared abstract.

    Note that we have left the readFromSocket() and writeToSocket() methods in the ClientServer class. These methods are written in a general way and can be used, without change, by a wide range of clients and servers. If necessary, they can also be overridden by a client or server. In fact, as we will see, the NimServer class does override the writeToSocket() method. Similarly, note that the readFromKeyboard() method is defined in the Client superclass. This is a general method that can be used by a large variety of clients, so it is best if they don’t have to redefine it themselves.

    These design decisions lead to the definitions of Server and Client shown in Figures [fig-serverclass] and [fig-clientclass], respectively. Note that provideService() and requestService() are left unimplemented. Subclasses of Server, such as NimServer, and subclasses of Client, such as NimClient, can implement provideService() and requestService() in a way that is appropriate for the particular service they are providing.

    import java.net.*;
    import java.io.*;
    
    public abstract class Server extends ClientServer {
      protected ServerSocket port;
      protected Socket socket;
    
      public Server(int portNum, int nBacklog)  {
        try {
          port = new ServerSocket (portNum, nBacklog);
        } catch (IOException e) {
          e.printStackTrace();
        } // try/catch
      }// Server()
    
      public void run() {
        try {
          System.out.println("Server at " + 
                           InetAddress.getLocalHost() +
                           " waiting for connections ");
          while (true) {
            socket = port.accept();
            System.out.println("Accepted a connection from " 
                                    + socket.getInetAddress());
            provideService(socket);
            socket.close();
            System.out.println("Closed the connection\n");
          }// while
        } catch (IOException e) {
          e.printStackTrace();
        } // try/catch 
      }// run()
    
     // Implemented in subclass
     protected abstract void provideService(Socket socket);  
    }// Server

    -1pc

    import java.net.*;
    import java.io.*;
    
    public abstract class Client extends ClientServer {
      protected Socket socket;
    
      public Client(String url, int port) {
        try {
          socket = new Socket(url,port);
          System.out.println("CLIENT: connected to " + url + 
                                                  ":" + port);
        } catch (Exception e) {
          e.printStackTrace();
          System.exit(1);
        } // try/catch block
      }// Client()
    
      public void run() {
        try {
          requestService(socket);
          socket.close();
          System.out.println("CLIENT: connection closed");
        } catch (IOException e) {
          System.out.println(e.getMessage());
          e.printStackTrace();
        } // try/catch block
      }// run()
    
      // Implemented in subclass
      protected abstract void requestService(Socket socket) 
                                          throws IOException;
    
      protected String readFromKeyboard() throws IOException {
        BufferedReader input = new BufferedReader 
                         (new InputStreamReader(System.in));
        System.out.print("INPUT: ");
        String line = input.readLine();
        return line;
      }// readFromKeyboard()
    }// Client

    The NimServer Class

    Given the abstract definition of the Server class, defining a new service is simply a matter of extending Server and implementing the provideService() method in the new subclass. We will name the subclass NimServer.

    import java.net.*;
    import java.io.*;
    
    public class NimServer extends Server {
      public NimServer(int port, int backlog) {
        super(port, backlog);
      }
      protected void provideService (Socket socket) {
        OneRowNim nim = new OneRowNim();
        try {
          writeToSocket(socket, "Hi Nim player. You're Player 1 and I'm Player 2. " +
               nim.reportGameState()  + " " + nim.getGamePrompt() + "\n");
          play(nim, socket);
        } catch (IOException e) {
          e.printStackTrace();
        } // try/catch
      } // provideService()
    
      private void play(OneRowNim nim, Socket socket) throws IOException {
        NimPlayer computerPlayer = new NimPlayer(nim);
        nim.addComputerPlayer(computerPlayer);
        String str="", response="";
        int userTakes = 0, computerTakes = 0;
        do {     
          str = readFromSocket(socket);
          boolean legalMove = false;
          do {
            userTakes = Integer.parseInt(str);
            if (nim.takeSticks(userTakes)) {
              legalMove = true;
              nim.changePlayer();
              response = nim.reportGameState() + " ";
              if (!nim.gameOver()) {
                computerTakes = Integer.parseInt(computerPlayer.makeAMove(""));    
                response = response + " My turn. I take " + computerTakes + " sticks. ";
                nim.takeSticks(computerTakes);
                nim.changePlayer();
                response = response + nim.reportGameState() + " ";
                if (!nim.gameOver())
                   response = response + nim.getGamePrompt();
              } // if not game over
              writeToSocket(socket, response);
            } else {
               writeToSocket(socket, "That's an illegal move. Try again.\n");
               str = readFromSocket(socket);
            } // if user takes
          } while (!legalMove);
        }  while (!nim.gameOver());
      } // play
    
      // Overriding writeToSocket to remove \n from str
      protected void writeToSocket(Socket soc, String str) throws IOException {
        StringBuffer sb = new StringBuffer();
        for (int k = 0; k < str.length(); k++)
          if (str.charAt(k) != '\n')
            sb.append(str.charAt(k));
        super.writeToSocket(soc, sb.toString() + "\n");
      }
      public static void main(String args[]) {
        NimServer server = new NimServer(10001,5);
        server.start();
      } // main()
    } // NimServer

    Figure [fig-nimserverclass] provides a definition of the NimServer subclass. Note how its implementation of provideService() uses an instance of the OneRowNim class from Chapter 8. Also, the play() method, which encapsulates the game-playing algorithm, uses an instance of NimPlayer, also from Chapter 8. You might recall that OneRowNim is a TwoPlayerGame and NimPlayer defines methods that allow a computer to play an optimal game of One Row Nim. In this example, the server acts as one of the players and use a NimPlayer to manage its playing strategy. Thus, clients that connect to NimServer will be faced by a computer that plays an optimal game.

    If you compare the details of the NimServer’s play() method with the play() method from the implementation of OneRowNim, you will see that they are very similar. In this implementation, we use public methods of the OneRowNim object to manage the playing of the game. Thus, addComputerPlayer() adds an instance of NimPlayer to the game. The takeSticks(), changePlayer(), and gameOver() methods are used to manage the moves made by both itself and the client. And the getGamePrompt() and reportGameState() methods are used to manage the interaction and communication with the client. Note also that whenever it is the server’s turn to move, it uses the NimPlayer’s makeAMove() method to determine the optimal move to make.

    Although the programming logic employed in the play() method looks somewhat complex, it is very similar to the logic employed in the Chapter 8 version of the game. The main difference here is that the server uses the writeToSocket() and readFromSocket() methods to manage the communication with the client. In this regard, this instance of provideService() is no different than the provideService() method we used in the EchoServer class.

    Finally, note that NimServer provides an implementation of the method. This method is implemented in the ClientServer() class and is inherited by NimServer. However, the default implementation assumes that the client will use the a carriage return (\n) to determine the end of a particular message in the socket. Because OneRowNim’s methods, getGamePrompt() and reportGameState(), contain embedded carriage returns, it is necessary to filter these. The new version of writeToSocket() performs this filtering and calls the default method (super.writeToSocket(), after it has finished its filtering task.

    The NimClient Class

    The NimClient class is even easier to implement. As its task is simply to manage the communication between the human user and the NimServer, it is very similar to the requestService() method we used in EchoClient. The relationship between the abstract Client class (Fig. [fig-clientclass]) and its extension in NimClient (Fig. [fig-nimclientclass]) is very similar to the relationship between Server and NimServer. The requestService() method is called by Client.run(). It is implemented in NimClient. In this way, the Client class can serve as a superclass for any number of clients. New clients for new services can be derived from Client by simply implementing their own requestService() method.

    import java.net.*;
    import java.io.*;
    
    public class NimClient extends Client {
      private KeyboardReader kb = new KeyboardReader();
      public NimClient( String url, int port) {
        super(url,port);
      }
    
      protected void requestService(Socket socket) throws IOException {
        String servStr = readFromSocket(socket);    // Get server's response
        kb.prompt("NIM SERVER: " + servStr +"\n");  // Report server's response
        if ( servStr.substring(0,6).equals("Hi Nim") ) {
          String userStr = "";
          do {
            userStr = kb.getKeyboardInput();        // Get user's move
            writeToSocket(socket, userStr + "\n");  // Send it to server
            servStr = readFromSocket(socket);       // Read server's response
            kb.prompt("NIM SERVER: " + servStr + "\n");  // Report response
          } while(servStr.indexOf("Game over!") == -1); // Until game over
        }
      }// requestService()
    
      public static void main(String args[]) {
        NimClient client = new NimClient("localhost", 10001);
        client.start();
      } // main()
    } // NimClient

    Testing the Nim Service

    Testing the One Row Nim service will be no different than testing the Echo service. To test the service, you want to run both NimServer and NimClient at the same time and preferably on different computers. As they are presently coded, you will have to modify the main() methods of both NimServer and NimClient to provide the correct URL and port for your environment.

    The design of the client/server hierarchy makes it easy to create a new service by extending both the Server and Client classes. Describe how you would implement a scramble service with this model. A scramble service can be used by people trying to solve the daily scramble puzzles found in many newspapers. Given a string of letters, the scramble service will return a string containing all possible letter combinations. For example, given “cat,” the scramble service will return “act atc cat cta tac tca.”

    Describe what happens when each of the following errors is introduced into the EchoClient or EchoServer programs:

    • Specify the wrong host name when running EchoClient.
    • Specify the wrong port number when running EchoClient.
    • Remove the reference to \n in the writeToSocket() call in requestService().

    This page titled 15.9: Playing One Row Nim Over the Network is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Ralph Morelli & Ralph Wade via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.