This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def single_run_bubble_sort(data) | |
| i = 0 | |
| while i < (data.count - 1) | |
| if unsorted?(data[i], data[i + 1]) | |
| data[i], data[i+1] = data[i+1], data[i] | |
| end | |
| i += 1 | |
| end | |
| data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def prompts | |
| puts "Hi. Give me a file to shuffle." | |
| filename = gets.chomp | |
| file = load(filename) | |
| shuffled = txt_shuffle(file) | |
| save(shuffled, filename) | |
| end | |
| def load(filename) | |
| file = File.read(filename).split("\n") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def beats?(chooser) # check for when guesser "wins" / beats chooser | |
| !chooser.board.include?("_") && @hangcount != 0 | |
| end | |
| @guesser.guess(@chooser) | |
| @chooser.check_opponent_guess(@guesser) | |
| puts "#{@guesser.hangcount} hang count" | |
| end | |
| if @guesser.beats?(@chooser) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def exp2(base, power) | |
| case power | |
| when 0 | |
| 1 | |
| when 1 | |
| base | |
| else | |
| exp2(base, (power / 2.0).floor) * exp2(base, (power / 2.0).ceil) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def exp2(base, exponent) | |
| return 1 if exponent == 0 | |
| if exponent.even? | |
| even_recurse = exp2(base, exponent / 2) | |
| even_recurse * even_recurse | |
| else | |
| odd_recurse = exp2(base, (exponent - 1) / 2) | |
| base * (odd_recurse * odd_recurse) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def new_move_positions(old_position) | |
| shifts = ([1,-1].product([-2,2]) + [2,-2].product([-1,1])) | |
| shifts.map!{ |vector| Vector.elements(vector)} | |
| new_positions = [] | |
| shifts.each do |shift| | |
| new_positions << (shift + old_position) | |
| end | |
| new_positions.select do |position| | |
| position[0] < 8 && position[0] > -1 && position[1] < 8 && position[1] > -1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| BACK_ROW = ["Rook","Knight","Bishop","Queen","King","Bishop","Knight","Rook"] | |
| def set_back_row | |
| bothsides = [[0, :black], [7, :white]] | |
| bothsides.each do |side| | |
| BACK_ROW.each_with_index do |piece, piece_index| | |
| Object.const_get(piece).new(self, [side[0], piece_index], side[1]) | |
| end | |
| end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def dup | |
| board_copy = Board.new | |
| piece_set.each do |piece| | |
| piece.class.new(board_copy, piece.position, piece.color) | |
| end | |
| board_copy | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'matrix' | |
| require_relative "piece" | |
| class SlidingPiece < Piece | |
| def moves | |
| move_possibilities = [] | |
| self.class::SLIDE_DIRECTIONS.each do |direction| | |
| (1..7).each do |magnitude| | |
| pos_move = (magnitude * Vector.elements(direction) + Vector.elements(@position)).to_a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def assert_legal_direction(dir) | |
| unless dir.all?{|coord| coord.abs == 1} | |
| raise InvalidMoveError.new "tried to move too many squares" | |
| end | |
| unless (dir[0] + dir[1]).even? | |
| raise InvalidMoveError.new "did not move diagonally" | |
| end | |
| unless king |
OlderNewer