I want to draw a ASCII-art Christmas tree in C++ using functions with parameters.
This is what I have got so far, which works for the most part however when drawing the actual body of the tree (leaves) it seems to repeat it multiple times.
This effect seems to increase as the height of the tree does as well. So for example if a height of 4 is entered for the tree then the body will be drawn 2 times. if the height is 5, then it is drawn 3 times. 6 is 4 times and so on.
Any help?
#include <iostream>
using namespace std;
const char BLANK = ' ';
const char LEAF = '#';
const char WOOD = '|';
void drawAXmasTree();
void drawFoliage(int);
void drawTrunk(int);
void getValidHeight(int&);
void drawALineOfFoliage(int, int);
int main()
{
cout << "Due on 11 December 2018 \n\n";
drawAXmasTree();
}
void drawAXmasTree()
{
int treeHeight = 0;
getValidHeight(treeHeight); //read in a valid value for the tree height
drawFoliage(treeHeight); //draw tree foliage
drawTrunk(treeHeight); //draw tree trunk
}
void drawFoliage(int trHgt) //draw the foliage
{
int branchLine = 1;
int treeHeight = trHgt;
while (branchLine <= (trHgt - 2))
{
drawALineOfFoliage(treeHeight, branchLine);
branchLine += 1;
}
}
void drawTrunk(int trHgt) //draw the trunk
{
int trunkLine = 1;
int spaces;
while (trunkLine <= 2) // for each line in the truck
{
spaces = 1;
while (spaces <= (trHgt - 2)) //draw the spaces on the left
{
cout << BLANK;
spaces += 1;
}
cout << WOOD; //draw the truck
cout << endl; //go to next line
trunkLine += 1;
}
}
void getValidHeight(int& trHgt)
{
do
{
cout << "Enter the size of the tree(4 - 20): ";
cin >> trHgt;
if (trHgt < 4 || trHgt > 20)
{
cout << "ERROR: Invalid height! ";
}
}
while (trHgt < 4 || trHgt > 20);
}
void drawALineOfFoliage(int trHgt, int brLine)
{
int treeHeight = trHgt;
int branchLine = brLine;
int spaces = trHgt - 2;
for (int i = 0; i < (treeHeight - 2); i++) {
for (int j = spaces; j > 0; j--)
{
cout << BLANK;
}
for (int foliage = 0; foliage <= i * 2; foliage++)
{
cout << LEAF;
}
spaces--;
cout << endl;
}
}
The problem in your code is the following:
In the function drawFoliage you intended to loop and call drawALineOfFoliage for each line. But you actually draw the whole tree every time you call drawALineOfFoliage.
In order to fix you code, just replace drawFoliage for drawALineOfFoliage in the drawAXMasTree function, like this:
void drawAXmasTree()
{
int treeHeight = 0;
getValidHeight(treeHeight); //read in a valid value for the tree height
drawALineOfFoliage(treeHeight);
drawTrunk(treeHeight);
}
Notice you don't need the second argument in drawALineOfFoliage since you don't actually use it.
As for drawFoliage, you can just erase it.
Thanks for the help, I managed to resolve this because like people said it was drawing the whole tree rather than just one line. All I had to do was change the drawALineOfFoliage function to the following;
void drawALineOfFoliage(int trHgt, int brLine)
{
int treeHeight = trHgt;
int branchLine = brLine - 1;
int spaces = trHgt - 2;
for (int i = spaces; i > branchLine; i--)
{
cout << BLANK;
}
for (int foliage = 0; foliage <= branchLine * 2; foliage++)
{
cout << LEAF;
}
spaces--;
cout << endl;
}
Check this out:
#include <iostream>
using namespace std;
const char BLANK = ' ';
const char LEAF = '#';
const char WOOD = '|';
void drawAXmasTree();
void drawFoliage(int);
void drawTrunk(int);
void getValidHeight(int&);
void drawALineOfFoliage(int, int);
int main()
{
cout << "Due on 11 December 2018 \n\n";
drawAXmasTree();
system("pause");
}
void drawAXmasTree()
{
int treeHeight = 0;
getValidHeight(treeHeight);
drawFoliage(treeHeight);
drawTrunk(treeHeight);
}
void drawFoliage(int trHgt)
{
int branchLine = 1;
int treeHeight = trHgt;
while (branchLine <= (trHgt - 2))
{
drawALineOfFoliage(treeHeight, branchLine);
branchLine += 1;
}
}
void drawTrunk(int trHgt)
{
int trunkLine = 1;
int spaces;
for (trunkLine; trunkLine <= 2; trunkLine++)
{
for (spaces = 1; spaces <= (trHgt - 1); spaces++)
{
cout << BLANK;
}
cout << WOOD;
cout << "\n";
}
}
void getValidHeight(int& trHgt)
{
do
{
cout << "Enter the size of the tree(4 - 20): ";
cin >> trHgt;
if (trHgt < 4 || trHgt > 20)
{
cout << "ERROR: Invalid height! ";
}
} while (trHgt < 4 || trHgt > 20);
}
void drawALineOfFoliage(int trHgt, int brLine)
{
int treeHeight = trHgt;
int branchLine = brLine;
int spaces(0);
int tree(0);
for (spaces; spaces < (trHgt - branchLine); spaces++)
{
cout << BLANK;
}
for (tree; tree < (branchLine * 2 - 1); tree++)
{
cout << LEAF;
}
cout << endl;
}
I'm new to C++ and I'm trying to figure out why I get two "*" symbols in my game board when I'm moving around. The game is supposed to be about avoiding the troll (#). But I am getting duplicate # and * symbols, and I can't figure out why. It seems that the problem is either in one of the for loops or in the posX or posY variables, which I found out by commenting out segments of the code:
#include <iostream>
#include <string>
using namespace std;
void ClearScreen()
{
cout << string(100, '\n');
}
main()
{
int size_arrx = 10;
int size_arry = 20;
int posX = 0;
int posY = 0;
int trollX = size_arrx - 1;
int trollY = size_arry - 1;
char a[size_arry][size_arrx];
bool Alive = true;
char player = '*';
char troll = '#';
while (Alive == true) {
ClearScreen();
for (int i = 0; i<size_arrx; i++)
{
for (int j = 0; j<size_arry; j++)
{
a[i][j] = 'x';
}
}
for (int i = 0; i<size_arrx; i++)
{
for (int j = 0; j<size_arry; j++)
{
a[posX][posY] = player;
a[trollX][trollY] = troll;
cout << a[i][j];
if (posX< 0) {
a[posX = 0][posY] = player;
cout << a[i][j];
}
else if (posY< 0) {
a[posX][posY = 0] = player;
cout << a[i][j];
}
else if (posY > size_arry - 1) {
a[posX][posY = size_arry - 1] = player;
cout << a[i][j];
}
else if (posX > size_arrx - 1) {
a[posX = size_arrx - 1][posY] = player;
cout << a[i][j];
}
}
cout << endl;
}
char dir;
cin >> dir;
if (dir == 'w') {
trollX++;
posX--;
}
if (dir == 's') {
trollX--;
posX++;
}
if (dir == 'd') {
trollY--;
posY++;
}
if (dir == 'a') {
trollY++;
posY--;
}
}
if ((trollX == posX) && (trollY == posY)) {
Alive == false;
}
}
The result looks like this. I only want one *. The * can move perfectly fine, but a duplicate * follows the original * but 11 X's away.
xxxxxxxxxx*xxxxxxxxx <---- This is a duplicate *
*xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx#
xxxxxxxxx#xxxxxxxxxx <---- This is a duplicate #
Thanks in advance if you can help me
for (int i=0;i<size_arrx;i++){
for (int j=0;j<size_arry;j++){
a[i][j]='x';
}
}
a[posX][posY]=player;
a[trollX][trollY]=troll;
for (int i=0;i<size_arrx;i++){
for (int j=0;j<size_arry;j++){
cout << a[i][j];
Using this code gave the same error. I'm interpreting this as a[i][j]='x' populates all positions of a[][] with X's. a[posX][posY]=player; overwrites the position of the player with an * (could be x 2 y 5 for example) and then the board gets printed by cout << a[i][j];. I don't understand how a duplicate symbol gets thrown in there.
Let's simplify your program.
Initialize the board outside of the while loop.
There should be no reason to keep initializing it:
for (unsigned int row = 0; row < size_arry; ++row)
{
std::fill(&a[row][0], &a[row][size_arrx], 'x'); // Fill a row.
}
Printing the board should be simple:
for (unsigned int row = 0; row < size_arry; ++row)
{
for (unsigned int column = 0; column < size_arrx; ++column)
{
cout << a[row][column];
}
cout << '\n';
}
Now the character logic.
Every character has a position, row and column, of where it is. To ease restoration, every character should have a previous position also.
struct Position
{
unsigned int row;
unsigned int column;
};
Sorry about that code, the fingers and keyboard are not cooperating.
To move a character to a valid new position, you have to restore the previous position:
unsigned int player_now_x;
unsigned int player_now_y;
unsigned int player_prev_x;
unsigned int player_prev_y;
//...
a[player_prev_y][player_prev_x] = 'x';
a[player_now_y][player_now_y] = player;
For processing single letter commands, a switch statement may be more readable:
// Update previous position.
player_prev_x = player_now_x;
player_prev_y = player_now_y;
switch (dir)
{
case 'd':
if (player_now_y < size_arry)
{
++player_now_y;
}
break;
case 's':
if (player_now_x < size_arrx)
{
++player_now_x;
}
break;
// ...
}
Simplifications.
You can print the board with one cout if you add an extra column. The ending column of each row (except the last) will have a line ending character, '\n'. The last column of the last row will have a termination character, '\0'.
struct Board
{
void set_player(const Position& pos, char player_token)
{
a[pos.x][pos.y] = player_token;
}
void move_player(const Position& new_position,
const Position& previous_position,
char player_token)
{
set_player(previous_position, 'x');
set_player(new_position, player_token);
}
void print()
{
std::cout << &a[0][0] << "\n";
}
Board()
{
for (unsigned int y = 0; y < size_arry; ++y)
{
std::fill(&a[y][0], &a[y][size_arrx], 'x');
a[y][size_arrx - 1] = '\n';
}
a[size_arry - 1][size_arrx - 1] = '\0';
}
};
//...
Board b;
Position player_now;
Position player_prev;
const char player_token = '*';
//...
switch (dir)
{
case 'd':
if (player_now.y < size_arry)
{
++player_now.y;
}
//...
}
b.move_player(player_now, player_previous, player_token);
Sorry again, for the above code fragment, it's the fingers typing out what they want.
Can someone please help me. I am struggling to find in my code why the last value in column B always gets incremented by one. I have written some code since its an assignment due today. I also cant figure out why the last value in column B is not equal to 196 because in the reset function it sets all the values in the array to 196 . Any suggestion would be appreciated. Thank you in advance
#include <iostream> //includes cin cout
#include <iomanip>
using namespace std; //setting up the environment
const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats);
bool isFull(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
bool isEmpty(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
int main() { //main starts
int maxSeats;
int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
int SeatCountNumber = 0;
bool anyFreeSeats;
reset(seats);
anyFreeSeats = true;
SeatCountNumber = 0;
while (anyFreeSeats) {
printAllSeats(seats);
askForUsersSeat(seats, SeatCountNumber, anyFreeSeats);
}
system("pause");
return 0;
} //main ends
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]) {
cout << endl;
cout << setw(10) << " - = Available R = Reserved\n\n";
for (int i = 0; i <= NUMBER_OF_ROWS; i++) {
cout << setw(15) << i << " ";
for (int j = 0; j < NUMBER_OF_COLUMNS; j++) {
if (i == 0) {
cout << " " << static_cast<char>(j + 65) << " ";
} else {
cout << " " << static_cast<char>(seats[i][j]) << " ";
}
}
cout << endl;
}
cout << endl;
}
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]) {
//set all values in array to 196
for (int i = 0; i <= NUMBER_OF_ROWS; i++) {
for (int j = 0; j <= NUMBER_OF_COLUMNS; j++) {
seats[i][j] = 196;
}
}
}
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats) {
int seatChoiceNumber;
char seatChoiceLetter;
int letter;
int maxSeats = NUMBER_OF_ROWS * NUMBER_OF_COLUMNS;
cout << "Seat (Row, Column): ";
cin >> seatChoiceNumber >> seatChoiceLetter;
letter = static_cast<int>(toupper(seatChoiceLetter));
if (((letter >= 65) && (letter < (65 + NUMBER_OF_COLUMNS))) && ((seatChoiceNumber > 0) && (seatChoiceNumber <= NUMBER_OF_ROWS))) {
if (seats[(seatChoiceNumber)][(letter - 65)] == 82) {
} else {
seats[(seatChoiceNumber)][(letter - 65)] = 82;
SeatCountNumber++; //this changes last value in column B for some reason
if (SeatCountNumber < maxSeats) {
anyFreeSeats = true;
}
else if (SeatCountNumber > maxSeats) {
printAllSeats(seats);
anyFreeSeats = false;
}
}
} else {
}
}
I kind of cleaned up the code a bit. It seems you found your answer in the comments, so I just did some indentation. Try and eliminate whitespaces in your code (mind you, the one I am putting here is not perfect either, but you get the point). Clean and easy to read code doesn't only make it better for you, but as you get higher up in the industry and other people begin reading and working on your code, having clean and easy to read code really helps :)
#include <iostream> //includes cin cout
#include <iomanip>
using namespace std; //setting up the environment
const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats);
bool isFull(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
bool isEmpty(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
int main()
{
int maxSeats;
int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
int SeatCountNumber = 0;
bool anyFreeSeats;
reset(seats);
anyFreeSeats = true;
SeatCountNumber = 0;
while (anyFreeSeats)
{
printAllSeats(seats);
askForUsersSeat(seats, SeatCountNumber, anyFreeSeats);
}
system("pause");
return 0;
} //main ends
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS])
{
cout << endl;
cout << setw(10) << " - = Available R = Reserved\n\n";
for (int i = 0; i <= NUMBER_OF_ROWS; i++)
{
cout << setw(15) << i << " ";
for (int j = 0; j < NUMBER_OF_COLUMNS; j++)
{
if (i == 0)
{
cout << " " << static_cast<char>(j + 65) << " ";
}
else
{
cout << " " << static_cast<char>(seats[i][j]) << " ";
}
}
cout << endl;
}
cout << endl;
}
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS])
{
//set all values in array to 196
for (int i = 0; i <= NUMBER_OF_ROWS; i++)
{
for (int j = 0; j <= NUMBER_OF_COLUMNS; j++)
{
seats[i][j] = 196;
}
}
}
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats)
{
int seatChoiceNumber;
char seatChoiceLetter;
int letter;
int maxSeats = NUMBER_OF_ROWS * NUMBER_OF_COLUMNS;
cout << "Seat (Row, Column): ";
cin >> seatChoiceNumber >> seatChoiceLetter;
letter = static_cast<int>(toupper(seatChoiceLetter));
if (((letter >= 65) && (letter < (65 + NUMBER_OF_COLUMNS))) && ((seatChoiceNumber > 0) && (seatChoiceNumber <= NUMBER_OF_ROWS)))
{
if (seats[(seatChoiceNumber)][(letter - 65)] == 82)
{
}
else
{
seats[(seatChoiceNumber)][(letter - 65)] = 82;
SeatCountNumber++; //this changes last value in column B for some reason
if (SeatCountNumber < maxSeats)
{
anyFreeSeats = true;
}
else if (SeatCountNumber > maxSeats)
{
printAllSeats(seats);
anyFreeSeats = false;
}
}
}
else {
}
}
Note: Some more whitespaces could even come out but I generally like to have spaces after certain statements (personal preference).
I have this program that graphs simple parametric equations on a board of a defined length and width by me. It compiles fine but prints multiple instances of the function in different positions of the graph. If someone could please help me figure out why I am getting this output, I would greatly appreciate it. I included comments throughout the code to help understand what is going on.
I do not have enough reputation to post a picture of the output but if you compile and execute it you will see what I am talking about.
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <time.h>
#include <cmath>
using namespace std;
#define N 25
#define M 60
/*
This function prints the board each time it is called and places an *
in the place corresponding to the value of the function.
*/
void print_board(char p[M][N]) {
int i, j;
for (i=0; i<=N; i++) {
for (j=0; j<=M; j++)
if (i==0) cout << '=';
else if (j==0) cout << '|';
else if (i==N) cout << '=';
else if (j==M) cout << '|';
else if (p[i][j]== '*') cout << '*';
else cout << ' ';
cout << endl;
}
}
/*
These functions accepts an integer for time and computes a value for x and y
for the parametirc equations given and returns each.
*/
int fx(int t) {
int x = t;
return x;
}
int fy(int t) {
//int y = 5 * sin(0.2 * t) + 15;
int y = (pow(t,2)/60) - t + 25;
return y;
}
/*
This function copies the old board and comoputes what the new board is.
*/
void next_board(char p[M][N], int t) {
int i, j;
//copies the old board
int q[M][N];
for (i=0; i<=N; i++) {
for (j=0; j<=M; j++) {
q[i][j] = p[i][j];
}
}
//creates the new board
int x, y;
for (i=0; i<=N; i++) {
for (j=0; j<=M; j++) {
x = fx(t);
y = fy(t);
if (i == y && j == x) {
p[i][j] = '*'; //stores an * for the values of x and y
}
}
}
}
int main() {
char p[M][N];
print_board(p);
int t = 0;
while(t <= M) {
cout << string(80, '\n');
next_board(p , t);
print_board(p);
usleep(20000);
t++;
}
return 0;
}
Please help and thank you for all who try!
everywhere in your program where you have
char p[M][N]
change them to
char p[N][M]
and you should get the results that youd expect, your mixing axes in your program
heres the whole working code if youd like
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <time.h>
#include <cmath>
#include <string>
using namespace std;
#define N 25
#define M 60
/*
This function prints the board each time it is called and places an *
in the place corresponding to the value of the function.
*/
void print_board(char p[N][M]) {
int i, j;
for (i = 0; i <= N; i++) {
for (j = 0; j <= M; j++)
if (i == 0) cout << '=';
else if (j == 0) cout << '|';
else if (i == N) cout << '=';
else if (j == M) cout << '|';
else if (p[i][j] == '*') cout << '*';
else cout << ' ';
cout << endl;
}
}
/*
These functions accepts an integer for time and computes a value for x and y
for the parametirc equations given and returns each.
*/
int fx(int t) {
int x = t;
return x;
}
int fy(int t) {
//int y = 5 * sin(0.2 * t) + 15;
int y = (pow(t, 2) / 60) - t + 25;
return y;
}
/*
This function copies the old board and comoputes what the new board is.
*/
void next_board(char p[N][M], int t) {
int i, j;
//copies the old board
int q[M][N];
for (i = 0; i <= N; i++) {
for (j = 0; j <= M; j++) {
q[i][j] = p[i][j];
}
}
//creates the new board
int x, y;
for (i = 0; i <= N; i++) {
for (j = 0; j <= M; j++) {
x = fx(t);
y = fy(t);
if (i == y && j == x) {
p[i][j] = '*'; //stores an * for the values of x and y
}
}
}
}
int main() {
char p[N][M];
print_board(p);
int t = 0;
while (t <= M) {
cout << string(80, '\n');
next_board(p, t);
print_board(p);
usleep(20000);
t++;
}
return 0;
}
The problem is simple. I made a 3X3 tictactoe game with 3X3 arrays. But the problem is:
array[0][3] = array[1][0]
Which is strange because first of all, the array I made didn't have a fourth column. So array[0][3] doesn't even exist! And to make matters complicated, it takes the value of [1][0]
I'm having problems when I input co ordinates of my move as: 0 2
void displayBoard(int tictac[3][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout << tictac[i][j] << " ";
} cout << "\n" ;
} cout << "\n";
}
int Horizontal(int x, int y, int tictac[3][3])
{
if(tictac[x][y+1]==0)
{
tictac[x][y+1]=2;
return 1;
}
if(tictac[x][y-1]==0)
{
tictac[x][y-1]=2;
return 1;
}
if(tictac[x][y-2]==0)
{
tictac[x][y-2]=2;
return 1;
}
if(tictac[x][y+2]==0)
{
tictac[x][y+2]=2;
return 1;
}
return 0;
}
int Vertical(int x, int y, int tictac[3][3])
{
if(tictac[x+1][y]==0)
{
tictac[x+1][y]=2;
return 1;
}
if(tictac[x-1][y]==0)
{
tictac[x-1][y]=2;
return 1;
}
if(tictac[x-2][y]==0)
{
tictac[x-2][y]=2;
return 1;
}
if(tictac[x+2][y]==0)
{
tictac[x+2][y]=2;
return 1;
}
return 0;
}
void AI(int X,int Y,int tictac[3][3])
{
int done = 0;
cout << "\n-------------------------\nComputer plays: \n";
done = Horizontal(X,Y,tictac);
if(done == 0)
{
done = Vertical(X,Y,tictac);
}
}
int main()
{
int tictac[3][3] = {{0,0,0},{0,0,0}, {0,0,0} };
int X, Y;
for(int r=1; r<100; r++)
{
cout << "\n-------------------------\nPlayer play a move: \n";
cin >> X;
cin >> Y;
if(tictac[X][Y]==0)
{
tictac[X][Y] = 1;
displayBoard(tictac);
AI(X,Y,tictac);
displayBoard(tictac);
}
else
{
cout << "Space occupied. Try different cell." << endl;
}
}
}
You need to add bounds checking. For instance when the user inputs the move coordinates you need to ensure they are within the range of 0 to 2. The example below validates the input to ensure only numbers are entered, that both X and Y coordinates are entered on a single line and that the coordinates are within range. It uses std::stringstream to parse the coordinates instead of having to deal with checking and clearing the fail bits on std::cin
#include <string> // at top of your .cpp file
#include <sstream>
// in main()
// Get en entire input line so we can skip extra characters
// after the cell coordinates
string inputLine;
std::getline(cin, inputLine);
stringstream inputStream(inputLine);
if(!(inputStream >> X) || !(inputStream >> Y))
{
cout << "Please enter the cell coordinates in the form of # #" << endl;
continue;
}
bool invalidCoordinates = false;
if(X < 0 || X > 2)
{
cout << "invalid X location" << endl;
invalidCoordinates = true;
}
if(Y < 0 || Y > 2)
{
cout << "invalid Y location" << endl;
invalidCoordinates = true;
}
// check for invalid input
if(invalidCoordinates) continue;
You also need to do the same thing in your Vertical and Horizontal functions when checking if a valid move is possible. For instance if x is 2 and y is 2 the following lines from Vertical will access data outside the bounds of the array.
if(tictac[x+1][y]==0)
{
tictac[x+1][y]=2;
return 1;
}
This is because you are actually accessing the forth element with x+1. This element technically doesn't exist but with a multi-dimensional array you end up accessing tictac[0][y+1] instead.
You can get around the bounds checking in Vertical and Horizontal by adding some padding around the edges and fill them with a value that indicates they are unusable. In your case increase the size by 3 in each direction.
int tictac[9][9] = {
{3,3,3,3,3,3,3,3,3},
{3,3,3,3,3,3,3,3,3},
{3,3,3,3,3,3,3,3,3},
{3,3,3,0,0,0,3,3,3},
{3,3,3,0,0,0,3,3,3},
{3,3,3,0,0,0,3,3,3},
{3,3,3,3,3,3,3,3,3},
{3,3,3,3,3,3,3,3,3},
{3,3,3,3,3,3,3,3,3},
};
You will need to make adjustments to X and Y appropriately so they point to the correct location.
X += 3; // Adjust for padding
Y += 3; // Adjust for padding
if(tictac[X][Y]==0)
{
tictac[X][Y] = 1;
displayBoard(tictac);
AI(X,Y,tictac);
displayBoard(tictac);
}
You may need to make adjustments in other parts of your code but the above example should get your started.
There is also a problem in your displayBoard function. When it prints out the elements of the array i and j are reversed so the board appears rotated 90 degrees. Change the following line
cout << tictac[i][j] << " ";
to
cout << tictac[j][i] << " ";
Another problem is that you are using \n at the end of each line you output without using std::flush to ensure the line is sent to the console. You can either put << flush; after those lines or remove the \n and put << endl; at the end of the line.
cout << "\n-------------------------\nComputer plays: \n" << flush;
or
cout << "\n-------------------------\nComputer plays: " << endl;
The code below is a complete update of the original code included in your question. It incorporates the above suggestions and makes a couple of other changes. I've also added an endgame check to determine if there are any moves left.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
static const int BoardSize = 3;
static const int BoardPadding = BoardSize;
static const int ArraySize = BoardSize + (BoardPadding * 2);
void displayBoard(int tictac[ArraySize][ArraySize])
{
for(int y = 0; y < BoardSize; y++)
{
for(int x = 0; x < BoardSize; x++)
{
cout << tictac[BoardPadding + x][BoardPadding + y] << " ";
}
cout << endl ;
}
cout << endl;
}
int Horizontal(int x, int y, int tictac[ArraySize][ArraySize])
{
if(tictac[x][y+1]==0)
{
tictac[x][y+1]=2;
return 1;
}
if(tictac[x][y-1]==0)
{
tictac[x][y-1]=2;
return 1;
}
if(tictac[x][y-2]==0)
{
tictac[x][y-2]=2;
return 1;
}
if(tictac[x][y+2]==0)
{
tictac[x][y+2]=2;
return 1;
}
return 0;
}
int Vertical(int x, int y, int tictac[ArraySize][ArraySize])
{
if(tictac[x+1][y]==0)
{
tictac[x+1][y]=2;
return 1;
}
if(tictac[x-1][y]==0)
{
tictac[x-1][y]=2;
return 1;
}
if(tictac[x-2][y]==0)
{
tictac[x-2][y]=2;
return 1;
}
if(tictac[x+2][y]==0)
{
tictac[x+2][y]=2;
return 1;
}
return 0;
}
void AI(int X,int Y,int tictac[ArraySize][ArraySize])
{
int done = 0;
cout << "\n-------------------------\nComputer plays: " << endl;
done = Horizontal(X,Y,tictac);
if(done == 0)
{
done = Vertical(X,Y,tictac);
}
}
// Check if all moves have been made
bool isEndGame(int tictac[ArraySize][ArraySize])
{
int count = 0;
for(int y = 0; y < BoardSize; y++)
{
for(int x = 0; x < BoardSize; x++)
{
count += tictac[BoardPadding + x][BoardPadding + y] ? 1 : 0;
}
}
return count == (BoardSize * BoardSize);
}
int main()
{
int tictac[ArraySize][ArraySize] = {
{3,3,3,3,3,3,3,3,3},
{3,3,3,3,3,3,3,3,3},
{3,3,3,3,3,3,3,3,3},
{3,3,3,0,0,0,3,3,3},
{3,3,3,0,0,0,3,3,3},
{3,3,3,0,0,0,3,3,3},
{3,3,3,3,3,3,3,3,3},
{3,3,3,3,3,3,3,3,3},
{3,3,3,3,3,3,3,3,3},
};
int X, Y;
while(isEndGame(tictac) == false)
{
cout << "\n-------------------------\nPlayer play a move: " << flush;
// Get en entire input line so we can skip extra characters
// after the cell coordinates
string inputLine;
std::getline(cin, inputLine);
stringstream inputStream(inputLine);
if(!(inputStream >> X) || !(inputStream >> Y))
{
cout << "Please enter the cell coordinates in the form of # #" << endl;
continue;
}
bool invalidCoordinates = false;
if(X < 0 || X >= BoardSize)
{
cout << "invalid X location" << endl;
invalidCoordinates = true;
}
if(Y < 0 || Y >= BoardSize)
{
cout << "invalid Y location" << endl;
invalidCoordinates = true;
}
// check for invalid input
if(invalidCoordinates) continue;
// adjust the coordinates and do our thing
X += BoardPadding;
Y += BoardPadding;
if(tictac[X][Y]==0)
{
tictac[X][Y] = 1;
displayBoard(tictac);
AI(X,Y,tictac);
displayBoard(tictac);
}
else
{
cout << "Space occupied. Try different cell." << endl;
}
}
cout << "game finished...check for winner" << endl;
}
Note: It's a bad idea to use using namespace std;. It pulls everything from the std namespace into the current scope (in this case the global namespace) and can cause conflicts. It's best to use fully qualified names such as std::cout instead to avoid this.
In case of this array
int array[3][3];
the following statement is valid
array[0][3] == array[1][0]
because:
C/C++ does not perform any boundary checks.
3x3 array is stored as a 1D array. When you specify the 2D indices, the compiler transforms them to 1D index: [j][i] becomes [j * width + i].
Thus, array[0][3] points to 0 * 3 + 3 (third) cell in a memory, but array[1][0] points to 1 * 3 + 0 (also third!) cell of the memory, starting from the start of your 2D array.