Windows compatibility broken in Linux kernel (fixed)

linux_steam_broken

I’m running Ubuntu Linux and today I found that Steam did not work anymore (nor did some other Windows applications). Steam could not find my games and the store was not working. I remember installing some security updates. It turns out that Wine crashes on Ubuntu 14.04 LTS with the latest kernel update (3.13.0.59). There are several workarounds:

  1. Run “wineserver -p” in the terminal before starting Windows applications (like “steam”).
  2. Revert the kernel update with “sudo apt-get remove linux-image-3.13.0-59-generic” and “sudo update-grub”.
  3. Upgrade the kernel with “sudo apt-get install linux-image-generic-lts-vivid”.

Bug reports on the web

There are multiple places where people are discussing this bug:

I hope this helps you all… Happy gaming on Linux!

Update: A new kernel has been released, which fixes the problem: linux-image-3.13.0-61-generic (automatically installed)

 

Linux multi-player command line games (daemon-games)

snaked

Daemon-games is a set of multi-user console games for Linux. The name is a tribute to the well known Debian “bsdgames” package. A “daemon” is defined in Wikipedia as:

In multitasking computer operating systems, a daemon (/ˈdiːmən/ or /ˈdeɪmən/) is a computer program that runs as a background process, rather than being under the direct control of an interactive user. – Wikipedia

The first game in this series is multi-player snake and it is named “snaked” (the “d” is for daemon). You can see it in the screen-shot above. The next game I want to make is multi-player tetris (named “tetrisd”).

Implemented in C

This server is implemented in C. You can find the common code in the files “daemon.c” and “daemon.h”. It aims to be a simple implementation and that is why it is using the “select” call. The select call waits for input on a set of sockets (or file descriptors). When one of them has data the call returns and you can handle the socket(s) that have data. The server calculates the screens and sends them over in ANSI. It also calculates the differences between frames to reduce bandwidth. All user input is directly forwarded to the server.

Getting started with “snaked”

Running the server is as easy as:

git clone https://github.com/mevdschee/daemon-games
cd daemon-games
make
./snaked 9000

Running the client is as easy as:

wget https://raw.githubusercontent.com/mevdschee/daemon-games/master/client.sh
bash client.sh 127.0.0.1 9000

You should replace “127.0.0.1” by the IP address of the server. The number “9000” is the TCP port the server runs on. You can easily review the client to see that it actually just running netcat. You can play with the “wasd” keys and “q” for quit.

Download the source code

You can! Please play with it and send me improvements or make complete new games. I would love to accept any PR. All code is on GitHub:

https://github.com/mevdschee/daemon-games

Play peg solitaire on the Linux command line

peg-solitaire

Peg solitaire rules

Peg solitaire is a very old game that you can play alone to kill some time. Classic versions are made of a wooden playfield with indentations for either wooden pegs or glass marbles on them. The objective is to remove one peg from the center and jump pegs until you have only one peg left in the center.

When one peg jumps over another peg, then the peg that you jumped over is removed from the playing field. The peg that made the jump is not removed and is now located either 2 positions up, left, right or down from it’s initial position. Diagonal jumps are not allowed, nor are jumps over multiple pegs at once.

Peg solitaire layouts

peg-solitaire_layouts

There are several layouts of the playing field. The picture above shows the layouts that are most common. The French layout is also called the “European” layout. Special about the European layout is that you cannot start and finish in the center, but you should start one peg above the center and end one peg below the center.

Implementation in C

I have written a Peg solitaire implementation in C for the Linux command line, just like I did with the 2048 game. It is again a single C file that you can easily compile with gcc and has no external dependencies. You can start the application with the layout name (from the picture above) as the first argument. If you did not provide any arguments the most common layout (English) is chosen.

wget https://raw.githubusercontent.com/mevdschee/peg-solitaire.c/master/peg-solitaire.c
gcc -o peg-solitaire peg-solitaire.c
./peg-solitaire french

Linux ANSI codes for command-line games

When you are going to write games for the command line you will probably need ANSI control sequences. ANSI control sequences allow you to: clear the screen, show or hide the cursor, move the cursor position, change the color of the characters and their background. Here is an overview of the most useful ANSI control sequences when creating console games:

printf("\e[2J");      // clear screen and move cursor to root
printf("\e[#;#H");    // move cursor to position #,# (default 1,1)
printf("\e[#A");      // move cursor # lines up (default 1)
printf("\e[#B");      // move cursor # lines down (default 1)
printf("\e[#C");      // move cursor # columns right (default 1)
printf("\e[#D");      // move cursor # columns left (default 1)
printf("\e[?25l");    // hide cursor
printf("\e[?25h");    // show cursor
printf("\e[0m");      // reset attributes (and color)
printf("\e[1m");      // set attribute bold
printf("\e[2m");      // set attribute half-bright
printf("\e[4m");      // set attribute underscore
printf("\e[7m");      // set attribute reverse video
printf("\e[21m");     // unset attribute bold
printf("\e[22m");     // unset attribute half-bright
printf("\e[24m");     // unset attribute underscore
printf("\e[27m");     // unset attribute reverse video
printf("\e[3#m");     // set foreground color to # (0-7)
printf("\e[4#m");     // set background color to # (0-7)
printf("\e[38;5;#m"); // set foreground color to # (0-255)
printf("\e[48;5;#m"); // set background color to # (0-255)

