Coder Social home page Coder Social logo

metanet / match-symbols-game Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 773 KB

Match Symbols Game with Python Coroutines

Home Page: https://basri.dev/posts/2020-05-27-writing-a-simple-game-with-python-coroutines-match-symbols/

License: MIT License

Python 100.00%
python3 coroutines tcp-server telnet-client game-server demo curio asyncio concurrency

match-symbols-game's Introduction

Match Symbols Game with Python Coroutines

This repository contains the source code of a very simple game. The game is played by 2 people in turns. There is a deck of symbols and initially all symbols are closed. Each player opens 2 cells on her turn. If both cells have the same symbol, the player’s score is incremented and the player opens 2 other cells. Otherwise, it becomes the other player’s turn. The game is completed when either one of the players manages to match more than half of the symbols, or both match the same number of symbols, which is a tie.

The game is played via a simple and text-based TCP server. The game server supports an arbitrary number of players. Since the game is for 2 players, the game server pairs connecting players in the FIFO order. When a player joins the server, she will wait in the lobby until another player chimes in. When a player disconnects during the game, the other player returns back to the lobby. Games and communication with connected clients are handled with coroutines.

I am mostly done with the development of this game for now. It is available here with the MIT license in case other Python developers may find it interesting or useful. So, have fun!

Demo

You can see a quick demo below. In this demo, I start a game server with the default settings, then connect 2 TELNET clients. When the first client connects, it waits for another client to join. When the second client chimes in, the game starts. Each client opens 2 cells on its turn. After some time, one of the clients just disconnect and the other client goes back to the lobby.

Try it yourself

You can start the game server as below. It binds to localhost:10670 by default. Make sure you have Python 3.7+.

git clone https://github.com/metanet/match-symbols-game.git
cd match-symbols-game 
pip install -r requirements.txt
python3 src/game_server.py

Once the server is up and running, you can connect to it with telnet localhost 10670. If you want to disconnect your TELNET client, press CTRL+], then type close. It is shown in the demo above.

How does it work?

See my blog post.

match-symbols-game's People

Contributors

metanet avatar

Stargazers

 avatar

Watchers

 avatar

match-symbols-game's Issues

Problem at startup, maybe WSL related?

python3 game_server.py
Starting the TCP server on localhost:6700 for the game deck of 4x6.
2020-05-22 00:06:13,838 [ERROR] curio.kernel : Task Crash: Task(id=3, name='tcp_server', state='TERMINATED')
Traceback (most recent call last):
  File "../lib/python3.6/site-packages/curio/kernel.py", line 737, in kernel_run
    trap = current.send(current._trap_result)
  File "../lib/python3.6/site-packages/curio/task.py", line 167, in send
    return self._send(value)
  File "../lib/python3.6/site-packages/curio/task.py", line 171, in _task_runner
    return await coro
  File "../lib/python3.6/site-packages/curio/network.py", line 155, in tcp_server
    sock = tcp_server_socket(host, port, family, backlog, reuse_address, reuse_port)
  File "../lib/python3.6/site-packages/curio/network.py", line 143, in tcp_server_socket
    sock.bind((host, port))
PermissionError: [Errno 13] Permission denied

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.