c++小练习——黑白棋

/*
	Othello.cpp
	黑白棋,实现随时结束并判断胜负的功能
	成功运行于Visual Studio 2013
*/

#include <iostream>
#include <string.h>
using namespace std;
#define N 9
char _ChessBoard[N][N] = {" "};
char _ChessBoardBefore[N][N] = { " " };

//初始化棋盘
void initChessBoard(){
	_ChessBoard[4][4] = 'x';
	_ChessBoard[5][5] = 'x';
	_ChessBoard[4][5] = 'o';
	_ChessBoard[5][4] = 'o';
}

void printChessBoard(char _ChessBoard[N][N]){        //画棋盘
	printf("     A   B   C   D   E   F   G   H\n");
	printf("   |---|---|---|---|---|---|---|---|\n");
	for (int i = 1; i < N; ++i)
	{
		printf("%2d ", i);
		printf("| %c | %c | %c | %c | %c | %c | %c | %c |\n", _ChessBoard[i][1], _ChessBoard[i][2], _ChessBoard[i][3], _ChessBoard[i][4], _ChessBoard[i][5], _ChessBoard[i][6], _ChessBoard[i][7], _ChessBoard[i][8]);
		printf("   |---|---|---|---|---|---|---|---|\n");
	}
	cout << endl;
}

