tictactoe_tracypholmes 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/setup +8 -0
- data/bin/tictactoe_tracypholmes +19 -0
- metadata +9 -11
- data/lib/tictactoe_tracypholmes/board.rb +0 -65
- data/lib/tictactoe_tracypholmes/game.rb +0 -91
- data/lib/tictactoe_tracypholmes/player.rb +0 -9
- data/lib/tictactoe_tracypholmes/players/computer.rb +0 -50
- data/lib/tictactoe_tracypholmes/players/human.rb +0 -16
- data/lib/tictactoe_tracypholmes/version.rb +0 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 25d5c24b29e9ab49966ef65b773f918d293fa91e
|
|
4
|
+
data.tar.gz: 18abd79386e8aef4ba2a173371dc0fa6511c7f97
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e2af2ed37091a770ae530addfdb7990b1586b465530fca92ed01864a06e22203ca2313c1b805b4e6504306a214065d5b4dc08498b5da9a90e242d83392fab3fe
|
|
7
|
+
data.tar.gz: 5edbfc3ef5ae32f1875f25147d9f1d78db90c5d71fb2edc9874f2c3e61f2e7a3af0d6cbaa6c6cbfcd326f381097e2ed2ba9992a262bfd714815a0a579a2a39d2
|
data/bin/setup
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "tictactoe_tracypholmes"
|
|
5
|
+
# require_relative '../config/environment'
|
|
6
|
+
require 'colorize'
|
|
7
|
+
|
|
8
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
9
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
10
|
+
|
|
11
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
12
|
+
# require "pry"
|
|
13
|
+
# Pry.start
|
|
14
|
+
|
|
15
|
+
# require "irb"
|
|
16
|
+
# IRB.start
|
|
17
|
+
|
|
18
|
+
game = TictactoeTracypholmes::TicTacToeCLI.new
|
|
19
|
+
game.call
|
metadata
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: tictactoe_tracypholmes
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- tracypholmes
|
|
8
8
|
autorequire:
|
|
9
|
-
bindir:
|
|
9
|
+
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
date: 2017-01-18 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
@@ -80,25 +80,23 @@ dependencies:
|
|
|
80
80
|
- - "~>"
|
|
81
81
|
- !ruby/object:Gem::Version
|
|
82
82
|
version: 0.8.1
|
|
83
|
-
description: TicTacToe game with somewhat unbeatable AI
|
|
83
|
+
description: TicTacToe game with somewhat unbeatable AI. run `tictactoe_tracypholmes`
|
|
84
|
+
to play.
|
|
84
85
|
email:
|
|
85
86
|
- tracyholmes@gmail.com
|
|
86
|
-
executables:
|
|
87
|
+
executables:
|
|
88
|
+
- tictactoe_tracypholmes
|
|
87
89
|
extensions: []
|
|
88
90
|
extra_rdoc_files: []
|
|
89
91
|
files:
|
|
92
|
+
- bin/setup
|
|
93
|
+
- bin/tictactoe_tracypholmes
|
|
90
94
|
- lib/tictactoe_tracypholmes.rb
|
|
91
|
-
- lib/tictactoe_tracypholmes/board.rb
|
|
92
|
-
- lib/tictactoe_tracypholmes/game.rb
|
|
93
|
-
- lib/tictactoe_tracypholmes/player.rb
|
|
94
|
-
- lib/tictactoe_tracypholmes/players/computer.rb
|
|
95
|
-
- lib/tictactoe_tracypholmes/players/human.rb
|
|
96
|
-
- lib/tictactoe_tracypholmes/version.rb
|
|
97
95
|
homepage: https://github.com/tracypholmes/tictactoe_tracypholmes
|
|
98
96
|
licenses:
|
|
99
97
|
- MIT
|
|
100
98
|
metadata: {}
|
|
101
|
-
post_install_message:
|
|
99
|
+
post_install_message: Thanks for installing!
|
|
102
100
|
rdoc_options: []
|
|
103
101
|
require_paths:
|
|
104
102
|
- lib
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
class Board
|
|
2
|
-
attr_accessor :cells
|
|
3
|
-
|
|
4
|
-
def initialize()
|
|
5
|
-
reset!
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
def reset!
|
|
9
|
-
@cells = Array.new(9, " ")
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def display
|
|
13
|
-
puts " #{cells[0]} | #{cells[1]} | #{cells[2]} "
|
|
14
|
-
puts '-----------'
|
|
15
|
-
puts " #{cells[3]} | #{cells[4]} | #{cells[5]} "
|
|
16
|
-
puts '-----------'
|
|
17
|
-
puts " #{cells[6]} | #{cells[7]} | #{cells[8]} "
|
|
18
|
-
puts "\n"
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
def input_index(input) # this one line is all over the place!
|
|
22
|
-
input.to_i - 1
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
# position method
|
|
26
|
-
def position(input)
|
|
27
|
-
@cells[input_index(input)]
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
# define full here - every element on the board contains "X" or "O"
|
|
31
|
-
def full?
|
|
32
|
-
@cells.all? do |mark|
|
|
33
|
-
mark == 'X' || mark == 'O'
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
# define turn_count here
|
|
38
|
-
def turn_count
|
|
39
|
-
counter = 0
|
|
40
|
-
@cells.each do |occupied_spot|
|
|
41
|
-
counter += 1 if occupied_spot != ' '
|
|
42
|
-
end
|
|
43
|
-
counter
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
# taken? method
|
|
47
|
-
def taken?(input)
|
|
48
|
-
exxo = position(input)
|
|
49
|
-
exxo != " " ? true:false
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
# valid_move? method here
|
|
53
|
-
def valid_move?(input)
|
|
54
|
-
if position(input) == " " && input.to_i.between?(1, 9)
|
|
55
|
-
true # input on the board && input not taken
|
|
56
|
-
else
|
|
57
|
-
false # return false or nil here for invalid move
|
|
58
|
-
end
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
# define update
|
|
62
|
-
def update(input, player)
|
|
63
|
-
@cells[input_index(input)] = player.token
|
|
64
|
-
end
|
|
65
|
-
end
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
require 'tictactoe_tracypholmes/version'
|
|
2
|
-
require 'tictactoe_tracypholmes/players/computer'
|
|
3
|
-
require 'tictactoe_tracypholmes/players/human'
|
|
4
|
-
require 'tictactoe_tracypholmes/board'
|
|
5
|
-
require 'tictactoe_tracypholmes/player'
|
|
6
|
-
require 'tictactoe_tracypholmes'
|
|
7
|
-
require 'colorize'
|
|
8
|
-
|
|
9
|
-
module TictactoeTracypholmes
|
|
10
|
-
class Game
|
|
11
|
-
WIN_COMBINATIONS = [
|
|
12
|
-
[0, 1, 2],
|
|
13
|
-
[3, 4, 5],
|
|
14
|
-
[6, 7, 8],
|
|
15
|
-
[0, 3, 6],
|
|
16
|
-
[1, 4, 7],
|
|
17
|
-
[2, 5, 8],
|
|
18
|
-
[0, 4, 8],
|
|
19
|
-
[6, 4, 2]
|
|
20
|
-
].freeze
|
|
21
|
-
|
|
22
|
-
attr_accessor :board, :player_1, :player_2
|
|
23
|
-
|
|
24
|
-
# need to add name somewhere in the initialization?
|
|
25
|
-
|
|
26
|
-
def initialize(player_1 = Players::Human.new('X'), player_2 = Players::Human.new('O'), board = Board.new)
|
|
27
|
-
@player_1 = player_1
|
|
28
|
-
@player_2 = player_2
|
|
29
|
-
@board = board
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
# define current_player here
|
|
33
|
-
def current_player
|
|
34
|
-
@board.turn_count.even? ? player_1 : player_2
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
# over? here - won, is a draw, or full
|
|
38
|
-
def over?
|
|
39
|
-
won? || draw?
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def won?
|
|
43
|
-
WIN_COMBINATIONS.detect do |win_combo|
|
|
44
|
-
if @board.cells[win_combo[0]] == @board.cells[win_combo[1]] &&
|
|
45
|
-
@board.cells[win_combo[1]] == @board.cells[win_combo[2]] &&
|
|
46
|
-
(@board.cells[win_combo[0]] == 'X' || @board.cells[win_combo[0]] == 'O')
|
|
47
|
-
return win_combo
|
|
48
|
-
else
|
|
49
|
-
false
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
# define draw? here
|
|
55
|
-
def draw?
|
|
56
|
-
!won? && board.full?
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
# define winner here
|
|
60
|
-
def winner
|
|
61
|
-
if win_combo = won?
|
|
62
|
-
@board.cells[win_combo.first]
|
|
63
|
-
end
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
# define turn method here
|
|
67
|
-
def turn # work on this dadgum method
|
|
68
|
-
move = current_player.move(@board)
|
|
69
|
-
unless board.valid_move?(move)
|
|
70
|
-
puts 'NOT a valid move. Play again, please!'.red
|
|
71
|
-
turn
|
|
72
|
-
end
|
|
73
|
-
@board.update(move, current_player)
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
# define #play here
|
|
77
|
-
def play
|
|
78
|
-
until over? # until the game is over
|
|
79
|
-
@board.display
|
|
80
|
-
turn # take turns
|
|
81
|
-
end
|
|
82
|
-
if won?
|
|
83
|
-
@board.display
|
|
84
|
-
puts "Congratulations #{winner}!".green
|
|
85
|
-
elsif draw?
|
|
86
|
-
@board.display
|
|
87
|
-
puts "Cat's Game!".yellow
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
end
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
require 'tictactoe_tracypholmes/board'
|
|
2
|
-
require 'tictactoe_tracypholmes/player'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
module TictactoeTracypholmes
|
|
6
|
-
module Players
|
|
7
|
-
class Players::Computer < Player # represents a computer player of Tic Tac Toe. Implement a #move method that accepts a board and returns the move the computer wants to make in the form of a 1-9 string.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
def move(board)
|
|
11
|
-
move = nil
|
|
12
|
-
|
|
13
|
-
# Center move if possible
|
|
14
|
-
if !board.taken?(5)
|
|
15
|
-
move = '5'
|
|
16
|
-
# If player2 and the scoundrel took 5, goto the first position
|
|
17
|
-
elsif board.turn_count == 1
|
|
18
|
-
move = '1'
|
|
19
|
-
# Go for the corners! Presumes you went first
|
|
20
|
-
elsif board.turn_count == 2
|
|
21
|
-
move = [1, 3, 7, 9].find{|i| !board.taken?(i)}.to_s
|
|
22
|
-
|
|
23
|
-
# Player2 side strategy
|
|
24
|
-
elsif board.turn_count == 3 && (board.position(1) == board.position(9) || board.position(3) == board.position(7))
|
|
25
|
-
move = "2"
|
|
26
|
-
|
|
27
|
-
# You worked hard on these combinations - let's see who has the best combos thus far
|
|
28
|
-
else Game::WIN_COMBINATIONS.find do |c|
|
|
29
|
-
|
|
30
|
-
# Do I have a winner?
|
|
31
|
-
if c.select{|i| board.position(i+1) == token}.size == 2 && c.any?{|i| board.position(i+1) == " "}
|
|
32
|
-
move = c.select{|i| !board.taken?(i+1)}.first.to_i.+(1).to_s
|
|
33
|
-
|
|
34
|
-
# No winner - let's block the other guy
|
|
35
|
-
elsif c.select{|i| board.position(i+1) != " " && board.position(i+1) != token}.size == 2 && c.any?{|i| board.position(i+1) == " "}
|
|
36
|
-
move = c.select{|i| !board.taken?(i+1)}.first.to_i.+(1).to_s
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# No one has a winner, let's play out the corners and sides if possible
|
|
41
|
-
move = [1, 3, 7, 9, 2, 4, 6, 8].find{|i| !board.taken?(i)}.to_s if move == nil
|
|
42
|
-
end
|
|
43
|
-
move
|
|
44
|
-
|
|
45
|
-
# input = (1..9).to_a.sample
|
|
46
|
-
# input.to_s
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
end
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
require 'tictactoe_tracypholmes/player'
|
|
2
|
-
|
|
3
|
-
module TictactoeTracypholmes
|
|
4
|
-
module Players
|
|
5
|
-
class Human < Player
|
|
6
|
-
# move method
|
|
7
|
-
|
|
8
|
-
attr_accessor :input
|
|
9
|
-
|
|
10
|
-
def move(board)
|
|
11
|
-
puts 'Please make a move by entering a number from 1 - 9.'
|
|
12
|
-
gets.chomp
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
end
|