C++ 编写 黑白棋 源代码 适合初学者

2020-02-21 22:39

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 using namespace std;

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) {


C++ 编写 黑白棋 源代码 适合初学者.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:pep人教版六年级英语上册Unit1(第五课时)课堂练习(含答案)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: