Git Product home page Git Product logo

tictactoe's Introduction

#Tic Tac Toe Documentation

Build Status Coverage Status Code Climate

A simple game. Or is it!? Try your skill at http://perfecttictactoe.herokuapp.com/

Along with the game there is a "Tic Tac Toe" API, simply post a JSON request indicating the current board state and the "turn" piece. The response will be an updated board state.

API End Point

POST /api/v2/play

Example Request

{
   "player_piece":"o",
   "opponent_piece":"x",
   "board":[
      {
         "id":"top-left",
         "value":""
      },
      {
         "id":"top-center",
         "value":""
      },
      {
         "id":"top-right",
         "value":"x"
      },
      {
         "id":"middle-left",
         "value":""
      },
      {
         "id":"middle-center",
         "value":"o"
      },
      {
         "id":"middle-right",
         "value":""
      },
      {
         "id":"bottom-left",
         "value":"x"
      },
      {
         "id":"bottom-center",
         "value":"o"
      },
      {
         "id":"bottom-right",
         "value":"x"
      }
   ]
}

Example Response

Note the addition of "o" in the top-center and that the player and opponent has switched. Also any spaces in a "winning" line are indicated as such with the attribute "winning_space": true.

{
   "status":"success",
   "data":{
      "player_piece":"x",
      "opponent_piece":"o",
      "board":[
         {
            "id":"top-left",
            "value":""
         },
         {
            "id":"top-center",
            "value":"o"
         },
         {
            "id":"top-right",
            "value":"x",
            "winning_space": true
         },
         {
            "id":"middle-left",
            "value":""
         },
         {
            "id":"middle-center",
            "value":"o",
            "winning_space": true
         },
         {
            "id":"middle-right",
            "value":""
         },
         {
            "id":"bottom-left",
            "value":"x"
         },
         {
            "id":"bottom-center",
            "value":"o",
            "winning_space": true
         },
         {
            "id":"bottom-right",
            "value":"x"
         }
      ]
   }
}

###Development This game was built on top of my sinatra-boilerplate app. Specifics to that boilerplate can be found at its repository.

###Notes on Testing Tests are best executed using the rake tasks:

  • rake test
  • rake system
  • rake js (Run javascript unit tests via jasmine on phantomjs.)
  • rake build_full (For all the tests.)

This is due to conditional configuration based on test type to improve the execution speed. You may run tests directly with the rspec command but this will include all dependencies.

To start a development server and guard simply run:

bundle exec guard

To start a jasmine server to view the results of javascript tests in the browser run:

bundle exec rake jasmine

###Room for Improvement

  • The board class is a little heavy, there may be a way to pull this apart.
  • There may be a way to further speed up the algorithm

##License

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

License located in LICENSE.md

##Change Log

###2.1.1 - March 28, 2015

  • Add GPL v3 License

###2.1.0 - January 23, 2014

  • Split Board into a Board and GameState object for better separation of responsibilities.
  • Improve turn hand off, isolating knowledge of pieces to the GameState.
  • Cleaned up with win check algorithm to leverage a more universal function.
  • Added mechanism to identify which "lines" on a board can never be winning because they contain at least 1 of both players' pieces. These are cached to prevent future checks of that line and improve speed.

###2.0.0 - January 10, 2014

  • Ignore expensive win check if there are not enough pieces for a win to be possible (while following the rules). For example, the soonest a player could win on a 3x3 board is when marking the 5th square.
  • Refactor board state object to improve copying performance and holding the state of the game as moves are made, this prevents expensive win checking of the whole board any time that method is called.
  • Refactor minimax algorithm to employ alpha-beta pruning, which improved performance significantly on the first computer move.
  • Update to version 2 of the api which reports winning spaces, allowing all the win check logic to be stripped out of the client.
  • Refactor client javascript code to simply read/write board and manage turn/messages.
  • Spruced up the styles a little.
  • Various test/code cleaning and performance optimizations.

tictactoe's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tictactoe's Issues

Algorithm Improvement

Firstly came to this repo from your old article which is really good, kudos to that. Secondly, I came across an improvement that could be added. The same is explained below.

Initial state (It is easy to arrive at this state)

X | O | X
  | O |
  |   |

Now If I play X in row 3 col 2 the algorithm the minimax algorithm returns a position (row 3 col 1) like this.

X | O | X
  | O |
O | X |

while this may seem alright, a smart player in addition to predicting the result would place it such that there is a possibility of winning instead of presuming he's up against a good adversary. Long story short the best position should be row 2 col 1 or row 2 col 3

My short improvement is that I maintain a list of all best moves (All moves which have maximum board value and are same) and for all of them I calculate a possibility score denoting the chance of winning ( calculated as two spots occupied by same player and 1 spot empty). In the end If I have multiple best moves I get the one with the maximum possibility score.

I have added the same here.

Question about utility of 'depth' improvement

The way I understand it, if the human opponent plays perfectly, the AI player should at least always be able to determine a draw scenario, (i.e., a score of 0), which will always be more valuable than a score of depth - 10. So, my question is, when will the AI ever be in a situation where it has to choose, say, -6 over -8? (like your example here). Won't 0 always be a choice?

This question is open to anyone who might be passing by, not just the repo maintainer. thanks!

Two player

To improve your game I think you should try and make the game a two player game so two people can play the game so it makes the game more enjoyable as people will play with their friends rather than playing with the computer.

Tutorial site is out of date with ruby code

Just thought it was noteworthy, since your tutorial says it was updated 8 months ago that the Github code and what's in the tutorial have some noticeable differences.

In particular your minimax function accepts upperBound and lowerBound arguments which are never discussed.

Handy tutorial, but I'm wondering if it tells the whole picture.

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.