3 Replies - 208 Views - Last Post: 03 October 2011 - 06:34 AM Rate Topic: -----

#1 LostSoul  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-October 11

Newbie C++ For loop issue

Posted 02 October 2011 - 10:19 PM

So I am trying to create a Tic-Tac-Toe game and have read a few topics of others doing the same. I feel like I am on track but cannot get my loops to stop all cycling with the first entry I have tried
this
for(rowVar == 1 && colvar == 1;grid[0][0] == 'X' || grid[0][0] == 'O';choice++
That did not work because as soon as I asked for the first entry my initialization statement was true ,but so was my test expression. It seems I cannot find a way to stop these loops.
Thank you in advance for any help as my assignment was due 12 minutes ago and I have been messing with this for going on a week.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;


//Global constants
	const int rows = 3;			//Rows on the game board
	const int col = 3;		    //Columns on the game board

//Global variables
	int     choice,
			choice1 = 0,
			choice2	= 0,
			choice3 = 0,
			choice4 = 0,
			choice5 = 0,
			choice6 = 0,
			choice7 = 0,
			choice8 = 0,
			choice9 = 0,
  turnsAllowed = 9,
		rowVar = 10,			//used for player to select the row they want to mark
		colVar = 10,			//used for player to select the column the want to mark
		  Turn = 0;				//used with exchanging turns equation line 80
   bool winner = false;			

//Function prototypes
	void displayBoard(char [][col]);
	
//2D Array for the game board
	char grid[rows][col];

int _tmain(int argc, _TCHAR* argv[])
{
	//initialize the array
	char grid[rows][col] = {{'*','*','*'},
							{'*','*','*'},
							{'*','*','*'}}; 	
	
	//call the board
	displayBoard(grid);

	//ask for row and column
	cout << "\n";
	cout << "Enter a row and column to place an X." << "\nRow: ";
	cin >> rowVar;
	cout << "\nColumn:";
	cin >> colVar;
	//grid[rowVar][colVar]; //enter the row and column


	while( winner == false || choice < turnsAllowed )
	{		
		
		for(rowVar == 1 && colVar == 1;choice1 < 1; choice1++)
		{
			if( Turn == 0 ) 
			{
				grid[0][0] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
								
			}
			else 
			{
				grid[0][0] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
								
			}
		}         
	    
		for(rowVar == 1 && colVar == 2;choice2 < 1; choice2++)
		{
			if( Turn == 0 ) 
			{
				grid[0][1] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[0][1] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		}         
	    
		//if(rowVar == 0 || colVar == 2)
		for(rowVar == 1 && colVar == 3;choice3 < 1; choice3++)
		{
			if( Turn == 0 ) 
			{
				grid[0][2] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[0][2] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		}         
	    
		for(rowVar == 2 && colVar == 1;choice4 < 1; choice4++)
		{
			if( Turn == 0 ) 
			{
				grid[1][0] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[1][0] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		}

		for(rowVar == 2 && colVar == 2;choice5 < 1; choice5++)
		{
			if( Turn == 0 ) 
			{
				grid[1][1] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[1][1] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		}         
	    
		for(rowVar == 2 && colVar == 3;choice6 < 1; choice6++)
		{
			if( Turn == 0 ) 
			{
				grid[1][2] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[1][2] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		}         
	    
		for(rowVar == 3 && colVar == 1;choice7 < 1; choice7++)
		{
			if( Turn == 0 ) 
			{
				grid[2][0] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[2][0] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		}         
	    
		for(rowVar == 3 && colVar == 2;choice8 < 1; choice8++)
		{
			if( Turn == 0 ) 
			{
				grid[2][1] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[2][1] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		}

		for(rowVar == 3 && colVar == 3;choice9 < 1; choice9++)
		{
			if( Turn == 0 ) 
			{
				grid[2][2] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[2][2] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		  }
		        
	    
	    
	    
		Turn = (Turn +1) % 2;
		cout << "\nRow:";
		cin >> rowVar;
		cout << "\nColumn:";
		cin >> colVar;
		cout << "test3";
		system("pause");

		
	}
	       
	    

		

	system("pause");
	return 0;
	
}


//*******************************************************************************
//Definition of function showBoard												*
//To print the game board														*
//*******************************************************************************
	
void displayBoard(char grid[rows][col]) 
{
	
	cout << "        Columns\n";
	cout <<    "        1  2  3\n";
	cout << "Row 1:  "<< grid[0][0] << "  " << grid[0][1] << "  " << grid[0][2] << endl;
	cout << "Row 2:  "<< grid[1][0] << "  " << grid[1][1] << "  " << grid[1][2] << endl;
	cout << "Row 3:  "<< grid[2][0] << "  " << grid[2][1] << "  " << grid[2][2];
}




Is This A Good Question/Topic? 0
  • +

Replies To: Newbie C++ For loop issue

#2 LostSoul  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-October 11

Re: Newbie C++ For loop issue

Posted 02 October 2011 - 11:04 PM

I changed my loops to if statements and so far I am where I want

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;


//Global constants
	const int rows = 3;			//Rows on the game board
	const int col = 3;		    //Columns on the game board

//Global variables
	int     choice,
			choice1 = 0,
			choice2	= 0,
			choice3 = 0,
			choice4 = 0,
			choice5 = 0,
			choice6 = 0,
			choice7 = 0,
			choice8 = 0,
			choice9 = 0,
  turnsAllowed = 9,
		rowVar = 44,			//used for player to select the row they want to mark
		colVar = 44,			//used for player to select the column the want to mark
		  Turn = 0;				//used with exchanging turns equation line 80
   bool winner = false;			

//Function prototypes
	void displayBoard(char [][col]);
	
//2D Array for the game board
	char grid[rows][col];

int _tmain(int argc, _TCHAR* argv[])
{
	//initialize the array
	char grid[rows][col] = {{'*','*','*'},
							{'*','*','*'},
							{'*','*','*'}}; 	
	
	//call the board
	displayBoard(grid);

	//ask for row and column
	cout << "\n";
	cout << "Enter a row and column to place an X." << "\nRow: ";
	cin >> rowVar;
	cout << "\nColumn:";
	cin >> colVar;
	//grid[rowVar][colVar]; //enter the row and column


	while( winner == false || choice < turnsAllowed )
	{		
		if(rowVar < 1 || rowVar > 3 || colVar < 1 || colVar > 3)
		{
			cout << "Please choose from the Rows and Columns listed.";
		}
		
		else if(rowVar == 1 && colVar == 1 && choice1 < 1)
		{
			if( Turn == 0 ) 
			{
				grid[0][0] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				choice1++;	
				
			}
			else 
			{
				grid[0][0] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				choice1++;				
			}
		}         
	    
		else if(rowVar == 1 && colVar == 2)
		{
			if( Turn == 0 ) 
			{
				grid[0][1] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[0][1] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		}         
	    
		//if(rowVar == 0 || colVar == 2)
		else if(rowVar == 1 && colVar == 3)
		{
			if( Turn == 0 ) 
			{
				grid[0][2] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[0][2] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		}         
	    
		else if(rowVar == 2 && colVar == 1)
		{
			if( Turn == 0 ) 
			{
				grid[1][0] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[1][0] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		}

		else if(rowVar == 2 && colVar == 2)
		{
			if( Turn == 0 ) 
			{
				grid[1][1] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[1][1] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		}         
	    
		else if(rowVar == 2 && colVar == 3)
		{
			if( Turn == 0 ) 
			{
				grid[1][2] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[1][2] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		}         
	    
		else if(rowVar == 3 && colVar == 1)
		{
			if( Turn == 0 ) 
			{
				grid[2][0] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[2][0] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		}         
	    
		else if(rowVar == 3 && colVar == 2)
		{
			if( Turn == 0 ) 
			{
				grid[2][1] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[2][1] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		}

		else if(rowVar == 3 && colVar == 3)
		{
			if( Turn == 0 ) 
			{
				grid[2][2] = 'X';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an O .";
				
			}
			else 
			{
				grid[2][2] = 'O';
				displayBoard(grid);
				cout << "\nEnter a row and column to place an X .";
				
			}
		  }
		
			    
		Turn = (Turn +1) % 2;//change from X to O
		cout << "\nRow:";
		cin >> rowVar;
		cout << "\nColumn:";
		cin >> colVar;
		choice++; 		
		

		
	}
	       
	    

		

	system("pause");
	return 0;
	
}


//*******************************************************************************
//Definition of function showBoard												*
//To print the game board														*
//*******************************************************************************
	
void displayBoard(char grid[rows][col]) 
{
	
	cout << "        Columns\n";
	cout <<    "        1  2  3\n";
	cout << "Row 1:  "<< grid[0][0] << "  " << grid[0][1] << "  " << grid[0][2] << endl;
	cout << "Row 2:  "<< grid[1][0] << "  " << grid[1][1] << "  " << grid[1][2] << endl;
	cout << "Row 3:  "<< grid[2][0] << "  " << grid[2][1] << "  " << grid[2][2];
}


Was This Post Helpful? 0
  • +
  • -

#3 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 603
  • View blog
  • Posts: 1,863
  • Joined: 02-August 09

Re: Newbie C++ For loop issue

Posted 03 October 2011 - 06:29 AM

In the first code you posted, the format of your for loop seems incorrect.

i.e.
for(rowVar == 3 && colVar == 2;choice8 < 1; choice8++)


The format of a for loop is as follows.

for (initialisation; condition; increment)

Was This Post Helpful? 0
  • +
  • -

#4 Kilorn  Icon User is offline

  • XNArchitect
  • member icon




Reputation: 1325
  • View blog
  • Posts: 3,504
  • Joined: 03-May 10

Re: Newbie C++ For loop issue

Posted 03 October 2011 - 06:34 AM

Looping through a 2d array requires nested for loops instead of just one for loop.
for (x = 0; x < blah; x++)
{
    for (y = 0; y < argh; y++)
    {
        // ... Logic goes here.
    }
}


This post has been edited by Kilorn: 03 October 2011 - 06:35 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1