2 Replies - 236 Views - Last Post: 20 February 2013 - 09:44 PM Rate Topic: -----

#1 splicecube  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 03-November 12

Simple Alternation Problem?

Posted 20 February 2013 - 07:30 PM

Ok so long story short, this is a program that I am working on that is very similar to tic-tac-toe in that this game uses 2 players: X and O and it check for 3 in a row. In my main method I have a variable called turn which is initialized to 1 and should ideally alternate between 1 and -1 which respectively tell whether Player X or Player O should be playing. I have if statements that should change the "turn" variable between 1 and -1, but no matter what the turn is always 1 and it is always Player X's turn. Can someone tell me what minor error I'm having in my main method that prevents the alternation of my "turn" variable???? Your efforts are great appreciated. Thank you in advance!
(This code runs, it just always has turn set to 1, which is player X)

#include <cstdlib>
#include <iostream>
using namespace std;

char      a1='.';
char	  a4='.';
char     a7='.';
char	 b2='.';
char	  b4='.';
char	  b6='.';
char	  c3='.';
char	  c4='.';
char	  c5='.';
char	  d1='.';
char	  d2='.';
char	  d3='.';
char	  d5='.';
char	  d6='.';
char	  d7='.';
char	  e3='.';
char	  e4='.';
char	  e5='.';
char	  f2='.';
char	  f4='.';
char	  f6='.';
char	  g1='.';
char	  g4='.';
char	  g7='.';
	
	
void displayBoard()
	{
	cout<<"\n\n    1   2   3  4  5   6   7     \n";
cout<<"\n A  ";cout<<a1<<"..........";cout<<a4<<"..........";cout<<a7<<"  A ";
cout<<"\n    .          .          .	   ";
cout<<"\n B  .   ";cout<<b2<<"......";cout<<b4<<"......";cout<<b6<<"   .  B "; 
cout<<"\n    .   .      .      .   .    ";
cout<<"\n C  .   .   ";cout<<c3<<"..";cout<<c4<<"..";cout<<c5<<"   .   .  C ";
cout<<"\n    .   .   .     .   .   .    ";
cout<<"\n D  ";cout<<d1<<"...";cout<<d2<<"...";cout<<d3<<"     ";cout<<d5<<"...";cout<<d6<<"...";cout<<d7<<"  D ";
cout<<"\n    .   .   .     .   .   .    ";
cout<<"\n E  .   .   ";cout<<e3<<"..";cout<<e4<<"..";cout<<e5<<"   .   .  E ";
cout<<"\n    .   .      .      .   .    ";
cout<<"\n F  .   ";cout<<f2<<"......";cout<<f4<<"......";cout<<f6<<"   .  F "; 
cout<<"\n    .          .          .    ";
cout<<"\n G  ";cout<<g1<<"..........";cout<<g4<<"..........";cout<<g7<<"  G ";
cout<<"\n\n    1   2   3  4  5   6   7    \n\n";
	}//end displayBoard()

//void ThreeInRow(char a, char b, char c)
//
//{
//	
////switch(piece)
//
//}

	/* 
      When asking the user where to place their piece, they would enter, for example A1, the A and the 1 would be scanned as
		individual variables. 
		   -Then what???????????????????
	*/



 
 
 
void Mover(int x)
{
     
     
     
     }

