Tic Tac Toe

I know practically everyone has had to create a tic tac toe project at some point. I've been searching online, and have found various examples of code that work great. However, I think my game is supposed to be even more simple than theirs. Yet, I am completely clueless in how to even begin the project.

I need to write a program that allows two players to play a game of tic tac toe (human vs. human - no computer). I need to use a 2D char array with 3 rows and 3 columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that:

(1) Displays the contents of the board array.
(2) Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and column number (user just puts in coordinates).
(3) Allows player 2 to select a location on the board for an O (Again, asking for coordinates).
(4) Determines whether a player has won, or a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should say so and end.

I need a function that displays the board, and a function that returns a bool, indicating whether a specific player has won. It should include a parameter that specifies which player we are evaluating ('X' or 'O'). It should return true if the player won, and false otherwise. (False does not necessarily mean the other player won, it just means the board isn't currently in a 'win' state for that player)

I have not really started on this yet. I am completely clueless in how to even begin. I don't understand how to display the board with a 2D array.

I have this code right now:
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
#include <iostream>
using namespace std;

const int SIZE = 3;

void displayBoard(char[][SIZE]);

int main()
{
	char board[SIZE][SIZE] = { { ' ', ' ', ' ' },
				   { ' ', ' ', ' ' },
				   { ' ', ' ', ' ' } };

	displayBoard(board);

	return 0;
}

void displayBoard(char board[][SIZE])
{
	cout << "     |     |     " << endl;
	cout << "  " << board[0][0] << "  |  " << board[0][1] << "  |  " << board[0][2] << endl;
	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;
	cout << "  " << board[1][0] << "  |  " << board[1][1] << "  |  " << board[1][2] << endl;
	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;
	cout << "  " << board[2][0] << "  |  " << board[2][1] << "  |  " << board[2][2] << endl;
	cout << "     |     |     " << endl;
}


I just got this from looking at different codes all day. I don't understand how the 2D array works in there. This is all of the code I have so far.

I also don't understand how/why each element should be initialized with an asterisk (mentioned in the instructions above). I've never had to do that before, so why should I have to do it now? I don't even know what that means.

I guess the logic of the entire game is confusing to me. I have not used a bool before, or created a game before.. I just feel completely lost.

If anyone could help me with any part of this, I would really appreciate it. I don't know what to do. I'm sorry for not having a lot of code to work with.
Hi there!
I'm really new to cplusplus.com but quite fine with c++.

I created a tic-tac-toe game myself a few months ago.
what you did with the array is fine but to maintain a little bit of order so you can understand the code I advise you to use a "for" loop.

You do'nt have to initialize every spot in the array- you can do:
char board[3][3]={0};
it will simply assign 0 to the entire array. (only works with 0.)

plus, you may want to move the printing code into a separate function... reply if you need anything else or... perhaps you did'nt understand a word of what I just said. Good luck!

example:

1
2
3
4
5
6
7
void print(char board[3][3])
{
     for(int i=0;i<3;++i,cout<<endl)
         for(int j=0;j<3;++j)
             cout<<board[i][j]<<' ';
     system("pause");
}
Here's a basic logic code to help you out:

1. while the board is not full:
2. -print board.
3. -ask for the player1's coordinates(were he wants to place his mark)
4. -place the mark
5. -get coordinates
6. -if the board is full or a player1 won or draw: break
7. -print board.
8. -ask for the player1's coordinates(were he wants to place his mark)
9. -get coordinates
10. -place the mark
(end loop)
11. if nobody won: print "draw"
12. else print "player *find who won* won!"
Topic archived. No new replies allowed.