这是 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