void playerPlace(char player)
{
     char row;
     char column;
	cout<<"Player "<<player<<" where would you like to place your piece? ";
    
    //Where would I declare row and column?
	cin>>row>>column;
//	cout<<row<<column;
	
if (row=='A')

	{cout<<"Im in the outter!";
           if (column=='1')
           {a1=player;}
           else if (column=='4')
           {a4=player;}
           else if (column=='7')
           {a7=player;}
           }
else if (row=='B')
	{
           if (column=='2')
           {b2=player;}
           else if (column=='4')
           {b4=player;}
           else if (column=='6')
           {b6=player;}
               
               }
else if (row=='C')
	{
         if (column=='3')
           {c3=player;}
           else if (column=='4')
           {c4=player;}
           else if (column=='5')
           {c5=player;}      
               }
else if (row=='D')
	{
           if (column=='1')
           {d1=player;}
           else if (column=='2')
           {d2=player;}
           else if (column=='3')
           {d3=player;}
           else if (column=='5')
           {d5=player;}
           else if (column=='6')
           {d6=player;}
           else if (column=='7')
           {d7=player;}       
               }
else if (row=='E')
	{
         if (column=='3')
           {e3=player;}
           else if (column=='4')
           {e4=player;}
           else if (column=='5')
           {e5=player;}      
               }
else if (row=='F')
	{
          if (column=='2')
           {f2=player;}
           else if (column=='4')
           {f4=player;}
           else if (column=='6')
           {f6=player;}     
               }
else if (row=='G')
 {
          if (column=='1')
           {g1=player;}
           else if (column=='4')
           {g4=player;}
           else if (column=='7')
           {g7=player;}  
            
            }
// else
// {
//     
//     }
	//take these values and make if statements
	//if (row==A&&column==1)
	//a1='X';
	//
	//24 if statements?Is there a simpler way???
	
	//In these if statements you'd want to have a 
    //rowCount and a columnCount if either is three
    //a mill should occur 

}

//cout<<"the user enters"<<userinput<<"";

void checkMill(){}

void checkWinner(){}

 void playerXmove(){}
 
 void playerOmove(){}
 
int main ()
{
     char row;
     char column;
     char playerToGo = 'X';
     bool winnerExists =0;
    
	char userInput = ' ';//takes the user input and stores it
	//intializes a turn variable that alternates 
    //between -1, and 1 to tell which user's turn it is (-1 for O, 1 for X)
    int xPieceNum =1;
    int oPieceNum=1;
	  

cout<<"Alfred Joseph\n";
cout<<"Project 3, Nine Man's Morris";
	displayBoard();
	 int turn=1;
	do{
 
	if (turn=1)
	{playerToGo='X';}
	else
	{playerToGo='O';}

   if(xPieceNum<10||oPieceNum<10)
   {
	playerPlace(playerToGo);
	cout<<turn;
	displayBoard();
	checkMill();

	checkWinner();
    turn=turn*-1;
//	system("pause");
   }
   else
   {
       Mover(playerToGo);
   	displayBoard();  
  	   checkMill();
  	  
	   checkWinner();
	  
//	   	system("pause");
turn=turn*-1;
   }

    
} 
while (winnerExists==0);
}






yea that doesn't look nice...

#include <cstdlib>
#include <iostream>
using namespace std;

char a1='.';
char	 a4='.';
char a7='.';
char	 b2='.';
char	 b4='.';
char	 b6='.';
char	 c3='.';
char	 c4='.';
char	 c5='.';
char	 d1='.';
char	 d2='.';
char	 d3='.';
char	 d5='.';
char	 d6='.';
char	 d7='.';
char	 e3='.';
char	 e4='.';
char	 e5='.';
char	 f2='.';
char	 f4='.';
char	 f6='.';
char	 g1='.';
char	 g4='.';
char	 g7='.';


void displayBoard()
{
cout<<"\n\n 1 2 3 4 5 6 7 \n";
cout<<"\n A ";cout<<a1<<"..........";cout<<a4<<"..........";cout<<a7<<" A ";
cout<<"\n . . .	 ";
cout<<"\n B . ";cout<<b2<<"......";cout<<b4<<"......";cout<<b6<<" . B "; 
cout<<"\n . . . . . ";
cout<<"\n C . . ";cout<<c3<<"..";cout<<c4<<"..";cout<<c5<<" . . C ";
cout<<"\n . . . . . . ";
cout<<"\n D ";cout<<d1<<"...";cout<<d2<<"...";cout<<d3<<" ";cout<<d5<<"...";cout<<d6<<"...";cout<<d7<<" D ";
cout<<"\n . . . . . . ";
cout<<"\n E . . ";cout<<e3<<"..";cout<<e4<<"..";cout<<e5<<" . . E ";
cout<<"\n . . . . . ";
cout<<"\n F . ";cout<<f2<<"......";cout<<f4<<"......";cout<<f6<<" . F "; 
cout<<"\n . . . ";
cout<<"\n G ";cout<<g1<<"..........";cout<<g4<<"..........";cout<<g7<<" G ";
cout<<"\n\n 1 2 3 4 5 6 7 \n\n";
}//end displayBoard()

