1

I am working on a tic tac toe game and need to create a function which creates the board based on user input. Can be 3x3 or bigger I have this so far but when I run it prints memory location.

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

// Array which creates boeard based on user input
int *drawboard(int width, int height)
{
int* board_data = new int[width * height];
int** board = new int* [width];
for (int i = 0; i < width; ++i) 
{
    board[i] = board_data + height * i;
}
return board_data;
}






void main()
{

int width = 0;
int height = 0;


cout << " Welcome to Tic Tac Toe v1! " << endl;
cout << " Choose your board size, enter width: " << endl;
cin >> width;
cout << " Choose your height: " << endl;
cin >> height;
cout << drawboard << endl;
int *board = drawboard(width, height);
delete[] board;



system("pause");

}

Krowe3
  • 23
  • 6
  • Seems like it works as it should then. You're printing a pointer. – Borgleader Jul 26 '17 at 01:25
  • yes it works but when I input my width and height it prints Welcome to Tic Tac Toe v1! Choose your board size, enter width: 3 Choose your height: 3 00CB14A1 – Krowe3 Jul 26 '17 at 01:26
  • As I said, you're passing the function pointer of drawboard to the standard output. Not much sense in doing that but the output is what I would expect. Even if you passed the result of drawboard you'd get something similar. If you want to print the actual board in a nice 2d grid you have to write code for that. I'm not sure what your question is here as you havent actually asked one. – Borgleader Jul 26 '17 at 01:30

2 Answers2

0

I think this post will help you out a lot. It seems you're just trying to declare a dynamic 2D array of integers, then use the virtual 2D array as a tic tac toe board, right? Not quite sure what you mean by drawboard(), but here's a simple way to print a grid:

void printGrid(int y, int x) {
    for (int j = 0; j < y; j++) {
        for (int i = 0; i < x; i++) cout << " ---";
        cout << endl;
        for (int i = 0; i < x; i++) cout << "|   ";
        cout << "|" << endl;
    }
    for (int i = 0; i < x; i++) cout << " ---";
    cout << endl;
}
Chris
  • 40
  • 6
  • yes I'm stuck, I want to write a code which prints the array so if the user inputs a 3x3 array its prints out {"123456789"} in grid shape – Krowe3 Jul 27 '17 at 01:36
  • Did you recognize that I have a hyperlink where I said "this post"? I think you may have missed that – Chris Jul 28 '17 at 22:25
  • Thank you that helped a lot now all I got to do is create a function for player moves x and o – Krowe3 Jul 29 '17 at 00:52
  • How would I write a do loop to take player input and place an x or o in a space marked with ' - '. – Krowe3 Jul 29 '17 at 22:07
  • I think you will need a Player class to keep track of variables like who is X and who is O. The class can have a bool array based on the board's size, and then the player is asked which position to put their X or O which updates the bool array. Then you could make a member function part of the Player class like `printPlayerGrid()` which places their X and O on top of the recently printed grid by `printGrid()` This is a lot more complex than i thought. – Chris Jul 30 '17 at 05:40
0

This is my code when I run it my output is AAA BBB CCC if I input 3x3 board

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>

using namespace std;

// Array which creates boeard based on user input
char **createboard(int rows, int cols)
{
char** boardArray = new char*[rows];
for (int i = 0; i < rows; ++i) {
    boardArray[i] = new char[cols];
}

// Fill the array
for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < cols; ++j) {
        boardArray[i][j] = char(i + 65);
    }
}

// Output the array
for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < cols; ++j) {
        cout << boardArray[i][j];
    }
    cout << endl;
}

// Deallocate memory by deleting
for (int i = 0; i < rows; ++i) {
    delete[] boardArray[i];
}
delete[] boardArray;
return boardArray;
 }



 int main()
 {

int rows = 0;
int cols = 0;
char **boardArray = createboard(rows, cols);
cout << " Welcome to Tic Tac Toe v1! " << endl;
cout << " Choose your board width: " << endl;
cin >> rows;
cout << " Choose your board height " << endl;
cin >> cols;
cout << endl;
createboard(rows, cols);






system("pause");

}
Krowe3
  • 23
  • 6