Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
3e4f3e8
initialized board class
Nov 1, 2012
ff4170b
initialized cluster class
Nov 1, 2012
313183e
new folder spec, including specs for board and cluster
Nov 1, 2012
a9d6d00
working board with tests
Nov 1, 2012
9d85394
added player, game and ui classes
Nov 1, 2012
21ba6d2
tons o stuff
Nov 2, 2012
8bb6504
removing cluster stuff
Nov 2, 2012
05b2ae4
updating gems
Nov 2, 2012
6f20df8
rob fixes
Nov 2, 2012
64c2ad3
commiting pulled things
Jauny Nov 2, 2012
adecd90
added printout of game and some ui
Nov 2, 2012
7a37fd9
switched order of players
Nov 2, 2012
50f54cc
git ignores now db
Nov 2, 2012
ecd972c
Gemfile changes
Nov 2, 2012
814023f
Twitter class init
Nov 2, 2012
8328072
changes in c_4, board, db, game; changed name to ui
Nov 2, 2012
ef5f759
3 modes in UI to choose
Nov 2, 2012
5414e5a
changes in twitter, and minor in c_4, game and ui
Nov 3, 2012
9cbbcd8
twitter changes
Nov 3, 2012
840bab4
massive refactor
Nov 3, 2012
dfbf0c8
integrate computer_player class
Nov 3, 2012
d76ba2e
removed .db file
wockenfuss Nov 3, 2012
f29a2c4
updates to gitignore and gemfile
wockenfuss Nov 3, 2012
44ac2cf
twitter related updates
wockenfuss Nov 3, 2012
95f7f97
twitter class creates new player from twitter stream
wockenfuss Nov 3, 2012
631173a
working twitter player
wockenfuss Nov 4, 2012
1871579
some cleanup and fixed computer player bug
wockenfuss Nov 4, 2012
4141b62
deleted twitter scratch file
wockenfuss Nov 4, 2012
975269a
make the 1-deep coin analyze work
Jauny Nov 4, 2012
ccd7dad
complete the merge
Jauny Nov 4, 2012
6889519
removing old twitter file dependency from executable
wockenfuss Nov 4, 2012
45b3ce3
Merge branch 'master' of github.com:SittingDucks/connect_four
wockenfuss Nov 4, 2012
acee5b2
integration of defense ai into computer player
wockenfuss Nov 4, 2012
67485b5
fixed computer_player nil bug
wockenfuss Nov 4, 2012
0ae6601
Games to DB
Nov 4, 2012
d773ee7
Player statistic: changes in game, player and ui
danigro77 Nov 5, 2012
fcc7d84
Bugs fixed: exit, input col>num, empty input,...
danigro77 Nov 5, 2012
3c938a6
Merge branch 'master' of github.com:SittingDucks/connect_four
danigro77 Nov 5, 2012
3a152f7
spec updates
danigro77 Nov 5, 2012
bfb8652
made the AI play with there is the more consecutive opponent's coins
Jauny Nov 5, 2012
a3f7444
computer and ui
Jauny Nov 5, 2012
76c7725
merging cnflicts
Jauny Nov 5, 2012
ddbe052
refactoring classes: db, player, ui
danigro77 Nov 5, 2012
04ba861
changed conflicts in ui
danigro77 Nov 5, 2012
37f599f
added back in working line in computer player logic that had been com…
wockenfuss Nov 5, 2012
c5ccbe5
incorporated cell_rating_bottom methods from Jonathan's defense class
wockenfuss Nov 5, 2012
b1f7f14
name changes for computer player piece updates
wockenfuss Nov 5, 2012
1b965bf
switched player one to 'O' and player two to 'X'
wockenfuss Nov 5, 2012
e90200e
DB error solved when called with PC or Twitter Player
danigro77 Nov 5, 2012
4ef8a31
dryed board class with diagonals
danigro77 Nov 5, 2012
2c33bde
Merge branch 'master' of github.com:SittingDucks/connect_four
danigro77 Nov 5, 2012
01a6d04
Changed the formatting of printing the board to console while playing…
wockenfuss Nov 5, 2012
1ee2455
the bot is working yeah
Jauny Nov 5, 2012
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.db
*.gem
*.rbc
.bundle
Expand All @@ -16,3 +17,6 @@ tmp
.yardoc
_yardoc
doc/
oauth/
db/connectfour.db
oauth_test
Empty file added .schema
Empty file.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'
gem 'rspec'
gem 'sqlite3'
gem 'tweetstream'
gem 'twitter'
47 changes: 47 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.3.2)
cookiejar (0.3.0)
daemons (1.1.9)
diff-lcs (1.1.3)
em-http-request (1.0.3)
addressable (>= 2.2.3)
cookiejar
em-socksify
eventmachine (>= 1.0.0.beta.4)
http_parser.rb (>= 0.5.3)
em-socksify (0.2.1)
eventmachine (>= 1.0.0.beta.4)
em-twitter (0.2.1)
eventmachine (~> 1.0)
http_parser.rb (~> 0.5)
simple_oauth (~> 0.1)
eventmachine (1.0.0)
faraday (0.8.4)
multipart-post (~> 1.1)
http_parser.rb (0.5.3)
multi_json (1.3.6)
multipart-post (1.1.5)
rspec (2.11.0)
rspec-core (~> 2.11.0)
rspec-expectations (~> 2.11.0)
rspec-mocks (~> 2.11.0)
rspec-core (2.11.1)
rspec-expectations (2.11.3)
diff-lcs (~> 1.1.3)
rspec-mocks (2.11.3)
simple_oauth (0.1.9)
sqlite3 (1.3.6)
tweetstream (2.3.0)
daemons (~> 1.1)
em-http-request (~> 1.0.2)
em-twitter (~> 0.2)
multi_json (~> 1.3)
twitter (~> 4.0)
twitter (4.2.0)
faraday (~> 0.8)
multi_json (~> 1.3)
simple_oauth (~> 0.1.6)

