Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Version 1.0.0

  • Loading branch information...
commit d324c1171b1052ded71564c234feab29681f3050 1 parent be750d0
@skalnik authored
View
2  README.rdoc
@@ -1,6 +1,6 @@
= tic-tac-toe
-Description goes here.
+A very simple tic-tac-toe game.
== Note on Patches/Pull Requests
View
4 Rakefile
@@ -5,8 +5,8 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "tic-tac-toe"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.summary = %Q{A simple tic-tac-toe game!}
+ gem.description = %Q{A very simple tic-tac-toe game. Run `tic-tac-toe` to play.}
gem.email = "mike.skalnik@gmail.com"
gem.homepage = "http://github.com/skalnik/tic-tac-toe"
gem.authors = ["Mike Skalnik"]
View
27 bin/tic-tac-toe
@@ -0,0 +1,27 @@
+#!/usr/bin/env ruby
+
+require 'tic-tac-toe'
+
+board = Board.new
+player_one = Player.new('X')
+player_two = Player.new('O')
+
+until board.game_over?
+ puts board
+ print "Make a move, player 1 (1-9): "
+ position = gets
+ player_one.make_move(board, position)
+ break if board.game_over?
+ puts board
+ print "Make a move, player 2 (1-9): "
+ position = gets
+ player_two.make_move(board, position)
+end
+
+puts board
+winner = board.get_winner
+if winner
+ puts "#{winner} Won!"
+else
+ puts "Tie game!"
+end
View
2  lib/tic-tac-toe.rb
@@ -0,0 +1,2 @@
+require 'tic-tac-toe/board'
+require 'tic-tac-toe/player'
View
47 lib/tic-tac-toe/board.rb
@@ -0,0 +1,47 @@
+class Board
+ # Board Positions:
+ # 0 | 1 | 2
+ # ---------
+ # 3 | 4 | 5
+ # ---------
+ # 6 | 7 | 8
+
+ def initialize
+ @board = Array.new()
+ end
+
+ def to_s
+ str = "-----------\n"
+ (1..3).to_a.each do |i|
+ str << " #{@board[(i*3)-3] || (i*3)-2} | #{@board[(i*3)-2] || (i*3)-1} | #{@board[(i*3)-1] || i*3} \n"
+ str << "-----------\n"
+ end
+ str
+ end
+
+ def place_marker(marker, location)
+ if location < 9 && location >= 0
+ unless @board[location].nil?
+ raise 'Location already taken'
+ else
+ @board[location] = marker
+ end
+ else
+ raise 'Location does not exist'
+ end
+ end
+
+ def get_winner
+ return @board[0] if(@board[0] == @board[1] && @board[1] == @board[2]) # Top Row
+ return @board[3] if(@board[3] == @board[4] && @board[4] == @board[5]) # Middle Row
+ return @board[6] if(@board[6] == @board[7] && @board[7] == @board[8]) # Bottom Row
+ return @board[0] if(@board[0] == @board[4] && @board[4] == @board[8]) # Diagonal from Top Right
+ return @board[2] if(@board[2] == @board[4] && @board[4] == @board[6]) # Diagonal from Top Left
+ return nil # No one has won
+ end
+
+ def game_over?
+ (0..8).inject(true) { |val, i| val && @board[i] } || get_winner
+ end
+end
+
View
9 lib/tic-tac-toe/player.rb
@@ -0,0 +1,9 @@
+class Player
+ def initialize(marker)
+ @marker = marker
+ end
+
+ def make_move(board, location)
+ board.place_marker(@marker, location.to_i - 1)
+ end
+end
View
55 tic-tac-toe.gemspec
@@ -0,0 +1,55 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{tic-tac-toe}
+ s.version = "1.0.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Mike Skalnik"]
+ s.date = %q{2010-11-02}
+ s.default_executable = %q{tic-tac-toe}
+ s.description = %q{A very simple tic-tac-toe game. Run `tic-tac-toe` to play.}
+ s.email = %q{mike.skalnik@gmail.com}
+ s.executables = ["tic-tac-toe"]
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.rdoc"
+ ]
+ s.files = [
+ ".document",
+ ".gitignore",
+ "LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION",
+ "lib/tic-tac-toe.rb",
+ "test/helper.rb",
+ "test/test_tic-tac-toe.rb"
+ ]
+ s.homepage = %q{http://github.com/skalnik/tic-tac-toe}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.7}
+ s.summary = %q{A simple tic-tac-toe game!}
+ s.test_files = [
+ "test/helper.rb",
+ "test/test_tic-tac-toe.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
+ else
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
+ end
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.