wangweimin 3 vuotta sitten
vanhempi
säilyke
d0482b9651
2 muutettua tiedostoa jossa 26 lisäystä ja 26 poistoa
  1. 25 25
      demos/gomoku_game.py
  2. 1 1
      demos/markdown_previewer.py

+ 25 - 25
demos/gomoku_game.py

@@ -1,7 +1,7 @@
 """
 """
 Online Shared Gomoku Game
 Online Shared Gomoku Game
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-All online players are divided into two roles (black and white) and play same Gomoku game together.
+All online players are assigned to two groups (black and white) and play same Gomoku game together.
 
 
 :demo_host:`Demo </gomoku_game>`, `Source code <https://github.com/wang0618/PyWebIO/blob/dev/demos/gomoku_game.py>`_
 :demo_host:`Demo </gomoku_game>`, `Source code <https://github.com/wang0618/PyWebIO/blob/dev/demos/gomoku_game.py>`_
 """
 """
@@ -9,24 +9,24 @@ import pywebio, time
 from pywebio.output import *
 from pywebio.output import *
 from pywebio.session import *
 from pywebio.session import *
 
 
-chessboard_size = 15
+goboard_size = 15
 # -1 -> none, 0 -> black, 1 -> white
 # -1 -> none, 0 -> black, 1 -> white
-chessboard = [
-    [-1] * chessboard_size
-    for _ in range(chessboard_size)
+goboard = [
+    [-1] * goboard_size
+    for _ in range(goboard_size)
 ]
 ]
 
 
 def winner():  # return winner piece, return None if no winner
 def winner():  # return winner piece, return None if no winner
-    for x in range(2, chessboard_size - 2):
-        for y in range(2, chessboard_size - 2):
+    for x in range(2, goboard_size - 2):
+        for y in range(2, goboard_size - 2):
             # check if (x,y) is the win center
             # check if (x,y) is the win center
-            if chessboard[x][y] != -1 and any([
-                all(chessboard[x][y] == chessboard[m][n] for m, n in [(x - 2, y), (x - 1, y), (x + 1, y), (x + 2, y)]),
-                all(chessboard[x][y] == chessboard[m][n] for m, n in [(x, y - 2), (x, y - 1), (x, y + 1), (x, y + 2)]),
-                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)]),
-                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)]),
+            if goboard[x][y] != -1 and any([
+                all(goboard[x][y] == goboard[m][n] for m, n in [(x - 2, y), (x - 1, y), (x + 1, y), (x + 2, y)]),
+                all(goboard[x][y] == goboard[m][n] for m, n in [(x, y - 2), (x, y - 1), (x, y + 1), (x, y + 2)]),
+                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)]),
+                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)]),
             ]):
             ]):
-                return ['⚫', '⚪'][chessboard[x][y]]
+                return ['⚫', '⚪'][goboard[x][y]]
 
 
 session_id = 0          # auto incremented id for each session
 session_id = 0          # auto incremented id for each session
 current_turn = 0        # 0 for black, 1 for white
 current_turn = 0        # 0 for black, 1 for white
@@ -34,10 +34,10 @@ player_count = [0, 0]   # count of player for two roles
 def main():
 def main():
     """Online Shared Gomoku Game
     """Online Shared Gomoku Game
 
 
-    All online players are divided into two roles (black and white) and share this game."""
-    global session_id, current_turn, chessboard
+    A web based Gomoku (AKA GoBang, Five in a Row) game made with PyWebIO under 100 lines of Python code."""
+    global session_id, current_turn, goboard
     if winner():  # The current game is over, reset game
     if winner():  # The current game is over, reset game
-        chessboard = [[-1] * chessboard_size for _ in range(chessboard_size)]
+        goboard = [[-1] * goboard_size for _ in range(goboard_size)]
         current_turn = 0
         current_turn = 0
 
 
     my_turn = session_id % 2
     my_turn = session_id % 2
@@ -53,33 +53,33 @@ def main():
     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
     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
 
 
     put_markdown(f"""# Online Shared Gomoku Game
     put_markdown(f"""# Online Shared Gomoku Game
-    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.
+    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.
     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)
     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)
     Currently online player: {player_count[0]} for ⚫, {player_count[1]} for ⚪.
     Currently online player: {player_count[0]} for ⚫, {player_count[1]} for ⚪.
     Your role is {my_chess}.
     Your role is {my_chess}.
     """, lstrip=True)
     """, lstrip=True)
 
 
-    def set_chess(pos):
+    def set_stone(pos):
         global current_turn
         global current_turn
         if current_turn != my_turn:
         if current_turn != my_turn:
             toast("It's not your turn!!", color='error')
             toast("It's not your turn!!", color='error')
             return
             return
         x, y = pos
         x, y = pos
-        chessboard[x][y] = my_turn
+        goboard[x][y] = my_turn
         current_turn = (current_turn + 1) % 2
         current_turn = (current_turn + 1) % 2
 
 
-    @use_scope('chessboard', clear=True)
-    def show_chessboard():
+    @use_scope('goboard', clear=True)
+    def show_goboard():
         table = [
         table = [
             [
             [
-                put_buttons([dict(label=' ', value=(x, y), color='light')], onclick=set_chess) if cell == -1 else [' ⚫', ' ⚪'][cell]
+                put_buttons([dict(label=' ', value=(x, y), color='light')], onclick=set_stone) if cell == -1 else [' ⚫', ' ⚪'][cell]
                 for y, cell in enumerate(row)
                 for y, cell in enumerate(row)
             ]
             ]
-            for x, row in enumerate(chessboard)
+            for x, row in enumerate(goboard)
         ]
         ]
         put_table(table)
         put_table(table)
 
 
-    show_chessboard()
+    show_goboard()
     while not winner():
     while not winner():
         with use_scope('msg', clear=True):
         with use_scope('msg', clear=True):
             current_turn_copy = current_turn
             current_turn_copy = current_turn
@@ -89,7 +89,7 @@ def main():
                 put_row([put_text("Your opponent's turn, waiting... "), put_loading().style('width:1.5em; height:1.5em')], size='auto 1fr')
                 put_row([put_text("Your opponent's turn, waiting... "), put_loading().style('width:1.5em; height:1.5em')], size='auto 1fr')
             while current_turn == current_turn_copy:  # wait for next move
             while current_turn == current_turn_copy:  # wait for next move
                 time.sleep(0.2)
                 time.sleep(0.2)
-            show_chessboard()
+            show_goboard()
     with use_scope('msg', clear=True):
     with use_scope('msg', clear=True):
         put_text('Game over. The winner is %s!\nRefresh page to start a new round.' % winner())
         put_text('Game over. The winner is %s!\nRefresh page to start a new round.' % winner())
 
 

+ 1 - 1
demos/markdown_previewer.py

@@ -20,7 +20,7 @@ def main():
     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).
     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).
     ## Write your Markdown
     ## Write your Markdown
     """, lstrip=True)
     """, lstrip=True)
-    put_textarea('md_text', rows=18, code=True)
+    put_textarea('md_text', rows=18, code={'mode': 'markdown'})
 
 
     put_buttons(['Download content'], lambda _: download('saved.md', pin.md_text.encode('utf8')), small=True)
     put_buttons(['Download content'], lambda _: download('saved.md', pin.md_text.encode('utf8')), small=True)