8.8 Othello is played as follows: Each Othello piece is white on one side and black on the other. When a piece is surrounded by its opponents on both the left and right sides, or both the top and bottom, it is said to be captured and its color is flipped. On your turn, you must capture at least one of your opponent's pieces. The game ends when either user has no more valid moves. The win is assigned to the person with the most pieces. Implement the object-oriented design for Othello.

这道题是经典的黑白棋游戏,我最早接触到这个游戏是在文曲星上,当年文曲星很火的时候,上面的各种游戏我都爱不释手啊,什么英雄坛说,华容道,汉诺塔啊,黑白棋啊都是我常玩的游戏,尤其这道黑白棋,总是玩不过困难模式的电脑,后来想想玩不过也应该,电脑应该可以把所有可以走的步骤都计算一遍,每次都选择最优的解,下不赢电脑也很正常嘛。今天才算深入了解这个游戏的设计原理啊,可参见下面代码:

enum Direction { Left, Right, Up, Down };
enum Color { White, Black }; class Piece {
public:
Piece(Color c): _color(c) {}
void flip() {
if (_color == Color::Black) _color = Color::White;
else _color = Color::Black;
}
Color getColor() { return _color; } private:
Color _color;
}; class Location {
public:
Location(int r, int c): _row(r), _col(c) {}
bool isSameAs(int r, int c) {
return _row == r && _col == c;
}
int getRow() { return _row; }
int getCol() { return _col; } private:
int _row;
int _col;
}; class Board {
public:
Board(int rows, int cols) {
_board.resize(rows, vector<Piece*>(cols));
}
void initialize() {
int midRow = _board.size() / ;
int midCol = _board[midRow].size() / ;
_board[midRow][midCol] = new Piece(Color::White);
_board[midRow + ][midCol] = new Piece(Color::Black);
_board[midRow + ][midCol + ] = new Piece(Color::White);
_board[midRow][midCol + ] = new Piece(Color::Black);
_blackCnt = ;
_whiteCnt = ;
}
bool placeColor(int row, int col, Color color) {
if (_board[row][col] != nullptr) {
return false;
}
vector<int> res(, );
res[] = flipSection(row - , col, color, Direction::Up);
res[] = flipSection(row + , col, color, Direction::Down);
res[] = flipSection(row, col + , color, Direction::Right);
res[] = flipSection(row, col - , color, Direction::Left);
int flipped = ;
for (auto a : res) {
if (a > ) flipped += a;
}
if (flipped < ) return false;
_board[row][col] = new Piece(color);
updateScore(color, flipped + );
return true;
}
int getScoreForColor(Color c) {
if (c == Color::Black) return _blackCnt;
else return _whiteCnt;
}
void updateScore(Color newColor, int newPieces) {
if (newColor == Color::Black) {
_whiteCnt -= newPieces - ;
_blackCnt += newPieces;
} else {
_blackCnt -= newPieces - ;
_whiteCnt += newPieces;
}
}
void printBoard() {
for (int r = ; r < _board.size(); ++r) {
for (int c = ; c < _board[r].size(); ++c) {
if (_board[r][c] == nullptr) {
cout << "_";
} else if (_board[r][c]->getColor() == Color::White) {
cout << "W";
} else {
cout << "B";
}
}
cout << endl;
}
} private:
int _blackCnt = ;
int _whiteCnt = ;
vector<vector<Piece*> > _board;
int flipSection(int row, int col, Color color, Direction d) {
int r = , c = ;
switch (d) {
case Direction::Up: r = -; break;
case Direction::Down: r = ; break;
case Direction::Left: c = -; break;
case Direction::Right: c = ; break;
}
if (row < || row >= _board.size() || col < || col >= _board[row].size() || _board[row][col] == nullptr) {
return -;
}
if (_board[row][col]->getColor() == color) {
return ;
}
int flipped = flipSection(row + r, col + c, color, d);
if (flipped < ) return -;
_board[row][col]->flip();
return flipped + ;
}
}; class Player {
public:
Player(Color c): _color(c) {}
int getScore() { } // ...
bool playPiece(int r, int c) {
return Game::getInstance()->getBoard()->placeColor(r, c, _color);
} private:
Color _color;
}; class Game {
public:
static Game* getInstance() {
if (_instance == nullptr) {
_instance = new Game();
}
return _instance;
}
Board* getBoard() { return _board; } private:
vector<Player*> _players;
static Game *_instance;
Board *_board;
const int _ROWS = ;
const int _COLUMNS = ;
Game() {
_board = new Board(_ROWS, _COLUMNS);
_players.resize(, nullptr);
_players[] = new Player(Color::Black);
_players[] = new Player(Color::White);
}
};

