gomoku_game.py 3.7 KB

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