Implementing rock, paper, scissor game using Jaclang and Spatial Data Structure.
Imports: The code begins with importing the random module from Python, which is used to generate random choices for the opponent's move.
Global Variable: It declares a global variable i of type integer and initializes it to 0.It uses to count node number
Walker Definition: Defines a walker named Creator. This walker has the capability do, which allows it to perform actions with the root entry.
Node Definition: Defines a node named play. Each play node represents a single round of the game.
- user: Represents the user's choice input.
- opponent: Represents the opponent's randomly chosen move.
- val: Represents the value of the node.
Defines the behavior of the Creater walker's do capability. It increments the global variable i and spawns a play node with the current value of i.
Defines the behavior of the play node's compete capability. It:
- Assigns the user's input to the player variable and generates a random choice for the opponent's move.
- Checks if the player's input is valid (either 'r', 'p', or 's').
- Checks if the player wins, loses, or if it's a tie based on the rules of Rock-Paper-Scissors.
- Prints the result along with both the player's and opponent's choices.
- Defines an inner capability is_won() to check if the player wins against the opponent.
Starts the main loop with the with entry block. It:
- Displays a welcome message for the Rock-Paper-Scissors game.
- Enters a loop that prompts the user to choose between playing a round or quitting the game.
- If the user chooses to play, it spawns a Creator walker, initiating a new round of the game. If the user chooses to quit, the loop breaks.
- The variable i is incremented after each round.
Overall, this code implements a basic Rock-Paper-Scissors game where the user can play multiple rounds until they choose to quit.
Note :-