beginner tic tac toe game

This is my code, and the problem I am having well maybe what I need to add is instead of "Player 1" and "Player 2" I need to have people input their names and how many times they have won. I also need it to let it play any amount of time or exit when done laying and I need to display results of how many time they have won that looks like this
ex.
name | number of times won
John | 2
Paul | 3

below is my code,and thanks for any help! I am using Borland C++ compiler
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
#include <iostream.h>

void main()
{

int i = 0;
int player = 0; // Player number - 1 or 2
int go = 0; // Square selection
int row = 0; // Row index
int column = 0; // Column index for a square
int line = 0; // Row or column index in checking loop
int winner = 0; // The winning player
char board[3][3] = { // The board
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};

//  The game continues for up to 9 turns
// As long as there is no winner
for( i = 0; i<9 && winner==0; i++)
{
/* Display the board */
cout<< board[0][0]<<" | "<< board[0][1]<<" | "<<board[0][2]<<endl;
cout<<"---+---+---"<<endl;
cout<< board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<endl;
cout<<"---+---+---"<<endl;
cout<< board[2][0]<<" | " <<board[2][1]<<" | "<< board[2][2]<<endl;


player = i%2 + 1; /* Select player */

/* Get valid player square selection */
do
{
cout<<" Player " <<player <<" please enter the square "
<<"where you want to place your X Or O : ";
cin>>go;

row = --go/3; // Get row index of square
column = go%3; // Get column index of square
}while(go<0 || go>9 || board[row][column]>'9');

board[row][column] = (player == 1) ? 'X' : 'O'; // Insert player's letter

/* Check for a winning line - diagonals first */
if((board[0][0] == board[1][1] && board[0][0] == board[2][2]) ||
(board[0][2] == board[1][1] && board[0][2] == board[2][0]))
winner = player;
else
// Check rows and columns the winner
for(line = 0; line <= 2; line ++)
if((board[line][0] == board[line][1] && board[line][0] == board[line][2])||
(board[0][line] == board[1][line] && board[0][line] == board[2][line]))
winner = player;


}
// Game is over so display the final board

cout<< board[0][0]<<" | "<< board[0][1]<<" | "<<board[0][2]<<endl;
cout<<"---+---+---"<<endl;
cout<< board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<endl;
cout<<"---+---+---"<<endl;
cout<< board[2][0]<<" | " <<board[2][1]<<" | "<< board[2][2]<<endl;

// Display result message
if(winner == 0)
cout<<"It is a draw"<<endl;
else
cout<<"Congratulations, player "<<winner <<"YOU ARE THE WINNER!"<<endl;

}
I think it would simplify things if you created two string objects called player1 and player2, at the beginning of the game ask for user input of each of their names, and store it in player1 and player2.
That seems easier but the thing is how do I make them into strings? just use #include <string> and where exactly would i substitute it in my code instead of Player 1 and Player 2
You don't need to include <string> to use string, it's included in namespace std;

Create a 2 string array to hold the names.
Instead of displaying the variable [i]player[i], use [i]player[i] as an index to display the player's name.
You don't need to include <string> to use string, it's included in namespace std;

No, you do need to include <string> to use string. Yes it resides in the namespace std, but you can't just use everything inside std without using it.
Trisomy wrote:
You don't need to include <string> to use string, it's included in namespace std;

Incorrect. A lot of implementations have other headers include <string> for simplicity, but this is counter-standard.

EDIT: Ninja'd.

EDIT2: And now, some advice.

Is there any reason why you're using Borland? It's really old and it doesn't support the latest version/standard of C++ (C++11). More modern options for compilers would include Visual Studio's compiler or MinGW's compiler.

Also, although this doesn't fix your problem, you shouldn't be using void main(). Again, a lot of compilers support it, but that doesn't make it standard-compliant. Would you mind using int main() in the future?

You might want to put what you have so far into a do-while loop that checks if the players want to play again. Just a hint.

Finally, although I admit I only skimmed through the code, is the -- on line 40 deliberate?

Happy coding,
-Albatross
Last edited on
I'm using borland because that's the only compiler I have and my professor thinks it's the best one..
The thing is that I am a beginner and this is all so complicated! I just need my code to run and ask for players name, let the user keep playing or decide to leave, and have a chart of how many wins each player has won!
Topic archived. No new replies allowed.