From 3e4f3e875212454589342efe01267e1421562259 Mon Sep 17 00:00:00 2001 From: Apprentice Date: Thu, 1 Nov 2012 13:59:47 -0700 Subject: [PATCH 01/47] initialized board class --- lib/board.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lib/board.rb diff --git a/lib/board.rb b/lib/board.rb new file mode 100644 index 0000000..9e15093 --- /dev/null +++ b/lib/board.rb @@ -0,0 +1,29 @@ +class Board + attr_reader :col_num, :row_num, :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 place_piece(column, piece) + position = (col_num * (row_num - 1)) + (column - 1) + until cells[position].empty? + position -= 7 + end + cells[position] = piece + end + 1 + + def full? + cells.all? {|cell| cell != "" } + end + +end + From ff4170b1636a5bb0d20075796d5b47225fb79b38 Mon Sep 17 00:00:00 2001 From: Apprentice Date: Thu, 1 Nov 2012 14:00:13 -0700 Subject: [PATCH 02/47] initialized cluster class --- lib/cluster.rb | 120 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 lib/cluster.rb diff --git a/lib/cluster.rb b/lib/cluster.rb new file mode 100644 index 0000000..ff9b1c8 --- /dev/null +++ b/lib/cluster.rb @@ -0,0 +1,120 @@ +require_relative 'board' + +class Cluster < Board + + def initialize + super + 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 + +# DELETE when TESTED!!!!!! + # def diagonal_1 + # 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_2 + # 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 column == row_num || row == 0 + # 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 (column == row_num || row == 0) && direction == "to_left" + end + diagonal + end + end + + def diagonals + # (diagonal_1 + diagonal_2).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 + +# cluster = Cluster.new +# p cluster.diagonals \ No newline at end of file From 313183e1199d3ee2f3b0b3bf9bc651f5f5d1dff9 Mon Sep 17 00:00:00 2001 From: Apprentice Date: Thu, 1 Nov 2012 14:01:14 -0700 Subject: [PATCH 03/47] new folder spec, including specs for board and cluster --- spec/board_spec.rb | 65 ++++++++++++++++++++++++ spec/cluster_spec.rb | 117 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 spec/board_spec.rb create mode 100644 spec/cluster_spec.rb diff --git a/spec/board_spec.rb b/spec/board_spec.rb new file mode 100644 index 0000000..e6a8352 --- /dev/null +++ b/spec/board_spec.rb @@ -0,0 +1,65 @@ +require './lib/board.rb' + +describe Board do + + before(:each){ @board = Board.new} + + context "#initialize" do + it "should exist" do + @board.should_not be_nil + end + + it "accepts a number of columns" do + @board.col_num.should eq 7 + end + + it "accepts a number of rows" do + @board.row_num.should eq 6 + end + + it "is empty" do + @board.should be_empty + end + end + + context "#place_piece" do + + it "places a piece on the board" do + @board.place_piece(1, "anything") + @board.should_not be_empty + end + + it "places the piece in the correct position" do + @board.place_piece(3, "anything") + @board.cells[37].should_not be_empty + end + + it "places the correct colored piece on the board" do + @board.place_piece(1, "Striped") + @board.cells[35].should eq "Striped" + end + + it "doesn't put a piece in an occupied cell" do + @board.place_piece(1, "Striped") + @board.place_piece(1, "Striped") + @board.place_piece(1, "Striped") + @board.cells[21].should eq "Striped" + end + + end + + context "#full?" do + it "returns true if the board is full" do + @board.cells.map! { |cell| cell = "tooth fairy"} + @board.should be_full + end + + it "returns false of the board is not full" do + @board.should_not be_full + end + + end + + + +end \ No newline at end of file diff --git a/spec/cluster_spec.rb b/spec/cluster_spec.rb new file mode 100644 index 0000000..cf2896c --- /dev/null +++ b/spec/cluster_spec.rb @@ -0,0 +1,117 @@ +#require './lib/cluster.rb' +require './lib/cluster.rb' + +describe Cluster do + + before(:each) do + @cluster = Cluster.new + end + + context "#rows" do + it "has row_num rows" + end + + context "#columns" do + it "has col_num columns" + end + + context "#get_start_indexes_to_right" do + it "should have unique index numbers" + it "should only include numbers from the first row and the first column" + it "should not include any other numbers than the ones from the first row and the first column" + it "should not include the numbers of the first row bigger than 4" + + end + + context "#get_start_indexes_to_left" do + it "should have unique index numbers" + it "should only include numbers from the first row and the last column" + it "should not include any other numbers than the ones from the first row and the last column" + it "should not include the numbers smaller than 4" + end + + context "#row_index_number" do + it "returns the indexnumber of the row of a certain field" + end + + context "#column_index_number" do + it "returns the indexnumber of the column of a certain field" + end + + context "#diagonal_indexes" do + it "has a maximum of row_num units when its going to right" + it "has a maximum of row_num units when its going to left" + end + + context "#diagonals" do + it "has diagonals in both directions" + it "has gathered the contents of the cells" + end + + context "#check_four_consecutive?" do + it "returns true if there are four occupied cells in a row" do + @cluster.place_piece(1, "Striped") + @cluster.place_piece(2, "Striped") + @cluster.place_piece(3, "Striped") + @cluster.place_piece(4, "Striped") + @cluster.check_four_consecutive?.should be_true + end + + it "returns false if there are four occupied cells in a row" do + @cluster.place_piece(1, "Striped") + @cluster.place_piece(2, "Striped") + @cluster.place_piece(3, "Striped") + @cluster.place_piece(5, "Striped") + @cluster.check_four_consecutive?.should be_false + end + + it "returns true if there are four occupied cells in a column" do + @cluster.place_piece(1, "Striped") + @cluster.place_piece(1, "Striped") + @cluster.place_piece(1, "Striped") + @cluster.place_piece(1, "Striped") + @cluster.check_four_consecutive?.should be_true + end + + it "returns false if there are four occupied cells in a row" do + @cluster.place_piece(1, "Striped") + @cluster.place_piece(1, "Striped") + @cluster.place_piece(1, "Striped") + @cluster.place_piece(5, "Striped") + @cluster.check_four_consecutive?.should be_false + end + + it "returns true if there are four occupied cells in a column" do + @cluster.place_piece(1, "Striped") + @cluster.place_piece(2, "Striped2") + @cluster.place_piece(2, "Striped") + @cluster.place_piece(3, "Striped2") + @cluster.place_piece(3, "Striped") + @cluster.place_piece(3, "Striped") + @cluster.place_piece(4, "Striped") + @cluster.place_piece(4, "Striped2") + @cluster.place_piece(4, "Striped") + @cluster.place_piece(4, "Striped") + @cluster.check_four_consecutive?.should be_true + end + + it "returns false if there are four occupied cells in a row" do + @cluster.place_piece(1, "Striped2") + @cluster.place_piece(2, "Striped2") + @cluster.place_piece(2, "Striped") + @cluster.place_piece(3, "Striped2") + @cluster.place_piece(3, "Striped") + @cluster.place_piece(3, "Striped") + @cluster.place_piece(4, "Striped") + @cluster.place_piece(4, "Striped2") + @cluster.place_piece(4, "Striped") + @cluster.place_piece(4, "Striped") + @cluster.check_four_consecutive?.should be_false + end + end + + context "#four_consecutive" do + + end + +end From a9d6d00e521e1bab571899e683d8401cace4aca1 Mon Sep 17 00:00:00 2001 From: Apprentice Date: Thu, 1 Nov 2012 15:40:16 -0700 Subject: [PATCH 04/47] working board with tests --- lib/board.rb | 111 +++++++++++++++++++++++++++++++++++++++-- spec/board_spec.rb | 122 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 230 insertions(+), 3 deletions(-) diff --git a/lib/board.rb b/lib/board.rb index 9e15093..fc24264 100644 --- a/lib/board.rb +++ b/lib/board.rb @@ -3,7 +3,6 @@ class Board def initialize(col_num = 7, row_num = 6) @cells = Array.new(col_num * row_num, "") - # @col_num = col_num @row_num = row_num end @@ -15,15 +14,121 @@ def empty? def place_piece(column, piece) position = (col_num * (row_num - 1)) + (column - 1) until cells[position].empty? - position -= 7 + position -= col_num end cells[position] = piece end - 1 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 diff --git a/spec/board_spec.rb b/spec/board_spec.rb index e6a8352..4736ae9 100644 --- a/spec/board_spec.rb +++ b/spec/board_spec.rb @@ -60,6 +60,128 @@ end + context "#rows" do + it "has row_num rows" do # TODO: better way for testing this???? + @board.rows.length.should eq(@board.row_num) + end + end + + context "#columns" do + it "has col_num columns" do # TODO: better way for testing this???? + @board.columns.length.should eq(@board.col_num) + end + end + + context "#get_start_indexes_to_right" do + it "should have unique index numbers" do + @board.get_start_indexes_to_right(4).uniq.length.should eq(@board.get_start_indexes_to_right(4).length) + end + it "should only include numbers from the first row and the first column" + it "should not include any other numbers than the ones from the first row and the first column" + it "should not include the numbers of the first row bigger than 4" + + end + + context "#get_start_indexes_to_left" do + it "should have unique index numbers" do + @board.get_start_indexes_to_left(4).uniq.length.should eq(@board.get_start_indexes_to_right(4).length) + end + it "should only include numbers from the first row and the last column" + it "should not include any other numbers than the ones from the first row and the last column" + it "should not include the numbers smaller than 4" + end + + context "#row_index_number" do + it "returns the indexnumber of the row of a certain field" + end + + context "#column_index_number" do + it "returns the indexnumber of the column of a certain field" + end + + context "#diagonal_indexes" do + it "has a maximum of row_num units when its going to right" + it "has a maximum of row_num units when its going to left" + end + + context "#diagonals" do + it "has diagonals in both directions" + it "has gathered the empty contents of the cells" do + @board.diagonals[0].should include('') + end + it "has gathered the contents of the cells" do + @board.place_piece(6, "Token") + @board.diagonals[0].should include("Token") + end + end + + context "#check_four_consecutive?" do + it "returns true if there are four occupied cells in a row" do + @board.place_piece(1, "Striped") + @board.place_piece(2, "Striped") + @board.place_piece(3, "Striped") + @board.place_piece(4, "Striped") + @board.check_four_consecutive?.should be_true + end + + it "returns false if there are four occupied cells in a row" do + @board.place_piece(1, "Striped") + @board.place_piece(2, "Striped") + @board.place_piece(3, "Striped") + @board.place_piece(5, "Striped") + @board.check_four_consecutive?.should be_false + end + + it "returns true if there are four occupied cells in a column" do + @board.place_piece(1, "Striped") + @board.place_piece(1, "Striped") + @board.place_piece(1, "Striped") + @board.place_piece(1, "Striped") + @board.check_four_consecutive?.should be_true + end + + it "returns false if there are four occupied cells in a row" do + @board.place_piece(1, "Striped") + @board.place_piece(1, "Striped") + @board.place_piece(1, "Striped") + @board.place_piece(5, "Striped") + @board.check_four_consecutive?.should be_false + end + + it "returns true if there are four occupied cells in a column" do + @board.place_piece(1, "Test") + @board.place_piece(2, "Striped2") + @board.place_piece(2, "Test") + @board.place_piece(3, "Striped2") + @board.place_piece(3, "Striped") + @board.place_piece(3, "Test") + @board.place_piece(4, "Striped") + @board.place_piece(4, "Striped2") + @board.place_piece(4, "Striped") + @board.place_piece(4, "Test") + @board.check_four_consecutive?.should be_true + end + + it "returns false if there are four occupied cells in a row" do + @board.place_piece(1, "Test_wrong") + @board.place_piece(2, "Striped2") + @board.place_piece(2, "Test") + @board.place_piece(3, "Striped2") + @board.place_piece(3, "Striped") + @board.place_piece(3, "Test") + @board.place_piece(4, "Striped") + @board.place_piece(4, "Striped2") + @board.place_piece(4, "Striped") + @board.place_piece(4, "Test") + @board.check_four_consecutive?.should be_false + end + end + + context "#four_consecutive" do + it "returns false, if a line of the board has not four of the same, consecutive tokens in it" + it "returns true, if a line of the board has four of the same, consecutive tokens in it" + end + end \ No newline at end of file From 9d85394966ce5a072ffd939ee49dcb37beb4c793 Mon Sep 17 00:00:00 2001 From: Apprentice Date: Thu, 1 Nov 2012 15:44:20 -0700 Subject: [PATCH 05/47] added player, game and ui classes --- db/schema.sql | 17 +++++++++++++++++ db/test.db | Bin 0 -> 5120 bytes lib/board.rb | 4 ++++ lib/connect_four.rb | 0 lib/database.rb | 16 ++++++++++++++++ lib/game.rb | 21 +++++++++++++++++++++ lib/player.rb | 11 +++++++++++ spec/connect_four_spec.rb | 0 spec/database_spec.rb | 32 ++++++++++++++++++++++++++++++++ spec/game_spec.rb | 28 ++++++++++++++++++++++++++++ spec/player_spec.rb | 19 +++++++++++++++++++ 11 files changed, 148 insertions(+) create mode 100644 db/schema.sql create mode 100644 db/test.db create mode 100644 lib/connect_four.rb create mode 100644 lib/database.rb create mode 100644 lib/game.rb create mode 100644 lib/player.rb create mode 100644 spec/connect_four_spec.rb create mode 100644 spec/database_spec.rb create mode 100644 spec/game_spec.rb create mode 100644 spec/player_spec.rb diff --git a/db/schema.sql b/db/schema.sql new file mode 100644 index 0000000..ce051ac --- /dev/null +++ b/db/schema.sql @@ -0,0 +1,17 @@ +CREATE TABLE IF NOT EXISTS players ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name VARCHAR NOT NULL, + twitter VARCHAR NOT NULL UNIQUE, + password VARCHAR NOT NULL, + created_at DATETIME, + updated_at DATETIME +); + +CREATE TABLE IF NOT EXISTS games ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + player1 INTEGER NOT NULL, + player2 INTEGER NOT NULL, + winner INTEGER, + FOREIGN KEY (player1) REFERENCES Players(id), + FOREIGN KEY (player2) REFERENCES Players(id) +); \ No newline at end of file diff --git a/db/test.db b/db/test.db new file mode 100644 index 0000000000000000000000000000000000000000..d1aaa11ce8f7f4714dbb4b6924afe9dd4744f974 GIT binary patch literal 5120 zcmeH~y>8nu5P(Thi5meKf({xy^hOIw20JfM)n!tlw(Kx+0Z)b+v4sL#P9zy_mI6t} zK3gB5OZRR8x^(E7ri<8xhprV5{5kS?r0x?bffwhUw3P5+nor^qPKZq?B`*O8Aq?{l z=9O5e*sBKC^ohPrW#st#i+%Qs*z_~8zOk>?7gUUB0{4zxb6uA{6lMH=EI-B*S*T*C zEw~?Xi2PR_4yp?p4xpq71YyL_xPZP0y1p2~TRsGT5cPr(y}CS%j?~EOaeOQD=AYqV zFM@E;={&WzwpizBmdSi&TzZ`L1P{(awOm6_?t$Q^T=1~XFQBg$EE;LzZS`nv^KiUA z+jS2Q=}@64ZpOGTM}@qZ$?Q^YH0?D!8^%V47?04?yPMFOTd&iHrkZp4%!RJ3wK;U> zKSN%u1@C;(e&Y*RCtS|cvP6iRRv3iA`GD)!5EsRKnkRpaTAD9&8J98{;n{qR`ymRt z+HZE9Y#JQz*xGgdIwfhA$WNL|JS(TFJ<=hLnp#<$Z+jmL%6L^eUcZx6RgxgYi=KlVCDp2p9s@An=U2d+HOg{Qo}^_E?R;O_YW})d@J1)%Wxz TU~&G}ZuM1%W8yaiDn#Hng@M*@ literal 0 HcmV?d00001 diff --git a/lib/board.rb b/lib/board.rb index fc24264..e2256c0 100644 --- a/lib/board.rb +++ b/lib/board.rb @@ -11,6 +11,10 @@ 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? diff --git a/lib/connect_four.rb b/lib/connect_four.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/database.rb b/lib/database.rb new file mode 100644 index 0000000..956b04f --- /dev/null +++ b/lib/database.rb @@ -0,0 +1,16 @@ +require 'sqlite3' + +class DB + + def self.create(name = "connectfour.db") + + unless File.exists?(name) + system("touch #{name}") + system("sqlite3 #{name} < schema.sql") + end + + return SQLite3::Database.new(name) + end +end + + diff --git a/lib/game.rb b/lib/game.rb new file mode 100644 index 0000000..6bc62f3 --- /dev/null +++ b/lib/game.rb @@ -0,0 +1,21 @@ +require './board.rb' + +class Game + attr_reader :player1, :player2, :winner, :board + + def initialize(player1, player2) + @player1 = player1 + @player2 = player2 + @board = Board.new + end + + def next_move(column) + round = next_round + board.place_piece(column, round) + end + + def next_round + @board.empty_cells.even? ? "black" : "red" + end + +end diff --git a/lib/player.rb b/lib/player.rb new file mode 100644 index 0000000..0785f35 --- /dev/null +++ b/lib/player.rb @@ -0,0 +1,11 @@ +class Player + attr_reader :name, :twitter, :password, :id + + def initialize(params = {}) + @name = params[:name] + @twitter = params[:twitter] + @password = params[:password] + end + + +end \ No newline at end of file diff --git a/spec/connect_four_spec.rb b/spec/connect_four_spec.rb new file mode 100644 index 0000000..e69de29 diff --git a/spec/database_spec.rb b/spec/database_spec.rb new file mode 100644 index 0000000..221ad09 --- /dev/null +++ b/spec/database_spec.rb @@ -0,0 +1,32 @@ +require './lib/database.rb' + +describe DB do + before(:each) { @db = DB.create("test.db") } + after(:each) { system("rm test.db") } + + context "when there's no database" do + it "creates a database" do + File.exists?("test.db").should be_true + end + + it "returns a new db connection" do + @db.should be_an_instance_of SQLite3::Database + end + + it "creates the users table" do + @db.execute("SELECT * FROM games;").should_not raise_error + end + end + + context "when there is already a database" do + # it "does not creates a new database" do + # db = DB.create("test.rb") + # db.execute("INSERT INTO players (name, twitter, password) VALUES ('brent', 'brent', 123);") + # db = DB.create("test.rb") + # db.execute("SELECT COUNT(*) FROM players").should eq 1 + # end + + end + + +end \ No newline at end of file diff --git a/spec/game_spec.rb b/spec/game_spec.rb new file mode 100644 index 0000000..cd5a16b --- /dev/null +++ b/spec/game_spec.rb @@ -0,0 +1,28 @@ +require './lib/game.rb' +require './lib/player.rb' + +describe Game do + + let(:player1) {Player.new({:name => 'brant', :twitter => 'brant', :password => 'master'})} + let(:player2) {Player.new({:name => 'jo', :twitter => 'jauny', :password => 'blah'})} + + before(:each) { @game = Game.new(player1, player2) } + + describe '.new' do + it 'has 2 players' do + @game.player1.should be_true + @game.player2.should be_true + end + + it "has a parameter for winner" do + @game.should respond_to(:winner) + end + + it "has a board" do + @game.should respond_to(:board) + end + end + + + +end \ No newline at end of file diff --git a/spec/player_spec.rb b/spec/player_spec.rb new file mode 100644 index 0000000..a1d98dd --- /dev/null +++ b/spec/player_spec.rb @@ -0,0 +1,19 @@ +require './lib/player.rb' + +describe Player do + before(:each) { @player = Player.new({ name: "Brent", twitter: "Blah", password: "master" }) } + + describe "#initialize" do + it "has a name" do + @player.name.should eq "Brent" + end + + it "has a twitter account" do + @player.twitter.should eq "Blah" + end + + it "has a password" do + @player.password.should eq "master" + end + end +end \ No newline at end of file From 21ba6d2fc93f3b88d4eb2d49c09854226fe20c1d Mon Sep 17 00:00:00 2001 From: Apprentice Date: Thu, 1 Nov 2012 17:10:37 -0700 Subject: [PATCH 06/47] tons o stuff --- db/test.db => connectfour.db | Bin 5120 -> 5120 bytes db/schema.sql | 6 ++++-- lib/connect_four.rb | 8 ++++++++ lib/{ => connect_four}/board.rb | 0 lib/{ => connect_four}/cluster.rb | 0 lib/connect_four/connect_four.rb | 32 ++++++++++++++++++++++++++++++ lib/connect_four/database.rb | 22 ++++++++++++++++++++ lib/{ => connect_four}/game.rb | 4 +--- lib/connect_four/player.rb | 18 +++++++++++++++++ lib/database.rb | 16 --------------- lib/player.rb | 11 ---------- spec/board_spec.rb | 2 +- spec/cluster_spec.rb | 3 +-- spec/connect_four_spec.rb | 1 + spec/database_spec.rb | 11 ++++++---- spec/game_spec.rb | 3 +-- spec/player_spec.rb | 2 +- spec/spec_helper.rb | 1 + 18 files changed, 98 insertions(+), 42 deletions(-) rename db/test.db => connectfour.db (81%) rename lib/{ => connect_four}/board.rb (100%) rename lib/{ => connect_four}/cluster.rb (100%) create mode 100644 lib/connect_four/connect_four.rb create mode 100644 lib/connect_four/database.rb rename lib/{ => connect_four}/game.rb (93%) create mode 100644 lib/connect_four/player.rb delete mode 100644 lib/database.rb delete mode 100644 lib/player.rb create mode 100644 spec/spec_helper.rb diff --git a/db/test.db b/connectfour.db similarity index 81% rename from db/test.db rename to connectfour.db index d1aaa11ce8f7f4714dbb4b6924afe9dd4744f974..97f6bdec5f554f0acb8146c56c7f689a5b733d5a 100644 GIT binary patch delta 441 zcmZqBXwaA-&B!uQ##xkwLHCh7FY_M;7Df&RrrpeEnKBtUCN@stYKmiF7Z(?2Y?Yqe z!MNBjxhOTUBsC>Iu|&be)y*-~CqyB+w5TXGuOz-CGdH!kBr&%@hf4tnN()j5s+_FK zw1caOpOIZ$TAH!Bbn^)$032NQE01M>mqy3K+tMa=aMOzaGz@{-2JE{S=W zK&O^xmXxFxrRC=*B^DVO7#iss8tNJtDi|4CnV4A_m|zjIV`OI#mliiRP6BxrYCuM6 zPEI}+#ppu(Kr^`*7#NtCpEEE&XZ{Lw)NN*EUS@elxLcUGn58+P#_|G{GBST=VE(*W ZP~asqFE=BzG-E+dVr6PkG1DRe764VEet-Y~ delta 197 zcmZqBXwaA-&B#1a##xk^LHCh7FY_M;7REXTrn}5%nYtP4HVd+FF>*C=v9ODai!(MC zPZnZaJeiYe6IWv{BfGe?G-I>j1u8N5 zG_xfeBlBAZ=C_*#1s*fY3No@Y2#O0cFl6OtC6?w@8X6gMF)%PNGJj`a{ti^~mRXpC kS)3Cr#|xJD!od6mDDjG!hl`O}nzJA$u`;!&c#!}L08`j9YXATM diff --git a/db/schema.sql b/db/schema.sql index ce051ac..5211a9e 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -3,8 +3,8 @@ CREATE TABLE IF NOT EXISTS players ( name VARCHAR NOT NULL, twitter VARCHAR NOT NULL UNIQUE, password VARCHAR NOT NULL, - created_at DATETIME, - updated_at DATETIME + created_at DEFAULT current_timestamp, + updated_at DEFAULT current_timestamp ); CREATE TABLE IF NOT EXISTS games ( @@ -12,6 +12,8 @@ CREATE TABLE IF NOT EXISTS games ( player1 INTEGER NOT NULL, player2 INTEGER NOT NULL, winner INTEGER, + created_at DEFAULT current_timestamp, + updated_at DEFAULT current_timestamp, FOREIGN KEY (player1) REFERENCES Players(id), FOREIGN KEY (player2) REFERENCES Players(id) ); \ No newline at end of file diff --git a/lib/connect_four.rb b/lib/connect_four.rb index e69de29..ac9abe1 100644 --- a/lib/connect_four.rb +++ b/lib/connect_four.rb @@ -0,0 +1,8 @@ +require_relative 'connect_four/board' +require_relative 'connect_four/game' +require_relative 'connect_four/player' +require_relative 'connect_four/database' +require_relative 'connect_four/connect_four' +require_relative 'connect_four/cluster' +# game = ConnectFour.new +# game.start \ No newline at end of file diff --git a/lib/board.rb b/lib/connect_four/board.rb similarity index 100% rename from lib/board.rb rename to lib/connect_four/board.rb diff --git a/lib/cluster.rb b/lib/connect_four/cluster.rb similarity index 100% rename from lib/cluster.rb rename to lib/connect_four/cluster.rb diff --git a/lib/connect_four/connect_four.rb b/lib/connect_four/connect_four.rb new file mode 100644 index 0000000..e9b5713 --- /dev/null +++ b/lib/connect_four/connect_four.rb @@ -0,0 +1,32 @@ +class ConnectFour + attr_reader :game + def start + DB.create + @player1 = Player.new(create_player("Player 1")) + @player2 = Player.new(create_player("Player 2")) + @player1.insert_to_db + @player2.insert_to_db + @game = Game.new(@player1, @player2) + play + end + + def create_player(player) + puts "Enter username for #{player}" + player_name = gets.chomp + puts "Enter your twitter account" + player_twitter = gets.chomp + puts "Enter your password" + player_password = gets.chomp + return { name: player_name, twitter: player_twitter, password: player_password } + end + + def play + until game.board.full? || game.board.check_four_consecutive? + game.next_round == "black" ? current_turn = "Player 1" : current_turn = "Player 2" + puts "#{current_turn}, what column do you want to play in?" + game.next_move(gets.chomp) + end + puts "The game is over." + end + +end diff --git a/lib/connect_four/database.rb b/lib/connect_four/database.rb new file mode 100644 index 0000000..f0c1840 --- /dev/null +++ b/lib/connect_four/database.rb @@ -0,0 +1,22 @@ +require 'sqlite3' + +class DB + + def self.create(name = "connectfour.db") + + unless File.exists?(name) + system("touch #{name}") + system("sqlite3 #{name} < ./db/schema.sql") + end + + return SQLite3::Database.new(name) + end + + def self.handler(string, *values) + db = SQLite3::Database.new("connectfour.db") + db.execute(string, *values.flatten) + db.close + end +end + + diff --git a/lib/game.rb b/lib/connect_four/game.rb similarity index 93% rename from lib/game.rb rename to lib/connect_four/game.rb index 6bc62f3..910b3d6 100644 --- a/lib/game.rb +++ b/lib/connect_four/game.rb @@ -1,5 +1,3 @@ -require './board.rb' - class Game attr_reader :player1, :player2, :winner, :board @@ -13,7 +11,7 @@ def next_move(column) round = next_round board.place_piece(column, round) end - + def next_round @board.empty_cells.even? ? "black" : "red" end diff --git a/lib/connect_four/player.rb b/lib/connect_four/player.rb new file mode 100644 index 0000000..85c30db --- /dev/null +++ b/lib/connect_four/player.rb @@ -0,0 +1,18 @@ +class Player + attr_reader :name, :twitter, :password, :id + + def initialize(params = {}) + @name = params[:name] + @twitter = params[:twitter] + @password = params[:password] + end + + def insert_to_db + values = [name, twitter, password] + if DB.handler("select * from players where twitter = #{twitter}").nil? + DB.handler("INSERT INTO players (name, twitter, password) VALUES (?, ?, ?);", values) + end + end + + +end \ No newline at end of file diff --git a/lib/database.rb b/lib/database.rb deleted file mode 100644 index 956b04f..0000000 --- a/lib/database.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'sqlite3' - -class DB - - def self.create(name = "connectfour.db") - - unless File.exists?(name) - system("touch #{name}") - system("sqlite3 #{name} < schema.sql") - end - - return SQLite3::Database.new(name) - end -end - - diff --git a/lib/player.rb b/lib/player.rb deleted file mode 100644 index 0785f35..0000000 --- a/lib/player.rb +++ /dev/null @@ -1,11 +0,0 @@ -class Player - attr_reader :name, :twitter, :password, :id - - def initialize(params = {}) - @name = params[:name] - @twitter = params[:twitter] - @password = params[:password] - end - - -end \ No newline at end of file diff --git a/spec/board_spec.rb b/spec/board_spec.rb index 4736ae9..6fc8d15 100644 --- a/spec/board_spec.rb +++ b/spec/board_spec.rb @@ -1,4 +1,4 @@ -require './lib/board.rb' +require 'spec_helper' describe Board do diff --git a/spec/cluster_spec.rb b/spec/cluster_spec.rb index cf2896c..d512f8a 100644 --- a/spec/cluster_spec.rb +++ b/spec/cluster_spec.rb @@ -1,5 +1,4 @@ -#require './lib/cluster.rb' -require './lib/cluster.rb' +require 'spec_helper' describe Cluster do diff --git a/spec/connect_four_spec.rb b/spec/connect_four_spec.rb index e69de29..f8ec369 100644 --- a/spec/connect_four_spec.rb +++ b/spec/connect_four_spec.rb @@ -0,0 +1 @@ +require 'spec_helper' diff --git a/spec/database_spec.rb b/spec/database_spec.rb index 221ad09..2d43332 100644 --- a/spec/database_spec.rb +++ b/spec/database_spec.rb @@ -1,20 +1,23 @@ -require './lib/database.rb' +require 'spec_helper' describe DB do - before(:each) { @db = DB.create("test.db") } + let(:db) { DB.create("test.db") } after(:each) { system("rm test.db") } context "when there's no database" do it "creates a database" do + db File.exists?("test.db").should be_true end it "returns a new db connection" do - @db.should be_an_instance_of SQLite3::Database + db.should be_an_instance_of SQLite3::Database end it "creates the users table" do - @db.execute("SELECT * FROM games;").should_not raise_error + expect { + db.execute("SELECT * FROM games;") + }.to_not raise_error end end diff --git a/spec/game_spec.rb b/spec/game_spec.rb index cd5a16b..2753f44 100644 --- a/spec/game_spec.rb +++ b/spec/game_spec.rb @@ -1,5 +1,4 @@ -require './lib/game.rb' -require './lib/player.rb' +require 'spec_helper' describe Game do diff --git a/spec/player_spec.rb b/spec/player_spec.rb index a1d98dd..65ab6c9 100644 --- a/spec/player_spec.rb +++ b/spec/player_spec.rb @@ -1,4 +1,4 @@ -require './lib/player.rb' +require 'spec_helper' describe Player do before(:each) { @player = Player.new({ name: "Brent", twitter: "Blah", password: "master" }) } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..79d2a04 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1 @@ +require_relative '../lib/connect_four' \ No newline at end of file From 8bb6504a1d39f329d82ec21fc0d69ae9e1aa63d7 Mon Sep 17 00:00:00 2001 From: Daniela Grossmann + Jonathan Lue Date: Thu, 1 Nov 2012 17:13:18 -0700 Subject: [PATCH 07/47] removing cluster stuff --- lib/connect_four.rb | 2 +- lib/connect_four/cluster.rb | 120 ------------------------------------ spec/cluster_spec.rb | 116 ---------------------------------- 3 files changed, 1 insertion(+), 237 deletions(-) delete mode 100644 lib/connect_four/cluster.rb delete mode 100644 spec/cluster_spec.rb diff --git a/lib/connect_four.rb b/lib/connect_four.rb index ac9abe1..7a7dc31 100644 --- a/lib/connect_four.rb +++ b/lib/connect_four.rb @@ -3,6 +3,6 @@ require_relative 'connect_four/player' require_relative 'connect_four/database' require_relative 'connect_four/connect_four' -require_relative 'connect_four/cluster' + # game = ConnectFour.new # game.start \ No newline at end of file diff --git a/lib/connect_four/cluster.rb b/lib/connect_four/cluster.rb deleted file mode 100644 index ff9b1c8..0000000 --- a/lib/connect_four/cluster.rb +++ /dev/null @@ -1,120 +0,0 @@ -require_relative 'board' - -class Cluster < Board - - def initialize - super - 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 - -# DELETE when TESTED!!!!!! - # def diagonal_1 - # 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_2 - # 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 column == row_num || row == 0 - # 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 (column == row_num || row == 0) && direction == "to_left" - end - diagonal - end - end - - def diagonals - # (diagonal_1 + diagonal_2).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 - -# cluster = Cluster.new -# p cluster.diagonals \ No newline at end of file diff --git a/spec/cluster_spec.rb b/spec/cluster_spec.rb deleted file mode 100644 index d512f8a..0000000 --- a/spec/cluster_spec.rb +++ /dev/null @@ -1,116 +0,0 @@ -require 'spec_helper' - -describe Cluster do - - before(:each) do - @cluster = Cluster.new - end - - context "#rows" do - it "has row_num rows" - end - - context "#columns" do - it "has col_num columns" - end - - context "#get_start_indexes_to_right" do - it "should have unique index numbers" - it "should only include numbers from the first row and the first column" - it "should not include any other numbers than the ones from the first row and the first column" - it "should not include the numbers of the first row bigger than 4" - - end - - context "#get_start_indexes_to_left" do - it "should have unique index numbers" - it "should only include numbers from the first row and the last column" - it "should not include any other numbers than the ones from the first row and the last column" - it "should not include the numbers smaller than 4" - end - - context "#row_index_number" do - it "returns the indexnumber of the row of a certain field" - end - - context "#column_index_number" do - it "returns the indexnumber of the column of a certain field" - end - - context "#diagonal_indexes" do - it "has a maximum of row_num units when its going to right" - it "has a maximum of row_num units when its going to left" - end - - context "#diagonals" do - it "has diagonals in both directions" - it "has gathered the contents of the cells" - end - - context "#check_four_consecutive?" do - it "returns true if there are four occupied cells in a row" do - @cluster.place_piece(1, "Striped") - @cluster.place_piece(2, "Striped") - @cluster.place_piece(3, "Striped") - @cluster.place_piece(4, "Striped") - @cluster.check_four_consecutive?.should be_true - end - - it "returns false if there are four occupied cells in a row" do - @cluster.place_piece(1, "Striped") - @cluster.place_piece(2, "Striped") - @cluster.place_piece(3, "Striped") - @cluster.place_piece(5, "Striped") - @cluster.check_four_consecutive?.should be_false - end - - it "returns true if there are four occupied cells in a column" do - @cluster.place_piece(1, "Striped") - @cluster.place_piece(1, "Striped") - @cluster.place_piece(1, "Striped") - @cluster.place_piece(1, "Striped") - @cluster.check_four_consecutive?.should be_true - end - - it "returns false if there are four occupied cells in a row" do - @cluster.place_piece(1, "Striped") - @cluster.place_piece(1, "Striped") - @cluster.place_piece(1, "Striped") - @cluster.place_piece(5, "Striped") - @cluster.check_four_consecutive?.should be_false - end - - it "returns true if there are four occupied cells in a column" do - @cluster.place_piece(1, "Striped") - @cluster.place_piece(2, "Striped2") - @cluster.place_piece(2, "Striped") - @cluster.place_piece(3, "Striped2") - @cluster.place_piece(3, "Striped") - @cluster.place_piece(3, "Striped") - @cluster.place_piece(4, "Striped") - @cluster.place_piece(4, "Striped2") - @cluster.place_piece(4, "Striped") - @cluster.place_piece(4, "Striped") - @cluster.check_four_consecutive?.should be_true - end - - it "returns false if there are four occupied cells in a row" do - @cluster.place_piece(1, "Striped2") - @cluster.place_piece(2, "Striped2") - @cluster.place_piece(2, "Striped") - @cluster.place_piece(3, "Striped2") - @cluster.place_piece(3, "Striped") - @cluster.place_piece(3, "Striped") - @cluster.place_piece(4, "Striped") - @cluster.place_piece(4, "Striped2") - @cluster.place_piece(4, "Striped") - @cluster.place_piece(4, "Striped") - @cluster.check_four_consecutive?.should be_false - end - end - - context "#four_consecutive" do - - end - -end From 05b2ae40326cd861a9e07df16d28fef21ebca4b5 Mon Sep 17 00:00:00 2001 From: Daniela Grossmann + Jonathan Lue Date: Thu, 1 Nov 2012 17:14:22 -0700 Subject: [PATCH 08/47] updating gems --- Gemfile | 2 ++ Gemfile.lock | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/Gemfile b/Gemfile index e69de29..52b8994 100644 --- a/Gemfile +++ b/Gemfile @@ -0,0 +1,2 @@ +gem 'rspec' +gem 'sqlite3' diff --git a/Gemfile.lock b/Gemfile.lock index 83f331d..d64a427 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,19 @@ GEM specs: + diff-lcs (1.1.3) + 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) + sqlite3 (1.3.6) PLATFORMS ruby DEPENDENCIES + rspec + sqlite3 From 6f20df8a2cd329b8360bad9588ddc2b4ea30b628 Mon Sep 17 00:00:00 2001 From: Daniela Grossmann + Jonathan Lue Date: Thu, 1 Nov 2012 20:51:50 -0700 Subject: [PATCH 09/47] rob fixes --- connectfour.db | Bin 5120 -> 0 bytes lib/connect_four.rb | 2 +- lib/connect_four/connect_four.rb | 10 ++- lib/connect_four/database.rb | 41 ++++++++-- lib/connect_four/game.rb | 40 +++++++++ lib/connect_four/player.rb | 7 +- spec/board_spec.rb | 134 +++++++++++++++---------------- spec/database_spec.rb | 7 +- spec/game_spec.rb | 15 ++-- spec/player_spec.rb | 8 +- 10 files changed, 170 insertions(+), 94 deletions(-) delete mode 100644 connectfour.db diff --git a/connectfour.db b/connectfour.db deleted file mode 100644 index 97f6bdec5f554f0acb8146c56c7f689a5b733d5a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5120 zcmeHKPjAyO6u0AS9Yqx<6rmjIW2>!F(@Y$8KntBaBD%E8Qlws}(#9K++N4WdbzC?M z@u|4*1^5zN_5lz#4mfEt(b6F}P@&0BiT(Wk*nYnkzsP5&ZJ$wm9!3+FVI9Z-LU4#N z0GtJ2i@!NZoJnLk!JPR`FN4~batVD0GTZ^uNAy|pIrAUV^=rAHC<^?bGIuzp7w&|{ zX|vU|iQy3J7|+`Try^V_Vott?P0Jz2#Kv9QJTdG(eo6Y+=s6wJ;$A1ja`sZo>DYZs zBkk7Q>Nwcywc9^y8+T~ueh|-hz4xHe7~YWP^&?nNR29CTrE@O!9A}-hG)E@<>%=8%;a7&; zd|}vlIcse0GsY9Ms>MCaJnfMzwQd~G!^r!^>R;rsSglFhilIW_2Oho3aC2uYOvi%^ zgn^bZh#y0_#2X2{1LzZP1PKHL0vkl&P%41OZA7rH>`Ts-o74S!pKtLdH{}FfykqYx`QgZ&6ZuR#+ei5NSKp>DI@B{S4 BA!Gmm diff --git a/lib/connect_four.rb b/lib/connect_four.rb index 7a7dc31..9e33967 100644 --- a/lib/connect_four.rb +++ b/lib/connect_four.rb @@ -1,7 +1,7 @@ +require_relative 'connect_four/database' require_relative 'connect_four/board' require_relative 'connect_four/game' require_relative 'connect_four/player' -require_relative 'connect_four/database' require_relative 'connect_four/connect_four' # game = ConnectFour.new diff --git a/lib/connect_four/connect_four.rb b/lib/connect_four/connect_four.rb index e9b5713..375726a 100644 --- a/lib/connect_four/connect_four.rb +++ b/lib/connect_four/connect_four.rb @@ -22,9 +22,13 @@ def create_player(player) def play until game.board.full? || game.board.check_four_consecutive? - game.next_round == "black" ? current_turn = "Player 1" : current_turn = "Player 2" - puts "#{current_turn}, what column do you want to play in?" - game.next_move(gets.chomp) + game.next_round == "black" ? current_turn = "#{player1.name}" : current_turn = "#{player2.name}" + if current_turn == "Computer" + game.next_move(rand(7)) + else + puts "#{current_turn}, what column do you want to play in?" + game.next_move(gets.chomp) + end end puts "The game is over." end diff --git a/lib/connect_four/database.rb b/lib/connect_four/database.rb index f0c1840..fb21d79 100644 --- a/lib/connect_four/database.rb +++ b/lib/connect_four/database.rb @@ -1,15 +1,15 @@ require 'sqlite3' class DB - def self.create(name = "connectfour.db") + file_path = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'db', name)) - unless File.exists?(name) - system("touch #{name}") - system("sqlite3 #{name} < ./db/schema.sql") + unless File.exists?(file_path) + #system("touch #{name}") + system("sqlite3 #{file_path} < ./db/schema.sql") end - return SQLite3::Database.new(name) + SQLite3::Database.new(file_path) end def self.handler(string, *values) @@ -17,6 +17,37 @@ def self.handler(string, *values) db.execute(string, *values.flatten) db.close end + + # def self.db + # @db ||= find_or_create + # end + # + # def self.find_or_create + # name = "connectfour.db" + # system("sqlite3 #{name} < ./db/schema.sql") + # SQLite3::Database.new(name) + # end + # + # def self.execute(query, *args) + # db.execute(query, args) + # end +end + +module Database + def db + @db ||= find_or_create + end + + def find_or_create + name = "connectfour.db" + db_path = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'db')) + p db_path + schema_path = File.join(db_path, 'schema.sql') + database_path = File.join(db_path, name) + + system("sqlite3 #{file_path} < #{schema_path}") + SQLite3::Database.new(database_path) + end end diff --git a/lib/connect_four/game.rb b/lib/connect_four/game.rb index 910b3d6..8ba0db1 100644 --- a/lib/connect_four/game.rb +++ b/lib/connect_four/game.rb @@ -1,12 +1,25 @@ class Game + include Database attr_reader :player1, :player2, :winner, :board def initialize(player1, player2) @player1 = player1 @player2 = player2 + @players = [@player1, @player2] @board = Board.new end + def start! + while !over? + if board.place(current_player.move) + toggle_player + else + puts "invalid move" + end + end + puts board.winner || "Tie game!" + end + def next_move(column) round = next_round board.place_piece(column, round) @@ -16,4 +29,31 @@ def next_round @board.empty_cells.even? ? "black" : "red" end + def current_player + @players.first + end + + def toggle_player + @players.rotate! + end + + def save + + end + + def over? + board.full? || board.four_in_a_row? + end + + def self.wins_for(player_id) + db.execute("SELECT COUNT(*) FROM games WHERE winner = ?", player_id).first + end + + def self.losses_for(player_id) + db.execute("SELECT COUNT(*) FROM games WHERE player1 = ? OR player2 = ? AND winner != ? AND winner IS NOT NULL", Array.new(3, player_id)).first + end + + def self.ties_for(player_id) + db.execute("SELECT COUNT(*) FROM games WHERE player1 = ? OR player2 = ? AND winner IS NULL", Array.new(2, player_id)).first + end end diff --git a/lib/connect_four/player.rb b/lib/connect_four/player.rb index 85c30db..adb2878 100644 --- a/lib/connect_four/player.rb +++ b/lib/connect_four/player.rb @@ -1,4 +1,5 @@ class Player + #include Database attr_reader :name, :twitter, :password, :id def initialize(params = {}) @@ -7,10 +8,10 @@ def initialize(params = {}) @password = params[:password] end - def insert_to_db + def save values = [name, twitter, password] - if DB.handler("select * from players where twitter = #{twitter}").nil? - DB.handler("INSERT INTO players (name, twitter, password) VALUES (?, ?, ?);", values) + if db.execute("SELECT * FROM players WHERE twitter = ?", twitter).empty? + db.execute("INSERT INTO players (name, twitter, password) VALUES (?, ?, ?);", values) end end diff --git a/spec/board_spec.rb b/spec/board_spec.rb index 6fc8d15..9ad26c3 100644 --- a/spec/board_spec.rb +++ b/spec/board_spec.rb @@ -2,79 +2,79 @@ describe Board do - before(:each){ @board = Board.new} + let(:board) { Board.new } context "#initialize" do it "should exist" do - @board.should_not be_nil + board.should_not be_nil end it "accepts a number of columns" do - @board.col_num.should eq 7 + board.col_num.should eq 7 end it "accepts a number of rows" do - @board.row_num.should eq 6 + board.row_num.should eq 6 end it "is empty" do - @board.should be_empty + board.should be_empty end end context "#place_piece" do it "places a piece on the board" do - @board.place_piece(1, "anything") - @board.should_not be_empty + board.place_piece(1, "anything") + board.should_not be_empty end it "places the piece in the correct position" do - @board.place_piece(3, "anything") - @board.cells[37].should_not be_empty + board.place_piece(3, "anything") + board.cells[37].should_not be_empty end it "places the correct colored piece on the board" do - @board.place_piece(1, "Striped") - @board.cells[35].should eq "Striped" + board.place_piece(1, "Striped") + board.cells[35].should eq "Striped" end it "doesn't put a piece in an occupied cell" do - @board.place_piece(1, "Striped") - @board.place_piece(1, "Striped") - @board.place_piece(1, "Striped") - @board.cells[21].should eq "Striped" + board.place_piece(1, "Striped") + board.place_piece(1, "Striped") + board.place_piece(1, "Striped") + board.cells[21].should eq "Striped" end end context "#full?" do it "returns true if the board is full" do - @board.cells.map! { |cell| cell = "tooth fairy"} - @board.should be_full + board.cells.map! { |cell| cell = "tooth fairy"} + board.should be_full end it "returns false of the board is not full" do - @board.should_not be_full + board.should_not be_full end end context "#rows" do it "has row_num rows" do # TODO: better way for testing this???? - @board.rows.length.should eq(@board.row_num) + board.rows.length.should eq(board.row_num) end end context "#columns" do it "has col_num columns" do # TODO: better way for testing this???? - @board.columns.length.should eq(@board.col_num) + board.columns.length.should eq(board.col_num) end end context "#get_start_indexes_to_right" do it "should have unique index numbers" do - @board.get_start_indexes_to_right(4).uniq.length.should eq(@board.get_start_indexes_to_right(4).length) + board.get_start_indexes_to_right(4).uniq.length.should eq(board.get_start_indexes_to_right(4).length) end it "should only include numbers from the first row and the first column" it "should not include any other numbers than the ones from the first row and the first column" @@ -84,7 +84,7 @@ context "#get_start_indexes_to_left" do it "should have unique index numbers" do - @board.get_start_indexes_to_left(4).uniq.length.should eq(@board.get_start_indexes_to_right(4).length) + board.get_start_indexes_to_left(4).uniq.length.should eq(board.get_start_indexes_to_right(4).length) end it "should only include numbers from the first row and the last column" it "should not include any other numbers than the ones from the first row and the last column" @@ -107,73 +107,73 @@ context "#diagonals" do it "has diagonals in both directions" it "has gathered the empty contents of the cells" do - @board.diagonals[0].should include('') + board.diagonals[0].should include('') end it "has gathered the contents of the cells" do - @board.place_piece(6, "Token") - @board.diagonals[0].should include("Token") + board.place_piece(6, "Token") + board.diagonals[0].should include("Token") end end context "#check_four_consecutive?" do it "returns true if there are four occupied cells in a row" do - @board.place_piece(1, "Striped") - @board.place_piece(2, "Striped") - @board.place_piece(3, "Striped") - @board.place_piece(4, "Striped") - @board.check_four_consecutive?.should be_true + board.place_piece(1, "Striped") + board.place_piece(2, "Striped") + board.place_piece(3, "Striped") + board.place_piece(4, "Striped") + board.check_four_consecutive?.should be_true end it "returns false if there are four occupied cells in a row" do - @board.place_piece(1, "Striped") - @board.place_piece(2, "Striped") - @board.place_piece(3, "Striped") - @board.place_piece(5, "Striped") - @board.check_four_consecutive?.should be_false + board.place_piece(1, "Striped") + board.place_piece(2, "Striped") + board.place_piece(3, "Striped") + board.place_piece(5, "Striped") + board.check_four_consecutive?.should be_false end it "returns true if there are four occupied cells in a column" do - @board.place_piece(1, "Striped") - @board.place_piece(1, "Striped") - @board.place_piece(1, "Striped") - @board.place_piece(1, "Striped") - @board.check_four_consecutive?.should be_true + board.place_piece(1, "Striped") + board.place_piece(1, "Striped") + board.place_piece(1, "Striped") + board.place_piece(1, "Striped") + board.check_four_consecutive?.should be_true end it "returns false if there are four occupied cells in a row" do - @board.place_piece(1, "Striped") - @board.place_piece(1, "Striped") - @board.place_piece(1, "Striped") - @board.place_piece(5, "Striped") - @board.check_four_consecutive?.should be_false + board.place_piece(1, "Striped") + board.place_piece(1, "Striped") + board.place_piece(1, "Striped") + board.place_piece(5, "Striped") + board.check_four_consecutive?.should be_false end it "returns true if there are four occupied cells in a column" do - @board.place_piece(1, "Test") - @board.place_piece(2, "Striped2") - @board.place_piece(2, "Test") - @board.place_piece(3, "Striped2") - @board.place_piece(3, "Striped") - @board.place_piece(3, "Test") - @board.place_piece(4, "Striped") - @board.place_piece(4, "Striped2") - @board.place_piece(4, "Striped") - @board.place_piece(4, "Test") - @board.check_four_consecutive?.should be_true + board.place_piece(1, "Test") + board.place_piece(2, "Striped2") + board.place_piece(2, "Test") + board.place_piece(3, "Striped2") + board.place_piece(3, "Striped") + board.place_piece(3, "Test") + board.place_piece(4, "Striped") + board.place_piece(4, "Striped2") + board.place_piece(4, "Striped") + board.place_piece(4, "Test") + board.check_four_consecutive?.should be_true end it "returns false if there are four occupied cells in a row" do - @board.place_piece(1, "Test_wrong") - @board.place_piece(2, "Striped2") - @board.place_piece(2, "Test") - @board.place_piece(3, "Striped2") - @board.place_piece(3, "Striped") - @board.place_piece(3, "Test") - @board.place_piece(4, "Striped") - @board.place_piece(4, "Striped2") - @board.place_piece(4, "Striped") - @board.place_piece(4, "Test") - @board.check_four_consecutive?.should be_false + board.place_piece(1, "Test_wrong") + board.place_piece(2, "Striped2") + board.place_piece(2, "Test") + board.place_piece(3, "Striped2") + board.place_piece(3, "Striped") + board.place_piece(3, "Test") + board.place_piece(4, "Striped") + board.place_piece(4, "Striped2") + board.place_piece(4, "Striped") + board.place_piece(4, "Test") + board.check_four_consecutive?.should be_false end end diff --git a/spec/database_spec.rb b/spec/database_spec.rb index 2d43332..23dcdc6 100644 --- a/spec/database_spec.rb +++ b/spec/database_spec.rb @@ -1,13 +1,14 @@ -require 'spec_helper' +require_relative 'spec_helper' describe DB do + let(:path) { File.expand_path(File.join(File.dirname(__FILE__), '..', 'db', 'test.db')) } let(:db) { DB.create("test.db") } - after(:each) { system("rm test.db") } + after(:each) { system("rm #{path}") } context "when there's no database" do it "creates a database" do db - File.exists?("test.db").should be_true + File.exists?(path).should be_true end it "returns a new db connection" do diff --git a/spec/game_spec.rb b/spec/game_spec.rb index 2753f44..77fd45f 100644 --- a/spec/game_spec.rb +++ b/spec/game_spec.rb @@ -2,23 +2,22 @@ describe Game do - let(:player1) {Player.new({:name => 'brant', :twitter => 'brant', :password => 'master'})} - let(:player2) {Player.new({:name => 'jo', :twitter => 'jauny', :password => 'blah'})} - - before(:each) { @game = Game.new(player1, player2) } + let(:player1) { Player.new({:name => 'brant', :twitter => 'brant', :password => 'master'}) } + let(:player2) { Player.new({:name => 'jo', :twitter => 'jauny', :password => 'blah'}) } + let(:game) { Game.new(player1, player2) } describe '.new' do it 'has 2 players' do - @game.player1.should be_true - @game.player2.should be_true + game.player1.should be_true + game.player2.should be_true end it "has a parameter for winner" do - @game.should respond_to(:winner) + game.should respond_to(:winner) end it "has a board" do - @game.should respond_to(:board) + game.should respond_to(:board) end end diff --git a/spec/player_spec.rb b/spec/player_spec.rb index 65ab6c9..282c13e 100644 --- a/spec/player_spec.rb +++ b/spec/player_spec.rb @@ -1,19 +1,19 @@ require 'spec_helper' describe Player do - before(:each) { @player = Player.new({ name: "Brent", twitter: "Blah", password: "master" }) } + let(:player) { Player.new({ name: "Brent", twitter: "Blah", password: "master" }) } describe "#initialize" do it "has a name" do - @player.name.should eq "Brent" + player.name.should eq "Brent" end it "has a twitter account" do - @player.twitter.should eq "Blah" + player.twitter.should eq "Blah" end it "has a password" do - @player.password.should eq "master" + player.password.should eq "master" end end end \ No newline at end of file From 64c2ad364b6a7c72d3a74c3ce1f443467adef310 Mon Sep 17 00:00:00 2001 From: Jonathan Pepin Date: Thu, 1 Nov 2012 22:39:09 -0700 Subject: [PATCH 10/47] commiting pulled things --- .schema | 0 database.rb | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 .schema create mode 100644 database.rb diff --git a/.schema b/.schema new file mode 100644 index 0000000..e69de29 diff --git a/database.rb b/database.rb new file mode 100644 index 0000000..e69de29 From adecd90566472671b569f25569221106134ace56 Mon Sep 17 00:00:00 2001 From: Daniela Grossmann + Jonathan Lue Date: Fri, 2 Nov 2012 11:41:57 -0700 Subject: [PATCH 11/47] added printout of game and some ui --- db/connectfour.db | Bin 0 -> 5120 bytes lib/connect_four.rb | 5 ++- lib/connect_four/connect_four.rb | 43 +++++++++++--------- lib/connect_four/database.rb | 42 ++++++++++---------- lib/connect_four/game.rb | 65 +++++++++++++++++++------------ lib/connect_four/player.rb | 7 ++-- 6 files changed, 95 insertions(+), 67 deletions(-) create mode 100644 db/connectfour.db diff --git a/db/connectfour.db b/db/connectfour.db new file mode 100644 index 0000000000000000000000000000000000000000..90cdd8be1286738d645827f6066b59916ebdc5a1 GIT binary patch literal 5120 zcmeH~&u-H&7{Ki~TcN1xF^3%RvC&r1q#R%;vf_@2EbX!ssT`=%#zT?Xv`ZXy<2b}) z@FY9~JOr-|veR<@Cf38O3MQc;YkM1TugSJi{0O zt^%;b&q5@wENq3qLix*82KBEGE9eK1;XNRILZ79OD_ba6Z{(7qDDZ>I{Nb3s^(QpR zhMks6OpjpCJhTbUOt@CYT*3fbjz^A&i@UCMY`T5?lJv3J^E#HpvyO@5?Prm*v42Tp z{a3it@vzgg?ccq|4c>VeMKms)%ZyP>eMW-;@7QzFHhZ>*M`;|>hz(fC7tZ|2Y#Eu( zf`5!`cU)o}Iavv{e8C!aNt?LDX^~Uh&5B5BVW2JN-jHG3lu_2YXjj?WgZ&I|az5s7 z-5{aoDUC*S?N(kvzUHh&yfyngQ@g6dlA@~ceVWa=Tyv6Nq?I{x<9|19@htqxbXzY> z7q51W%|pg`W7fU6=U68_lDF1RlKC_a{)oDbK9;q*w5ymZ1Ys1=i=3OEvT1fd$RP~$ zTtR#r$`w9H=p#U1_#i|O5Cpb|z$2*ys+DT9`TCvF(2csT*Bb`bpBy}H95kP9Ff|}g zsPcrv;c$a{gQ?7`mC!Xn*XaB96fRaG2nYhVM?gjuD9NaLf4KPmL-Y;sLx>cfWp7p&%d# board.col_num end def next_round - @board.empty_cells.even? ? "black" : "red" - end - - def current_player - @players.first - end - - def toggle_player - @players.rotate! - end - - def save - - end - - def over? - board.full? || board.four_in_a_row? + @board.empty_cells.even? ? "O" : "X" end def self.wins_for(player_id) @@ -57,3 +45,32 @@ def self.ties_for(player_id) db.execute("SELECT COUNT(*) FROM games WHERE player1 = ? OR player2 = ? AND winner IS NULL", Array.new(2, player_id)).first end end + + + +# def start! + # while !over? + # if board.place(current_player.move) + # toggle_player + # else + # puts "invalid move" + # end + # end + # puts board.winner || "Tie game!" + # end + + # def current_player + # @players.first + # end + # + # def toggle_player + # @players.rotate! + # end + # + # def save + # + # end + + # def over? + # board.full? || board.four_in_a_row? + # end diff --git a/lib/connect_four/player.rb b/lib/connect_four/player.rb index adb2878..fc851cf 100644 --- a/lib/connect_four/player.rb +++ b/lib/connect_four/player.rb @@ -1,5 +1,6 @@ class Player - #include Database + # include Database + attr_reader :name, :twitter, :password, :id def initialize(params = {}) @@ -10,8 +11,8 @@ def initialize(params = {}) def save values = [name, twitter, password] - if db.execute("SELECT * FROM players WHERE twitter = ?", twitter).empty? - db.execute("INSERT INTO players (name, twitter, password) VALUES (?, ?, ?);", values) + if DB.handler("SELECT * FROM players WHERE twitter = ?", twitter).empty? + DB.handler("INSERT INTO players (name, twitter, password) VALUES (?, ?, ?);", values) end end From 7a37fd9887e9b8b26c6bec7acdf59b446d47ded2 Mon Sep 17 00:00:00 2001 From: Daniela Grossmann + Jonathan Lue Date: Fri, 2 Nov 2012 11:52:44 -0700 Subject: [PATCH 12/47] switched order of players --- lib/connect_four/game.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/connect_four/game.rb b/lib/connect_four/game.rb index a017993..6ba36cd 100644 --- a/lib/connect_four/game.rb +++ b/lib/connect_four/game.rb @@ -11,7 +11,7 @@ def initialize(player1, player2) def play until board.full? || board.check_four_consecutive? - next_round == "O" ? current_turn = "#{player1.name}" : current_turn = "#{player2.name}" + next_round == "X" ? current_turn = "#{player1.name}" : current_turn = "#{player2.name}" if current_turn == "Computer" next_move(rand(7)) else @@ -30,7 +30,7 @@ def next_move(column) end def next_round - @board.empty_cells.even? ? "O" : "X" + @board.empty_cells.even? ? "X" : "O" end def self.wins_for(player_id) From 50f54ccee4bebd83b953a1b73591313d7424b06e Mon Sep 17 00:00:00 2001 From: Daniela Grossmann + Jonathan Lue Date: Fri, 2 Nov 2012 15:44:31 -0700 Subject: [PATCH 13/47] git ignores now db --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 560d1a6..42b07e6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.db *.gem *.rbc .bundle @@ -16,3 +17,4 @@ tmp .yardoc _yardoc doc/ +oauth/ From ecd972c7900c401b19152b9e329e07dc8025672d Mon Sep 17 00:00:00 2001 From: Daniela Grossmann + Jonathan Lue Date: Fri, 2 Nov 2012 15:45:17 -0700 Subject: [PATCH 14/47] Gemfile changes --- Gemfile | 2 ++ Gemfile.lock | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/Gemfile b/Gemfile index 52b8994..2ad1943 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,4 @@ gem 'rspec' gem 'sqlite3' +gem 'tweetstream' +gem 'twitter' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index d64a427..0b86424 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,27 @@ GEM 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) @@ -9,7 +30,18 @@ GEM 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 @@ -17,3 +49,5 @@ PLATFORMS DEPENDENCIES rspec sqlite3 + tweetstream + twitter From 814023f8d3f069dce63b881335ca0a4afb4841e6 Mon Sep 17 00:00:00 2001 From: Daniela Grossmann + Jonathan Lue Date: Fri, 2 Nov 2012 15:46:19 -0700 Subject: [PATCH 15/47] Twitter class init --- lib/connect_four/twitter.rb | 82 +++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 lib/connect_four/twitter.rb diff --git a/lib/connect_four/twitter.rb b/lib/connect_four/twitter.rb new file mode 100644 index 0000000..32dc57b --- /dev/null +++ b/lib/connect_four/twitter.rb @@ -0,0 +1,82 @@ +require 'tweetstream' +#require 'player' +#require 'game' + +class Tweet + + def initialize + @file_path = File.expand_path(File.join(File.dirname(__FILE__) , 'oauth'))#, '..', '..', 'db', name)) + consumer_key_path = File.join(@file_path, "consumer_key.txt") + consumer_secret_path = File.join(@file_path, "consumer_secret.txt") + oauth_token_path = File.join(@file_path, "oauth_token.txt") + oauth_token_secret_path = File.join(@file_path, "oauth_token_secret.txt") + + Twitter.configure do |config| + config.consumer_key = File.read(consumer_key_path) + config.consumer_secret = File.read(consumer_secret_path) + config.oauth_token = File.read(oauth_token_path) + config.oauth_token_secret = File.read(oauth_token_secret_path) + end + + TweetStream.configure do |config| + config.consumer_key = File.read(consumer_key_path) + config.consumer_secret = File.read(consumer_secret_path) + config.oauth_token = File.read(oauth_token_path) + config.oauth_token_secret = File.read(oauth_token_secret_path) + config.auth_method = :oauth + end + game_play + end + + def track_new_game + TweetStream::Client.new.track('bieber') do |status| + Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" + player1 = Player.new(status.user[:name], status.user[:screen_name], "password") + return player1 + end + end + + def game_play + player2 = UI.create_player + player1 = track_new_game + UI.start(player1, player2) + TweetStream::Client.new.track(player1) do |status| + UI.game.board.cells = UI.board_from_twitter(status.text) + UI.player_move(UI.player2.name) + tweet_board(UI.board_to_twitter(UI.game.board.cells)) + end + end + + def tweet_board(tweet, message = '#dbc-c4') + + puts "#{player1.twitter} #{tweet} #{message}" + #Twitter.update("#{player1.twitter} #{tweet} #dbc_c4") + end + + # def board_to_twitter(board_info) + # board_format, row_format = "|", "" + # board_info.each { |field| field == "" ? (row_format += ".") : (row_format += field) } + # row_num.times do |i| + # start_i, end_i = (col_num*i), (col_num*(i+1)) + # board_format += row_format[start_i...end_i] + "|" + # end + # board_format + # end + + # def board_from_twitter(move_string) + # board_info = move_string.gsub(/\|/, "").split("") + # board_info.each { |field| field.gsub!(/\./, "") } + # board_info + # end + + #when you receive a tweet, create a player2 and respond with game on. + + #initiate a game with two players + + #start listening for the other player's tweets and look for first move. + + #when you receive the first move tweet plug it into game and prompt player for next move. + + #format next move and tweet + +end From 832807255d84f7a21d18b784e5a01c66f12bfb56 Mon Sep 17 00:00:00 2001 From: Daniela Grossmann + Jonathan Lue Date: Fri, 2 Nov 2012 15:48:02 -0700 Subject: [PATCH 16/47] changes in c_4, board, db, game; changed name to ui --- db/connectfour.db | Bin 5120 -> 5120 bytes lib/connect_four.rb | 7 +-- lib/connect_four/board.rb | 3 +- lib/connect_four/connect_four.rb | 43 ---------------- lib/connect_four/database.rb | 14 ----- lib/connect_four/game.rb | 13 +++-- lib/connect_four/ui.rb | 86 +++++++++++++++++++++++++++++++ 7 files changed, 100 insertions(+), 66 deletions(-) delete mode 100644 lib/connect_four/connect_four.rb create mode 100644 lib/connect_four/ui.rb diff --git a/db/connectfour.db b/db/connectfour.db index 90cdd8be1286738d645827f6066b59916ebdc5a1..cfacb66ae54e45184c4b4201da304bb9a29c3fac 100644 GIT binary patch delta 483 zcmZqBXwaA-&B#4b#+i|OW5N<zh79j(6sE(kbA~flG zGb=+&G%=8n0UK0HczHP%X)HnptWX_(d3k8k##W~29svm%u&^^Efc+j;R)(s@$i&LX z1dEUXGt?626*@$Rc(SSCANPq Date: Fri, 2 Nov 2012 16:11:37 -0700 Subject: [PATCH 17/47] 3 modes in UI to choose --- lib/connect_four/ui.rb | 143 +++++++++++++++++++++++------------------ 1 file changed, 82 insertions(+), 61 deletions(-) diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index acd0aa9..1590966 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -3,84 +3,105 @@ class UI def self.start(@player1, @player2) DB.create - puts "How do you want to play today?" - puts "1 (1 vs 1), 2 (1 vs PC), 3 (1 vs Twitter)" + puts "How do you want to play today? Pick a number" + puts "1 - (1 vs 1), 2 - (1 vs PC), 3 - (1 vs Twitter)" case gets.chomp when "1" - + puts "Good choice!" + UI.create_1vs1_player when "2" - + puts "How dare you! I will demolish you!" + UI.create_1vsPC_player when "3" - + puts "I will send out the message pigeon!" + tweet = Tweet.new + UI.create_1vsTwitter_player else puts "Sorry, playing against Queen Elizabeth is not an option!" UI.start end - # @player1 = Player.new(create_player("Player 1")) - # @player2 = Player.new(create_player("Player 2")) - # @player1.save - # @player2.save @game = Game.new(@player1, @player2) end - def self.game @game end - # def self.create_player(player) - # puts "Enter username for #{player}" - # player_name = gets.chomp.capitalize - # puts "Enter your twitter account" - # player_twitter = gets.chomp - # puts "Enter your password" - # player_password = gets.chomp - # return { name: player_name, twitter: player_twitter, password: player_password } - # end - # - # def self.player_move(current_turn) - # puts "#{current_turn}, what column do you want to play in?" - # game.next_move(gets.chomp.to_i) - # end - # - # def self.congratulations(player) - # - # puts "Congratulations, #{player}. You are a real winner." - # end - # - # def self.print_board - # game.board.rows.each { |row| p row } - # end - # - # def self.board_to_twitter(board_info) - # board_format, row_format = "|", "" - # board_info.each { |field| field == "" ? (row_format += ".") : (row_format += field) } - # game.board.row_num.times do |i| - # start_i, end_i = (game.board.col_num*i), (game.board.col_num*(i+1)) - # board_format += row_format[start_i...end_i] + "|" - # end - # board_format + ' #dbc-c4' - # end - # - # def self.board_from_twitter(move_string) - # board_info = move_string.gsub(/\|/, "").split("") - # board_info.each { |field| field.gsub!(/\./, "") } - # board_info - # end + def self.create_1vs1_player + puts "Who wants to be first?" + @player1 = Player.new(create_player("Player 1")) + @player1.save + puts "Last but not least:" + @player2 = Player.new(create_player("Player 2")) + @player2.save + end + + def self.create_1vsPC_player + puts "Whose name should I write on you gravestone?" + @player1 = computer_player + @player2 = Player.new(create_player("the dead guy")) + @player2.save + end + + def self.create_1vsTwitter_player + @player1 = tweet.track_new_game + @player2 = Player.new(create_player("Player")) + @player2.save + end + + def self.create_player(player) + puts "Enter username for #{player}" + player_name = gets.chomp.capitalize + puts "Enter your twitter account" + player_twitter = gets.chomp + puts "Enter your password" + player_password = gets.chomp + return { name: player_name, twitter: player_twitter, password: player_password } + end + + def self.player_move(current_turn) + puts "#{current_turn}, what column do you want to play in?" + game.next_move(gets.chomp.to_i) + end + + def self.congratulations(player) + + puts "Congratulations, #{player}. You are a real winner." + end + + def self.print_board + game.board.rows.each { |row| p row } + end + + def self.board_to_twitter(board_info) + board_format, row_format = "|", "" + board_info.each { |field| field == "" ? (row_format += ".") : (row_format += field) } + game.board.row_num.times do |i| + start_i, end_i = (game.board.col_num*i), (game.board.col_num*(i+1)) + board_format += row_format[start_i...end_i] + "|" + end + board_format + ' #dbc-c4' + end + def self.board_from_twitter(move_string) + board_info = move_string.gsub(/\|/, "").split("") + board_info.each { |field| field.gsub!(/\./, "") } + board_info + end - # def play - # until game.board.full? || game.board.check_four_consecutive? - # game.next_round == "black" ? current_turn = "#{player1.name}" : current_turn = "#{player2.name}" - # if current_turn == "Computer" - # game.next_move(rand(7)) - # else - # puts "#{current_turn}, what column do you want to play in?" - # game.next_move(gets.chomp) - # end - # end - # puts "The game is over." - # end + # def play + # until game.board.full? || game.board.check_four_consecutive? + # game.next_round == "black" ? current_turn = "#{player1.name}" : current_turn = "#{player2.name}" + # if current_turn == "Computer" + # game.next_move(rand(7)) + # else + # puts "#{current_turn}, what column do you want to play in?" + # game.next_move(gets.chomp) + # end + # end + # puts "The game is over." + # end + # end From 5414e5a1bfeebb8c0a68c76464950a2961f9f2f2 Mon Sep 17 00:00:00 2001 From: Apprentice Date: Fri, 2 Nov 2012 17:08:07 -0700 Subject: [PATCH 18/47] changes in twitter, and minor in c_4, game and ui --- lib/connect_four.rb | 4 +-- lib/connect_four/game.rb | 8 ++++-- lib/connect_four/twitter.rb | 39 +++++++++++++++------------- lib/connect_four/ui.rb | 51 ++++++++++++++++++++----------------- 4 files changed, 58 insertions(+), 44 deletions(-) diff --git a/lib/connect_four.rb b/lib/connect_four.rb index 4ff8371..f73dfb8 100644 --- a/lib/connect_four.rb +++ b/lib/connect_four.rb @@ -6,5 +6,5 @@ require_relative 'connect_four/twitter' # -UI.start -UI.game.play \ No newline at end of file +# UI.start +# UI.game.play \ No newline at end of file diff --git a/lib/connect_four/game.rb b/lib/connect_four/game.rb index b9019e8..d57b368 100644 --- a/lib/connect_four/game.rb +++ b/lib/connect_four/game.rb @@ -22,8 +22,12 @@ def play #UI.print_board puts UI.board_to_twitter(board.cells) end - winner = current_turn - UI.congratulations(current_turn) + if board.full? + winner = nil + else + winner = current_turn + end + UI.congratulations(winner) #save game end diff --git a/lib/connect_four/twitter.rb b/lib/connect_four/twitter.rb index 32dc57b..5f1f51f 100644 --- a/lib/connect_four/twitter.rb +++ b/lib/connect_four/twitter.rb @@ -1,6 +1,7 @@ require 'tweetstream' #require 'player' #require 'game' +require_relative 'ui' class Tweet @@ -29,7 +30,8 @@ def initialize end def track_new_game - TweetStream::Client.new.track('bieber') do |status| + TweetStream::Client.new.track('#dbc_c4') do |status| + puts "#{status.text}" Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" player1 = Player.new(status.user[:name], status.user[:screen_name], "password") return player1 @@ -37,20 +39,30 @@ def track_new_game end def game_play - player2 = UI.create_player - player1 = track_new_game - UI.start(player1, player2) + player2 = UI.create_player("Player 2")#get manual user info and create local user + player1 = track_new_game#create player from twitter + UI.start(player1, player2)#create game instance + TweetStream::Client.new.track(player1) do |status| UI.game.board.cells = UI.board_from_twitter(status.text) UI.player_move(UI.player2.name) - tweet_board(UI.board_to_twitter(UI.game.board.cells)) + #test board + if UI.game.board.full? # tie + message = "Draw game. Play again? #dbc_c4" + elsif UI.game.board.check_four_consecutive? #winner + puts "Somebody won." + #message = "I win! Good game. #dbc_c4" + #message = "You win. #dbc_c4" + else + message = '#dbc_c4' + end + tweet_board(UI.board_to_twitter(UI.game.board.cells, message)) end end - def tweet_board(tweet, message = '#dbc-c4') - + def tweet_board(tweet, message) puts "#{player1.twitter} #{tweet} #{message}" - #Twitter.update("#{player1.twitter} #{tweet} #dbc_c4") + #Twitter.update("#{player1.twitter} #{tweet} #{message}") end # def board_to_twitter(board_info) @@ -69,14 +81,7 @@ def tweet_board(tweet, message = '#dbc-c4') # board_info # end - #when you receive a tweet, create a player2 and respond with game on. - - #initiate a game with two players - - #start listening for the other player's tweets and look for first move. - - #when you receive the first move tweet plug it into game and prompt player for next move. - - #format next move and tweet end + +Tweet.new \ No newline at end of file diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index 1590966..505cf86 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -1,7 +1,7 @@ class UI attr_reader :game - def self.start(@player1, @player2) + def self.start(player1, player2) DB.create puts "How do you want to play today? Pick a number" puts "1 - (1 vs 1), 2 - (1 vs PC), 3 - (1 vs Twitter)" @@ -27,6 +27,7 @@ def self.game @game end + def self.create_1vs1_player puts "Who wants to be first?" @player1 = Player.new(create_player("Player 1")) @@ -49,6 +50,7 @@ def self.create_1vsTwitter_player @player2.save end + def self.create_player(player) puts "Enter username for #{player}" player_name = gets.chomp.capitalize @@ -60,34 +62,37 @@ def self.create_player(player) end def self.player_move(current_turn) - puts "#{current_turn}, what column do you want to play in?" - game.next_move(gets.chomp.to_i) - end - - def self.congratulations(player) + puts "#{current_turn}, what column do you want to play in?" + game.next_move(gets.chomp.to_i) + end - puts "Congratulations, #{player}. You are a real winner." + def self.congratulations(player) + if player == nil + puts "Draw." + else + puts "Congratulations #{player}. You are a real winner." end + end - def self.print_board - game.board.rows.each { |row| p row } - end + def self.print_board + game.board.rows.each { |row| p row } + end - def self.board_to_twitter(board_info) - board_format, row_format = "|", "" - board_info.each { |field| field == "" ? (row_format += ".") : (row_format += field) } - game.board.row_num.times do |i| - start_i, end_i = (game.board.col_num*i), (game.board.col_num*(i+1)) - board_format += row_format[start_i...end_i] + "|" - end - board_format + ' #dbc-c4' + def self.board_to_twitter(board_info) + board_format, row_format = "|", "" + board_info.each { |field| field == "" ? (row_format += ".") : (row_format += field) } + game.board.row_num.times do |i| + start_i, end_i = (game.board.col_num*i), (game.board.col_num*(i+1)) + board_format += row_format[start_i...end_i] + "|" end + board_format + end - def self.board_from_twitter(move_string) - board_info = move_string.gsub(/\|/, "").split("") - board_info.each { |field| field.gsub!(/\./, "") } - board_info - end + def self.board_from_twitter(move_string) + board_info = move_string.gsub(/\|/, "").split("") + board_info.each { |field| field.gsub!(/\./, "") } + board_info + end # def play From 9cbbcd8fbf22fdd998e8047d6809340920abec76 Mon Sep 17 00:00:00 2001 From: Brent Coughenour + Daniela Grossmann Date: Fri, 2 Nov 2012 18:12:44 -0700 Subject: [PATCH 19/47] twitter changes --- lib/connect_four/game.rb | 28 ++++++++++++++--- lib/connect_four/twitter.rb | 62 ++++++++++++++----------------------- lib/connect_four/ui.rb | 22 +++---------- 3 files changed, 52 insertions(+), 60 deletions(-) diff --git a/lib/connect_four/game.rb b/lib/connect_four/game.rb index d57b368..34c0ab1 100644 --- a/lib/connect_four/game.rb +++ b/lib/connect_four/game.rb @@ -2,11 +2,13 @@ class Game include Database attr_reader :player1, :player2, :winner, :board - def initialize(player1, player2) + def initialize(player1, player2, kind) @player1 = player1 @player2 = player2 #@players = [@player1, @player2] @board = Board.new + play if kind == "1" || kind == "2" + play_twitter if kind == "3" end def play @@ -15,9 +17,7 @@ def play if current_turn == "Computer" next_move(rand(7)) else - UI.player_move(current_turn) - # puts "#{current_turn}, what column do you want to play in?" - # next_move(gets.chomp.to_i) + UI.next_move_request(current_turn) end #UI.print_board puts UI.board_to_twitter(board.cells) @@ -31,6 +31,26 @@ def play #save game end + def play_twitter + tweet = Tweet.new + status = tweet.get_status + TweetStream::Client.new.track(player1) do |status| + board.cells = UI.board_from_twitter(status.text) + UI.next_move_request(UI.player2.name) + #test board + if board.full? # tie + message = "Draw game. Play again? #dbc_c4" + elsif board.check_four_consecutive? #winner + puts "Somebody won." + #message = "I win! Good game. #dbc_c4" + #message = "You win. #dbc_c4" + else + message = '#dbc_c4' + end + tweet_board(UI.board_to_twitter(UI.game.board.cells, message)) + end + end + def next_move(column) round = next_round board.place_piece(column, round) unless column > board.col_num diff --git a/lib/connect_four/twitter.rb b/lib/connect_four/twitter.rb index 5f1f51f..49ed163 100644 --- a/lib/connect_four/twitter.rb +++ b/lib/connect_four/twitter.rb @@ -6,6 +6,7 @@ class Tweet def initialize + @random_tag = (('a'..'z').to_a + ('A'..'Z').to_a + (1..9).to_a).shuffle[0..2].join @file_path = File.expand_path(File.join(File.dirname(__FILE__) , 'oauth'))#, '..', '..', 'db', name)) consumer_key_path = File.join(@file_path, "consumer_key.txt") consumer_secret_path = File.join(@file_path, "consumer_secret.txt") @@ -38,50 +39,33 @@ def track_new_game end end - def game_play - player2 = UI.create_player("Player 2")#get manual user info and create local user - player1 = track_new_game#create player from twitter - UI.start(player1, player2)#create game instance - - TweetStream::Client.new.track(player1) do |status| - UI.game.board.cells = UI.board_from_twitter(status.text) - UI.player_move(UI.player2.name) - #test board - if UI.game.board.full? # tie - message = "Draw game. Play again? #dbc_c4" - elsif UI.game.board.check_four_consecutive? #winner - puts "Somebody won." - #message = "I win! Good game. #dbc_c4" - #message = "You win. #dbc_c4" - else - message = '#dbc_c4' - end - tweet_board(UI.board_to_twitter(UI.game.board.cells, message)) - end - end + # def game_play + # player2 = UI.create_player("Player 2")#get manual user info and create local user + # player1 = track_new_game#create player from twitter + # UI.start(player1, player2)#create game instance + # + # TweetStream::Client.new.track(player1) do |status| + # UI.game.board.cells = UI.board_from_twitter(status.text) + # UI.player_move(UI.player2.name) + # #test board + # if UI.game.board.full? # tie + # message = "Draw game. Play again? #dbc_c4" + # elsif UI.game.board.check_four_consecutive? #winner + # puts "Somebody won." + # #message = "I win! Good game. #dbc_c4" + # #message = "You win. #dbc_c4" + # else + # message = '#dbc_c4' + # end + # tweet_board(UI.board_to_twitter(UI.game.board.cells, message)) + # end + # end def tweet_board(tweet, message) - puts "#{player1.twitter} #{tweet} #{message}" + puts "#{player1.twitter} #{tweet} #{message} #{random_tag}" #Twitter.update("#{player1.twitter} #{tweet} #{message}") end - # def board_to_twitter(board_info) - # board_format, row_format = "|", "" - # board_info.each { |field| field == "" ? (row_format += ".") : (row_format += field) } - # row_num.times do |i| - # start_i, end_i = (col_num*i), (col_num*(i+1)) - # board_format += row_format[start_i...end_i] + "|" - # end - # board_format - # end - - # def board_from_twitter(move_string) - # board_info = move_string.gsub(/\|/, "").split("") - # board_info.each { |field| field.gsub!(/\./, "") } - # board_info - # end end - -Tweet.new \ No newline at end of file diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index 505cf86..ddf8533 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -1,7 +1,7 @@ class UI attr_reader :game - def self.start(player1, player2) + def self.start DB.create puts "How do you want to play today? Pick a number" puts "1 - (1 vs 1), 2 - (1 vs PC), 3 - (1 vs Twitter)" @@ -16,11 +16,12 @@ def self.start(player1, player2) puts "I will send out the message pigeon!" tweet = Tweet.new UI.create_1vsTwitter_player + kind = "3" else puts "Sorry, playing against Queen Elizabeth is not an option!" UI.start end - @game = Game.new(@player1, @player2) + @game = Game.new(@player1, @player2, kind) end def self.game @@ -61,7 +62,7 @@ def self.create_player(player) return { name: player_name, twitter: player_twitter, password: player_password } end - def self.player_move(current_turn) + def self.next_move_request(current_turn) #NAME_CHANGE from player_move puts "#{current_turn}, what column do you want to play in?" game.next_move(gets.chomp.to_i) end @@ -94,19 +95,6 @@ def self.board_from_twitter(move_string) board_info end - - # def play - # until game.board.full? || game.board.check_four_consecutive? - # game.next_round == "black" ? current_turn = "#{player1.name}" : current_turn = "#{player2.name}" - # if current_turn == "Computer" - # game.next_move(rand(7)) - # else - # puts "#{current_turn}, what column do you want to play in?" - # game.next_move(gets.chomp) - # end - # end - # puts "The game is over." - # end - # end +UI.start \ No newline at end of file From 840bab48d9a04005a80e96e12cf74b0e33570c06 Mon Sep 17 00:00:00 2001 From: Brent Coughenour + Daniela Grossmann Date: Fri, 2 Nov 2012 19:11:42 -0700 Subject: [PATCH 20/47] massive refactor --- .gitignore | 1 + bin/connect_four | 6 ++- db/connectfour.db | Bin 5120 -> 5120 bytes lib/connect_four/computer_player.rb | 15 +++++++ lib/connect_four/game.rb | 66 +++++++++++++++------------- lib/connect_four/player.rb | 10 ++++- lib/connect_four/twitter_player.rb | 9 ++++ lib/connect_four/ui.rb | 16 +++---- 8 files changed, 82 insertions(+), 41 deletions(-) mode change 100644 => 100755 bin/connect_four create mode 100644 lib/connect_four/computer_player.rb create mode 100644 lib/connect_four/twitter_player.rb diff --git a/.gitignore b/.gitignore index 42b07e6..ee9d0e0 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ tmp _yardoc doc/ oauth/ +db/connectfour.db \ No newline at end of file diff --git a/bin/connect_four b/bin/connect_four old mode 100644 new mode 100755 index e1d4259..c244e70 --- a/bin/connect_four +++ b/bin/connect_four @@ -1 +1,5 @@ -#!/usr/bin/ruby \ No newline at end of file +#!/usr/bin/env ruby + +require_relative '../lib/connect_four' + +UI.start \ No newline at end of file diff --git a/db/connectfour.db b/db/connectfour.db index cfacb66ae54e45184c4b4201da304bb9a29c3fac..1122a00683c7d8a2d7332c380c776d528af6ce84 100644 GIT binary patch delta 653 zcmZqBXwaA-%_uxk#+gxgW5N<|#2Ih0jbD6W5Et%Pwt}-oVDrB-}Vqo0H*vuHf zD9CV?VKGDD#76ge13`8Metv#qVa1Mman1JVQ71L_0bvk?rk1GCHvGXwz%q7VcYG#OL? diff --git a/lib/connect_four/computer_player.rb b/lib/connect_four/computer_player.rb new file mode 100644 index 0000000..3fd55b0 --- /dev/null +++ b/lib/connect_four/computer_player.rb @@ -0,0 +1,15 @@ +class ComputerPlayer + + attr_reader :name, :twitter, :password, :id, :piece + + def initialize(params = {}) + @name = params[:name] + @twitter = params[:twitter] + @password = params[:password] + @piece = params[:piece] + end + + def move + rand(7) + end +end \ No newline at end of file diff --git a/lib/connect_four/game.rb b/lib/connect_four/game.rb index 34c0ab1..72f86df 100644 --- a/lib/connect_four/game.rb +++ b/lib/connect_four/game.rb @@ -2,60 +2,66 @@ class Game include Database attr_reader :player1, :player2, :winner, :board - def initialize(player1, player2, kind) + def initialize(player1, player2) @player1 = player1 @player2 = player2 - #@players = [@player1, @player2] + @players = [@player1, @player2] @board = Board.new - play if kind == "1" || kind == "2" - play_twitter if kind == "3" + # play if kind == "1" || kind == "2" + # play_twitter if kind == "3" end def play - until board.full? || board.check_four_consecutive? - next_round == "X" ? current_turn = "#{player1.name}" : current_turn = "#{player2.name}" - if current_turn == "Computer" - next_move(rand(7)) - else - UI.next_move_request(current_turn) - end - #UI.print_board + until over? + board.place_piece(current_player.move.to_i, current_player.piece) + toggle_player unless over? puts UI.board_to_twitter(board.cells) end if board.full? winner = nil else - winner = current_turn + winner = current_player end UI.congratulations(winner) - #save game + end + + def toggle_player + @players.rotate! + end + + def current_player + @players.first end def play_twitter tweet = Tweet.new status = tweet.get_status - TweetStream::Client.new.track(player1) do |status| - board.cells = UI.board_from_twitter(status.text) - UI.next_move_request(UI.player2.name) - #test board - if board.full? # tie - message = "Draw game. Play again? #dbc_c4" - elsif board.check_four_consecutive? #winner - puts "Somebody won." - #message = "I win! Good game. #dbc_c4" - #message = "You win. #dbc_c4" - else - message = '#dbc_c4' - end - tweet_board(UI.board_to_twitter(UI.game.board.cells, message)) - end - end + TweetStream::Client.new.track(player1) do |status| + board.cells = UI.board_from_twitter(status.text) + UI.next_move_request(UI.player2.name) + #test board + if board.full? # tie + message = "Draw game. Play again? #dbc_c4" + elsif board.check_four_consecutive? #winner + puts "Somebody won." + #message = "I win! Good game. #dbc_c4" + #message = "You win. #dbc_c4" + else + message = '#dbc_c4' + end + tweet_board(UI.board_to_twitter(UI.game.board.cells, message)) + end + end def next_move(column) round = next_round board.place_piece(column, round) unless column > board.col_num end + def over? + board.full? || board.check_four_consecutive? + end + def next_round @board.empty_cells.even? ? "X" : "O" end diff --git a/lib/connect_four/player.rb b/lib/connect_four/player.rb index fc851cf..3d09e1b 100644 --- a/lib/connect_four/player.rb +++ b/lib/connect_four/player.rb @@ -1,12 +1,13 @@ class Player # include Database - attr_reader :name, :twitter, :password, :id + attr_reader :name, :twitter, :password, :id, :piece def initialize(params = {}) @name = params[:name] @twitter = params[:twitter] @password = params[:password] + @piece = params[:piece] end def save @@ -16,5 +17,12 @@ def save end end + def move + puts "#{name}, what column do you want to play in?" + gets.chomp.to_i + end + def to_s + name + end end \ No newline at end of file diff --git a/lib/connect_four/twitter_player.rb b/lib/connect_four/twitter_player.rb new file mode 100644 index 0000000..382e394 --- /dev/null +++ b/lib/connect_four/twitter_player.rb @@ -0,0 +1,9 @@ +class TwitterPlayer + def initialize(options={}) + @piece = options[:piece] + end + + def move + rand(7) + end +end \ No newline at end of file diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index ddf8533..4a08726 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -16,12 +16,12 @@ def self.start puts "I will send out the message pigeon!" tweet = Tweet.new UI.create_1vsTwitter_player - kind = "3" else puts "Sorry, playing against Queen Elizabeth is not an option!" UI.start end - @game = Game.new(@player1, @player2, kind) + @game = Game.new(@player1, @player2) + @game.play end def self.game @@ -31,17 +31,17 @@ def self.game def self.create_1vs1_player puts "Who wants to be first?" - @player1 = Player.new(create_player("Player 1")) + @player1 = Player.new(create_player("Player 1").merge(:piece => 'X')) @player1.save puts "Last but not least:" - @player2 = Player.new(create_player("Player 2")) + @player2 = Player.new(create_player("Player 2").merge(:piece => 'O')) @player2.save end def self.create_1vsPC_player puts "Whose name should I write on you gravestone?" @player1 = computer_player - @player2 = Player.new(create_player("the dead guy")) + @player2 = Player.new(create_player("the dead guy").merge(:piece => 'O')) @player2.save end @@ -59,7 +59,7 @@ def self.create_player(player) player_twitter = gets.chomp puts "Enter your password" player_password = gets.chomp - return { name: player_name, twitter: player_twitter, password: player_password } + { name: player_name, twitter: player_twitter, password: player_password } end def self.next_move_request(current_turn) #NAME_CHANGE from player_move @@ -95,6 +95,4 @@ def self.board_from_twitter(move_string) board_info end -end - -UI.start \ No newline at end of file +end \ No newline at end of file From dfbf0c89bc9c068a83f9901f69850ff5c2a8190b Mon Sep 17 00:00:00 2001 From: Brent Coughenour + Daniela Grossmann Date: Fri, 2 Nov 2012 19:31:45 -0700 Subject: [PATCH 21/47] integrate computer_player class --- db/connectfour.db | Bin 5120 -> 7168 bytes lib/connect_four.rb | 5 +- lib/connect_four/board.rb | 7 ++- lib/connect_four/computer_player.rb | 4 +- lib/connect_four/game.rb | 84 +++++++++------------------- lib/connect_four/ui.rb | 10 ++-- 6 files changed, 41 insertions(+), 69 deletions(-) diff --git a/db/connectfour.db b/db/connectfour.db index 1122a00683c7d8a2d7332c380c776d528af6ce84..a9d961605bfb9ad1e8679d1a2c3f6f948d90bfd4 100644 GIT binary patch delta 411 zcmZqBXt0y|!p;jcQi#EBGCPY3_1xRf%mXr)iFpnK^Eu`@n+16$FsB%Zuru(3 iyzZKsit0^c11m!VEJ6mtP#tb*X{gdhMph=4ScCvJ228~O delta 106 zcmZp$XwaA-%__{mz`#0D!JbigW5NPvK3*VKkfD%)`5g0H=IqIQEV2`u+%^}A^fPO5 zfuxu_7?{5?FK1rM+`)W?`8V?v<`2w=n4d6jVZOwCgSii==@s+6&4N5}%$qy7%UKu& I7YVQc0Demy6951J diff --git a/lib/connect_four.rb b/lib/connect_four.rb index f73dfb8..255299e 100644 --- a/lib/connect_four.rb +++ b/lib/connect_four.rb @@ -4,7 +4,4 @@ require_relative 'connect_four/player' require_relative 'connect_four/ui' require_relative 'connect_four/twitter' - -# -# UI.start -# UI.game.play \ No newline at end of file +require_relative 'connect_four/computer_player' \ No newline at end of file diff --git a/lib/connect_four/board.rb b/lib/connect_four/board.rb index 1963565..c594626 100644 --- a/lib/connect_four/board.rb +++ b/lib/connect_four/board.rb @@ -19,7 +19,12 @@ def empty_cells def place_piece(column, piece) position = (col_num * (row_num - 1)) + (column - 1) until cells[position].empty? - position -= col_num + if position < 0 + puts "That column is full. Choose again." + #game.play + else + position -= col_num + end end cells[position] = piece end diff --git a/lib/connect_four/computer_player.rb b/lib/connect_four/computer_player.rb index 3fd55b0..917d483 100644 --- a/lib/connect_four/computer_player.rb +++ b/lib/connect_four/computer_player.rb @@ -4,8 +4,8 @@ class ComputerPlayer def initialize(params = {}) @name = params[:name] - @twitter = params[:twitter] - @password = params[:password] + # @twitter = params[:twitter] + # @password = params[:password] @piece = params[:piece] end diff --git a/lib/connect_four/game.rb b/lib/connect_four/game.rb index 72f86df..5266f88 100644 --- a/lib/connect_four/game.rb +++ b/lib/connect_four/game.rb @@ -7,8 +7,6 @@ def initialize(player1, player2) @player2 = player2 @players = [@player1, @player2] @board = Board.new - # play if kind == "1" || kind == "2" - # play_twitter if kind == "3" end def play @@ -33,38 +31,38 @@ def current_player @players.first end - def play_twitter - tweet = Tweet.new - status = tweet.get_status - TweetStream::Client.new.track(player1) do |status| - board.cells = UI.board_from_twitter(status.text) - UI.next_move_request(UI.player2.name) - #test board - if board.full? # tie - message = "Draw game. Play again? #dbc_c4" - elsif board.check_four_consecutive? #winner - puts "Somebody won." - #message = "I win! Good game. #dbc_c4" - #message = "You win. #dbc_c4" - else - message = '#dbc_c4' - end - tweet_board(UI.board_to_twitter(UI.game.board.cells, message)) - end - end - - def next_move(column) - round = next_round - board.place_piece(column, round) unless column > board.col_num - end + # def play_twitter + # tweet = Tweet.new + # status = tweet.get_status + # TweetStream::Client.new.track(player1) do |status| + # board.cells = UI.board_from_twitter(status.text) + # UI.next_move_request(UI.player2.name) + # #test board + # if board.full? # tie + # message = "Draw game. Play again? #dbc_c4" + # elsif board.check_four_consecutive? #winner + # puts "Somebody won." + # #message = "I win! Good game. #dbc_c4" + # #message = "You win. #dbc_c4" + # else + # message = '#dbc_c4' + # end + # tweet_board(UI.board_to_twitter(UI.game.board.cells, message)) + # end + # end + + # def next_move(column) + # round = next_round + # board.place_piece(column, round) unless column > board.col_num + # end def over? board.full? || board.check_four_consecutive? end - def next_round - @board.empty_cells.even? ? "X" : "O" - end + # def next_round + # @board.empty_cells.even? ? "X" : "O" + # end def self.wins_for(player_id) db.execute("SELECT COUNT(*) FROM games WHERE winner = ?", player_id).first @@ -79,31 +77,3 @@ def self.ties_for(player_id) end end - - -# def start! - # while !over? - # if board.place(current_player.move) - # toggle_player - # else - # puts "invalid move" - # end - # end - # puts board.winner || "Tie game!" - # end - - # def current_player - # @players.first - # end - # - # def toggle_player - # @players.rotate! - # end - # - # def save - # - # end - - # def over? - # board.full? || board.four_in_a_row? - # end diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index 4a08726..f84f42e 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -39,16 +39,16 @@ def self.create_1vs1_player end def self.create_1vsPC_player - puts "Whose name should I write on you gravestone?" - @player1 = computer_player - @player2 = Player.new(create_player("the dead guy").merge(:piece => 'O')) + #puts "Whose name should I write on you gravestone?" + @player1 = ComputerPlayer.new({name: "MCP", piece: 'X'}) + @player2 = Player.new(create_player("Player 2").merge(:piece => 'O')) @player2.save end def self.create_1vsTwitter_player @player1 = tweet.track_new_game @player2 = Player.new(create_player("Player")) - @player2.save + #@player2.save end @@ -71,7 +71,7 @@ def self.congratulations(player) if player == nil puts "Draw." else - puts "Congratulations #{player}. You are a real winner." + puts "Congratulations #{player.name}. You are a real winner." end end From d76ba2e0e994421da494f9d3e8da674841b152c8 Mon Sep 17 00:00:00 2001 From: Brent Coughenour Date: Sat, 3 Nov 2012 10:47:50 -0700 Subject: [PATCH 22/47] removed .db file --- db/connectfour.db | Bin 7168 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 db/connectfour.db diff --git a/db/connectfour.db b/db/connectfour.db deleted file mode 100644 index a9d961605bfb9ad1e8679d1a2c3f6f948d90bfd4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 7168 zcmeHLJ#5=X6u#q0qAf>J5EDpS*`*SpvTV~*3U{bLNmMLUrYw?@ z8l#hg0-ZW_Os5Q;x)oizb#6Np2+*lR(Wz5`0$tLfcht!eX&P{b0)fGU_;h^lzWe#! zy_4SU?^U|4i93D!$Z)X&VgN$$F2(?WTjb%%Gc6pc#HKIg8_v})s@?+YzvoiuUl4-} zfcqZ(#C1vKH7ute#dtvw;AdIaX!gv<#*yiGBEF?-#hQj|#m$O_Jr!O{Vp6(oT&~u% z2bzv|_40O6Kfv#62e??T?UbtowXIcacf6LzJ>!XKOJ~iiJ2hObS1R*X`2y>>Ygwi} zHBJyM+caFW-5?%&Pb(Gcl^SjhZQHcmhTA0*ca5XtNz37J`;sk7JGxeWQ1zCu#wMJ{ zx>nM3t-7V{<6UnN&RVyfpY$DqkS{_c^SdY^+`bJDJbIl$kL^{}X8zt`Mx*L8_wf>vCw zmiOu!bFJYx!N~1 zS(P?a3HHiJUBFg%TkI?HlrvKDSn7tPz!|+m2{0j zGK7)KLmn>iAeYN2%8q+T_Tb~;lm5BNxsfC>M0G#uTW))3S^bdZlDQLK`P}8H#6{+t zqG6nwWh_govN7+?nMzz>h`qt!k_et5blVOf?via5q7oAf@xgc;Vh&M>afYZ`R=}*N zZ_W0}WF1r`#u(z!Xyiu_V&s{hJ%pzc5kqXXS^7H5; z3BPlFOjROeLu@viA!fgdgmPjzN#qI|0rU_08hwL4M?aEN;7jxu`T>p5r|1v#E&2uh zioQT!p+C`Y=+qzo(Obko#K5aF5Jzcfu29*zN#)5hm2!qk=f(nBh4cq2y@&+p9mdzG zK}sc121?Sk6w1M~^L~jo@{a8!ZDcG`Iar|5=BXSfsI=l#j$%}{Pzbk!IcCZJT;fcxHm*kg?_DC$_~+0w8voCtLos3?V&L^*KwM$Bd75dSoj~5@x+1Fn b+gw@oRN_sBD0Mo1ge Date: Sat, 3 Nov 2012 13:59:16 -0700 Subject: [PATCH 23/47] updates to gitignore and gemfile --- .gitignore | 3 ++- Gemfile | 1 + Gemfile.lock | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ee9d0e0..ef2202f 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,5 @@ tmp _yardoc doc/ oauth/ -db/connectfour.db \ No newline at end of file +db/connectfour.db +oauth_test \ No newline at end of file diff --git a/Gemfile b/Gemfile index 2ad1943..ce8f808 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ +source 'https://rubygems.org' gem 'rspec' gem 'sqlite3' gem 'tweetstream' diff --git a/Gemfile.lock b/Gemfile.lock index 0b86424..3ed621a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,4 +1,5 @@ GEM + remote: https://rubygems.org/ specs: addressable (2.3.2) cookiejar (0.3.0) From 44ac2cf16e6f5360a217dc053514269c93fd30df Mon Sep 17 00:00:00 2001 From: Brent Coughenour Date: Sat, 3 Nov 2012 14:06:12 -0700 Subject: [PATCH 24/47] twitter related updates --- lib/connect_four.rb | 3 +- lib/connect_four/board.rb | 10 +++ lib/connect_four/game.rb | 7 +- lib/connect_four/twitter.rb | 66 +++++++---------- lib/connect_four/twitter_player.rb | 63 +++++++++++++++- lib/connect_four/ui.rb | 114 +++++++++++++++++++---------- spec/tweet_spec.rb | 1 + 7 files changed, 179 insertions(+), 85 deletions(-) create mode 100644 spec/tweet_spec.rb diff --git a/lib/connect_four.rb b/lib/connect_four.rb index 255299e..e5f7232 100644 --- a/lib/connect_four.rb +++ b/lib/connect_four.rb @@ -4,4 +4,5 @@ require_relative 'connect_four/player' require_relative 'connect_four/ui' require_relative 'connect_four/twitter' -require_relative 'connect_four/computer_player' \ No newline at end of file +require_relative 'connect_four/computer_player' +require_relative 'connect_four/twitter_player' \ No newline at end of file diff --git a/lib/connect_four/board.rb b/lib/connect_four/board.rb index c594626..db78115 100644 --- a/lib/connect_four/board.rb +++ b/lib/connect_four/board.rb @@ -29,6 +29,16 @@ def place_piece(column, piece) cells[position] = piece end + def to_s + board_format, row_format = "|", "" + cells.each { |field| field == "" ? (row_format += ".") : (row_format += field) } + row_num.times do |i| + start_i, end_i = (col_num*i), (col_num*(i+1)) + board_format += row_format[start_i...end_i] + "|" + end + board_format + end + def full? cells.all? {|cell| cell != "" } end diff --git a/lib/connect_four/game.rb b/lib/connect_four/game.rb index 5266f88..50b577a 100644 --- a/lib/connect_four/game.rb +++ b/lib/connect_four/game.rb @@ -13,7 +13,8 @@ def play until over? board.place_piece(current_player.move.to_i, current_player.piece) toggle_player unless over? - puts UI.board_to_twitter(board.cells) + UI.board_display unless over? + #puts UI.board_to_twitter(board.cells) end if board.full? winner = nil @@ -60,10 +61,6 @@ def over? board.full? || board.check_four_consecutive? end - # def next_round - # @board.empty_cells.even? ? "X" : "O" - # end - def self.wins_for(player_id) db.execute("SELECT COUNT(*) FROM games WHERE winner = ?", player_id).first end diff --git a/lib/connect_four/twitter.rb b/lib/connect_four/twitter.rb index 49ed163..4748b72 100644 --- a/lib/connect_four/twitter.rb +++ b/lib/connect_four/twitter.rb @@ -1,7 +1,7 @@ require 'tweetstream' #require 'player' #require 'game' -require_relative 'ui' +#require_relative 'ui' class Tweet @@ -13,51 +13,39 @@ def initialize oauth_token_path = File.join(@file_path, "oauth_token.txt") oauth_token_secret_path = File.join(@file_path, "oauth_token_secret.txt") - Twitter.configure do |config| - config.consumer_key = File.read(consumer_key_path) - config.consumer_secret = File.read(consumer_secret_path) - config.oauth_token = File.read(oauth_token_path) - config.oauth_token_secret = File.read(oauth_token_secret_path) - end - + Twitter.configure { |config| twit_config(config) } + TweetStream.configure do |config| - config.consumer_key = File.read(consumer_key_path) - config.consumer_secret = File.read(consumer_secret_path) - config.oauth_token = File.read(oauth_token_path) - config.oauth_token_secret = File.read(oauth_token_secret_path) - config.auth_method = :oauth + twit_config(config) + config.auth_method = :oauth end - game_play + #game_play + end + + def twit_config(config) + config.consumer_key = File.read(consumer_key_path) + config.consumer_secret = File.read(consumer_secret_path) + config.oauth_token = File.read(oauth_token_path) + config.oauth_token_secret = File.read(oauth_token_secret_path) end - def track_new_game - TweetStream::Client.new.track('#dbc_c4') do |status| + def twitter_player + TweetStream::Client.new.track('#dbc_c4') do |status, client| puts "#{status.text}" - Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" - player1 = Player.new(status.user[:name], status.user[:screen_name], "password") - return player1 + puts "Game on! #dbc_c4" if "#{status.text}" == "Who wants to get demolished?" + client.stop + #Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" + return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X'}) end end - # def game_play - # player2 = UI.create_player("Player 2")#get manual user info and create local user - # player1 = track_new_game#create player from twitter - # UI.start(player1, player2)#create game instance - # - # TweetStream::Client.new.track(player1) do |status| - # UI.game.board.cells = UI.board_from_twitter(status.text) - # UI.player_move(UI.player2.name) - # #test board - # if UI.game.board.full? # tie - # message = "Draw game. Play again? #dbc_c4" - # elsif UI.game.board.check_four_consecutive? #winner - # puts "Somebody won." - # #message = "I win! Good game. #dbc_c4" - # #message = "You win. #dbc_c4" - # else - # message = '#dbc_c4' - # end - # tweet_board(UI.board_to_twitter(UI.game.board.cells, message)) + # def twitter_move_relay + # TweetStream::Client.new.track("#{player1.twitter}") do |status, client| + # puts "#{status.text}" + # #look for incoming move + # #Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" + # client.stop + # return "#{status.text}" # end # end @@ -68,4 +56,6 @@ def tweet_board(tweet, message) + + end diff --git a/lib/connect_four/twitter_player.rb b/lib/connect_four/twitter_player.rb index 382e394..d5810fa 100644 --- a/lib/connect_four/twitter_player.rb +++ b/lib/connect_four/twitter_player.rb @@ -1,9 +1,70 @@ -class TwitterPlayer + + +class TwitterPlayer < Player +attr_reader :name, :twitter, :piece + def initialize(options={}) + @name = options[:name] + @twitter = options[:twitter] @piece = options[:piece] end + def self.new + @random_tag = (('a'..'z').to_a + ('A'..'Z').to_a + (1..9).to_a).shuffle[0..2].join + @file_path = File.expand_path(File.join(File.dirname(__FILE__) , 'oauth'))#, '..', '..', 'db', name)) + consumer_key_path = File.join(@file_path, "consumer_key.txt") + consumer_secret_path = File.join(@file_path, "consumer_secret.txt") + oauth_token_path = File.join(@file_path, "oauth_token.txt") + oauth_token_secret_path = File.join(@file_path, "oauth_token_secret.txt") + + Twitter.configure { |config| twit_config(config) } + + TweetStream.configure do |config| + twit_config(config) + config.auth_method = :oauth + end + + return twitter_player + + end + + def twit_config(config) + config.consumer_key = File.read(consumer_key_path) + config.consumer_secret = File.read(consumer_secret_path) + config.oauth_token = File.read(oauth_token_path) + config.oauth_token_secret = File.read(oauth_token_secret_path) + end + + def twitter_player + TweetStream::Client.new.track('#dbc_c4') do |status, client| + puts "#{status.text}" + puts "Game on! #dbc_c4" if "#{status.text}" == "Who wants to get demolished?" + client.stop + #Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" + return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X'}) + end + end + def move + #board = to_a(board_as_string) rand(7) end + + + def board_as_string + TweetStream::Client.new.track("#{player1.twitter}") do |status, client| + puts "#{status.text}" + #look for incoming move + #Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" + client.stop + return "#{status.text}" + end + end + + def to_a(move_string) + board_info = move_string.gsub(/\|/, "").split("") + board_info.each { |field| field.gsub!(/\./, "") } + board_info + end + end \ No newline at end of file diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index f84f42e..7482401 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -1,5 +1,6 @@ class UI - attr_reader :game + + attr_reader :game, :tweet def self.start DB.create @@ -28,7 +29,6 @@ def self.game @game end - def self.create_1vs1_player puts "Who wants to be first?" @player1 = Player.new(create_player("Player 1").merge(:piece => 'X')) @@ -46,53 +46,87 @@ def self.create_1vsPC_player end def self.create_1vsTwitter_player - @player1 = tweet.track_new_game - @player2 = Player.new(create_player("Player")) + @tweet = Tweet.new + @player1 = tweet.twitter_player + @player2 = ComputerPlayer.new({name: "MCP", piece: 'O'}) + #@player2 = Player.new(create_player("Player 2").merge(:piece => 'O')) #@player2.save end + def self.board_display + if @player1.class == TwitterPlayer + game.board.to_s + #tweet_board + else + print_board + end + end def self.create_player(player) - puts "Enter username for #{player}" - player_name = gets.chomp.capitalize - puts "Enter your twitter account" - player_twitter = gets.chomp - puts "Enter your password" - player_password = gets.chomp - { name: player_name, twitter: player_twitter, password: player_password } - end + puts "Enter username for #{player}" + player_name = gets.chomp.capitalize + puts "Enter your twitter account" + player_twitter = gets.chomp + puts "Enter your password" + player_password = gets.chomp + { name: player_name, twitter: player_twitter, password: player_password } + end - def self.next_move_request(current_turn) #NAME_CHANGE from player_move - puts "#{current_turn}, what column do you want to play in?" - game.next_move(gets.chomp.to_i) - end + # def self.next_move_request(current_player) #NAME_CHANGE from player_move + # puts "#{current_player}, what column do you want to play in?" + # game.next_move(gets.chomp.to_i) + # end - def self.congratulations(player) - if player == nil - puts "Draw." - else - puts "Congratulations #{player.name}. You are a real winner." - end - end + def self.congratulations(player) + if @player1.class == TwitterPlayer + tag = twitter_tag + tweet_board(game.board.to_s, tag) + else + print_board + if player == nil + puts "Draw." + elsif player.name == "MCP" + puts "You lose, loser." + else + puts "Congratulations #{player.name}. You are a real winner." + end + end + end + + def self.print_board + puts game.board.to_s #needs vertical formatting + #game.board.rows.each { |row| p row } + end - def self.print_board - game.board.rows.each { |row| p row } - end + # def self.board_to_twitter(board_info) + # board_format, row_format = "|", "" + # board_info.each { |field| field == "" ? (row_format += ".") : (row_format += field) } + # game.board.row_num.times do |i| + # start_i, end_i = (game.board.col_num*i), (game.board.col_num*(i+1)) + # board_format += row_format[start_i...end_i] + "|" + # end + # board_format + # end - def self.board_to_twitter(board_info) - board_format, row_format = "|", "" - board_info.each { |field| field == "" ? (row_format += ".") : (row_format += field) } - game.board.row_num.times do |i| - start_i, end_i = (game.board.col_num*i), (game.board.col_num*(i+1)) - board_format += row_format[start_i...end_i] + "|" - end - board_format - end + # def self.board_from_twitter(move_string) + # board_info = move_string.gsub(/\|/, "").split("") + # board_info.each { |field| field.gsub!(/\./, "") } + # board_info + # end - def self.board_from_twitter(move_string) - board_info = move_string.gsub(/\|/, "").split("") - board_info.each { |field| field.gsub!(/\./, "") } - board_info - end + def twitter_tag + if board.full? # tie + message = "Draw game. Play again? #dbc_c4" + elsif board.check_four_consecutive? #winner + if game.board.empty_cells.even? + message = "I win! Good game. #dbc_c4" + else + message = "You win. #dbc_c4" + end + else + message = '#dbc_c4' + end + return message + end end \ No newline at end of file diff --git a/spec/tweet_spec.rb b/spec/tweet_spec.rb new file mode 100644 index 0000000..335cafc --- /dev/null +++ b/spec/tweet_spec.rb @@ -0,0 +1 @@ +require 'spec_helper' \ No newline at end of file From 95f7f977d9a51383e48a9d88883dce1e7d051e6a Mon Sep 17 00:00:00 2001 From: Brent Coughenour Date: Sat, 3 Nov 2012 16:02:28 -0700 Subject: [PATCH 25/47] twitter class creates new player from twitter stream --- lib/connect_four/twitter.rb | 40 +++++++++++------- lib/connect_four/twitter_player.rb | 67 ++++++++++++++++++------------ lib/connect_four/ui.rb | 4 +- spec/tweet_spec.rb | 19 ++++++++- 4 files changed, 84 insertions(+), 46 deletions(-) diff --git a/lib/connect_four/twitter.rb b/lib/connect_four/twitter.rb index 4748b72..7f021a4 100644 --- a/lib/connect_four/twitter.rb +++ b/lib/connect_four/twitter.rb @@ -4,38 +4,43 @@ #require_relative 'ui' class Tweet + attr_reader :random_tag def initialize @random_tag = (('a'..'z').to_a + ('A'..'Z').to_a + (1..9).to_a).shuffle[0..2].join @file_path = File.expand_path(File.join(File.dirname(__FILE__) , 'oauth'))#, '..', '..', 'db', name)) consumer_key_path = File.join(@file_path, "consumer_key.txt") consumer_secret_path = File.join(@file_path, "consumer_secret.txt") - oauth_token_path = File.join(@file_path, "oauth_token.txt") - oauth_token_secret_path = File.join(@file_path, "oauth_token_secret.txt") + oauth_token_path = File.join(@file_path, "access_token.txt") + oauth_token_secret_path = File.join(@file_path, "access_token_secret.txt") - Twitter.configure { |config| twit_config(config) } + Twitter.configure do |config| + config.consumer_key = File.read(consumer_key_path) + config.consumer_secret = File.read(consumer_secret_path) + config.oauth_token = File.read(oauth_token_path) + config.oauth_token_secret = File.read(oauth_token_secret_path) + end TweetStream.configure do |config| - twit_config(config) + config.consumer_key = File.read(consumer_key_path) + config.consumer_secret = File.read(consumer_secret_path) + config.oauth_token = File.read(oauth_token_path) + config.oauth_token_secret = File.read(oauth_token_secret_path) config.auth_method = :oauth end #game_play end - def twit_config(config) - config.consumer_key = File.read(consumer_key_path) - config.consumer_secret = File.read(consumer_secret_path) - config.oauth_token = File.read(oauth_token_path) - config.oauth_token_secret = File.read(oauth_token_secret_path) - end - def twitter_player TweetStream::Client.new.track('#dbc_c4') do |status, client| - puts "#{status.text}" - puts "Game on! #dbc_c4" if "#{status.text}" == "Who wants to get demolished?" - client.stop - #Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" - return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X'}) + #puts status.text + text = status.text.gsub(/\#dbc_c4/, "").strip + if text == "blib?" + client.stop + #puts "@#{status.user[:screen_name]} Game on! #dbc_c4" + Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4 #{random_tag}") + return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X'}) + end end end @@ -59,3 +64,6 @@ def tweet_board(tweet, message) end + +# tweet = Tweet.new +# tweet.twitter_player diff --git a/lib/connect_four/twitter_player.rb b/lib/connect_four/twitter_player.rb index d5810fa..51eeae3 100644 --- a/lib/connect_four/twitter_player.rb +++ b/lib/connect_four/twitter_player.rb @@ -1,7 +1,7 @@ +require 'tweetstream' - -class TwitterPlayer < Player -attr_reader :name, :twitter, :piece +class TwitterPlayer +attr_reader :name, :twitter, :piece, :random_tag def initialize(options={}) @name = options[:name] @@ -9,41 +9,51 @@ def initialize(options={}) @piece = options[:piece] end - def self.new - @random_tag = (('a'..'z').to_a + ('A'..'Z').to_a + (1..9).to_a).shuffle[0..2].join + def self.from_twitter + @random_tag = (('a'..'z').to_a + ('A'..'Z').to_a + (1..9).to_a).shuffle[0..2].join @file_path = File.expand_path(File.join(File.dirname(__FILE__) , 'oauth'))#, '..', '..', 'db', name)) consumer_key_path = File.join(@file_path, "consumer_key.txt") consumer_secret_path = File.join(@file_path, "consumer_secret.txt") - oauth_token_path = File.join(@file_path, "oauth_token.txt") - oauth_token_secret_path = File.join(@file_path, "oauth_token_secret.txt") + oauth_token_path = File.join(@file_path, "access_token.txt") + oauth_token_secret_path = File.join(@file_path, "access_token_secret.txt") - Twitter.configure { |config| twit_config(config) } + Twitter.configure do |config| + config.consumer_key = File.read(consumer_key_path) + config.consumer_secret = File.read(consumer_secret_path) + config.oauth_token = File.read(oauth_token_path) + config.oauth_token_secret = File.read(oauth_token_secret_path) + end TweetStream.configure do |config| - twit_config(config) + config.consumer_key = File.read(consumer_key_path) + config.consumer_secret = File.read(consumer_secret_path) + config.oauth_token = File.read(oauth_token_path) + config.oauth_token_secret = File.read(oauth_token_secret_path) config.auth_method = :oauth end - return twitter_player - - end - - def twit_config(config) - config.consumer_key = File.read(consumer_key_path) - config.consumer_secret = File.read(consumer_secret_path) - config.oauth_token = File.read(oauth_token_path) - config.oauth_token_secret = File.read(oauth_token_secret_path) - end - - def twitter_player + puts "...waiting for player" TweetStream::Client.new.track('#dbc_c4') do |status, client| - puts "#{status.text}" - puts "Game on! #dbc_c4" if "#{status.text}" == "Who wants to get demolished?" - client.stop - #Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" - return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X'}) + #puts status.text + text = status.text.gsub(/\#dbc_c4/, "").strip + if text == "bab?" + client.stop + puts "@#{status.user[:screen_name]} Game on! #dbc_c4 #{@random_tag}" + Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4 #{@random_tag}") + return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X'}) + end end end + + # def twitter_player + # TweetStream::Client.new.track('#dbc_c4') do |status, client| + # puts "#{status.text}" + # puts "Game on! #dbc_c4" if "#{status.text}" == "Who wants to get demolished?" + # client.stop + # #Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" + # return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X'}) + # end + # end def move #board = to_a(board_as_string) @@ -67,4 +77,9 @@ def to_a(move_string) board_info end + def tweet_board(tweet, message) + puts "#{player1.twitter} #{tweet} #{message} #{random_tag}" + #Twitter.update("#{player1.twitter} #{tweet} #{message}") + end + end \ No newline at end of file diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index 7482401..c2335f7 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -39,7 +39,6 @@ def self.create_1vs1_player end def self.create_1vsPC_player - #puts "Whose name should I write on you gravestone?" @player1 = ComputerPlayer.new({name: "MCP", piece: 'X'}) @player2 = Player.new(create_player("Player 2").merge(:piece => 'O')) @player2.save @@ -47,10 +46,9 @@ def self.create_1vsPC_player def self.create_1vsTwitter_player @tweet = Tweet.new - @player1 = tweet.twitter_player + @player1 = TwitterPlayer.from_twitter @player2 = ComputerPlayer.new({name: "MCP", piece: 'O'}) #@player2 = Player.new(create_player("Player 2").merge(:piece => 'O')) - #@player2.save end def self.board_display diff --git a/spec/tweet_spec.rb b/spec/tweet_spec.rb index 335cafc..24df01f 100644 --- a/spec/tweet_spec.rb +++ b/spec/tweet_spec.rb @@ -1 +1,18 @@ -require 'spec_helper' \ No newline at end of file +require 'spec_helper' + +describe Tweet do + let(:tweet) {Tweet.new} + + describe "#initialize" do + it "creates a random tag" do + tweet.random_tag.should be_an_instance_of String + end + end + + describe "#twitter_player" do + it "tracks a keyword" do + + end + end + +end \ No newline at end of file From 631173aaa406ea59ecd2bfeea22768a7070cffcf Mon Sep 17 00:00:00 2001 From: Brent Coughenour Date: Sat, 3 Nov 2012 19:19:13 -0700 Subject: [PATCH 26/47] working twitter player --- lib/connect_four/game.rb | 3 +- lib/connect_four/twitter_player.rb | 86 +++++++++++++++++++++--------- lib/connect_four/ui.rb | 62 ++++++++++++--------- 3 files changed, 100 insertions(+), 51 deletions(-) diff --git a/lib/connect_four/game.rb b/lib/connect_four/game.rb index 50b577a..aff94e8 100644 --- a/lib/connect_four/game.rb +++ b/lib/connect_four/game.rb @@ -11,10 +11,9 @@ def initialize(player1, player2) def play until over? - board.place_piece(current_player.move.to_i, current_player.piece) + board.place_piece(current_player.move, current_player.piece) toggle_player unless over? UI.board_display unless over? - #puts UI.board_to_twitter(board.cells) end if board.full? winner = nil diff --git a/lib/connect_four/twitter_player.rb b/lib/connect_four/twitter_player.rb index 51eeae3..b56ae00 100644 --- a/lib/connect_four/twitter_player.rb +++ b/lib/connect_four/twitter_player.rb @@ -1,12 +1,14 @@ require 'tweetstream' class TwitterPlayer -attr_reader :name, :twitter, :piece, :random_tag +attr_reader :name, :twitter, :piece, :random_tag, :id - def initialize(options={}) + def initialize(options={}, tag) @name = options[:name] @twitter = options[:twitter] @piece = options[:piece] + @id = options[:id] + @random_tag = tag end def self.from_twitter @@ -34,43 +36,69 @@ def self.from_twitter puts "...waiting for player" TweetStream::Client.new.track('#dbc_c4') do |status, client| - #puts status.text - text = status.text.gsub(/\#dbc_c4/, "").strip - if text == "bab?" + puts "id = #{status.user[:id]}, text = #{status.text}" + text = status.text.gsub(/#.*/, "").strip + if text == "c" client.stop puts "@#{status.user[:screen_name]} Game on! #dbc_c4 #{@random_tag}" Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4 #{@random_tag}") - return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X'}) + return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X', id: status.user[:id]}, @random_tag) end end end - - # def twitter_player - # TweetStream::Client.new.track('#dbc_c4') do |status, client| - # puts "#{status.text}" - # puts "Game on! #dbc_c4" if "#{status.text}" == "Who wants to get demolished?" - # client.stop - # #Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" - # return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X'}) - # end - # end def move - #board = to_a(board_as_string) - rand(7) + board = to_a(board_as_string) + #p board + column = parsed_board(board) + #puts column + return column + #rand(7) end - def board_as_string - TweetStream::Client.new.track("#{player1.twitter}") do |status, client| - puts "#{status.text}" - #look for incoming move - #Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" + TweetStream::Client.new.follow("#{@id}") do |status, client| + #puts "#{status.text}" + raw_board = status.text.gsub!(/#.*/, "") + board = raw_board.match(/[^@\w*](.*)/).to_s.strip + #puts board client.stop - return "#{status.text}" + return board end end + def parsed_board(board_as_array) + result = nil + board_as_array + board_as_array.each_with_index do |cell, index| + if cell != UI.game.board.cells[index] + # puts "not equal" + #puts "#{cell} at #{index}" + result = (index % 7) + 1 + end + end + return result + end + + + + + # difference = board_as_array - UI.game.board.cells + # puts difference + # difference.each_with_index do |cell, index| + # puts cell + # if cell != "" + # puts "different #{index}" + # result = (index + 1) % 7 + # end + # #return result + # end + # return result + #compare incoming board to old board to find the difference in column + + +#@player2 |.......|.......|.......|.......|.......|..O....| #dbc_c4 + def to_a(move_string) board_info = move_string.gsub(/\|/, "").split("") board_info.each { |field| field.gsub!(/\./, "") } @@ -82,4 +110,14 @@ def tweet_board(tweet, message) #Twitter.update("#{player1.twitter} #{tweet} #{message}") end + # def twitter_player + # TweetStream::Client.new.track('#dbc_c4') do |status, client| + # puts "#{status.text}" + # puts "Game on! #dbc_c4" if "#{status.text}" == "Who wants to get demolished?" + # client.stop + # #Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" + # return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X'}) + # end + # end + end \ No newline at end of file diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index c2335f7..003b823 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -45,21 +45,31 @@ def self.create_1vsPC_player end def self.create_1vsTwitter_player - @tweet = Tweet.new - @player1 = TwitterPlayer.from_twitter + #@tweet = Tweet.new @player2 = ComputerPlayer.new({name: "MCP", piece: 'O'}) #@player2 = Player.new(create_player("Player 2").merge(:piece => 'O')) + @player1 = TwitterPlayer.from_twitter + end def self.board_display - if @player1.class == TwitterPlayer - game.board.to_s - #tweet_board - else + if game.current_player == @player1 + if @player1.class == TwitterPlayer + puts game.board.to_s + tweet_board(game.board.to_s, twitter_tag) + else + print_board + end + else print_board end end + def self.tweet_board(tweet, message) + puts "@#{@player1.twitter} #{tweet} #{message} #{@player1.random_tag}" + Twitter.update("@#{@player1.twitter} #{tweet} #{message} #{@player1.random_tag}") + end + def self.create_player(player) puts "Enter username for #{player}" player_name = gets.chomp.capitalize @@ -70,10 +80,20 @@ def self.create_player(player) { name: player_name, twitter: player_twitter, password: player_password } end - # def self.next_move_request(current_player) #NAME_CHANGE from player_move - # puts "#{current_player}, what column do you want to play in?" - # game.next_move(gets.chomp.to_i) - # end + def self.twitter_tag + if game.board.full? # tie + message = "Draw game. Play again? #dbc_c4" + elsif game.board.check_four_consecutive? #winner + if game.board.empty_cells.even? + message = "I win! Good game. #dbc_c4" + else + message = "You win. #dbc_c4" + end + else + message = '#dbc_c4' + end + return message + end def self.congratulations(player) if @player1.class == TwitterPlayer @@ -92,7 +112,12 @@ def self.congratulations(player) end def self.print_board - puts game.board.to_s #needs vertical formatting + #puts game.board.to_s #needs vertical formatting + board_array = game.board.to_s.split('|') + #p board_array + board_array.each do |row| + puts "|#{row}|" unless row == "" + end #game.board.rows.each { |row| p row } end @@ -112,19 +137,6 @@ def self.print_board # board_info # end - def twitter_tag - if board.full? # tie - message = "Draw game. Play again? #dbc_c4" - elsif board.check_four_consecutive? #winner - if game.board.empty_cells.even? - message = "I win! Good game. #dbc_c4" - else - message = "You win. #dbc_c4" - end - else - message = '#dbc_c4' - end - return message - end + end \ No newline at end of file From 187157901439005523f32041f2ee1e3a8867c9a4 Mon Sep 17 00:00:00 2001 From: Brent Coughenour Date: Sat, 3 Nov 2012 20:37:05 -0700 Subject: [PATCH 27/47] some cleanup and fixed computer player bug --- lib/connect_four/board.rb | 32 +++------ lib/connect_four/computer_player.rb | 4 +- lib/connect_four/game.rb | 25 ------- lib/connect_four/twitter.rb | 108 ++++++++++++++-------------- lib/connect_four/twitter_player.rb | 43 +---------- lib/connect_four/ui.rb | 30 ++------ 6 files changed, 68 insertions(+), 174 deletions(-) diff --git a/lib/connect_four/board.rb b/lib/connect_four/board.rb index db78115..26c77ed 100644 --- a/lib/connect_four/board.rb +++ b/lib/connect_four/board.rb @@ -18,12 +18,14 @@ def empty_cells def place_piece(column, piece) position = (col_num * (row_num - 1)) + (column - 1) - until cells[position].empty? - if position < 0 - puts "That column is full. Choose again." - #game.play - else - position -= col_num + if cells[position].empty? == false + until cells[position].empty? + if position < 0 + puts "That column is full. Choose again." + UI.game.play + else + position -= col_num + end end end cells[position] = piece @@ -115,31 +117,13 @@ def diagonal_indexes_to_left 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 diff --git a/lib/connect_four/computer_player.rb b/lib/connect_four/computer_player.rb index 917d483..8eb7f01 100644 --- a/lib/connect_four/computer_player.rb +++ b/lib/connect_four/computer_player.rb @@ -4,12 +4,10 @@ class ComputerPlayer def initialize(params = {}) @name = params[:name] - # @twitter = params[:twitter] - # @password = params[:password] @piece = params[:piece] end def move - rand(7) + rand(7) + 1 end end \ No newline at end of file diff --git a/lib/connect_four/game.rb b/lib/connect_four/game.rb index aff94e8..765f744 100644 --- a/lib/connect_four/game.rb +++ b/lib/connect_four/game.rb @@ -31,31 +31,6 @@ def current_player @players.first end - # def play_twitter - # tweet = Tweet.new - # status = tweet.get_status - # TweetStream::Client.new.track(player1) do |status| - # board.cells = UI.board_from_twitter(status.text) - # UI.next_move_request(UI.player2.name) - # #test board - # if board.full? # tie - # message = "Draw game. Play again? #dbc_c4" - # elsif board.check_four_consecutive? #winner - # puts "Somebody won." - # #message = "I win! Good game. #dbc_c4" - # #message = "You win. #dbc_c4" - # else - # message = '#dbc_c4' - # end - # tweet_board(UI.board_to_twitter(UI.game.board.cells, message)) - # end - # end - - # def next_move(column) - # round = next_round - # board.place_piece(column, round) unless column > board.col_num - # end - def over? board.full? || board.check_four_consecutive? end diff --git a/lib/connect_four/twitter.rb b/lib/connect_four/twitter.rb index 7f021a4..4abc1ab 100644 --- a/lib/connect_four/twitter.rb +++ b/lib/connect_four/twitter.rb @@ -1,69 +1,69 @@ -require 'tweetstream' -#require 'player' -#require 'game' -#require_relative 'ui' +# require 'tweetstream' +# #require 'player' +# #require 'game' +# #require_relative 'ui' -class Tweet - attr_reader :random_tag +# class Tweet +# attr_reader :random_tag - def initialize - @random_tag = (('a'..'z').to_a + ('A'..'Z').to_a + (1..9).to_a).shuffle[0..2].join - @file_path = File.expand_path(File.join(File.dirname(__FILE__) , 'oauth'))#, '..', '..', 'db', name)) - consumer_key_path = File.join(@file_path, "consumer_key.txt") - consumer_secret_path = File.join(@file_path, "consumer_secret.txt") - oauth_token_path = File.join(@file_path, "access_token.txt") - oauth_token_secret_path = File.join(@file_path, "access_token_secret.txt") +# def initialize +# @random_tag = (('a'..'z').to_a + ('A'..'Z').to_a + (1..9).to_a).shuffle[0..2].join +# @file_path = File.expand_path(File.join(File.dirname(__FILE__) , 'oauth'))#, '..', '..', 'db', name)) +# consumer_key_path = File.join(@file_path, "consumer_key.txt") +# consumer_secret_path = File.join(@file_path, "consumer_secret.txt") +# oauth_token_path = File.join(@file_path, "access_token.txt") +# oauth_token_secret_path = File.join(@file_path, "access_token_secret.txt") - Twitter.configure do |config| - config.consumer_key = File.read(consumer_key_path) - config.consumer_secret = File.read(consumer_secret_path) - config.oauth_token = File.read(oauth_token_path) - config.oauth_token_secret = File.read(oauth_token_secret_path) - end +# Twitter.configure do |config| +# config.consumer_key = File.read(consumer_key_path) +# config.consumer_secret = File.read(consumer_secret_path) +# config.oauth_token = File.read(oauth_token_path) +# config.oauth_token_secret = File.read(oauth_token_secret_path) +# end - TweetStream.configure do |config| - config.consumer_key = File.read(consumer_key_path) - config.consumer_secret = File.read(consumer_secret_path) - config.oauth_token = File.read(oauth_token_path) - config.oauth_token_secret = File.read(oauth_token_secret_path) - config.auth_method = :oauth - end - #game_play - end +# TweetStream.configure do |config| +# config.consumer_key = File.read(consumer_key_path) +# config.consumer_secret = File.read(consumer_secret_path) +# config.oauth_token = File.read(oauth_token_path) +# config.oauth_token_secret = File.read(oauth_token_secret_path) +# config.auth_method = :oauth +# end +# #game_play +# end - def twitter_player - TweetStream::Client.new.track('#dbc_c4') do |status, client| - #puts status.text - text = status.text.gsub(/\#dbc_c4/, "").strip - if text == "blib?" - client.stop - #puts "@#{status.user[:screen_name]} Game on! #dbc_c4" - Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4 #{random_tag}") - return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X'}) - end - end - end +# def twitter_player +# TweetStream::Client.new.track('#dbc_c4') do |status, client| +# #puts status.text +# text = status.text.gsub(/\#dbc_c4/, "").strip +# if text == "blib?" +# client.stop +# #puts "@#{status.user[:screen_name]} Game on! #dbc_c4" +# Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4 #{random_tag}") +# return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X'}) +# end +# end +# end - # def twitter_move_relay - # TweetStream::Client.new.track("#{player1.twitter}") do |status, client| - # puts "#{status.text}" - # #look for incoming move - # #Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" - # client.stop - # return "#{status.text}" - # end - # end +# # def twitter_move_relay +# # TweetStream::Client.new.track("#{player1.twitter}") do |status, client| +# # puts "#{status.text}" +# # #look for incoming move +# # #Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" +# # client.stop +# # return "#{status.text}" +# # end +# # end - def tweet_board(tweet, message) - puts "#{player1.twitter} #{tweet} #{message} #{random_tag}" - #Twitter.update("#{player1.twitter} #{tweet} #{message}") - end +# def tweet_board(tweet, message) +# puts "#{player1.twitter} #{tweet} #{message} #{random_tag}" +# #Twitter.update("#{player1.twitter} #{tweet} #{message}") +# end -end +# end # tweet = Tweet.new # tweet.twitter_player diff --git a/lib/connect_four/twitter_player.rb b/lib/connect_four/twitter_player.rb index b56ae00..a3ee26f 100644 --- a/lib/connect_four/twitter_player.rb +++ b/lib/connect_four/twitter_player.rb @@ -38,7 +38,7 @@ def self.from_twitter TweetStream::Client.new.track('#dbc_c4') do |status, client| puts "id = #{status.user[:id]}, text = #{status.text}" text = status.text.gsub(/#.*/, "").strip - if text == "c" + if text == "Who wants to get demolished?" client.stop puts "@#{status.user[:screen_name]} Game on! #dbc_c4 #{@random_tag}" Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4 #{@random_tag}") @@ -49,19 +49,14 @@ def self.from_twitter def move board = to_a(board_as_string) - #p board column = parsed_board(board) - #puts column return column - #rand(7) end def board_as_string TweetStream::Client.new.follow("#{@id}") do |status, client| - #puts "#{status.text}" raw_board = status.text.gsub!(/#.*/, "") board = raw_board.match(/[^@\w*](.*)/).to_s.strip - #puts board client.stop return board end @@ -72,52 +67,16 @@ def parsed_board(board_as_array) board_as_array board_as_array.each_with_index do |cell, index| if cell != UI.game.board.cells[index] - # puts "not equal" - #puts "#{cell} at #{index}" result = (index % 7) + 1 end end return result end - - - - # difference = board_as_array - UI.game.board.cells - # puts difference - # difference.each_with_index do |cell, index| - # puts cell - # if cell != "" - # puts "different #{index}" - # result = (index + 1) % 7 - # end - # #return result - # end - # return result - #compare incoming board to old board to find the difference in column - - -#@player2 |.......|.......|.......|.......|.......|..O....| #dbc_c4 - def to_a(move_string) board_info = move_string.gsub(/\|/, "").split("") board_info.each { |field| field.gsub!(/\./, "") } board_info end - def tweet_board(tweet, message) - puts "#{player1.twitter} #{tweet} #{message} #{random_tag}" - #Twitter.update("#{player1.twitter} #{tweet} #{message}") - end - - # def twitter_player - # TweetStream::Client.new.track('#dbc_c4') do |status, client| - # puts "#{status.text}" - # puts "Game on! #dbc_c4" if "#{status.text}" == "Who wants to get demolished?" - # client.stop - # #Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" - # return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X'}) - # end - # end - end \ No newline at end of file diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index 003b823..d6bb1c7 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -9,17 +9,17 @@ def self.start case gets.chomp when "1" puts "Good choice!" - UI.create_1vs1_player + create_1vs1_player when "2" puts "How dare you! I will demolish you!" - UI.create_1vsPC_player + create_1vsPC_player when "3" puts "I will send out the message pigeon!" tweet = Tweet.new - UI.create_1vsTwitter_player + create_1vsTwitter_player else puts "Sorry, playing against Queen Elizabeth is not an option!" - UI.start + start end @game = Game.new(@player1, @player2) @game.play @@ -45,7 +45,6 @@ def self.create_1vsPC_player end def self.create_1vsTwitter_player - #@tweet = Tweet.new @player2 = ComputerPlayer.new({name: "MCP", piece: 'O'}) #@player2 = Player.new(create_player("Player 2").merge(:piece => 'O')) @player1 = TwitterPlayer.from_twitter @@ -112,31 +111,10 @@ def self.congratulations(player) end def self.print_board - #puts game.board.to_s #needs vertical formatting board_array = game.board.to_s.split('|') - #p board_array board_array.each do |row| puts "|#{row}|" unless row == "" end - #game.board.rows.each { |row| p row } end - # def self.board_to_twitter(board_info) - # board_format, row_format = "|", "" - # board_info.each { |field| field == "" ? (row_format += ".") : (row_format += field) } - # game.board.row_num.times do |i| - # start_i, end_i = (game.board.col_num*i), (game.board.col_num*(i+1)) - # board_format += row_format[start_i...end_i] + "|" - # end - # board_format - # end - - # def self.board_from_twitter(move_string) - # board_info = move_string.gsub(/\|/, "").split("") - # board_info.each { |field| field.gsub!(/\./, "") } - # board_info - # end - - - end \ No newline at end of file From 4141b622ca3fc3b9fc7194aa6db553e93852e3b6 Mon Sep 17 00:00:00 2001 From: Brent Coughenour Date: Sat, 3 Nov 2012 20:40:06 -0700 Subject: [PATCH 28/47] deleted twitter scratch file --- lib/connect_four/twitter.rb | 69 ------------------------------------- lib/connect_four/ui.rb | 1 - 2 files changed, 70 deletions(-) delete mode 100644 lib/connect_four/twitter.rb diff --git a/lib/connect_four/twitter.rb b/lib/connect_four/twitter.rb deleted file mode 100644 index 4abc1ab..0000000 --- a/lib/connect_four/twitter.rb +++ /dev/null @@ -1,69 +0,0 @@ -# require 'tweetstream' -# #require 'player' -# #require 'game' -# #require_relative 'ui' - -# class Tweet -# attr_reader :random_tag - -# def initialize -# @random_tag = (('a'..'z').to_a + ('A'..'Z').to_a + (1..9).to_a).shuffle[0..2].join -# @file_path = File.expand_path(File.join(File.dirname(__FILE__) , 'oauth'))#, '..', '..', 'db', name)) -# consumer_key_path = File.join(@file_path, "consumer_key.txt") -# consumer_secret_path = File.join(@file_path, "consumer_secret.txt") -# oauth_token_path = File.join(@file_path, "access_token.txt") -# oauth_token_secret_path = File.join(@file_path, "access_token_secret.txt") - -# Twitter.configure do |config| -# config.consumer_key = File.read(consumer_key_path) -# config.consumer_secret = File.read(consumer_secret_path) -# config.oauth_token = File.read(oauth_token_path) -# config.oauth_token_secret = File.read(oauth_token_secret_path) -# end - -# TweetStream.configure do |config| -# config.consumer_key = File.read(consumer_key_path) -# config.consumer_secret = File.read(consumer_secret_path) -# config.oauth_token = File.read(oauth_token_path) -# config.oauth_token_secret = File.read(oauth_token_secret_path) -# config.auth_method = :oauth -# end -# #game_play -# end - -# def twitter_player -# TweetStream::Client.new.track('#dbc_c4') do |status, client| -# #puts status.text -# text = status.text.gsub(/\#dbc_c4/, "").strip -# if text == "blib?" -# client.stop -# #puts "@#{status.user[:screen_name]} Game on! #dbc_c4" -# Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4 #{random_tag}") -# return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X'}) -# end -# end -# end - -# # def twitter_move_relay -# # TweetStream::Client.new.track("#{player1.twitter}") do |status, client| -# # puts "#{status.text}" -# # #look for incoming move -# # #Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4") if "#{status.text}" == "Who wants to get demolished?" -# # client.stop -# # return "#{status.text}" -# # end -# # end - -# def tweet_board(tweet, message) -# puts "#{player1.twitter} #{tweet} #{message} #{random_tag}" -# #Twitter.update("#{player1.twitter} #{tweet} #{message}") -# end - - - - - -# end - -# tweet = Tweet.new -# tweet.twitter_player diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index d6bb1c7..234a223 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -15,7 +15,6 @@ def self.start create_1vsPC_player when "3" puts "I will send out the message pigeon!" - tweet = Tweet.new create_1vsTwitter_player else puts "Sorry, playing against Queen Elizabeth is not an option!" From 975269a9dd97e4109d7db5cd61778a00be9700c5 Mon Sep 17 00:00:00 2001 From: Jonathan Pepin Date: Sat, 3 Nov 2012 23:33:44 -0700 Subject: [PATCH 29/47] make the 1-deep coin analyze work --- connect4/board.rb | 138 +++++++++++++++++++++++++++++++++++++++ connect4/defense.rb | 87 ++++++++++++++++++++++++ connect4/defense_spec.rb | 73 +++++++++++++++++++++ 3 files changed, 298 insertions(+) create mode 100644 connect4/board.rb create mode 100644 connect4/defense.rb create mode 100644 connect4/defense_spec.rb diff --git a/connect4/board.rb b/connect4/board.rb new file mode 100644 index 0000000..12c1d18 --- /dev/null +++ b/connect4/board.rb @@ -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 \ No newline at end of file diff --git a/connect4/defense.rb b/connect4/defense.rb new file mode 100644 index 0000000..8634adc --- /dev/null +++ b/connect4/defense.rb @@ -0,0 +1,87 @@ +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) + 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) + else + positions_ratings[position%7+1] ||= 0 + end + end + end + + def cell_rating_left2(position) + left_cell2 = board.cells[position-2] + if left_cell2 == "red" + positions_ratings[position%7+1] += 4 + cell_rating_left_3(position) unless [37, 30, 23, 16, 9, 2].include?(position) + end + end + + def cell_rating_left_3(position) + left_cell3 = board.cells[position-3] + if left_cell3 == "red" + positions_ratings[position%7+1] += 8 + end + end + + def cell_rating_right(position) + if position % 7 < 6 + right_cell = board.cells[position+1] + if right_cell == "red" + positions_ratings[position%7+1] = 2 + else + positions_ratings[position%7+1] ||= 0 + end + 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 + else + positions_ratings[position%7+1] ||= 0 + end + end + end + + def play + rate_all_cells + play_column = positions_ratings.max_by {|k, v| v}.first.to_i + board.place_piece(play_column, "black") + positions_ratings.clear + end + +end \ No newline at end of file diff --git a/connect4/defense_spec.rb b/connect4/defense_spec.rb new file mode 100644 index 0000000..dad3eba --- /dev/null +++ b/connect4/defense_spec.rb @@ -0,0 +1,73 @@ +require_relative 'defense.rb' + +describe Defense do + + describe '.new' do + it "should get a new board" do + Defense.new.board.should be_true + end + end + + describe "#avaiable_move" do + it "should give all available cells" do + deff = Defense.new + deff.board.place_piece(1, "red") + deff.available_moves.should eq [28, 36, 37, 38, 39, 40, 41] + end + end + + describe "#cell_rating" do + it "should rate 0 a lonely cell" do + deff = Defense.new + deff.cell_rating_left(36).should eq 0 + end + + it "should rate a cell with pieces on the left" do + deff = Defense.new + deff.board.place_piece(2, "red") + deff.cell_rating_left(37).should eq 2 + end + + it "should rate a cell with pieces on the right" do + deff = Defense.new + deff.board.place_piece(7, "red") + deff.cell_rating_right(40).should eq 2 + end + + it "should rate a cell with pieces on the bottom" do + deff = Defense.new + deff.board.place_piece(4, "red") + deff.cell_rating_bottom(31).should eq 2 + end + + it "should not rate a friend-neighbor cell" do + deff = Defense.new + deff.board.place_piece(4, "black") + deff.cell_rating_bottom(31).should eq 0 + end + + it "should rate all the available moves at once" do + deff = Defense.new + deff.board.place_piece(7, "red") + deff.rate_all_cells.should eq ({1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>2, 7=>0}) + end + end + + describe "#play" do + it "should play the highest rated move" do + deff = Defense.new + deff.board.place_piece(7, "red") + deff.play + deff.board.cells.should eq ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "black", "red"] + end + + it "should clear the grades hash" do + deff = Defense.new + deff.board.place_piece(7, "red") + deff.play + deff.positions_ratings.should be_empty + end + end + + +end \ No newline at end of file From 68895197334d8447707ecccbcfb31980710c1ef5 Mon Sep 17 00:00:00 2001 From: Brent Coughenour Date: Sun, 4 Nov 2012 10:52:01 -0800 Subject: [PATCH 30/47] removing old twitter file dependency from executable --- lib/connect_four.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/connect_four.rb b/lib/connect_four.rb index e5f7232..0f158dc 100644 --- a/lib/connect_four.rb +++ b/lib/connect_four.rb @@ -3,6 +3,5 @@ require_relative 'connect_four/game' require_relative 'connect_four/player' require_relative 'connect_four/ui' -require_relative 'connect_four/twitter' require_relative 'connect_four/computer_player' require_relative 'connect_four/twitter_player' \ No newline at end of file From acee5b25b0304acc8e83c46c66dd3989c16c0de2 Mon Sep 17 00:00:00 2001 From: Brent Coughenour Date: Sun, 4 Nov 2012 12:41:41 -0800 Subject: [PATCH 31/47] integration of defense ai into computer player --- connect4/defense.rb | 19 +++--- lib/connect_four/board.rb | 5 +- lib/connect_four/computer_player.rb | 91 ++++++++++++++++++++++++++++- 3 files changed, 102 insertions(+), 13 deletions(-) diff --git a/connect4/defense.rb b/connect4/defense.rb index 8634adc..35f2ad5 100644 --- a/connect4/defense.rb +++ b/connect4/defense.rb @@ -4,7 +4,7 @@ class Defense attr_reader :board, :positions_ratings def initialize - @board = Board.new + #@board = Board.new @positions_ratings = { 1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>0, 7=>0} end @@ -12,7 +12,7 @@ def available_moves @available_moves = [] first_row = [35, 36, 37, 38, 39, 40, 41] first_row.each do |position| - until board.cells[position].empty? + until UI.game.board.cells[position].empty? position -= board.col_num end @available_moves << position @@ -29,7 +29,7 @@ def rate_all_cells end def cell_rating_left(position) - left_cell = board.cells[position-1] + left_cell = UI.game.board.cells[position-1] unless [35, 28, 21, 14, 7, 0].include?(position) if left_cell == "red" positions_ratings[(position%7)+1] += 2 @@ -41,7 +41,7 @@ def cell_rating_left(position) end def cell_rating_left2(position) - left_cell2 = board.cells[position-2] + left_cell2 = UI.game.board.cells[position-2] if left_cell2 == "red" positions_ratings[position%7+1] += 4 cell_rating_left_3(position) unless [37, 30, 23, 16, 9, 2].include?(position) @@ -49,7 +49,7 @@ def cell_rating_left2(position) end def cell_rating_left_3(position) - left_cell3 = board.cells[position-3] + left_cell3 = UI.game.board.cells[position-3] if left_cell3 == "red" positions_ratings[position%7+1] += 8 end @@ -57,7 +57,7 @@ def cell_rating_left_3(position) def cell_rating_right(position) if position % 7 < 6 - right_cell = board.cells[position+1] + right_cell = UI.game.board.cells[position+1] if right_cell == "red" positions_ratings[position%7+1] = 2 else @@ -68,7 +68,7 @@ def cell_rating_right(position) def cell_rating_bottom(position) unless [35, 36, 37, 38, 39, 40, 41].include?(position) - bottom_cell = board.cells[position+7] + bottom_cell = UI.game.board.cells[position+7] if bottom_cell == "red" positions_ratings[position%7+1] = 2 else @@ -77,11 +77,12 @@ def cell_rating_bottom(position) end end - def play + def move rate_all_cells play_column = positions_ratings.max_by {|k, v| v}.first.to_i - board.place_piece(play_column, "black") + #board.place_piece(play_column, "black") positions_ratings.clear + return play_column end end \ No newline at end of file diff --git a/lib/connect_four/board.rb b/lib/connect_four/board.rb index 26c77ed..e150044 100644 --- a/lib/connect_four/board.rb +++ b/lib/connect_four/board.rb @@ -18,16 +18,17 @@ def empty_cells def place_piece(column, piece) position = (col_num * (row_num - 1)) + (column - 1) - if cells[position].empty? == false + #if cells[position].empty? == false until cells[position].empty? if position < 0 puts "That column is full. Choose again." UI.game.play + break else position -= col_num end end - end + #end cells[position] = piece end diff --git a/lib/connect_four/computer_player.rb b/lib/connect_four/computer_player.rb index 8eb7f01..5ae1cb7 100644 --- a/lib/connect_four/computer_player.rb +++ b/lib/connect_four/computer_player.rb @@ -1,13 +1,100 @@ class ComputerPlayer - attr_reader :name, :twitter, :password, :id, :piece + attr_reader :name, :twitter, :password, :id, :piece, :positions_ratings, + :opponent_piece def initialize(params = {}) @name = params[:name] @piece = params[:piece] + @positions_ratings = { 1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>0, 7=>0} + @piece == "X" ? @opponent_piece = "O" : @opponent_piece = "X" + end + + # def move + # rand(7) + 1 + # end + + def available_moves + @available_moves = [] + first_row = [35, 36, 37, 38, 39, 40, 41] + first_row.each do |position| + until UI.game.board.cells[position].empty? || position < 0 + position -= UI.game.board.col_num + end + @available_moves << position unless position < 0 + end + return @available_moves + end + + def rate_all_cells + available_moves.each do |move| + #puts move + cell_rating_left(move) + cell_rating_right(move) + end + positions_ratings + end + + def cell_rating_left(position) + left_cell = UI.game.board.cells[position-1] + unless [35, 28, 21, 14, 7, 0].include?(position) + if left_cell == opponent_piece + + puts positions_ratings[(position%7)+1].inspect + #positions_ratings[(position%7)+1] += 2 + + # ^^^^^^^^for some reason this is nil ^^^^^^^^^^^ + + # cell_rating_left2(position) unless [36, 29, 22, 15, 8, 1].include?(position) + else + positions_ratings[position%7+1] ||= 0 + end + end + end + + def cell_rating_left2(position) + left_cell2 = UI.game.board.cells[position-2] + if left_cell2 == opponent_piece + positions_ratings[position%7+1] += 4 + cell_rating_left_3(position) unless [37, 30, 23, 16, 9, 2].include?(position) + end + end + + def cell_rating_left_3(position) + left_cell3 = UI.game.board.cells[position-3] + if left_cell3 == opponent_piece + positions_ratings[position%7+1] += 8 + end + end + + def cell_rating_right(position) + if position % 7 < 6 + right_cell = UI.game.board.cells[position+1] + if right_cell == opponent_piece + positions_ratings[position%7+1] = 2 + else + positions_ratings[position%7+1] ||= 0 + end + end + end + + def cell_rating_bottom(position) + unless [35, 36, 37, 38, 39, 40, 41].include?(position) + bottom_cell = UI.game.board.cells[position+7] + if bottom_cell == opponent_piece + positions_ratings[position%7+1] = 2 + else + positions_ratings[position%7+1] ||= 0 + end + end end def move - rand(7) + 1 + rate_all_cells + play_column = positions_ratings.max_by {|k, v| v}.first.to_i + #board.place_piece(play_column, "black") + positions_ratings.clear + return play_column end + end \ No newline at end of file From 67485b5e46ab610923f7dfe823eb5f25f742e020 Mon Sep 17 00:00:00 2001 From: Brent Coughenour Date: Sun, 4 Nov 2012 14:47:43 -0800 Subject: [PATCH 32/47] fixed computer_player nil bug --- lib/connect_four/computer_player.rb | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/lib/connect_four/computer_player.rb b/lib/connect_four/computer_player.rb index 5ae1cb7..5aaeac8 100644 --- a/lib/connect_four/computer_player.rb +++ b/lib/connect_four/computer_player.rb @@ -19,7 +19,7 @@ def available_moves first_row = [35, 36, 37, 38, 39, 40, 41] first_row.each do |position| until UI.game.board.cells[position].empty? || position < 0 - position -= UI.game.board.col_num + position -= 7 end @available_moves << position unless position < 0 end @@ -28,7 +28,6 @@ def available_moves def rate_all_cells available_moves.each do |move| - #puts move cell_rating_left(move) cell_rating_right(move) end @@ -36,16 +35,11 @@ def rate_all_cells end def cell_rating_left(position) - left_cell = UI.game.board.cells[position-1] + left_cell = UI.game.board.cells[position - 1] unless [35, 28, 21, 14, 7, 0].include?(position) if left_cell == opponent_piece - - puts positions_ratings[(position%7)+1].inspect - #positions_ratings[(position%7)+1] += 2 - - # ^^^^^^^^for some reason this is nil ^^^^^^^^^^^ - - # cell_rating_left2(position) unless [36, 29, 22, 15, 8, 1].include?(position) + positions_ratings[(position%7)+1] += 2 + cell_rating_left2(position) unless [36, 29, 22, 15, 8, 1].include?(position) else positions_ratings[position%7+1] ||= 0 end @@ -92,8 +86,7 @@ def cell_rating_bottom(position) 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.clear + positions_ratings = { 1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>0, 7=>0} return play_column end From 0ae66014d464deb056f7c57761b825da2d249bfc Mon Sep 17 00:00:00 2001 From: Daniela Grossmann Date: Sun, 4 Nov 2012 15:56:26 -0800 Subject: [PATCH 33/47] Games to DB --- bin/connect_four | 2 +- lib/connect_four/board.rb | 16 ++++++++-------- lib/connect_four/game.rb | 11 ++++++++++- lib/connect_four/player.rb | 6 +++++- lib/connect_four/ui.rb | 9 ++++++--- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/bin/connect_four b/bin/connect_four index c244e70..e1399af 100755 --- a/bin/connect_four +++ b/bin/connect_four @@ -2,4 +2,4 @@ require_relative '../lib/connect_four' -UI.start \ No newline at end of file +UI.start diff --git a/lib/connect_four/board.rb b/lib/connect_four/board.rb index e150044..c10797f 100644 --- a/lib/connect_four/board.rb +++ b/lib/connect_four/board.rb @@ -19,15 +19,15 @@ def empty_cells def place_piece(column, piece) position = (col_num * (row_num - 1)) + (column - 1) #if cells[position].empty? == false - until cells[position].empty? - if position < 0 - puts "That column is full. Choose again." - UI.game.play - break - else - position -= col_num - end + until cells[position].empty? + if position < 0 + puts "That column is full. Choose again." + UI.game.play + break + else + position -= col_num end + end #end cells[position] = piece end diff --git a/lib/connect_four/game.rb b/lib/connect_four/game.rb index 765f744..fd31b61 100644 --- a/lib/connect_four/game.rb +++ b/lib/connect_four/game.rb @@ -1,5 +1,5 @@ class Game - include Database + # include Database attr_reader :player1, :player2, :winner, :board def initialize(player1, player2) @@ -19,8 +19,11 @@ def play winner = nil else winner = current_player + winner_id = DB.handler("SELECT id FROM players where name = #{current_player};").flatten + save(winner_id) end UI.congratulations(winner) + UI.start("What do you want to do now?") end def toggle_player @@ -34,6 +37,12 @@ def current_player def over? board.full? || board.check_four_consecutive? end + + def save(winner_id) + values = [@player1.id, @player2.id, winner_id] + p values + DB.handler("INSERT INTO games (player1, player2, winner) VALUES (?, ?, ?);", values) + end def self.wins_for(player_id) db.execute("SELECT COUNT(*) FROM games WHERE winner = ?", player_id).first diff --git a/lib/connect_four/player.rb b/lib/connect_four/player.rb index 3d09e1b..4dd2832 100644 --- a/lib/connect_four/player.rb +++ b/lib/connect_four/player.rb @@ -1,7 +1,7 @@ class Player # include Database - attr_reader :name, :twitter, :password, :id, :piece + attr_reader :name, :twitter, :password, :piece def initialize(params = {}) @name = params[:name] @@ -16,6 +16,10 @@ def save DB.handler("INSERT INTO players (name, twitter, password) VALUES (?, ?, ?);", values) end end + + def id + @id = DB.handler("SELECT id FROM players WHERE twitter = #{@twitter}") + end def move puts "#{name}, what column do you want to play in?" diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index 234a223..cd10ca0 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -2,10 +2,10 @@ class UI attr_reader :game, :tweet - def self.start + def self.start(start_message = "Welcome to Connect Four! Pick a number") DB.create - puts "How do you want to play today? Pick a number" - puts "1 - (1 vs 1), 2 - (1 vs PC), 3 - (1 vs Twitter)" + puts start_message + puts "0 - (quit), 1 - (1 vs 1), 2 - (1 vs PC), 3 - (1 vs Twitter)" case gets.chomp when "1" puts "Good choice!" @@ -16,6 +16,9 @@ def self.start when "3" puts "I will send out the message pigeon!" create_1vsTwitter_player + when "0" + puts "Bye!" + return else puts "Sorry, playing against Queen Elizabeth is not an option!" start From d773ee70794de119924917b01cd5f97046d9904a Mon Sep 17 00:00:00 2001 From: Daniela Grossmann Date: Sun, 4 Nov 2012 16:52:35 -0800 Subject: [PATCH 34/47] Player statistic: changes in game, player and ui --- lib/connect_four/game.rb | 16 ++-------------- lib/connect_four/player.rb | 14 +++++++++++++- lib/connect_four/ui.rb | 19 +++++++++++++++---- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/lib/connect_four/game.rb b/lib/connect_four/game.rb index fd31b61..c99d57b 100644 --- a/lib/connect_four/game.rb +++ b/lib/connect_four/game.rb @@ -17,10 +17,10 @@ def play end if board.full? winner = nil + save(0) else winner = current_player - winner_id = DB.handler("SELECT id FROM players where name = #{current_player};").flatten - save(winner_id) + save(winner.id) end UI.congratulations(winner) UI.start("What do you want to do now?") @@ -40,20 +40,8 @@ def over? def save(winner_id) values = [@player1.id, @player2.id, winner_id] - p values DB.handler("INSERT INTO games (player1, player2, winner) VALUES (?, ?, ?);", values) end - def self.wins_for(player_id) - db.execute("SELECT COUNT(*) FROM games WHERE winner = ?", player_id).first - end - - def self.losses_for(player_id) - db.execute("SELECT COUNT(*) FROM games WHERE player1 = ? OR player2 = ? AND winner != ? AND winner IS NOT NULL", Array.new(3, player_id)).first - end - - def self.ties_for(player_id) - db.execute("SELECT COUNT(*) FROM games WHERE player1 = ? OR player2 = ? AND winner IS NULL", Array.new(2, player_id)).first - end end diff --git a/lib/connect_four/player.rb b/lib/connect_four/player.rb index 4dd2832..930aa2a 100644 --- a/lib/connect_four/player.rb +++ b/lib/connect_four/player.rb @@ -18,7 +18,7 @@ def save end def id - @id = DB.handler("SELECT id FROM players WHERE twitter = #{@twitter}") + @id = DB.handler("SELECT id FROM players WHERE twitter = ?;", twitter).flatten.join.to_i end def move @@ -29,4 +29,16 @@ def move def to_s name end + + def wins + DB.handler("SELECT COUNT(*) FROM games WHERE winner = ?", id).flatten.join + end + + def losses + DB.handler("SELECT COUNT(*) FROM games WHERE (player1 = ? OR player2 = ?) AND winner != ? AND winner != 0", Array.new(3, id)).flatten.join + end + + def ties + DB.handler("SELECT COUNT(*) FROM games WHERE (player1 = ? OR player2 = ?) AND winner = 0", Array.new(2, id)).flatten.join + end end \ No newline at end of file diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index cd10ca0..ca8afc3 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -5,7 +5,7 @@ class UI def self.start(start_message = "Welcome to Connect Four! Pick a number") DB.create puts start_message - puts "0 - (quit), 1 - (1 vs 1), 2 - (1 vs PC), 3 - (1 vs Twitter)" + puts "1 - (1 vs 1), 2 - (1 vs PC), 3 - (1 vs Twitter), 4 - (Statistic), q - (quit), " case gets.chomp when "1" puts "Good choice!" @@ -16,9 +16,11 @@ def self.start(start_message = "Welcome to Connect Four! Pick a number") when "3" puts "I will send out the message pigeon!" create_1vsTwitter_player - when "0" + when "4" + show_user_statistic + when "q" puts "Bye!" - return + return else puts "Sorry, playing against Queen Elizabeth is not an option!" start @@ -50,7 +52,16 @@ def self.create_1vsTwitter_player @player2 = ComputerPlayer.new({name: "MCP", piece: 'O'}) #@player2 = Player.new(create_player("Player 2").merge(:piece => 'O')) @player1 = TwitterPlayer.from_twitter - + end + + def self.show_user_statistic + @player = Player.new(create_player("Player")) + puts "STATISTIC for #{@player}" + puts "=========================" + puts "WINS: #{@player.wins}" + puts "LOSSES: #{@player.losses}" + puts "TIES: #{@player.ties}" + start("What do you want to do now?") end def self.board_display From fcc7d84d6b70b93079f484a43bb499f6cc6e9f47 Mon Sep 17 00:00:00 2001 From: Daniela Grossmann Date: Sun, 4 Nov 2012 18:01:31 -0800 Subject: [PATCH 35/47] Bugs fixed: exit, input col>num, empty input,... --- lib/connect_four/board.rb | 15 ++++++++++----- lib/connect_four/database.rb | 21 +------------------- lib/connect_four/game.rb | 1 - lib/connect_four/player.rb | 2 -- lib/connect_four/twitter_player.rb | 2 +- lib/connect_four/ui.rb | 31 ++++++++++++++++++------------ 6 files changed, 31 insertions(+), 41 deletions(-) diff --git a/lib/connect_four/board.rb b/lib/connect_four/board.rb index c10797f..7490321 100644 --- a/lib/connect_four/board.rb +++ b/lib/connect_four/board.rb @@ -17,8 +17,15 @@ def empty_cells end def place_piece(column, piece) - position = (col_num * (row_num - 1)) + (column - 1) - #if cells[position].empty? == false + if column > col_num + puts "That column number is too big. Choose again." + UI.game.play + elsif column == "".to_i + puts "This is an invalid input." + UI.game.play + else + position = (col_num * (row_num - 1)) + (column - 1) + end until cells[position].empty? if position < 0 puts "That column is full. Choose again." @@ -28,7 +35,6 @@ def place_piece(column, piece) position -= col_num end end - #end cells[position] = piece end @@ -88,6 +94,7 @@ def column_index_number(index) index % col_num end +# DRY? I was not able to make these two dry, wierd results! def diagonal_indexes_to_right start_indexes = get_start_indexes_to_right(4) leap = col_num + 1 @@ -118,12 +125,10 @@ def diagonal_indexes_to_left end end - def diagonals (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? { |lines| four_consecutive?(lines) } end diff --git a/lib/connect_four/database.rb b/lib/connect_four/database.rb index 1ffae5d..6b43674 100644 --- a/lib/connect_four/database.rb +++ b/lib/connect_four/database.rb @@ -19,23 +19,4 @@ def self.handler(string, *values) result end -end - -module Database - # def db - # @db ||= find_or_create - # end - # - # def find_or_create - # name = "connectfour.db" - # db_path = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'db')) - # p db_path - # schema_path = File.join(db_path, 'schema.sql') - # database_path = File.join(db_path, name) - # - # system("sqlite3 #{db_path} < #{schema_path}") - # SQLite3::Database.new(database_path) - # end -end - - +end \ No newline at end of file diff --git a/lib/connect_four/game.rb b/lib/connect_four/game.rb index c99d57b..25c7bd0 100644 --- a/lib/connect_four/game.rb +++ b/lib/connect_four/game.rb @@ -1,5 +1,4 @@ class Game - # include Database attr_reader :player1, :player2, :winner, :board def initialize(player1, player2) diff --git a/lib/connect_four/player.rb b/lib/connect_four/player.rb index 930aa2a..b537340 100644 --- a/lib/connect_four/player.rb +++ b/lib/connect_four/player.rb @@ -1,6 +1,4 @@ class Player - # include Database - attr_reader :name, :twitter, :password, :piece def initialize(params = {}) diff --git a/lib/connect_four/twitter_player.rb b/lib/connect_four/twitter_player.rb index a3ee26f..62c4762 100644 --- a/lib/connect_four/twitter_player.rb +++ b/lib/connect_four/twitter_player.rb @@ -1,7 +1,7 @@ require 'tweetstream' class TwitterPlayer -attr_reader :name, :twitter, :piece, :random_tag, :id + attr_reader :name, :twitter, :piece, :random_tag, :id def initialize(options={}, tag) @name = options[:name] diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index ca8afc3..9ec83e7 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -1,12 +1,25 @@ class UI - attr_reader :game, :tweet def self.start(start_message = "Welcome to Connect Four! Pick a number") DB.create puts start_message - puts "1 - (1 vs 1), 2 - (1 vs PC), 3 - (1 vs Twitter), 4 - (Statistic), q - (quit), " - case gets.chomp + puts "1 - (1 vs 1), 2 - (1 vs PC), 3 - (1 vs Twitter), 4 - (Statistic), r - (repeat game), q - (quit), " + choice = choices(gets.chomp) + if choice == "q" + exit + else + @game = Game.new(@player1, @player2) + @game.play + end + end + + def self.game + @game + end + + def self.choices(user_input) + case user_input when "1" puts "Good choice!" create_1vs1_player @@ -18,26 +31,20 @@ def self.start(start_message = "Welcome to Connect Four! Pick a number") create_1vsTwitter_player when "4" show_user_statistic + when "r" + return "r" when "q" puts "Bye!" - return + return "q" else puts "Sorry, playing against Queen Elizabeth is not an option!" start end - @game = Game.new(@player1, @player2) - @game.play - end - - def self.game - @game end def self.create_1vs1_player - puts "Who wants to be first?" @player1 = Player.new(create_player("Player 1").merge(:piece => 'X')) @player1.save - puts "Last but not least:" @player2 = Player.new(create_player("Player 2").merge(:piece => 'O')) @player2.save end From 3a152f7f2112f2da3b6d74361440cce7447ff480 Mon Sep 17 00:00:00 2001 From: Daniela Grossmann Date: Sun, 4 Nov 2012 21:30:29 -0800 Subject: [PATCH 36/47] spec updates --- .DS_Store | Bin 0 -> 6148 bytes db/test.rb | Bin 0 -> 20480 bytes lib/connect_four/game.rb | 2 +- lib/connect_four/ui.rb | 20 ++++-- spec/board_spec.rb | 123 +++++++++++++++++++++++++++++++----- spec/database_spec.rb | 20 +++--- spec/game_spec.rb | 24 +++++-- spec/player_spec.rb | 1 + spec/twitter_player_spec.rb | 18 ++++++ 9 files changed, 173 insertions(+), 35 deletions(-) create mode 100644 .DS_Store create mode 100644 db/test.rb create mode 100644 spec/twitter_player_spec.rb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..ec50ef1d2340dfeacddfca6381248bb57f64cd40 GIT binary patch literal 6148 zcmeHKOHRW;47E!aMQkFmfaNS%A@v4PRgl=i0SYZywGgRECD`SF9Dv)v^G8L^N9+)) z?8<(ViS6mUiQ<@uc(!|<5@kd*feMaJVR}UFi*{t>Gh-lYj*2Q;P)#MRJKizz9~qFd z%jk(#_!T(4pYto|m9BA?Z&crH%GGjHF4dRl19=J2NJ#ez(uoBtAc8duYNtceM2^6*+f?hQ~ zl~y@OR#n$12%tIm~hyb*<-JMpY556sQb=Pdm2DPMJgdPUp;WhV&I1 zQfuJ$I=1X}#%%XkwVY494?Hq{HMe^%wFlkq@3rO)waXw3d9aaF*nB8I*w67)+R|3Df z8vB|a=1n~c)qeJ#esrV_6>oesk-N@O%&!(a9P_Qy+y?npWhInLb1ZvS3DKBdE^D6_ zYRuI&$7zmti@vs;)=SHwo2w?4fe>AOHafKmY;|fB*y_009U 'brant', :twitter => 'brant', :password => 'master'}) } + let(:player1) { Player.new({:name => 'brent', :twitter => 'brent', :password => 'master'}) } let(:player2) { Player.new({:name => 'jo', :twitter => 'jauny', :password => 'blah'}) } let(:game) { Game.new(player1, player2) } - - describe '.new' do + + context '#initialize' do it 'has 2 players' do game.player1.should be_true game.player2.should be_true @@ -21,6 +21,22 @@ end end - + context "#toggle_player" do + it "should change the order of the players" do + first_player = game.players.first + second_player = game.players.last + game.toggle_player + game.players.first.should eq(second_player) + end + end + + context "#current_player" do + it "should return the current player" do + first_player = game.players.first + second_player = game.players.last + game.toggle_player + game.current_player.should eq(second_player) + end + end end \ No newline at end of file diff --git a/spec/player_spec.rb b/spec/player_spec.rb index 282c13e..1bcce32 100644 --- a/spec/player_spec.rb +++ b/spec/player_spec.rb @@ -16,4 +16,5 @@ player.password.should eq "master" end end + end \ No newline at end of file diff --git a/spec/twitter_player_spec.rb b/spec/twitter_player_spec.rb new file mode 100644 index 0000000..58ad325 --- /dev/null +++ b/spec/twitter_player_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +describe TwitterPlayer do + let(:twitter_player) {TwitterPlayer.from_twitter} + + describe "#initialize" do + it "creates a random tag" do + # twitter_player.random_tag.should be_an_instance_of String + end + end + + describe "#twitter_player" do + it "tracks a keyword" do + + end + end + +end \ No newline at end of file From bfb86526355e503f80c701dd24bda88bd74a25c6 Mon Sep 17 00:00:00 2001 From: Jonathan Pepin Date: Sun, 4 Nov 2012 21:55:15 -0800 Subject: [PATCH 37/47] made the AI play with there is the more consecutive opponent's coins --- connect4/defense.rb | 73 ++++++++++++++++++++++++++++------------ connect4/defense_spec.rb | 63 +++++++++++++++++++++++++++++----- 2 files changed, 106 insertions(+), 30 deletions(-) diff --git a/connect4/defense.rb b/connect4/defense.rb index 35f2ad5..6370fa7 100644 --- a/connect4/defense.rb +++ b/connect4/defense.rb @@ -4,7 +4,7 @@ class Defense attr_reader :board, :positions_ratings def initialize - #@board = Board.new + @board = Board.new @positions_ratings = { 1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>0, 7=>0} end @@ -12,7 +12,7 @@ def available_moves @available_moves = [] first_row = [35, 36, 37, 38, 39, 40, 41] first_row.each do |position| - until UI.game.board.cells[position].empty? + until board.cells[position].empty? position -= board.col_num end @available_moves << position @@ -24,65 +24,94 @@ 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 = UI.game.board.cells[position-1] + 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) - else - positions_ratings[position%7+1] ||= 0 + 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 = UI.game.board.cells[position-2] + left_cell2 = board.cells[position-2] if left_cell2 == "red" positions_ratings[position%7+1] += 4 - cell_rating_left_3(position) unless [37, 30, 23, 16, 9, 2].include?(position) + cell_rating_left3(position) unless [37, 30, 23, 16, 9, 2].include?(position) end end - def cell_rating_left_3(position) - left_cell3 = UI.game.board.cells[position-3] + 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) - if position % 7 < 6 - right_cell = UI.game.board.cells[position+1] + 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 - else - positions_ratings[position%7+1] ||= 0 + 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 = UI.game.board.cells[position+7] + bottom_cell = board.cells[position+7] if bottom_cell == "red" - positions_ratings[position%7+1] = 2 - else - positions_ratings[position%7+1] ||= 0 + 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.clear - return play_column + board.place_piece(play_column, "black") + positions_ratings.each {|k,v| positions_ratings[k] = 0} end end \ No newline at end of file diff --git a/connect4/defense_spec.rb b/connect4/defense_spec.rb index dad3eba..ea964b7 100644 --- a/connect4/defense_spec.rb +++ b/connect4/defense_spec.rb @@ -22,24 +22,69 @@ deff.cell_rating_left(36).should eq 0 end - it "should rate a cell with pieces on the left" do + it "should rate 2 a cell with one pieces on the left" do deff = Defense.new deff.board.place_piece(2, "red") deff.cell_rating_left(37).should eq 2 end - it "should rate a cell with pieces on the right" do + it "should rate 6 a cell with 2 pieces on the left" do + deff = Defense.new + deff.board.place_piece(2, "red") + deff.board.place_piece(1, "red") + deff.cell_rating_left(37).should eq 6 + end + + it "should rate 14 a cell with 2 pieces on the left" do + deff = Defense.new + deff.board.place_piece(2, "red") + deff.board.place_piece(3, "red") + deff.board.place_piece(1, "red") + deff.cell_rating_left(38).should eq 14 + end + + it "should rate 2 a cell with one pieces on the right" do deff = Defense.new deff.board.place_piece(7, "red") deff.cell_rating_right(40).should eq 2 end - it "should rate a cell with pieces on the bottom" do + it "should rate 6 a cell with 2 pieces on the right" do + deff = Defense.new + deff.board.place_piece(7, "red") + deff.board.place_piece(6, "red") + deff.cell_rating_right(39).should eq 6 + end + + it "should rate 14 a cell with 2 pieces on the right" do + deff = Defense.new + deff.board.place_piece(7, "red") + deff.board.place_piece(6, "red") + deff.board.place_piece(5, "red") + deff.cell_rating_right(38).should eq 14 + end + + it "should rate 2 a cell with 1 pieces on the bottom" do deff = Defense.new deff.board.place_piece(4, "red") deff.cell_rating_bottom(31).should eq 2 end + it "should rate 6 a cell with 2 pieces on the bottom" do + deff = Defense.new + deff.board.place_piece(4, "red") + deff.board.place_piece(4, "red") + deff.cell_rating_bottom(24).should eq 6 + end + + it "should rate 14 a cell with 3 pieces on the bottom" do + deff = Defense.new + deff.board.place_piece(4, "red") + deff.board.place_piece(4, "red") + deff.board.place_piece(4, "red") + deff.cell_rating_bottom(17).should eq 14 + end + it "should not rate a friend-neighbor cell" do deff = Defense.new deff.board.place_piece(4, "black") @@ -49,7 +94,7 @@ it "should rate all the available moves at once" do deff = Defense.new deff.board.place_piece(7, "red") - deff.rate_all_cells.should eq ({1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>2, 7=>0}) + deff.rate_all_cells.should eq ({1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>2, 7=>2}) end end @@ -57,15 +102,17 @@ it "should play the highest rated move" do deff = Defense.new deff.board.place_piece(7, "red") - deff.play - deff.board.cells.should eq ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "black", "red"] + deff.board.place_piece(7, "red") + deff.board.place_piece(7, "red") + deff.move + deff.board.cells.should eq ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "black", "", "", "", "", "", "", "red", "", "", "", "", "", "", "red", "", "", "", "", "", "", "red"] end it "should clear the grades hash" do deff = Defense.new deff.board.place_piece(7, "red") - deff.play - deff.positions_ratings.should be_empty + deff.move + expect { deff.positions_ratings.all? {|k,v| positions_ratings[k] == 0} }.to be_true end end From a3f7444a269b60c1621dd0f8161acce70bd19a36 Mon Sep 17 00:00:00 2001 From: Jonathan Pepin Date: Sun, 4 Nov 2012 21:56:48 -0800 Subject: [PATCH 38/47] computer and ui --- .DS_Store | Bin 0 -> 6148 bytes lib/connect_four/computer_player.rb | 5 +++-- lib/connect_four/ui.rb | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..66bb95ac9d070d41dbd28cda0a4b0ebc8d3f03ff GIT binary patch literal 6148 zcmeHKJ8Hu~5S@ut2-3K8xmO6}0E>`M;0qWM<0eR8lGLhlE+5TrK7wp;7}d-yN&|D41f6`%tDN&#*9L*L<*vbQc?&U$TuAK_MWg_~jR6a;U_KySy` gSUcW%QPdS%<9 Date: Sun, 4 Nov 2012 22:41:48 -0800 Subject: [PATCH 39/47] refactoring classes: db, player, ui --- lib/connect_four/database.rb | 5 ++ lib/connect_four/player.rb | 3 +- lib/connect_four/ui.rb | 104 +++++++++++++++++++++-------------- spec/tweet_spec.rb | 18 ------ 4 files changed, 70 insertions(+), 60 deletions(-) delete mode 100644 spec/tweet_spec.rb diff --git a/lib/connect_four/database.rb b/lib/connect_four/database.rb index 6b43674..cd78d84 100644 --- a/lib/connect_four/database.rb +++ b/lib/connect_four/database.rb @@ -18,5 +18,10 @@ def self.handler(string, *values) db.close result end + + def self.check_twitter(player_name, twitter) + authorisation = handler("SELECT name, twitter FROM players WHERE twitter = ?;", twitter).flatten + authorisation[0] == player_name || authorisation.empty? + end end \ No newline at end of file diff --git a/lib/connect_four/player.rb b/lib/connect_four/player.rb index b537340..ebb13a8 100644 --- a/lib/connect_four/player.rb +++ b/lib/connect_four/player.rb @@ -20,7 +20,8 @@ def id end def move - puts "#{name}, what column do you want to play in?" + puts "-" * 50 + print "#{name}, what column do you want to play in? " gets.chomp.to_i end diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index 8c0aa07..e6301f6 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -1,17 +1,19 @@ +require 'io/console' class UI attr_reader :game, :tweet def self.start(start_message = "Welcome to Connect Four! Pick a number") DB.create + puts "=" * 100 puts start_message - puts "1 - (1 vs 1), 2 - (1 vs PC), 3 - (1 vs Twitter), 4 - (Statistic), r - (repeat game), q - (quit), " - choice = choices(gets.chomp) - if choice == "q" - exit - else - @game = Game.new(@player1, @player2) - @game.play - end + puts "1 - (1 vs 1), 2 - (1 vs PC), 3 - (1 vs Twitter), 4 - (PC vs Twitter), " + puts "s - (Statistic), r - (repeat game), q - (quit)" + puts "=" * 100 + choices(gets.chomp) + puts "=" * 50 + puts "THE GAME IS ON:" + @game = Game.new(@player1, @player2) + @game.play end def self.game @@ -30,55 +32,58 @@ def self.choices(user_input) puts "I will send out the message pigeon!" create_1vsTwitter_player when "4" + puts "Game on!" + create_PCvsTwitter_player + when "s" show_user_statistic when "r" - return "r" + return when "q" - puts "Bye!" - return "q" + puts "=" * 100 + puts "Auf Wiedersehen!" + exit else - puts "Sorry, playing against Queen Elizabeth is not an option!" - start + start("Sorry, playing against Queen Elizabeth is not an option! Try again.") end end def self.create_1vs1_player - @player1 = Player.new(create_player("Player 1").merge(:piece => 'X')) + @player1 = Player.new(create_player("Player X").merge(:piece => 'X')) @player1.save - @player2 = Player.new(create_player("Player 2").merge(:piece => 'O')) + @player2 = Player.new(create_player("Player O").merge(:piece => 'O')) @player2.save end def self.create_1vsPC_player @player1 = ComputerPlayer.new({name: "MCP", piece: 'X'}) - @player2 = Player.new(create_player("Player 2").merge(:piece => 'O')) + @player2 = Player.new(create_player("Player O").merge(:piece => 'O')) @player2.save end def self.create_1vsTwitter_player + @player2 = Player.new(create_player("Player O").merge(:piece => 'O')) + @player1 = TwitterPlayer.from_twitter + end + + def self.create_PCvsTwitter_player @player2 = ComputerPlayer.new({name: "MCP", piece: 'O'}) - #@player2 = Player.new(create_player("Player 2").merge(:piece => 'O')) @player1 = TwitterPlayer.from_twitter end def self.show_user_statistic @player = Player.new(create_player("Player")) puts "STATISTIC for #{@player}" - puts "=========================" + puts "=" * 50 puts "WINS: #{@player.wins}" puts "LOSSES: #{@player.losses}" puts "TIES: #{@player.ties}" start("What do you want to do now?") end - + def self.board_display - if game.current_player == @player1 - if @player1.class == TwitterPlayer - puts game.board.to_s - tweet_board(game.board.to_s, twitter_tag) - else - print_board - end + if (game.current_player == @player1) && (@player1.class == TwitterPlayer) + puts game.board.to_s + tweet_board(game.board.to_s, twitter_tag) else print_board end @@ -91,39 +96,56 @@ def self.tweet_board(tweet, message) def self.create_player(player) player_name = get_player_name(player) - player_twitter = unique_twitter_account - player_password = secure_password - { name: player_name, twitter: player_twitter, password: player_password } + { name: player_name, + twitter: unique_twitter_account(player_name, player), + password: secure_password + } end def self.get_player_name(player) + puts "=" * 50 puts "Enter username for #{player}" gets.chomp.capitalize end - def self.unique_twitter_account + def self.unique_twitter_account(player_name,player) puts "Enter your twitter account" - player_twitter = gets.chomp # TODO: secure uniquness of twitter + twitter = gets.chomp + if valid_twitter?(player_name, twitter) + return twitter + else + puts "Invalid player name or twitter account" + create_player(player) + end + end + + def self.valid_twitter?(player_name, twitter) + DB.check_twitter(player_name, twitter) end def self.secure_password - puts "Enter your password" # TODO: secure password - player_password = gets.chomp + puts "Enter your password" + pwd = STDIN.noecho {|i| i.gets}.chomp + valid_password?(pwd) + end + + def self.valid_password?(pwd) + unless pwd.empty? # && pwd.length > 6 + return pwd + else + puts "Password too short" + secure_password + end end def self.twitter_tag - if game.board.full? # tie + if game.board.full? message = "Draw game. Play again? #dbc_c4" - elsif game.board.check_four_consecutive? #winner - if game.board.empty_cells.even? - message = "I win! Good game. #dbc_c4" - else - message = "You win. #dbc_c4" - end + elsif game.board.check_four_consecutive? + game.board.empty_cells.even? ? message = "I win! Good game. #dbc_c4" : message = "You win. #dbc_c4" else message = '#dbc_c4' end - return message end def self.congratulations(player) diff --git a/spec/tweet_spec.rb b/spec/tweet_spec.rb deleted file mode 100644 index 24df01f..0000000 --- a/spec/tweet_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -require 'spec_helper' - -describe Tweet do - let(:tweet) {Tweet.new} - - describe "#initialize" do - it "creates a random tag" do - tweet.random_tag.should be_an_instance_of String - end - end - - describe "#twitter_player" do - it "tracks a keyword" do - - end - end - -end \ No newline at end of file From 37f599ffda1567a166e183ef5bc631ce83b875f6 Mon Sep 17 00:00:00 2001 From: Brent Coughenour Date: Mon, 5 Nov 2012 08:33:12 -0800 Subject: [PATCH 40/47] added back in working line in computer player logic that had been commented out --- lib/connect_four/computer_player.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/connect_four/computer_player.rb b/lib/connect_four/computer_player.rb index e34f2d5..b1cedfa 100644 --- a/lib/connect_four/computer_player.rb +++ b/lib/connect_four/computer_player.rb @@ -39,13 +39,8 @@ def cell_rating_left(position) left_cell = UI.game.board.cells[position - 1] unless [35, 28, 21, 14, 7, 0].include?(position) if left_cell == opponent_piece - - # puts positions_ratings[(position%7)+1].inspect positions_ratings[position%7 + 1] += 2 - - # ^^^^^^^^for some reason this is nil ^^^^^^^^^^^ - - # cell_rating_left2(position) unless [36, 29, 22, 15, 8, 1].include?(position) + cell_rating_left2(position) unless [36, 29, 22, 15, 8, 1].include?(position) else positions_ratings[position%7+1] ||= 0 end From c5ccbe506cf8bb1dffc9c87ec7a3ab140c9ad84b Mon Sep 17 00:00:00 2001 From: Brent Coughenour Date: Mon, 5 Nov 2012 08:38:11 -0800 Subject: [PATCH 41/47] incorporated cell_rating_bottom methods from Jonathan's defense class --- lib/connect_four/computer_player.rb | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/connect_four/computer_player.rb b/lib/connect_four/computer_player.rb index b1cedfa..feb58ab 100644 --- a/lib/connect_four/computer_player.rb +++ b/lib/connect_four/computer_player.rb @@ -76,12 +76,27 @@ def cell_rating_right(position) def cell_rating_bottom(position) unless [35, 36, 37, 38, 39, 40, 41].include?(position) bottom_cell = UI.game.board.cells[position+7] - if bottom_cell == opponent_piece - positions_ratings[position%7+1] = 2 - else - positions_ratings[position%7+1] ||= 0 + 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 = UI.game.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 = UI.game.board.cells[position+21] + if bottom_cell3 == "red" + positions_ratings[position%7+1] += 8 + end end def move From b1f7f14e9f40dbea8802a2665ac50adedd3ba6ac Mon Sep 17 00:00:00 2001 From: Brent Coughenour Date: Mon, 5 Nov 2012 08:42:42 -0800 Subject: [PATCH 42/47] name changes for computer player piece updates --- lib/connect_four/computer_player.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/connect_four/computer_player.rb b/lib/connect_four/computer_player.rb index feb58ab..779970e 100644 --- a/lib/connect_four/computer_player.rb +++ b/lib/connect_four/computer_player.rb @@ -76,7 +76,7 @@ def cell_rating_right(position) def cell_rating_bottom(position) unless [35, 36, 37, 38, 39, 40, 41].include?(position) bottom_cell = UI.game.board.cells[position+7] - if bottom_cell == "red" + if bottom_cell == opponent_piece positions_ratings[position%7+1] += 2 cell_rating_bottom2(position) unless [28, 29, 30, 31, 32, 33, 34].include?(position) end @@ -86,7 +86,7 @@ def cell_rating_bottom(position) def cell_rating_bottom2(position) bottom_cell2 = UI.game.board.cells[position+14] - if bottom_cell2 == "red" + if bottom_cell2 == opponent_piece positions_ratings[position%7+1] += 4 cell_rating_bottom3(position) unless [22, 23, 24, 25, 26, 27].include?(position) end @@ -94,7 +94,7 @@ def cell_rating_bottom2(position) def cell_rating_bottom3(position) bottom_cell3 = UI.game.board.cells[position+21] - if bottom_cell3 == "red" + if bottom_cell3 == opponent_piece positions_ratings[position%7+1] += 8 end end From 1b965bf912a911fcf4c83d364188d9eccb84716f Mon Sep 17 00:00:00 2001 From: Brent Coughenour Date: Mon, 5 Nov 2012 08:50:05 -0800 Subject: [PATCH 43/47] switched player one to 'O' and player two to 'X' --- lib/connect_four/twitter_player.rb | 2 +- lib/connect_four/ui.rb | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/connect_four/twitter_player.rb b/lib/connect_four/twitter_player.rb index 62c4762..8af32e3 100644 --- a/lib/connect_four/twitter_player.rb +++ b/lib/connect_four/twitter_player.rb @@ -42,7 +42,7 @@ def self.from_twitter client.stop puts "@#{status.user[:screen_name]} Game on! #dbc_c4 #{@random_tag}" Twitter.update("@#{status.user[:screen_name]} Game on! #dbc_c4 #{@random_tag}") - return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'X', id: status.user[:id]}, @random_tag) + return TwitterPlayer.new({name: status.user[:name], twitter: status.user[:screen_name], piece: 'O', id: status.user[:id]}, @random_tag) end end end diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index b306f64..83e315f 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -49,25 +49,25 @@ def self.choices(user_input) end def self.create_1vs1_player - @player1 = Player.new(create_player("Player X").merge(:piece => 'X')) + @player1 = Player.new(create_player("Player O").merge(:piece => 'O')) @player1.save - @player2 = Player.new(create_player("Player O").merge(:piece => 'O')) + @player2 = Player.new(create_player("Player X").merge(:piece => 'X')) @player2.save end def self.create_1vsPC_player - @player1 = ComputerPlayer.new({name: "MCP", piece: 'X'}) - @player2 = Player.new(create_player("Player O").merge(:piece => 'O')) + @player1 = ComputerPlayer.new({name: "MCP", piece: 'O'}) + @player2 = Player.new(create_player("Player X").merge(:piece => 'X')) @player2.save end def self.create_1vsTwitter_player - @player2 = Player.new(create_player("Player O").merge(:piece => 'O')) + @player2 = Player.new(create_player("Player X").merge(:piece => 'X')) @player1 = TwitterPlayer.from_twitter end def self.create_PCvsTwitter_player - @player2 = ComputerPlayer.new({name: "MCP", piece: 'O'}) + @player2 = ComputerPlayer.new({name: "MCP", piece: 'X'}) @player1 = TwitterPlayer.from_twitter end From e90200eca8559555d49ce335c5b393aa81b739f2 Mon Sep 17 00:00:00 2001 From: Daniela Grossmann Date: Mon, 5 Nov 2012 09:48:13 -0800 Subject: [PATCH 44/47] DB error solved when called with PC or Twitter Player --- lib/connect_four/game.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/connect_four/game.rb b/lib/connect_four/game.rb index 6e4106f..f100c83 100644 --- a/lib/connect_four/game.rb +++ b/lib/connect_four/game.rb @@ -19,7 +19,7 @@ def play save(0) else winner = current_player - save(winner.id) + (winner.name == "MCP" || winner.class == TwitterPlayer) ? save(0) : save(winner.id) end UI.congratulations(winner) UI.start("What do you want to do now?") @@ -38,7 +38,11 @@ def over? end def save(winner_id) - values = [@player1.id, @player2.id, winner_id] + if @player1.name == "MCP" || @player1.class == TwitterPlayer + values = [0, @player2.id, winner_id] + else + values = [@player1.id, @player2.id, winner_id] + end DB.handler("INSERT INTO games (player1, player2, winner) VALUES (?, ?, ?);", values) end From 4ef8a3194526fe04a722fc8a5cde3d7ff336007f Mon Sep 17 00:00:00 2001 From: Daniela Grossmann Date: Mon, 5 Nov 2012 09:48:45 -0800 Subject: [PATCH 45/47] dryed board class with diagonals --- lib/connect_four/board.rb | 27 +++++---------------------- spec/board_spec.rb | 4 ++-- 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/lib/connect_four/board.rb b/lib/connect_four/board.rb index 7490321..82d6edd 100644 --- a/lib/connect_four/board.rb +++ b/lib/connect_four/board.rb @@ -94,39 +94,22 @@ def column_index_number(index) index % col_num end -# DRY? I was not able to make these two dry, wierd results! - def diagonal_indexes_to_right - start_indexes = get_start_indexes_to_right(4) - leap = col_num + 1 + def diagonal_indexes(leap_no, end_no, start_indexes) + leap = col_num + leap_no 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 + break if row == row_num || column == end_no 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 diagonals - (diagonal_indexes_to_right + diagonal_indexes_to_left).map {|diagonal| diagonal.map { |i| i = cells[i]}} + (diagonal_indexes(1, 0, get_start_indexes_to_right(4)) + diagonal_indexes(-1, col_num-1, get_start_indexes_to_left(4))).map {|diagonal| diagonal.map { |i| i = cells[i]}} end def check_four_consecutive? diff --git a/spec/board_spec.rb b/spec/board_spec.rb index 03e2b53..c00bda5 100644 --- a/spec/board_spec.rb +++ b/spec/board_spec.rb @@ -181,12 +181,12 @@ def fake_column_array(index) context "#diagonal_indexes" do it "has a maximum of row_num units when its going to right" do - board.diagonal_indexes_to_right.each do |diagonal| + board.diagonal_indexes(1, 0, board.get_start_indexes_to_right(4)).each do |diagonal| diagonal.length.should <= board.row_num end end it "has a maximum of row_num units when its going to left" do - board.diagonal_indexes_to_left.each do |diagonal| + board.diagonal_indexes(-1, board.col_num-1, board.get_start_indexes_to_left(4)).each do |diagonal| diagonal.length.should <= board.row_num end end From 01a6d045f9286fb383bac76474779b8dd60709f1 Mon Sep 17 00:00:00 2001 From: Brent Coughenour Date: Mon, 5 Nov 2012 10:12:25 -0800 Subject: [PATCH 46/47] Changed the formatting of printing the board to console while playing twitter game --- lib/connect_four/twitter_player.rb | 2 +- lib/connect_four/ui.rb | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/connect_four/twitter_player.rb b/lib/connect_four/twitter_player.rb index 8af32e3..c09c4ab 100644 --- a/lib/connect_four/twitter_player.rb +++ b/lib/connect_four/twitter_player.rb @@ -36,7 +36,7 @@ def self.from_twitter puts "...waiting for player" TweetStream::Client.new.track('#dbc_c4') do |status, client| - puts "id = #{status.user[:id]}, text = #{status.text}" + #puts "id = #{status.user[:id]}, text = #{status.text}" text = status.text.gsub(/#.*/, "").strip if text == "Who wants to get demolished?" client.stop diff --git a/lib/connect_four/ui.rb b/lib/connect_four/ui.rb index 83e315f..8bb27c1 100644 --- a/lib/connect_four/ui.rb +++ b/lib/connect_four/ui.rb @@ -37,10 +37,10 @@ def self.choices(user_input) create_PCvsTwitter_player when "s" show_user_statistic - when "r" - return - when "q" - puts "=" * 100 + when "r" + return + when "q" + puts "=" * 100 puts "Auf Wiedersehen!" exit else @@ -83,7 +83,8 @@ def self.show_user_statistic def self.board_display if (game.current_player == @player1) && (@player1.class == TwitterPlayer) - puts game.board.to_s + puts "Tweeted:" + print_board tweet_board(game.board.to_s, twitter_tag) else print_board @@ -91,7 +92,7 @@ def self.board_display end def self.tweet_board(tweet, message) - puts "@#{@player1.twitter} #{tweet} #{message} #{@player1.random_tag}" + #puts "@#{@player1.twitter} #{tweet} #{message} #{@player1.random_tag}" Twitter.update("@#{@player1.twitter} #{tweet} #{message} #{@player1.random_tag}") end From 1ee24554f874a070de326a3983935d93eeede25a Mon Sep 17 00:00:00 2001 From: Jonathan Pepin Date: Mon, 5 Nov 2012 11:06:45 -0800 Subject: [PATCH 47/47] the bot is working yeah --- connect4/defense_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connect4/defense_spec.rb b/connect4/defense_spec.rb index ea964b7..25405c7 100644 --- a/connect4/defense_spec.rb +++ b/connect4/defense_spec.rb @@ -56,7 +56,7 @@ deff.cell_rating_right(39).should eq 6 end - it "should rate 14 a cell with 2 pieces on the right" do + it "should rate 14 a cell with 3 pieces on the right" do deff = Defense.new deff.board.place_piece(7, "red") deff.board.place_piece(6, "red")