[CareerCup] 8.8 Othello Game 黑白棋游戏的更多相关文章

  1. 用Dart写的黑白棋游戏

    2013年11月,Dart语言1.0稳定版SDK发布,普天同庆.从此,网页编程不再纠结了. 在我看来,Dart语法简直就是C#的升级版,太像了.之所以喜欢Ruby的一个重要理由是支持mixin功能,而 ...

  2. 黑白棋游戏 (codevs 2743)题解

    [问题描述] 黑白棋游戏的棋盘由4×4方格阵列构成.棋盘的每一方格中放有1枚棋子,共有8枚白棋子和8枚黑棋子.这16枚棋子的每一种放置方案都构成一个游戏状态.在棋盘上拥有1条公共边的2个方格称为相邻方 ...

  3. BZOJ2319 : 黑白棋游戏

    将01串按1分段,那么分析可得长度为$a$的段拼上长度为$b$的段的SG值为$a-[a\leq b]$. 设$f[i][j][k][l]$表示从后往前用了$i$个1,$j$个0,当前段长度为$k$,后 ...

  4. 洛谷 - P1225 - 黑白棋游戏 - bfs

    神奇bug,没有记录pre就show了,找了1个小时. #include <bits/stdc++.h> using namespace std; #define ll long long ...

  5. 洛谷 题解 P1225 【黑白棋游戏】

    看见很多dalao写了什么双向BFS,蒟蒻表示不会写啊. 怎么办办? 先来分析一下题目,一眼看去就是一个搜索题,考虑DFS与BFS. 先放一份DFS的代码: #include<bits/stdc ...

  6. BZOJ 2281: [Sdoi2011]黑白棋 (Nim游戏+dp计数)

    题意 这题目有一点问题,应该是在n个格子里有k个棋子,k是偶数.从左到右一白一黑间隔出现.有两个人不妨叫做小白和小黑.两个人轮流操作,每个人可以选 1~d 枚自己颜色的棋子,如果是白色则只能向右移动, ...

  7. bzoj 2281 [Sdoi2011]黑白棋(博弈+组合计数)

    黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色 ...

  8. 【BZOJ2281】【博弈论+DP】 [Sdoi2011]黑白棋

    Description 黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是 ...

  9. [SDOI2011]黑白棋

    Description 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色不同. 小 ...

随机推荐

  1. Effective Java 57 Use exceptions only for exceptional conditions

    Principle Exceptions are, as their name implies, to be used only for exceptional conditions; they sh ...

  2. mongo学习笔记(六):linux上搭建

    linux分以下几台 monogos mongocfg mongod1 mongod2 1.用ssh把 mongodb-linux-x86_64-3.0.6.tgz 移到linux /root上 2. ...

  3. 问题解决——SolidWorks 已停止工作 (Windows7 + SolidWorks 2010 SP0.0)

    给同事的SolidWorks解决问题时偶然间发现的. -------------------------------------------------------------- 本文原创,转载请注明 ...

  4. centos6.7 安装Docker

      一.查看系统版本 [root@localhost ~]# cat /etc/redhat-release CentOS release 6.7 (Final) 二.安装EPEL 1.进入cento ...

  5. Windows8下PhoneGap 4 + Android Studio 1.0 + VS2013配置指南

    1.准备工作 安装JDK1.6+,设置环境变量 JAVA_HOME C:\Program Files\Java\jdk1.5.0_07 CLASSPATH .;%JAVA_HOME%\lib Path ...

  6. Hog SVM 车辆 行人检测

    HOG SVM 车辆检测 近期需要对卡口车辆的车脸进行检测,首先选用一个常规的检测方法即是hog特征与SVM,Hog特征是由dalal在2005年提出的用于道路中行人检测的方法,并且取的了不错的识别效 ...

  7. 作为一个测试leader平时应该注意哪些方面

    平时对管理方面很少有总结,总觉得管理是一门艺术,一门需要意会的艺术,虽然目前在做测试leader,平时也看些管理方面的书,但实际中总感觉理解的不够透彻,在工作上实施的话会有各种各样的情况,想要做好管理 ...

  8. 数论+spfa算法 bzoj 2118 墨墨的等式

    2118: 墨墨的等式 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1283  Solved: 496 Description 墨墨突然对等式很感兴 ...

  9. DP+单调队列 codevs 1748 瑰丽华尔兹(还不是很懂具体的代码实现)

    codevs 1748 瑰丽华尔兹 2005年NOI全国竞赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Descripti ...

  10. 关键路径 SDUTOJ 2498

    SDUTOJ 2498 AOE网上的关键路径 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 一个无环的有向图称为无环图(Dire ...