Issues with Replacement in Arrays

Hey everyone, I'm fairly new to C++. I'm finishing up my first semester of my first class. Right now, I"m trying to write a program that lets two people play Tic Tac Toe. I'm having some issues actually getting the numbers within the array that makes up the board to be replaced by the 'x'. Essentially I want to replace that element in the array that the player chooses with the 'x' or 'o' so that when I run the display function again, that number is replaced by the respective icon. For whatever reason though, this isn't working. Any tips?

PS This program is a current work in progress, which is why some functions are commented out and variables aren't being used.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<cmath>

using std::cout;
using std::cin;
using std::endl;

const int row = 3;
const int col = 3;
const int numplayers = 2;

main(){
	void displayboard ( char[][col] );
	void makemove ( char[][col], int );
	bool checkwinner ( char [][col] );
	char ContAns;
	do{

		char board[row][col] = {{'1','2','3'},
					{'4','5','6'},
					{'7','8','9'}};
		bool winner;

		cout << "Welcome to Tic Tac Toe! I hope you and a friend are ready to play!" << endl;
		cout << "Player 1 will be 'x' and Player 2 will be 'o'!" << endl;
		int currentplayer = 1;

		do{
			displayboard( board );

			cout << "It's Player " << currentplayer << "'s turn!" << endl;

			makemove( board, currentplayer );

			displayboard(board);

			//winner=checkwinner(board);
		}while(winner == false);
	
		
		cout << "Continue? (y/n)" << endl;
		cin >> ContAns;
	}while( ContAns != 'n' );
}

void displayboard (char board[][col] ){
	for ( int r=0; r < row; r++ ){
		for ( int c=0; c < col; c++ ){
			cout << '|';
			cout << board[r][c];
			cout << '|'; 
		}
		cout << "\n---------\n";
	}
}

void makemove ( char board[][col], int cp ){
	int move;
	cout << "Player " << cp << ", what move would you like to make? (Input a space number to place your icon) " << endl;
	cin >> move;
	while ( move != board[row][col] ){
		cout << "That's not on the board or that spot is taken! Enter a move that's free on the board!" << endl;
		cin >> move;
	}
	for ( int r=0; r < row; r++ ){
		for ( int c=0; c < col; c++ ){
			if ( board[r][c] == move  && cp == 1 ){
				board[row][col] = 'x';
				break;
			}
			else if ( move == board[r][c] && cp == 2 ){
				board[r][c] = 'o';
				break;
			}
		}
	}
}				
			
//bool checkwinner ( char board[][col] ){
//	if (board[row][col] == {'x','x','x'}
}				
If you still looking for an answer so the first thing is that your move type is int, but your board is char type, so when you do move != board[r][c] you are checking if ASCII value on the board is equal to the number you entered which is integer, and you will always get false, change your move type to char.
Second, change this board[row][col] = 'x'; to board[r][c] = 'x';
Last edited on
Topic archived. No new replies allowed.