如何在数组中找到特定值的频率?

时间:2012-12-22 00:13:25

标签: c++

我是C ++编程的新手,我正在阅读这本名为Alex Allain的跳入C ++的书,这本书中有一个井字游戏练习,我很难完成这个练习,所以我所做的就是提示用户输入X或O值,它存储在2d数组中,我想跟踪例如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;
            }
        }
    }
}

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

我的答案:

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

我认为您的问题在解决练习方面有些误导。我一开始以同样的方式想到了你的方式。问题在于模式不仅仅被3的频率检测到,因为它会允许错误的检查模式。使其可视化:

7 8 9
4 5 6
1 2 3

f.e。像1,2,5或9 6 5之类的3的频率不是正确答案,而是-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;
}