//检查落子的位置并落子
int check(int i, int j, int flag){
	int rightflag = 0;//落子正确与否标记
	int x, y;
	x = i; y = j;
	if (flag == 0 && _ChessBoard[i][j] == NULL)//x子落子
	{
		//分为八个方向,分别判断是否可以落子并且实现反转
		x = i; y = j;
		x++;//x正向
		while (_ChessBoard[x][y] != 'x'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x + 1][y] == 'x'&&x!=i)
			{
				while (x > i){
					_ChessBoard[x][y] = 'x';//o反转为x
					x--;
				}
				rightflag = 1;//进入循环说明此落子位置正确
				break;
			}
			x++;
		}
		//与上类似,方向换位y正向
		x = i; y = j;
		y++;
		while (_ChessBoard[x][y] != 'x'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x][y+1] == 'x'&&y != j)
			{
				while (y > j){
					_ChessBoard[x][y] = 'x';
					y--;
				}
				rightflag = 1;
				break;
			}
			y++;
		}

		x = i; y = j;
		x--;
		while (_ChessBoard[x][y] != 'x'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x-1][y] == 'x'&&x != i)
			{
				while (x<i){
					_ChessBoard[x][y] = 'x';
					x++;
				}
				rightflag = 1;
				break;
			}
			x--;
		}

		x = i; y = j;
		y--;
		while ((_ChessBoard[x][y] != 'x') && (_ChessBoard[x][y] != NULL))
		{
			if (_ChessBoard[x][y - 1] == 'x'&&y != j)
			{
				while (y < j){
					_ChessBoard[x][y] = 'x';
					y++;
				}
				rightflag = 1;
				break;
			}
			y--;
		}

		x = i; y = j;
		x++; y++;
		while (_ChessBoard[x][y] != 'x'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x + 1][y + 1] == 'x'&&y != j)
			{
				while (y > j){
					_ChessBoard[x][y] = 'x';
					x--; y--;
				}
				rightflag = 1;
				break;
			}
			x++; y++;
		}

		x = i; y = j;
		x--; y++;
		while (_ChessBoard[x][y] != 'x'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x - 1][y + 1] == 'x'&&y != j)
			{
				while (y > j){
					_ChessBoard[x][y] = 'x';
					x++; y--;
				}
				rightflag = 1;
				break;
			}
			x--; y++;
		}

		x = i; y = j;
		x++; y--;
		while (_ChessBoard[x][y] != 'x'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x + 1][y - 1] == 'x'&&y != j)
			{
				while (y < j){
					_ChessBoard[x][y] = 'x';
					x--; y++;
				}
				rightflag = 1;
				break;
			}
			x++; y--;
		}

		x = i; y = j;
		x--; y--;
		while (_ChessBoard[x][y] != 'x'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x - 1][y - 1] == 'x'&&y != j)
			{
				while (y < j){
					_ChessBoard[x][y] = 'x';
					x++; y++;
				}
				rightflag = 1;
				break;
			}
			x--; y--;
		}
	}
	else if (flag == 1 && _ChessBoard[i][j] == NULL)
	{
		x = i; y = j;
		x++;
		while (_ChessBoard[x][y] != 'o'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x + 1][y] == 'o'&&x != i)
			{
				while (x > i){
					_ChessBoard[x][y] = 'o';
					x--;
				}
				rightflag = 1;
				break;
			}
			x++;
		}

		x = i; y = j;
		y++;
		while (_ChessBoard[x][y] != 'o'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x][y + 1] == 'o'&&y != j)
			{
				while (y > j){
					_ChessBoard[x][y] = 'o';
					y--;
				}
				rightflag = 1;
				break;
			}
			y++;
		}

		x = i; y = j;
		x--;
		while (_ChessBoard[x][y] != 'o'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x - 1][y] == 'o'&&x != i)
			{
				while (x<i){
					_ChessBoard[x][y] = 'o';
					x++;
				}
				rightflag = 1;
				break;
			}
			x--;
		}

		x = i; y = j;
		y--;
		while ((_ChessBoard[x][y] != 'o') && (_ChessBoard[x][y] != NULL))
		{
			if (_ChessBoard[x][y - 1] == 'o'&&y != j)
			{
				while (y < j){
					_ChessBoard[x][y] = 'o';
					y++;
				}
				rightflag = 1;
				break;
			}
			y--;
		}

		x = i; y = j;
		x++; y++;
		while (_ChessBoard[x][y] != 'o'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x + 1][y + 1] == 'o'&&y != j)
			{
				while (y > j){
					_ChessBoard[x][y] = 'o';
					x--; y--;
				}
				rightflag = 1;
				break;
			}
			x++; y++;
		}

		x = i; y = j;
		x--; y++;
		while (_ChessBoard[x][y] != 'o'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x - 1][y + 1] == 'o'&&y != j)
			{
				while (y > j){
					_ChessBoard[x][y] = 'o';
					x++; y--;
				}
				rightflag = 1;
				break;
			}
			x--; y++;
		}

		x = i; y = j;
		x++; y--;
		while (_ChessBoard[x][y] != 'o'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x + 1][y - 1] == 'o'&&y != j)
			{
				while (y < j){
					_ChessBoard[x][y] = 'o';
					x--; y++;
				}
				rightflag = 1;
				break;
			}
			x++; y--;
		}

		x = i; y = j;
		x--; y--;
		while (_ChessBoard[x][y] != 'o'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x - 1][y - 1] == 'o'&&y != j)
			{
				while (y < j){
					_ChessBoard[x][y] = 'o';
					x++; y++;
				}
				rightflag = 1;
				break;
			}
			x--; y--;
		}
		if (rightflag != 1) rightflag = 0;
	}
	else if (_ChessBoard[i][j] != NULL)
	{
		rightflag = 0;
	}
	return rightflag;
}

