我的答案:
首先是问题的最后一部分:编码看起来不错,如果您对某些操作进行评论,即使程序非常引人注目(// /* */),我也会很高兴。
在我看来,你的问题在解决练习方面有点误导。我一开始也是这样想你的。问题是模式不仅仅被频率 3 检测到,因为它会允许错误的检查模式。使其可视化:
7 8 9
4 5 6
1 2 3
fe 3 的频率如 1,2,5 或 9 6 5 不是正确答案,但 - 1 5 9 是。即使你计算了每一个频率模式——它也会比我在我的解决方案的以下博客中定义的解决方案编码更多:
status_check = possibilities(field,player,2,0,1,1,0,2); //diagonal 1 5 9
status_check = possibilities(field,player,0,0,1,1,2,2); //diagonal 7 5 3
status_check = possibilities(field,player,0,0,1,0,2,0); //vertical 7 4 1
status_check = possibilities(field,player,0,1,1,1,2,1); //vertical 8 5 2
status_check = possibilities(field,player,0,2,1,2,2,2); //vertical 9 6 3
status_check = possibilities(field,player,0,0,0,1,0,2); //horizontal 7 8 9
status_check = possibilities(field,player,1,0,1,1,1,2); //horizontal 4 5 5
status_check = possibilities(field,player,2,0,2,1,2,2); //horizontal 1 2 3
*这就是我认为可以使用数字键盘进行锻炼的方式。(我为那段代码感到非常自豪。:-))
顺便说一句,我很喜欢那本书,但很遗憾,我想不出那本书:“奖励:你能不能让你的程序在整个格子填满之前检测出比赛是否不能由任何一方获胜?” 但如果有人跳过那一章 - 这个解决方案肯定会有所帮助:*
练习如下(第 121 页,跳转到 Cpp):
编写一个小型井字游戏程序,让两个玩家竞争性地玩井字游戏。您的程序应检查是否有任何玩家获胜,或者棋盘是否已完全填满(游戏以平局结束)。奖励:你可以让你的程序在整个网格被填满之前检测到任何一方都无法赢得比赛吗?
以下代码可以使用 c++ 17 标准进行简单编译和执行,并且所有库都应该在 gcc (GCC) 8.2.1 中可用
#include <iostream>
#include <string>
#include <limits> //just for the input validation
using namespace std;
int input_check(char field[3][3],char player);
void display_field(char field[3][3],int size);
void initialize_field(char field[3][3],int size,char player);
string win_check(char field[3][3],char player,int size);
int code(char field[3][3],int i,int j,char player);
int possibilities(char field[3][3],char player,int a,int aa,int b,int bb,int c,int cc);
int main()
{
char field[3][3];
int size = 3;
char player ='-';
initialize_field(field,size,player);
display_field(field,size);
while (field[0][0]!='w')
{
player = 'X';
input_check(field,player);
system("clear");
display_field(field,size);
cout << win_check(field,player,size);
if (field[0][0] =='w')
{
cout << endl;
break;
}
player = 'O';
input_check(field,player);
system("clear");
display_field(field,size);
cout << win_check(field,player,size);
if (field[0][0] =='w')
{
cout << endl;
break;
}
}
return 0;
}
void initialize_field(char field[3][3],int size,char player)
{
int i,k;
for (i = 0; i < size; i++)
{
for (k = 0; k < size; k++)
{
field[i][k]= player;
}
}
}
int input_check(char field[3][3],char player)
{
int input = 0;
bool def = false;
while (def != true)
{
while(!(cin >> input))
{ //check the Input format for integer the right way
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Try again: ";
}
switch (input)
{
case 1:{
def = code(field,2,0,player);
break;}
case 2:{
def = code(field,2,1,player);
break;}
case 3:{
def = code(field,2,2,player);
break;}
case 4:{
def = code(field,1,0,player);
break;}
case 5:{
def = code(field,1,1,player);
break;}
case 6:{
def = code(field,1,2,player);
break;}
case 7:{
def = code(field,0,0,player);
break;}
case 8:{
def = code(field,0,1,player);
break;}
case 9:{
def = code(field,0,2,player);
break;}
default:{
cout << "Invalid input. Try again: " << endl;
break;}
}
}
/*
7 8 9 00 01 02
4 5 6 10 11 12
1 2 3 20 21 22
*/
return input;
}
int code(char field[3][3],int i,int j,char player)
{
int def=0;
if (field[i][j]=='-')
{
field[i][j]=player;
def = true;
}
else
{
cout << "Invalid input. Try again: " << endl;
}
return def;
}
void display_field(char field[3][3],int size)
{
int i,k;
for (i = 0; i < size; i++)
{
for (k = 0; k < size; k++)
{
cout << field[i][k];
cout << " ";
if (k==2) //seperate with new line after the third k
{
cout << endl;
}
}
}
}
string win_check(char field[3][3],char player,int size)
{
string status;
int status_check;
/*
7 8 9 00 01 02
4 5 6 10 11 12
1 2 3 20 21 22
*/
if ((field[0][0]!='-')&&(field[0][1]!='-')&&(field[0][2]!='-') &&(field[1][0]!='-')&&(field[1][1]!='-')&&(field[1][2]!='-')&&(field[2][0]!='-')&&(field[2][1]!='-')&&(field[2][2]!='-'))
{
status = "Rien ne va plus - Nichts geht mehr meine Lieben. Unentschieden";
field[0][0]='w';
}
status_check = possibilities(field,player,2,0,1,1,0,2); //diagonal 1 5 9
status_check = possibilities(field,player,0,0,1,1,2,2); //diagonal 7 5 3
status_check = possibilities(field,player,0,0,1,0,2,0); //vertical 7 4 1
status_check = possibilities(field,player,0,1,1,1,2,1); //vertical 8 5 2
status_check = possibilities(field,player,0,2,1,2,2,2); //vertical 9 6 3
status_check = possibilities(field,player,0,0,0,1,0,2); //horizontal 7 8 9
status_check = possibilities(field,player,1,0,1,1,1,2); //horizontal 4 5 5
status_check = possibilities(field,player,2,0,2,1,2,2); //horizontal 1 2 3
if (status_check == true)
{
status = "Player " + string(1, player) + " won!!";
field[0][0] ='w';
}
return status;
}
int possibilities(char field[3][3],char player,int a,int aa,int b,int bb,int c,int cc)
{
int status;
if ((field[a][aa]==player)&&(field[b][bb]==player)&&(field[c][cc]==player))
{
status = true;
}
return status;
}