tic_tac_toe_logic 0.1.1

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: 722b86d399ad1c363d680df892f39e3452e2835e00bf44fe0ec77f43e728b794
4
+ data.tar.gz: bcd9b127b2116d0195eb8802bd278a59bbce72cff68cd5b5439d184423d56167
5
+ SHA512:
6
+ metadata.gz: 0bd64a51dfd5e978753c29580af96e962d00582abc9c620706385eaa1778184e1bf6ebb755479c7d73861be84e341de3a855b9f9341f5eeae3d3363e8de7aae3
7
+ data.tar.gz: e61a317fda0829718dc01cfeb33facf1c91ad668763d39b42ccf81b6889ba538da4c7fee77508c573ada03403a34f811217baf315934547f084d1ee506b9f243
data/.DS_Store ADDED
Binary file
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in tic_tac_toe_logic.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # TicTacToeLogic
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_logic`. 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_logic'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tic_tac_toe_logic
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 the created tag, 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_logic.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "tic_tac_toe_logic"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ 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,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TicTacToeLogic
4
+ VERSION = "0.1.1"
5
+ end
@@ -0,0 +1,103 @@
1
+ require_relative "tic_tac_toe_logic/version"
2
+
3
+ module TicTacToeLogic
4
+ class GameBoardData < Array
5
+
6
+ WIN_COMBOS = [
7
+ [0,1,2], # top_row
8
+ [3,4,5], # middle_row
9
+ [6,7,8], # bottom_row
10
+ [0,3,6], # left_column
11
+ [1,4,7], # center_column
12
+ [2,5,8], # right_column
13
+ [0,4,8], # left_diagonal
14
+ [6,4,2] # right_diagonal
15
+ ]
16
+
17
+ def initialize
18
+ super Array.new(9, :available)
19
+ end
20
+
21
+ def play_move(space)
22
+ if self[space] == :available
23
+ self[space] = current_player()
24
+ end
25
+ end
26
+
27
+ def turn_count
28
+ self.size - self.count(:available)
29
+ end
30
+
31
+ def current_player
32
+ turn_count() % 2 == 0 ? :player_one : :player_two
33
+ end
34
+
35
+ def winner
36
+ winner = nil
37
+ for win_combo in WIN_COMBOS do
38
+ row = Array.new
39
+ for index in win_combo do
40
+ row.append(self[index])
41
+ end
42
+ if (row.all? { |each| each == row[0]})
43
+ if row[0] != :available
44
+ winner = row[0]
45
+ end
46
+ end
47
+ end
48
+ if board_full?() && winner == nil
49
+ winner = :tie
50
+ end
51
+ winner
52
+ end
53
+
54
+ def game_over?
55
+ board_full?() || winner()
56
+ end
57
+
58
+ def board_full?
59
+ !self.include?(:available)
60
+ end
61
+
62
+
63
+ def best_move
64
+ move = cpu_check_for_wins_or_blocks(current_player())
65
+ move ||= cpu_check_for_wins_or_blocks(non_current_player())
66
+ move ||= cpu_take_open_space()
67
+ end
68
+
69
+
70
+ #CPU LOGIC METHODS
71
+ def non_current_player
72
+ current_player() == :player_one ? :player_two : :player_one
73
+ end
74
+
75
+ def open_spaces
76
+ spaces_available_index = self.each_index.select {|i| self[i] == :available}
77
+ spaces_available_index.map {|each| each}
78
+ end
79
+
80
+ def cpu_take_open_space
81
+ self.open_spaces().sample
82
+ end
83
+
84
+ def cpu_check_for_wins_or_blocks(player_symbol)
85
+ for win_combo in WIN_COMBOS do
86
+ row = Array.new
87
+ for index in win_combo do
88
+ row.append(self[index])
89
+ end
90
+ if (row.count(player_symbol) == 2) && (row.include?(:available))
91
+ return win_combo[row.index(:available)]
92
+ end
93
+ end
94
+ nil
95
+ end
96
+
97
+
98
+
99
+
100
+
101
+
102
+ end
103
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tic_tac_toe_logic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Cristian-Baeza
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-11-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - cristianbaeza092@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".DS_Store"
21
+ - ".rspec"
22
+ - Gemfile
23
+ - README.md
24
+ - Rakefile
25
+ - bin/console
26
+ - bin/setup
27
+ - lib/tic_tac_toe_logic.rb
28
+ - lib/tic_tac_toe_logic/version.rb
29
+ homepage:
30
+ licenses: []
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.2.27
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Write a short summary, because RubyGems requires one.
51
+ test_files: []