🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.comp.lang.learn.c-c++
1 messages
1 total messages Started by andrewtitus Sun, 09 Sep 2001 19:38
problem with array
#76
Author: andrewtitus
Date: Sun, 09 Sep 2001 19:38
137 lines
5008 bytes
The program compiles and runs now. I still have a problem with my Starter
value being placed in board[2][3] though. I have stared at it and tried
different things and still nothing. My array board[][] is set to all 0's in
the constructor and my Starter variable is not even connected to array but
it still takes that value. Can someone point me in the right direction.

Thank You,
Andy

#include <iostream>


using namespace std;

// initalize 3x3 array board
TicTacToe::TicTacToe()
{
   for(int i = 1; i <= 3; i++)
     for(int j = 1; j <= 3; j++)
        board[i][j] = 0;
}
// end constructor array initalization

// deconstructor
TicTacToe::~TicTacToe() {}
// end deconstructor

//-------------------------------------------------------------------------
// display game and rule information
void TicTacToe::displayRules() {

    clrscr();
    cout << "*** WELCOME TO TIC-TAC-TOE ***" << endl;
    cout << "Player 1: for each square chosen a 1 will appear" << endl;
    cout << "Player 2: for each square chosen a 2 will appear" << endl;
    cout << "You will enter the square you want by:\n";
    cout << "1) choosing a square with a 0\n";
    cout << "2) by selecting row and column\n";
    cout << endl;

} //end displayRules()
//-------------------------------------------------------------------------
// find out which player will start first
void TicTacToe::whoStarts() {

    cout << "1) Player1" << endl;
    cout << "2) Player2" << endl;
    cout << "\nPlease enter who will start first: ";
    cin >> Starter;


} // end whoStarts()
//-------------------------------------------------------------------------
// play game function
void TicTacToe::playGame() {

     int i=0,j=0;

     // get row and column position of next move from user
     cout << "Where would you like to move first player "<< Starter <<": "
     << endl;
     cout << "ROW: ";
        cin  >> i;
     cout << "COLUMN: ";
        cin >> j;


     // test to see if space is empty
     while( board[i][j] != 0 )
      {

        cout << "Pick space with a 0 for move: " << endl;
        cout << "ROW: ";
        cin  >> i;
        cout << "COLUMN: ";
        cin >> j;
      }

     // set starter to either 1 or 2 to board[i][j]
     board[i][j] = Starter;

     // switch personUp to next player
     if( Starter == PLAYER1 )
         Starter = PLAYER2;
     else
         Starter = PLAYER1;


} // end playGame()



//-------------------------------------------------------------------------
// to be added later
// gameOver -- tell user' if there is a winner
/*void TicTacToe::gameOver() {

     gameStatus game;

} // end gameOver
*/
//------------------------------------------------------------------------


// displayBoard-- displays game board to user'
void TicTacToe::displayBoard() {

/*

  It seems to be messed up in here. For some reason it is takeing my
  value for starter and placing it in board[2][3].

*/
     clrscr();         // clears screen

     cout << "board[2][3]: " << board[2][3];     // this is a test!!!

     // reinitializes array board[][]
     for(int i = 1; i <= 3; i++)  {
        for(int j = 1; j <= 3; j++)
           if(board[i][j] != PLAYER1)
             if(board[i][j] != PLAYER2)
               board[i][j] = 0;
     } // end for loop

     // display board
     for(int i = 1; i <= 3; i++) {
       cout << endl;
         for(int j = 1; j <= 3; j++)
            cout << board[i][j] << " ";
     } // end for loop



     cout << endl << endl;

} // end displayBoard()
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads