Thread: player option

  1. #1
    Registered User synyster's Avatar
    Join Date
    May 2011
    Posts
    1

    player option

    I am a little stuck with a game I am working on anmd this is not neccessary but I like to try and outdo myself if possible, I fail a lot I am looking at how you would set up so that if 2 players inputed their names it said hi player 1 it is your turn and vice a versa. I have a little code here and if somebody can help that is all good but it is mainly for fun now.

    Code:
    void playerNames(){ //players can enter their names
    	
    
    	cout<<"\t\t"<<"Player One enter name you are X:"; cin>>name1; "\n";
    	
    	cout<<"\t\t"<<"Player Two enter name you are O:"; cin>>name2; "\n";
    	
    	
    }
    
    
    void playGame(){
    	 
        symbol='X','O';
        playerNames();
       do
       {
    	  do
    	  {
    		 
    		 displayBoard();//dispays board for each turn
    		 
    		 cout<<"\n\n"<< name1 <<" choose a column to place your go:";// instructs user to take their go
    		 cin>>column;
    		 system("cls");// refreshes screen once x takes their go, refreshes upon each new go.
    		
    		 
    
    		 if (column<1 || column>7) 
    			cout <<"\n"<<"that is an invaild choice choose 1-7"<<"\n";//if a column less than 1 or more than 7 is chosen the error message is outputted to the screen
    			
    	  }
          while (column<1 || column>7);
    
          column--;
    	  i=0; 
          
    	  while ((gameArray[i][column]=='X' || gameArray[i][column]=='O') && i<7)//if one of the game array slots is equal to X or O
             		    i++;
    		  	  
          
    	  if (i<7)
    	  {
    	     gameArray[i][column]=symbol;
    	     count++;
    		 
    	  
             if (symbol=='X')//symbol is set to start as always X then O goes
    	  		symbol='O';
    	     else symbol='X';
    	  }
    	  else cout <<"\n"<<"column is full please try another"<<"\n";
    	  
       }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Just add a variable like so

    int currentPlayer = 0;


    At the change of turn, you do
    if ( currentPlayer == 0 ) currentPlayer = 1; else currentPlayer = 0;


    At other points in the code, you just have
    if ( currentPlayer == 0 ) ... else ...
    to control such things as displaying names, choosing player symbol etc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it a watch? Is it an MP3 player? Is it a video player?
    By twomers in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-15-2006, 10:54 PM
  2. y or n option?
    By Munkey01 in forum C++ Programming
    Replies: 11
    Last Post: 12-24-2002, 10:41 AM
  3. VS.NET .SLN Option
    By nTanker in forum C# Programming
    Replies: 0
    Last Post: 09-25-2002, 01:44 PM
  4. wave player or mp3 player using C
    By lliero in forum C Programming
    Replies: 5
    Last Post: 02-27-2002, 11:33 AM