PLATFORMS
ruby

DEPENDENCIES
rspec
sqlite3
tweetstream
twitter
6 changes: 5 additions & 1 deletion bin/connect_four
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
#!/usr/bin/ruby
#!/usr/bin/env ruby

require_relative '../lib/connect_four'

UI.start
138 changes: 138 additions & 0 deletions connect4/board.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
class Board
attr_reader :col_num, :row_num
attr_accessor :cells

def initialize(col_num = 7, row_num = 6)
@cells = Array.new(col_num * row_num, "")
@col_num = col_num
@row_num = row_num
end

def empty?
cells.all? { |cell| cell.empty? }
end

def empty_cells
cells.select {|cell| cell == "" }.size
end

def place_piece(column, piece)
position = (col_num * (row_num - 1)) + (column - 1)
until cells[position].empty?
position -= col_num
end
cells[position] = piece
end

def full?
cells.all? {|cell| cell != "" }
end

def rows
cells.each_slice(col_num).collect { |row| row }
end

def columns
columns_array = Array.new(col_num) { Array.new([]) }
cells.each_with_index do |cell, i|
columns_array[i % col_num] << cell
end
columns_array
end

def get_start_indexes_to_right(win_no)
container = []
(col_num - (win_no-1)).times do |i|
container << i
end
(row_num - (win_no-1)).times do |i|
container << i*col_num unless i == 0
end
container
end

def get_start_indexes_to_left(win_no)
container = []
(col_num - win_no+1).times do |i|
container << i + (win_no-1)
end
(row_num - (win_no-1)).times do |i|
container << (i*col_num + col_num-1) unless i == 0
end
container
end

def row_index_number(index)
(index / col_num).floor
end

def column_index_number(index)
index % col_num
end

def diagonal_indexes_to_right
start_indexes = get_start_indexes_to_right(4)
leap = col_num + 1
start_indexes.map do |index|
diagonal = []
while true
diagonal << index
index += leap
row, column = row_index_number(index), column_index_number(index)
break if row == row_num || column == 0
end
diagonal
end
end

def diagonal_indexes_to_left
start_indexes = get_start_indexes_to_left(4)
leap = col_num - 1
start_indexes.map do |index|
diagonal = []
while true
diagonal << index
index += leap
row, column = row_index_number(index), column_index_number(index)
break if row == row_num || column == col_num-1
end
diagonal
end
end

