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.