Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-29-2007, 11:14 AM   #1 (permalink)
Black Magic
Registered User
 
Join Date: Mar 2005
Posts: 64
+0 Internets
C++ Project Crashing. Don't know why.

My intro to programming class was assigned to create our own game using C++. My group chose to do connect 4, everyone wrote a piece of code and then we compiled it all together. The problem is when i run the program, after the first move is made the program crashes. I have been looking at it for hours and don't know whats wrong, if anyone can spot the problem in the code i would appreciate it a bunch.


Edit: I guess the forum dosent like my "{}'s" they are all Mcfuzzled. Thx in advance for the help.
__________________
Image hosted by Photobucket.com

Last edited by Black Magic : 03-29-2007 at 11:19 AM.
Black Magic is offline   Reply With Quote
Old 03-29-2007, 11:17 AM   #2 (permalink)
Black Magic
Registered User
 
Join Date: Mar 2005
Posts: 64
+0 Internets
Here is my Code(using Dev-C++)

#include
#include
using namespace std;
int const rows=6;
int const cols=7;
int const connect=4;

void Who_Moves( char cur_val)
{

if (cur_val=='-')
cout << " "; // val is 0, so draw a space
else if (cur_val=='O')
cout << "O"; // val is 1, so draw a 'O'
else if (cur_val=='*')
cout << "*"; // val is 2, so draw a '*'
}

bool Is_Legal(const vector >Board, int cur_col)
{
if(Board[0][cur_col] != '-')
{
return false;
}
return true;
}

bool Tie_Check(const vector >& Board)
{
for(int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if(Board[i][j] == '-')
{
return false;
}
else
{
return true;
}
}
}
}

//function that checks to see if the game is over
bool End_Game_Check(const vector >Board, int row, int cur_col, char cur_move)
{
cout << "row " << row << " cur_col " << cur_col << " cur_move " << cur_move << endl;
//check for horizontal win in the current row (4 cases)
if(Board[row][0]== cur_move && Board [row][1]== cur_move && Board [row][2]== cur_move && Board[row][3]== cur_move)
{
return true;
}

if(Board[row][1]== cur_move && Board [row][2]== cur_move && Board [row][3]== cur_move && Board[row][4]== cur_move)
{
return true;
}

if(Board[row][2]== cur_move && Board [row][3]== cur_move && Board [row][4]== cur_move && Board[row][5]== cur_move)
{
return true;
}

if(Board[row][3]== cur_move && Board [row][4]== cur_move && Board [row][5]== cur_move && Board[row][6]== cur_move)
{
return true;
}

//check for vertical win in the current column (3 cases)
if(Board[5][cur_col] == cur_move && Board[4][cur_col] == cur_move && Board[3][cur_col] && Board[2][cur_col] == cur_move)
{
return true;
}

if(Board[4][cur_col] == cur_move && Board[3][cur_col] == cur_move && Board[2][cur_col] && Board[1][cur_col] == cur_move)
{
return true;
}

if(Board[3][cur_col] == cur_move && Board[2][cur_col] == cur_move && Board[1][cur_col] && Board[0][cur_col] == cur_move)
{
return true;
}

//check for diagonal win that contains the current position (6 cases)
if(Board[row][cur_col] == Board[row + 1][cur_col- 1] && Board[row + 1][cur_col - 1] == Board[row + 2][cur_col - 2] && Board[row + 2][cur_col - 2] == Board[row + 3][cur_col - 3])
{
return true;
}
if(Board[row][cur_col] == Board[row + 1][cur_col - 1] && Board[row + 1][cur_col - 1] == Board[row + 2][cur_col- 2] && Board[row + 2][cur_col- 2] == Board[row - 1][cur_col + 1])
{
return true;
}
if(Board[row][cur_col] == Board[row + 1][cur_col - 1] && Board[row + 1][cur_col - 1] == Board[row - 1][cur_col + 1] && Board[row - 1][cur_col + 1] == Board[row - 2][cur_col + 2])
{
return true;
}
if(Board[row][cur_col] == Board[row + 1][cur_col + 1] && Board[row][cur_col] == Board[row + 2][cur_col + 2] && Board[row][cur_col] == Board[row + 3][cur_col + 3])
{
return true;
}
if(Board[row][cur_col] == Board[row - 1][cur_col - 1] && Board[row][cur_col] == Board[row + 1][cur_col + 1] && Board[row][cur_col] == Board[row + 2][cur_col+ 2])
{
return true;
}

if(Board[row][cur_col] == Board[row - 2][cur_col - 2] && Board[row][cur_col] == Board[row - 1][cur_col - 1] && Board[row][cur_col] == Board[row + 1][cur_col + 1])
{
return true;
}
else
{
return false;
}
}



void Print_Board(const vector >&Board)
{
for (int i=0; i {
cout << endl;
for(int j=0; j {
cout << Board[i][j];
}
cout << endl;
}
}


int main()
{
int cur_row;
int cur_col;
vector >Board;
Board.resize(rows);
for (int i=0; i {
Board[i].resize(cols);
for (int j=0; j {
Board[i][j] = '-';
}
}

Print_Board(Board);

bool Game_Over=false;
char cur_move='O';//o goes first

while (!Game_Over)
{
cout<<"Player " << cur_move <<" please enter the column in which you would like your marker placed"< cin >> cur_col;
while(Is_Legal(Board,cur_col - 1)==false)
{
cout << "illegal column please try enter column again"< cin >> cur_col;
}
bool Place_Marker=false;
int i=5;

do
{
if (Board[i][cur_col-1]=='-')
{
Board[i][cur_col-1]=cur_move;
Place_Marker=true;
Print_Board(Board);
}
else
{
i--;
}
}while(Place_Marker==false && i>=0);
Game_Over=End_Game_Check(Board,i , cur_col-1, cur_move);
cout << "GOT HERE 1" << endl;
if(Game_Over == true)
{
cout << endl << cur_move << " has won!" << endl;
}
if(Game_Over == false)
{
bool Draw = Tie_Check(Board);
if(Draw == true)
{
cout << "There's a DRAW - GAME OVER";
Game_Over = true;
}
}

if(cur_move=='O')
{
cur_move='*';
}
else
{
cur_move='O';
}
}
system("pause");
return 0;
}
__________________
Image hosted by Photobucket.com
Black Magic is offline   Reply With Quote
Old 03-29-2007, 03:10 PM   #3 (permalink)
tikkus
Banned
 
Join Date: Nov 2003
Posts: 1,219
-3 Internets
Post again using (code) but replace the () with [].
tikkus is offline   Reply With Quote
Old 03-29-2007, 03:19 PM   #4 (permalink)
Shots
Registered User
 
Shots's Avatar
 
Join Date: Nov 2006
Posts: 340
+1 Internets
Did the board eat some of your code? Your 'for' loops in main and Print_Board are declared incorrectly (not closed).
Shots is offline   Reply With Quote
Old 03-29-2007, 04:47 PM   #5 (permalink)
Vorph
-666 Internets
 
Vorph's Avatar
 
Join Date: May 2002
Location: Hell
Posts: 4,985
Yea, the board eats < and everything on that line following it, because it thinks it's bad HTML. Use a [code] block like tikkus said, or &lt; instead.
__________________
  
Vorph is offline   Reply With Quote
Old 03-29-2007, 10:24 PM   #6 (permalink)
Black Magic
Registered User
 
Join Date: Mar 2005
Posts: 64
+0 Internets
Code:
#include #include using namespace std; int const rows=6; int const cols=7; int const connect=4; void Who_Moves( char cur_val) { if (cur_val=='-') cout << " "; // val is 0, so draw a space else if (cur_val=='O') cout << "O"; // val is 1, so draw a 'O' else if (cur_val=='*') cout << "*"; // val is 2, so draw a '*' } bool Is_Legal(const vector >Board, int cur_col) { if(Board[0][cur_col] != '-') { return false; } return true; } bool Tie_Check(const vector >& Board) { for(int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if(Board[i][j] == '-') { return false; } else { return true; } } } } //function that checks to see if the game is over bool End_Game_Check(const vector >Board, int row, int cur_col, char cur_move) { cout << "row " << row << " cur_col " << cur_col << " cur_move " << cur_move << endl; //check for horizontal win in the current row (4 cases) if(Board[row][0]== cur_move && Board [row][1]== cur_move && Board [row][2]== cur_move && Board[row][3]== cur_move) { return true; } if(Board[row][1]== cur_move && Board [row][2]== cur_move && Board [row][3]== cur_move && Board[row][4]== cur_move) { return true; } if(Board[row][2]== cur_move && Board [row][3]== cur_move && Board [row][4]== cur_move && Board[row][5]== cur_move) { return true; } if(Board[row][3]== cur_move && Board [row][4]== cur_move && Board [row][5]== cur_move && Board[row][6]== cur_move) { return true; } //check for vertical win in the current column (3 cases) if(Board[5][cur_col] == cur_move && Board[4][cur_col] == cur_move && Board[3][cur_col] && Board[2][cur_col] == cur_move) { return true; } if(Board[4][cur_col] == cur_move && Board[3][cur_col] == cur_move && Board[2][cur_col] && Board[1][cur_col] == cur_move) { return true; } if(Board[3][cur_col] == cur_move && Board[2][cur_col] == cur_move && Board[1][cur_col] && Board[0][cur_col] == cur_move) { return true; } //check for diagonal win that contains the current position (6 cases) if(Board[row][cur_col] == Board[row + 1][cur_col- 1] && Board[row + 1][cur_col - 1] == Board[row + 2][cur_col - 2] && Board[row + 2][cur_col - 2] == Board[row + 3][cur_col - 3]) { return true; } if(Board[row][cur_col] == Board[row + 1][cur_col - 1] && Board[row + 1][cur_col - 1] == Board[row + 2][cur_col- 2] && Board[row + 2][cur_col- 2] == Board[row - 1][cur_col + 1]) { return true; } if(Board[row][cur_col] == Board[row + 1][cur_col - 1] && Board[row + 1][cur_col - 1] == Board[row - 1][cur_col + 1] && Board[row - 1][cur_col + 1] == Board[row - 2][cur_col + 2]) { return true; } if(Board[row][cur_col] == Board[row + 1][cur_col + 1] && Board[row][cur_col] == Board[row + 2][cur_col + 2] && Board[row][cur_col] == Board[row + 3][cur_col + 3]) { return true; } if(Board[row][cur_col] == Board[row - 1][cur_col - 1] && Board[row][cur_col] == Board[row + 1][cur_col + 1] && Board[row][cur_col] == Board[row + 2][cur_col+ 2]) { return true; } if(Board[row][cur_col] == Board[row - 2][cur_col - 2] && Board[row][cur_col] == Board[row - 1][cur_col - 1] && Board[row][cur_col] == Board[row + 1][cur_col + 1]) { return true; } else { return false; } } void Print_Board(const vector >&Board) { for (int i=0; i >Board; Board.resize(rows); for (int i=0; i> cur_col; while(Is_Legal(Board,cur_col - 1)==false) { cout << "illegal column please try enter column again"<> cur_col; } bool Place_Marker=false; int i=5; do { if (Board[i][cur_col-1]=='-') { Board[i][cur_col-1]=cur_move; Place_Marker=true; Print_Board(Board); } else { i--; } }while(Place_Marker==false && i>=0); Game_Over=End_Game_Check(Board,i , cur_col-1, cur_move); cout << "GOT HERE 1" << endl; if(Game_Over == true) { cout << endl << cur_move << " has won!" << endl; } if(Game_Over == false) { bool Draw = Tie_Check(Board); if(Draw == true) { cout << "There's a DRAW - GAME OVER"; Game_Over = true; } } if(cur_move=='O') { cur_move='*'; } else { cur_move='O'; } } system("pause"); return 0; }
Test

Edit: There it goes.
__________________
Image hosted by Photobucket.com
Black Magic is offline   Reply With Quote
Old 03-29-2007, 10:31 PM   #7 (permalink)
Zuuljin
So there's this plane on a treadmill...
 
Zuuljin's Avatar
 
Join Date: Jan 2005
Location: Southern California
Posts: 2,863
+2 Internets
Send a message via AIM to Zuuljin
No debugger?
Zuuljin is offline   Reply With Quote
Old 03-29-2007, 10:42 PM   #8 (permalink)
Hachima
Registered User
 
Join Date: Oct 2004
Posts: 1,570
Still looks messed up. You can use pastebin - collaborative debugging tool to paste your code better.
Hachima is offline   Reply With Quote
Old 03-29-2007, 10:50 PM   #9 (permalink)
Zuuljin
So there's this plane on a treadmill...
 
Zuuljin's Avatar
 
Join Date: Jan 2005
Location: Southern California
Posts: 2,863
+2 Internets
Send a message via AIM to Zuuljin
Your missing the main method in your code snippet there.
Zuuljin is offline   Reply With Quote
Old 03-30-2007, 11:34 AM   #10 (permalink)
Vorph
-666 Internets
 
Vorph's Avatar
 
Join Date: May 2002
Location: Hell
Posts: 4,985
Here... formatting is still atrocious, but at least the code is all there.

If it compiles in VC++, I'll take a look. I second Zuuljin's question though--haven't they taught you about using a debugger yet?

Code:
#include <iostream> #include <vector> using namespace std; int const rows=6; int const cols=7; int const connect=4; void Who_Moves( char cur_val) { if (cur_val=='-') cout << " "; // val is 0, so draw a space else if (cur_val=='O') cout << "O"; // val is 1, so draw a 'O' else if (cur_val=='*') cout << "*"; // val is 2, so draw a '*' } bool Is_Legal(const vector<vector<char> >Board, int cur_col) { if(Board[0][cur_col] != '-') { return false; } return true; } bool Tie_Check(const vector<vector<char> >& Board) { for(int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if(Board[i][j] == '-') { return false; } else { return true; } } } } //function that checks to see if the game is over bool End_Game_Check(const vector<vector<char> >Board, int row, int cur_col, char cur_move) { cout << "row " << row << " cur_col " << cur_col << " cur_move " << cur_move << endl; //check for horizontal win in the current row (4 cases) if(Board[row][0]== cur_move && Board [row][1]== cur_move && Board [row][2]== cur_move && Board[row][3]==cur_move) { return true; } if(Board[row][1]== cur_move && Board [row][2]== cur_move && Board [row][3]== cur_move && Board[row][4]==cur_move) { return true; } if(Board[row][2]== cur_move && Board [row][3]== cur_move && Board [row][4]== cur_move && Board[row][5]==cur_move) { return true; } if(Board[row][3]== cur_move && Board [row][4]== cur_move && Board [row][5]== cur_move && Board[row][6]==cur_move) { return true; } //check for vertical win in the current column (3 cases) if(Board[5][cur_col] == cur_move && Board[4][cur_col] == cur_move && Board[3][cur_col] && Board[2][cur_col] == cur_move) { return true; } if(Board[4][cur_col] == cur_move && Board[3][cur_col] == cur_move && Board[2][cur_col] && Board[1][cur_col] == cur_move) { return true; } if(Board[3][cur_col] == cur_move && Board[2][cur_col] == cur_move && Board[1][cur_col] && Board[0][cur_col] == cur_move) { return true; } //check for diagonal win that contains the current position (6 cases) if(Board[row][cur_col] == Board[row + 1][cur_col- 1] && Board[row + 1][cur_col - 1] == Board[row + 2][cur_col - 2] && Board[row + 2][cur_col - 2] == Board[row + 3][cur_col - 3]) { return true; } if(Board[row][cur_col] == Board[row + 1][cur_col - 1] && Board[row + 1][cur_col - 1] == Board[row + 2][cur_col- 2] && Board[row + 2][cur_col- 2] == Board[row - 1][cur_col + 1]) { return true; } if(Board[row][cur_col] == Board[row + 1][cur_col - 1] && Board[row + 1][cur_col - 1] == Board[row - 1][cur_col + 1] && Board[row - 1][cur_col + 1] == Board[row - 2][cur_col + 2]) { return true; } if(Board[row][cur_col] == Board[row + 1][cur_col + 1] && Board[row][cur_col] == Board[row + 2][cur_col + 2] && Board[row][cur_col] == Board[row + 3][cur_col + 3]) { return true; } if(Board[row][cur_col] == Board[row - 1][cur_col - 1] && Board[row][cur_col] == Board[row + 1][cur_col + 1] && Board[row][cur_col] == Board[row + 2][cur_col+ 2]) { return true; } if(Board[row][cur_col] == Board[row - 2][cur_col - 2] && Board[row][cur_col] == Board[row - 1][cur_col - 1] && Board[row][cur_col] == Board[row + 1][cur_col + 1]) { return true; } else { return false; } } void Print_Board(const vector<vector<char> >&Board) { for (int i=0; i<rows; i++) { cout << endl; for(int j=0; j<cols; j++) { cout << Board[i][j]; } cout << endl; } } int main() { int cur_row; int cur_col; vector<vector<char> >Board; Board.resize(rows); for (int i=0; i<rows; i++) { Board[i].resize(cols); for (int j=0; j<cols; j++) { Board[i][j] = '-'; } } Print_Board(Board); bool Game_Over=false; char cur_move='O';//o goes first while (!Game_Over) { cout<<"Player " << cur_move <<" please enter the column in which you would like your marker placed"<<endl; cin >> cur_col; while(Is_Legal(Board,cur_col - 1)==false) { cout << "illegal column please try enter column again"<<endl; cin >> cur_col; } bool Place_Marker=false; int i=5; do { if (Board[i][cur_col-1]=='-') { Board[i][cur_col-1]=cur_move; Place_Marker=true; Print_Board(Board); } else { i--; } }while(Place_Marker==false && i>=0); Game_Over=End_Game_Check(Board,i , cur_col-1, cur_move); cout << "GOT HERE 1" << endl; if(Game_Over == true) { cout << endl << cur_move << " has won!" << endl; } if(Game_Over == false) { bool Draw = Tie_Check(Board); if(Draw == true) { cout << "There's a DRAW - GAME OVER"; Game_Over = true; } } if(cur_move=='O') { cur_move='*'; } else { cur_move='O'; } } system("pause"); return 0; }
__________________
  
Vorph is offline   Reply With Quote
Old 03-30-2007, 11:59 AM   #11 (permalink)
Vorph
-666 Internets
 
Vorph's Avatar
 
Join Date: May 2002
Location: Hell
Posts: 4,985
Ok, that was easy. You're going out of bounds on your win checks... you can't just add and subtract to the column without making sure you're still inside the board. Also you're using constants for the size of the board, but you have hard-coded stuff like Board[row][6] which is only valid with the current board size. If someone changes those constants at the top of the code, your win checker won't work at all.
__________________
  

Last edited by Vorph : 03-30-2007 at 12:03 PM.
Vorph is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
uberguilds network



All times are GMT -7. The time now is 10:18 PM.


Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6