Sign in to follow this  

hello

This topic is 4391 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was writing output for a Tic Tac Toe board, and it compiles fine and everything, but it doesn't want to give the right output. It gives a bunch of jumbled letters rather than -, X, or O. Here's my code.
[source = 'C++']
#include <iostream>
using namespace std;
enum valid {OPEN, X, O}; //State of the cell
enum state {WIN, LOSS}; //State of the game i.e. Win or loss
struct cell { //Creates a cell, with a state of open, x or o
  valid cellv;
};
struct board { //creates a board, with an array of cells, a statistic of the board, saying win or loss, and a drawing function
  cell bcell[3][3];
  state bstate;
  void draw();
};
void board::draw() { //drawing of the board, accounting for taken spaces
  char cellstate[4][3];
  for(int y=0; y<3; y++) {
    for(int x=0; x<3; x++) {
      if(bcell[x][y].cellv == OPEN) {
        cellstate[x][y] = '-';
      }
      if(bcell[x][y].cellv == X) {
        cellstate[x][y] = 'X';
      }
      if(bcell[x][y].cellv == O) {
        cellstate[x][y] = 'O';
      }
    }
  }
  for(int i=0; i<3; i++) {
    cellstate[4][i] = '\0';
  }
  for(int y2=0; y2<3; y2++) {
    for(int x2=0; x2<3; x2++) {
      cout<<cellstate[x2][y2];
    }
  }
};
int main() {
  board MyBoard;
  MyBoard.draw();
  return 0;
}

Share this post


Link to post
Share on other sites
1° "Hello" is not a very descriptive (or attracting) thread title
2° What is the initial value of the elements of bcell and cellstate? Considering this, what is the value of the elements of cellstate right before you display it?

Share this post


Link to post
Share on other sites
You aren't setting your data to anything on initilization, so when it comes time to draw the board your actually spitting out what data that was in the memory before it was allocated to your bcell array. Try something like:

void board::clear() {
for(int y=0; y<3; y++)
for(int x=0; x<3; x++)
cellstate[x][y] = OPEN;
}

int main() {
board MyBoard;
MyBoard.clear();
MyBoard.draw();
return 0;
}

Share this post


Link to post
Share on other sites
ofcourse Tok means initializing bcell, (not cellstate).

in order to write more robust code you can add error checking
you may want to read about writing an assert macro (there is an article here in gamedev i think).

for example:

if(bcell[x][y].cellv == OPEN) {
cellstate[x][y] = '-';
}
if(bcell[x][y].cellv == X) {
cellstate[x][y] = 'X';
}
if(bcell[x][y].cellv == O) {
cellstate[x][y] = 'O';
}
else ErrorReport("bad cell ?!");




Share this post


Link to post
Share on other sites

This topic is 4391 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this