Some help

This is a discussion on Some help within the C++ Programming forums, part of the General Programming Boards category; I'm currently devoloping code for a version of the game Connect 4 that was assigned to me and right now ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    16

    Some help

    I'm currently devoloping code for a version of the game Connect 4 that was assigned to me and right now I'm at the initial stages. My problem is getting the move from the player and putting it on the board. I thought I had it figured it, when I compile I get no errors, however when I go to execute I get an error from microsoft saying that the .exe file has encountered an error and needs to close. The games played with X's and O's so I made an array called board. If anyone can take a look at my code and let me know what I'm doing wrong I would appreciate it.

    Here's what the first part of the assignment asks:

    Declare a 1D array in the function main which records the
    moves which have been made. (Hint: you know which player
    made each move -- the even moves are made by X, the odd
    moves by O.)
    Declare a 2D array in the function main, which holds the
    current state of the game.
    Write functions clearScreen and showBoard which can be used
    to redraw the board after each move. (Hint: showBoard must
    be passed the board array as a parameter.)
    Write a function getNextMove which:
    prompts the user whose turn it is to enter a move (Hint:
    getNextMove can figure this out if it knows which move
    this is)
    checks that it is a legal move (the column must be from
    1 to 7, and the column can't already be full)
    repeats the above until the player enters a legal move
    returns the move
    Write a loop in main which calls getNextMove, stores the
    move in the board array and displays the current state of
    the game, until the board is full.


    Code:
    #include<iostream>
    #include<cstring>
    #include<iomanip>
    #include<fstream>
    
    void clearScreen ();
    void showBoard (char board[6][7]);
    void initializeBoard (char board[6][7]);
    void getNextMove(char board[6][7], char trackMove[], int MAXMOVES, int row, int column, int currentMove);
    
    using namespace std;
    
    int main ()
    
    {
    
    	const int MAXMOVES = 42;
    	char board[6][7];
    	char trackMove[MAXMOVES];
    	int row = 5;
    	int column;
    	int currentMove;
    
    	showBoard(board);
    	
    	for (currentMove = 0; currentMove < MAXMOVES; currentMove++)
    	
    	{
    		getNextMove(board, trackMove, MAXMOVES, row, column, currentMove);
    		board[row][column - 1] = trackMove[currentMove];
    		clearScreen ();
    		showBoard(board);
    
    
    	}
    	
    	
    	return 0;
    
    }
    
    //Function Definitions
    
    void clearScreen ( void )
    {
      for ( int i = 0; i < 50; i++ )
        cout<<'\n';
      cout<<flush;
    }
    
    void showBoard (char board[6][7])
    {
    	
    	initializeBoard(board);
    
    	
    	cout<<setw(40)<<"  Connect 4"<<endl;
    	cout<<setw(30)<<endl;
    	cout<<setw(30)<<"| "<<board[0][0]<<" "<<board[0][1]<<" "<<board[0][2]<<" "<<board[0][3]<<" "<<board[0][4]<<" "<<board[0][5]<<" "<<board[0][6]<<" |"<<endl;
    	cout<<setw(30)<<"| "<<board[1][0]<<" "<<board[1][1]<<" "<<board[1][2]<<" "<<board[1][3]<<" "<<board[1][4]<<" "<<board[1][5]<<" "<<board[1][6]<<" |"<<endl;
    	cout<<setw(30)<<"| "<<board[2][0]<<" "<<board[2][1]<<" "<<board[2][2]<<" "<<board[2][3]<<" "<<board[2][4]<<" "<<board[2][5]<<" "<<board[2][6]<<" |"<<endl;
    	cout<<setw(30)<<"| "<<board[3][0]<<" "<<board[3][1]<<" "<<board[3][2]<<" "<<board[3][3]<<" "<<board[3][4]<<" "<<board[3][5]<<" "<<board[3][6]<<" |"<<endl;
    	cout<<setw(30)<<"| "<<board[4][0]<<" "<<board[4][1]<<" "<<board[4][2]<<" "<<board[4][3]<<" "<<board[4][4]<<" "<<board[4][5]<<" "<<board[4][6]<<" |"<<endl;
    	cout<<setw(30)<<"| "<<board[5][0]<<" "<<board[5][1]<<" "<<board[5][2]<<" "<<board[5][3]<<" "<<board[5][4]<<" "<<board[5][5]<<" "<<board[5][6]<<" |"<<endl;
    	cout<<setw(44)<<"  _ _ _ _ _ _ _ "<<endl;
    	cout<<setw(44)<<"  1 2 3 4 5 6 7 "<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    }
    
    void initializeBoard (char board[6][7])
    {
    	for(int row = 0; row < 6; row++)
    	for(int col = 0; col < 7; col++)
    		board[row][col] = ' ';
    }
    
    void getNextMove(char board[6][7], char trackMove[], int MAXMOVES, int row, int column, int currentMove)
    {
    	
    		
    	if (currentMove % 2 == 0)
    		{
    			cout<<"Player 1 make your move";
    			cout<<endl;
    			cout<<endl;
    			cout<<"Enter a column (1-7):  ";			
    			cin>>column;
    			cout<<endl;
    			cout<<endl;
    			while (!(column <=7 && column >=1))
    			{
    				cout<<"Please enter a legal move!  A number between 1-7 ";
    				cin>>column;
    				cout<<endl;
    			}
    			trackMove[currentMove] = 'X';
    			row--;
    		}
    
    	else
    		{
    			cout<<"Player 2 make your move.";
    			cout<<endl;
    			cout<<endl;
    			cout<<"Enter a column (1-7):  ";			
    			cin>>column;
    			cout<<endl;
    			cout<<endl;
    			while (!(column <=7 && column >=1))
    			{
    				cout<<"Please enter a legal move!  A number between 1-7 ";
    				cin>>column;
    				cout<<endl;
    			}
    			trackMove[currentMove] = 'O';
    			row--;
    			
    			
    		}
    
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    You didn't initialize "column".
    But it still doesn't work.

    gg

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    44
    Maybe the system is trying to spite you for writing such horribly formatted code. (just kidding)

    I did notice one problem in your method. It shouldn't cause this weird termination, but you'll run into it eventually.

    Have you ever heard of call-by-value/call-by-reference?
    Call-by-value is the normal way we pass data to a function.

    Such as: void myFunction(int x);

    When you call this function, it makes a copy of whatever int you pass it and stores it in the local variable x. So, if you change the value of the variable x, the variable you passed it from the calling function does not change.

    example:
    Code:
    void getSomething(int x)
    {
        x = 5;
    }
    
    int main()
    {
        int x = 8;
    
        getSomething(x);
        cout << x << endl;
    
        return 0;
    }
    Guess what this puts out. 8

    If you want to give the called function access to the variable in the calling function (give getSomething(x) access to the x in main()), you have to use call-by-reference. That is, instead of passing the called function the value x, pass it x's address and store it in a pointer. Then use the dereference operator (*) to access the variable.

    It's a lot more understandable when you just see the code.
    Code:
    void getSomething(int* x)
    {
        *x = 5;
    }
    
    int main()
    {
        int x = 8;
    
        getSomething(&x);
        cout << x << endl;
    
        return 0;
    }
    This code puts out 5.

    That's more or less how that getNextMove() function should work. Hope that helps you along.

    Edit: Come to think of it, that just may be your problem. Let me know how it turns out.
    Last edited by Wick; 11-09-2003 at 03:03 PM.
    Check out all my dimensions:
    Height, width, and for a limited time only... Depth!
    -sb

Popular pages Recent additions subscribe to a feed

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