Thread: Players don't change

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    8

    Question Players don't change

    Ok I have a Tic Tac Toe game, and I just changed the structure of it and it won't alternate between players. It switches in the function, but the player doesn't change when the loop starts over. Here are the three functions that will be needed:
    Code:
    void GameMain() {
    	GameInit();
    	do {
    		ShowGrid();
    		GetInput(player);
    		Write2Grid(choice, player);
    	}while (true);
    }
    
    char GetInput(int player) {  
    		switch(player) {	
    			case 1:
    			cout << "It's X's turn. " << endl;
    			cin >> choice;
    			break;
    		case 2:
    			cout << "It's O's turn. " << endl;
    			cin >> choice;
    			break;
    		default:
    			cout << "Error! Program will now terminate.";
    			getch();
    			exit(0);
    			break;
    	}
    	return choice;
    }
    
    void Write2Grid(int choice, int player) {	
    	if (choice < 1 || choice > 9) {
    		cout << "Select a number that's on the board." << endl;
    		getch();
    	}
    	char xo;					
    	xo = (player == 1) ? 'X' : 'O';
    	int c = 2 - (choice-1)/3;
    	int d = (choice-1)%3;
    	if ( (Grid[c][d] == 'X') || (Grid[c][d] == 'O') ) {
    		cout << "Spot has already been taken! Select another spot." << endl;
    		getch();
    	}
    	else {
    		Grid[c][d] = xo;
    		if (CheckWin() == true) {
    			system("cls");
    			ShowGrid();
    			cout << "Congradulations, "<< xo << " has won!" << endl;
    			getch();
    			PlayAgain();
    		}
    		else {
    			player = (player == 1) ? 2 : 1;
    		}
    	}
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>void Write2Grid(int choice, int player)
    Change that to
    void Write2Grid(int choice, int &player)

    Otherwise, the value of player will only be modified within the Write2Grid function, and the player will stay the same.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    8
    ok thanks, i have to understand pointers better so things like this won't happen again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++builder6 change form names problem
    By Leite33 in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2008, 08:20 AM
  2. how to change static char* and not lose mem ?
    By jabka in forum C Programming
    Replies: 15
    Last Post: 09-07-2007, 05:33 PM
  3. Change Value in an array
    By beginner999 in forum C Programming
    Replies: 3
    Last Post: 01-18-2003, 07:16 AM
  4. Replies: 2
    Last Post: 11-08-2002, 03:22 AM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM