Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
VGSources
Blog

Implementing Klondike Solitaire in Java: A Complete Guide

A practical guide to implementing three-card-draw Klondike Solitaire in Java, from the 52-card deal and legal moves to Swing or JavaFX rendering and tests.
WorldJava: A Complete Guide Length6 min Posted Quest giverVGSources Team
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This guide builds a playable three-card-draw Klondike Solitaire game: a 52-card deck, seven tableau columns, a stock and waste pile, and four foundations. The rules and initial deal follow Kansas State University’s CIS 300 assignment; the Java architecture below is a practical way to keep those rules separate from the interface. Choose Swing or JavaFX based on your project and experience—neither is required for the game logic.

Choose a ruleset before writing code

“Solitaire” names a family of games. This implementation uses the Klondike rules in the Kansas State University CIS 300 assignment (Fall 2024), including drawing up to three cards from the stock. Decide and document any variation before coding; otherwise, the interface and rules engine can end up enforcing different versions of the game.

As an Amazon Associate I earn from qualifying purchases.

Initial deal

Shuffle a standard 52-card deck and deal seven tableau columns. Column one has one card, column two has two, continuing through column seven with seven. In each column, only the top card is face up. The other 24 cards become the stock. Start with four empty foundations and an empty waste pile.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Rules to enforce

  • Foundations: Start each foundation with an Ace, then build upward by rank in the same suit through King.
  • Tableau: Build downward by rank, alternating red and black. A sequence moved as a unit must preserve that ordering.
  • Empty columns: Only a King, or a valid sequence beginning with a King, may be placed in an empty tableau column.
  • Face-down cards: When a move uncovers the top card of a tableau column, turn that card face up.
  • Stock and waste: Draw up to three cards at a time. Define the order in which drawn cards become playable and how the stock is recycled, then apply that behavior consistently.
  • Win condition: The game is won when all four foundations contain complete Ace-through-King suit sequences.

The assignment supplies the deal and core move rules. Make stock recycling and draw behavior explicit in your implementation and tests, rather than letting incidental UI behavior determine the rules.

Model the game independently of the interface

Keep the authoritative game state in ordinary Java objects. The rules assignment distinguishes game logic from graphics, and the exact-title tutorial likewise introduces Card and Deck classes. The class boundaries below are recommendations, not framework requirements.

Card and deck

A Card can hold a rank, suit, and whether it is face up. Represent rank with an enum ordered from Ace through King, and suit with an enum; derive color from suit so that a card’s color cannot contradict its suit. A Deck can create the 52 unique cards, shuffle them, and provide them in a defined order for dealing.

Piles and game state

Represent the stock, waste, four foundations, and seven tableau columns as ordered collections of cards. A GameState can own those piles and expose operations such as drawing, moving a sequence, recycling the waste, and checking for a win. Keep pile-specific validation in the model or a rules service, not in mouse-event code.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a clear convention for collection order—for example, the end of a list is the top of a pile—and apply it everywhere. This avoids confusing bugs when drawing, moving, or rendering cards. Keep card face state in the model so a redraw or a new view does not accidentally change the rules.

Rank #2
Classic Klondike Solitaire Card Game
  • Klondike Solitaire. It’s one of the most popular classic card games in the world, and the best-known solitaire card

Implement legal moves as state transitions

For each action, first identify the source and destination, validate the proposed transition against the current state, and mutate the state only if the move is legal. Returning a success result, or a structured failure reason, lets the interface respond without duplicating rule checks.

Stock draw and recycle

When the player draws, remove up to three cards from the stock and place them in the waste in the ruleset’s defined order. Handle a stock with fewer than three cards by drawing only those remaining. When the stock is empty, recycle the waste according to the chosen rule—for example, moving its cards back into the stock in the correct order. Test the order explicitly: an incorrect reversal can make cards appear in a different sequence on each pass.

Tableau moves

A move onto a nonempty tableau pile is legal only when the moving sequence’s first card is one rank lower than the destination’s top card and has the opposite color. The sequence itself must already descend by rank and alternate color. An empty column accepts only a King-led sequence. When a move leaves a tableau card exposed, turn that card face up as part of the same state transition.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Foundation moves

A foundation accepts an Ace when empty. Otherwise, the moved card must be exactly one rank above the foundation’s top card and match its suit. Apply the same validation whether the source is the waste or a tableau column.

