User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,712 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,405 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 2407 | Replies: 1
Reply
Join Date: Apr 2006
Posts: 2
Reputation: qljolly is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
qljolly qljolly is offline Offline
Newbie Poster

Help Please Help C++ Connect four program due tonight by 12 (2)

  #1  
Apr 27th, 2006
some one told me to repost this with code tags so here it is again
I have been working on this for the past week and i am almost done but i dont know why my program keeps crashing at the end game
check and also it doesnt realize that there is a piece that interrupts the connect four if it were like XXXOX it still says that X wins and it is due by 12 oclock tonight andi dont know what to due please help!!!!!
I'm using Metroworks Code Warrior and Dev C++ for my writing my programs the code i have so far is below


 
#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;
}

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){
    //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);
        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;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Please Help C++ Connect four program due tonight by 12 (2)

  #2  
Apr 27th, 2006
>> 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])


when row == 5 the above causes exception because Board[row+1] is illegal cell reference. I responded with 1 to the question "PlayerO please enter the column in which you would like your marker placed". variable i is hardcoded to = 5 in main(), which may explain the problem.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:11 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC