三角网络五子棋--六边形棋盘(人机对战)

规则:
黑棋先行,白棋后行,黑白棋交替进行。落子只能落在未有落子的三条线的共同交叉点上。 3. 当一方在率先组成了五子相连直线,则该方胜利,游戏结束。
打印棋盘:

void Board::print()
{
	int len_=0;

	//前7行
	while(len_<7)
	{
		for (int i = 0; i < N-len_-1; i++)
			cout << " ";
		cout<<len_<<" ";
		for (int k = 0; k <= len_; k++)
			cout << board[len_ -k][k] << " ";
		cout <<len_ << endl;
		len_++;
	}
	//中间行
	len_ = 0;
	int x = 7;
	int y = 0;
	int len=7,flag=1;
	while (len_ < 8)
	{
		if (len_ + 7 < 10)len = 7;
		else len = 6;
		for (int i = 0; i < len; i++)
			cout << " ";
		if (flag == 1)
		{
			flag = 0;

			cout << len_ + 7<<" ";
			for (y = len_; y <= len_ + 7; y++)
				cout << board[x - y][y] << " ";
			cout << len_ + 7 << endl;
			if (len_ == 7)
				break;
		}
		else
		{
			flag = 1;
			cout << "   ";
			if (len_ + 7 >= 10)
				cout << " ";
			for (y = len_+1; y <= len_ + 7; y++)
				cout << board[x - y+1][y] << " ";
			cout << endl;
			x += 2; len_++;
		}
	}

	//后七行
	len_ = 7
		;
	x = 14+8;
	y = 8;
	while (len_ >=0)
	{
		for (int i = 0; i <= N - len_+1; i++)
			cout << " ";
		for (int k=y;k<=14;k++)
			cout << board[x-k][k] << " ";
		cout  << endl;
		len_--; y++; x++;
	}
}

 总结:ai算法还是有点问题,但已经能够提交。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
三角网络五子棋是一种基于三角形网格的变体五子棋游戏,玩家需要在三角形交汇处落子,而不是在传统的方格交汇处落子。以下是一个基于C++的简单实现: ```c++ #include <iostream> #include <vector> #include <ctime> #include <cstdlib> using namespace std; const int BOARD_SIZE = 5; // 三角棋盘大小 const int EMPTY = 0; // 空格子 const int PLAYER_X = 1; // 玩家X方 const int PLAYER_O = 2; // 玩家O方 const int WIN_COUNT = 3; // 获胜所需连续棋子数 // 三角棋盘类 class TriangularBoard { public: TriangularBoard() { board.resize(BOARD_SIZE); for (int i = 0; i < BOARD_SIZE; i++) { board[i].resize(BOARD_SIZE - i); } current_player = PLAYER_X; srand(time(NULL)); } // 判断当前位置是否可以下子 bool is_valid_move(int row, int col) { if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE - row) { return false; } return board[row][col] == EMPTY; } // 下子 void make_move(int row, int col) { board[row][col] = current_player; if (current_player == PLAYER_X) { current_player = PLAYER_O; } else { current_player = PLAYER_X; } } // 判断当前状态是否结束 bool is_game_over() { return get_winner() != EMPTY || get_available_moves().empty(); } // 获取获胜方 int get_winner() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE - i; j++) { int winner = get_winner_at_position(i, j); if (winner != EMPTY) { return winner; } } } return EMPTY; } // 获取可下子位置列表 vector<pair<int, int>> get_available_moves() { vector<pair<int, int>> moves; for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE - i; j++) { if (is_valid_move(i, j)) { moves.push_back(make_pair(i, j)); } } } return moves; } // 获取当前玩家 int get_current_player() { return current_player; } // 打印棋盘 void print_board() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < i; j++) { cout << " "; } for (int j = 0; j < BOARD_SIZE - i; j++) { if (board[i][j] == PLAYER_X) { cout << "X "; } else if (board[i][j] == PLAYER_O) { cout << "O "; } else { cout << ". "; } } cout << endl; } } private: // 获取指定位置的获胜方 int get_winner_at_position(int row, int col) { if (board[row][col] == EMPTY) { return EMPTY; } int count1 = 0, count2 = 0, count3 = 0; int x = row, y = col; while (x < BOARD_SIZE && y >= 0) { if (board[x][y] == board[row][col]) { count1++; } else { break; } x++; y--; } x = row, y = col; while (x >= 0 && y < BOARD_SIZE - x) { if (board[x][y] == board[row][col]) { count1++; } else { break; } x--; y++; } x = row, y = col; while (y < BOARD_SIZE - x) { if (board[x][y] == board[row][col]) { count2++; } else { break; } y++; } x = row, y = col; while (y >= 0) { if (board[x][y] == board[row][col]) { count2++; } else { break; } y--; } x = row, y = col; while (x < BOARD_SIZE) { if (board[x][y] == board[row][col]) { count3++; } else { break; } x++; } x = row, y = col; while (x >= 0) { if (board[x][y] == board[row][col]) { count3++; } else { break; } x--; } if (count1 >= WIN_COUNT || count2 >= WIN_COUNT || count3 >= WIN_COUNT) { return board[row][col]; } else { return EMPTY; } } vector<vector<int>> board; // 棋盘 int current_player; // 当前玩家 }; // 人机对战类 class HumanVsAI { public: HumanVsAI() { board = TriangularBoard(); srand(time(NULL)); } // 进行游戏 void play() { while (!board.is_game_over()) { board.print_board(); if (board.get_current_player() == PLAYER_X) { cout << "Your turn (row col): "; int row, col; cin >> row >> col; if (board.is_valid_move(row, col)) { board.make_move(row, col); } else { cout << "Invalid move!" << endl; } } else { cout << "Computer's turn..." << endl; vector<pair<int, int>> moves = board.get_available_moves(); int index = rand() % moves.size(); board.make_move(moves[index].first, moves[index].second); } } board.print_board(); int winner = board.get_winner(); if (winner == PLAYER_X) { cout << "You win!" << endl; } else if (winner == PLAYER_O) { cout << "Computer wins!" << endl; } else { cout << "Tie!" << endl; } } private: TriangularBoard board; // 三角棋盘 }; int main() { HumanVsAI game; game.play(); return 0; } ``` 该程序实现了一个简单的人机对战,玩家使用行列坐标指定下子位置,计算机随机选择一个可用位置下子。可以根据需要对程序进行扩展,例如增加计算机的智能水平,让它根据当前局面选择最优的下子位置。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值