From: Sharon Phillips Date: 2007-11-30T20:53:06+09:00 Subject: Re: Trouble with a while loop Hi Bashka, I've had a it of a shot at your code, mainly looking at reducing the repetition and catching a few of the cases that might cause an endless loop (like if no-one bid higher than the valuation. It looks from your code like you newish to programming, so there's a fair chance I've used some constructs not familiar to you. If so, let us know and someone will help explain. This is only a quick attempt (I'm supposed to be unpacking boxes - we've just moved house) and a bit messy. Here tis: class Bidder attr_reader :still_bidding, :num def initialize num, reserve_price, max_bid @num= num @max_bid= max_bid @margin= @max_bid- reserve_price @bid= reserve_price+ @margin/2 @still_bidding= @max_bid> reserve_price end def current_bid @bid end def raise_bid @bid+= @margin/4 @still_bidding= @bid<= @max_bid end end @bidders= [] def get_num msg print "#{msg} >" gets.to_i end #we're only interested in bidders who haven't yet reached their max def bidders_left @bidders.select {|bidder| bidder.still_bidding} end valuation= get_num "Enter the valuation" reserve_price= get_num "Enter the reserve price" #set up the bidders (1..(get_num "How many bidders?")).each do |index| max_bid= get_num "How much is invester #{index} willing to spend?" @bidders << Bidder.new(index, reserve_price, max_bid) end #no-one's won yet, and we haven't started winner= nil round= 0 while bidders_left.any? && winner.nil? print "\nRound #{round+=1}: " #get the current lowest bidder current_bidder= bidders_left.sort{|a,b| a.current_bid <=> b.current_bid}.first bid= current_bidder.current_bid puts "Investor #{current_bidder.num} is bidding $#{bid}" #either they won, or they raise their bid if bid> valuation winner= current_bidder else current_bidder.raise_bid end end if winner.nil? puts "No one bid high enough. Property passed in" else puts "** This property has been sold to Invester #{current_bidder.num} **" end On 30/11/2007, at 8:00 PM, Bashka Dooble wrote: > Hi, > > Please forgive if this forum is not the right one for this post. > > I'm writing a small ruby program that implements sale of a plot of > land > to 5 investors through English Auction. In an English auction, the > auctioneer begins the auction with the reserve price (lowest > acceptable > price) and then takes larger and larger bids from the customers > until no > one will increase the bid. The item is then sold to the highest > bidder. > > The logic that i'm using to decide which investor to bid first is > this: > 1. Each investor has a maximum willing price that they can't exceed. > 2. First find out the difference/margin btw an investor's willing > price > and the reserve price > 3. The investor that has the lowest margin bids first > 4. An investor's first bid = reserve price + half of margin > 5. Subsequent bids = first bid + 25% of margin i.e. half of the other > half of margin > > The code as it is now is attached. > > Thanks, > > Bashka > > Attachments: > http://www.ruby-forum.com/attachment/1072/engBid42.rb > > -- > Posted via http://www.ruby-forum.com/. >