五子棋(源代码)

朋友们今天小编为大家带来五子棋游戏的源代码。

提醒:小编带来的代码是以坐标形式来下棋的。

源代码如下:

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
using namespace std;
const int N = 15; 
const char ChessBoard = ' '; 
const char flag1 = 'o'; 
const char flag2 = 'x'; 
typedef struct Position{ 
 int row; 
 int col; 
}Position;

class GoBang{ 
public:
 GoBang(){
 InitChessBoard(); 
 }
 void Play(){
 Position Play1;
 Position Play2;
 while (1){
 int mode = ChoiceMode();
 while (1){
 if (mode == 1){
 ComputerChess(Play1, flag1); 
 if (GetVictory(Play1, 0, flag1)){ 
 break;
 }
 PlayChess(Play2, 2, flag2); 
 if (GetVictory(Play2, 2, flag2)){ 
 break;
 }
 }
 else{
 PlayChess(Play1, 1, flag1);
 if (GetVictory(Play1, 1, flag1)){
 break;
 }
 PlayChess(Play2, 2, flag2); 
 if (GetVictory(Play2, 2, flag2)){ 
 break;
 }
 }
 }
 cout << "======再来一局=======" << endl;
 cout << "yes or no :"; 
 char s[] = "yes";
 cin >> s;
 if (strcmp(s, "no") == 0){
 break;
 }
 }
 }
protected:
 void InitChessBoard(){ 
 for (int i = 0; i < N + 1; ++i){
 for (int j = 0; j < N + 1; ++j){
 _ChessBoard[i][j] = ChessBoard;
 }
 }
 }
 int ChoiceMode(){
 system("cls");
 InitChessBoard();
 cout << "*************************************************" << endl;
 cout << "******************0、退出************************" << endl;
 cout << "******************1、电脑VS玩家******************" << endl;
 cout << "******************2、玩家VS玩家******************" << endl;
 cout << "*************************************************" << endl;
 while (1){
 int i = 0;
 cout << "请选择模式:";
 cin >> i;
 if (i == 0){
 exit(1);
 }
 if (i == 1 || i == 2){
 return i;	
 }
 else{
 cout << "非法输入,请重新输入!" << endl;
 }
 }
 }

 void PrintChessBoard(){ //打印棋盘
 printf("     1   2   3   4   5   6   7   8   9   10  11  12  13  14  15\n");
 printf(" |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");
 for (int i = 1; i < N + 1; ++i)
 {
 printf("%2d ", i);
 printf("| %c | %c | %c | %c | %c | %c | %c | %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], _ChessBoard[i][9], _ChessBoard[i][10], _ChessBoard[i][11], _ChessBoard[i][12], _ChessBoard[i][13], _ChessBoard[i][14], _ChessBoard[i][15]);
 printf(" |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");
 }
 cout << endl;
 }
 void ComputerChess(Position& pos, char flag){ 
 int x = 0;
 int y = 0;
 while (1){ 
 x = (rand() % N) + 1; 
 srand((unsigned int)time(NULL));
 y = (rand() % N) + 1; 
 srand((unsigned int)time(NULL));
 if (_ChessBoard[x][y] == ChessBoard){
 break;
 }
 }
 pos.row = x;
 pos.col = y;
 _ChessBoard[pos.row][pos.col] = flag;
 }

 void PlayChess(Position& pos, int player, char flag){
 PrintChessBoard(); 
 while (1){
 printf("请玩家%d输入坐标:", player);
 cin >> pos.row >> pos.col;
 if (JudgeValue(pos) == 1){ 
 break;
 }
 cout << "坐标不合法,请重新输入:" << endl;
 }
 _ChessBoard[pos.row][pos.col] = flag;
 }
 int JudgeValue(const Position& pos){ 
 if (pos.row > 0 && pos.row <= N && pos.col > 0 && pos.col <= N){
 if (_ChessBoard[pos.row][pos.col] == ChessBoard){
 return 1; 
 } 
 }
 return 0;
 }
 int JudgeVictory(Position pos, char flag){ 
 int begin = 0;
 int end = 0;
 (pos.col - 4) > 0 ? begin = (pos.col - 4) : begin = 1;
 (pos.col + 4) > N ? end = N : end = (pos.col + 4);
 for (int i = pos.row, j = begin; j + 4 <= end; ++j){
 if (_ChessBoard[i][j] == flag && _ChessBoard[i][j + 1] == flag &&
 _ChessBoard[i][j + 2] == flag && _ChessBoard[i][j + 3] == flag &&
 _ChessBoard[i][j + 4] == flag)
 return 1;
 }
 (pos.row - 4) > 0 ? begin = (pos.row - 4) : begin = 1;
 (pos.row + 4) > N ? end = N : end = (pos.row + 4);
 for (int j = pos.col, i = begin ; i + 4 <= end; ++i){
 if (_ChessBoard[i][j] == flag && _ChessBoard[i + 1][j] == flag &&
 _ChessBoard[i + 2][j] == flag && _ChessBoard[i + 3][j] == flag &&
 _ChessBoard[i + 4][j] == flag)
 return 1;
 }
 int len = 0;
 int start = 0;
 int finish = 0;
 pos.row > pos.col ? len = pos.col - 1 : len = pos.row - 1;
 if (len > 4){
 len = 4;
 }
 begin = pos.row - len; 
 start = pos.col - len; 

 pos.row > pos.col ? len = N - pos.row : len = N - pos.col;
 if (len > 4){
 len = 4;
 }
 end = pos.row + len; 
 finish = pos.col + len; 
 for (int i = begin, j = start; (i + 4 <= end) && (j + 4 <= finish); ++i, ++j){
 if (_ChessBoard[i][j] == flag && _ChessBoard[i + 1][j + 1] == flag &&
 _ChessBoard[i + 2][j + 2] == flag && _ChessBoard[i + 3][j + 3] == flag &&
 _ChessBoard[i + 4][j + 4] == flag)
 return 1;
 }
 (pos.row - 1) > (N - pos.col) ? len = N - pos.col : len = pos.row - 1;
 if (len > 4){
 len = 4;
 }
 begin = pos.row - len;
 start = pos.col + len; 

 (N - pos.row) > (pos.col - 1) ? len = pos.col - 1 : len = N - pos.row;
 if (len > 4){
 len = 4;
 }
 end = pos.row + len; 
 finish = pos.col - len; 
 for (int i = begin, j = start; (i + 4 <= end) && (j - 4 >= finish); ++i, --j){
 if (_ChessBoard[i][j] == flag && _ChessBoard[i + 1][j - 1] == flag &&
 _ChessBoard[i + 2][j - 2] == flag && _ChessBoard[i + 3][j - 3] == flag &&
 _ChessBoard[i + 4][j - 4] == flag)
 return 1;
 }
	for (int x = 1; x < N + 1; ++x){
	for (int y = 1; y < N + 1; ++y){
	if (_ChessBoard[x][y] == ChessBoard){
		return 0;
			}
		}
	}
	return -1;
}
	bool GetVictory(Position& pos, int player, char flag){
	if (JudgeVictory(pos, flag) != 0){
		if(JudgeVictory(pos, flag) == 1){
			PrintChessBoard();
				if(player == 0){
					cout << "电脑获胜!" << endl;
				}
				else{
					printf("恭喜玩家%d获胜!\n", player);
				}
}
 else{
		printf("和局!\n");
	}
		return true;
	} 
		return false;
	}
	private:
	char _ChessBoard[N + 1][N + 1];
};
int main(){
	GoBang g;
	g.Play();
	system("pause");
	return 0;
}

希望大家可以为小编点一个赞。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值