C++ Help.

thevil1

New Member
OK so I have a new question. Yes I'm new to this forum, so forgive me if this is the wrong place. I plan to stick around as well since this seems like a very useful place. But anyway, Unless you know a bit about programming you probably wont be able to help, But you can still say what you think.
I'm trying to make a console program of the math game Suduku, in C++. I have the algorithm wrote out, but then syntax is a problem for me. This is what I have so far:
Code:
//By: Thevil1
#include <iostream>                                                     //line 1
using namespace std;                                                    //line 2
int main()                                                              //line 3

{                                                                       //line 4
    int aa, ab, ac, ad, ae, af, ag, ah, ai,                             //line 5
        ba, bb, bc, bd, be, bf, bg, bh, bi,                             //line 6
        ca, cb, cc, cd, ce, cf, cg, ch, ci,                             //line 7
        da, db, dc, dd, de, df, dg, dh, di,                             //line 8
        ea, eb, ec, ed, ee, ef, eg, eh, ei,                             //line 9
        fa, fb, fc, fd, fe, ff, fg, fh, fi,                             //line 10
        ga, gb, gc, gd, ge, gf, gg, gh, gi,                             //line 11
        ha, hb, hc, hd, he, hf, hg, hh, hi,                             //line 12
        ia, ib, ic, id, ie, iF, ig, ih, ii;                             //line 13
ab = 3; ad = 1; ae = 7; ai = 4;  bd = 9; bf = 8; bg = 6; cc = 7; ce = 2;//line 14
ch = 8; ci = 5; dd = 7; dg = 8; di = 2; ea = 5; ec = 8; ed = 6; ef = 2; //line 15
eg = 7; ei = 1; fa = 1; fc = 2; ff = 5; ga = 7; gb = 8; ge = 4; gg = 3; //line 16
hc = 3; hd = 2; hf = 1; ia = 4; ie = 6; iF = 7; ih = 1;                 //line 17


  
    //board game layout
    cout << "|_1___2___3_|_4___5___6|__7___8___9_|  \n";                //line 18
    cout << "|   |   |   |   |   |   |   |   |   |  \n";                //line 19
    cout << "|___|___|___|___|___|___|___|___|___|a \n";                //line 20
    cout << "|   |   |   |   |   |   |   |   |   |  \n";                //line 21
    cout << "|___|___|___|___|___|___|___|___|___|b \n";                //line 22
    cout << "|   |   |   |   |   |   |   |   |   |  \n";                //line 23
    cout << "|___|___|___|___|___|___|___|___|___|c_\n";                //line 24
    cout << "|   |   |   |   |   |   |   |   |   |  \n";                //line 25
    cout << "|___|___|___|___|___|___|___|___|___|d \n";                //line 26
    cout << "|   |   |   |   |   |   |   |   |   |  \n";                //line 27
    cout << "|___|___|___|___|___|___|___|___|___|e \n";                //line 28
    cout << "|   |   |   |   |   |   |   |   |   |  \n";                //line 29
    cout << "|___|___|___|___|___|___|___|___|___|f_\n";                //line 30
    cout << "|   |   |   |   |   |   |   |   |   |  \n";                //line 31
    cout << "|___|___|___|___|___|___|___|___|___|g \n";                //line 32
    cout << "|   |   |   |   |   |   |   |   |   |  \n";                //line 33
    cout << "|___|___|___|___|___|___|___|___|___|h \n";                //line 34
    cout << "|   |   |   |   |   |   |   |   |   |  \n";                //line 35
    cout << "|___|___|___|___|___|___|___|___|___|i_\n";                //line 36
    cout << endl;                                                       
    
       
system("pause");                                                        //line 37                                  
return 0;                                                               //line 38                         
}                                                                       //line 39
I copied a puzzle from websudoku.com. My problem is How do I get the pre-defined integers (lines 14-17) into their correct place on the bored, an the one that are user defined into their correct places. Then I will just use one big 'if' statement to sum it all up.

NOTE: the board layout goes as follows:
'aa' goes in 1a, 'ab' goes in 2a etc... I tried to make it as simple as I could.
PLEASE HELP! :) Thank You! :) And again I hope to stick around.
 
Last edited:

Cromewell

Administrator
Staff member
I don't have a compiler here to test this and make sure it works, it should only take a little tweaking if there are any errors. You're probably better off trying to tackle it with a 2 dimensional array. It would look something like this:
Code:
//By: Thevil1
#include <iostream>
#define BOX_SIZE 9


using namespace std;
int main(){
	int boxes[BOX_SIZE][BOX_SIZE];

	for( i = 0; i < BOX_SIZE; i++) //set all elements to -1 to indicate they have no value set
		for( j = 0; j < BOX_SIZE; j++)
			boxes[i][j] = -1;


	/*
	array is 0 indexed - letter code:
	a 0
	b 1
	c 2
	d 3
	e 4
	f 5
	g 6
	h 7
	i 8
	*/
	boxes[0][1] = 3; //ideally you would write some initialize code here (or a function) to generate new games for you
	boxes[0][3] = 1;
	boxes[0][4] = 7;
	boxes[0][8] = 4;
	boxes[1][3] = 9;
	boxes[1][5] = 8;
	boxes[1][6] = 6;
	boxes[2][2] = 7;
	boxes[2][4] = 2;
	boxes[2][7] = 8;
	boxes[2][8] = 5;
	boxes[3][3] = 7;
	boxes[3][6] = 8;
	boxes[3][8] = 2;
	boxes[4][0] = 5;
	boxes[4][2] = 8;
	boxes[4][3] = 6;
	boxes[4][5] = 2;
	boxes[4][6] = 7;
	boxes[4][8] = 1;
	boxes[5][0] = 1;
	boxes[5][2] = 2;
	boxes[5][5] = 5;
	boxes[6][0] = 7;
	boxes[6][1] = 8;
	boxes[6][4] = 4;
	boxes[6][6] = 3;
	boxes[7][2] = 3;
	boxes[7][3] = 2;
	boxes[7][5] = 1;
	boxes[8][0] = 4;
	boxes[8][4] = 6;
	boxes[8][5] = 7;
	boxes[8][7] = 1;
  

	//board game layout
	//draw the first line - this should be it's own draw function that you call when you want to redraw the board
	cout << "|_1___2___3_|_4___5___6|  7   8   9 |" << endl;
	cout << "|---+---+---|---+---+---|---+---+---|" << endl;

	for (int x=0; x<BOX_SIZE; x++){
		cout << "|";
		for (int y=0; y<BOX_SIZE; y++){
			cout << " " << (boxes[x][y] == -1)  ? "_" : boxes[x][y]  <<" |";
		}
		count << x << endl; // print out the row number
		cout << "|---|---|---|---|---|---|---|---|---|" << endl; // print the grid lines
		
	}
    
       
	system("pause");
	return 0;
}
 
Last edited:
Top