Check the ANSI palette script to see what colors are possible:

bash_256_colors

To get a good overview of supported codes in Linux read the man page:

man console_codes

Disable input buffering and local echo in Linux

Normally input is buffered and only when you press the “enter” key the input is readable from the standard input of the application. Also input is shown on the screen. When you are programming a game you don’t want this. You need to respond directly when a key is pressed and there is no need to display the pressed keys. This can be achieved by disabling input buffering and local echo. In C this is done with the following function:

#include <termios.h>
#include <stdbool.h>

void setBufferedInput(bool enable) {
	static bool enabled = true;
	static struct termios old;
	struct termios new;

	if (enable && !enabled) {
		// restore the former settings
		tcsetattr(STDIN_FILENO,TCSANOW,&old);
		// set the new state
		enabled = true;
	} else if (!enable && enabled) {
		// get the terminal settings for standard input
		tcgetattr(STDIN_FILENO,&new);
		// we want to keep the old setting to restore them at the end
		old = new;
		// disable canonical mode (buffered i/o) and local echo
		new.c_lflag &=(~ICANON & ~ECHO);
		// set the new settings immediately
		tcsetattr(STDIN_FILENO,TCSANOW,&new);
		// set the new state
		enabled = false;
	}
}

For the full source code check out: https://github.com/mevdschee/peg-solitaire.c

Text mode 2048 game in C, algorithm explained

2048 is hot! It may be considered the next Flappy Bird. I implemented 2048 in the C programming language, so you can run it on your Linux server on the console (in text mode). You can find the 2048.c project on Github.

2048

Rules

You can move the tiles in four directions using the arrow keys: up, down, left, and right. All numbers on the board will slide into that direction until they hit the wall and if they bump into each other then two numbers will be combined into one if they have the same value. Each number will only be combined once per move. Every move a new number 2 or 4 appears. If you have a 2048 on the board you have won, but you lose once the board is full and you cannot make a move.

Algorithm

I chose to store the field in a two-dimensional array called board that is 4×4.

[2][0][8][2]          [2][4][8][4]
[0][4][0][2]  press   [4][8][4][4]
[4][0][2][2]  up key  [0][0][0][0]
[0][8][2][2]          [0][0][0][0]

Since I want the board to be addressed as board[x][y], the board consists of a set of four columns size four. So the first column is the array [2,0,4,0]. If we press the up button this should become [2,4,0,0]. The function “slideArray()” is reponsible for this. This function will “slide” the numbers in the arrays like this:

[2,0,4,0] => [2,4,0,0]
[0,4,0,8] => [4,8,0,0]
[8,0,2,2] => [8,4,0,0]
[2,2,2,2] => [4,4,0,0]

The algorithm can just use board[x] to point to a column and work directly on that. In pseudocode, this is what the algorithm does:

- walk over the array from the first to the last number
  - for each original number in the array that is not zero
    - look backwards for a target position that does not contain a zero (unless it is position zero)
      - if the target position does not contain the original number use the next position
    - if the target position is different from the original position
      - add the number to the number on the target position
      - replace the original number by zero

The above algorithm executed will do all transformations, it will:

[2,2,2,2] => [4,4,0,0]
[0,8,2,2] => [8,4,0,0]

But there is a problem, it will also do this:

[2,2,4,4] => [8,4,0,0]

The two’s are merged into a four and then the first four is merged into that making it an eight. This is wrong. It should be doing this:

[2,2,4,4] => [4,8,0,0]

This is avoided by adding a “stop” variable that will be initially set to zero, but when a merge has been done it will be set to the merge position plus one. This will make sure any next slide will stop before it merges into this number again, since double merges are not allowed.

To prevent complex programming I use a function rotateBoard that rotates the board 90 degrees counter-clockwise. This allows the moveLeft(board) to be implemented as:

rotateBoard(board);
moveUp(board);
rotateBoard(board);
rotateBoard(board);
rotateBoard(board);

As long as you rotate four times in total everything works as expected. This method is not very efficient, but reduces the complexity of the code.

Compiling and running

Since 2048.c is a single C file it is easy to get running, just execute the following commands:

wget https://raw.githubusercontent.com/mevdschee/2048.c/master/2048.c
gcc -o 2048 2048.c
./2048

This will run on most machines. If not, then execute the following command to install the compiler:

sudo apt-get install build-essential

Further reading

Free multi-player games for OSX & Linux

Arcade style computer games can be fun. Even better when you can play them with your friends online. And it is really awesome when these games do not cost a dime! In this post I review my three favorite free cross platform multi-player arcade games.
BitFighter
A space shooter with very minimal vector graphics. Easy to get the hang of this game. Especially capture the flag mode is awesome. You win when you have good team play. Really light game, runs on virtually any hardware. Not as esthetically pleasing as the other two, unless you once were a big Asteroids video game fan.
TeeWorlds
A very well (vector) drawn cartoony shooter in 2D. It is a bit hard to learn the controls, since it is operated with mouse and keyboard together. Big differences in skill level might ruin the fun, but if the players are equally skilled this is a great game!
Armagetron Advanced
A 3D vector game based on Tron. A great game that is controlled by two keys: turn-left and turn-right. More fun than you can imagine. Has some awesome game modes as well. Not as user friendly as the other two games, but since the game is easy to learn it provides instant fun.
That’s all for now… have fun playing!