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. C#中RichTextBox字体不统一(中英文)

    this.richTextBox1.Font = new System.Drawing.Font("微软雅黑", 12F);// new System.Drawing.Font(& ...

  2. Django Field lookups (字段查找)

    字段查找是指定SQL WHERE子句的核心内容的方式. 它们被指定为QuerySet方法filter().exclude()和get()的关键字参数. 1.exact:精确查找.如果为比较提供的值为N ...

  3. Raknet—视频会议系统最佳的数据传输引擎

    RakNet是一个跨平台的C++和C#的游戏引擎,它主要是为高效的数据传输而设计,使用者可以通过它进行游戏和其他的程序的开发.RakNet虽然是一个游戏引擎,但同样也是一个非常好的视频会议系统传输引擎 ...

  4. 安装php的memcache扩展

    linux: 安装环境 RHEL 4 Php 5.2.6 所需软件 libevent-1.4.6-stable.tar.gz (http://monkey.org/~provos/libevent/) ...

  5. eclips新建Maven Web项目

    一.创建项目 1.Eclipse中用Maven创建项目 上图中Next 2.继续Next 3.选maven-archetype-webapp后,next 4.填写相应的信息,Packaged是默认创建 ...

  6. 抽象类和final

    抽象类: 概念:在继承过程中,形成一个继承金字塔,位于金字塔底部的类越来越具体(强大),位于塔顶的越来越抽象(简单). 关键字  :abstract 抽象类特性: [1]抽象类过于抽象,实例化后无语义 ...

  7. 20145238-荆玉茗 《Java程序设计》第6周学习总结

    20145238 <Java程序设计>第6周学习总结 教材学习内容总结 第十章输入和输出 10.1.1 ·如果要将数据从来源中取出,可以使用输入串流,若将数据写入目的地,可以使用输出串流. ...

  8. first 和firstordefault的用法 简介

    https://www.cnblogs.com/1312mn/p/9212325.html

  9. OCCI的迭代修改

    传统的在执行多行DML(INSERT.UPDATE.DELETE)时,我们是多次调用executeUpdate():注意!当我们调用一次此函数时,则执行一次网络往返,当数据量大时则效率非常低.不过 O ...

  10. DB设计工具——dbschema

      Preface       I've got a db design job about meeting room booking system last week.There're many s ...