gomoku_game.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import time
  2. import pywebio
  3. from pywebio.output import *
  4. from pywebio.session import *
  5. goboard_size = 15
  6. # -1 -> none, 0 -> black, 1 -> white
  7. goboard = [
  8. [-1] * goboard_size
  9. for _ in range(goboard_size)
  10. ]
  11. def winner(): # return winner piece, return None if no winner
  12. for x in range(2, goboard_size - 2):
  13. for y in range(2, goboard_size - 2):
  14. # check if (x,y) is the win center
  15. if goboard[x][y] != -1 and any([
  16. all(goboard[x][y] == goboard[m][n] for m, n in [(x - 2, y), (x - 1, y), (x + 1, y), (x + 2, y)]),
  17. all(goboard[x][y] == goboard[m][n] for m, n in [(x, y - 2), (x, y - 1), (x, y + 1), (x, y + 2)]),
  18. 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)]),
  19. 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)]),
  20. ]):
  21. return ['⚫', '⚪'][goboard[x][y]]
  22. session_id = 0 # auto incremented id for each session
  23. current_turn = 0 # 0 for black, 1 for white
  24. player_count = [0, 0] # count of player for two roles
  25. def main():
  26. """Online Shared Gomoku Game
  27. A web based Gomoku (AKA GoBang, Five in a Row) game made with PyWebIO under 100 lines of Python code."""
  28. global session_id, current_turn, goboard
  29. if winner(): # The current game is over, reset game
  30. goboard = [[-1] * goboard_size for _ in range(goboard_size)]
  31. current_turn = 0
  32. my_turn = session_id % 2
  33. my_chess = ['⚫', '⚪'][my_turn]
  34. session_id += 1
  35. player_count[my_turn] += 1
  36. @defer_call
  37. def player_exit():
  38. player_count[my_turn] -= 1
  39. set_env(output_animation=False)
  40. 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
  41. put_markdown(f"""# Online Shared Gomoku Game
  42. 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.
  43. 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)
  44. Currently online player: {player_count[0]} for ⚫, {player_count[1]} for ⚪.
  45. Your role is {my_chess}.
  46. """, lstrip=True)
  47. def set_stone(pos):
  48. global current_turn
  49. if current_turn != my_turn:
  50. toast("It's not your turn!!", color='error')
  51. return
  52. x, y = pos
  53. goboard[x][y] = my_turn
  54. current_turn = (current_turn + 1) % 2
  55. @use_scope('goboard', clear=True)
  56. def show_goboard():
  57. table = [
  58. [
  59. put_buttons([dict(label=' ', value=(x, y), color='light')], onclick=set_stone) if cell == -1 else [' ⚫', ' ⚪'][cell]
  60. for y, cell in enumerate(row)
  61. ]
  62. for x, row in enumerate(goboard)
  63. ]
  64. put_table(table)
  65. show_goboard()
  66. while not winner():
  67. with use_scope('msg', clear=True):
  68. current_turn_copy = current_turn
  69. if current_turn_copy == my_turn:
  70. put_text("It's your turn!")
  71. else:
  72. put_row([put_text("Your opponent's turn, waiting... "), put_loading().style('width:1.5em; height:1.5em')], size='auto 1fr')
  73. while current_turn == current_turn_copy: # wait for next move
  74. time.sleep(0.2)
  75. show_goboard()
  76. with use_scope('msg', clear=True):
  77. put_text('Game over. The winner is %s!\nRefresh page to start a new round.' % winner())
  78. if __name__ == '__main__':
  79. pywebio.start_server(main, debug=True, port=8080, cdn=False)