splatoperator
splatoperator

Reputation: 235

Modeling the card game War in Ruby and keep getting an infinite loop. Could someone help me understand what's going wrong in my control flow

I'm new to Ruby and only started a few weeks ago. I'm trying to create an algorithm that simulates how the game is played, but I'm getting stuck in an infinite loop. Something is wrong with my control flow and I just don't see it. Right now, I'm not adding in the loop for the war part and just trying to take the card out of the end of the array and put it back at the beginning. Also, just to note, I don't use .shuffle! because we were told not to. This is part of my algorithms class. Also, I'm not cheating, we were told to use Stack Overflow and other resources.

Can someone help me understand where I'm going wrong? Thanks!

deck = (2..14).to_a*4


deck.sort! {|a, b| rand <=> rand}
player1 = deck.values_at(0..25)
player2 = deck.values_at(26..51)
print "Player 1's cards: #{player1}"
print "Player 2's cards: #{player2}"

hand_count = 0

until player1.count == 52 || player2.count == 52
    hand_count += 1
    state = player1[-1] <=> player2[-1]
    if state == -1
        player2.unshift(player2.pop)
        player2.unshift(player1.pop)
        puts "Player 2 won hand #{hand_count}"
    elsif state == 1
        player1.unshift(player1.pop)
        player1.unshift(player2.pop)
        puts "Player 1 won hand #{hand_count}"
    else
        player2.unshift(player2.pop)
        player1.unshift(player1.pop)
        puts "It's a tie"
    end
end

puts "Player 1 wins" if player1.length == 52
puts "Player 2 wins" if player2.length == 52

Upvotes: 0

Views: 428

Answers (1)

Matthew Leonard
Matthew Leonard

Reputation: 2005

I think the problem is that if both player 1 and player 2 each have a card of value "12", the other will never get to 52.

The 12's will just be put back in the decks of each player. One player needs to win when there is a tie.

UPDATE

Here's a working example

deck = (2..14).to_a*4


deck.sort! {|a, b| rand <=> rand}
player1 = deck.values_at(0..25)
player2 = deck.values_at(26..51)
print "Player 1's cards: #{player1}"
print "Player 2's cards: #{player2}"

hand_count = 0

until player1.count == 52 || player2.count == 52
    hand_count += 1
    state = player1[-1] <=> player2[-1]
    if state == -1
        player2.unshift(player2.pop)
        player2.unshift(player1.pop)
        puts "Player 2 won hand #{hand_count}"
    elsif state == 1
        player1.unshift(player1.pop)
        player1.unshift(player2.pop)
        puts "Player 1 won hand #{hand_count}"
    else
        player1_war_cars = player1.pop(3)
        player2_war_cars = player2.pop(3)

        if player1_war_cars.first < player2_war_cars.first
            player1.unshift(player1_war_cars)
            player1.unshift(player2_war_cars)

            player1 = player1.flatten
            puts "Player 2 won hand #{hand_count}"
        elsif player1_war_cars.first > player2_war_cars.first
            player2.unshift(player1_war_cars)
            player2.unshift(player2_war_cars)

            player2 = player2.flatten
            puts "Player 1 won hand #{hand_count}"
        else
            player1.unshift(player1_war_cars)
            player2.unshift(player2_war_cars)

            player1 = player1.flatten
            player2 = player2.flatten
            puts "It's a tie"
        end
     end
 end


puts "Player 1 wins" if player1.length == 52
puts "Player 2 wins" if player2.length == 52

Upvotes: 1

Related Questions