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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. 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 ...

  7. 《Cracking the Coding Interview》——第8章:面向对象设计——题目10

    2014-04-24 00:05 题目:用拉链法设计一个哈希表. 解法:一个简单的哈希表,就看成一个数组就好了,每个元素是一个桶,用来放入元素.当有多个元素落入同一个桶的时候,就用链表把它们连起来.由 ...

  8. 《Cracking the Coding Interview》——第8章:面向对象设计——题目9

    2014-04-23 23:57 题目:如何设计一个内存文件系统,如果可以的话,附上一些代码示例. 解法:很遗憾,对我来说不可以.完全没有相关经验,所以实在无从入手.这题目应该和工作经验相关吧? 代码 ...

  9. 《Cracking the Coding Interview》——第8章:面向对象设计——题目7

    2014-04-23 23:38 题目:你要如何设计一个聊天服务器,有什么技术难点? 解法:这是基于工作经验的面试题吗?否则,一个new grad碰上这种题目能打点草稿也就算不错了. 代码: // 8 ...

随机推荐

  1. HTML入门1—HTML基础学习

    html文档结构 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  2. Android(java)学习笔记72:ProgressBar的使用

    1. ProgressBar使用 首先我们看例程如下: (1) main.xml文件如下: <?xml version="1.0" encoding="utf-8& ...

  3. DP上课覆盖知识点,POJ(1513)

    题目链接:http://poj.org/problem?id=1513 解题报告: 思路: 知识点从第二个开始扫,递推表达式是:minlec[i]=min(minlec[k])+1,并且要保证,tim ...

  4. python 3+djanjo 2.0.7简单学习(四)--Django视图

    1.概念 Django 中的视图的概念是「一类具有相同功能和模板的网页的集合」.比如,在一个博客应用中,你可能会创建如下几个视图: 博客首页——展示最近的几项内容. 内容“详情”页——详细展示某项内容 ...

  5. js获取对象所有的keys

    Js中获取对象的所有key值   假如现在有一个对象 var obj = { A:2 ,B:"Ray" ,C:true ,D:function(){} } 如果想遍历对象obj中的 ...

  6. string find简析

    原文链接 #include <string>#include <iostream>using namespace std; void main(){ ////find函数返回类 ...

  7. leetCode没那么难啦 in Java (一)

    前言    感觉写博客是一个很耗心力的东西T_T,简单的写了似乎没什么用,复杂的三言两语也只能讲个大概,呸呸...怎么能有这些消极思想呢QAQ!那想来想去,先开一个leetcode的坑,虽然已经工作了 ...

  8. 当Java遇见了Html--Servlet篇

    ###一.什么是servlet servlet是在服务器上运行的小程序.一个servlet就是一个 java类,并且通过"请求-响应"编程模型来访问的这个驻留在服务器内存里的程序. ...

  9. 关于 Angular引用Material出现node_modules/@angular/material/button-toggle/typings/button-toggle.d.ts(154,104): error TS2315: Type 'ElementRef' is not generic.问题

    百度了好久 ,,,最后谷歌出来了.. 该错误可能来自于您将@ angular / material设置为6.0.0, 但所有其他Angular包都是5.x.您应该始终确保Material主要版本与An ...

  10. Spring MVC 接收前端参数的方式

    方式一: 普通方式接收 1 @RequestMapping("/index") 2 public String getUserName(String username) { 3 S ...