← All posts

Connect Three with a KUKA Robot Arm

Final project in the Robotics module at Hochschule Karlsruhe: programming an industrial KUKA robot arm in KRL to play a Connect Four–style game against humans or itself. Graded 1.0.

roboticskukakrlgame-aiteam-project

In the summer semester 2026, I took Robotics as an elective in my Computer Science studies at Hochschule Karlsruhe, taught by Prof. Dr. Björn Hein. For the final project, my teammates Luca, Noel and I programmed a KUKA industrial robot arm to play Connect Three, a smaller variant of Connect Four. We received a 1.0, the best possible grade.

The Game

Connect Three is played on a horizontal 6×4 board, and each player has 12 stones. Whoever gets three stones in a row (horizontal, vertical or diagonal) first wins. There are two variants:

  • Gravity on: like classic Connect Four, a stone “falls” to the lowest free row of its column.
  • Gravity off: any free field on the board can be chosen.

The robot handles the whole game by itself: it moves the stones, tracks the board state, checks for a winner and, when it plays, decides on its own move.

The Setup

  • Robot: KUKA KR AGILUS sixx, a compact six-axis arm, with a KR C4 compact controller
  • Gripper: controlled through KUKA’s GripperTech library, which also checks that each grip succeeded
  • Board: a 6×4 grid, with a supply area of 12 stones for each player next to it
  • Interface: the KUKA smartPAD, the controller’s handheld teach pendant

Everything is written in KRL (KUKA Robot Language) and runs directly on the robot controller.

Our KRL program open in the editor on the KUKA smartPAD

What We Built

Game Modes and Controls

At the start of each match, the smartPAD asks for the game mode (human vs. human, human vs. robot or robot vs. robot), the gravity mode and the starting player. Human players then choose their move through softkey dialogs. A smartPAD dialog only fits four softkeys, so we split column selection across two pages (columns 1–3 with “next”, columns 4–6 with “back”). Invalid moves, like a full column or an occupied field, show an error and ask again. Every move is logged as a message, for example “Player 1 places on B3”.

The Robot’s Strategy

When it’s the robot’s turn, it picks a move with a multi-step heuristic:

  1. Win: try every legal move and take one that wins immediately.
  2. Block: if the opponent could win with their next move, block it.
  3. Score all remaining moves: building its own lines counts fully, disrupting the opponent’s lines counts half, the middle columns get a small bonus, and in gravity mode columns that are already high get a penalty.
  4. Vary: instead of always playing the single best move, the robot picks randomly among all moves close to the top score. This leaves more room for variation in the opening.

KRL has no random number generator, so we built our own seed from the system clock and a hash of the current board state. That way the robot doesn’t play the same game twice.

Win Detection

After every move, the program only checks the four lines through the stone that was just placed (horizontal, vertical and both diagonals), counting matching stones in both directions. There’s no need to scan the whole board.

Motion

All board and supply positions are calculated at runtime from row and column, relative to a single taught reference point, so we only had to teach a handful of points instead of every field.

  • In gravity mode, the gripper first enters the column from above and then slides horizontally to the target row in one continuous spline motion, so it never hits stones already in the column.
  • Wherever no exact stop is needed, like between lifting a stone and moving it across, we used approximate positioning (C_DIS). This blends the movements into one smooth path and saves cycle time. Right before the gripper opens or closes, the robot stops exactly.
  • After every match (win, draw or abort), the robot automatically clears the board and sorts each stone back into its player’s supply area.

Abort Button

A physical button can end a match at any time. Its interrupt handler only sets a flag. It never starts a movement or opens a dialog itself, because an interrupt can fire in the middle of a running motion. The main loop checks the flag at a safe point before the next move, asks for confirmation and then cleans up the board.

Testing

We tested all six combinations of game mode and gravity mode, plus every possible ending: wins in every direction, a full board ending in a draw, and aborting a match with the button. In every case, the robot played correctly and cleared the board afterwards. We also recorded a video of a full match, from mode selection to the final cleanup.

What We Learned

  • Industrial tooling is a different world. Programming in KRL on the smartPAD meant doing without many things that are standard in modern software development, like autocomplete, proper debugging or version control. Getting used to it was a real learning experience, and that kind of tooling is common in industry.
  • Backups and documentation matter. We shared the robot cell with other groups, and more than once our program was gone from the controller, overwritten by another group. Because we kept our own backups, we never lost any code. But manual setup steps like calibrating the board’s base coordinate system weren’t backed up, and we had to redo them every time. That cost us a lot of time and taught us to document everything, including configuration.
  • Seeing your code move something real is incredibly motivating. Watching the robot play a full game we had programmed from scratch was the highlight of the semester.

What Could Come Next

  • A minimax search in place of the one-move lookahead. With only 24 fields, the board is small enough to search almost completely.
  • Adjustable difficulty, by making the robot’s tolerance for “good enough” moves configurable.
  • Per-group storage for programs and calibration data on shared robot cells, so groups can’t overwrite each other’s setup.