[Programme C ++] Jeu de Gobang (ordinateur humain VS Lv1) (idées et cadre, contenu à remplir)

Idées

Le premier est le niveau le plus important, c'est-à-dire que vous pouvez gagner à l'étape suivante, d'abord vous-même et ensuite l'adversaire.
Vient ensuite le deuxième niveau le plus important, c'est-à-dire que la prochaine étape sera une situation meurtrière. Dans le même temps, il est nécessaire de réaliser des statistiques quantitatives pour ce type de poste, et de choisir le plus efficace, d'abord soi-même puis l'autre partie.
Enfin, l'importance est notée. Score basé sur les huit directions d'un point vide. Vers le bas dans la position la plus élevée.
Mais la situation est trop compliquée, je ne peux pas écrire plus de 800 lignes, et j'utilise la fonction to_string de manière incorrecte, puis ça explose.
Alors mémorisez d'abord vos idées et ajustez-les plus tard si vous en avez l'occasion.

Procédure (inachevée)

//This program is an unfinished simple gobang game.

#include <iostream>
#include <string>
#include <vector>
#include <cstddef>
#include <ctime>
using namespace std;

char player = 'X';
char computer_player = 'X';
unsigned step_count = 0;
vector<vector<char>> p_n; // struct an undefined board;

void def_empty_board(vector<vector<char>>& p) // define an empty 15X15 board
{
    
    
	vector<char> p_n_temp;
	for (size_t i = 0; i != 15; i++) p_n_temp.push_back(' '); // the inner vector
	for (size_t i = 0; i != 15; i++) p.push_back(p_n_temp);   // the outer vector(i.e. the board)
}

void print_board(vector<vector<char>> p)
{
    
    
	char ch_15[15];
	for (size_t i = 0; i != 15; i++) ch_15[i] = 65 + i; // A-O ~ 65-79 (according to ASCII)
	cout << "| |0|0|0|0|0|0|0|0|0|1|1|1|1|1|1|" << endl;
	cout << "| |1|2|3|4|5|6|7|8|9|0|1|2|3|4|5|" << endl;
	for (size_t i = 0; i != 15; i++) // print 15 lines from A to O
	{
    
    
		cout << '|' << ch_15[i] << '|';
		for (size_t j = 0; j != 15; j++)
			cout << p[i][j] << '|';
		cout << endl;
	}
	cout << endl;
}

int change_board(vector<vector<char>>& p, string l)
{
    
    
	size_t location_letter, location_number;
	if (l.size() == 2)
	{
    
    
		if (toupper(l[0]) >= 65 && l[1] >= 48)
		{
    
    
			location_letter = toupper(l[0]) - 65; // row
			location_number = l[1] - 48 - 1;      // column
			if (location_letter > 14 || location_number > 9) return -1; // PASS
		}
		else return -1; // no change is made, i.e. PASS
	}
	else if (l.size() == 3)
		if (toupper(l[0]) >= 65 && l[1] >= 48 && l[2] >= 48)
		{
    
    
			location_letter = toupper(l[0]) - 65;                 // row
			location_number = 10 * (l[1] - 48) + (l[2] - 48) - 1; // column
			if (location_letter > 14 || location_number > 14) return -1; // PASS
		}
		else return -1; // no change is made, i.e. PASS
	else return -1;     // no change is made, i.e. PASS
	if (p[location_letter][location_number] == ' ')
	{
    
    
		p[location_letter][location_number] = player; // change the board
		return 0; // indicate success
	}
	else return -1; // no change is made, i.e. PASS
}

int win_lose(vector<vector<char>> p, int n)
{
    
    
	char c = 'X';
	for (int k = 1; k != 3; k++) // k = 1(check 'X') ; k = 2(check '0')
	{
    
    
		for (size_t i = 0; i <= 14; i++)
		{
    
    
			for (size_t j = 0; j <= 10; j++)
			{
    
    
				if (p[i][j] == c && p[i][j + 1] == c && p[i][j + 2] == c && p[i][j + 3] == c && p[i][j + 4] == c) // five in a row
					return k;
			}
		}
		for (size_t i = 0; i <= 10; i++)
		{
    
    
			for (size_t j = 0; j <= 14; j++)
			{
    
    
				if (p[i][j] == c && p[i + 1][j] == c && p[i + 2][j] == c && p[i + 3][j] == c && p[i + 4][j] == c) // five in a column
					return k;
			}
		}
		for (size_t i = 0; i <= 10; i++)
		{
    
    
			for (size_t j = 0; j <= 10; j++)
			{
    
    
				if (p[i][j] == c && p[i + 1][j + 1] == c && p[i + 2][j + 2] == c && p[i + 3][j + 3] == c && p[i + 4][j + 4] == c) // five in a diagonal1
					return k;
			}
		}
		for (size_t i = 0; i <= 10; i++)
		{
    
    
			for (size_t j = 4; j <= 14; j++)
			{
    
    
				if (p[i][j] == c && p[i + 1][j - 1] == c && p[i + 2][j - 2] == c && p[i + 3][j - 3] == c && p[i + 4][j - 4] == c) // five in a diagonal2
					return k;
			}
		}
		c = '0'; // Then test player '0'.
		// 'return 1' indicates 'X' wins while 'return 2' indicates '0' wins.	
	}
	if (n == 225) return 3; // the board if full and no win, end in a draw
	else return 0; //unfinished
}

