五子棋c++实现

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<stdio.h>
#include<Windows.h>
#include<conio.h>
#include<time.h>
#include<string.h>

using namespace std;


#define N 25
char arr[N][N] = { 0 };
struct posi
{
	int x;
	int y;
};
enum way { n, ne, e, se, s, sw, w, nw };

void initBoard();
void printBoard();
void play();
void game(int kind);
posi playerMove();
posi player2Move();
posi computerMove();
int oneWayGo(way that, int x, int y, char it);
int twoWayGo(way that, int x, int y, char it);
bool isWin(posi* set);


int main() {
	srand((unsigned int)time(NULL));
	
	play();

	return 0;
}

void initBoard() {
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++) {
			arr[i][j] = ' ';
		}
	}
}

void printBoard() {
	printf("\n  ");
	for (int i = 1; i <= N; i++) {
		printf("%2d  ", i);
	}
	printf("\n");//第一行数字

	for (int i = 1; i < N; i++) {
		printf("%2d", i);
		for (int j = 0; j < N - 1; j++) {
			printf(" %c |", arr[i - 1][j]);
		}
		printf(" %c \n  ", arr[i - 1][N - 1]);
		for (int j = 0; j < N - 1; j++) {
			printf("---|");
		}
		printf("---\n");
	}//中间主体

	printf("%2d", N);
	for (int i = 0; i < N - 1; i++) {
		printf(" %c |", arr[N - 1][i]);
	}
	printf(" %c \n", arr[N - 1][N - 1]);
}

void play() {
	int input;
	
	do {
		printf("*****************************\n");
		printf("*** 1.单人 2.双人  0.退出 ***\n");
		printf("********   请选择:   ********\n");
		input = (int)_getch() - 48;
		switch (input) {
		case 1:
			printf("start...\n");
			Sleep(1000);
			system("cls");
			game(1);
			break;
		case 2:
			printf("start...\n");
			Sleep(1000);
			system("cls");
			game(2);
			break;
		case 0:
			printf("exit...\n");
			Sleep(1000);
			break;
		default:
			printf("输入错误,请重新输入:\n");
			break;
		}
	} while (input);
	
}

void game(int kind) {
	initBoard();
	int num = 0;
	posi(*another)();
	another = kind == 1 ? computerMove : player2Move;
	posi(*p)();
	posi set;
	do{
		num++;
		printBoard();
		p = (num % 2 == 1) ? playerMove : another;
		set = (*p)();
		//printf("%d %d", set.x, set.y);
		Sleep(500);
	} while (isWin(&set) == false);
	printBoard();
	printf((num % 2 == 1) ? "玩家1胜利!\n" : (kind == 1 ? "电脑胜利!\n" : "玩家2胜利!\n"));
}

posi playerMove() {
	printf("玩家1下 ,请输入坐标:\n");
	int x;
	int y;
	again:
	scanf("%d %d", &x, &y);
	x--; y--;
	if (arr[x][y] == ' ')
		arr[x][y] = '*';
	else {
		printf("请重新输入:");
		goto again;
	}
	system("cls");
	return { x, y };
}

posi player2Move() {
	printf("玩家2下 ,请输入坐标:\n");
	int x;
	int y;
again:
	scanf("%d %d", &x, &y);
	x--; y--;
	if (arr[x][y] == ' ')
		arr[x][y] = '#';
	else {
		printf("请重新输入:");
		goto again;
	}
	system("cls");
	return { x, y };
}

posi computerMove() {
	printf("电脑下...\n");
	Sleep(1000);
	system("cls");
	again2:
	int x = rand() % N;
	int y = rand() % N;

	if (arr[x][y] != '* ' && arr[x][y] != '#')
		arr[x][y] = '#';
	else {

		goto again2;
	}
	return { x, y };
}

int oneWayGo(way that, int x, int y, char it) {
	switch (that)
	{
	case n:
		if (x - 4 >= 0 && 
			arr[x - 2][y] == arr[x - 3][y] && 
			arr[x - 3][y] == arr[x - 4][y] &&
			arr[x - 4][y] == it) {
			return 1;
		}
		break;
	case ne:
		if (x - 4 >= 0 && y + 4 <= N - 1 &&
			arr[x - 2][y + 2] == arr[x - 3][y + 3] && 
			arr[x - 3][y + 3] == arr[x - 4][y + 4] &&
			arr[x - 4][y + 4] == it) {
			return 1;
		}
		break;
	case e:
		if (y + 4 <= N - 1 && 
			arr[x][y + 2] == arr[x][y + 3] &&
			arr[x][y + 3] == arr[x][y + 4] &&
			arr[x][y + 4] == it) {
			return 1;
		}
		break;
	case se:
		if (x + 4 <= N - 1 && y + 4 <= N - 1 &&
			arr[x + 2][y + 2] == arr[x + 3][y + 3] && 
			arr[x + 3][y + 3] == arr[x + 4][y + 4] &&
			arr[x + 4][y + 4] == it) {
			return 1;
		}
		break;
	case s:
		if (x + 4 <= N - 1 && 
			arr[x + 2][y] == arr[x + 3][y] && 
			arr[x + 3][y] == arr[x + 4][y] && 
			arr[x + 4][y] == it) {
			return 1;
		}
		break;
	case sw:
		if (x + 4 <= N - 1 && y - 4 >= 0 &&
			arr[x + 2][y - 2] == arr[x + 3][y - 3] &&
			arr[x + 3][y - 3] == arr[x + 4][y - 4] &&
			arr[x + 4][y - 4] == it) {
			return 1;
		}
		break;
	case w:
		if (y - 4 >= 0 && 
			arr[x][y - 2] == arr[x][y - 3] && 
			arr[x][y - 3] == arr[x][y - 4] && 
			arr[x][y - 4] == it) {
			return 1;
		}
		break;
	case nw:
		if (x - 4 >= 0 && y - 4 >= 0 &&
			arr[x - 2][y - 2] == arr[x - 3][y - 3] && 
			arr[x - 3][y - 3] == arr[x - 4][y - 4] &&
			arr[x - 4][y - 4] == it) {
			return 1;
		}
		break;
	default:
		break;
	}
	return 0;
}

