저는 C++ 프로그래밍의 초보자이고 Alex Allain의 C++로 점프하는이 책을 따르면서 책에 tic-tac-toe 연습 문제가 있습니다. 지금까지이 운동을 완료하는 데 어려움을 겪고 있습니다. 내가 한 일은 사용자에게 X 또는 O 값을 입력하도록 요청하는 것입니다.이 값은 2 차원 배열에 저장됩니다. 예를 들어 문자 X가 배열에 3 번 나타난 경우를 추적하려면 지금까지 카운터 ++를 사용하지만 한 번만 증가시킵니다 . 아래에 내가 지금까지 한 일의 원천이 있기 때문에 바라건대 내 질문이 훨씬 더 명확 해지고 내 코드가 구조 및 기능과 같은 모양으로되어있는 방식을 알게되었습니다.배열에서 특정 값의 빈도를 찾는 방법은 무엇입니까?
#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;
}
}
}
}
@ 로지 : C++은 서로 다른 C 및 C#과 다릅니다. 비슷한 이름은 혼란 스럽지만 한 프로그래머가 다른 사람들을 도울 수 없을만큼 뚜렷합니다. – RonaldBarzell
나는 그것을 알고 있으며 사과드립니다. – Rojee
중요한 문제는 범위 지정 및 기능 작동 방법을 모르는 것 같습니다. – OmnipotentEntity