【C++ 程序】 井字棋游戏(人 VS Lv2电脑)

这是 Lv2 电脑级别,其中的逻辑是1.判断自己是否下一步可获胜,2.判断对手是否下一步会获胜,3.剩余的空位随机选择下在哪里(调用 Lv1 的函数)。

程序

//This program is a simple tic-tac-toe game.

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

char point_now[3][3] = {
    
     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
const string location_computer[3][3] = {
    
     {
    
    "A1","A2","A3"},{
    
    "B1","B2","B3"},{
    
    "C1","C2","C3"} };
char player = 'X';
char computer_player = 'X';
unsigned step_count = 0;

int win_lose(char point[3][3], int n)
{
    
    
	if (point[0][0] == point[0][1] && point[0][1] == point[0][2] && point[0][0] == 'X') return 1; // X wins
	if (point[1][0] == point[1][1] && point[1][1] == point[1][2] && point[1][0] == 'X') return 1; // X wins
	if (point[2][0] == point[2][1] && point[2][1] == point[2][2] && point[2][0] == 'X') return 1; // X wins
	if (point[0][0] == point[1][0] && point[1][0] == point[2][0] && point[0][0] == 'X') return 1; // X wins
	if (point[0][1] == point[1][1] && point[1][1] == point[2][1] && point[0][1] == 'X') return 1; // X wins
	if (point[0][2] == point[1][2] && point[1][2] == point[2][2] && point[0][2] == 'X') return 1; // X wins
	if (point[0][0] == point[1][1] && point[1][1] == point[2][2] && point[1][1] == 'X') return 1; // X wins
	if (point[0][2] == point[1][1] && point[1][1] == point[2][0] && point[1][1] == 'X') return 1; // X wins

	if (point[0][0] == point[0][1] && point[0][1] == point[0][2] && point[0][0] == '0') return 2; // 0 wins
	if (point[1][0] == point[1][1] && point[1][1] == point[1][2] && point[1][0] == '0') return 2; // 0 wins
	if (point[2][0] == point[2][1] && point[2][1] == point[2][2] && point[2][0] == '0') return 2; // 0 wins
	if (point[0][0] == point[1][0] && point[1][0] == point[2][0] && point[0][0] == '0') return 2; // 0 wins
	if (point[0][1] == point[1][1] && point[1][1] == point[2][1] && point[0][1] == '0') return 2; // 0 wins
	if (point[0][2] == point[1][2] && point[1][2] == point[2][2] && point[0][2] == '0') return 2; // 0 wins
	if (point[0][0] == point[1][1] && point[1][1] == point[2][2] && point[1][1] == '0') return 2; // 0 wins
	if (point[0][2] == point[1][1] && point[1][1] == point[2][0] && point[1][1] == '0') return 2; // 0 wins

	if (n == 9) return 3; // end up 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 computer1(char p[3][3])
{
    
    
	string ret;

	unsigned available_n = 49;
	for (int c_i = 0; c_i != 3; c_i++)
	{
    
    
		for (int c_j = 0; c_j != 3; c_j++)
		{
    
    
			if ((p[c_i][c_j] != 'X') && (p[c_i][c_j] != '0'))
				p[c_i][c_j] = available_n++; // mark empty places with numbers 1,2,3...
		}
	}
	srand((unsigned)time(NULL));
	int ran = rand() % (available_n - 49) + 49; // generate a random number
	for (int c_i = 0; c_i != 3; c_i++)
	{
    
    
		for (int c_j = 0; c_j != 3; c_j++)
		{
    
    
			if (p[c_i][c_j] == ran)
				ret = location_computer[c_i][c_j]; // the chosen place
		}
	}
	for (int c_i = 0; c_i != 3; c_i++)
	{
    
    
		for (int c_j = 0; c_j != 3; c_j++)
		{
    
    
			if ((p[c_i][c_j] != 'X') && (p[c_i][c_j] != '0'))
				p[c_i][c_j] = ' '; // return to Space
		}
	}
	return ret; // this is the copmuter-chosen location
}

string computer2(char p[3][3])
{
    
    
	char man_player = (computer_player == 'X') ? '0' : 'X'; // define man_player

	/**/ if (p[0][0] == computer_player && p[0][2] == computer_player && p[0][1] == ' ') return location_computer[0][1];
	else if (p[0][0] == computer_player && p[0][1] == computer_player && p[0][2] == ' ') return location_computer[0][2];
	else if (p[0][1] == computer_player && p[0][2] == computer_player && p[0][0] == ' ') return location_computer[0][0]; // Row A
	else if (p[1][0] == computer_player && p[1][2] == computer_player && p[1][1] == ' ') return location_computer[1][1];
	else if (p[1][0] == computer_player && p[1][1] == computer_player && p[1][2] == ' ') return location_computer[1][2];
	else if (p[1][1] == computer_player && p[1][2] == computer_player && p[1][0] == ' ') return location_computer[1][0]; // Row B
	else if (p[2][0] == computer_player && p[2][2] == computer_player && p[2][1] == ' ') return location_computer[2][1];
	else if (p[2][0] == computer_player && p[2][1] == computer_player && p[2][2] == ' ') return location_computer[2][2];
	else if (p[2][1] == computer_player && p[2][2] == computer_player && p[2][0] == ' ') return location_computer[2][0]; // Row C
	else if (p[0][0] == computer_player && p[2][0] == computer_player && p[1][0] == ' ') return location_computer[1][0];
	else if (p[0][0] == computer_player && p[1][0] == computer_player && p[2][0] == ' ') return location_computer[2][0];
	else if (p[1][0] == computer_player && p[2][0] == computer_player && p[0][0] == ' ') return location_computer[0][0]; // Column 1
	else if (p[0][1] == computer_player && p[2][1] == computer_player && p[1][1] == ' ') return location_computer[1][1];
	else if (p[0][1] == computer_player && p[1][1] == computer_player && p[2][1] == ' ') return location_computer[2][1];
	else if (p[1][1] == computer_player && p[2][1] == computer_player && p[0][1] == ' ') return location_computer[0][1]; // Column 2
	else if (p[0][2] == computer_player && p[2][2] == computer_player && p[1][2] == ' ') return location_computer[1][2];
	else if (p[0][2] == computer_player && p[1][2] == computer_player && p[2][2] == ' ') return location_computer[2][2];
	else if (p[1][2] == computer_player && p[2][2] == computer_player && p[0][2] == ' ') return location_computer[0][2]; // Column 3
	else if (p[0][0] == computer_player && p[2][2] == computer_player && p[1][1] == ' ') return location_computer[1][1];
	else if (p[0][0] == computer_player && p[1][1] == computer_player && p[2][2] == ' ') return location_computer[2][2];
	else if (p[1][1] == computer_player && p[2][2] == computer_player && p[0][0] == ' ') return location_computer[0][0]; // Diagonal 1
	else if (p[0][2] == computer_player && p[2][0] == computer_player && p[1][1] == ' ') return location_computer[1][1];
	else if (p[0][2] == computer_player && p[1][1] == computer_player && p[2][0] == ' ') return location_computer[2][0];
	else if (p[1][1] == computer_player && p[2][0] == computer_player && p[0][2] == ' ') return location_computer[0][2]; // Diagonal 2

	else if (p[0][0] == man_player && p[0][2] == man_player && p[0][1] == ' ') return location_computer[0][1];
	else if (p[0][0] == man_player && p[0][1] == man_player && p[0][2] == ' ') return location_computer[0][2];
	else if (p[0][1] == man_player && p[0][2] == man_player && p[0][0] == ' ') return location_computer[0][0]; // Row A
	else if (p[1][0] == man_player && p[1][2] == man_player && p[1][1] == ' ') return location_computer[1][1];
	else if (p[1][0] == man_player && p[1][1] == man_player && p[1][2] == ' ') return location_computer[1][2];
	else if (p[1][1] == man_player && p[1][2] == man_player && p[1][0] == ' ') return location_computer[1][0]; // Row B
	else if (p[2][0] == man_player && p[2][2] == man_player && p[2][1] == ' ') return location_computer[2][1];
	else if (p[2][0] == man_player && p[2][1] == man_player && p[2][2] == ' ') return location_computer[2][2];
	else if (p[2][1] == man_player && p[2][2] == man_player && p[2][0] == ' ') return location_computer[2][0]; // Row C
	else if (p[0][0] == man_player && p[2][0] == man_player && p[1][0] == ' ') return location_computer[1][0];
	else if (p[0][0] == man_player && p[1][0] == man_player && p[2][0] == ' ') return location_computer[2][0];
	else if (p[1][0] == man_player && p[2][0] == man_player && p[0][0] == ' ') return location_computer[0][0]; // Column 1
	else if (p[0][1] == man_player && p[2][1] == man_player && p[1][1] == ' ') return location_computer[1][1];
	else if (p[0][1] == man_player && p[1][1] == man_player && p[2][1] == ' ') return location_computer[2][1];
	else if (p[1][1] == man_player && p[2][1] == man_player && p[0][1] == ' ') return location_computer[0][1]; // Column 2
	else if (p[0][2] == man_player && p[2][2] == man_player && p[1][2] == ' ') return location_computer[1][2];
	else if (p[0][2] == man_player && p[1][2] == man_player && p[2][2] == ' ') return location_computer[2][2];
	else if (p[1][2] == man_player && p[2][2] == man_player && p[0][2] == ' ') return location_computer[0][2]; // Column 3
	else if (p[0][0] == man_player && p[2][2] == man_player && p[1][1] == ' ') return location_computer[1][1];
	else if (p[0][0] == man_player && p[1][1] == man_player && p[2][2] == ' ') return location_computer[2][2];
	else if (p[1][1] == man_player && p[2][2] == man_player && p[0][0] == ' ') return location_computer[0][0]; // Diagonal 1
	else if (p[0][2] == man_player && p[2][0] == man_player && p[1][1] == ' ') return location_computer[1][1];
	else if (p[0][2] == man_player && p[1][1] == man_player && p[2][0] == ' ') return location_computer[2][0];
	else if (p[1][1] == man_player && p[2][0] == man_player && p[0][2] == ' ') return location_computer[0][2]; // Diagonal 2

	else return computer1(point_now);
}

int main()
{
    
    
	cout << "This program is a simple tic-tac-toe game.\nProgrammer:Teddy van Jerry\n" << endl;
	cout << "Please choose 'Man VS Man'(1) or 'Man VS Computer'(2): ";
	char Man_or_Computer;
	cin >> Man_or_Computer;
	cout << endl;
	char difficulty = '2'; // default setup
	if (Man_or_Computer == '2') // for Man VS Computer
	{
    
    
		cout << "Please choose Difficulty: 'Lv.1 (Easy)'(1) or 'Lv.2 (Medium)'(2): ";
		cin >> difficulty;
		cout << endl;
		cout << "You go first or the computer? You(1), Computer(2): ";
		char You_or_Computer;
		cin >> You_or_Computer;
		cout << endl;
		if (You_or_Computer == '1')
			computer_player = '0';
		else computer_player = 'X';
	}

	// Print an empty board.
	cout << "| |1|2|3|" << endl;
	cout << "|A| | | |" << endl;
	cout << "|B| | | |" << endl;
	cout << "|C| | | |\n" << endl;

	while (win_lose(point_now, step_count) == 0)
	{
    
    
		string location;
		unsigned location_letter = 0; // A/B/C
		unsigned location_number = 0; // 1/2/3

	begin: // a label

		std::cout << "Player " << player << ", make your move: ";
		if(Man_or_Computer=='1')
			cin >> location; // input the location (e.g. B2)
		else
		{
    
    
			if (player != computer_player)
				cin >> location; // input the location (e.g. B2)
			else
			{
    
    
				if(difficulty=='1')
				    location = computer1(point_now);
				if(difficulty=='2')
					location = computer2(point_now);
				cout << location << endl;
			}
			cout << endl;
		}

		switch (location[0])
		{
    
    
		case 'a': case 'A':
			location_letter = 0;
			break;
		case 'b': case 'B':
			location_letter = 1;
			break;
		case 'c': case 'C':
			location_letter = 2;
			break;
		default: // illegal input
			location_letter = 100; // indicate an error
			break;
		}

		switch (location[1])
		{
    
    
		case '1':
			location_number = 0;
			break;
		case '2':
			location_number = 1;
			break;
		case '3':
			location_number = 2;
			break;
		default: // illegal input
			location_number = 100; // indicate an error
			break;
		}

		try
		{
    
    
			if (location_letter != 100 && location_number != 100 && point_now[location_letter][location_number] == ' ')
				point_now[location_letter][location_number] = player;
			else throw runtime_error("Illegal input!");
		}
		catch (runtime_error err)
		{
    
    
			cout << err.what() << "\nTry Again? Enter Y or N." << endl;
			char decision;
			cin >> decision;
			if (!cin || decision == 'n' || decision == 'N')
				break;
			else
			{
    
    
				cout << endl;
				goto begin; // go back to the label 'begin'
			}
		}

		// Print the board
		cout << "| |1|2|3|" << endl;
		cout << "|A|" << point_now[0][0] << "|" << point_now[0][1] << "|" << point_now[0][2] << "|" << endl;
		cout << "|B|" << point_now[1][0] << "|" << point_now[1][1] << "|" << point_now[1][2] << "|" << endl;
		cout << "|C|" << point_now[2][0] << "|" << point_now[2][1] << "|" << point_now[2][2] << "|" << endl;

		game_player_change(player); // change the player
		++step_count; // count one more time
		cout << endl;
	}

	int final_result = win_lose(point_now, step_count);
	char winner = 'N';
	switch (final_result)
	{
    
    
	case 1:
		winner = 'X';
		cout << "Congratulations! The winner is X." << endl;
		break;
	case 2:
		winner = '0';
		cout << "Congratulations! The winner is 0." << endl;
		break;
	case 3:
		cout << "The game ended in a draw." << endl;
		break;
	}

	if (Man_or_Computer == '2')
	{
    
    
		if (winner == computer_player)
			cout << "Computer won!" << endl;
		else
		{
    
    
			if (winner != 'N')
				cout << "You won!!!" << endl;
		}
	}

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

//Copyright :2020 Teddy van Jerry

输出示例

Output

这个没那么容易赢电脑了。

分析

  • 对于1.判断自己是否下一步可获胜,2.判断对手是否下一步会获胜,本程序是比较暴力的,我直接枚举出所有情况。
  • 此程序兼容 人 VS 人, 人 VS Lv1 电脑,开局时可以选择。
  • 该程序一样对于不合法输入进行了判别(分为两种:1.坐标超出范围,2.输入到原来已有棋子的位置上),详见【C++ 程序】 井字棋游戏(人 VS 人)中的分析。

ALL RIGHTS RESERVED © 2020 Teddy van Jerry
欢迎转载,转载请注明出处。


See also

Teddy van Jerry 的导航页
【C++ 程序】 井字棋游戏(人 VS 人)
【C++ 程序】 井字棋游戏(人 VS Lv1电脑)
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)
【C++ 程序】 五子棋游戏(人 VS 人)
【C++ 程序】 随机数
【C++ 程序】 移动迷宫游戏
【C++ 程序】 贪吃蛇游戏
【C++ 程序】 数字推盘游戏(15-puzzle)
【C++ 程序】 2048游戏
【C++ 程序】 井字棋游戏(人 VS 人)(EasyX 图形界面)
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)(EasyX 图形界面)

猜你喜欢

转载自blog.csdn.net/weixin_50012998/article/details/108338400