From 4fccecf8f42063749a2bea506b209756cb0d530b Mon Sep 17 00:00:00 2001 From: Charles Green Date: Thu, 26 Mar 2015 14:53:07 -0400 Subject: [PATCH 01/19] added initial files --- Board.rb | 56 ++++++++++++++++++++++++++++++++++ game.rb | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ git workflow | 22 ++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 Board.rb create mode 100644 game.rb create mode 100644 git workflow diff --git a/Board.rb b/Board.rb new file mode 100644 index 0000000..52466bd --- /dev/null +++ b/Board.rb @@ -0,0 +1,56 @@ +classes + +class Board(args) + 2 + def initialize + @board = args + end + + def valid_moves + + end + + def find_piece_type + + end + + def move_piece + + end + + def capture + + end +end + +class Player #? +end + +class Piece + def initialize(color) + @color = color + end + + def possible_moves + end +end + +class Pawn < Piece + +end + +# TODO: deal with possible_moves method +class Rook < Piece +end + +class King < Piece +end + +class Queen < Piece +end + +class Bishop < Piece +end + +class Knight < Piece +end diff --git a/game.rb b/game.rb new file mode 100644 index 0000000..5763437 --- /dev/null +++ b/game.rb @@ -0,0 +1,85 @@ + +def initialize(board, view) + @board = board.new(args) + @view = view.new + @players = ["white", "black"] +end + +def play + # if !game_over + players.each do |player| + turns(player) + end + # end +end + +def turns(player) + # if blank square/outside of board square + # + # view.turn_message +end + + +class view + + def turn_message(player) + message(player_turn_message) + end + + def choose_piece + message(choose_piece_message) + @choice = user_input + end + # if user picks invalid square + def pick_again + message(choose_again_message) + end + + def display_valid_moves + message(piece_chosen_message) + end + + def display_player_move + message(player_move_message) + end + + def display_capture_move + message(capture_message) + end + + # Messages to console + + def player_turn_message(player) + "#{player}'s turn" + end + + def choose_piece_message(player) + "#{player}, which piece do you want to move? ex: a5, e8" + end + + def piece_chosen_message(player, piece, moves) + "moves for #{player} #{piece}" + moves.join(" ") + end + # @move gets sent to board + def pick_move(player, choice) + "#{player}, move #{choice} where?" + @move = user_input + end + + def player_move_message(player, piece, move) + "ok, #{player}'s #{piece} #{choice} to move to #{move}" + end + + def capture_message(player, player2, piece, captured_piece, choice, move) + "#{player}'s #{piece} {choice} captures #{player2}'s #{captured_piece} #{move}" + end + + # siphon methods for prompts and inputs + def user_input + gets.chomp + end + + def message(str) + puts str + end +end diff --git a/git workflow b/git workflow new file mode 100644 index 0000000..9ad9fc6 --- /dev/null +++ b/git workflow @@ -0,0 +1,22 @@ +git workflow + +Before working on a feature that might break something. + +git checkout - b "main branch name"_feature + +git checkout - b "main branch name"_anotherfeature + + +when you want to merge back in + +1st.) ON MASTER BRANCH Git pull origin master +2nd.) git fetch origin master +3nd.) git merge master (merges with your feature branch) +4rd.) fix merge issues +5th.) git merge master +6.) git push origin (branch) +7.) make pull request +8.) compare and merge + + +compare base branch master to feature branch \ No newline at end of file From 6d730b1fe19c176e220e9f00edbe550a699293ce Mon Sep 17 00:00:00 2001 From: Charles Green Date: Thu, 26 Mar 2015 16:53:43 -0400 Subject: [PATCH 02/19] prepping for work division --- Board.rb | 95 +++++++++++++++++++++++++++++++++++++++++----------- game.rb | 17 ++++++---- git workflow | 8 +++++ 3 files changed, 95 insertions(+), 25 deletions(-) diff --git a/Board.rb b/Board.rb index 52466bd..747b4e0 100644 --- a/Board.rb +++ b/Board.rb @@ -1,19 +1,58 @@ classes class Board(args) - 2 + 2 def initialize @board = args end - def valid_moves - end + def valid_moves(piece) + + valid_moves = [] + possibilities = [] + + x_location = piece.location[0] + y_location = piece.location[1] + x_difference = piece.moves[0] + y_difference = piece.moves[1] + # filter for out of bounds. out of bounds if x or y < 0 || x or y > 7 + possibilities << [(x_location + x_difference), (y_location + y_difference)] + out_of_bounds?#mathmatical possibilities + #check against piece.location + + possibilites.each do |coordinate| + # if collision with white piece, return false + if @board[coordinate[0]][coordinate[1]] - def find_piece_type + else + valid_moves << coordinate + + + # move to 0,2 and check a bunch + # + # figure out which directions are out of bounds + # + end + def find_piece(location_string) + # find the piece in the board array. (convert string to index) + index = string_to_index(location_string) + piece = @board[index[0]][index[1]] + location_col, location_row = index[0], index[1] + valid_moves(piece) + # run valid_moves(piece) + # return a piece object, and it's moves end + def find_piece(location_string) + index = string_to_index(location_string + piece = @board[index[0]][index[1]] + piece.location = [index[0], index[1]] + valid_moves(piece) + end + end + def move_piece end @@ -21,36 +60,54 @@ def move_piece def capture end + + def string_to_index(location_string) + # a5 + col_string, row_index = location_string.split("") + row_index = row_index.to_i + col_index = col_string.downcase.ord - 97 + [col_index, row_index] + end end class Player #? end class Piece + attr_accessor :color, :moves, :location def initialize(color) @color = color + @moves = moves #possible moves on an empty board + @location = location end - def possible_moves - end + end class Pawn < Piece + def initialize() + + self.moves = [0,1] + # pawn can only move forward. + # pawn can only move 1 space, unless it's current position is the starting col. + # pawn can move to left front or right front diagonal + # if opposing piece occupies that space + # stretch: en empasse move condition + # stretch: promotion + end -end - -# TODO: deal with possible_moves method -class Rook < Piece -end + # TODO: deal with possible_moves method + class Rook < Piece + end -class King < Piece -end + class King < Piece + end -class Queen < Piece -end + class Queen < Piece + end -class Bishop < Piece -end + class Bishop < Piece + end -class Knight < Piece -end + class Knight < Piece + end diff --git a/game.rb b/game.rb index 5763437..6ae4aaa 100644 --- a/game.rb +++ b/game.rb @@ -15,8 +15,11 @@ def play def turns(player) # if blank square/outside of board square - # - # view.turn_message + view.turn_message(player) + board.find_piece(view.choose_piece) +end + +def find_piece(input) end @@ -30,6 +33,11 @@ def choose_piece message(choose_piece_message) @choice = user_input end + + def pick_move(player, choice) + "#{player}, move #{choice} where?" + @move = user_input + end # if user picks invalid square def pick_again message(choose_again_message) @@ -61,10 +69,7 @@ def piece_chosen_message(player, piece, moves) "moves for #{player} #{piece}" + moves.join(" ") end # @move gets sent to board - def pick_move(player, choice) - "#{player}, move #{choice} where?" - @move = user_input - end + def player_move_message(player, piece, move) "ok, #{player}'s #{piece} #{choice} to move to #{move}" diff --git a/git workflow b/git workflow index 9ad9fc6..76918a7 100644 --- a/git workflow +++ b/git workflow @@ -1,3 +1,11 @@ + + +Rspec +MVC +classes + + + git workflow Before working on a feature that might break something. From 6b390e1e2886362c4555fbe1bd91df30832ed1c5 Mon Sep 17 00:00:00 2001 From: Alexander Mihalopoulos Date: Thu, 26 Mar 2015 16:55:52 -0400 Subject: [PATCH 03/19] Whatever --- Board.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Board.rb b/Board.rb index 52466bd..56cec74 100644 --- a/Board.rb +++ b/Board.rb @@ -44,9 +44,12 @@ class Rook < Piece end class King < Piece + moves = [[0, 1],[0, -1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]] + possibilities = [] end class Queen < Piece + end class Bishop < Piece From b1deda4ff2e1159ff80962b46eb2b5e7e6874d8f Mon Sep 17 00:00:00 2001 From: Alexander Mihalopoulos Date: Thu, 26 Mar 2015 17:24:58 -0400 Subject: [PATCH 04/19] possibilities. --- Board.rb | 78 +++++++++++++++++++++++++++++++++++++++++++++------- game.rb | 17 ++++++++---- git workflow | 8 ++++++ 3 files changed, 87 insertions(+), 16 deletions(-) diff --git a/Board.rb b/Board.rb index 56cec74..560eb50 100644 --- a/Board.rb +++ b/Board.rb @@ -1,19 +1,58 @@ classes class Board(args) - 2 + 2 def initialize @board = args end - def valid_moves - end + def valid_moves(piece) + + valid_moves = [] + possibilities = [] + + x_location = piece.location[0] + y_location = piece.location[1] + x_difference = piece.moves[0] + y_difference = piece.moves[1] + # filter for out of bounds. out of bounds if x or y < 0 || x or y > 7 + possibilities << [(x_location + x_difference), (y_location + y_difference)] + out_of_bounds?#mathmatical possibilities + #check against piece.location + + possibilites.each do |coordinate| + # if collision with white piece, return false + if @board[coordinate[0]][coordinate[1]] - def find_piece_type + else + valid_moves << coordinate + + # move to 0,2 and check a bunch + # + # figure out which directions are out of bounds + # + end + + def find_piece(location_string) + # find the piece in the board array. (convert string to index) + index = string_to_index(location_string) + piece = @board[index[0]][index[1]] + location_col, location_row = index[0], index[1] + valid_moves(piece) + # run valid_moves(piece) + # return a piece object, and it's moves end + def find_piece(location_string) + index = string_to_index(location_string + piece = @board[index[0]][index[1]] + piece.location = [index[0], index[1]] + valid_moves(piece) + end + end + def move_piece end @@ -21,27 +60,46 @@ def move_piece def capture end + + def string_to_index(location_string) + # a5 + col_string, row_index = location_string.split("") + row_index = row_index.to_i + col_index = col_string.downcase.ord - 97 + [col_index, row_index] + end end class Player #? end class Piece + attr_accessor :color, :moves, :location def initialize(color) @color = color + @moves = moves #possible moves on an empty board + @location = location end - def possible_moves - end + end class Pawn < Piece + def initialize() + + self.moves = [0,1] + # pawn can only move forward. + # pawn can only move 1 space, unless it's current position is the starting col. + # pawn can move to left front or right front diagonal + # if opposing piece occupies that space + # stretch: en empasse move condition + # stretch: promotion + end -end + # TODO: deal with possible_moves method + class Rook < Piece + end -# TODO: deal with possible_moves method -class Rook < Piece -end class King < Piece moves = [[0, 1],[0, -1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]] diff --git a/game.rb b/game.rb index 5763437..6ae4aaa 100644 --- a/game.rb +++ b/game.rb @@ -15,8 +15,11 @@ def play def turns(player) # if blank square/outside of board square - # - # view.turn_message + view.turn_message(player) + board.find_piece(view.choose_piece) +end + +def find_piece(input) end @@ -30,6 +33,11 @@ def choose_piece message(choose_piece_message) @choice = user_input end + + def pick_move(player, choice) + "#{player}, move #{choice} where?" + @move = user_input + end # if user picks invalid square def pick_again message(choose_again_message) @@ -61,10 +69,7 @@ def piece_chosen_message(player, piece, moves) "moves for #{player} #{piece}" + moves.join(" ") end # @move gets sent to board - def pick_move(player, choice) - "#{player}, move #{choice} where?" - @move = user_input - end + def player_move_message(player, piece, move) "ok, #{player}'s #{piece} #{choice} to move to #{move}" diff --git a/git workflow b/git workflow index 9ad9fc6..76918a7 100644 --- a/git workflow +++ b/git workflow @@ -1,3 +1,11 @@ + + +Rspec +MVC +classes + + + git workflow Before working on a feature that might break something. From b2c6e00cce37243282858000dab0f566d73c461b Mon Sep 17 00:00:00 2001 From: Charles Green Date: Thu, 26 Mar 2015 17:31:17 -0400 Subject: [PATCH 05/19] slight changes in board.rb --- Board.rb | 207 ++++++++++++++++++++++++++++++++----------------------- game.rb | 12 +++- 2 files changed, 129 insertions(+), 90 deletions(-) diff --git a/Board.rb b/Board.rb index 747b4e0..f0f1132 100644 --- a/Board.rb +++ b/Board.rb @@ -1,113 +1,144 @@ classes class Board(args) - 2 + def initialize @board = args end - def valid_moves(piece) - - valid_moves = [] - possibilities = [] - - x_location = piece.location[0] - y_location = piece.location[1] - x_difference = piece.moves[0] - y_difference = piece.moves[1] - # filter for out of bounds. out of bounds if x or y < 0 || x or y > 7 - possibilities << [(x_location + x_difference), (y_location + y_difference)] - out_of_bounds?#mathmatical possibilities - #check against piece.location - - possibilites.each do |coordinate| - # if collision with white piece, return false - if @board[coordinate[0]][coordinate[1]] + def to_s - else - valid_moves << coordinate - - - # move to 0,2 and check a bunch - # - # figure out which directions are out of bounds - # - end - - def find_piece(location_string) - # find the piece in the board array. (convert string to index) - index = string_to_index(location_string) - piece = @board[index[0]][index[1]] - location_col, location_row = index[0], index[1] - valid_moves(piece) - # run valid_moves(piece) - # return a piece object, and it's moves - end - - def find_piece(location_string) - index = string_to_index(location_string - piece = @board[index[0]][index[1]] - piece.location = [index[0], index[1]] - valid_moves(piece) + board_string = "" + @board.each do |col| + board_string += col.transpose.reverse end - end - - def move_piece - + board_to_string end - def capture - end - def string_to_index(location_string) - # a5 - col_string, row_index = location_string.split("") - row_index = row_index.to_i - col_index = col_string.downcase.ord - 97 - [col_index, row_index] - end -end + # @board = 8 subarrays, as columns, starting at bottom left. piece objects -class Player #? -end + def valid_moves(piece) -class Piece - attr_accessor :color, :moves, :location - def initialize(color) - @color = color - @moves = moves #possible moves on an empty board - @location = location - end + valid_moves = [] + possibilities = [] + x_location = piece.location[0] + y_location = piece.location[1] -end + (piece.moves).each do |vectors_array| -class Pawn < Piece - def initialize() + x_location += vectors_array[0] + y_location += vectors_array[1] + #filtered for out_of_bounds + next if out_of_bounds?(x_location,y_location) + possibilities << [x_location, y_location] - self.moves = [0,1] - # pawn can only move forward. - # pawn can only move 1 space, unless it's current position is the starting col. - # pawn can move to left front or right front diagonal - # if opposing piece occupies that space - # stretch: en empasse move condition - # stretch: promotion - end + end - # TODO: deal with possible_moves method - class Rook < Piece - end + possibilities.each do - class King < Piece - end - class Queen < Piece - end + valid_moves << + end - class Bishop < Piece - end - class Knight < Piece - end + # filter for out of bounds. out of bounds if x or y < 0 || x or y > 7 + def out_of_bounds?(x,y) + (x < 0 || y < 0 || x > 7 || y > 7) + end + #mathmatical possibilities + #check against piece.location + + possibilites.each do |coordinate| + # if collision with white piece, return false + if @board[coordinate[0]][coordinate[1]] + + else + valid_moves << coordinate + + + # move to 0,2 and check a bunch + # + # figure out which directions are out of bounds + # + end + + def find_piece(location_string) + # find the piece in the board array. (convert string to index) + index = string_to_index(location_string) + piece = @board[index[0]][index[1]] + location_col, location_row = index[0], index[1] + valid_moves(piece) + # run valid_moves(piece) + # return a piece object, and it's moves + end + + def find_piece(location_string) + index = string_to_index(location_string + piece = @board[index[0]][index[1]] + piece.location = [index[0], index[1]] + valid_moves(piece) + end + end + + def move_piece + + end + + def capture + + end + + def string_to_index(location_string) + # a5 + col_string, row_index = location_string.split("") + row_index = row_index.to_i + col_index = col_string.downcase.ord - 97 + [col_index, row_index] + end + end + + class Player #? + end + + class Piece + attr_accessor :color, :moves, :location + def initialize(color) + @color = color + @moves = moves #possible moves on an empty board + @location = location + end + + + end + + class Pawn < Piece + def initialize() + + self.moves = [0,1] + # pawn can only move forward. + # pawn can only move 1 space, unless it's current position is the starting col. + # pawn can move to left front or right front diagonal + # if opposing piece occupies that space + # stretch: en empasse move condition + # stretch: promotion + end + + # TODO: deal with possible_moves method + class Rook < Piece + end + + class King < Piece + end + + class Queen < Piece + end + + class Bishop < Piece + end + + class Knight < Piece + end diff --git a/game.rb b/game.rb index 6ae4aaa..59e0a37 100644 --- a/game.rb +++ b/game.rb @@ -19,12 +19,20 @@ def turns(player) board.find_piece(view.choose_piece) end -def find_piece(input) -end +# black chars +♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜ +♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ +# white chars + ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙ +♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖ class view + def display_board + + end + def turn_message(player) message(player_turn_message) end From e5f442a7f06669b71aa6e96fbbaa2533367c1760 Mon Sep 17 00:00:00 2001 From: Charles Green Date: Thu, 26 Mar 2015 17:39:55 -0400 Subject: [PATCH 06/19] have start of display working correctly --- Board.rb | 248 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 125 insertions(+), 123 deletions(-) diff --git a/Board.rb b/Board.rb index f0f1132..c25aa2f 100644 --- a/Board.rb +++ b/Board.rb @@ -1,144 +1,146 @@ -classes - -class Board(args) - +class Board + attr_reader :board def initialize - @board = args + @board = [ [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9] ] end - - def to_s - + def to_string board_string = "" + @board = @board.transpose.reverse @board.each do |col| - board_string += col.transpose.reverse + board_string += col.join(" ") + "\n" end - board_to_string + puts board_string end +end +b = Board.new +# p b.board +b.to_string - # @board = 8 subarrays, as columns, starting at bottom left. piece objects +# # @board = 8 subarrays, as columns, starting at bottom left. piece objects - def valid_moves(piece) +# def valid_moves(piece) - valid_moves = [] - possibilities = [] +# valid_moves = [] +# possibilities = [] - x_location = piece.location[0] - y_location = piece.location[1] +# x_location = piece.location[0] +# y_location = piece.location[1] - (piece.moves).each do |vectors_array| +# (piece.moves).each do |vectors_array| - x_location += vectors_array[0] - y_location += vectors_array[1] - #filtered for out_of_bounds - next if out_of_bounds?(x_location,y_location) - possibilities << [x_location, y_location] +# x_location += vectors_array[0] +# y_location += vectors_array[1] +# #filtered for out_of_bounds +# next if out_of_bounds?(x_location,y_location) +# possibilities << [x_location, y_location] - end +# end - possibilities.each do +# possibilities.each do - valid_moves << - end +# valid_moves << +# end - # filter for out of bounds. out of bounds if x or y < 0 || x or y > 7 - def out_of_bounds?(x,y) - (x < 0 || y < 0 || x > 7 || y > 7) - end - #mathmatical possibilities - #check against piece.location - - possibilites.each do |coordinate| - # if collision with white piece, return false - if @board[coordinate[0]][coordinate[1]] - - else - valid_moves << coordinate - - - # move to 0,2 and check a bunch - # - # figure out which directions are out of bounds - # - end - - def find_piece(location_string) - # find the piece in the board array. (convert string to index) - index = string_to_index(location_string) - piece = @board[index[0]][index[1]] - location_col, location_row = index[0], index[1] - valid_moves(piece) - # run valid_moves(piece) - # return a piece object, and it's moves - end - - def find_piece(location_string) - index = string_to_index(location_string - piece = @board[index[0]][index[1]] - piece.location = [index[0], index[1]] - valid_moves(piece) - end - end - - def move_piece - - end - - def capture - - end - - def string_to_index(location_string) - # a5 - col_string, row_index = location_string.split("") - row_index = row_index.to_i - col_index = col_string.downcase.ord - 97 - [col_index, row_index] - end - end - - class Player #? - end - - class Piece - attr_accessor :color, :moves, :location - def initialize(color) - @color = color - @moves = moves #possible moves on an empty board - @location = location - end - - - end - - class Pawn < Piece - def initialize() - - self.moves = [0,1] - # pawn can only move forward. - # pawn can only move 1 space, unless it's current position is the starting col. - # pawn can move to left front or right front diagonal - # if opposing piece occupies that space - # stretch: en empasse move condition - # stretch: promotion - end - - # TODO: deal with possible_moves method - class Rook < Piece - end - - class King < Piece - end - - class Queen < Piece - end - - class Bishop < Piece - end - - class Knight < Piece - end +# # filter for out of bounds. out of bounds if x or y < 0 || x or y > 7 +# def out_of_bounds?(x,y) +# (x < 0 || y < 0 || x > 7 || y > 7) +# end +# #mathmatical possibilities +# #check against piece.location + +# possibilites.each do |coordinate| +# # if collision with white piece, return false +# if @board[coordinate[0]][coordinate[1]] + +# else +# valid_moves << coordinate + + +# # move to 0,2 and check a bunch +# # +# # figure out which directions are out of bounds +# # +# end + +# def find_piece(location_string) +# # find the piece in the board array. (convert string to index) +# index = string_to_index(location_string) +# piece = @board[index[0]][index[1]] +# location_col, location_row = index[0], index[1] +# valid_moves(piece) +# # run valid_moves(piece) +# # return a piece object, and it's moves +# end + +# def find_piece(location_string) +# index = string_to_index(location_string) +# piece = @board[index[0]][index[1]] +# piece.location = [index[0], index[1]] +# valid_moves(piece) +# end +# end +# end + +# def move_piece + +# end + +# def capture + +# end + +# def string_to_index(location_string) +# # a5 +# col_string, row_index = location_string.split("") +# row_index = row_index.to_i +# col_index = col_string.downcase.ord - 97 +# [col_index, row_index] +# end +# end + +# class Player #? +# end + +# class Piece +# attr_accessor :color, :moves, :location +# def initialize(color) +# @color = color +# @moves = moves #possible moves on an empty board +# @location = location +# end + + +# end + +# class Pawn < Piece +# def initialize() + +# self.moves = [0,1] +# # pawn can only move forward. +# # pawn can only move 1 space, unless it's current position is the starting col. +# # pawn can move to left front or right front diagonal +# # if opposing piece occupies that space +# # stretch: en empasse move condition +# # stretch: promotion +# end + +# # TODO: deal with possible_moves method +# class Rook < Piece +# end + +# class King < Piece +# end + +# class Queen < Piece +# end + +# class Bishop < Piece +# end + +# class Knight < Piece +# end From e9007daf8619ac4cdcab28f0c7527a525a93e55c Mon Sep 17 00:00:00 2001 From: Charles Green Date: Thu, 26 Mar 2015 17:41:12 -0400 Subject: [PATCH 07/19] have start of display working correctly --- Board.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Board.rb b/Board.rb index c25aa2f..7cf2d92 100644 --- a/Board.rb +++ b/Board.rb @@ -5,10 +5,12 @@ def initialize end def to_string + row_num = 1 board_string = "" @board = @board.transpose.reverse @board.each do |col| - board_string += col.join(" ") + "\n" + board_string += row_num + col.join(" ") + "\n" + row += 1 end puts board_string end From ee1499f4f529c70dbbca23ada29887a884bb1039 Mon Sep 17 00:00:00 2001 From: grantziolkowski Date: Thu, 26 Mar 2015 18:37:31 -0400 Subject: [PATCH 08/19] Valid moves logic --- Board.rb | 82 +++++++++++++++++++++++++++++++------------------------- game.rb | 12 ++++----- 2 files changed, 52 insertions(+), 42 deletions(-) diff --git a/Board.rb b/Board.rb index 747b4e0..7fa4e27 100644 --- a/Board.rb +++ b/Board.rb @@ -1,57 +1,60 @@ -classes - class Board(args) - 2 def initialize - @board = args + @board = Array.new(8) { Array.new(nil)} + @ + @board.each do |row| + row[6].map! |cell| cell = Pawn.new + row[1].map! |cell| cell = Pawn.new + end + @board.transpose end def valid_moves(piece) - valid_moves = [] - possibilities = [] - - x_location = piece.location[0] - y_location = piece.location[1] - x_difference = piece.moves[0] - y_difference = piece.moves[1] - # filter for out of bounds. out of bounds if x or y < 0 || x or y > 7 - possibilities << [(x_location + x_difference), (y_location + y_difference)] - out_of_bounds?#mathmatical possibilities - #check against piece.location - possibilites.each do |coordinate| + x = piece.location[0] + y= piece.location[1] + possibilities = all_possible_directions(x, y) + #filter for out_of_bounds + #mathematical possibilities + #check against piece.location + possibilites.each do |coordinate| # if collision with white piece, return false - if @board[coordinate[0]][coordinate[1]] - - else - valid_moves << coordinate + x = coordinate[0] + y = coordinate[1] + next if out_of_bounds?(x,y) + next if (@board[x][y]).color == piece.color + valid_moves << coordinate + end + valid_moves +end +def all_possible_directions(x,y) + possibilities = [] + (piece.moves).each do |vector| + x += vectors[0] + y += vector[1] + possibilities << [x,y] + end + possibilities +end +def out_of_bounds?(x,y) + (x < 0 || y < 0 || x > 7|| y > 7) +end # move to 0,2 and check a bunch - # + # # figure out which directions are out of bounds # end - def find_piece(location_string) - # find the piece in the board array. (convert string to index) - index = string_to_index(location_string) - piece = @board[index[0]][index[1]] - location_col, location_row = index[0], index[1] - valid_moves(piece) - # run valid_moves(piece) - # return a piece object, and it's moves - end - def find_piece(location_string) index = string_to_index(location_string piece = @board[index[0]][index[1]] piece.location = [index[0], index[1]] valid_moves(piece) - end - end + end def move_piece @@ -64,9 +67,9 @@ def capture def string_to_index(location_string) # a5 col_string, row_index = location_string.split("") - row_index = row_index.to_i + row_index = BOARDLENGTH - row_index.to_i col_index = col_string.downcase.ord - 97 - [col_index, row_index] + [row_index, col_index] end end @@ -87,7 +90,7 @@ def initialize(color) class Pawn < Piece def initialize() - self.moves = [0,1] + self.moves = [1,0] # pawn can only move forward. # pawn can only move 1 space, unless it's current position is the starting col. # pawn can move to left front or right front diagonal @@ -101,6 +104,13 @@ class Rook < Piece end class King < Piece + def moves + adjacent_array = [] + (-1..1).each do |rowl| + (-1..1).each do |col| + adjacent_array << [row, col] unless (row == 0 && col == 0) + end + end end class Queen < Piece diff --git a/game.rb b/game.rb index 6ae4aaa..999e198 100644 --- a/game.rb +++ b/game.rb @@ -1,7 +1,7 @@ def initialize(board, view) - @board = board.new(args) - @view = view.new + @board = Board.new(args) + @view = View.new @players = ["white", "black"] end @@ -16,14 +16,14 @@ def play def turns(player) # if blank square/outside of board square view.turn_message(player) - board.find_piece(view.choose_piece) -end + piece = board.find_piece(view.choose_piece) + moves = + piece_chosen_message(player, piece, moves) -def find_piece(input) end -class view +class View def turn_message(player) message(player_turn_message) From 0aa0e4ae34375479f53b8f36d7c3ac81039a5f28 Mon Sep 17 00:00:00 2001 From: Alexander Mihalopoulos Date: Thu, 26 Mar 2015 19:05:17 -0400 Subject: [PATCH 09/19] Add icons to pieces --- Board.rb | 83 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 31 deletions(-) diff --git a/Board.rb b/Board.rb index 2b4c4bc..1f94912 100644 --- a/Board.rb +++ b/Board.rb @@ -1,9 +1,11 @@ -classes class Board(args) - 2 def initialize - @board = args + @board = [Array.new(8) { Array.new(nil) }] + @first_row = [Rook.new, Knight.new, Bishop.new, Queen.new, King.new, Bishop.new, Knight.new, Rook.new] + @second_row = Array.new(8) {Pawns.new} + @board[0], @board[7] = @first_row, @first_row.reverse.map { |piece| piece.color = "black"} + @board[1], @board[6] = @second_row, @second_row.map { |piece| piece.color = "black"} end @@ -14,25 +16,29 @@ def valid_moves(piece) x_location = piece.location[0] y_location = piece.location[1] - x_difference = piece.moves[0] - y_difference = piece.moves[1] - # filter for out of bounds. out of bounds if x or y < 0 || x or y > 7 - possibilities << [(x_location + x_difference), (y_location + y_difference)] - out_of_bounds?#mathmatical possibilities + (piece.moves).each do |vectors| + + x.location += piece.moves[0] + y.location += piece.moves[1] + + next if out_of_bounds?(x_location, y_location) + possibilities << [x_location, y_location] + #mathmatical possibilities #check against piece.location possibilites.each do |coordinate| # if collision with white piece, return false - if @board[coordinate[0]][coordinate[1]] + if @board[coordinate[0]][coordinate[1]] - else - valid_moves << coordinate + else + valid_moves << coordinate # move to 0,2 and check a bunch # # figure out which directions are out of bounds - # + def out_of_bounds?(x, y) + (x < 0 || y < 0 || x > 7 || y > 7) end def find_piece(location_string) @@ -65,18 +71,21 @@ class Player #? class Piece attr_accessor :color, :moves, :location - def initialize(color) - @color = color - @moves = moves #possible moves on an empty board - @location = location + attr_reader :display + def initialize(color = "white") + color = "white" ? @icon = + # @color = color + # @moves = moves #possible moves on an empty board + # @location = location + @display_options = display[color] end end class Pawn < Piece - def initialize() - + def initialize(color = "white") + color == "white" ? @icon = "♟" : @icon = '♙' self.moves = [0,1] # pawn can only move forward. # pawn can only move 1 space, unless it's current position is the starting col. @@ -88,29 +97,41 @@ def initialize() # TODO: deal with possible_moves method class Rook < Piece - end -<<<<<<< HEAD - - -class King < Piece - moves = [[0, 1],[0, -1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]] - possibilities = [] -end - -class Queen < Piece + def initialize(color = "white") + color == "white" ? @icon = "♜" : @icon = '♖' + end -end -======= + end class King < Piece + def initialize(color = "white") + color =="white" ? @icon = "♚" : @icon = '♔' + @moves = [[0, 1],[0, -1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]] + end + + # def move_options(x, y) + # @moves.map do |vector| + # x += vector[0] + # y += vector[1] + # end + # end end class Queen < Piece + def initialize(color = "white") + color == "white" ? @icon = "♛" : @icon = '♕' + end + end ->>>>>>> 6d730b1fe19c176e220e9f00edbe550a699293ce class Bishop < Piece + def initialize(color = "white") + color == "white" ? @icon = "♝" : @icon = '♗' + end end class Knight < Piece + def initialize(color = "white") + color == "white" ? @icon = "♞" : @icon = '♘' + end end From 6576eecfbdf33cd9bf4242ad2569afa4e627abb3 Mon Sep 17 00:00:00 2001 From: Charles Green Date: Thu, 26 Mar 2015 19:06:25 -0400 Subject: [PATCH 10/19] display method should be working for a board passed in with icons and whitespace --- Board.rb | 38 +++++++++++++++++++++++++++++--------- game.rb | 12 ++++-------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/Board.rb b/Board.rb index 7cf2d92..970f95b 100644 --- a/Board.rb +++ b/Board.rb @@ -1,25 +1,45 @@ +# require "byebug" + class Board attr_reader :board def initialize - @board = [ [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9], [1,2,3,4,5,6,7,8,9] ] + # @board = [[" ♜ " , " ♞ ", " ♝ ", " ♛ ", " ♚ ", " ♝ ", " ♞ ", " ♜ "], [ " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ "], [" "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "], [" "," "," "," "," "," "," "," "], [" "," "," "," "," "," "," "," "], [" ♙ ", " ♙ ", " ♙ ", " ♙ ", " ♙ ", " ♙ ", " ♙ ", " ♙ "], [" ♖ ", " ♘ ", " ♗ ", " ♕ ", " ♔ ", " ♗ ", " ♘ ", " ♖ "]] + @board = [[" ♜ " , " ♞ ", " ♝ ", " ♛ ", " ♚ ", " ♝ ", " ♞ ", " ♜ "], [ " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ "], [nil, " ♙ ",nil,nil,nil,nil,nil,nil], [nil,nil,nil,nil,nil, " ♙ ",nil,nil], [nil,nil,nil,nil,nil,nil,nil,nil],[" ♙ ", " ♙ ", " ♙ ", " ♙ ", " ♙ ",nil , " ♙ "], [" ♖ ", " ♘ ", " ♗ ", " ♕ ", " ♔ ", " ♗ ", " ♘ ", " ♖ "]] + @display_board = @board + @col_letters = [" a ", " b ", " c "," d "," e "," f "," g "," h "] + @whitespace = " " end - def to_string - row_num = 1 + + # # turns display_board into icons and nils into spaces + # def to_icons + # @display_board.each do |row| + # row.each do |square| + # # if square equals piece, square = piece.value, else square = whitespace (" ") + # end + # end + # end + # need this to not puts 0 to last row + def display + row_num = 8 board_string = "" - @board = @board.transpose.reverse - @board.each do |col| - board_string += row_num + col.join(" ") + "\n" - row += 1 + @display_board << @col_letters + @display_board = @display_board + @display_board.each do |col| + # if value is a piece, turn into ascii + # if value is nil, turn into " " + board_string += "#{row_num} " + col.join(" ") + "\n" + row_num -= 1 end - puts board_string + puts board_string end end b = Board.new # p b.board -b.to_string + +b.display # # @board = 8 subarrays, as columns, starting at bottom left. piece objects diff --git a/game.rb b/game.rb index 59e0a37..1cc9a6b 100644 --- a/game.rb +++ b/game.rb @@ -9,6 +9,10 @@ def play # if !game_over players.each do |player| turns(player) + # clear screen + # display_board + # ask for user input + # end # end end @@ -19,14 +23,6 @@ def turns(player) board.find_piece(view.choose_piece) end -# black chars -♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜ -♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ - -# white chars - ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙ -♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖ - class view def display_board From 390deb4e8a3cf45510ca52688f7a7779da5917cc Mon Sep 17 00:00:00 2001 From: grantziolkowski Date: Thu, 26 Mar 2015 19:20:28 -0400 Subject: [PATCH 11/19] Grant merge to Charles --- Board.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/Board.rb b/Board.rb index 7fa4e27..8530051 100644 --- a/Board.rb +++ b/Board.rb @@ -12,7 +12,6 @@ def initialize def valid_moves(piece) valid_moves = [] - x = piece.location[0] y= piece.location[1] possibilities = all_possible_directions(x, y) From a3be469f591063ec6814277754b6c9cc27885cad Mon Sep 17 00:00:00 2001 From: Alexander Mihalopoulos Date: Thu, 26 Mar 2015 19:32:27 -0400 Subject: [PATCH 12/19] Add moves instance vars to each piece class --- Board.rb | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Board.rb b/Board.rb index 1f94912..b369404 100644 --- a/Board.rb +++ b/Board.rb @@ -4,8 +4,8 @@ def initialize @board = [Array.new(8) { Array.new(nil) }] @first_row = [Rook.new, Knight.new, Bishop.new, Queen.new, King.new, Bishop.new, Knight.new, Rook.new] @second_row = Array.new(8) {Pawns.new} - @board[0], @board[7] = @first_row, @first_row.reverse.map { |piece| piece.color = "black"} - @board[1], @board[6] = @second_row, @second_row.map { |piece| piece.color = "black"} + @board[0], @board[7] = @first_row, @first_row.reverse.map! { |piece| piece.color = "black"} + @board[1], @board[6] = @second_row, @second_row.map! { |piece| piece.color = "black"} end @@ -84,21 +84,25 @@ def initialize(color = "white") end class Pawn < Piece + attr_accessor :moves def initialize(color = "white") color == "white" ? @icon = "♟" : @icon = '♙' - self.moves = [0,1] - # pawn can only move forward. - # pawn can only move 1 space, unless it's current position is the starting col. - # pawn can move to left front or right front diagonal - # if opposing piece occupies that space - # stretch: en empasse move condition - # stretch: promotion + @moves = [0, 1] end + # def moves(x, y) + # x += @moves[0] + # y += @moves[1] + # end + +end + # TODO: deal with possible_moves method class Rook < Piece + attr_accessor :moves def initialize(color = "white") color == "white" ? @icon = "♜" : @icon = '♖' + @moves = [[0,1], [1,0], [-1,0], [0, -1]] end end @@ -120,6 +124,7 @@ def initialize(color = "white") class Queen < Piece def initialize(color = "white") color == "white" ? @icon = "♛" : @icon = '♕' + @moves = [[1, 0], [1,1], [1, -1], [0, -1], [0, 1], [-1, 0], [-1, 1], [-1, -1]] end end @@ -127,11 +132,13 @@ def initialize(color = "white") class Bishop < Piece def initialize(color = "white") color == "white" ? @icon = "♝" : @icon = '♗' + @moves = [[1, 1], [1, -1], [-1, 1], [-1, -1]] end end class Knight < Piece def initialize(color = "white") color == "white" ? @icon = "♞" : @icon = '♘' + @moves = [[1, 2], [1, -2], [2, 1], [2, -1], [-1, 2], [-1, -2], [-2, 1], [-2, -1]] end end From bff32f9114ff04b5307551509d18cbf15f9477c4 Mon Sep 17 00:00:00 2001 From: Alexander Mihalopoulos Date: Thu, 26 Mar 2015 19:39:40 -0400 Subject: [PATCH 13/19] Add piece classes moves --- Board.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Board.rb b/Board.rb index b369404..bb773e5 100644 --- a/Board.rb +++ b/Board.rb @@ -141,4 +141,4 @@ def initialize(color = "white") color == "white" ? @icon = "♞" : @icon = '♘' @moves = [[1, 2], [1, -2], [2, 1], [2, -1], [-1, 2], [-1, -2], [-2, 1], [-2, -1]] end - end + end \ No newline at end of file From 94a18043460867900d3809fe8c1caea183b14007 Mon Sep 17 00:00:00 2001 From: grantziolkowski Date: Thu, 26 Mar 2015 19:45:50 -0400 Subject: [PATCH 14/19] Valid Logic moves to Grant branch --- Board.rb | 2 +- game.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Board.rb b/Board.rb index 8530051..05022c3 100644 --- a/Board.rb +++ b/Board.rb @@ -9,7 +9,6 @@ def initialize @board.transpose end - def valid_moves(piece) valid_moves = [] x = piece.location[0] @@ -38,6 +37,7 @@ def all_possible_directions(x,y) end possibilities end + def out_of_bounds?(x,y) (x < 0 || y < 0 || x > 7|| y > 7) end diff --git a/game.rb b/game.rb index 999e198..3ca3c12 100644 --- a/game.rb +++ b/game.rb @@ -17,7 +17,7 @@ def turns(player) # if blank square/outside of board square view.turn_message(player) piece = board.find_piece(view.choose_piece) - moves = + # moves = piece_chosen_message(player, piece, moves) end From e717809407df97f3c4be6ac4be979f7e05346cc9 Mon Sep 17 00:00:00 2001 From: Alexander Mihalopoulos Date: Thu, 26 Mar 2015 20:02:39 -0400 Subject: [PATCH 15/19] Update for merge --- Board.rb | 69 +++++--------------------------------------------------- 1 file changed, 6 insertions(+), 63 deletions(-) diff --git a/Board.rb b/Board.rb index 803ee48..077ee77 100644 --- a/Board.rb +++ b/Board.rb @@ -3,11 +3,12 @@ class Board attr_reader :board def initialize - # @board = [Array.new(8) { Array.new(nil) }] - # @first_row = [Rook.new, Knight.new, Bishop.new, Queen.new, King.new, Bishop.new, Knight.new, Rook.new] - # @second_row = Array.new(8) {Pawns.new} - # @board[0], @board[7] = @first_row, @first_row.reverse.map! { |piece| piece.color = "black"} - # @board[1], @board[6] = @second_row, @second_row.map! { |piece| piece.color = "black"} + @board = [Array.new(8) { Array.new(nil) }] + @first_row = [Rook.new, Knight.new, Bishop.new, Queen.new, King.new, Bishop.new, Knight.new, Rook.new] + @second_row = Array.new(8) {Pawns.new} + @board[0], @board[7] = @first_row, @first_row.reverse.map! { |piece| piece.color = "black"} + @board[1], @board[6] = @second_row, @second_row.map! { |piece| piece.color = "black"} + @board = [[" ♜ " , " ♞ ", " ♝ ", " ♛ ", " ♚ ", " ♝ ", " ♞ ", " ♜ "], [ " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ "], [nil, " ♙ ",nil,nil,nil,nil,nil,nil], [nil,nil,nil,nil,nil, " ♙ ",nil,nil], [nil,nil,nil,nil,nil,nil,nil,nil],[" ♙ ", " ♙ ", " ♙ ", " ♙ ", " ♙ ",nil , " ♙ "], [" ♖ ", " ♘ ", " ♗ ", " ♕ ", " ♔ ", " ♗ ", " ♘ ", " ♖ "]] @display_board = @board @col_letters = [" a ", " b ", " c "," d "," e "," f "," g "," h "] @@ -64,69 +65,18 @@ def display b.display -# # @board = 8 subarrays, as columns, starting at bottom left. piece objects - - -# def valid_moves(piece) - - # move to 0,2 and check a bunch - # - # figure out which directions are out of bounds def out_of_bounds?(x, y) (x < 0 || y < 0 || x > 7 || y > 7) end -# valid_moves = [] -# possibilities = [] - -# x_location = piece.location[0] -# y_location = piece.location[1] - - -# (piece.moves).each do |vectors_array| - -# x_location += vectors_array[0] -# y_location += vectors_array[1] -# #filtered for out_of_bounds -# next if out_of_bounds?(x_location,y_location) -# possibilities << [x_location, y_location] - -# end - -# possibilities.each do - - -# valid_moves << -# end - class Piece attr_accessor :color, :moves, :location attr_reader :display def initialize(color = "white") - color = "white" ? @icon = - # @color = color - # @moves = moves #possible moves on an empty board - # @location = location - @display_options = display[color] end -# # filter for out of bounds. out of bounds if x or y < 0 || x or y > 7 -# def out_of_bounds?(x,y) -# (x < 0 || y < 0 || x > 7 || y > 7) -# end -# #mathmatical possibilities -# #check against piece.location - - -# possibilites.each do |coordinate| -# # if collision with white piece, return false -# if @board[coordinate[0]][coordinate[1]] - -# else -# valid_moves << coordinate - class Pawn < Piece attr_accessor :moves @@ -157,13 +107,6 @@ def initialize(color = "white") color =="white" ? @icon = "♚" : @icon = '♔' @moves = [[0, 1],[0, -1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]] end - - # def move_options(x, y) - # @moves.map do |vector| - # x += vector[0] - # y += vector[1] - # end - # end end class Queen < Piece From 0e89d76252dd080e39ce584eea5091c3133b49d9 Mon Sep 17 00:00:00 2001 From: grantziolkowski Date: Thu, 26 Mar 2015 20:26:43 -0400 Subject: [PATCH 16/19] Add BOARDLENGTH constant --- Board.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Board.rb b/Board.rb index d1f8b5c..f81ee88 100644 --- a/Board.rb +++ b/Board.rb @@ -1,7 +1,7 @@ -<<<<<<< HEAD # require "byebug" class Board + BOARDLENGTH = 8 attr_reader :board def initialize # @board = [[" ♜ " , " ♞ ", " ♝ ", " ♛ ", " ♚ ", " ♝ ", " ♞ ", " ♜ "], [ " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ ", " ♟ "], [" "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "], [" "," "," "," "," "," "," "," "], [" "," "," "," "," "," "," "," "], [" ♙ ", " ♙ ", " ♙ ", " ♙ ", " ♙ ", " ♙ ", " ♙ ", " ♙ "], [" ♖ ", " ♘ ", " ♗ ", " ♕ ", " ♔ ", " ♗ ", " ♘ ", " ♖ "]] @@ -22,7 +22,6 @@ def initialize # end # need this to not puts 0 to last row def display - row_num = 8 board_string = "" @display_board << @col_letters @display_board = @display_board @@ -30,7 +29,7 @@ def display # if value is a piece, turn into ascii # if value is nil, turn into " " board_string += "#{row_num} " + col.join(" ") + "\n" - row_num -= 1 + BOARDLENGTH -= 1 end puts board_string end From 1b81d17b69bcc42f0bf6482bef91a9426e07b326 Mon Sep 17 00:00:00 2001 From: grantziolkowski Date: Thu, 26 Mar 2015 21:04:40 -0400 Subject: [PATCH 17/19] Grant first pass at recursive move check --- Board.rb | 33 +++++++++++++++++++++++++++++---- game.rb | 28 ++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/Board.rb b/Board.rb index 107d45f..ec7faf0 100644 --- a/Board.rb +++ b/Board.rb @@ -43,7 +43,7 @@ def valid_moves(piece) valid_moves = [] x = piece.location[0] y= piece.location[1] - possibilities = all_possible_directions(x, y) + possibilities = move_one(x, y) #filter for out_of_bounds #mathematical possibilities #check against piece.location @@ -58,7 +58,34 @@ def valid_moves(piece) valid_moves end -def all_possible_directions(x,y) +def recursive_move_check(piece) + row = piece.location[0] + col = piece.location[1] + (piece.moves).each do |vector| + x = vector[0] + row + y = vector[1] + col + if match?(x, y) + count_more_in_same_direction(direction, adjacent_r, adjacent_c, count=2) + end + end + end + def match?(comp_row, comp_col) + @board[comp_row][comp_col] != nil + end + + def count_more_in_same_direction(direction, row, col, count) + @count = count + x = (piece.moves)[direction][0] + row + y = (piece.moves)[direction][1] + col + if match?(x, y) + valid_moves << [x, y] + count_more_in_same_direction(direction, x, y, count+1) + end + end + + +end +def move_one(x,y) possibilities = [] (piece.moves).each do |vector| x += vectors[0] @@ -75,8 +102,6 @@ def out_of_bounds?(x,y) def find_piece(location_string) index = string_to_index(location_string piece = @board[index[0]][index[1]] - piece.location = [index[0], index[1]] - valid_moves(piece) end def string_to_index(location_string) diff --git a/game.rb b/game.rb index 1e7ff3d..554150a 100644 --- a/game.rb +++ b/game.rb @@ -21,8 +21,15 @@ def turns(player) # if blank square/outside of board square view.turn_message(player) piece = board.find_piece(view.choose_piece) - # moves = - piece_chosen_message(player, piece, moves) + name = piece.name.downcase! + board.recursive_move_check(piece) if name == "queen" || name == "bishop" || name == "rook" + moves = valid_moves(piece) + + view.piece_chosen_message(player, name, moves) + view.display_valid_moves(player, location, move) #prompts move + view.pick_move + view.player_move_message(player, piece, move) + @@ -41,7 +48,9 @@ def choose_piece message(choose_piece_message) @choice = user_input end - + def display_valid_moves + message(piece_chosen_message) + end def pick_move(player, choice) "#{player}, move #{choice} where?" @move = user_input @@ -51,9 +60,7 @@ def pick_again message(choose_again_message) end - def display_valid_moves - message(piece_chosen_message) - end + def display_player_move message(player_move_message) @@ -78,11 +85,16 @@ def piece_chosen_message(player, piece, moves) end # @move gets sent to board - + def player_choose_move_message(player, location, move) + "ok, #{player}, move #{location} where?" + user_input + end def player_move_message(player, piece, move) - "ok, #{player}'s #{piece} #{choice} to move to #{move}" + "ok, #{player}'s #{piece} #{choice} #{location}to move to #{move}" end + + def capture_message(player, player2, piece, captured_piece, choice, move) "#{player}'s #{piece} {choice} captures #{player2}'s #{captured_piece} #{move}" end From 29eecd092a7e5656a5730b8e0f1cb10bccd56786 Mon Sep 17 00:00:00 2001 From: grantziolkowski Date: Fri, 27 Mar 2015 09:59:31 -0400 Subject: [PATCH 18/19] Grant recursive valid move draft location --- Board.rb | 62 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/Board.rb b/Board.rb index ec7faf0..bf9e15b 100644 --- a/Board.rb +++ b/Board.rb @@ -39,11 +39,10 @@ def display puts board_string end - def valid_moves(piece) - valid_moves = [] + def valid_moves(valid_moves = []) x = piece.location[0] y= piece.location[1] - possibilities = move_one(x, y) + possibilities = move_one(x, y) if possibilities = [] #otherwise you get values from the recursive move check #filter for out_of_bounds #mathematical possibilities #check against piece.location @@ -58,33 +57,29 @@ def valid_moves(piece) valid_moves end -def recursive_move_check(piece) +def recursive_move_check(piece, check_x=0, check_y=0, valid_before_bounds_check=[]) + directions = piece.moves row = piece.location[0] col = piece.location[1] - (piece.moves).each do |vector| - x = vector[0] + row - y = vector[1] + col - if match?(x, y) - count_more_in_same_direction(direction, adjacent_r, adjacent_c, count=2) + check_x += row #incrementing out from current x location + check_y += col #incrementing out from current y location + directions.each do |direction| + check_x += direction[0] + check_y += direction[1] + if free_space?(check_x, check_y) + valid_before_bounds_check << [check_x,check_y] + recursive_move_check(piece, check_x, check_y, valid_before_bounds_check) + else + return valid_moves(valid_before_bounds_check) #base case for if it runs into a guy end end - end - def match?(comp_row, comp_col) - @board[comp_row][comp_col] != nil - end - def count_more_in_same_direction(direction, row, col, count) - @count = count - x = (piece.moves)[direction][0] + row - y = (piece.moves)[direction][1] + col - if match?(x, y) - valid_moves << [x, y] - count_more_in_same_direction(direction, x, y, count+1) - end end + def free_space?(check_row, check_col) + @board[check_row][check_col] == nil + end -end def move_one(x,y) possibilities = [] (piece.moves).each do |vector| @@ -120,18 +115,22 @@ def string_to_index(location_string) b.display class Piece - attr_accessor :color, :moves, :location + attr_accessor :color, :moves, :location, :name attr_reader :display def initialize(color = "white") + @all_adjacent = [[0, 1],[0, -1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]] end end class Pawn < Piece - attr_accessor :moves + attr_accessor :moves, :first_move?, :capturing? def initialize(color = "white") color == "white" ? @icon = "♟" : @icon = '♙' - @moves = [0, 1] + moves = [[0, 1]] + moves << [0,2] if first_move? + moves << [1,1] if capturing? + name = "pawn" end end @@ -141,7 +140,8 @@ class Rook < Piece attr_accessor :moves def initialize(color = "white") color == "white" ? @icon = "♜" : @icon = '♖' - @moves = [[0,1], [1,0], [-1,0], [0, -1]] + moves = @all_adjacent - [1, 1] - [1, -1] - [-1, 1] - [-1, -1] + name = "rook" end @@ -150,7 +150,8 @@ def initialize(color = "white") class King < Piece def initialize(color = "white") color =="white" ? @icon = "♚" : @icon = '♔' - @moves = [[0, 1],[0, -1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]] + @moves = @all_adjacent + name = "king" end end @@ -158,14 +159,16 @@ def initialize(color = "white") class Queen < Piece def initialize(color = "white") color == "white" ? @icon = "♛" : @icon = '♕' - @moves = [[1, 0], [1,1], [1, -1], [0, -1], [0, 1], [-1, 0], [-1, 1], [-1, -1]] + @moves = @all_adjacent + name = "queen" end end class Bishop < Piece def initialize(color = "white") color == "white" ? @icon = "♝" : @icon = '♗' - @moves = [[1, 1], [1, -1], [-1, 1], [-1, -1]] + @moves = @all_adjacent - [0,1] - [0,-1], [1,0], [-1,0] + name = "bishop" end end @@ -173,5 +176,6 @@ class Knight < Piece def initialize(color = "white") color == "white" ? @icon = "♞" : @icon = '♘' @moves = [[1, 2], [1, -2], [2, 1], [2, -1], [-1, 2], [-1, -2], [-2, 1], [-2, -1]] + name = "knight" end end From 9dc898733cab82668daeda1fe3bd9871193b318c Mon Sep 17 00:00:00 2001 From: grantziolkowski Date: Fri, 27 Mar 2015 10:08:25 -0400 Subject: [PATCH 19/19] Add first move logic for pawn --- Board.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Board.rb b/Board.rb index bf9e15b..b4aff73 100644 --- a/Board.rb +++ b/Board.rb @@ -42,9 +42,9 @@ def display def valid_moves(valid_moves = []) x = piece.location[0] y= piece.location[1] - possibilities = move_one(x, y) if possibilities = [] #otherwise you get values from the recursive move check + valid_moves = move_one(x, y) if valid_moves = [] #otherwise you get values from the recursive move check #filter for out_of_bounds - #mathematical possibilities + #mathematical valid_moves #check against piece.location possibilites.each do |coordinate| # if collision with white piece, return false @@ -81,13 +81,13 @@ def free_space?(check_row, check_col) end def move_one(x,y) - possibilities = [] + valid_moves = [] (piece.moves).each do |vector| x += vectors[0] y += vector[1] - possibilities << [x,y] + valid_moves << [x,y] end - possibilities + valid_moves end def out_of_bounds?(x,y) @@ -127,6 +127,7 @@ class Pawn < Piece attr_accessor :moves, :first_move?, :capturing? def initialize(color = "white") color == "white" ? @icon = "♟" : @icon = '♙' + first_move? = true if self.location[0] == 1 || self.location[0] == 6 #initial row value for pawns moves = [[0, 1]] moves << [0,2] if first_move? moves << [1,1] if capturing?