Rolando Andrade
Back to projects

Built in 2019

Truco game

Truco Card Game

Truco game preview

Spain cards "Truco" game for local networks that uses RS232 as a communication protocol.

No TCP/IP. No HTTP. Only raw serial data moving between machines through RS232 ports. That was the rule I picked for a four player card game.

Networking courses focus on theory. You learn about protocols, layers and packet structures. Reading about serial communication is not the same as sending bytes through a wire. The ideas stay abstract until you build something that depends on them.

My Computer Networks I course asked for a practical project. Most students built web chat apps. I wanted something different, something that would force me to understand low level communication. I chose Truco, a traditional Venezuelan card game.

Four players, forty cards

Truco is a four player card game. People play it in Venezuela and other Latin American countries. Players form two teams. Each round, players take turns playing a card from their hand. The highest card wins the round. Win two rounds and your team scores points.

The game uses a Spanish deck with 40 cards in four suits: gold, cups, swords and clubs. The ranking does not follow the numbers. The ace of swords beats everything. The ace of clubs comes second. These special ranks add strategy beyond comparing numbers.

A “vira” card is drawn at the start and changes the game. Cards with the next number in the vira’s suit become the strongest. This wild card makes every match different.

Players can call “truco” to raise the stakes. The other team must accept or give up. If they accept, the round is worth more points. If they decline, the team that called wins. That is where bluffing comes in.

The game runs on four separate machines. One is the server. It deals the cards and sends the game state to everyone. The others connect through serial ports. Each player sees their own hand and the cards the others play. A graphical interface built with Java’s AWT draws the table.

Three layers, one direction

The code has three Maven modules. The Common module holds the shared models: cards, decks, players and game rooms. The Logic Layer has the commands that change the game state. The UI Layer handles drawing and serial communication.

Each layer has a clear job. The models know nothing about how they are shown. The logic layer knows nothing about serial ports. Each layer depends only on the one below it.

Frames between dollar signs

RS232 is an old standard for serial communication. It is older than modern networking. Data moves as raw bytes at a fixed speed, measured in baud (symbols per second). I set the connection to 1200 baud, with 8 data bits, no parity and one stop bit.

Every message uses a custom frame format. A frame starts with two dollar signs and ends with two percent signs. Between them, fields hold the sender, the receiver and the payload.

A card deal message has the sender ID, the receiver ID, then three cards. Each card is a two digit number followed by a suit code. Gold is “O”, cups is “C”, swords is “E” and clubs is “B”.

When a player plays a card, the frame carries the player ID and the card code. The receiver reads the frame, takes out the card and updates its local game state. Other messages handle truco calls, answers and handshakes.

Is everyone listening?

Keeping four players in sync over serial ports was hard. RS232 does not manage connections the way TCP does. You cannot know if the other side got your message.

So I wrote a handshake. The server sends a handshake request. Each client passes the message on when it arrives. When the server sees its own message come back, it knows the ring is complete. Only then does it deal the cards.

This relay makes sure every player is connected before the game starts. Without it, some players could miss the first deal.

Reading a stream of characters

Data arrives as a stream of characters. One message can arrive in pieces across several events. The receiver stores characters in a buffer until it sees the end marker.

When a full frame arrives, a filter finds the message type. Each prefix means a different action: a card deal, a card play, a truco call or a truco answer. The filter sends it to the right handler.

The parser turns suit codes back into internal values. It takes the player IDs and turns them into array positions. Error handling is minimal, because it assumes messages arrive intact. A production system would use checksums to check them.

Fourteen points for one ace

Card values follow complex rules. Each card has a strategy object that returns its point value. The ace of swords is worth 14 points. The ace of clubs is worth 13. The seven of swords and the seven of gold rank high. Face cards and the other numbers follow a different scale.

When the vira card appears, some cards get bonus points. If the vira is a 5 of gold, then all 6s become strong. This changing rank uses a visitor pattern. Each card checks if it matches the special condition and adjusts its value.

The highest value card wins the round. A tie between teammates goes to their team. A tie between opponents is a draw, and the next round decides.

Cards you can click

The interface uses Java’s Abstract Window Toolkit (AWT). A main frame holds the game canvas. Cards are image components loaded from resource files. Each suit has 10 card images stored as JPEGs.

Each card on screen knows its position, its size and whether it is face up or face down. A click counts when it lands inside the card’s box. Hovering makes the card a little bigger.

Each hand shows as a row of cards. You see your own cards face up. Opponents’ cards stay face down until they play them. The middle of the table shows the cards played in the current round.

Whose turn is it

The game manager tracks whose turn it is. A counter goes through players 0 to 3. When a player plays a card, the turn moves on. After all four cards are down, the game finds the winner.

The winner starts the next round. After three rounds, the scores update based on who won more rounds. If one team wins two rounds, they take the points. Then the vira and the hands reset for a new deal.

Truco calls break the normal flow. When a player calls truco, the next opponent must answer. Accepting raises the stake. Declining ends the hand at once, and the caller gets the points.

Why protocols exist

Building a multiplayer game over serial ports showed me why protocols exist. Each choice about frames, markers and message types solved a real problem. Ideas like synchronization became concrete when messages arrived out of order.

The layers paid off too. Changes to the drawing code never touched the game logic. Fixing a serial parsing bug did not need any knowledge of card ranks. Clear boundaries kept debugging manageable.

I built this with a teammate, so we had to coordinate. We split the work by layer. I handled the communication protocol and my partner focused on the game logic. The interfaces between modules were our contracts.

Nobody picks RS232 for a new project today. Low level serial communication still matters when you work with embedded systems, hardware interfaces and old equipment. The technology aged. The ideas did not.

Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build