CosmicJoghurt
New Member
Hey there.
I made a C++ Tic Tac Toe core/display. It's basically the function that prints the board, with a 3x3 array to store X's and O's. It's fully functional... for now.
Yes, i am aware that it isn't optimized, it's dirty, it's confusing... I'll work on cleanness later. But it's filled with comments for explanation purposes.
Anyways, I'd like some advice. Is there anything in particular that should be cleaned?
Beware, I've spent about half a dozen hours on this, it's messy. That's what you get for fixing a bugged display at 5:30 AM with the sun rising.
And yes, I'm aware everyone makes a tictactoe game. So quit it.
I made a C++ Tic Tac Toe core/display. It's basically the function that prints the board, with a 3x3 array to store X's and O's. It's fully functional... for now.
Code:
#include <iostream>
#include <string>
using namespace std;
/* Displaying the board works the following way:
There's a 3x3 array to hold the X/O's for the board.
First, the pBoard function prints the initial line of the tic-tac-toe board.
This initial line is printed every time a horizontal set of 3 X/O's is done printing.
Then, the actual X/O printing starts at the 1x1 spot of the board/array[0][0].
It prints each X/O's first line, in
the first set of 3 horizontal X/O's of the board. Then it does the same for each of the two remaining sets of 3 X/O's
in the board. When it's done printing any of the lines for each set of 3 X/O's,
it prints the final character of each line, a '|'. It's part of the outline of the board.
*/
/* Creating the strings for circles, crosses, empty rooms, the final board column and
the lines in between of each line of three X/O spots.*/
string bCircle1 = "|/ \\"; // For the printing of each X/O there are two lines:
string bCircle2 = "|\\ /";
string bCross1 = "| \\/ ";
string bCross2 = "| /\\ ";
string bEmpty = "| ";
string bColumn = "|"; // When the X/O are done printing we need the last column of the board to be printed;
string bLine = " ---- ---- ----"; //
int line = 0; // Current line (to use with 3x3 X/O array which stores data from [0][0])
int column = 0; // Current column (/*/)
int bLoop = 1; // Determines which one of the cross/circle *cout*'s is executed.
void pBoard(void){
int n1 = 1;
string spot[3][3];
spot[0][0] = "X"; // 3x3 array to store players' X/O's.
spot[1][0] = " "; // Example.
spot[2][0] = " ";
spot[0][1] = "O";
spot[1][1] = " ";
spot[2][1] = " ";
spot[0][2] = "X";
spot[1][2] = " ";
spot[2][2] = " ";
/* Function to be called for showing the current board */
for (int c = 0; c<3; c++) // One iteration of the *for* loop for each line of X/O's.
{
if (n1 == 1){ // Only displays bLine once, since it displays at the end of each X/O line.
cout << bLine << endl;// Line in between of each set of X/O's and at the beginning of each board.
}
for (int b = 0; b<2; b++){ // Each X/O has two lines for /'s and \'s.
for (int a = 0; a<3; a++) { // One iteration of the *for* loop for each X/O.
if (spot[line][column] == "X") { // Checks if current spot has an X/O/nothing on it.
if (bLoop == 1){
cout << bCross1; // First line for the X.
}
else if (bLoop == 2){
cout << bCross2; // Second line for the X.
}
}
if (spot[line][column] == "O") { // Same here.
if (bLoop == 1){
cout << bCircle1;
}
else if (bLoop == 2){
cout << bCircle2;
}
}
if (spot[line][column] == " ") { // Same.
if (bLoop == 1){
cout << bEmpty;
}
else if (bLoop == 2){
cout << bEmpty;
}
}
column++; // When it's done with printing an X/O/nothing - line, increase the column variable;
}
cout << bColumn << endl; // Print the last column of the board.
column = 0; // Reset the column to 0 (remember, the 3x3 array stores from [0][0], not [1][1];
bLoop++; // Increase bLoop when the first line of the first set of X/O's is done printing, so it prints the second part of each X/O.
if (b == 1) { // If the twice-looping loop is finishing the second and last iteration, reset bLoop so the next set of X/O's is well printed.
bLoop = 1;
}
}
cout << bLine << endl; // Line in between of each of the 3 sets of X/O's.
line++; // Move on, print the second set of X/O's.
n1++; // Makes sure the initial bLine is never printed again.
}
}
int main (int argc, char * const argv[]) {
pBoard();
return 0;
}
Yes, i am aware that it isn't optimized, it's dirty, it's confusing... I'll work on cleanness later. But it's filled with comments for explanation purposes.
Anyways, I'd like some advice. Is there anything in particular that should be cleaned?
Beware, I've spent about half a dozen hours on this, it's messy. That's what you get for fixing a bugged display at 5:30 AM with the sun rising.
And yes, I'm aware everyone makes a tictactoe game. So quit it.