Coder Social home page Coder Social logo

matchilling / rockpaperscissors Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 6.0 152 KB

The purpose of this experimental program is to provide a network version of the hand game Rock-paper-scissors using TCP.

License: MIT License

Java 100.00%
rock-paper-scissors java-game scissors rock paper tcp

rockpaperscissors's Introduction

Rock-Paper-Scissors TCP network edition in JAVA

The purpose of the program is to provide a network version of the hand game Rock-paper-scissors ✊ ✋ ✌️ using the Transmission Control Protocol.

The server class – Server.java

Execution of the server class script (Server.java) Illustr. 1: Execution of the server class script (Server.java)

By executing the server class (Server.java) the user will be prompt to specify a port number which has to be an integer value strictly greater than zero and less than or equal to 65535. Alternatively “0” is accepted for the standard port (1337) . After submitting the script runs a little validation and checks if the value is within the defined range (0 > x <= 65535) and sets the port value. So far no further validation is implemented, we're not verifying for example if the selected port is reserved or just busy at the moment. If no exception is thrown the program dumps a status message to inform the user, that the server is running on the specified port number.

Since the socket is not closed the server remains in a while() loop waiting for two incoming connections (player one and player two). A corresponding status message will be provided if a new client connects.

The server accepts an input stream from both clients. Once the players have sent their packets, the program computes a result based on the user inputs (R – rock ✊, S – scissors ✌️, P - paper ✋) and the following rule set. Rock beats scissors, scissors beats paper, paper beats rock.

If the characters received from client one and client two are the same, then the server sends back to both clients the string "DRAW". If the server receives “R” from client one and “S” from client two, it sends the string “YOU WIN" to client one and the string "YOU LOSE" to client two. If the server receives “S” from client one and “R” from client two, it sends the string "YOU LOSE" to client one and the string "YOU WIN" to client two.

The following table shows all nine possible combinations.

Input Result
Client 1 Client 2 Client 1 Client 2
R R Draw Draw
S S Draw Draw
P P Draw Draw
R S You Win You Lose
S R You Lose You Win
R P You Lose You Win
P R You Win You Lose
S P You Win You Lose
P S You Lose You Win

The result will be echoed out and a correspondent massage will be send to each client. Afterwards the server waits again for two clients to connect.

The client class – Client.java

![Client one chooses "R" and wins](/resources/images/Client1.png "Client one chooses "R" and wins") Illustr. 2: Client one chooses "R" and wins

![Client two chooses "S" and loses](/resources/images/Client2.png "Client two chooses "S" and loses") Illustr. 3: Client two chooses "S" and loses

The client class (Client.java) creates a connection to the server on the local host at the default port 1337. If the connection has been successfully established the script prompts the user to choose a correspondent character to (R)ock, (P)aper or (S)cissors. By typing “-rules” the rule set can be displayed alternatively. After sending the character to the server via the TCP protocol the client waits for a reply from the server and a notification will be dumped.

Once the client receives a response from the server the message will be printed to the screen and the connection will be closed.

License

The MIT License (MIT)

Copyright (c) 2015 Mathias Schilling [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Support or Contact

Having trouble with this repository? Check out the documentation at the repository's site or contact info@pinkbigmacmedia.com and we’ll help you sort it out.

Happy Coding

rockpaperscissors's People

Contributors

matchilling avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.