【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)

这是 Lv3 电脑级别,其中的逻辑是1.由我自己推导电脑不败之法,2.当没有变数时,调用 Lv2 函数
此外,此版本可以选择重复游戏,与电脑对战时统计战绩


图形界面(鼠标控制)见我的博客:
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)(EasyX 图形界面)


程序

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

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

vector<vector<char>> point_now{
    {
    ' ', ' ', ' '},{
   ' ', ' ', ' '},{
    ' ', ' ', ' '} };
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(vector<vector<char>> point, 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(vector<vector<char>> p) // Computer Lv.1
{
   
	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(vector<vector<char>> p
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值