This file is indexed.

/usr/share/doc/cairo-5c/connect-3.5c is in cairo-5c 1.14.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
 * Copyright © 2012 Keith Packard
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 *
 * The Original Code is the cairo graphics library.
 *
 * The Initial Developer of the Original Code is Keith Packard
 *
 * Contributor(s):
 *      Keith Packard <keithp@keithp.com>
 */

/*
 * Connect-3 in a 3x4 grid
 */

autoload Mutex;
autoimport Cairo;
autoimport PRNG;
autoimport Nichrome;
autoload Nichrome::Button;
autoload Nichrome::Label;
autoload Nichrome::Box;

import Widget;
import Nichrome;
import Button;
import Button;
import Box;
import Label;

dev_srandom (32);

typedef int piece_t;

piece_t piece__ = 0;
piece_t piece_x = 1;
piece_t piece_o = 2;

int cols = 3;
int rows = 4;
int row = min(cols,rows);

typedef piece_t[rows, cols] board_t;

typedef struct {
    int x, y;
} position_t;

/*
 * Encode the current game state, which is
 * either 'in progress' or 'player foo has
 * won with pieces from p1 to p2'
 */
typedef union {
    void    playing;
    struct {
	piece_t	    winner;
	position_t  p1, p2;
    } finished;
} status_t;

typedef status_t game_status_t;
/*
 * Game state
 */
typedef struct {
    mutex	    lock;
    bool	    quiting;
    semaphore	    waiter;
    board_t	    board;
    status_t	    status;
    piece_t	    turn;
    piece_t	    computer;
} game_t;

void
set_color (cairo_t cr, piece_t piece)
{
    switch (piece) {
    case piece_x:
	set_source_rgba (cr, 1, 0, 0, 1);
	break;
    case piece_o:
	set_source_rgba (cr, 0, 0, 1, 1);
	break;
    case piece__:
	break;
    }
}

/*
 * Set up cr to draw the game board
 */
void
scale_board (cairo_t cr)
{
    scale (cr, Cairo::width (cr), Cairo::height (cr));
}

void
draw_grid (cairo_t cr)
{
    set_source_rgba (cr, 0, 0, 0, 1);
    for (int line = 0; line < cols - 1; line++)
    {
	move_to (cr, (line + 1) / cols, .05);
	line_to (cr, (line + 1) / cols, rows/cols - .05);
    }
    for (int line = 0; line < rows - 1; line++)
    {    
	move_to (cr, .05, (line + 1) / rows * rows/cols);
	line_to (cr, .95, (line + 1) / rows * rows/cols);
    }
    stroke (cr);
}

void
draw_piece (cairo_t cr, piece_t piece)
{
    new_path (cr);
    set_line_width (cr, 0.2);
    set_line_cap (cr, line_cap_t.ROUND);
    switch (piece) {
    case piece__:
	return;
    case piece_x:
	move_to (cr, -.4, -.4);
	line_to (cr, .4, .4);
	move_to (cr, .4, -.4);
	line_to (cr, -.4, .4);
	break;
    case piece_o:
	arc (cr, 0, 0, 0.4, 0, 2 * pi);
	break;
    }
    set_color (cr, piece);
    stroke (cr);
}

/*
 * Draw the whole board
 */
void
draw_pieces (cairo_t cr, &board_t board)
{
    for (int y = 0; y < rows; y++)
	 for (int x = 0; x < cols; x++)
	 {
	    save (cr);
	    translate (cr, (x + 1/2) / cols, (y + 1/2) / rows * rows/cols);
	    scale (cr, 1/(cols * 4/3), 1/(rows * 4/3) * rows/cols);
	    draw_piece (cr, board[y,x]);
	    restore (cr);
	 }
}

/*
 * Draw a line through the three winning pieces
 */
void
draw_win_line (cairo_t cr, &status_t status)
{
    union switch (status) {
    case finished f:
	if (f.winner == piece__)
	    break;
	set_color (cr, f.winner);
	real x1, x2, y1, y2;
	if (f.p1.x == f.p2.x)
	    x2 = x1 = (f.p1.x + .5) / cols;
	else {
	    if (f.p1.x < f.p2.x) {
		x1 = f.p1.x / cols + 0.05;
		x2 = (f.p2.x + 1) / cols - 0.05;
	    } else {
		x1 = (f.p1.x + 1) / cols - 0.05;
		x2 = f.p2.x / cols + 0.05;
	    }
	}
	if (f.p1.y == f.p2.y)
	    y2 = y1 = (f.p1.y + .5) / rows;
	else {
	    y1 = f.p1.y / rows + 0.05;
	    y2 = (f.p2.y + 1) / rows - 0.05;
	}
	move_to (cr, x1, y1 * rows/cols);
	line_to (cr, x2, y2 * rows/cols);
	stroke (cr);
	break;
    case playing:
	break;
    }
}

/*
 * Figure out the current game status
 */
status_t get_status (&board_t board)
{
    position_t	p1, p2;
    
    bool in_a_row (piece_t piece, position_t p, position_t d) {
	p1 = p;
	for (int i = 0; i < row; i++) {
	    if (board[p.y,p.x] != piece)
		return false;
	    p2 = p;
	    p.x += d.x;
	    p.y += d.y;
	}
	return true;
    }
    
    static piece_t[*] pieces = { piece_x, piece_o };
    
    status_t make_winner (piece_t piece) {
	return (status_t) { finished = { winner = piece, p1 = p1, p2 = p2 } };
    }

    position_t make_pos (x, y) = (position_t) { x = x, y = y };
    
    for (int p = 0; p < dim (pieces); p++) {
	piece_t piece = pieces[p];
	for (int x = 0; x < cols; x++) {
	    for (int y = 0; y <= rows - row; y++) {
		if (in_a_row (piece, make_pos (x, y), make_pos (0, 1)))
		    return make_winner (piece);
	    }
	}
	for (int y = 0; y < rows; y++) {
	    for (int x = 0; x <= cols - row; x++) {
		if (in_a_row (piece, make_pos (x, y), make_pos (1, 0)))
		    return make_winner (piece);
	    }
	}
	for (int y = 0; y <= rows - row; y++) {
	    for (int x = 0; x <= cols - row; x++) {
		if (in_a_row (piece, make_pos (x, y), make_pos (1, 1)))
		    return make_winner (piece);
		if (in_a_row (piece, make_pos (x + row - 1, y), make_pos (-1, 1)))
		    return make_winner (piece);
	    }
	}
    }
    for (int y = 0; y < rows; y++)
	for (int x = 0; x < cols; x++)
	    if (board[y,x] == piece__)
		return (status_t) { playing =  };
    return make_winner (piece__);
}

/*
 * Make a move and return true if the space is empty,
 * otherwise return false
 */
bool move (&board_t board, int x, int y, piece_t piece)
{
    if (board[y,x] != piece__)
	return false;
    if (y > 0 && board[y-1,x] == piece__)
	return false;
    board[y,x] = piece;
    return true;
}

piece_t other_player (piece_t piece)
{
    if (piece == piece_x)
	return piece_o;
    return piece_x;
}

/*
 * Minmax score computation -- very simple, just 0, 2, -2
 * depending on whether the game is in progress, won by us,
 * or won by the other player. A special -1 value is returned
 * for a cats game
 */
int score (&board_t board, piece_t turn)
{
    status_t	status = get_status (&board);

    union switch (status) {
    case playing:
	return 0;
    case finished f:
	if (f.winner == piece__)
	    return -1;
	else if (f.winner == turn)
	    return 2;
	else
	    return -2;
    }
}

typedef struct {
    position_t	position;
    int		score;
} move_t;

int moves = 0;
int memos = 0;
int rands = 0;

move_t[board_t] memo;

void reset_stats () {
    moves = 0;
    memos = 0;
    rands = 0;
}

bool pick_random () {
    ++rands;
    return randbits(1) != 0;
}

/*
 * Compute the best move using minmax search
 */
move_t best_move (&board_t board, piece_t turn) {
    ++moves;

    move_t  best = {
	position = { x = -1, y = -1 },
	score = score(&board, turn)
    };
    
    if (best.score != 0) {
	return best;
    }
    
    /*
     * Check to see if this position has already
     * been scored
     */
    if (hash_test (memo, board)) {
	++memos;
	return memo[board];
    }
    
    piece_t next_turn = other_player (turn);
    
    best.score = -3;
    /*
     * Search the whole board for possible moves
     */
    for (int y = 0; y < rows; y++)
	for (int x = 0; x < cols; x++) {
	    if (move (&board, x, y, turn)) {
		move_t next = best_move (&board, next_turn);
		/*
		 * Minimize our opponents best possible move.
		 * When two moves are equal, pick randomly
		 */
		if (-next.score > best.score || 
		    -next.score == best.score && pick_random())
		{
		    best.position.x = x;
		    best.position.y = y;
		    best.score = -next.score;
		}
		/*
		 * Undo this move
		 */
		board[y,x] = piece__;
	    }
	}
    
    /*
     * Remember this move for next time
     */
    memo[board] = best;
    return best;
}

void reset_game (&game_t game)
{
    game.board = (board_t) { { piece__ ... } ... };
    game.status.playing = ;
    game.turn = piece_x;
    memo = (move_t[board_t]) {};
    Semaphore::signal (game.waiter);
}

bool make_move (&game_t game, int x, int y)
{
    if (move (&game.board, x, y, game.turn))
    {
	game.turn = other_player (game.turn);
	game.status = get_status (&game.board);
        Semaphore::signal (game.waiter);
	return true;
    }
    return false;
}

void computer_player (&game_t game, &label_t label)
{
    Mutex::acquire (game.lock);
    while (!game.quiting) {
	Mutex::release (game.lock);
	Semaphore::wait (game.waiter);
	Mutex::acquire (game.lock);
	if (game.status == game_status_t.playing &&
	    game.turn == game.computer) 
	{
	    Label::relabel (&label, "thinking...");
	    Widget::redraw (&label);
	    board_t best_board = game.board;
	    move_t computer = best_move (&best_board, game.turn);
	    assert (computer.position.x != -1, "no move");
	    make_move (&game, computer.position.x, computer.position.y);
	    Label::relabel (&label, sprintf("score: %d", computer.score));
	    Widget::redraw (&label);
	}
    }
    Mutex::release (game.lock);
}

void set_computer (&game_t game, piece_t player)
{
    twixt (Mutex::acquire (game.lock); Mutex::release (game.lock)) {
	game.computer = player;
        Semaphore::signal (game.waiter);
    }
}

extend namespace Nichrome {
    public namespace Gridwidget {
	public typedef widget_t + struct {
	    int down_x, down_y;
	    &game_t game;
	} grid_widget_t;

	/*
	 * Draw the current state of the game
	 */

	void draw (cairo_t cr, &grid_widget_t widget) {
	    scale (cr, widget.geometry.width, -widget.geometry.height*cols/rows);
	    translate (cr, 0, -rows/cols);
	    set_line_width (cr, 0.05);
	    set_line_cap (cr, line_cap_t.ROUND);
	    draw_grid (cr);
	    draw_pieces (cr, &widget.game.board);
	    draw_win_line (cr, &widget.game.status);
	}

	void outline (cairo_t cr, &grid_widget_t widget) {
	    rectangle (cr, 0, 0, widget.geometry.width,
		       widget.geometry.height);
	}
	    
	void natural (cairo_t cr, &grid_widget_t widget)
	{
	    rectangle (cr, 0, 0, cols * 100, rows * 100);
	}
	
	void button (&grid_widget_t widget, &button_event_t event) {
	    &game_t game = &widget.game;
	    int x_i = floor (event.x * cols / widget.geometry.width);
	    int y_i = rows - 1 - floor (event.y * rows / widget.geometry.height);
	    if (event.type == button_type_t.press) {
		widget.down_x = x_i;
		widget.down_y = y_i;
	    } else {
		Mutex::acquire (game.lock);
		if (widget.game.status == game_status_t.playing &&
		    widget.game.turn != widget.game.computer &&
		    x_i == widget.down_x &&
		    y_i == widget.down_y)
		{
		    if (make_move (&widget.game, x_i, y_i))
			Widget::redraw (&widget);
		}
		Mutex::release (game.lock);
	    }
	}
	
	public *grid_widget_t new (&nichrome_t nichrome, &game_t game) {
	    grid_widget_t    widget;
	    
	    Widget::init (&nichrome, &widget);
	    widget.button = button;
	    widget.draw = draw;
	    widget.outline = outline;
	    widget.natural = natural;
	    &widget.game = &game;
	    return &widget;
	}
    }
}

import Nichrome::Gridwidget;

/*
 * Play tic tac toe
 */
void play ()
{
    Widget::default_font = "Comic Sans MS-16";
    &nichrome_t	    nichrome = Nichrome::new ("Connect-3");
    game_t  game = {
	lock = Mutex::new (),
	waiter = Semaphore::new (),
	quiting = false,
	board = { { piece__ ... } ... },
	status = { playing =  },
	turn = piece_x,
	computer = piece_o,
    };
    
    &button_t	    quit = Nichrome::Button::new (&nichrome, "Quit",
						void func (&widget_t w, bool state) {
						    w.nichrome.running = false;
						    game.quiting = true;
						    Semaphore::signal (game.waiter);
						});

    &button_t	    new = Nichrome::Button::new (&nichrome, "New",
					       void func (&widget_t w, bool state) {
						    reset_game (&game);
					       });
    &button_t	    computer;
    void computer_callback (&widget_t w, bool state) {
	set_computer (&game,
		      other_player (game.computer));
	Button::relabel (&computer,
			 "Computer: " + 
			 (game.computer == piece_x ? "x" : "o"));
    }
    
    &computer = Nichrome::Button::new (&nichrome, "Computer: o",
					void func (&widget_t w, bool state) {
					    set_computer (&game,
							  other_player (game.computer));
					    Button::relabel (&computer,
							     "Computer: " + 
							     (game.computer == piece_x ? "x" : "o"));
					});

    &label_t	    thinking = Nichrome::Label::new (&nichrome, "");
    
    &grid_widget_t   widget = Nichrome::Gridwidget::new (&nichrome, &game);
	
    &box_t	    menu = Nichrome::Box::new (Box::dir_t.horizontal,
					       Box::widget_item (&new, 0),
					       Box::widget_item (&quit, 0),
					       Box::widget_item (&computer, 0),
					       Box::glue_item (1),
					       Box::widget_item (&thinking, 0));
    &box_t	    box = Nichrome::Box::new (Box::dir_t.vertical,
					      Box::box_item (&menu),
					      Box::widget_item (&widget));
    Nichrome::set_box (&nichrome, &box);
    fork computer_player (&game, &thinking);
    Nichrome::main_loop (&nichrome);
}

play ();