[CareerCup] 8.8 Othello Game 黑白棋游戏
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 黑白棋游戏的更多相关文章
- 用Dart写的黑白棋游戏
2013年11月,Dart语言1.0稳定版SDK发布,普天同庆.从此,网页编程不再纠结了. 在我看来,Dart语法简直就是C#的升级版,太像了.之所以喜欢Ruby的一个重要理由是支持mixin功能,而 ...
- 黑白棋游戏 (codevs 2743)题解
[问题描述] 黑白棋游戏的棋盘由4×4方格阵列构成.棋盘的每一方格中放有1枚棋子,共有8枚白棋子和8枚黑棋子.这16枚棋子的每一种放置方案都构成一个游戏状态.在棋盘上拥有1条公共边的2个方格称为相邻方 ...
- BZOJ2319 : 黑白棋游戏
将01串按1分段,那么分析可得长度为$a$的段拼上长度为$b$的段的SG值为$a-[a\leq b]$. 设$f[i][j][k][l]$表示从后往前用了$i$个1,$j$个0,当前段长度为$k$,后 ...
- 洛谷 - P1225 - 黑白棋游戏 - bfs
神奇bug,没有记录pre就show了,找了1个小时. #include <bits/stdc++.h> using namespace std; #define ll long long ...
- 洛谷 题解 P1225 【黑白棋游戏】
看见很多dalao写了什么双向BFS,蒟蒻表示不会写啊. 怎么办办? 先来分析一下题目,一眼看去就是一个搜索题,考虑DFS与BFS. 先放一份DFS的代码: #include<bits/stdc ...
- BZOJ 2281: [Sdoi2011]黑白棋 (Nim游戏+dp计数)
题意 这题目有一点问题,应该是在n个格子里有k个棋子,k是偶数.从左到右一白一黑间隔出现.有两个人不妨叫做小白和小黑.两个人轮流操作,每个人可以选 1~d 枚自己颜色的棋子,如果是白色则只能向右移动, ...
- bzoj 2281 [Sdoi2011]黑白棋(博弈+组合计数)
黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色 ...
- 【BZOJ2281】【博弈论+DP】 [Sdoi2011]黑白棋
Description 黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是 ...
- [SDOI2011]黑白棋
Description 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色不同. 小 ...
随机推荐
- linux命令之 用户和群组
一.保存用户信息的文件 1 /etc/passwd root:x:::root:/root:/bin/bash pwftp:x::::/alidata/www/wwwroot/:/sbin/nolog ...
- github与eclipse创建仓库及克隆仓库
1.前往github官网注册账号,并下载客户端: 2.为eclipse工程创建本地仓库: 1,目前大多eclipse都预装了egit插件,如果没有请自行安装 2,在eclipse内创建工程->右 ...
- 介绍一种css水平垂直居中的方法(非常好用!)
这次介绍一下一个水平垂直居中的css方法,这个方法可以说是百试百灵,废话不多说,直接附上代码: html,body{ width:100%; height:100%; } 你需要居中的元素{ posi ...
- Effective Java 38 Check parameters for validity
For public methods, use the Javadoc @throws tag to document the exception that will be thrown if a r ...
- 关于String对象的比较
1.String对象的比较 String 是一个常量,从String类中的代码可以看出.String类内部是通过char数组来存储字符串,这个char数组是被声明成final的. // Java中只要 ...
- MONGODB(三)——Java操作Mongo
相比于java调用MySqlApI来操作数据库,调用Mongo要简洁容易的多.通过一个简单的样例,很容易地就可以上手 一.导入Jar包 添加Monog支持Java的jar包,这里使用的是2.9.3 & ...
- MyEclipse 6.5 代码自动提示功能配置教程
1. 打开MyEclipse 6.0.1,然后“window”→“Preferences” 2. 选择“java”,展开,“Editor”,选择“Content Assist”. 3. 选择“Cont ...
- Hive2 jdbc test
package andes; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOExce ...
- Expect 入门
一, Expect的作用 Expect广泛应用于交互式操作和自动化测试的场景之中,用来实现自动和交互式任务进行通信,而无需人的干预. 二, Ubuntu安装Expect sudo ...
- Ngrok远程桌面及ssh配置
上一篇Ngrok 内网穿透利器 使用教程我们讲到Ngrok的基本使用教程,这篇描述一下Ngrok的远程桌面及ssh配置 Step 1 修改配置文件ngrok.cfg server_addr: &quo ...