《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 ...
随机推荐
- Js arguments.callee();函数自己调用自己
1.阶乘的时候,函数一般要用到递归算法,所以函数内部一定会调用自身 //递归,阶乘 function sum(num){ ) { ; } else{ ); //自己调用自己,递归 } } alert( ...
- Xcode SDK模拟器安装及安装路径
将SDK想要装的版本,将SDK包放入‘mac中的SDK安装路径’.再将Xcode模拟器重启. 再打开Xcode模拟器,就可以在菜单栏的 ‘硬件’->’设备‘->’iPhone Retina ...
- hdu-1892 See you~---二维树状数组运用
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1892 题目大意: 题目大意:有很多方格,每个方格对应的坐标为(I,J),刚开始时每个格子里有1本书, ...
- 【HHHOJ】ZJOI2019模拟赛(十四)03.12 解题报告
点此进入比赛 得分: \(50+5+24=79\) 排名: \(Rank\ 2\) \(Rating\):\(+79\) \(T1\):[HHHOJ197]古明地(点此看题面) 基本上全部时间都用来想 ...
- 简单广搜,迷宫问题(POJ3984)
题目链接:http://poj.org/problem?id=3984 解题报告: 1.设置node结构体,成员pre记录该点的前驱. 2.递归输出: void print(int i) { ) { ...
- 深入理解HDFS的架构和原理
(一) HDFS主要是用于做什么的? HDFS(Hadoop Distributed File System)是Hadoop项目的核心子项目,是分布式计算中数据存储管理的基础,是基于流数据模式访问和处 ...
- Javascript入门笔记1-script标签
1.script标签在HTML文件中添加JavaScript代码. JavaScript代码只能写在HTML文件中吗?当然不是,我们可以把HTML文件和JS代码分开,并单独创建一个JavaScript ...
- LeetCode426.Convert Binary Search Tree to Sorted Doubly Linked List
题目 Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right point ...
- github上更新fork项目
转载:https://blog.csdn.net/qq1332479771/article/details/56087333 ps:需要用GitHub所指定的chrome或者firefox浏览器,其它 ...
- Linux添加swap分区
swap分区的作用为当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用,那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到S ...