//void ThreeInRow(char a, char b, char c)
//
//{
//	
////switch(piece)
//
//}

/* 
When asking the user where to place their piece, they would enter, for example A1, the A and the 1 would be scanned as
individual variables. 
-Then what???????????????????
*/






void Mover(int x)
{



}

void playerPlace(char player)
{
char row;
char column;
cout<<"Player "<<player<<" where would you like to place your piece? ";

//Where would I declare row and column?
cin>>row>>column;
//	cout<<row<<column;

if (row=='A')

{cout<<"Im in the outter!";
if (column=='1')
{a1=player;}
else if (column=='4')
{a4=player;}
else if (column=='7')
{a7=player;}
}
else if (row=='B')
{
if (column=='2')
{b2=player;}
else if (column=='4')
{b4=player;}
else if (column=='6')
{b6=player;}

}
else if (row=='C')
{
if (column=='3')
{c3=player;}
else if (column=='4')
{c4=player;}
else if (column=='5')
{c5=player;} 
}
else if (row=='D')
{
if (column=='1')
{d1=player;}
else if (column=='2')
{d2=player;}
else if (column=='3')
{d3=player;}
else if (column=='5')
{d5=player;}
else if (column=='6')
{d6=player;}
else if (column=='7')
{d7=player;} 
}
else if (row=='E')
{
if (column=='3')
{e3=player;}
else if (column=='4')
{e4=player;}
else if (column=='5')
{e5=player;} 
}
else if (row=='F')
{
if (column=='2')
{f2=player;}
else if (column=='4')
{f4=player;}
else if (column=='6')
{f6=player;} 
}
else if (row=='G')
{
if (column=='1')
{g1=player;}
else if (column=='4')
{g4=player;}
else if (column=='7')
{g7=player;} 

}
// else
// {
// 
// }
//take these values and make if statements
//if (row==A&&column==1)
//a1='X';
//
//24 if statements?Is there a simpler way???

//In these if statements you'd want to have a 
//rowCount and a columnCount if either is three
//a mill should occur 

}

//cout<<"the user enters"<<userinput<<"";

void checkMill(){}

void checkWinner(){}

void playerXmove(){}

void playerOmove(){}

int main ()
{
char row;
char column;
char playerToGo = 'X';
bool winnerExists =0;

char userInput = ' ';//takes the user input and stores it
//intializes a turn variable that alternates 
//between -1, and 1 to tell which user's turn it is (-1 for O, 1 for X)
int xPieceNum =1;
int oPieceNum=1;


cout<<"Alfred Joseph\n";
cout<<"Project 3, Nine Man's Morris";
displayBoard();
int turn=1;
do{

if (turn=1)
{playerToGo='X';}
else
{playerToGo='O';}

if(xPieceNum<10||oPieceNum<10)
{
playerPlace(playerToGo);
cout<<turn;
displayBoard();
checkMill();

checkWinner();
turn=turn*-1;
//	system("pause");
}
else
{
Mover(playerToGo);
displayBoard(); 
checkMill();

checkWinner();

//	 system("pause");
turn=turn*-1;
}


} 
while (winnerExists==0);
}





This post has been edited by Skydiver: 20 February 2013 - 09:26 PM
Reason for edit:: Fixed code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Simple Alternation Problem?

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1344
  • View blog
  • Posts: 4,608
  • Joined: 19-February 09

Re: Simple Alternation Problem?

Posted 20 February 2013 - 08:14 PM

204	if (turn=1)


Was This Post Helpful? 1
  • +
  • -

#3 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Simple Alternation Problem?

Posted 20 February 2013 - 09:44 PM

For two player turn based games, I like this:

int player = 0; //could be = 1, whichever way you count and could be bool

//later, after each move:
player = !player; //changes player from 0 to 1 or 1 to 0.



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1