Skip to content

Commit d0482b9

Browse files
committed
demo update
1 parent 4a5fa81 commit d0482b9

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

demos/gomoku_game.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
"""
22
Online Shared Gomoku Game
33
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4-
All online players are divided into two roles (black and white) and play same Gomoku game together.
4+
All online players are assigned to two groups (black and white) and play same Gomoku game together.
55
66
:demo_host:`Demo </gomoku_game>`, `Source code <https://github.com/wang0618/PyWebIO/blob/dev/demos/gomoku_game.py>`_
77
"""
88
import pywebio, time
99
from pywebio.output import *
1010
from pywebio.session import *
1111

12-
chessboard_size = 15
12+
goboard_size = 15
1313
# -1 -> none, 0 -> black, 1 -> white
14-
chessboard = [
15-
[-1] * chessboard_size
16-
for _ in range(chessboard_size)
14+
goboard = [
15+
[-1] * goboard_size
16+
for _ in range(goboard_size)
1717
]
1818

1919
def winner(): # return winner piece, return None if no winner
20-
for x in range(2, chessboard_size - 2):
21-
for y in range(2, chessboard_size - 2):
20+
for x in range(2, goboard_size - 2):
21+
for y in range(2, goboard_size - 2):
2222
# check if (x,y) is the win center
23-
if chessboard[x][y] != -1 and any([
24-
all(chessboard[x][y] == chessboard[m][n] for m, n in [(x - 2, y), (x - 1, y), (x + 1, y), (x + 2, y)]),
25-
all(chessboard[x][y] == chessboard[m][n] for m, n in [(x, y - 2), (x, y - 1), (x, y + 1), (x, y + 2)]),
26-
all(chessboard[x][y] == chessboard[m][n] for m, n in [(x - 2, y - 2), (x - 1, y - 1), (x + 1, y + 1), (x + 2, y + 2)]),
27-
all(chessboard[x][y] == chessboard[m][n] for m, n in [(x - 2, y + 2), (x - 1, y + 1), (x + 1, y - 1), (x + 2, y - 2)]),
23+
if goboard[x][y] != -1 and any([
24+
all(goboard[x][y] == goboard[m][n] for m, n in [(x - 2, y), (x - 1, y), (x + 1, y), (x + 2, y)]),
25+
all(goboard[x][y] == goboard[m][n] for m, n in [(x, y - 2), (x, y - 1), (x, y + 1), (x, y + 2)]),
26+
all(goboard[x][y] == goboard[m][n] for m, n in [(x - 2, y - 2), (x - 1, y - 1), (x + 1, y + 1), (x + 2, y + 2)]),
27+
all(goboard[x][y] == goboard[m][n] for m, n in [(x - 2, y + 2), (x - 1, y + 1), (x + 1, y - 1), (x + 2, y - 2)]),
2828
]):
29-
return ['⚫', '⚪'][chessboard[x][y]]
29+
return ['⚫', '⚪'][goboard[x][y]]
3030

3131
session_id = 0 # auto incremented id for each session
3232
current_turn = 0 # 0 for black, 1 for white
3333
player_count = [0, 0] # count of player for two roles
3434
def main():
3535
"""Online Shared Gomoku Game
3636
37-
All online players are divided into two roles (black and white) and share this game."""
38-
global session_id, current_turn, chessboard
37+
A web based Gomoku (AKA GoBang, Five in a Row) game made with PyWebIO under 100 lines of Python code."""
38+
global session_id, current_turn, goboard
3939
if winner(): # The current game is over, reset game
40-
chessboard = [[-1] * chessboard_size for _ in range(chessboard_size)]
40+
goboard = [[-1] * goboard_size for _ in range(goboard_size)]
4141
current_turn = 0
4242

4343
my_turn = session_id % 2
@@ -53,33 +53,33 @@ def player_exit():
5353
put_html("""<style> table th, table td { padding: 0px !important;} button {padding: .75rem!important; margin:0!important} </style>""") # Custom styles to make the board more beautiful
5454

5555
put_markdown(f"""# Online Shared Gomoku Game
56-
All online players are divided into two roles (black and white) and share this game. You can open this page in multiple tabs of your browser to simulate multiple users.
56+
All online players are assigned to two groups (black and white) and share this game. \nYou can open this page in multiple tabs of your browser to simulate multiple users.
5757
This application uses less than 100 lines of code, the source code is [here](https://github.com/wang0618/PyWebIO/blob/dev/demos/gomoku_game.py)
5858
Currently online player: {player_count[0]} for ⚫, {player_count[1]} for ⚪.
5959
Your role is {my_chess}.
6060
""", lstrip=True)
6161

62-
def set_chess(pos):
62+
def set_stone(pos):
6363
global current_turn
6464
if current_turn != my_turn:
6565
toast("It's not your turn!!", color='error')
6666
return
6767
x, y = pos
68-
chessboard[x][y] = my_turn
68+
goboard[x][y] = my_turn
6969
current_turn = (current_turn + 1) % 2
7070

71-
@use_scope('chessboard', clear=True)
72-
def show_chessboard():
71+
@use_scope('goboard', clear=True)
72+
def show_goboard():
7373
table = [
7474
[
75-
put_buttons([dict(label=' ', value=(x, y), color='light')], onclick=set_chess) if cell == -1 else [' ⚫', ' ⚪'][cell]
75+
put_buttons([dict(label=' ', value=(x, y), color='light')], onclick=set_stone) if cell == -1 else [' ⚫', ' ⚪'][cell]
7676
for y, cell in enumerate(row)
7777
]
78-
for x, row in enumerate(chessboard)
78+
for x, row in enumerate(goboard)
7979
]
8080
put_table(table)
8181

82-
show_chessboard()
82+
show_goboard()
8383
while not winner():
8484
with use_scope('msg', clear=True):
8585
current_turn_copy = current_turn
@@ -89,7 +89,7 @@ def show_chessboard():
8989
put_row([put_text("Your opponent's turn, waiting... "), put_loading().style('width:1.5em; height:1.5em')], size='auto 1fr')
9090
while current_turn == current_turn_copy: # wait for next move
9191
time.sleep(0.2)
92-
show_chessboard()
92+
show_goboard()
9393
with use_scope('msg', clear=True):
9494
put_text('Game over. The winner is %s!\nRefresh page to start a new round.' % winner())
9595

demos/markdown_previewer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def main():
2020
The online markdown editor with live preview. The source code of this application is [here](https://github.com/wang0618/PyWebIO/blob/dev/demos/markdown_previewer.py).
2121
## Write your Markdown
2222
""", lstrip=True)
23-
put_textarea('md_text', rows=18, code=True)
23+
put_textarea('md_text', rows=18, code={'mode': 'markdown'})
2424

2525
put_buttons(['Download content'], lambda _: download('saved.md', pin.md_text.encode('utf8')), small=True)
2626

0 commit comments

Comments
 (0)