int twoWayGo(way that, int x, int y, char it) {
	switch (that)
	{
	case nw:
		if (x - 2 >= 0 && y - 2 >= 0 && arr[x - 2][y - 2] == it) {
			if (x + 2 <= N - 1 && y + 2 <= N - 1 && arr[x + 2][y + 2] == it) {
				return 1;
			}
			else if (x - 3 >= 0 && y - 3 >= 0 && arr[x - 3][y - 3] == it) {
				return 1;
			}
		}
		else if (x + 2 <= N - 1 && y + 2 <= N - 1 && arr[x + 2][y + 2] == it) {
			if (x + 3 <= N - 1 && y + 3 <= N - 1 && arr[x + 3][y + 3] == it) {
				return 1;
			}
		}
		break;
	case n:
		if (x - 2 >= 0 && arr[x - 2][y] == it) {
			if (x + 2 <= N - 1 && arr[x + 2][y] == it) {
				return 1;
			}
			else if (x - 3 >= 0 && arr[x - 3][y] == it) {
				return 1;
			}
		}
		else if (x + 2 <= N - 1 && arr[x + 2][y] == it) {
			if (x + 3 <= N - 1 && arr[x + 3][y] == it) {
				return 1;
			}
		}
		break;
	case ne:
		if (x - 2 >= 0 && y + 2 <= N - 1 && arr[x - 2][y + 2] == it) {
			if (x + 2 <= N - 1 && y - 2 >= 0 && arr[x + 2][y - 2] == it) {
				return 1;
			}
			else if (x - 3 >= 0 && y + 3 <= N - 1 && arr[x - 3][y + 3] == it) {
				return 1;
			}
		}
		else if (x + 2 <= N - 1 && y - 2 >= 0 && arr[x + 2][y - 2] == it) {
			if (x + 3 <= N - 1 && y - 3 >= 0 && arr[x + 3][y - 3] == it) {
				return 1;
			}
		}
		break;
	case e:
		if (y - 2 >= 0 && arr[x][y - 2] == it) {
			if (y + 2 <= N - 1 && arr[x][y + 2] == it) {
				return 1;
			}
			else if (y - 3 >= 0 && arr[x][y - 3] == it) {
				return 1;
			}
		}
		else if (y + 2 <= N - 1 && arr[x][y + 2] == it) {
			if (y + 3 <= N - 1 && arr[x][y + 3] == it) {
				return 1;
			}
		}
		break;
	
	default:
		break;
	}
	return 0;
}

bool isWin(posi* set) {
	int x = set->x;
	int y = set->y;
	char it = arr[x][y];

	int fact = 0;

	if (arr[x - 1][y - 1] == it) {
		if (arr[x + 1][y + 1] == it) {
			fact = twoWayGo(nw, x, y, it);
		}
		else {
			fact = oneWayGo(nw, x, y, it);
		}
	}
	if (fact == 1)
		return true;//nw

	if (arr[x - 1][y] == it) {
		if (arr[x + 1][y] == it) {
			fact = twoWayGo(n, x, y, it);
		}
		else {
			fact = oneWayGo(n, x, y, it);
		}
	}
	if (fact == 1)
		return true;//n

	if (arr[x - 1][y + 1] == it) {
		if (arr[x + 1][y - 1] == it) {
			fact = twoWayGo(ne, x, y, it);
		}
		else {
			fact = oneWayGo(ne, x, y, it);
		}
	}
	if (fact == 1)
		return true;//ne

	if (arr[x][y + 1] == it) {
		if (arr[x][y - 1] == it) {
			fact = twoWayGo(e, x, y, it);
		}
		else {
			fact = oneWayGo(e, x, y, it);
		}
	}
	if (fact == 1)
		return true;//e

	if (arr[x + 1][y + 1] == it) {
		fact = oneWayGo(se, x, y, it);
	}
	if (fact == 1)
		return true;//se

	if (arr[x + 1][y] == it) {
		fact = oneWayGo(s, x, y, it);
	}
	if (fact == 1)
		return true;//s

	if (arr[x + 1][y - 1] == it) {
		fact = oneWayGo(sw, x, y, it);
	}
	if (fact == 1)
		return true;//sw

	if (arr[x][y - 1] == it) {
		fact = oneWayGo(w, x, y, it);
	}
	if (fact == 1)
		return true;//w

	return false;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值