Thread: Grid printer - need help

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    3

    Grid printer - need help

    im trying to make a program that would print 1 or 2 in a grid of 0. this is not my full code so it might look confusing but here is my problem :

    this program is supposed to print 1 or 2 (int this case 1) where the user wants to put it. but the output is always (no matter the input):

    020000000
    000000000
    000000000
    000000000
    000000000
    000000000
    000000000
    000000000
    000000000

    i dont know why it always prints 2 when it should print 1 or why its always in this position.

    can anyone help me and explain why this is happening ?



    Code:
    #include <iostream>
    
    int main()
    {
    	enum Players { P1, P2 };
    
    
    	enum FieldFillers { EMPTY, X_FILLED, O_FILLED };
    
    
    	int gridSize = 8;
    
    
    	int playerOnTurn = P1;
    
    
    	int fields[10][10];
    
    
    	for (int i = 0; i <= gridSize; i++)
    	{
    		for (int j = 0; j <= gridSize; j++)
    		{
    			fields[i][j] = EMPTY;
    		}	
    	}
    
    
    	int cordinates[2];
    
    
    
    
    	enum CordinatesDimentions { X_CORDINATE, Y_CORDINATE };
    
    
    	while (1)
    	{
    		std::cout << "\tEnter X cordinates  (number only)\n";
    		std::cin >> cordinates[X_CORDINATE];
    		cordinates[X_CORDINATE]--;
    
    
    		std::cout << cordinates[X_CORDINATE];
    
    
    		std::cout << "\tEnter Y cordinates  (number only)\n";
    		std::cin >> cordinates[Y_CORDINATE];
    		cordinates[Y_CORDINATE]--;
    
    
    		std::cout << cordinates[Y_CORDINATE];
    
    
    
    
    		if (cordinates[X_CORDINATE] <= gridSize &&
    			cordinates[Y_CORDINATE] <= gridSize &&
    			fields[X_CORDINATE][Y_CORDINATE] == EMPTY)
    		{
    			if (playerOnTurn = P1)
    			{
    				fields[X_CORDINATE][Y_CORDINATE] = X_FILLED;
    				break;
    			}
    
    
    			else
    			{
    				fields[X_CORDINATE][Y_CORDINATE] = O_FILLED;
    				break;
    			}
    		}
    	}
    
    
    	for (int x = 0; x <= gridSize; x++) 
    	{
    		for (int y = 0; y <= gridSize; y++)
    		{
    			std::cout << fields[x][y];
    		}
    		std::cout << '\n';
    	}
    }

  2. #2
    Guest
    Guest
    The whole design seems unnecessarily complicated. I would suggest you simplify the code where ever possible and print out variables at several stages to verify their values behave as predicted.

    I would first get rid of the enums. Use two ints x and y for the coordinates. Define the fields using the gridSize constant. Make sure your loop conditions fit the case. Take out P1 checking until the rest works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suduko Grid For Solving From Already Generated Grid Function
    By Nick Krause in forum C Programming
    Replies: 21
    Last Post: 11-17-2014, 03:16 PM
  2. 2d grid
    By lord in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2009, 08:06 PM
  3. grid
    By xlnk in forum Windows Programming
    Replies: 3
    Last Post: 12-14-2002, 08:40 PM

Tags for this Thread