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. 使用Apache Tomcat Maven插件部署运行 Web 项目

    什么是Apache Tomcat Maven Plugin? Maven Plugin 是Apache Tomcat 提供的一个Maven插件,它可以在你没有tomcat容器时将任何一个war项目文件 ...

  2. Android Design Support Library——Snackbar

    Snackbar是一个轻量级控件,它可以很方便的提供消息的提示和动作反馈,类似于Toast.Snackbar包括一段文字信息与一个可选的操作按钮,超时自动隐藏,也可以通过滑动来删除.效果如下所示: S ...

  3. 用shell脚本写一个for循环

    一.输出十遍北京 for((i=1;i<10;i++))> do> echo '北京';> done 二.死循环 for((;;))do#java -jar producer. ...

  4. IE6/7/8中parseInt第一个参数为非法八进制字符串且第二个参数不传时返回值为0

    JavaScript中数字有十进制.八进制.十六进制.以"0"开头的是八进制,"0x"或"0X"开头的是十六进制. parseInt用来把字 ...

  5. history介绍及bash命令快速调用

    在日常工作中,能够快速并准确的使用命令是必不可少的,下面为大家介绍一下其中的小技巧. 一.查找命令历史——history 使用history能够快速的找到之前输入过的命令. # history 大家可 ...

  6. 大型文档源文件拆分编辑编译\include{filename}

    大型文档,如果把所有的文字都录入在同一个.tex文件中,那个文件的体积是不可估量的,文件的结构式混乱不堪的,文字的定位也是令人头疼的.幸亏latex提供了结构化的处理命令---include. 命令\ ...

  7. 最小的k个数

    // 最小的k个数.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include & ...

  8. HDU 4990 Ordered Subsequence --数据结构优化DP

    题意:给一串数字,问长度为m的严格上升子序列有多少个 解法:首先可以离散化为10000以内,再进行dp,令dp[i][j]为以第i个元素结尾的长度为j的上升子序列的个数, 则有dp[i][j] = S ...

  9. UVA 12730 Skyrk's Bar --期望问题

    题意:有n个地方,现在要站人进去,而每两个人之间至少要隔k个空地,问这n个地方能站的人数的期望是多少. 分析:考虑dp[i]表示 i 个地方能站的期望数,从左往右推, 如果i-k-1<1,那么最 ...

  10. Dijkstra求最短路径

    单源点的最短路径问题:给定带权有向图G和源点V,求从V到G中其余各顶点的最短路径 Dijkstra算法描述如下: (1)用带权的邻接矩阵arcs表示有向图,arcs[i][j]表示弧<vi,vj ...