《Cracking the Coding Interview》——第8章:面向对象设计——题目8
2014-04-23 23:49
题目:有个棋牌游戏叫Othello,也叫Reversi。请看游戏规则。中文应该叫黑白棋吧,不常玩儿就是了。
解法:既然这题的规则很清楚,也很清楚,我就写了一个命令行的程序来模拟玩游戏的过程。
代码:
// 8.8 Othello game, the rule is balabala. Try to design a class to play the game.
// Here are the rules:
// 1. two players, one black, one white, play alternatively, black first.
// 2. the board is 8x8, two blacks and two whites are placed in the center first.
// 00000000
// 00000000
// 00000000
// 000bw000
// 000wb000
// 00000000
// 00000000
// 00000000
// 3. every time, a player has to place a piece on an empty position, if the eight directions have the player's pieces, then all the opponent's pieces trapped between those pieces and the new piece is flipped to the current player.
// 4. for every move, the player must at least flip one of the opponent's pieces.
// 5. if a player is unable to make a move, the game ends. Whoever has more pieces on the board wins.
#include <cstdio>
#include <vector>
using namespace std; class OthelloGame {
public:
OthelloGame() {
board.resize();
possible_position.resize();
int i, j;
for (i = ; i < ; ++i) {
board[i].resize();
possible_position[i].resize();
}
for (i = ; i < ; ++i) {
for (j = ; j < ; ++j) {
board[i][j] = ;
}
}
board[][] = board[][] = ;
board[][] = board[][] = ;
winner = ; dd[][] = -;
dd[][] = -;
dd[][] = -;
dd[][] = ;
dd[][] = -;
dd[][] = +;
dd[][] = ;
dd[][] = -;
dd[][] = ;
dd[][] = +;
dd[][] = +;
dd[][] = -;
dd[][] = +;
dd[][] = ;
dd[][] = +;
dd[][] = +;
}; bool playGame() {
int i, j;
int x, y;
int current_player;
int pc[]; current_player = ;
while (!winner) {
if (!checkPositions(current_player)) {
pc[] = pc[] = ;
for (i = ; i < ; ++i) {
for (j = ; j < ; ++j) {
if (board[i][j]) {
++pc[board[i][j] - ];
}
}
}
winner = (pc[] > pc[] ? : pc[] < pc[] ? : );
break;
}
while (true) {
printBoard();
printf("Player %d please move: ", current_player);
scanf("%d%d", &x, &y);
if (inBound(x, y) && possible_position[x][y]) {
setPiece(x, y, current_player);
current_player = (current_player == ) ? : ;
break;
} else {
printf("Invalid move.\n");
}
}
}
return ;
}; ~OthelloGame() {
int i; for (i = ; i < ; ++i) {
board[i].clear();
possible_position[i].clear();
}
board.clear();
possible_position.clear();
}
private:
// 1 for player one, 2 for player two, 0 for empty.
vector<vector<int> > board;
vector<vector<int> > possible_position;
int dd[][];
int winner; void printBoard() {
int i, j;
putchar(' ');
putchar(' ');
for (i = ; i < ; ++i) {
putchar('' + i);
putchar(' ');
}
puts(" \n");
for (i = ; i < ; ++i) {
putchar('' + i);
putchar(' ');
for (j = ; j < ; ++j) {
putchar('' + board[i][j]);
putchar(' ');
}
putchar('\n');
}
}; bool inBound(int x, int y) {
return x >= && x <= && y >= && y <= ;
}; bool checkPositions(int current_player) {
int i, j, k;
int x, y;
int len;
int count; for (i = ; i < ; ++i) {
for (j = ; j < ; ++j) {
possible_position[i][j] = ;
}
}
count = ; for (i = ; i < ; ++i) {
for (j = ; j < ; ++j) {
if (board[i][j]) {
continue;
}
for (k = ; k < ; ++k) {
len = ;
while (true) {
x = i + len * dd[k][];
y = j + len * dd[k][];
if (!inBound(x, y)) {
break;
}
if (board[x][y] == ) {
break;
} else if (board[x][y] == current_player) {
if (len > ) {
possible_position[i][j] = ;
++count;
}
break;
} else {
++len;
}
}
if (possible_position[i][j]) {
break;
}
}
}
} return count > ;
}; void setPiece(int x, int y, int current_player) {
int xx, yy;
int k;
int len; board[x][y] = current_player;
for (k = ; k < ; ++k) {
len = ;
while (true) {
xx = x + len * dd[k][];
yy = y + len * dd[k][];
if (!inBound(x, y)) {
break;
}
if (board[xx][yy] == ) {
break;
} else if (board[xx][yy] == current_player) {
while (--len > ) {
xx = x + len * dd[k][];
yy = y + len * dd[k][];
board[xx][yy] = current_player;
}
} else {
++len;
}
}
}
};
}; int main()
{
OthelloGame game; game.playGame(); return ;
}
《Cracking the Coding Interview》——第8章:面向对象设计——题目8的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目10
2014-04-24 00:05 题目:用拉链法设计一个哈希表. 解法:一个简单的哈希表,就看成一个数组就好了,每个元素是一个桶,用来放入元素.当有多个元素落入同一个桶的时候,就用链表把它们连起来.由 ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目9
2014-04-23 23:57 题目:如何设计一个内存文件系统,如果可以的话,附上一些代码示例. 解法:很遗憾,对我来说不可以.完全没有相关经验,所以实在无从入手.这题目应该和工作经验相关吧? 代码 ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目7
2014-04-23 23:38 题目:你要如何设计一个聊天服务器,有什么技术难点? 解法:这是基于工作经验的面试题吗?否则,一个new grad碰上这种题目能打点草稿也就算不错了. 代码: // 8 ...
随机推荐
- Windows 系统System帐号及权限
今天碰到一同事,在那里删除注册表,死活都删除不掉,想起以前在学校的时候老是被莫名的被别人叫过去修电脑(开玩笑,真觉得那时候的我比现在牛B很多),什么删除不掉的东西没见过,然后小小的百度了一下很快就帮他 ...
- ubuntu linux查看cpu信息
$ cat /proc/cpuinfo CPU核心数量 $ grep -c processor /proc/cpuinfo
- SQL-有关数据库的提问
各位大侠,小弟初来乍到,对sql , pl/sql ,t/sql概念比较模糊,有以下几个问题希望能帮我解答: 1:plsql到底是语言还是工具?我看到有人说oracle的存储过程是拿plsql写的,那 ...
- Poj(2236),简单并查集
题目链接:http://poj.org/problem?id=2236 思路很简单,傻逼的我输出写成了FALL,然后遍历的时候for循环写错了,还好很快我就Debug出来了. #include < ...
- G711格式语音采集/编码/转码/解码/播放
2019-05-01 语音g711格式和AMR格式类似,应用很简单,很多人已经整理过了,收录于此,以备不时之需,用别人现成的足矣,我们的时间应该用来干更有意义的事. 1.PCM to G711 Fas ...
- leetcode_No.1 Two Sum
原题: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- Bean的初始化和销毁
在我们实际开发的时候,经常会遇到在Bean在使用之前或者之后做些必要的操作,Spring对Bean的生命周期的操作提供了支持.在使用Java配置和注解配置下提供如下两种方式. 1.Java配置方 ...
- python中的for循环如何控制步长
for i in range(开始/左边界, 结束/右边界, 步长): print i 例如 for i in range(1, 10, 2): print i 等价于 for (i=1;i<= ...
- C#自动更新本地程序
关于系统的自动更新.近日有一情况是需要将java端后台最新版本的系统文件覆盖本地客户端,简称自动更新了. 本地会获取当前系统的版本号去请求后台java的接口数据.返回给我的是后台压缩包转的base64 ...
- Pj Immediate Decodability
判断一个串是否是其他的前缀 我们需要建立一颗tire树 在插入边的时候,如果遇到一个其他串的结尾,那么就说明至少有一个串,是插入串的前缀.如果在插入完后没有新增的节点,那么插入的串就是其他串的前缀 # ...