Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,420 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,992 people online right now. Registration is fast and FREE... Join Now!




Sudoku game just one problem and it's without arrays

 
Reply to this topicStart new topic

Sudoku game just one problem and it's without arrays

inferi
11 Jan, 2008 - 09:51 AM
Post #1

New D.I.C Head
*

Joined: 30 Nov, 2007
Posts: 7


My Contributions
hi,
I am writing the Sudoku game code and it is just from 1 to 3 (NOT to 9 like the ususal one) when i did right it give me the win message but when i did it wrong it also give me the win message, the first chart is to tell you the places and the second id the one to fill (you will see it after compile) and after finishing you have to enter the number -1 to see if you win or lose, here is the code.AND I CAN NOT USE ARRAYS

CODE
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<cstdlib>
#include<ctime>



int main ()
{

char x1=' ';
char x2=' ';
char x3=' ';

char y1=' ';
char y2=' ';
char y3=' ';

char z1=' ';
char z2=' ';
char z3=' ';
int face;
srand( time(0) );
face =1+rand() %15;
switch (face)
{
case 1:

x1='3';
x3='1';
y2='2';

break;

case 2:
x1='2';
y2='3';
z3='1';
break;


case 3:
x2='1';
y1='2';
z2='3';

break;

case 4:
x2='2';
y3='1';
z1='3';
break;

case 5:
x3='3';
y2='2';
z2='1';
break;
case 6:
x2='2';
y2='3';
z1='1';
break;
case 7:
x2='2';
y2='1';
z3='3';
break;
case 8:
x3='1';
y2='2';
z3='3';
break;
case 9:
x2='3';
y2='2';
y3='1';
break;
case 10:
x1='1';
y2='2';
z2='3';
break;
case 11:
x2='2';
x3='1';
z2='3';
break;
case 12:
x3='3';
y1='1';
z1='2';
break;
case 13:
x2='1';
y1='3';
y2='2';
break;
case 14:
y1='1';
y2='2';
y3='3';
break;
case 15:
z1='3';
z2='2';
z3='1';
break;
}
/*
cout<<endl;
cout<<1<<"_|_"<<2<<"_|_"<<3<<endl;
cout<<4<<"_|_"<<5<<"_|_"<<6<<endl;
cout<<7<<" | "<<8<<" | "<<9<<endl;
cout<<"\n\n";
cout<<x1<<"_|_"<<x2<<"_|_"<<x3<<endl;
cout<<y1<<"_|_"<<y2<<"_|_"<<y3<<endl;
cout<<z1<<" | "<<z2<<" | "<<z3<<endl;
cout<<endl;
*/

int q,w;
while (true)
{
lb:
system("cls");
cout<<"Cells Numbers"<<endl;
cout<<1<<"_|_"<<2<<"_|_"<<3<<endl;
cout<<4<<"_|_"<<5<<"_|_"<<6<<endl;
cout<<7<<" | "<<8<<" | "<<9<<endl;
cout<<"\n\nGrid Status\n";
cout<<x1<<"_|_"<<x2<<"_|_"<<x3<<endl;
cout<<y1<<"_|_"<<y2<<"_|_"<<y3<<endl;
cout<<z1<<" | "<<z2<<" | "<<z3<<endl;
cout<<endl;


cout<<" enter the cell number or -1 to exist: ";
cin>>q;
//
if (q == -1) break;

cout<<"\n enter the a value: ";
cin>>w;
switch(q)
{

case 1:
    if (x1 ==' ')
    switch (w)
    {
        case 1: x1='1';break;
        case 2: x1='2';break;
        case 3: x1='3';break;
        case 4: x1='4';break;
        case 5: x1='5';break;
        case 6: x1='6';break;
        case 7: x1='7';break;
        case 8: x1='8';break;
        case 9: x1='9';
    }
    else
    {
        cout<<"play again";
        // skip the remain and strat anoth loop itertation
        continue;
        goto lb;
    }
break;

case 2:
    if (x2==' ')
    switch (w)
    {
        case 1: x2='1';break;
        case 2: x2='2';break;
        case 3: x2='3';break;
        case 4: x2='4';break;
        case 5: x2='5';break;
        case 6: x2='6';break;
        case 7: x2='7';break;
        case 8: x2='8';break;
        case 9: x2='9';
    }
    else
    {
        cout<<"play again";
        continue;
        goto lb;
    }
break;


case 3:
    if (x3==' ')
    switch (w)
    {
        case 1: x3='1';break;
        case 2: x3='2';break;
        case 3: x3='3';break;
        case 4: x3='4';break;
        case 5: x3='5';break;
        case 6: x3='6';break;
        case 7: x3='7';break;
        case 8: x3='8';break;
        case 9: x3='9';
    }
    else
    {
        cout<<"play again";
        continue;
        goto lb;
    }
break;


case 4:
    if (y1==' ')
    switch (w)
    {
        case 1: y1='1';break;
        case 2: y1='2';break;
        case 3: y1='3';break;
        case 4: y1='4';break;
        case 5: y1='5';break;
        case 6: y1='6';break;
        case 7: y1='7';break;
        case 8: y1='8';break;
        case 9: y1='9';
    }
    else
    {
        cout<<"play again";
        continue;
        goto lb;
    }
    break;

case 5:
if (y2==' ')
    switch (w)
    {
        case 1: y2='1';break;
        case 2: y2='2';break;
        case 3: y2='3';break;
        case 4: y2='4';break;
        case 5: y2='5';break;
        case 6: y2='6';break;
        case 7: y2='7';break;
        case 8: y2='8';break;
        case 9: y2='9';
    }
    else
    {
    cout<<"play again";
    continue;
    goto lb;
    }
    break;


case 6:
if (y3==' ')
    switch (w)
    {
        case 1: y3='1';break;
        case 2: y3='2';break;
        case 3: y3='3';break;
        case 4: y3='4';break;
        case 5: y3='5';break;
        case 6: y3='6';break;
        case 7: y3='7';break;
        case 8: y3='8';break;
        case 9: y3='9';
    }
    else
    {
        cout<<"play again";
        continue;
        goto lb;
    }
break;


case 7:
if (z1==' ')
    switch (w)
    {
        case 1: z1='1';break;
        case 2: z1='2';break;
        case 3: z1='3';break;    
        case 4: z1='4';break;
        case 5: z1='5';break;
        case 6: z1='6';break;
        case 7: z1='7';break;
        case 8: z1='8';break;
        case 9: z1='9';
    }
    else
    {
        cout<<"play again";
        continue;
        goto lb;
    }
break;

case 8:
if (z2==' ')
switch (w)
    {
        case 1: z2='1';break;
        case 2: z2='2';break;
        case 3: z2='3';break;
        case 4: z2='4';break;
        case 5: z2='5';break;
        case 6: z2='6';break;
        case 7: z2='7';break;
        case 8: z2='8';break;
        case 9: z2='9';
    }
    else
    {
        cout<<"play again";
        continue;
        goto lb;
    }
break;

case 9:
if (z3==' ')
switch (w)
    {
        case 1: z3='1';break;
        case 2: z3='2';break;
        case 3: z3='3';break;
        case 4: z3='4';break;
        case 5: z3='5';break;
        case 6: z3='6';break;
        case 7: z3='7';break;
        case 8: z3='8';break;
        case 9: z3='9';
    }
    else
    {
        cout<<"play again";
        continue;
        goto lb;
    }
break;

default:
    continue;
    goto lb;
}
system("cls");
cout<<endl;
cout<<"Cells Numbers"<<endl;
cout<<1<<"_|_"<<2<<"_|_"<<3<<endl;
cout<<4<<"_|_"<<5<<"_|_"<<6<<endl;
cout<<7<<" | "<<8<<" | "<<9<<endl;
cout<<"\n\n Grid Status";
cout<<x1<<"_|_"<<x2<<"_|_"<<x3<<endl;
cout<<y1<<"_|_"<<y2<<"_|_"<<y3<<endl;
cout<<z1<<" | "<<z2<<" | "<<z3<<endl;
cout<<endl;

}


if((x1=='1')&&(x2=='2')&&(x3=='3')
||(x1=='2')&&(x2=='1')&&(x3=='3')
||(x1=='2')&&(x2=='3')&&(x3=='1')
||(x1=='1')&&(x2=='3')&&(x3=='2')
||(x1=='3')&&(x2=='2')&&(x3=='1')
||(x1=='3')&&(x2=='1')&&(x3=='2')

||(y1=='1')&&(y2=='2')&&(y3=='3')
||(y1=='2')&&(y2=='1')&&(y3=='3')
||(y1=='2')&&(y2=='3')&&(y3=='1')
||(y1=='1')&&(y2=='3')&&(y3=='2')
||(y1=='3')&&(y2=='2')&&(y3=='1')
||(y1=='3')&&(y2=='1')&&(y3=='2')

||(z1=='1')&&(z2=='2')&&(z3=='3')
||(z1=='2')&&(z2=='1')&&(z3=='3')
||(z1=='2')&&(z2=='3')&&(z3=='1')
||(z1=='1')&&(z2=='3')&&(z3=='2')
||(z1=='3')&&(z2=='2')&&(z3=='1')
||(z1=='3')&&(z2=='1')&&(z3=='2'))

/********************************/

if((x1=='1')&&(x2=='2')&&(x3=='3')
||(x1=='2')&&(x2=='1')&&(x3=='3')
||(x1=='2')&&(x2=='3')&&(x3=='1')
||(x1=='1')&&(x2=='3')&&(x3=='2')
||(x1=='3')&&(x2=='2')&&(x3=='1')
||(x1=='3')&&(x2=='1')&&(x3=='2')

||(y1=='1')&&(y2=='2')&&(y3=='3')
||(y1=='2')&&(y2=='1')&&(y3=='3')
||(y1=='2')&&(y2=='3')&&(y3=='1')
||(y1=='1')&&(y2=='3')&&(y3=='2')
||(y1=='3')&&(y2=='2')&&(y3=='1')
||(y1=='3')&&(y2=='1')&&(y3=='2')

||(z1=='1')&&(z2=='2')&&(z3=='3')
||(z1=='2')&&(z2=='1')&&(z3=='3')
||(z1=='2')&&(z2=='3')&&(z3=='1')
||(z1=='1')&&(z2=='3')&&(z3=='2')
||(z1=='3')&&(z2=='2')&&(z3=='1')
||(z1=='3')&&(z2=='1')&&(z3=='2'))
{

    if ((x1=='1')&&(y1=='2')&&(z1=='3')
      ||(x1=='2')&&(y1=='1')&&(z1=='3')
      ||(x1=='2')&&(y1=='3')&&(z1=='1')
      ||(x1=='1')&&(y1=='3')&&(z1=='2')
      ||(x1=='3')&&(y1=='2')&&(z1=='1')
      ||(x1=='3')&&(y1=='1')&&(z1=='2')
  
      ||(x2=='1')&&(y2=='2')&&(z2=='3')
      ||(x2=='2')&&(y2=='1')&&(z2=='3')
      ||(x2=='2')&&(y2=='3')&&(z2=='1')
      ||(x2=='1')&&(y2=='3')&&(z2=='2')
      ||(x2=='3')&&(y2=='2')&&(z2=='1')
      ||(x2=='3')&&(y2=='1')&&(z2=='2')

      ||(x3=='1')&&(y3=='2')&&(z3=='3')
      ||(x3=='2')&&(y3=='1')&&(z3=='3')
      ||(x3=='2')&&(y3=='3')&&(z3=='1')
      ||(x3=='1')&&(y3=='3')&&(z3=='2')
      ||(x3=='3')&&(y3=='2')&&(z3=='1')
      ||(x3=='3')&&(y3=='1')&&(z3=='2'))
    {
        cout<<"you win"<<endl;
    }
    else if((x1!=' ')&&(x2!=' ')&&(x3!=' ')
            ||(y1!=' ')&&(y2!=' ')&&(y3!=' ')
            ||(z1!=' ')&&(z2!=' ')&&(z3!=' '))
    {
        cout<<"you lose"<<endl;
    }
                
    
}
return 0;
}