//检查棋盘
int checkBefore(int i, int j, int flag){
	int rightflag = 0;
	int x, y;
	x = i; y = j;
	if (flag == 0 && _ChessBoard[i][j] == NULL)
	{
		x = i; y = j;
		x++;
		while (_ChessBoard[x][y] != 'x'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x + 1][y] == 'x'&&x != i)
			{
				while (x > i){
					x--;
				}
				rightflag = 1;
				break;
			}
			x++;
		}

		x = i; y = j;
		y++;
		while (_ChessBoard[x][y] != 'x'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x][y + 1] == 'x'&&y != j)
			{
				while (y > j){
					y--;
				}
				rightflag = 1;
				break;
			}
			y++;
		}

		x = i; y = j;
		x--;
		while (_ChessBoard[x][y] != 'x'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x - 1][y] == 'x'&&x != i)
			{
				while (x<i){
					x++;
				}
				rightflag = 1;
				break;
			}
			x--;
		}

		x = i; y = j;
		y--;
		while ((_ChessBoard[x][y] != 'x') && (_ChessBoard[x][y] != NULL))
		{
			if (_ChessBoard[x][y - 1] == 'x'&&y != j)
			{
				while (y < j){
					y++;
				}
				rightflag = 1;
				break;
			}
			y--;
		}

		x = i; y = j;
		x++; y++;
		while (_ChessBoard[x][y] != 'x'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x + 1][y + 1] == 'x'&&y != j)
			{
				while (y > j){
					x--; y--;
				}
				rightflag = 1;
				break;
			}
			x++; y++;
		}

		x = i; y = j;
		x--; y++;
		while (_ChessBoard[x][y] != 'x'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x - 1][y + 1] == 'x'&&y != j)
			{
				while (y > j){
					x++; y--;
				}
				rightflag = 1;
				break;
			}
			x--; y++;
		}

		x = i; y = j;
		x++; y--;
		while (_ChessBoard[x][y] != 'x'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x + 1][y - 1] == 'x'&&y != j)
			{
				while (y < j){
					x--; y++;
				}
				rightflag = 1;
				break;
			}
			x++; y--;
		}

		x = i; y = j;
		x--; y--;
		while (_ChessBoard[x][y] != 'x'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x - 1][y - 1] == 'x'&&y != j)
			{
				while (y < j){
					x++; y++;
				}
				rightflag = 1;
				break;
			}
			x--; y--;
		}
	}
	else if (flag == 1 && _ChessBoard[i][j] == NULL)
	{
		x = i; y = j;
		x++;
		while (_ChessBoard[x][y] != 'o'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x + 1][y] == 'o'&&x != i)
			{
				while (x > i){
					x--;
				}
				rightflag = 1;
				break;
			}
			x++;
		}

		x = i; y = j;
		y++;
		while (_ChessBoard[x][y] != 'o'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x][y + 1] == 'o'&&y != j)
			{
				while (y > j){
					y--;
				}
				rightflag = 1;
				break;
			}
			y++;
		}

		x = i; y = j;
		x--;
		while (_ChessBoard[x][y] != 'o'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x - 1][y] == 'o'&&x != i)
			{
				while (x<i){
					x++;
				}
				rightflag = 1;
				break;
			}
			x--;
		}

		x = i; y = j;
		y--;
		while ((_ChessBoard[x][y] != 'o') && (_ChessBoard[x][y] != NULL))
		{
			if (_ChessBoard[x][y - 1] == 'o'&&y != j)
			{
				while (y < j){
					y++;
				}
				rightflag = 1;
				break;
			}
			y--;
		}

		x = i; y = j;
		x++; y++;
		while (_ChessBoard[x][y] != 'o'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x + 1][y + 1] == 'o'&&y != j)
			{
				while (y > j){
					x--; y--;
				}
				rightflag = 1;
				break;
			}
			x++; y++;
		}

		x = i; y = j;
		x--; y++;
		while (_ChessBoard[x][y] != 'o'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x - 1][y + 1] == 'o'&&y != j)
			{
				while (y > j){
					x++; y--;
				}
				rightflag = 1;
				break;
			}
			x--; y++;
		}

		x = i; y = j;
		x++; y--;
		while (_ChessBoard[x][y] != 'o'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x + 1][y - 1] == 'o'&&y != j)
			{
				while (y < j){
					x--; y++;
				}
				rightflag = 1;
				break;
			}
			x++; y--;
		}

		x = i; y = j;
		x--; y--;
		while (_ChessBoard[x][y] != 'o'&&_ChessBoard[x][y] != NULL)
		{
			if (_ChessBoard[x - 1][y - 1] == 'o'&&y != j)
			{
				while (y < j){
					x++; y++;
				}
				rightflag = 1;
				break;
			}
			x--; y--;
		}
		if (rightflag != 1) rightflag = 0;
	}
	else if (_ChessBoard[i][j] != NULL)
	{
		rightflag = 0;
	}
	return rightflag;
}

