tic_tac_toe_vj 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3790c471193ac46e14077ce32db0854d34fbfee1d7109ade0a430cf2c796438d
4
+ data.tar.gz: '09cfe835c06322566feacbfd099eacd702538a1257aed4a890f340f6dcb13aa4'
5
+ SHA512:
6
+ metadata.gz: 67b82d45f7c09264a72af6a4e34c5b98622510ceef7e1748f47acee589d6791dca04fa110a350ea39d8dbf92082e760ed6e0fcfccf09c59e3a1b9e4f7b2d0b4c
7
+ data.tar.gz: 47969150802662b56479f720a2c5c10402b0fade5a07c99057f64aef91bdd40cd3d0d3a38587d2957c4eae19c596cf69986d7ffa69104da2c094c8f34c677190
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.16.6
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in tic_tac_toe_vj.gemspec
6
+ gemspec
7
+
8
+ gem 'rspec'
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # TicTacToeVj
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tic_tac_toe_vj`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'tic_tac_toe_vj'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tic_tac_toe_vj
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tic_tac_toe_vj.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tic_tac_toe_vj"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,89 @@
1
+ require "tic_tac_toe_vj/version"
2
+ require 'tic_tac_toe_vj/board'
3
+ require 'tic_tac_toe_vj/game'
4
+ require 'tic_tac_toe_vj/player'
5
+ require 'tic_tac_toe_vj/location'
6
+ require 'tic_tac_toe_vj/game_status'
7
+
8
+ class TicTakToeController
9
+ include TicTacToeVj
10
+ include GameStatus
11
+
12
+ def initialize(player1_name="Vijay", player2_name="Ajay")
13
+ @board = Board.new
14
+ @tile_location = Location.new(0,0)
15
+ @player1 = Player.new(player1_name, :O)
16
+ @player2 = Player.new(player2_name, :X)
17
+ @game = Game.new(@board, @player1, @player2)
18
+ end
19
+
20
+ def start
21
+ puts "Tic tak toe is starting -------------"
22
+ sleep(1)
23
+ puts "terns should be write as row coma column ex 1,1 this will Enter your Symbol in the middle"
24
+ sleep(0.5)
25
+ user_play_move
26
+ end
27
+
28
+ def validate_input?(user_input)
29
+ split_input = user_input.split(",")
30
+ validate = split_input.length == 2
31
+ if validate
32
+ split_input.each do |index|
33
+ validate = validate && check_int?(index) && index.to_i >= 0 && index.to_i <= 2
34
+ end
35
+ end
36
+ return validate
37
+ end
38
+
39
+ def reset_game
40
+ @board = Board.new
41
+ @tile_location = Location.new(0,0)
42
+ @player1 = Player.new(@player1.name, :cross)
43
+ @player2 = Player.new(@player2.name, :zero)
44
+ @game = Game.new(@board, @player1, @player2)
45
+ start
46
+ end
47
+
48
+ private
49
+
50
+ def update_game_after_move(gamestatus)
51
+ case gamestatus
52
+ when GameStatus::NEXT_MOVE
53
+ puts "your tern #{@game.current_player.name}"
54
+ user_play_move
55
+ when GameStatus::DRAW
56
+ puts "its a draw"
57
+ puts "Game is starting again"
58
+ sleep(1)
59
+ reset_game
60
+ else
61
+ "#{@game.current_player.name} wins"
62
+ end
63
+ end
64
+
65
+ def check_int?(int)
66
+ result = Integer(int) rescue false
67
+ return result == false ? false : true
68
+ end
69
+
70
+ def user_play_move
71
+ puts "#{@board.tile}"
72
+ puts "Enter your tern #{@game.current_player.name}"
73
+ location = gets.chomp
74
+ if validate_input?(location)
75
+ move_locations = location.split(",")
76
+ @tile_location = Location.new(move_locations[0].to_i,move_locations[1].to_i)
77
+ if @board.is_tile_not_marked?(@tile_location)
78
+ @board.mark_tile(@tile_location, @game.current_player.symbol)
79
+ @game.play_move(@tile_location)
80
+ else
81
+ puts "location is already marked please do terns again"
82
+ user_play_move
83
+ end
84
+ else
85
+ puts "invalid input please give a valid input in the form of 1,1"
86
+ end
87
+ update_game_after_move(@game.status)
88
+ end
89
+ end
@@ -0,0 +1,73 @@
1
+ require __dir__ + '/location'
2
+
3
+ class Board
4
+ BOARD_SIZE = 3
5
+
6
+ attr_reader :tile
7
+
8
+ def initialize()
9
+ @number_of_tiles_marked = 0
10
+ @tile = Array.new(BOARD_SIZE) {Array.new(BOARD_SIZE)}
11
+ end
12
+
13
+ def mark_tile(location, mark)
14
+ if is_tile_not_marked?(location)
15
+ @tile[location.row][location.column] = mark
16
+ @number_of_tiles_marked += 1
17
+ end
18
+ end
19
+
20
+ def is_tile_not_marked?(location)
21
+ return @tile[location.row][location.column].nil?
22
+ end
23
+
24
+ def number_of_terns
25
+ @number_of_tiles_marked
26
+ end
27
+
28
+ def is_game_finish?(player_symbol)
29
+ return check_left_diognal?(player_symbol) || chek_right_diognal?(player_symbol) || check_row?(player_symbol) || check_column?(player_symbol)
30
+ end
31
+
32
+ private
33
+
34
+ def check_left_diognal?(player_symbol)
35
+ check_win = true
36
+ BOARD_SIZE.times do |index|
37
+ check_win = @tile[index][index] == player_symbol
38
+ return false if !check_win
39
+ end
40
+ return true
41
+ end
42
+
43
+ def chek_right_diognal?(player_symbol)
44
+ check_win = true
45
+ BOARD_SIZE.times do |index|
46
+ check_win = @tile[index][BOARD_SIZE - 1 - index] == player_symbol
47
+ return false if !check_win
48
+ end
49
+ return true
50
+ end
51
+
52
+ def check_row?(player_symbol)
53
+ BOARD_SIZE.times do |row|
54
+ check_win = true
55
+ BOARD_SIZE.times do |column|
56
+ check_win = check_win && @tile[row][column] == player_symbol
57
+ end
58
+ return true if check_win
59
+ end
60
+ return false
61
+ end
62
+
63
+ def check_column?(player_symbol)
64
+ BOARD_SIZE.times do |row|
65
+ check_win = true
66
+ BOARD_SIZE.times do |column|
67
+ check_win = check_win && @tile[column][row] == player_symbol
68
+ end
69
+ return true if check_win
70
+ end
71
+ return false
72
+ end
73
+ end
@@ -0,0 +1,58 @@
1
+ require __dir__ + '/board'
2
+ require __dir__ + '/player'
3
+ require __dir__ + '/game_status'
4
+
5
+ class Game
6
+
7
+ attr_reader :current_player, :status
8
+ def initialize(board, player1, player2)
9
+ @board = board
10
+ @player1 = player1
11
+ @player2 = player2
12
+ @current_player = player1
13
+ @status = GameStatus::NEXT_MOVE
14
+ end
15
+
16
+ def restart
17
+ @current_player = @player1
18
+ @status = GameStatus::NEXT_MOVE
19
+ @board.restart
20
+ end
21
+
22
+ def play_move(location)
23
+ if @status == GameStatus::NEXT_MOVE
24
+ @board.mark_tile(location, @current_player.symbol)
25
+ @status = check_game_status
26
+ @current_player = change_player
27
+ end
28
+ return status
29
+ end
30
+
31
+ private
32
+
33
+ def is_win?
34
+ @board.is_game_finish?(@current_player.symbol)
35
+ end
36
+
37
+ def winner
38
+ @current_player = change_player
39
+ return @current_player == @player1 ? GameStatus::PLAYER_1_WINS : GameStatus::PLAYER_2_WINS
40
+ end
41
+
42
+ def check_game_status
43
+ number_of_terns = @board.number_of_terns
44
+ if number_of_terns < 5
45
+ return GameStatus::NEXT_MOVE
46
+ elsif is_win?
47
+ return winner
48
+ elsif number_of_terns == 9
49
+ return GameStatus::DRAW
50
+ end
51
+ return GameStatus::NEXT_MOVE
52
+ end
53
+
54
+ def change_player
55
+ return @current_player == @player1 ? @player2 : @player1
56
+ end
57
+
58
+ end
@@ -0,0 +1,6 @@
1
+ module GameStatus
2
+ NEXT_MOVE = "next move"
3
+ PLAYER_1_WINS = "player 1"
4
+ PLAYER_2_WINS = "player 2"
5
+ DRAW = "draw"
6
+ end
@@ -0,0 +1,7 @@
1
+ class Location
2
+ attr_accessor :row, :column
3
+ def initialize(row, column)
4
+ @row = row
5
+ @column = column
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class Player
2
+ attr_accessor :name, :symbol
3
+ def initialize(name, symbol)
4
+ @name = name
5
+ @symbol = symbol
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module TicTacToeVj
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "tic_tac_toe_vj/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tic_tac_toe_vj"
8
+ spec.version = TicTacToeVj::VERSION
9
+ spec.authors = ["Vijay Pratap Singh"]
10
+ spec.email = ["vijayprsingh619@gmail.com"]
11
+
12
+ spec.summary = "A simple terminal tic tak toe game"
13
+ spec.description = "You can play tic tak toe game by using this gem"
14
+ spec.homepage = "https://github.com/vjpratap/tic_tac_toe_vj"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.8"
29
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tic_tac_toe_vj
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vijay Pratap Singh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.8'
55
+ description: You can play tic tak toe game by using this gem
56
+ email:
57
+ - vijayprsingh619@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/tic_tac_toe_vj.rb
71
+ - lib/tic_tac_toe_vj/board.rb
72
+ - lib/tic_tac_toe_vj/game.rb
73
+ - lib/tic_tac_toe_vj/game_status.rb
74
+ - lib/tic_tac_toe_vj/location.rb
75
+ - lib/tic_tac_toe_vj/player.rb
76
+ - lib/tic_tac_toe_vj/version.rb
77
+ - tic_tac_toe_vj.gemspec
78
+ homepage: https://github.com/vjpratap/tic_tac_toe_vj
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.7.6
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: A simple terminal tic tak toe game
102
+ test_files: []