Code Monkey home page Code Monkey logo

django-tictactoe's Introduction

Tic-Tac-Toe in Django

https://travis-ci.org/paulcwatts/django-tictactoe.png?branch=master

For people new to Django (or to programming in general) I tend to give people a challenge to write a tic-tac-toe game. This is my solution.

It's not only a working tic-tac-toe game, but it also includes many standard patterns used by Django apps such as unit testing models and views, importing modules, and testing with tox and Travis.

Building

The easiest way to get this set up is to use virtualenv. Usually it's easy as:

sudo pip install virtualenv

Once you have virtualenv and the code, you can create a virtualenv and install the dependencies:

cd <path_to_code>
mkvirtualenv tictactoe
pip install -r requirements.txt

Then run the server:

python manage.py runserver

Design

The game state is stored in the Game model. The board is stored as a 9 character string, with the option of having multiple player types:

class Game(models.Model):
    board = models.CharField(max_length=9, default=" " * 9)

    player_x = models.CharField(max_length=64)
    player_o = models.CharField(max_length=64)

You can see the rest of the logic of the same by checking out the models file: https://github.com/paulcwatts/django-tictactoe/blob/master/game/models.py

Each player is stored as a string which can either be "human" or the name of a class that implements the player logic. Player logic is separated out into classes of their own to maintain a good separation of roles. Right now there's only a player that plays randomly, but the intent is to create a new player that plays perfectly.

The UI for the game is fairly simplistic, and relies entirely on Django templates and views. It's not actually the way I would write it -- I'd create a front-end Javascript solution and use a JSON API to send and receive moves.

Testing

All of the code is tested, which is a good primer if you're new to testing Django applications: https://github.com/paulcwatts/django-tictactoe/tree/master/game/tests If you're unfamiliar with many of the testing tools available to Django apps, I have slides and code from a talk I did on testing Django).

Future

Eventually I'd like to create a real Javascript front-end and API. But for now this was specifically about Django development.

django-tictactoe's People

Contributors

paulcwatts avatar cleverprogrammer avatar

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.