//复制棋盘
void copyBoard(){
	int i, j;
	for (i = 0; i < N; i++)
	{
		for (j = 0; j < N; j++)
		{
			_ChessBoardBefore[i][j] = _ChessBoard[i][j];
		}
	}
}

//判断胜负
void whoWin()
{
	int player1 = 0, player2 = 0;
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			if (_ChessBoard[i][j] == 'x') player1++;
			else if (_ChessBoard[i][j] == 'o') player2++;
		}
	}
	if (player1>player2) cout << "x获胜" << endl;
	else if (player1 < player2) cout << "o获胜" << endl;
	else cout << "平局" << endl;
}

void fallChess(){	//落子
	int flag = 0;
	int i, j;
	int winFlag = 0;
	int rightflag;
	int k = 0, t = 0;
	char j0;
	initChessBoard();
	printChessBoard(_ChessBoard);
	while (1)
	{
		t = 0;	//游戏是否需要结束的标记
		copyBoard();//复制棋盘
		if (flag == 2) flag = 0;//换手
		for (int x = 0; x < N; x++)//找出可以落子的位置
		{
			for (int y = 0; y < N; y++)
			{
				rightflag = checkBefore(x, y, flag);
				if (rightflag) _ChessBoardBefore[x][y] = 'Y';
				else k++;
			}
		}
		printf("可落子的位置,用Y标记\n");
		printChessBoard(_ChessBoardBefore);//画出标记有可落子位置的棋盘

		if (k == 0)//若k为零,则此时落子方无子可落
		{
			printf("无法落子,换手\n");
			t++;
		}
		if (t == 2)//若t=2,则两方都无子可落,被动触发游戏结束
		{
			whoWin();//判定胜负
			printf("游戏结束\n");
			break;
		}
		cout << "请输入您要落子的位置,格式如 5 F(数值在1-8之间,字母在A-H之间,如输入10 X则游戏结束)" << endl;
		int isright=0;
		while (isright==0)
		{
			cin >> i >> j0;//输入位置
			j = j0 - 'A' + 1;//将字母转换为数字
			isright = checkBefore(i, j, flag);//判断输入是否可以落子
			if (isright == 0) cout << "输入错误" << endl;
			if (i == 10) break;//若输入i=10,则主动结束游戏
		}
		check(i, j, flag);//将中间的棋子反转
		if (flag == 0)//落子
		{
			_ChessBoard[i][j] = 'x';
		}
		else if (flag == 1)
		{
			_ChessBoard[i][j] = 'o';
		}
		printf("落子后的棋盘\n");
		printChessBoard(_ChessBoard);//打印棋盘
		flag++;//换手
		if (i == 10)//主动触发游戏结束
		{
			whoWin();
			printf("游戏结束\n");
			break;
		}
	}
}

int main(){
	cout << "游戏规则:把自己颜色的棋子放在棋盘的空格上,而当自己放下的棋子在横、竖、斜八个方向内有一个自己的棋子,则被夹在中间的对方棋子全部翻转会成为自己的棋子。夹住的位置上必须全部是对手的棋子,不能有空格。并且,只有在可以翻转棋子的地方才可以下子。一步棋可以在数个方向上翻棋,任何被夹住的棋子都必须被翻转过来,棋手无权选择不去翻某个棋子必须是刚下的子夹对方才能够翻对方的子,因翻转对方的棋子而夹住的子是不能被翻的。" << endl;
	fallChess();
	system("pause");
	return 0;
}
posted @ 2021-10-27 23:36  胡不归来  阅读(542)  评论(0编辑  收藏  举报