void game_player_change(char& player)
{
    
    
	if (player == 'X')
		player = '0'; // X -> 0
	else player = 'X';// 0 -> X
}

string priority1(vector<vector<char>> p) // top priority
{
    
    
	// top priority
}

string priority2(vector<vector<char>> p)
{
    
    
	// second priority
}

string signifacance(vector<vector<char>> p)
{
    
    
    // rank by signifance
}

string computer1(vector<vector<char>> p) // Computer Lv.1
{
    
    
	// priority1 -> priority2 -> significance
}

int main(int argc, char* argv[])
{
    
    
	string location;
	cout << "This program is a simple gobang game.\nProgrammer:Teddy van Jerry\n" << endl;
	cout << "If your input is illegal, we define it as you choose to PASS.(Or you can type in Pass to pass)" << endl;
	cout << "You can type in the location like 'B2' or 'B02', and no whitespace is allowed.\n" << endl; // a reminder
	cout << "Please choose 'Man VS Man'(1) or 'Man VS Computer'(2) : ";
	char Man_or_Computer;
	cin >> Man_or_Computer;
	cout << "\nYou go first or the computer? You(1), Computer(2): ";
	char You_or_Computer;
	cin >> You_or_Computer;
	std::cout << endl;
	computer_player = (You_or_Computer == '1') ? '0' : 'X';
	def_empty_board(p_n);
	print_board(p_n);
	while (win_lose(p_n, step_count) == 0) // while the game is unfinished
	{
    
    

		cout << "Player " << player << ", make your move: ";
		if (Man_or_Computer == 1 or player != computer_player)
			cin >> location;
		else
		{
    
    
			location = computer1(p_n);
			cout << location << endl;
		}
		if (change_board(p_n, location) == 0) // change the board and test whether it's a PASS
		{
    
    
			cout << endl;
			print_board(p_n);
			++step_count; // count one more time
		}
		else cout << "Player " << player << " choose PASS.\n" << endl;
		game_player_change(player);
	}
	switch (win_lose(p_n, step_count))
	{
    
    
	case 1:
		cout << "Congratulations! The winner is X." << endl;
		break;
	case 2:
		cout << "Congratulations! The winner is 0." << endl;
		break;
	case 3:
		cout << "The game ended in a draw." << endl;
	default:
		break;
	}

	if (player == computer_player)
		cout << "Computer won!" << endl;
	else cout << "You won!!!" << endl;

	cout << "\nALL RIGHTS RESERVED (c) 2020 Teddy van Jerry" << endl;
	return 0;
}

//Copyright :2020 Teddy van Jerry

Exemple de sortie

Production
un échec total !!!


TOUS DROITS RÉSERVÉS © 2020 Teddy van Jerry
Bienvenue à réimprimer, veuillez indiquer la source.


Voir également

Page de navigation de Teddy van Jerry
[programme C ++] Jeu Gobang (humain VS humain)
[programme C ++] Jeu Tic-Tac-Toe (humain VS humain)
[programme C ++] Jeu Tic-Tac-Toe (ordinateur VS humain Lv1)
[programme C ++] Tic Tac Toe Jeu d'échecs (Ordinateur humain VS Lv2)
[Programme C ++] Jeu Tic-Tac-Toe (Ordinateur humain VS Lv3)
[Programme C ++] Jeu Tic-Tac-Toe (Ordinateur humain VS Lv3) (Version statistique)
[Programme C ++] Jeu de labyrinthe mobile
[C ++ Programme] Jeu de serpent
[programme C ++] Jeu de société push numérique (15-puzzle)
[programme C ++] Jeu 2048
[ programme C ++] Jeu Tic-tac-toe (humain VS humain) (interface graphique EasyX)
[programme C ++] Tic-Tac-Toe Jeu (Human VS Lv3 Computer) (Statistics Edition) (EasyX Graphical Interface)

Je suppose que tu aimes

Origine blog.csdn.net/weixin_50012998/article/details/108380352
conseillé
Classement