Thread: Formatting Issues

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105

    Formatting Issues

    Hello there, here comes another issue regarding slidding puzzle xD.
    Now it has to do with the board format. The thing is, it becomes messy when you swap a 2 digit number with a 1 digit number.
    I tried using setw and some other stuff that Alex talked about, but I don't really know what should I do.

    Insert 11 when you run the program, so you can understand better what I am talking about.

    Code:
    #include <iostream>
    #include <algorithm>
    #include <ctime>
    #include <conio.h>
    
    
    using namespace std;
    
    
    void PuzzleBoard4x4(int array[])
    {
    	std::cout<<"  " <<array[0] <<"  |  " <<array[1] <<"  |  " <<array[2] <<"  |  " <<array[3] <<std::endl;
    	std::cout<<"-----+-----+-----+-----" <<std::endl;
    	std::cout<<"  " <<array[4] <<"  |  " <<array[5] <<"  |  " <<array[6] <<"  |  " <<array[7] <<std::endl;
    	std::cout<<"-----+-----+-----+-----" <<std::endl;
    	std::cout<<"  " <<array[8] <<"  |  " <<array[9] <<" |  " <<array[10] <<" |  " <<array[11] <<std::endl;
    	std::cout<<"-----+-----+-----+-----" <<std::endl;
    	std::cout<<"  " <<array[12] <<" |  " <<array[13] <<" |  " <<array[14] <<" |  " <<array[15] <<std::endl <<std::endl;
    }
    
    
    bool EndGameVerif(int array[], int ArraySize)
    {
    	int counter = 0;
    
    
    	for (int i = 0; i < (ArraySize - 1); i++) 
    	{
    		if (array[i] == (i + 1) && array[ArraySize - 1] == 0) {
    			counter = counter + 1;
    		}
    		else {
    			counter = counter;
    		}
    	}
    
    
    	if (counter == (ArraySize - 1)) {
    		return true;
    	}
    }
    
    
    void MoveValidation(int Move, int array[], int position, int BoardConfig, int arraySIZE, int Valid)
    {
    
    
    	if (Move == array[position])
    	{
    		if ((position - 1) >= 0 && array[position - 1] == 0) {
    			Valid = true;
    			std::swap(array[position], array[position -1]);
    		}
    		else if ((position - BoardConfig) > 0 && array[position - BoardConfig] == 0) {
    			Valid = true;
    			std::swap(array[position], array[position - BoardConfig]);
    		}
    		else if ((position + BoardConfig) < arraySIZE && array[position + BoardConfig]  == 0) {
    			Valid = true;
    			std::swap(array[position], array[position + BoardConfig]);
    		}
    		else if ((position + 1) < arraySIZE && array[position + 1] == 0) {
    			Valid = true;
    			std::swap(array[position], array[position + 1]);
    		}
    		else {
    			Valid = false;
    			std::cout<<"Invalid input, try again: ";
    		}
    	}
    }
    
    
    //Validates a move for the pieces on the right of the board except for the last one
    void MoveValidation2(int Move, int array[], int position, int BoardConfig, int arraySIZE, int Valid)
    {
    	if (Move == array[position])
    	{
    		if ((position - BoardConfig) >= 0 && array[position - BoardConfig] == 0)
    		{
    			Valid = true;
    			std::swap(array[position], array[position - BoardConfig]);
    		}
    		else if (array[position - 1] == 0)
    		{
    			Valid = true;
    			std::swap(array[position], array[position - 1]);
    		}
    		else if (array[position + BoardConfig] == 0)
    		{
    			Valid = true;
    			std::swap(array[position], array[position + BoardConfig]);
    		}
    		else
    		{
    			Valid = false;
    			std::cout<<"Invalid input, try again: ";
    		}
    	}
    }
    
    
    //Validates a move for the pieces on the left of the board except for the first one
    void MoveValidation3(int Move, int array[], int position, int BoardConfig, int arraySIZE, int Valid)
    {
    	if (Move == array[position])
    	{
    		if (array[position - BoardConfig] == 0)
    		{
    			Valid = true;
    			std::swap(array[position], array[position - BoardConfig]);
    		}
    		else if (array[position + 1] == 0)
    		{
    			Valid = true;
    			std::swap(array[position], array[position + 1]);
    		}
    		else if ((position + BoardConfig) < arraySIZE && array[position + BoardConfig] == 0)
    		{
    			Valid = true;
    			std::swap(array[position], array[position + BoardConfig]);
    		}
    		else
    		{
    			Valid = false;
    			std::cout<<"Invalid input, try again: ";
    		}
    	}
    }
    
    
    //Swaps the piece selected by the player with 0. Different approach on the position of the piece in order to avoid buffer overruns (eg:array[-1]) 
    void MakesMove(int arraySIZE, int Move, int array[], int BoardConfig, int Val2)
    {
    	for (int i = 0; i < arraySIZE; i++)
    	{
    		if (Move == array[i])
    		{
    			if ((i == (BoardConfig - 1) ) || (i == ((2 * BoardConfig) - 1)) || (i == ((3 * BoardConfig) - 1)) || (i == ((4 * BoardConfig) - 1)))
    			{
    				MoveValidation2(Move, array, i, BoardConfig, arraySIZE, Val2);
    				break;
    			}
    			else if ((i == BoardConfig) || (i == (2 * BoardConfig)) || (i == (3 * BoardConfig)) || (i == (4 * BoardConfig)))
    			{
    				MoveValidation3(Move, array, i, BoardConfig, arraySIZE, Val2);
    				break;
    			}
    			else
    			{
    				MoveValidation(Move, array, i, BoardConfig, arraySIZE, Val2);
    				break;
    			}
    		}
    	}
    }
    int main()
    {
    Restart:
    
    
    	int GameBoard4x4[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 13, 14, 15, 12};
    
    
    	//Starts timing
    	clock_t begin2 = clock();
    
    
    	//Main loop for the 4x4 slide puzzle
    	while (true)
    	{
    		bool Val2 = false;
    
    
    		//Initial board
    		std::cout<<std::endl <<std::endl <<std::endl;
    		PuzzleBoard4x4(GameBoard4x4);
    
    
    		//Prompts the player for a move
    		int Play4;
    		std::cin >> Play4;
    
    
    		//Validates a move
    		MakesMove(16, Play4, GameBoard4x4, 4, Val2);
    
    
    		//End Game Verification
    		if (EndGameVerif(GameBoard4x4, 16) == true)
    		{
    			break;
    		}
    
    
    		system("CLS");
    	}
    
    
    	system("CLS");
    
    
    	//Obtains the total amount of seconds taken
    	clock_t end2 = clock();
    	double elapsed_secs2 = double(end2 - begin2) / CLOCKS_PER_SEC;
    
    
    	//Displays the final winning board
    	PuzzleBoard4x4(GameBoard4x4);
    
    
    	std::cout<<"\n\nYou win!";
    
    
    	std::cout<< std::endl <<std::endl;
    	std::cout<< "You took " <<elapsed_secs2 <<" seconds to solve the puzzle." <<std::endl <<std::endl;
    
    
    	//Play again option
    	std::cout<<"Play again? (y/n): ";
    
    
    	bool ValidOption4 = false;
    
    
    	while (ValidOption4 != true)
    	{
    
    
    		char Again2 = ' ';
    		Again2 = _getch();
    
    
    		if (Again2 == 'y')
    		{
    			ValidOption4 = true;
    			goto Restart;
    		}
    		else if (Again2 == 'Y')
    		{
    			ValidOption4 = true;
    			goto Restart;
    		}
    		else if (Again2 == 'n')
    
    
    		{
    			ValidOption4 = true;
    			exit(0);
    		}
    		else if (Again2 == 'N')
    		{
    			ValidOption4 = true;
    			exit(0);
    		}
    		else
    		{
    			ValidOption4 = false;
    			std::cout<<"Invalid input, try again: ";
    		}
    	}
    
    
    
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I'd write a function that receives an int ( the value in the board array ) and returns a string. This string could be made of some leading spacess the number and then I would append a number of spaces depending on the value ( if the value is > 9 one space less ).
    Kurt

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Yeah I looked up some info and that seems to be the best solution. I did some code just so I could try it out. I don't know how should I append the space though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. formatting
    By yottaVolt in forum C Programming
    Replies: 3
    Last Post: 01-24-2009, 02:55 AM
  2. Formatting in c++
    By mmiskand in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2008, 02:17 AM
  3. O/I formatting
    By Downbound in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2004, 05:43 AM
  4. formatting
    By dP munky in forum Tech Board
    Replies: 5
    Last Post: 12-07-2002, 04:05 PM
  5. Formatting
    By MethodMan in forum Tech Board
    Replies: 6
    Last Post: 10-19-2002, 09:38 PM