Connect 4 Crashing!

This is a discussion on Connect 4 Crashing! within the Game Programming forums, part of the General Programming Boards category; My connect 4 game compiles fine, You can enter the names, then after it prints the table it crashes.. Can ...

  1. #1
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    Connect 4 Crashing!

    My connect 4 game compiles fine, You can enter the names, then after it prints the table it crashes..
    Can anyone see why/point something out I did wrong?

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <windows.h>
    
    typedef unsigned short int USHORT;
    char board[7][6];
    char playername1[25];
    char playername2[25];
    char playerup = '$';
    const char player1 = '$';
    const char player2 = '%';
    const char blankchar = ' ';
    USHORT i, ii, iii, iiii, x, y, moves, move, row;
    const USHORT maxmoves = 7*6;
    
    int checkWin(char player);
    int printBoard();
    int getInput(char player);
    void init();
    int setInput(char player, USHORT move);
    
    
    void init()
    {
    	for (USHORT i = 0; i<6; i++)
    	{
    		for (USHORT ii = 0; i<7; i++)
    		{
    			board[ii][i] = blankchar;
    		}
    	}
    	cout << "\tWelcome to Connect 4!\n";
    	cout << "[$] Player 1, enter your name now->";
    	cin.ignore();
    	cin.getline(playername1, 25, '\n');
    	cout << "[*] Player 2; now yours->";
    	cin.getline(playername2, 25, '\n');
    }
    
    int printBoard()
    {
    	cout << "\n  ";
    	for (i = 1; i<7; i++)
    	{
    		cout << "  [" << i << "] ";
    	}
    	cout << "\n";
    	USHORT ii = 6;
    	for (USHORT i = 0; i<7; i++)
    	{
    		cout << "  |  " << board[i][ii];
    		if (i == 6)
    		{
    			cout << "\n";
    			cout << "  +-----+-----+-----+-----+-----+-----+  ";
    			ii--;
    			if (ii >= 0)
    			{
    				i = 0;
    			}
    			cout << "\n";
    		}
    	}
    
    	return 0;
    }
    
    int getInput(char player)
    {
    	if (player == player1)
    	{
    		cout << "\t" << playername1 << ", make your move->";
    		cin>>move;
    		return 1;
    	} else {
    		cout << "\t" << playername2 << ", make your move->";
    		cin>>move;
    		return 2;
    	}
    }
    
    int setInput(char player, USHORT move)
    {
    
    	USHORT i = 0;
    	while (i < 6)
    	{	
    		if (board[move][i] != ' ')
    		{
    			i++;
    			if (i == 5)
    			{
    				cout << "That column is full!";
    				getInput(player);
    				return 0;
    			}
    			
    		} else {
    			board[move][i] = player;
    			if (player == player1)
    			{
    				return 1;
    			} else {
    				return 2;
    			}
    		}
    	}
    }
    
    int checkWin(char player)
    {
    	USHORT row = 0;
    	for (i = 0; i<4; i++)
    	{
    		for (USHORT ii = 1; ii<5; ii++)
    		{	
    			for (USHORT iii = 2; iii<6; iii++)
    			{
    				for (USHORT iiii = 3; iiii<7; iiii++)
    				{
    					if ((board[row][i] == board[row][ii]) && (board[row][ii] == board[row][iii]) && (board[row][iii] == board[row][iiii]))
    					{
    						if (player == player1)
    						{
    							//horizontal win;
    							return 1;
    						} else {
    							//horizontal win;
    							return 2;
    						}
    					}
    					if (iiii == 6)
    					{
    						if (row < 6)
    						{
    							row++;
    						}
    						i = 0;
    						ii = i++;
    						iii = ii++;
    						iiii = iii++;
    					}	
    				}
    			}
    		}
    	}
    
    	USHORT column = 0;
    	USHORT &c = column;
    	for (i = 0; i<3; i++)
    	{
    		for (USHORT ii = 1; ii<4; ii++)
    		{	
    			for (USHORT iii = 2; iii<5; iii++)
    			{
    				for (USHORT iiii = 3; iiii<6; iiii++)
    				{
    					if ((board[i][c] == board[ii][c]) && (board[ii][c] == board[iii][c]) && (board[iii][c] == board[iiii][c]))
    					{
    						if (player == player1)
    						{
    							//vertical win;
    							return 1;
    						} else { 
    							//vertical win;
    							return 2;
    						}
    					}
    					if (iiii == 5)
    					{
    						if (c < 6)
    						{
    							c++;
    						}
    						i = 0;
    						ii = i++;
    						iii = ii++;
    						iiii = iii++;
    					}
    				}
    			}
    		}
    	}
    	return 0;
    
    }
    
    int main ()
    {
    
    	init();
    	system("CLS");
    	printBoard();
    	do
    	{
    		if (moves < maxmoves)
    		{
    				getInput(playerup);
    				while (setInput(playerup, move))
    				{	
    					cout << "\tThat move has already been made!\n\n";
    					getInput(playerup);
    				}
    				system("CLS");
    				printBoard();
    				if (playerup == player2)
    				{
    					playerup = player1;
    				} else {
    					playerup = player2;
    				}
    		} else {
    			cout << "\n\tNo winner this time, thanks for playing\n\n\n\t";
    			//system("PAUSE");
    			_getch();
    			return 0;
    		}
    	} while (!checkWin(playerup));
    	if (playerup == player1)
    	{ 
    		cout << "\n\tCongratulations " << playername2 << ", you've won!\n\n\n\t";
    		//system("PAUSE");
    		_getch();
    		return 0;
    	} else {
    		cout << "\n\tCongratulations " << playername1 << ", you've won!\n\n\n\t";
    		//system("PAUSE");
    		_getch();
    		return 0;
    	}
    }
    I'm aware that not all the winning conditions are done yet.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    You made it very hard for yourself to start off with using global variables that should be local!! and names like i, ii, iii and iiii .... you're asking for confusion!!

    anyway, your printboard function should look something like this:

    Code:
    int printBoard()
    {
    	cout << "\n  ";
    	for (i = 1; i<7; i++)
    	{
    		cout << "  [" << i << "] ";
    	}
    
    	cout << "\n";
    
    	USHORT ii = 6;
    	for(USHORT j = 0; j < 6; j++)
    	{
    		cout << "\n  +-----+-----+-----+-----+-----+-----+  \n";
    		for (USHORT i = 0; i<7; i++)
    		{
    			cout << "  |  " << board[i][ii];
    		}
    		ii--;
    	}
    	cout << "\n  +-----+-----+-----+-----+-----+-----+  \n\n";
    
    	return 0;
    }
    be warned though... there LOTS of bugs in your code... this was not the only one i came across while compiling (I mean that in the nicest possible way )

    See ya
    good luck
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking connect()?
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 04-22-2009, 03:40 PM
  2. connect() function, strange error
    By Mr_Miguel in forum C Programming
    Replies: 1
    Last Post: 12-12-2006, 05:51 PM
  3. connect timeout
    By X PaYnE X in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-14-2005, 09:30 PM
  4. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 02:46 PM
  5. MySQL Connect from Outside Computer
    By juschillin in forum Windows Programming
    Replies: 0
    Last Post: 09-27-2002, 08:02 AM

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