|
| |||||||
| |
![]() |
| | LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
| | #1 (permalink) |
| Registered User Join Date: Mar 2005
Posts: 65
| 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.
__________________ Last edited by Black Magic : 03-29-2007 at 11:19 AM. |
| | |
| | #2 (permalink) |
| Registered User Join Date: Mar 2005
Posts: 65
| 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 { if(Board[0][cur_col] != '-') { return false; } return true; } bool Tie_Check(const vector { 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 { 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 { 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.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"< while(Is_Legal(Board,cur_col - 1)==false) { cout << "illegal column please try enter column again"< } 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; }
__________________ |
| | |
| | #6 (permalink) |
| Registered User Join Date: Mar 2005
Posts: 65
| Code:
Edit: There it goes.
__________________ |
| | |
| | #8 (permalink) |
| Registered User Join Date: Oct 2004
Posts: 1,685
| Still looks messed up. You can use pastebin - collaborative debugging tool to paste your code better. |
| | |
| | #10 (permalink) |
| Never Go Full Retard Join Date: May 2002 Location: Hell
Posts: 5,618
| 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:
|
| | |
| | #11 (permalink) |
| Never Go Full Retard Join Date: May 2002 Location: Hell
Posts: 5,618
| 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. |
| | |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
| |