-
-
Save kielni/9599291 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Go is a 2 player board game with simple rules. Two players alternate turns | |
* placing stones on a grid. If a stone is surrounded on 4 sides by stones of | |
* the opponent, it is captured. If a group of stones are surrounded, they are | |
* captured. | |
* See http://en.wikipedia.org/wiki/Rules_of_Go#Capture for a visual explanation. | |
* | |
* Below is an implementation of a Go board. Please write some code in the | |
* move() function to check for captures and output something when a capture | |
* occurs. The sample moves represent a capture of two black stones. | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#define BOARD_SIZE 19 | |
typedef unsigned char board_t[BOARD_SIZE][BOARD_SIZE]; | |
enum { | |
EMPTY = 0x1, | |
BLACK = 0x2, | |
WHITE = 0x4, | |
}; | |
int move(board_t board, int color, size_t row, size_t col) { | |
board[row][col] = color; | |
return 0; | |
} | |
void print_board(board_t board) { | |
for (size_t r = 0; r < BOARD_SIZE; r++) { | |
for (size_t c = 0; c < BOARD_SIZE; c++) { | |
if (board[r][c] == EMPTY) | |
putchar('_'); | |
else | |
printf("%d", board[r][c]); | |
} | |
putchar('\n'); | |
} | |
} | |
int main() { | |
board_t board; | |
memset(board, EMPTY, sizeof(unsigned char) * BOARD_SIZE * BOARD_SIZE); | |
move(board, BLACK, 4, 4); | |
move(board, BLACK, 4, 5); | |
move(board, WHITE, 3, 4); | |
move(board, WHITE, 3, 5); | |
move(board, WHITE, 4, 3); | |
move(board, WHITE, 4, 6); | |
move(board, WHITE, 5, 4); | |
move(board, WHITE, 5, 5); | |
print_board(board); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Go is a 2 player board game with simple rules. Two players alternate turns | |
# placing stones on a grid. If a stone is surrounded on 4 sides by stones of | |
# the opponent, it is captured. If a group of stones are surrounded, they are | |
# captured. | |
# See http://en.wikipedia.org/wiki/Rules_of_Go#Capture for a visual explanation. | |
EMPTY = 0 | |
BLACK = 1 | |
WHITE = 2 | |
class Board(object): | |
def __init__(self): | |
self.board = [[EMPTY] * 19 for _ in xrange(19)] # 2d 19x19 matrix of 0's | |
def __str__(self): | |
s = '' | |
for row in self.board: | |
if s: | |
s += '\n' | |
for sq in row: | |
if sq: | |
s += str(sq) | |
else: | |
s += '_' | |
return s | |
# pos is [row,col] | |
# get color at position or None if invalid position | |
def get_color(self, pos): | |
row = pos[0] | |
col = pos[1] | |
if row >= len(self.board) or row < 0: | |
return None | |
if col >= len(self.board[0]) or col < 0: | |
return None | |
return self.board[row][col] | |
# check in all directions if piece is capped by a piece of the opposite color | |
# pos is [row,col] | |
# seen is list of positions already checked | |
def is_capped(self, pos, seen): | |
capped = False | |
# left, right, up, down | |
for d in [ [0,-1], [0,1], [-1,0], [1,0] ]: | |
new_pos = [sum(x) for x in zip(pos, d)] | |
# already checked this position | |
if new_pos in seen: | |
continue | |
# add this position to the list of seen positions | |
seen += [new_pos] | |
color = self.get_color(new_pos) | |
# off the board or empty space | |
if color is None or color == EMPTY: | |
capped = False | |
break | |
# opposite color piece: stop moving in this direction | |
if color != self.board[pos[0]][pos[1]]: | |
capped = True | |
else: | |
# same color piece: recursively check this new piece | |
capped = self.is_capped(new_pos, seen) | |
if not capped: | |
break | |
return capped | |
def move(self, color, row, col): | |
self.board[row][col] = color | |
# find an adjacent piece of the opposite color | |
for d in [ [0,-1], [0,1], [-1,0], [1,0] ]: | |
adj_pos = [sum(x) for x in zip([row,col], d)] | |
color = self.get_color(adj_pos) | |
if color is not None and color != EMPTY and color != self.board[row][col]: | |
if self.is_capped(adj_pos, []): | |
print "** capture" | |
return True | |
return False | |
# 1x1 capture | |
b = Board() | |
b.move(BLACK, 4, 4) | |
b.move(WHITE, 3, 4) | |
b.move(WHITE, 5, 4) | |
b.move(WHITE, 4, 3) | |
b.move(WHITE, 4, 5) | |
print b | |
# 1x2 capture | |
b = Board() | |
b.move(BLACK, 4, 4) | |
b.move(BLACK, 4, 5) | |
b.move(WHITE, 3, 4) | |
b.move(WHITE, 3, 5) | |
b.move(WHITE, 4, 3) | |
b.move(WHITE, 4, 6) | |
b.move(WHITE, 5, 4) | |
b.move(WHITE, 5, 5) | |
print b | |
# 2x2 capture | |
b = Board() | |
b.move(WHITE, 1, 1) | |
b.move(WHITE, 1, 2) | |
b.move(WHITE, 2, 1) | |
b.move(WHITE, 2, 2) | |
b.move(BLACK, 0, 1) | |
b.move(BLACK, 0, 2) | |
b.move(BLACK, 1, 0) | |
b.move(BLACK, 1, 3) | |
b.move(BLACK, 2, 0) | |
b.move(BLACK, 2, 3) | |
b.move(BLACK, 3, 1) | |
b.move(BLACK, 3, 2) | |
print b | |
# 1x2 + 1x3 capture | |
b = Board() | |
b.move(WHITE, 1, 1) | |
b.move(WHITE, 1, 2) | |
b.move(WHITE, 2, 1) | |
b.move(WHITE, 2, 2) | |
b.move(WHITE, 2, 3) | |
b.move(BLACK, 0, 1) | |
b.move(BLACK, 0, 2) | |
b.move(BLACK, 1, 0) | |
b.move(BLACK, 1, 3) | |
b.move(BLACK, 2, 0) | |
b.move(BLACK, 2, 4) | |
b.move(BLACK, 3, 1) | |
b.move(BLACK, 3, 2) | |
b.move(BLACK, 3, 3) | |
print b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
go: go.c | |
gcc -o go -O go.c -Wall -Wextra -std=c11 -ggdb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment