[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个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色不同. 小 ...
随机推荐
- jquery1.0源码【1-60行】构造函数及全局$变量
一.jquery源码1-60行 该部分代码主要完成jquery对象的创建,以及全局变量$与jQurey类的映射: /* * jQuery - New Wave Javascript * * Copyr ...
- Effective Java 39 Make defensive copies when needed
Principle It is essential to make a defensive copy of each mutable parameter to the constructor. Def ...
- 如何正大光明的使用 google 进行搜索
对于程序猿来说,不能使用google,是一大痛所在,今天在使用 百度网盘 搜索时,突然发现 ,他能同时使用 baidu和 google进行搜索,于是想到了这个正大光明的使用google 的方法,不需要 ...
- MyCat 学习笔记 第十篇.数据分片 之 ER分片
1 应用场景 这篇来说下mycat中自带的er关系分片,所谓er关系分片即可以理解为有关联关系表之间数据分片.类似于订单主表与订单详情表间的分片存储规则. 本文所说的er分片分为两种: a. 依据主键 ...
- ASP.NET导出bdf文件
1.导出助手类 using System;using System.IO;using System.Data;using System.Data.OleDb;using System.Web;usin ...
- Linux Crash/Hang on Bay Trail/J1900/N2940
近几年的linux kernel, 尤其是4.1以后,在Bay Trail平台上会随机挂起和死机,亲测j1900,死机非常频繁,而且死机前毫无征兆,直接就挂起了,console也没有相应. 这个问题在 ...
- cordova 环境搭建
安装环境前题是nodejs已安装,android环境搭建完成,android环境没有通过http://www.androiddevtools.cn/安装,安装使用 淘宝 NPM 镜像 方式 1.运行c ...
- zabbix安装排错过程
在讲安装过程之前需要先把zabbix的工作流程简单的讲一遍:zabbix是个开源监控软件,通过web才能更直观的监控我们想要监控的主机,同时,zabbix从被监控主机获取到的信息需要存放在数据库中,因 ...
- MSBI--enlarge the DW database table volume
我们在学习MSBI的时候,经常会使用官方提供的Adventureworks和AdventureworksDW示例数据库,但是官方提供的数据量有点小, 以DW为例,Factinternetsales只有 ...
- 探索 OpenStack 之(11):cinder-api Service 启动过程分析 以及 WSGI / Paste deploy / Router 等介绍
OpenStack 中的每一个提供 REST API Service 的组件,比如 cinder-api,nova-api 等,其实是一个 WSGI App,其主要功能是接受客户端发来的 HTTP R ...