Number Challenge program...

This is a discussion on Number Challenge program... within the C++ Programming forums, part of the General Programming Boards category; Code: #include <fstream.h> #include <iomanip.h> #include <iostream.h> #define UNDEFINED 17 /* Format: Rows, columns, possibilities. */ unsigned short int array[16][16][18]; ...

  1. #1
    ER
    Guest

    Unhappy Number Challenge program...

    Code:
    #include <fstream.h>
    #include <iomanip.h>
    #include <iostream.h>
    
    #define UNDEFINED 17
    
    /* Format: Rows, columns, possibilities. */
    unsigned short int array[16][16][18];
    
    bool IsPossibility(unsigned short int, unsigned short int, unsigned short int);
    void Load();
    void CheckPossibility();
    void DisplayChallenge();
    void Step();
    
    int main()
    {
    	char selection;
    		
    	Load();
    	do
    	{
    		cout << "? ";
    		cin >> selection;
    
    		switch (selection)
    		{
    			case 'r' : Step();
    					   break;
    			case 'c' : Load();
    					   break;
    			case 'd' : DisplayChallenge();
    					   break;
    			case 'p' : CheckPossibility();
    		}
    	} while (selection != 'e');
    
    	return 0;
    }
    
    bool IsPossibility(unsigned short int column, unsigned short int row, unsigned short int test)
    {	
    	for (unsigned short int i = 4 * (column / 4); i < 4 * (column / 4) + 4; ++i)
    		for (unsigned short int j = 4 * (row / 4); j < 4 * (row / 4) + 4; ++j)
    			if (array[j][i][0] == test)
    				return false;
    
    	if (column == row)
    		for (unsigned short int k = 0; k <= 15; ++k)
    			if (array[k][k][0] == test)
    				return false;
    	else if (column == 15 - row)
    		for (unsigned short int k = 0; k <= 15; ++k)
    			if (array[k][15 - k][0] == test)
    				return false;
    
    	for (unsigned short int l = 0; l <= 16; ++l)
    		if (array[l][column][0] == test || array[row][l][0] == test)
    			return false;
    
    	return true;
    }
    
    void CheckPossibility()
    {
    	char letter;
    	unsigned short int number;
    	
    	cin >> letter >> number;
    	letter -= 'A'; --number;
    	if (array[number][letter][0] != UNDEFINED)
    		cout << setw(4) << array[number][letter][0];
    	else
    		for (unsigned short int i = 1; i <= 16; ++i)
    			if (IsPossibility(letter, number, i))
    				cout << setw(4) << i;
    	cout << endl;
    }
    
    void DisplayChallenge()
    {
    	cout << setw(4) << " ";
    	for (unsigned short int i = 0; i <= 16; ++i)
    	{
    		for (unsigned short int j = 0; j <= 16; ++j)
    			if (i == 0) cout << setw(4) << char(j + 65);
    			else if (j == 0) cout << setw(4) << i;
    			else
    				if (array[i - 1][j - 1][0] == UNDEFINED) cout << setw(4) << " ";
    				else cout << setw(4) << array[i - 1][j - 1][0];
    		cout << endl;
    	}
    }
    
    void Load()
    {
    	ifstream fin("Puzzle.txt");
    
    	for (int i = 0; i <= 15; ++i)
    		for (int j = 0; j <= 15; ++j)
    			fin >> array[i][j][0];
    		
    	fin.close();
    }
    
    void Step()
    {	
    	for (int i = 0; i <= 15; ++i)
    		for (int j = 0; j <= 15; ++j)
    			if (array[i][j][0] == UNDEFINED)
    			{
    				int numFound = 0;
    				for (int k = 0; k <= 16; ++k)
    					if (IsPossibility(j,i,k))
    					{
    						array[i][j][k + 1] = 1;
    						++numFound;
    					}
    				if (numFound == 1)
    					for (int l = 0; l <= 16; l++)
    						if (array[i][j][l + 1] == 1)
    							array[i][j][0] = l;
    			}
    }
    Code:
    In 'Puzzle.txt':
    17 17 17 13 17 4 12 17 6 17 3 17 17 7 17 16
    8 17 17 7 17 16 17 11 17 9 17 17 17 10 4 6
    1 5 17 17 7 6 17 17 13 17 12 17 3 9 17 17
    17 12 4 17 17 17 15 17 17 17 17 16 1 17 2 14
    10 13 3 17 17 1 17 14 9 7 17 17 5 17 17 17
    17 4 17 17 5 17 13 17 17 17 17 10 15 17 1 11
    2 17 8 1 17 3 17 17 14 17 17 12 17 6 17 13
    5 6 16 15 9 11 10 12 2 8 13 1 4 14 3 7
    9 17 7 17 6 17 17 13 5 17 11 17 17 4 15 17
    17 1 17 17 17 2 17 15 10 17 17 6 17 17 7 9
    17 17 11 14 1 17 17 16 12 17 17 8 2 17 17 17
    16 17 17 5 17 17 11 17 17 2 15 17 13 17 17 12
    17 9 17 17 12 15 17 17 11 16 17 17 17 17 14 17
    11 17 17 10 17 17 4 3 15 17 1 17 17 17 8 5
    17 17 5 4 2 17 17 6 17 17 17 3 11 16 17 17
    3 17 2 17 17 17 8 17 17 5 14 17 7 15 17 1
    The idea is to make a program that will solve for the remaining values (the 17s) with numbers 1 - 16. Each 4x4 square of numbers has each number appearing in it. Each column and row also has all 16 numbers appearing in it. The full diagonals, likewise, have 1-16 taking up all of the room with no repeats.

    Any ideas on what I may be doing to screw this up? I have a feeling its something stupid.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    snip
    Code:
    bool IsPossibility(unsigned short int column, unsigned short int row, unsigned short int test)
    {	
    	for (unsigned short int i = 4 * (column / 4); i < 4 * (column / 4) + 4; ++i)
    		for (unsigned short int j = 4 * (row / 4); j < 4 * (row / 4) + 4; ++j)
    			if (array[j][0] == test)
    				return false;
    hmmm look at the if statement
    array has 3 indexes, not 2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 09:28 AM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  5. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 04:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21