Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--color
--require spec_helper
--format doc
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "https://rubygems.org"

gem 'rspec'
gem 'guard-rspec', require: false
65 changes: 65 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.1)
diff-lcs (1.2.5)
ffi (1.9.14)
formatador (0.2.5)
guard (2.14.0)
formatador (>= 0.2.4)
listen (>= 2.7, < 4.0)
lumberjack (~> 1.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.9.12)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-compat (1.2.1)
guard-rspec (4.7.3)
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
ruby_dep (~> 1.2)
lumberjack (1.0.10)
method_source (0.8.2)
nenv (0.3.0)
notiffany (0.1.1)
nenv (~> 0.1)
shellany (~> 0.0)
pry (0.10.4)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rb-fsevent (0.9.8)
rb-inotify (0.9.7)
ffi (>= 0.5.0)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
ruby_dep (1.5.0)
shellany (0.0.1)
slop (3.6.0)
thor (0.19.4)

PLATFORMS
ruby

DEPENDENCIES
guard-rspec
rspec

BUNDLED WITH
1.13.6
71 changes: 71 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features) \
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}

## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
#
# $ mkdir config
# $ mv Guardfile config/
# $ ln -s config/Guardfile .
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"

# Note: The cmd option is now required due to the increasing number of ways
# rspec may be run, below are examples of the most common uses.
# * bundler: 'bundle exec rspec'
# * bundler binstubs: 'bin/rspec'
# * spring: 'bin/rspec' (This will use spring if running and you have
# installed the spring binstubs per the docs)
# * zeus: 'zeus rspec' (requires the server to be started separately)
# * 'just' rspec: 'rspec'

guard :rspec, cmd: "bundle exec rspec" do
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)

# Feel free to open issues for suggestions and improvements

# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)
watch(//) { "spec" }

# Ruby files
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)

# Rails files
rails = dsl.rails(view_extensions: %w(erb haml slim))
dsl.watch_spec_files_for(rails.app_files)
dsl.watch_spec_files_for(rails.views)

watch(rails.controllers) do |m|
[
rspec.spec.call("routing/#{m[1]}_routing"),
rspec.spec.call("controllers/#{m[1]}_controller"),
rspec.spec.call("acceptance/#{m[1]}")
]
end

# Rails config changes
watch(rails.spec_helper) { rspec.spec_dir }
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }

# Capybara features specs
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }

# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
end
end
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## project_game_center

[An Object-Oriented Ruby project from the Viking Code School](http://www.vikingcodeschool.com)
[An Object-Oriented Ruby project from the Viking Code School](http://www.vikingcodeschool.com)

Tingting Wang
1 change: 0 additions & 1 deletion blackjack/blackjack.rb

This file was deleted.

1 change: 0 additions & 1 deletion connect_four/connect_four.rb

This file was deleted.

39 changes: 39 additions & 0 deletions lib/ai.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative 'board'
require_relative 'player'
require 'pry'

class AI < Player

# all columns that are not yet full
def available_moves
possible_columns = []
(0..6).each do |i|
possible_columns << i if @board.columns[i].include?('-')
end
possible_columns
end

# look for 3 consecutive pieces
def better_moves
candidates = []
available_moves.each do |i|
row_index = @board.columns[i].index('-')
candidates << i if @board.columns[i].join.include?("OOO" || "XXX") || @board.rows(row_index).join.include?("OOO" || "XXX")
end
candidates
end

def choose_column
first_choice = better_moves
player_choice = 0
if first_choice.empty?
player_choice = available_moves.sample
else
player_choice = first_choice.sample
end
puts
puts "Player #{@number} chose column #{player_choice}!"
@board.process_choice(player_choice, @piece)
end

end
147 changes: 147 additions & 0 deletions lib/board.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
require 'pry'
class Board

attr_reader :columns

def initialize(board_input=false)
@columns = ( board_input || Array.new(7) { Array.new(6, '-') })
end

def rows(index)
rows = @columns.transpose
rows[index]
end

def render
puts
puts " 1 2 3 4 5 6 7"
5.downto(0) do |row_index|
print "|"
rows(row_index).each do |item|
print " #{item} "
end
print "|"
puts
end
end

def process_choice(choice, piece)
column_index = choice
row_index = @columns[column_index].index('-')
@columns[column_index][row_index] = piece
[column_index, row_index, piece]
end

def draw?
return false if @columns.flatten.include?("-")
true
end

def win?(move)
column_index = move[0]
row_index = move[1]
piece = move[2]
return true if horizontal_win?(row_index, piece) ||
vertical_win?(column_index, piece) ||
upper_diagonal_win?(column_index, row_index, piece)||
downward_diagonal_win?(column_index, row_index, piece)
false
end

protected

def test_object(relevant_array, piece)
test_object = relevant_array.join
return true if test_object.include?(piece*4)
false
end

def vertical_win?(column_index, piece)
test_object(@columns[column_index], piece)
end

def horizontal_win?(row_index, piece)
test_object(rows(row_index), piece)
end

def upper_diagonal_win?(column_index, row_index, piece)
test_object(upward_diagonal(column_index, row_index), piece)
end

def downward_diagonal_win?(column_index, row_index, piece)
test_object(downward_diagonal(column_index, row_index), piece)
end

def upward_diagonal(column_index, row_index)
a = upward_diagonal_lower_left(column_index, row_index)
b = upward_diagonal_upper_right(column_index, row_index)
a + b
end

def upward_diagonal_upper_right(column_index, row_index)
upper_right = [@columns[column_index][row_index]]
loop do
column_index += 1
row_index += 1
if (0..6).include?(column_index) && (0..5).include?(row_index)
upper_right << @columns[column_index][row_index]
else
break
end
end
upper_right
end

def upward_diagonal_lower_left(column_index, row_index)
lower_left = []
loop do
column_index -= 1
row_index -= 1
if (0..6).include?(column_index) && (0..5).include?(row_index)
lower_left << @columns[column_index][row_index]
else
break
end
end
lower_left.reverse
end

def downward_diagonal(column_index, row_index)
a = downward_diagonal_upper_left(column_index, row_index)
b = downward_diagonal_lower_right(column_index, row_index)
a + b
end

def downward_diagonal_upper_left(column_index, row_index)
upper_left = []
loop do
column_index -= 1
row_index += 1
if (0..6).include?(column_index) && (0..5).include?(row_index)
upper_left << @columns[column_index][row_index]
else
break
end
end
upper_left.reverse
end

def downward_diagonal_lower_right(column_index, row_index)
lower_right = [@columns[column_index][row_index]]
loop do
column_index += 1
row_index -= 1
if (0..6).include?(column_index) && (0..5).include?(row_index)
lower_right << @columns[column_index][row_index]
else
break
end
end
lower_right
end
end





8 changes: 8 additions & 0 deletions lib/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative 'game'
require_relative 'board'
require_relative 'player'
require_relative 'human'
require_relative 'ai'

game = Game.new
game.play
Loading