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