Reputation:
edit: this is how my output when I input number
Please enter the move:
1
X--
---
---
Please enter the move:
2
-X-
---
---
Please enter the move:
3
--X
---
---
Please enter the move:
4
---
X--
---
Please enter the move:
The change is not saved.
I'm trying to change the array using the function that input by a user. It does get the input but it doesn't affect anything to my array outside of the function.
I've tried all different approaches
void (char *array[])
or void(char array[][3])
or void(char **array)
None of them worked.
#include <iostream>
#include <sstream>
#include <fstream>
#include <string.h>
#include <random>
using std::string;
using std::getline;
using namespace ::std;
const string winningCases[8] = {"123","456","789","147","258","369","159","357"};
void make_board(char grid[3][3]){
// some code which works
}
void print_board(char grid[3][3]){
// some code which works
}
void enter_move(char grid[][3]){
char humanMove;
int num_humanMove;
while(true){
cout << "Please enter the move: " << endl;
cin >> humanMove;
// find index for a grid
num_humanMove = static_cast<int>(humanMove) - 49;
int row = num_humanMove / 3;
int col = num_humanMove % 3;
// check right input
if(49 > static_cast<int>(humanMove) && static_cast<int>(humanMove) < 57){
cout << "Not valid input. " << endl;
}else if(grid[row][col] == 'X' || grid[row][col]== 'O'){
cout << "It's taken. " << endl;
}else{
grid[row][col] = 'X';
// print_board(*grid[3]);
break;
}
}
}
int find_grid_space(char move){
// some code which works
}
char continue_play(){
// some code which works
}
int main(int argc, char *argv[]){
char grid[3][3];
char play='y';
bool win=true;
while(play == 'y' || play == 'Y'){
while(win){
make_board(grid);
print_board(grid);
enter_move(grid);
win = !check_for_win(grid);
}
play = continue_play();
}
return 0;
}
So, the function void enter_move(char grid[][3])
should get the input from the user and change the grid. It changes the grid in the function but it won't do anything outside of the function.
Upvotes: 0
Views: 61
Reputation: 87959
The problem seems to be here
while(win){
make_board(grid);
print_board(grid);
enter_move(grid);
win = !check_for_win(grid);
}
Each time round the loop you call make_board
which I'm guessing resets the board each time.
What you should have is this
make_board(grid);
while(win){
print_board(grid);
enter_move(grid);
win = !check_for_win(grid);
}
so that you only set up the board once.
Upvotes: 2