代码实现
#define _CRT_SECURE_NO_WARNINGS
#define MAX_ROW 3
#define MAX_COL 3
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std;
void init(char chessBoard[MAX_ROW][MAX_COL])//初始化棋盘
{
for (int row = 0; row < MAX_ROW; row++)
{
for (int col = 0; col < MAX_COL; col++)
{
chessBoard[row][col] = ' ';
}
}
}
void print_chessBoard(char chessBoard[MAX_ROW][MAX_COL]){
cout<<"+---+---+---+"<<endl;
for (int row = 0; row <MAX_ROW; row++) {
printf("| %c | %c | %c |\n", chessBoard[row][0],
chessBoard[row][1], chessBoard[row][2]);
cout<<"+---+---+---+"<<endl;
}
}
void playerMove(char chessBoard[MAX_ROW][MAX_COL]) {
while (1) {
int row = 0;
int col = 0;
cout<<"请输入坐标(row col):"<<endl;
cin>>row>>col;
if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL)
{
cout<<"您的棋都下到棋盘外了 请重新输入:";
continue;
}
if (chessBoard[row][col] != ' ') {
cout<<"您的坐标位置已经有子了!"<<endl;
continue;
}
chessBoard[row][col] = 'x';
break;
}
}
void computerMove(char chessBoard[MAX_ROW][MAX_COL]) {
while (1) {
int row = rand() % MAX_ROW;
int col = rand() % MAX_COL;
if (chessBoard[row][col] != ' ') {
continue;
}
chessBoard[row][col] = 'o';
break;
}
}
int isFull(char chessBoard[MAX_ROW][MAX_COL]) {
for (int row = 0; row < MAX_ROW; row++) {
for (int col = 0; col < MAX_COL; col++) {
if (chessBoard[row][col] == ' ') {
return 0;
}
}
}
return 1;
}
char isWin(char chessBoard[MAX_ROW][MAX_COL]) {
for (int row = 0; row < MAX_ROW; row++) {
if (chessBoard[row][0] != ' '
&& chessBoard[row][0] == chessBoard[row][1]
&& chessBoard[row][0] == chessBoard[row][2])
{
return chessBoard[row][0];
}
}
for (int col = 0; col < MAX_COL; col++) {
if (chessBoard[0][col] != ' '
&& chessBoard[0][col] == chessBoard[1][col]
&& chessBoard[0][col] == chessBoard[2][col])
{
return chessBoard[0][col];
}
}
if (chessBoard[0][0] != ' '
&& chessBoard[0][0] == chessBoard[1][1]
&& chessBoard[0][0] == chessBoard[2][2]) {
return chessBoard[0][0];
}
if (chessBoard[2][0] != ' '
&& chessBoard[2][0] == chessBoard[1][1]
&& chessBoard[2][0] == chessBoard[0][2]) {
return chessBoard[2][0];
}
if (isFull(chessBoard)) {
return 'q';
}
return ' ';
}
void game() {
char chessBoard[MAX_ROW][MAX_COL] = { 0 };
init(chessBoard);
char winner = ' ';
while (1) {
print_chessBoard(chessBoard);
playerMove(chessBoard);
winner = isWin(chessBoard);
if (winner != ' ') {
break;
}
computerMove(chessBoard);
winner = isWin(chessBoard);
if (winner != ' ') {
break;
}
}
print_chessBoard(chessBoard);
if (winner == 'x') {
cout<<"恭喜您, 您赢了!";
}
else if (winner == 'o') {
cout<<"您连人工智障都下不过!(低情商) 您这局状态不好(高情商)!!"<<endl;
}
else {
cout<<"你水平也就这样(低情商) 你居然能和AI达成平手(高情商)!!"<<endl;
}
}
int menu() {
cout<<"--------------------------\n";
cout << "--------1.开始游戏--------\n";
cout << "--------0.退出游戏--------\n";
cout << "--------------------------\n";
int choice = 0;
cout<<"召唤师 请输入你的选择:";
cin>>choice;
return choice;
}
int main()
{
srand((unsigned int)time(0));
while (1) {
int choice = menu();
if (choice == 1) {
game();
}
else if (choice == 0) {
cout<<"退出游戏,我不玩了";
break;
}
else {
cout<<"输入错误!请重新输入!";
continue;
}
}
return 0;
}