# def diagonal_indexes(direction)
# start_indexes = get_start_indexes_to_left(4)
# leap = col_num + 1 if direction == "to_right"
# leap = col_num - 1 if direction == "to_left"
# start_indexes.map do |index|
# diagonal = []
# while true
# diagonal << index
# index += leap
# row, column = row_index_number(index), column_index_number(index)
# break if (row == row_num || column == 0) && direction == "to_right"
# break if (row == row_num || column == col_num-1) && direction == "to_left"
# end
# diagonal
# end
# end

def diagonals
(diagonal_indexes_to_right + diagonal_indexes_to_left).map {|diagonal| diagonal.map { |i| i = cells[i]}}
# (diagonal_indexes("to_right") + diagonal_indexes("to_left")).map {|diagonal| diagonal.map { |i| i = cells[i]}}
end


def check_four_consecutive?
# (columns + rows + diagonals).any? { |group| group.four_consecutive? }
(columns + rows + diagonals).any? { |lines| four_consecutive?(lines) }
end

def four_consecutive?(line)
line.each_cons(4) do |element|
return true if (element.uniq.size == 1 && element.uniq[0] != "")
end
false
end

end
117 changes: 117 additions & 0 deletions connect4/defense.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
require_relative 'board'

class Defense
attr_reader :board, :positions_ratings

def initialize
@board = Board.new
@positions_ratings = { 1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>0, 7=>0}
end

def available_moves
@available_moves = []
first_row = [35, 36, 37, 38, 39, 40, 41]
first_row.each do |position|
until board.cells[position].empty?
position -= board.col_num
end
@available_moves << position
end
return @available_moves
end

def rate_all_cells
available_moves.each do |move|
cell_rating_left(move)
cell_rating_right(move)
cell_rating_bottom(move)
end
positions_ratings
end

def cell_rating_left(position)
left_cell = board.cells[position-1]
unless [35, 28, 21, 14, 7, 0].include?(position)
if left_cell == "red"
positions_ratings[(position%7)+1] += 2
cell_rating_left2(position) unless [36, 29, 22, 15, 8, 1].include?(position)
end
end
positions_ratings[position%7+1]
end

def cell_rating_left2(position)
left_cell2 = board.cells[position-2]
if left_cell2 == "red"
positions_ratings[position%7+1] += 4
cell_rating_left3(position) unless [37, 30, 23, 16, 9, 2].include?(position)
end
end

def cell_rating_left3(position)
left_cell3 = board.cells[position-3]
if left_cell3 == "red"
positions_ratings[position%7+1] += 8
end
end

def cell_rating_right(position)
right_cell = board.cells[position+1]
unless [41, 34, 27, 20, 13, 6].include?(position)
if right_cell == "red"
positions_ratings[position%7+1] += 2
cell_rating_right2(position) unless [40, 33, 26, 19, 12, 5].include?(position)
end
end
positions_ratings[position%7+1]
end

def cell_rating_right2(position)
right_cell2 = board.cells[position+2]
if right_cell2 == "red"
positions_ratings[position%7+1] += 4
cell_rating_right3(position) unless [39, 32, 25, 18, 11, 4].include?(position)
end
end

def cell_rating_right3(position)
right_cell3 = board.cells[position+3]
if right_cell3 == "red"
positions_ratings[position%7+1] += 8
end
end

def cell_rating_bottom(position)
unless [35, 36, 37, 38, 39, 40, 41].include?(position)
bottom_cell = board.cells[position+7]
if bottom_cell == "red"
positions_ratings[position%7+1] += 2
cell_rating_bottom2(position) unless [28, 29, 30, 31, 32, 33, 34].include?(position)
end
end
positions_ratings[position%7+1]
end

def cell_rating_bottom2(position)
bottom_cell2 = board.cells[position+14]
if bottom_cell2 == "red"
positions_ratings[position%7+1] += 4
cell_rating_bottom3(position) unless [22, 23, 24, 25, 26, 27].include?(position)
end
end

def cell_rating_bottom3(position)
bottom_cell3 = board.cells[position+21]
if bottom_cell3 == "red"
positions_ratings[position%7+1] += 8
end
end

def move
rate_all_cells
play_column = positions_ratings.max_by {|k, v| v}.first.to_i
board.place_piece(play_column, "black")
positions_ratings.each {|k,v| positions_ratings[k] = 0}
end

end
Loading