Rank #3
Solitaire
  • Draw 1 and Draw 3 Solitaire game modes
  • Portrait and Landscape support
  • Intuitive Gameplay with "Tap to Play" mode
  • Customizable card faces, card backs and backgrounds
  • Classic and Daily Challenge Mode

Centralize the checks

Give each move type a focused validation path, such as canMoveToTableau and canMoveToFoundation, with shared helpers for rank, color, and sequence checks. This makes it easier to test the rules and prevents the screen from permitting a move that the model would reject—or vice versa.

Choose Swing or JavaFX for the view

The game model does not depend on a GUI toolkit. The target tutorial assumes basic Swing familiarity, while the Java examples reviewed include JavaFX projects. Pick the toolkit you already know or that best suits the interactions you want; the examples do not establish a universal winner.

Choice What the examples establish Practical consideration
Swing The exact-title tutorial lists basic Swing familiarity as a prerequisite. See the tutorial. Suitable if you already know Swing. Follow the setup instructions for your own project.
JavaFX Two example projects use JavaFX. One documents an Eclipse setup with Java 21 and JavaFX 21 and says to add the JavaFX dependency; another documents JDK 11 or higher and a Gradle run command. These are project-specific configurations, not general minimum-version guidance. See the prmr example and the other JavaFX example. Account for the JavaFX dependency and use the instructions and versions specified by the project you choose.

Render piles, not one giant card

A Solitaire board shows several piles at once, often with partially overlapping tableau cards. Use a board-oriented layout or custom drawing for that view. Java AWT’s CardLayout does something different: it switches between components in a stack while showing one at a time. Its name does not mean it lays out playing cards on a board. See the AWT CardLayout documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Route input through the model

  1. Identify the selected card or sequence and its source pile.
  2. Identify the intended destination pile.
  3. Ask the game model to validate and apply the move.
  4. Refresh the view from the resulting game state, whether or not the move succeeded.

This keeps clicks, drag-and-drop, or other input methods from becoming separate implementations of the rules. It also lets the same model be exercised without opening a window.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Test the rules before polishing the board

Test transitions in the model, not just whether cards look right on screen. The rules assignment gives concrete cases to cover, and the prmr example documents JUnit tests and headless drivers, showing that nonvisual testing is practical for this kind of project.

  • Verify the initial deal: seven columns with one through seven cards, one face-up card per column, and 24 cards in stock.
  • Check drawing when three cards remain and when fewer than three remain.
  • Check that recycling an empty stock preserves the intended waste order.
  • Accept and reject tableau moves for both rank and alternating-color rules.
  • Accept a King-led sequence in an empty column and reject a non-King.
  • Check valid and invalid foundation moves, including suit and rank.
  • Verify that moving the top card exposes and flips the newly uncovered tableau card.
  • Check the win condition when foundations are complete and when they are not.

Keep deterministic test setups available so each test can construct a known arrangement rather than depend on a random shuffle. You can then test a difficult move or end condition directly, without reproducing it through a full game.

Build the game in small, verifiable stages

  1. Create the card and deck model. Confirm that a fresh deck contains each rank-and-suit combination once, then test shuffling and dealing.
  2. Implement the piles and initial deal. Check the seven column sizes, face-up states, and remaining stock count against the selected rules.
  3. Add move validation and transitions. Implement stock, tableau, waste, and foundation actions in the game model, with tests for accepted and rejected moves.
  4. Connect the view. Render the current state and send user actions through the model rather than changing piles directly in event handlers.
  5. Add feedback and finishing behavior. Show rejected moves clearly, make the win state visible, and verify that the interface stays synchronized after every accepted action.

For JavaFX setup, follow the chosen project’s own dependency and build instructions: the examples differ in their documented JDK and JavaFX configurations, so neither should be treated as a universal current minimum. For Swing, use the setup appropriate to your Java project and avoid adding a GUI dependency unless your chosen design requires one.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick Recap

Bestseller No. 3
Solitaire
Solitaire
Draw 1 and Draw 3 Solitaire game modes; Portrait and Landscape support; Intuitive Gameplay with "Tap to Play" mode

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More quests from Patch Notes

  1. How to Set Up a RedM (RDR2) Server in 2026: License Key, txAdmin and server.cfg, Step by StepBlog11min
  2. Best RedM (RDR2) Server Hosting in 2026: Comparing Four Hosts on Slots, Memory and PriceBlog10min
  3. How to Host a Mindustry Server in 2026: server-release.jar, Port 6567 and the Commands That MatterBlog7min
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.