C++ 编写 黑白棋 源代码 适合初学者
更新时间:2023-12-14 20:20:01 阅读量: 教育文库 文档下载
- c t 阴性阳性推荐度:
- 相关推荐
C++编写黑白棋
本人写的黑白棋是在控制台上面可以玩的输入时仅需要输入两个字母表示下棋的行与列,比如下在一行二列,只需要输入ab即可。如果需要五子棋的,可以搜索本人的《C语言编写五子棋》《C++编写五子棋》。
/******************************************************************** created: 2012/03/12
created: 12:3:2012 17:18
filename: e:\\Visual Studio 2005\\Projects\\othello\\othello\\othello.cpp file path: e:\\Visual Studio 2005\\Projects\\othello\\othello file base: othello file ext: cpp author: terranlong
*********************************************************************/
#include
class Othello {
public:
Othello() { init(); }
void init(); void print();
bool legalwithpostion(int x, int y, int xoffset, int yoffset, int legal); bool illegal(int x, int y); void reversal(int x, int y); bool skip(); void input(); bool gameover(); private:
const static int chessboard_size = 8;
char chessboard[chessboard_size][chessboard_size]; int count; int playerid;
int playercount1; int playercount2; };
void Othello::init() {
int i, j; count = 4; playerid = 0; playercount1 = 0; playercount2 = 0;
for (i = 0; i < chessboard_size; i++) { for (j = 0; j < chessboard_size; j++) { chessboard[i][j] = '+'; } }
chessboard[chessboard_size / 2 - 1][chessboard_size / 2 - 1] = 1; chessboard[chessboard_size / 2][chessboard_size / 2] = 1; chessboard[chessboard_size / 2 - 1][chessboard_size / 2] = 2; chessboard[chessboard_size / 2][chessboard_size / 2 - 1] = 2; }
void Othello::print() {
int i, j;
printf(\ a b c d e f g h\\n\);
for (i = 0; i < chessboard_size; i++) { printf(\, i + 'a'); for (j = 0; j < chessboard_size; j++) { printf(\, chessboard[i][j]); if (j != chessboard_size - 1) { printf(\); } } printf(\); } }
bool Othello::legalwithpostion(int x, int y, int xoffest, int yoffset, int legal)
{
int self = playerid + 1;
int emulant = (playerid + 1) % 2 + 1; x += xoffest; y += yoffset;
if (x < 0 || x >= chessboard_size || y < 0 || y >= chessboard_size) { return 0; }
if (chessboard[x][y] == emulant) { legal = 1; return legalwithpostion(x, y, xoffest, yoffset, legal); }
else if (chessboard[x][y] == self) { if (legal == 1) { return 1; } return 0; } else { return 0; }
return 0; }
bool Othello::illegal(int x, int y) {
int i, j, legal;
for (i = -1; i <= 1; i++) { for (j = -1; j <= 1; j++) { if (i == 0 && j == 0) { continue; } legal = 0; if (legalwithpostion(x, y, i, j, legal)) { return 0;
} } }
return 1; }
void Othello::reversal(int x, int y) {
int i, j, m, n, legal; int self = playerid + 1;
int emulant = (playerid + 1) % 2 + 1; for (i = -1; i <= 1; i++) { for (j = -1; j <= 1; j++) { if (i == 0 && j == 0) { continue; } legal = 0; if (legalwithpostion(x, y, i, j, legal)) { m = x + i; n = y + j; while (chessboard[m][n] == emulant) { chessboard[m][n] = self; m += i; n += j; } } } } }
bool Othello::skip() {
int i, j;
for (i = 0; i < chessboard_size; i++) { for (j = 0; j < chessboard_size; j++) { if (chessboard[i][j] != '+') {
continue; } if (!illegal(i, j)) { return 0; } } }
playerid = (playerid + 1) % 2; return 1; }
void Othello::input() {
char ix, iy; int x, y;
printf(\, 1); printf(\, 2); printf(\);
printf(\, playerid); scanf(\, &ix, &iy); getchar(); x = ix - 'a'; y = iy - 'a';
while (x < 0 || x >= chessboard_size || y < 0 || y >= chessboard_size || chessboard[x][y] != '+' || illegal(x, y)) { printf(\); scanf(\, &ix, &iy); getchar(); x = ix - 'a'; y = iy - 'a'; }
chessboard[x][y] = playerid + 1; reversal(x, y); count++;
playerid = (playerid + 1) % 2; }
bool Othello::gameover() {
int i, j;
if (count >= chessboard_size * chessboard_size) {
system(\); print(); for (i = 0; i < chessboard_size; i++) { for (j = 0; j < chessboard_size; j++) { if (chessboard[i][j] == 1) { playercount1++; } else if (chessboard[i][j] == 2) { playercount2++; } } } printf(\, playercount1); printf(\, playercount2); if (playercount1 > playercount2) { printf(\); } else if (playercount1 < playercount2) { printf(\); } else { printf(\); } return 1; }
return 0; }
Othello otl;
void gamestart() {
otl.init();
while (!otl.gameover()) { system(\); otl.print();
if (otl.skip()) { continue; } else { otl.input(); } } }
int main(int argc, char* argv[]) {
char ch = 'y'; while (1) { gamestart(); getchar(); printf(\); ch = getchar(); if (ch == 'n') { break; } }
return 0; }
正在阅读:
C++ 编写 黑白棋 源代码 适合初学者12-14
电子商务实验报告BTB刘婉君05-31
安全员C证考试重点资料09-01
低碳生活作文1000字03-12
给实习单位的感谢信02-08
物料管理模块的主要功能01-22
公司治理 14财管2班第三组06-06
销售业务员个人工作总结通用例文04-04
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 黑白棋
- C++
- 源代码
- 初学者
- 编写
- 适合