Last active
November 9, 2019 12:39
-
-
Save raylu/5053677 to your computer and use it in GitHub Desktop.
Go question
This file contains 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 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. | |
# Below is an implementation of a Go board. Please write some code in the | |
# move() method to check for captures and output something when a capture | |
# occurs. The sample moves represent a capture of two black stones. | |
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 | |
def move(self, color, row, col): | |
self.board[row][col] = color | |
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 |
This file contains 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