anyone can help? thank you.
User is offlineProfile CardPM
+Quote Post

Jingle
RE: Sudoku Game Just One Problem And It's Without Arrays
11 Jan, 2008 - 12:25 PM
Post #2

D.I.C Head
**

Joined: 20 Oct, 2007
Posts: 249


My Contributions
it would realy help if you used functions
if you want I can give you an example.

and please dont use the goto statment

This post has been edited by Jingle: 11 Jan, 2008 - 02:00 PM
User is offlineProfile CardPM
+Quote Post

inferi
RE: Sudoku Game Just One Problem And It's Without Arrays
11 Jan, 2008 - 01:35 PM
Post #3

New D.I.C Head
*

Joined: 30 Nov, 2007
Posts: 7


My Contributions
QUOTE(Jingle @ 11 Jan, 2008 - 01:25 PM) *

it would realy help if you used functions
if you want can example I can post it.

and please dont use the goto statment


i can not use funcations cause i do not how as for the goto what else can i use? and i will be really gratefull if you post the example. but can't you answer the main question please? thank you
User is offlineProfile CardPM
+Quote Post

Jingle
RE: Sudoku Game Just One Problem And It's Without Arrays
11 Jan, 2008 - 02:01 PM
Post #4

D.I.C Head
**

Joined: 20 Oct, 2007
Posts: 249


