-
Notifications
You must be signed in to change notification settings - Fork 40
Create rock_paper_scissors.py #1534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,78 @@ | ||||||||||||||
| # { "Depends": "py-genlayer:1jb45aa8ynh2a9c9xn3b7qqh8sm5q93hwfp7jqmwsfhh8jpz09h6" } | ||||||||||||||
| from genlayer import * | ||||||||||||||
| import json | ||||||||||||||
|
|
||||||||||||||
| class RockPaperScissors(gl.Contract): | ||||||||||||||
| last_result: str | ||||||||||||||
| player_wins: u256 | ||||||||||||||
| ai_wins: u256 | ||||||||||||||
| ties: u256 | ||||||||||||||
|
|
||||||||||||||
| def __init__(self): | ||||||||||||||
| self.last_result = "No game played yet" | ||||||||||||||
| self.player_wins = 0 | ||||||||||||||
| self.ai_wins = 0 | ||||||||||||||
| self.ties = 0 | ||||||||||||||
|
|
||||||||||||||
| @gl.public.write | ||||||||||||||
| def play(self, player_move: str) -> None: | ||||||||||||||
| player_move = player_move.lower().strip() | ||||||||||||||
| if player_move not in ["rock", "paper", "scissors"]: | ||||||||||||||
| return | ||||||||||||||
|
Comment on lines
+19
to
+21
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't silently accept invalid moves. Line 21 returns without updating 🛠️ Proposed fix player_move = player_move.lower().strip()
if player_move not in ["rock", "paper", "scissors"]:
- return
+ raise ValueError("player_move must be one of: rock, paper, scissors")📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
|
|
||||||||||||||
| prompt = """ | ||||||||||||||
| You are playing Rock Paper Scissors. | ||||||||||||||
| Pick one move from: rock, paper, scissors. | ||||||||||||||
|
|
||||||||||||||
| Respond using ONLY the following format: | ||||||||||||||
| { | ||||||||||||||
| "move": str | ||||||||||||||
| } | ||||||||||||||
| It is mandatory that you respond only using the JSON format above, | ||||||||||||||
| nothing else. Don't include any other words or characters, | ||||||||||||||
| your output must be only JSON without any formatting prefix or suffix. | ||||||||||||||
| This result should be perfectly parseable by a JSON parser without errors. | ||||||||||||||
| """ | ||||||||||||||
|
|
||||||||||||||
| def nondet(): | ||||||||||||||
| res = gl.nondet.exec_prompt(prompt) | ||||||||||||||
| backticks = "``" + "`" | ||||||||||||||
| res = res.replace(backticks + "json", "").replace(backticks, "") | ||||||||||||||
| dat = json.loads(res) | ||||||||||||||
| return dat["move"].lower().strip() | ||||||||||||||
|
|
||||||||||||||
| ai_move = gl.eq_principle.strict_eq(nondet) | ||||||||||||||
|
|
||||||||||||||
| if ai_move not in ["rock", "paper", "scissors"]: | ||||||||||||||
| ai_move = "rock" | ||||||||||||||
|
Comment on lines
+37
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move AI-response fallback inside The fallback on Lines 46-47 only runs after 🛠️ Proposed fix- def nondet() -> str:
+ def nondet() -> str:
res = gl.nondet.exec_prompt(prompt)
backticks = "``" + "`"
- res = res.replace(backticks + "json", "").replace(backticks, "")
- dat = json.loads(res)
- return dat["move"].lower().strip()
+ cleaned = res.replace(backticks + "json", "").replace(backticks, "")
+ try:
+ move = json.loads(cleaned)["move"].lower().strip()
+ except (TypeError, KeyError, json.JSONDecodeError):
+ return "rock"
+ return move if move in ["rock", "paper", "scissors"] else "rock"
ai_move = gl.eq_principle.strict_eq(nondet)
-
- if ai_move not in ["rock", "paper", "scissors"]:
- ai_move = "rock"🧰 Tools🪛 Ruff (0.15.5)[error] 38-38: (F405) [error] 44-44: (F405) 🤖 Prompt for AI Agents |
||||||||||||||
|
|
||||||||||||||
| if player_move == ai_move: | ||||||||||||||
| outcome = "tie" | ||||||||||||||
| self.ties += 1 | ||||||||||||||
| elif ( | ||||||||||||||
| (player_move == "rock" and ai_move == "scissors") or | ||||||||||||||
| (player_move == "scissors" and ai_move == "paper") or | ||||||||||||||
| (player_move == "paper" and ai_move == "rock") | ||||||||||||||
| ): | ||||||||||||||
| outcome = "player" | ||||||||||||||
| self.player_wins += 1 | ||||||||||||||
| else: | ||||||||||||||
| outcome = "ai" | ||||||||||||||
| self.ai_wins += 1 | ||||||||||||||
|
|
||||||||||||||
| self.last_result = ( | ||||||||||||||
| f"Player: {player_move} | AI: {ai_move} | " | ||||||||||||||
| f"Winner: {outcome}" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| @gl.public.view | ||||||||||||||
| def get_last_result(self) -> str: | ||||||||||||||
| return self.last_result | ||||||||||||||
|
|
||||||||||||||
| @gl.public.view | ||||||||||||||
| def get_score(self) -> str: | ||||||||||||||
| return ( | ||||||||||||||
| f"Player: {self.player_wins} | " | ||||||||||||||
| f"AI: {self.ai_wins} | " | ||||||||||||||
| f"Ties: {self.ties}" | ||||||||||||||
| ) | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: genlayerlabs/genlayer-studio
Length of output: 12946
Clarify whether
from genlayer import *is intentional or if examples should be exempt from linting.This star import pattern is used consistently across the entire codebase (tests, examples, and integration contracts), but
F403andF405are not listed in the globalignorerules inruff.toml. Either confirm that examples are excluded from linting in pre-commit or CI configuration, or adopt explicit imports if the SDK API is intended to be visible at the call site.🧰 Tools
🪛 Ruff (0.15.5)
[error] 2-2:
from genlayer import *used; unable to detect undefined names(F403)
🤖 Prompt for AI Agents