tictactoe

This is a discussion on tictactoe within the C++ Programming forums, part of the General Programming Boards category; it says a referenced memory could not be read anyone help please #include <iostream.h> char gridchange(int fplayer, int gridnum); int ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    66

    tictactoe

    it says a referenced memory could not be read anyone help please

    #include <iostream.h>

    char gridchange(int fplayer, int gridnum);
    int GridP();
    int Winner();



    int gridnum;
    char grid[3][4];
    int winner;


    void main()
    {
    int fplayer;

    do
    {
    cout << "Who goes first 1 for X or 2 for O";
    cin >> fplayer;
    }
    while(fplayer != 1 && fplayer != 2);

    GridP();

    do
    {
    gridchange(fplayer, gridnum);
    }
    while(winner != 1 || winner != 2);

    }

    char gridchange(int fplayer, int gridnum)
    {
    grid[1][4] = '\n';
    grid[2][4] = '\n';
    grid[3][4] = '\n';

    int x, y;
    if(fplayer == 1)
    {

    if(gridnum == 1)
    {

    if(grid[1][1] == 'X' || grid[1][1] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[1][1] != 'X' || grid[1][1] != 'O')
    {
    grid[1][1] = 'X';
    }
    }

    else if(gridnum == 2)
    {

    if(grid[1][2] == 'X' || grid[1][2] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[1][2] != 'X' || grid[1][2] != 'O')
    {
    grid[1][2] = 'X';
    }

    }

    else if(gridnum == 3)
    {

    if(grid[1][3] == 'X' || grid[1][3] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[1][3] != 'X' || grid[1][3] != 'O')
    {
    grid[1][3] = 'X';
    }
    }

    else if(gridnum == 4 )
    {

    if(grid[2][1] == 'X' || grid[2][1] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[2][1] != 'X' || grid[2][1] != 'O')
    {
    grid[2][1] = 'X';
    }
    }

    else if(gridnum == 5 )
    {

    if(grid[2][2] == 'X' || grid[2][2] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[2][2] != 'X' || grid[2][2] != 'O')
    {
    grid[2][2] = 'X';
    }
    }

    else if(gridnum == 6 )
    {

    if(grid[2][3] == 'X' || grid[2][3] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[2][3] != 'X' || grid[2][3] != 'O')
    {
    grid[2][3] = 'X';
    }
    }

    else if(gridnum == 7 )
    {

    if(grid[3][1] == 'X' || grid[3][1] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[3][1] != 'X' || grid[3][1] != 'O')
    {
    grid[3][1] = 'X';
    }
    }

    else if(gridnum == 8 )
    {

    if(grid[3][2] == 'X' || grid[3][2] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[3][2] != 'X' || grid[3][2] != 'O')
    {
    grid[3][2] = 'X';
    }
    }

    else if(gridnum == 9 )
    {

    if(grid[3][3] == 'X' || grid[3][3] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[3][3] != 'X' || grid[3][3] != 'O')
    {
    grid[3][3] = 'X';
    }

    }

    }

    if(fplayer == 2)
    {

    if(gridnum == 1)
    {

    if(grid[1][1] == 'X' || grid[1][1] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[1][1] != 'X' || grid[1][1] != 'O')
    {
    grid[1][1] = 'O';
    }
    }

    else if(gridnum == 2)
    {

    if(grid[1][2] == 'X' || grid[1][2] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[1][2] != 'X' || grid[1][2] != 'O')
    {
    grid[1][2] = 'O';
    }

    }

    else if(gridnum == 3)
    {

    if(grid[1][3] == 'X' || grid[1][3] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[1][3] != 'X' || grid[1][3] != 'O')
    {
    grid[1][3] = 'O';
    }
    }

    else if(gridnum == 4 )
    {

    if(grid[2][1] == 'X' || grid[2][1] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[2][1] != 'X' || grid[2][1] != 'O')
    {
    grid[2][1] = 'O';
    }
    }

    else if(gridnum == 5 )
    {

    if(grid[2][2] == 'X' || grid[2][2] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[2][2] != 'X' || grid[2][2] != 'O')
    {
    grid[2][2] = 'O';
    }
    }

    else if(gridnum == 6 )
    {

    if(grid[2][3] == 'X' || grid[2][3] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[2][3] != 'X' || grid[2][3] != 'O')
    {
    grid[2][3] = 'O';
    }
    }

    else if(gridnum == 7 )
    {

    if(grid[3][1] == 'X' || grid[3][1] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[3][1] != 'X' || grid[3][1] != 'O')
    {
    grid[3][1] = 'O';
    }
    }

    else if(gridnum == 8 )
    {

    if(grid[3][2] == 'X' || grid[3][2] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[3][2] != 'X' || grid[3][2] != 'O')
    {
    grid[3][2] = 'O';
    }
    }

    else if(gridnum == 9 )
    {

    if(grid[3][3] == 'X' || grid[3][3] == 'O')
    {
    cout << "Space Filled Try Again";
    GridP();
    }

    else if(grid[3][3] != 'X' || grid[3][3] != 'O')
    {
    grid[3][3] = 'O';
    }

    }

    Winner();

    }

    for(x = 1; x < 5; x++)
    {
    for(y = 1; y < 5; y++)
    {
    cout << grid[x][y];
    }
    }
    return 0;
    }

    int GridP()
    {
    do
    {
    cout << " | | \n 1 | 2 | 3 \n___|___|___\n | | \n 4 | 5 | 6 \n___|___|___\n | | \n 7 | 8 | 9 \n | | \n";
    cout << "What grid number would you like to enter?";
    cin >> gridnum;
    }
    while(gridnum <= 1 && gridnum >= 9);
    return gridnum;
    }


    int Winner()
    {

    if((grid[1][3] == 'X' && grid[1][2] == 'X' && grid[1][1] == 'X') || (grid[2][1] == 'X' && grid[2][2] == 'X' && grid[2][3] == 'X') ||
    (grid[3][1] == 'X' && grid[3][2] == 'X' && grid[3][3] == 'X') || (grid[1][1] == 'X' && grid[2][1] == 'X' && grid[3][1] == 'X') || (grid[1][2] == 'X' && grid[2][2] == 'X' && grid[3][2] == 'X') || (grid[1][3] == 'X' && grid[2][2] == 'X' && grid[3][3] == 'X') || (grid[1][1] == 'X' && grid[2][1] == 'X' && grid[3][1] == 'X') || (grid[1][3] == 'X' && grid[2][1] == 'X' && grid[3][3] == 'X'))
    {
    int winner = 1;
    }
    else if((grid[1][3] == 'O' && grid[1][2] == 'O' && grid[1][1] == 'O') || (grid[2][1] == 'O' && grid[2][2] == 'O' && grid[2][3] == 'Y') || (grid[3][1] == 'O' &&
    grid[3][2] == 'O' && grid[3][3] == 'O') || (grid[1][1] == 'O' && grid[2][1] == 'O' && grid[3][1] == 'O') || (grid[1][2] == 'O' && grid[2][2] == 'O' && grid[3][2] == 'O') || (grid[1][3] == 'O' && grid[2][2] == 'O' && grid[3][3] == 'O') || (grid[1][1] == 'O' && grid[2][1] == 'O' && grid[3][1] == 'O') || (grid[1][3] == 'Y' && grid[2][1] == 'O' && grid[3][3] == 'O'))
    {
    winner = 2;
    }
    else
    {
    winner = 0;
    }
    return 0;
    }





    Thanks everybody

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    In your grid array:

    char grid[3][4];

    you are trying to access the elements 3 and 4 but they don't exist because the array starts at [0][0] and ends at [2][3].

  3. #3
    Unregistered
    Guest
    Often the compiler tells you (about) which line to look at so you don't have to scan the entire program. However, I suspect the error being reported might have to do with trying to reference memory outside of that which you have reserved. In particular look at this line:

    grid[3][4] = '\n';

    Given that grid is declared thusly:

    char grid[3][4];

    the first index should never be greater than 2 and the second index should never be greater than 3, yet you refer to index 3 and 4 respectively in the first statement. There are several other instances of this type of error in your code as well. Try to fix those and I think your problem will go away.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    66
    Thanks You guys rule don't know if it worked yet working on redoing all my code . That was a pretty stupid mistake but now that you tell me I understand and for some reason the compiler didn't give me any errors.

    Ryan

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    66
    Now I know why constants are great

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TicTacToe AI
    By Loic in forum C++ Programming
    Replies: 9
    Last Post: 05-28-2007, 01:37 PM
  2. C++ Tictactoe
    By vinter in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2007, 09:56 PM
  3. tictactoe symbol entering problem
    By ademkiv in forum C Programming
    Replies: 1
    Last Post: 03-21-2006, 10:53 AM
  4. Tictactoe problem
    By endo in forum Windows Programming
    Replies: 3
    Last Post: 04-10-2005, 09:36 AM
  5. TicTacToe OOP
    By NickESP in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2003, 10:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21