I recently applied for a PHP programming job and they required me to do a coding challenge. One of the challenges involved simulating the card game war which I found intriguing. I "over-engineered" it as they requested, but I still don't think it was good enough for them. Is there anything I could have done better?

A live example is here:

The rules this game is based off are found here: http://www.pagat.com/war/war.html

Source files are attached.

Member #120589 commented: thanks for sharing - like it +14

Dani AI

Generated

Nice job with the simulation, — it shows solid design instincts. For time-boxed hiring exercises the biggest wins are: a small, correct core first; clear assumptions/instructions; and one or two polished extras. was right that the UI matters for polish, but for a PHP test the reviewer usually looks at correctness, clarity, and sensible tradeoffs first.

Practical backend tips you can apply quickly: represent cards as integers 0..51 (rank = card % 13, suit = floor(card / 13)), use an unbiased Fisher‑Yates shuffle, and keep deck ops as array/queue work for speed. Protect against infinite games by tracking seen states (hash both players' decks) or by enforcing a round cap and declaring a draw. Seed the RNG when you want deterministic runs for debugging.

// Fisher-Yates (simple, PHP5-friendly)
function shuffle_deck($deck) {
    $n = count($deck);
    for ($i = $n - 1; $i > 0; $i--) {
        $j = mt_rand(0, $i);
        $tmp = $deck[$i];
        $deck[$i] = $deck[$j];
        $deck[$j] = $tmp;
    }
    return $deck;
}

// basic cycle detection
$state = implode(',', $p1) . '|' . implode(',', $p2);
if (isset($seen[$state])) { /* break: loop detected */ }
$seen[$state] = true;

Testing and presentation matter as much as code. Add a tiny CLI runner + a README that lists rules you implemented (round cap, shuffle seed, tie behavior). A couple of unit tests that assert shuffle invariants and tie resolution show professionalism. Keep the architecture small (Deck, Player, Game) — avoid a heavy template system unless you need it. As hinted, sticking to common style/standards and leaving brief notes about what you would add with more time communicates good judgment to reviewers.

Recommended Answers

All 8 Replies

Member Avatar for Member #120589

the lifeboat link goes to a blank page

I tested the link before posting and it worked. Still does for me....

Member Avatar for Member #120589

Works now! May have been my system.

Ok, has a cursory look at the code. Seems OK - all OOPed.
However - I would have challeneged them with regard to PHP and told them it was a JS solution that would have been more appropriate. :(
For PHP post, I doubt whether they'd worry too much about the front end - but that's the only thing that I'd have maybe tweaked. How long did this take you to write? Were you under 'Hugh Jackman in Swordfish'-like conditions? :) Did they give you any feedback at all?

//EDIT
Like I said, the frontend - CSS: maybe use a single image sprite as opposed to individual images for all cards. Maybe some JS animations as the wars are a little abrupt.

Javascript would have been better suited for this, I agree, but it was a php challenge.

The front-end was thrown together pretty fast. I wanted to have an elaborate javascript based card animation, but that would have taken too long. A sprite for the cards was in the works, but it would have taken forever to finish creating the image and then coding all the positions into a css file, so I gave up.

They said it should take 2-4 hours, which I found was not possible for me. Maybe I just work slow, but it took 8-10 hours to complete.

They didn't give me any feedback. I was hoping for some either way, but they never responded.

Member Avatar for Member #120589

I really sympathise KK - I couldn't have produced anything that worked that well in 8-10 hours. 2-4 hours suggest that they were looking for soemthing REALLY simple. perhaps, as you mentioned, you over-engineered it - I noticed the template system. Seems you threw the kitchen sink at it - maybe this was the reason? Dunno. Either way - thanks for sharing the code - should be really useful for the rest of us plebs!

Hello, I'm looking at your code and noticed it's not really up to PHP 5 standards and that's probably why they didn't choose you?

it's not really up to PHP 5 standards

Care to elaborate?

Nevermind. Started really looking into it and saw he did all that. Why wouldn't they hire you? Maybe it was overtkill.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.