0

我是 C++ 编程的新手,我正在关注 Alex Alllain 的这本书,名为 jumping into C++,书中有一个井字游戏练习,我很难完成那个练习,到目前为止我'已经完成的是提示用户输入 X 或 O 值,它们存储在二维数组中,我想跟踪例如 char X 是否在数组中出现 3 次,到目前为止我使用 counter++ 但它只增加一次。下面是我到目前为止所做的事情的来源,希望它能让我的问题更加清晰,并让我知道我在代码的外观方面是如何做的,比如它的结构和功能:

#include "stdafx.h"
#include "iostream"
#include "string"

using namespace std;

void display_array(char array[][3]);
void check_input(char array[][3], int input);
void check_winner(char array[][3]);
int check_x(char array[][3]);
int check_o(char array[][3]);

int _tmain(int argc, _TCHAR* argv[])
{
    char array[3][3];
    int counter = 0;

    for(int row = 0; row < 3; row++){

        cout << "\n";

        for(int col = 0; col < 3; col++){
            counter++;
            array[row][col] = counter;
        }
    }

    display_array(array);

    system("PAUSE");
    return 0;
}

void display_array(char array[][3]){

    int position_input;
    string symbol_input;

    do{

    for(int i=0; i < 3; i++){

        for(int j=0; j < 3; j++){
            cout << " [ ";
            cout << array[i][j];
            cout << " ] ";
        }
        cout << "\n";
    }

    cout << "Position: ";
    cin >> position_input;

    check_input(array, position_input);


    }while(position_input != 0);
} 

void check_input(char array[][3], int input)
{
    char user_input = input;

    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            if(user_input == array[i][j])
            {
                cout << "array[" << i << "][" << j << "] replace with: ";
                cin >> array[i][j];

            }   

        }
    }
    check_winner(array);
}
void check_winner(char array[][3]){
    cout << check_x(array);

    if(check_x(array) == 3){
        cout << check_x(array);
    }

    else if(check_o(array) == true){
        cout << "o";
    }

}
int check_x(char array[][3]){
    int counter, x[3];
    counter = 0;

    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            if(array[i][j] == array[i][j]){
                counter++;
            }
            x[i] = counter;
            return x[i];
        }
    }

}
int  check_o(char array[][3]){

    int counter;
    counter = 0;

    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            if(array[i][j] == 'o'){
                counter++;
                return counter;
            }else{
                return counter;
            }
        }
    }
}
4

2 回答 2

2

你有一堆小问题,但最直接的问题似乎是这种事情:

for(int i = 0; i < 3; i++){
    for(int j = 0; j < 3; j++){
        if(array[i][j] == 'o'){
            counter++;
            return counter;
        }else{
            return counter;
        }
    }
}

无论测试的元素是否为“o”,您都将在一个循环后返回。把它改成这样:

for(int i = 0; i < 3; i++){
    for(int j = 0; j < 3; j++){
        if(array[i][j] == 'o')
            counter++;
    }
}

return counter;
于 2012-12-22T00:28:21.217 回答
0

我的答案:

首先是问题的最后一部分:编码看起来不错,如果您对某些操作进行评论,即使程序非常引人注目(// /* */),我也会很高兴。

在我看来,你的问题在解决练习方面有点误导。我一开始也是这样想你的。问题是模式不仅仅被频率 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;
}
于 2019-03-30T18:21:07.313 回答