Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Write the shortest game of alak

Alak was invented by the mathematician A. K. Dewdney, and described in his 1984 book Planiverse. The rules of Alak are simple:

Alak is a two-player game played on a one-dimensional board with eleven slots on it. Each slot can hold at most one piece at a time. There's two kinds of pieces, "x" and "o". x's belong to one player, o's to the other. The initial configuration of the board is:

 xxxx___oooo

The players take turns moving. At each turn, each player can move only one piece, once. A player cannot pass up on his turn. A player can move any one of his pieces to the next unoccupied slot to its right or left, which may involve jumping over occupied slots. A player cannot move a piece off the side of the board.

If a move creates a pattern where the opponent's pieces are surrounded, on both sides, by two pieces of the mover's color (with no intervening unoccupied blank slot), then those surrounded pieces are removed from the board.

The goal of the game is to remove all of your opponent's pieces, at which point the game ends. Removing all-but-one ends the game as well, since the opponent can't surround you with one piece, and so will always lose within a few moves anyway.

I found this game online and was wondering: can it be golfed?

Rules of the golf

  • Your code must follow all the rules in the game, handling captures, proper moving, etc. (only exception is you don't have to add a bot, but you must have both players controlled somehow, and one player must be human).
  • Input must be move piece at tile X to tile Y, or quit. For example, you can use 1 4 to say 'move this piece at tile 1 to tile 4'. quit would end the program, although using Control-C would be acceptable. You also have to check if a move is invalid (by going outside the board or moving somewhere that you would have to cross over unoccupied spaces to get to or sending a message that is not a pair of tiles or quit).
  • Outputs for players winning and invalid must be P1 WINS, P2 WINS, and INVALID, respectively. (All of these are 7 characters.)
  • Output must show the board. That's all that's required.
  • It doesn't matter if you use any aids like numbered tiles or other pieces.
  • The challenge ends if:

    • One answer gets 50 votes
    • One answer remains the top-voted for 3 weeks, and no other answers were posted in that time

and the challenge has at least 3 answers (so there's some real competition).

Rules of the game

  • The player on the left must start first.
  • Only one piece occupies a square at a time. You move the piece left or right until it hits an unoccupied space. The board does not wrap, and you can't move through unoccupied areas. For example:
    • xoo__o. Here, the x moving right would change the board to _oox_o.
    • xxooo_. Here, the farthest-left x could move to yield _xooox, which captures the os, leaving _x___x.
    • x__oox. Here, the os are not captured (there is still a gap). Capture is not possible because you can't move through unoccupied spaces. The x on the left could only move one space, because there are no other pieces in between (leaving _x_oox).
  • Multiple adjacent pieces can be captured at once if the group is surrounded by the opponent's pieces. E.g. from x_oox to _xoox will capture both os and result in _x__x.
  • If after a move, you first capture the opponent's pieces, before checking if your own piece should be remove. Take two examples:
    • o_oxx to oxox_. First, the second o is captured, ox_x_, so the first x remains on the board.
    • o_oox to oxoo_. This time, none of the os are captured, so the x is captured instead.
    • If you have only one piece, the game ends, because you can't capture with just one piece.

Let the games begin! I look forward to seeing what you come up with.

Answer*

Draft saved
Draft discarded
Cancel
3
  • \$\begingroup\$ I tested it, it works well, and nothing was left out. Good job! \$\endgroup\$ Commented Apr 3, 2015 at 19:10
  • \$\begingroup\$ You can save a few bytes by replacing printf("INVALID"); with puts("INVALID");, o<2||x<2 with o<2|x<2 and printf(b);while(!q){ with for(printf(b);!q;){ \$\endgroup\$ Commented May 5, 2015 at 7:19
  • \$\begingroup\$ 494 bytes \$\endgroup\$ Commented Sep 22, 2020 at 21:42

AltStyle によって変換されたページ (->オリジナル) /