《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 ...
随机推荐
- 【2017-06-17】QtGui基础控件:QSpinBox及QDoubleSpinBox
今天开始一个新的系列,主要是翻译并摘录QtGui中基础空间的常用方法,并做一些简单的实验程序: 我觉得这是一个炒冷饭的行为,但有时候冷饭不能不炒,不热不好吃,而且也很容易发霉. 其实到现在这种状态,对 ...
- Linux ELF格式分析
http://www.cnblogs.com/hzl6255/p/3312262.html ELF, Executable and Linking Format, 是一种用于可执行文件.目标文件.共享 ...
- nginx里面的rewrite配置
哎,我需要静静,刚刚在去怎么优化dom层级,发现更新完代码,层级又蹭蹭蹭的往上涨,顿时没脾气了,还是把昨天的nginx配置总结下,增加点动力,昨天前天两天都在搞这个问题,也是搞的没脾气,网上查了很多资 ...
- 深入浅出Nginx
深入浅出Nginx 文章源自zfz_linux_boy 前言 Nginx是一款轻量级的Web服务器.反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用. 上图基 ...
- BZOJ 3227: [Sdoi2008]红黑树(tree)
BZOJ 3227: [Sdoi2008]红黑树(tree) 标签(空格分隔): OI-BZOJ OI-其它 Time Limit: 10 Sec Memory Limit: 128 MB Descr ...
- Python -函数的参数定义
一.函数的参数有四种,位置参数.默认参数.可变参数和关键字参数 def func(x, y=0, *arg, **args): '''x为位置参数 y有默认值 *arg为可变参数 **args为关键字 ...
- iOS第三方开放者平台概览
前言:记录一些可能用到过的第三方开放者平台相关内容 视频类: 腾讯云移动直播:https://cloud.tencent.com/product/mlvb 遇到问题后发起工单是一种比较好的解决问题的方 ...
- JavaSE 面试题总结
一. JavaSE 4 1. 面向对象的特征有哪些方面 4 2. String是最基本的数据类型吗? 4 3. super()与this()的区别? 4 4. JAVA的事件委托机制和垃圾回收机制 4 ...
- Servlet的工作原理和生命周期
Servlet的工作原理 . Web服务器加载Servlet:Web服务器启动后,它会根据每个工程的web.xml文件去查找该工程的Servlet,并且找到这些Servlet的Class文件所在的地址 ...
- 理解css中的position属性
理解css中的position 两种类型的定位 static类型:只有一个值position: static.position默认值 relative类型:包括三个值,这三个值会相互影响,允许你以特定 ...