How to resolve the algorithm War card game step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm War card game step by step in the Julia programming language

Table of Contents

Problem Statement

War Card Game Simulate the card game War. Use the Bicycle playing card manufacturer's rules. Show a game as played. User input is optional. References: Related tasks:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm War card game step by step in the Julia programming language

This code simulates a card game of War using the Julia programming language.

  • The game of War is a simple card game where two players start with a deck of cards and take turns playing one card each.
  1. The player with the higher card takes both cards and adds them to their pile.
  2. If the cards are of equal value, there is a "war" and each player plays three more cards face down and one face up.
  3. The player with the higher face-up card takes all ten cards.
  4. The game continues until one player has all the cards.

The code begins by defining the suits and faces of the deck of cards, and then creates a vector of all possible cards. It also creates a dictionary that maps each card to its rank.

The next function, deal2, deals two decks of cards to two players. It shuffles the deck and then splits it into two equal decks.

The turn! function simulates a single turn of the game. It takes two decks of cards and an optional vector of pending cards. It pops the first card from each deck and compares their ranks. If one card has a higher rank, the player who played that card takes both cards and adds them to their pile. If the cards are of equal rank, there is a war and the function calls itself recursively with the pending cards added to the end of the vector.

The warcardgame function simulates the entire game of War. It deals two decks of cards to two players and then calls the turn! function until one player has all the cards. It then prints the winner of the game.

Here is an example of the output of the warcardgame function:

♠A         ♥A
Player 1 takes the cards.
♦A         ♣A
Tie!
?          ?
Cards are face down.
♥K         ♣K
Player 1 takes the cards.
♦10        ♠10
Tie!
?          ?
Cards are face down.
♦J         ♣J
Player 1 takes the cards.
♠Q         ♥Q
Tie!
?          ?
Cards are face down.
♦9         ♣9
Player 1 takes the cards.
♠8         ♥8
Tie!
?          ?
Cards are face down.
♦7         ♣7
Player 1 takes the cards.
♠2         ♥2
Player 1 takes the cards.
Player 1 wins the game.

In this example, Player 1 wins the game by taking all of Player 2's cards.

Source code in the julia programming language

# https://bicyclecards.com/how-to-play/war/

using Random

const SUITS = ["♣", "♦", "♥", "♠"]
const FACES = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" ]
const DECK = vec([f * s for s in SUITS, f in FACES])
const rdict = Dict(DECK[i] => div(i + 3, 4) for i in eachindex(DECK))

deal2(deck) = begin d = shuffle(deck); d[1:2:51], d[2:2:52] end

function turn!(d1, d2, pending)
    (isempty(d1) || isempty(d2)) && return false
    c1, c2 = popfirst!(d1), popfirst!(d2)
    r1, r2 = rdict[c1], rdict[c2]
    print(rpad(c1, 10), rpad(c2, 10))
    if r1 > r2
        println("Player 1 takes the cards.")
        push!(d1, c1, c2, pending...)
        empty!(pending)
    elseif r1 < r2
        println("Player 2 takes the cards.")
        push!(d2, c2, c1, pending...)
        empty!(pending)
    else # r1 == r2
        println("Tie!")
        (isempty(d1) || isempty(d2)) && return false
        c3, c4 = popfirst!(d1), popfirst!(d2)
        println(rpad("?", 10), rpad("?", 10), "Cards are face down.")
        return turn!(d1, d2, push!(pending, c1, c2, c3, c4))
    end
    return true
end

function warcardgame()
    deck1, deck2 = deal2(DECK)
    while turn!(deck1, deck2, []) end
    if isempty(deck2)
        if isempty(deck1)
            println("Game ends as a tie.")
        else
            println("Player 1 wins the game.")
        end
    else
        println("Player 2 wins the game.")
    end
end

warcardgame()


  

You may also check:How to resolve the algorithm Execute Brain step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the D programming language
You may also check:How to resolve the algorithm Gray code step by step in the Ruby programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Seven-sided dice from five-sided dice step by step in the OCaml programming language