My Contributions
[/quote]

i can not use funcations cause i do not how as for the goto what else can i use? and i will be really gratefull if you post the example. but can't you answer the main question please? thank you
[/quote]
functions take place of the goto.
i will work on it

This post has been edited by Jingle: 11 Jan, 2008 - 02:01 PM
User is offlineProfile CardPM
+Quote Post

Jingle
RE: Sudoku Game Just One Problem And It's Without Arrays
11 Jan, 2008 - 03:42 PM
Post #5

D.I.C Head
**

Joined: 20 Oct, 2007
Posts: 249


My Contributions
I can help you out but I'm not sure what it is that you want.


This post has been edited by Jingle: 11 Jan, 2008 - 04:32 PM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Sudoku Game Just One Problem And It's Without Arrays
11 Jan, 2008 - 04:35 PM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,047



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
My first thought on seeing this code was, "OMG, who the hell would do it like that!" This was followed immediately with, "but, how could you do it like that..."

Here's an example of your code using functions. I should note that a number of your seed patterns are invalid.

CODE

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<cstdlib>
#include<ctime>

char x1, x2, x3, y1, y2, y3, z1, z2, z3;

bool isFull() {
    return (!(
        (x1==' ')||(x2==' ')||(x3==' ')
        ||(y1==' ')||(y2==' ')||(y3==' ')
        ||(z1==' ')||(z2==' ')||(z3==' ')
    ));

}

void placeNumber(const int w, char &cell) {
    if (cell==' ') {
        switch (w) {
            case 1: cell='1';break;
            case 2: cell='2';break;
            case 3: cell='3';break;
            case 4: cell='4';break;
            case 5: cell='5';break;
            case 6: cell='6';break;
            case 7: cell='7';break;
            case 8: cell='8';break;
            case 9: cell='9';
        }
    } else {
        cout<<"play again";
    }
}

void showStatus() {
    cout<<"Cells Numbers"<<endl;
    cout<<1<<"_|_"<<2<<"_|_"<<3<<endl;
    cout<<4<<"_|_"<<5<<"_|_"<<6<<endl;
    cout<<7<<" | "<<8<<" | "<<9<<endl;
    cout<<"\n\nGrid Status\n";
    cout<<x1<<"_|_"<<x2<<"_|_"<<x3<<endl;
    cout<<y1<<"_|_"<<y2<<"_|_"<<y3<<endl;
    cout<<z1<<" | "<<z2<<" | "<<z3<<endl;
    cout<<endl;
}

void set123(char &cell1,char &cell2,char &cell3) {
    cell1='1'; cell2='2'; cell3='3';
}

bool isValidSet(const char cell1, const char cell2, const char cell3) {
    if ((cell1==' ')||(cell2==' ')||(cell3==' ')) { return false; }
    return ((cell1!=cell2) && (cell1!=cell3) && (cell2!=cell3));
}


int main () {
    x1=x2=x3=y1=y2=y3=z1=z2=z3=' ';
    srand( time(0) );
    int face =1+rand() %15;
    switch (face) {
        case 1: set123(x3,y2,x1); break;
        case 2: set123(z3,x1,y2); break;
        case 3: set123(x2,y1,z2); break;
        case 4: set123(y3,x2,z1); break;
        case 5: set123(z2,y2,x3); break;
        case 6: set123(z1,x2,y2); break;
        case 7: set123(y2,x2,z3); break;
        case 8: set123(x3,y2,z3); break;
        case 9: set123(y3,y2,x2); break;
        case 10: set123(x1,y2,z2); break;
        case 11: set123(x3,x2,z2); break;
        case 12: set123(y1,z1,x3); break;
        case 13: set123(x2,y2,y1); break;
        case 14: set123(y1,y2,y3); break;
        case 15: set123(z3,z2,z1); break;
    }


    while (!isFull()) {
        int q,w;
        showStatus();

        cout<<" enter the cell number or 0 to exit: ";
        cin>>q;
        if (q == 0) break;

        cout<<"\n enter the a value: ";
        cin>>w;
        switch(q) {
            case 1: placeNumber(w, x1); break;
            case 2: placeNumber(w, x2); break;
            case 3: placeNumber(w, x3); break;
            case 4: placeNumber(w, y1); break;
            case 5: placeNumber(w, y2); break;
            case 6: placeNumber(w, y3); break;
            case 7: placeNumber(w, z1); break;
            case 8: placeNumber(w, z2); break;
            case 9: placeNumber(w, z3); break;
        }
    }

    if (isValidSet(x1, x2, x3)&& isValidSet(y1, y2, y3)&& isValidSet(z1, z2, z3)
        && isValidSet(x1, y1, z1)&& isValidSet(x2, y2, z2)&& isValidSet(x3, y3, z3))
    {
        cout<<"you win"<<endl;
    } else {
        cout<<"you lose"<<endl;
    }
    return 0;
}


Hope this makes sense and helps.

User is offlineProfile CardPM
+Quote Post

inferi
RE: Sudoku Game Just One Problem And It's Without Arrays
14 Jan, 2008 - 10:55 AM
Post #7

New D.I.C Head
*

Joined: 30 Nov, 2007
Posts: 7


My Contributions
Thank you baavgai for that code it was really helpfull,I did not know hoe to do C++ with funcations but after i read it from the text it is really easy,thanks again.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:15AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month