XAndOs
x_and_os provides the basic structure and game logic to easily create a Tic Tac Toe game. It also comes with a mini command line app to play Tic Tac Toe / x and os.
Installation
Add this line to your application's Gemfile:
gem 'x_and_os'
And then execute:
$ bundle
Or install it yourself as:
$ gem install x_and_os
Usage
require 'x_and_os'
Game
Used to manage the basic state of game.
Initialize a new game with:
XAndOS::Game.new
or
include XAndOs
Game.new
By default Game.new
will create a new game object that uses the XAndOs::Board that has 3rows and 3columns.
It is possible to set rows and columns when instantiating a new game
XAndOs::Game.new(rows: 4, columns: 3)
If you wish to use a different game board (not recommended):
custom_game_board = Board.new
XAndOs::Game.new(board: custom_game_board)
game API
add_move
takes a cell number and a marker(optional) to put in the cell.
@game = XAndOs::Game.new
@game.add_move(5, 'x')
@game.add_move(1, 'o')
or
@game.add_move(5)
@game.add_move(1)
Will result in a board with a grid that looks like this.
@game.board.grid # [['o',' ',' '],
# [' ','x',' '],
# [' ',' ',' ']]
Cell numbers map left to right as such :
[[1,2,3],
[4,5,6],
[7,8,9]]
get_marker
returns the value of the next marker 'X' or 'O' depending on number of turns made. First move is always 'X'
winner?
returns true
if there is a complete line e.g
# x | | o
# --------
# o | x |
# --------
# | | x
draw?
will return true
when there are no moves available.
Board
TODO
Game Mastery
TODO
Command Line App
You can use x_and_os to play Tic Tac Toe / X and Os in the command line.
After installing the gem run :
$ x_and_os
And then follow the menu options.
- Quick game sets up a You vs Computer on 3x3 game board.
- settings allows the user to
- pick two players (either player(human) or computer)
- choose a grid size to play on. e.g 3x3 or 5x5
- The computer player uses the GameMaster class to be make the best possible moves (unbeatable on 3x3 board)
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
to create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
- Fork it ( https://github.com/[my-github-username]/x_and_os/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request