|
|
Jestah
Maniac (V) Mad Scientist
From: Long Island, NY Insane since: Jun 2000
|
posted 01-23-2001 05:28
Sorry guys. I know this really doesn't belong here, but I need help fast. My sister's midterm in AP Computer Science was to create a tic tac toe game. She keeps getting script overflow errors but I can't seem to locate the error. The program uses apmatrix, you can download that at collegeboard.org if need be. Anyway its extremely important. Can some of you take a look at her code? Id be forever in your debt.
The code is:
- //Kristen Seibert
//Computer Science
//Tic Tac Toe
#include<iostream.h>
#include"apmatrix.h"
void game(apmatrix<char>board,int row, int column, int i, int j);
void print(apmatrix<char>game, int i, int j);
void winner(apmatrix<char>board);
int main()
{
//declares variales
int i, r, c, j;
//declares matrix board 3x3
apmatrix<char>gameboard(3,3,' - ');
//prints blank board
for(j=1;j=gameboard.numrows();j++)
{
for (i=0; i=gameboard.numcols(); i++)
{
cout<<gameboard[j][i];
}
cout<<endl;
}
cout<<endl;
//calls game function
game(gameboard, r, c, i, j);
return 0;
}
void game(apmatrix<char>board,int row, int column, int i, int j)
{ for(i =0; i< board.numrows() - board.numcols(); i++)
{
//Player 1
//player 1 selects row and column
cout<<"Player 1, please select a row."<<endl<<"(remember numbering system starts at 0)"<<endl;
cin>>row;
cout<<"please enter a column"<<endl;
cin>>column;
//checks that row is less than 3
while (row>2)
{
cout<<"please enter a number less than 3"<<endl;
cin>> row;
}
//checks that column is less than 3
while (column>2)
{
cout<<"please enter a number less than 3"<<endl;
cin>>column;
}
//places an X in selected row/column
board[row][column] = 'X';
//calls print function
print(board, i,j);
//Player 2
//player 2 selects row and column
cout<<"Player 2, please select a row."<<endl<<"(remember numbering system starts at 0)"<<endl;
cin>>row;
cout<<"please enter a column"<<endl;
cin>>column;
//checks that row is less than 3
while (row>2)
{
cout<<"please enter a number less than 3"<<endl;
cin>> row;
}
//checks that column is less than 3
while (column>2)
{
cout<<"please enter a number less than 3"<<endl;
cin>>column;
}
//places an O in selected row/column
board[row][column] = 'O';
//calls print function
print(board, i, j);
}
}
void print (apmatrix<char> game,int i, int j)
{
//prints board after each move
for(i=0;i<game.numrows();i++)
{
for (j=0; j<game.numcols(); j++)
{
cout<<game[i][j];
}
cout<<endl;
}
cout<<endl;
//calls function to determine winner
winner(game);
}
void winner(apmatrix<char> board)
{
int i, j=0;
for(i =0; i<board.numrows(); i++)
{
j=0;
if(board[i][j]==board[i][j++]==board[i][j+=2])
{
if (board[i][j] ='X')
{
cout<<"player 1 wins"<<endl;
}
else
{
cout<<"player 2 wins"<<endl;
}
}
j=0;
if(board[j][i]==board[j][i++]==board[j][i+=2])
{
if (board[i][j] ='X')
{
cout<<"player 1 wins"<<endl;
}
else
{
cout<<"player 2 wins"<<endl;
}
}
}
}
|
Jestah
Maniac (V) Mad Scientist
From: Long Island, NY Insane since: Jun 2000
|
posted 01-23-2001 05:29
Geez. Sorry for the brackets not lining up. I can't seem to find the problem. I appreciate any help. So does my sister. Thank you.
P.S.
It really isnt be asking for it. Its my sister.
|
taxon
Neurotic (0) Inmate Newly admitted
|
posted 01-23-2001 06:01
It's not really me with the answer.... it's my dead cousin.
|
Jestah
Maniac (V) Mad Scientist
From: Long Island, NY Insane since: Jun 2000
|
posted 01-23-2001 06:26
Funny. Could either you or your dead cousin help with this? Im looking at the code now. I still don't see why the script goes outta range. Shesh. I think I've been studying too hard for my own midterms.
|
silence
Maniac (V) Inmate
From: soon to be "the land down under" Insane since: Jan 2001
|
posted 01-23-2001 20:04
Okay, first of all, you're using templates. Templates have a LOT of overhead, but that's not the worst thing.
The worst thing is that compilers still don't handle templates very well. If at all possible, your sister should write the apmatrix class herself.
I'm still trying to get the apmatrix.h header file so I can look at that.
Maybe if you put a direct link to where it is on collegeboard.com because the site itself is pretty hard to search through.
It would also help to know what compiler she is using and what OS (unix or windows).
|
Bugimus
Maniac (V) Mad Scientist
From: New California Insane since: Mar 2000
|
posted 01-23-2001 21:29
Well, I just took a quick look and think I see a coding error in function main:
//prints blank board
for(j=1;j=gameboard.numrows();j++)
{
for (i=0; i=gameboard.numcols(); i++)
{
Shouldn't she be using the "==" conditional operator instead of the "=" assignment operator in these for loops?
//prints blank board
for(j=1;j==gameboard.numrows();j++)
{
for (i=0; i==gameboard.numcols(); i++)
{
|
Jestah
Maniac (V) Mad Scientist
From: Long Island, NY Insane since: Jun 2000
|
posted 01-23-2001 21:57
No Bugimus, I think shes right with the single =.
Silence, Im sorry but I don't have a direct link for it. The only thing I could suggest is searching collegeboard.org for the AP Computer Science subset. I havent taken the course in a while but I believe thats it.
|
silence
Maniac (V) Inmate
From: soon to be "the land down under" Insane since: Jan 2001
|
posted 01-23-2001 22:29
I think bug is on the right track Jestah.
Instead of the assignment operator (=) I think it should be the less than or equal to operator (<=)
Otherwise, the for statement doesn't have an end condition and you get the big, bad wolf of coding:
an endless loop.
It looks like it should be:
for (j = 1; j <= gameboard.numrows(); j++)
{
for (i = 0; i <= gameboard.numcols(); i++)
{
cout << gameboard[j][i];
}
}
Here's the URL for the needed classes etc. http://collegeboard.org/ap/computer-science/html/classes.html
For those of you who don't have the inclination to check the above link, the basic rundown is that apmatrix.h is the interface for a matrix class that uses another vector class, apvector in apvector.h
:edit: - heh, had to correct my code...happens to the best of us - :edit:
[This message has been edited by silence (edited 01-23-2001).]
|
Jestah
Maniac (V) Mad Scientist
From: Long Island, NY Insane since: Jun 2000
|
posted 01-24-2001 00:25
Wow I really feel stupid. Your both very right. Guess Im a little rusty with C++, havent taken the course in 2 years now. Anyway she fixed the code on her own as of this morning. She said she had to add a "-1" or something somewhere in the program. Thanks a lot though.
|
Bugimus
Maniac (V) Mad Scientist
From: New California Insane since: Mar 2000
|
posted 01-24-2001 00:37
I'm glad to hear she got it fixed
|
mbridge
Paranoid (IV) Mad Scientist
From: Insane since: Jun 2000
|
posted 01-24-2001 00:40
I don't mean any disrespect, but Jeez...that's computer science AP? The final at my school for computer science 1 is harder than that!
|
silence
Maniac (V) Inmate
From: soon to be "the land down under" Insane since: Jan 2001
|
posted 01-24-2001 00:42
I'm with bug. Congrats
|
Jestah
Maniac (V) Mad Scientist
From: Long Island, NY Insane since: Jun 2000
|
posted 01-24-2001 16:52
No offense mbridge but how much harder could it have been at your school? Remember its a statewide test and all schools are just following the standards.
|