473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Sudoku game just one problem and it's without arrays

11
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

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5. #include<cstdlib>
  6. #include<ctime>
  7.  
  8.  
  9.  
  10. int main ()
  11. {
  12.  
  13. char x1=' ';
  14. char x2=' ';
  15. char x3=' ';
  16.  
  17. char y1=' ';
  18. char y2=' ';
  19. char y3=' ';
  20.  
  21. char z1=' ';
  22. char z2=' ';
  23. char z3=' ';
  24. int face;
  25. srand( time(0) );
  26. face =1+rand() %15;
  27. switch (face)
  28. {
  29. case 1:
  30.  
  31. x1='3';
  32. x3='1';
  33. y2='2';
  34.  
  35. break;
  36.  
  37. case 2:
  38. x1='2';
  39. y2='3';
  40. z3='1';
  41. break;
  42.  
  43.  
  44. case 3:
  45. x2='1';
  46. y1='2';
  47. z2='3';
  48.  
  49. break;
  50.  
  51. case 4:
  52. x2='2';
  53. y3='1';
  54. z1='3';
  55. break;
  56.  
  57. case 5:
  58. x3='3';
  59. y2='2';
  60. z2='1';
  61. break;
  62. case 6:
  63. x2='2';
  64. y2='3';
  65. z1='1';
  66. break;
  67. case 7:
  68. x2='2';
  69. y2='1';
  70. z3='3';
  71. break;
  72. case 8:
  73. x3='1';
  74. y2='2';
  75. z3='3';
  76. break;
  77. case 9:
  78. x2='3';
  79. y2='2';
  80. y3='1';
  81. break;
  82. case 10:
  83. x1='1';
  84. y2='2';
  85. z2='3';
  86. break;
  87. case 11:
  88. x2='2';
  89. x3='1';
  90. z2='3';
  91. break;
  92. case 12:
  93. x3='3';
  94. y1='1';
  95. z1='2';
  96. break;
  97. case 13:
  98. x2='1';
  99. y1='3';
  100. y2='2';
  101. break;
  102. case 14:
  103. y1='1';
  104. y2='2';
  105. y3='3';
  106. break;
  107. case 15:
  108. z1='3';
  109. z2='2';
  110. z3='1';
  111. break;
  112. }
  113. /*
  114. cout<<endl;
  115. cout<<1<<"_|_"<<2<<"_|_"<<3<<endl;
  116. cout<<4<<"_|_"<<5<<"_|_"<<6<<endl;
  117. cout<<7<<" | "<<8<<" | "<<9<<endl;
  118. cout<<"\n\n";
  119. cout<<x1<<"_|_"<<x2<<"_|_"<<x3<<endl;
  120. cout<<y1<<"_|_"<<y2<<"_|_"<<y3<<endl;
  121. cout<<z1<<" | "<<z2<<" | "<<z3<<endl;
  122. cout<<endl;
  123. */
  124.  
  125. int q,w;
  126. while (true)
  127. {
  128. lb:
  129. system("cls");
  130. cout<<"Cells Numbers"<<endl;
  131. cout<<1<<"_|_"<<2<<"_|_"<<3<<endl;
  132. cout<<4<<"_|_"<<5<<"_|_"<<6<<endl;
  133. cout<<7<<" | "<<8<<" | "<<9<<endl;
  134. cout<<"\n\nGrid Status\n";
  135. cout<<x1<<"_|_"<<x2<<"_|_"<<x3<<endl;
  136. cout<<y1<<"_|_"<<y2<<"_|_"<<y3<<endl;
  137. cout<<z1<<" | "<<z2<<" | "<<z3<<endl;
  138. cout<<endl;
  139.  
  140.  
  141. cout<<" enter the cell number or -1 to exist: ";
  142. cin>>q;
  143. //
  144. if (q == -1) break;
  145.  
  146. cout<<"\n enter the a value: ";
  147. cin>>w;
  148. switch(q)
  149. {
  150.  
  151. case 1:
  152.     if (x1 ==' ')
  153.     switch (w)
  154.     {
  155.         case 1: x1='1';break;
  156.         case 2: x1='2';break;
  157.         case 3: x1='3';break;
  158.         case 4: x1='4';break;
  159.         case 5: x1='5';break;
  160.         case 6: x1='6';break;
  161.         case 7: x1='7';break;
  162.         case 8: x1='8';break;
  163.         case 9: x1='9';
  164.     }
  165.     else
  166.     { 
  167.         cout<<"play again";
  168.         // skip the remain and strat anoth loop itertation
  169.         continue;
  170.         goto lb;
  171.     }
  172. break;
  173.  
  174. case 2:
  175.     if (x2==' ')
  176.     switch (w)
  177.     {
  178.         case 1: x2='1';break;
  179.         case 2: x2='2';break;
  180.         case 3: x2='3';break;
  181.         case 4: x2='4';break;
  182.         case 5: x2='5';break;
  183.         case 6: x2='6';break;
  184.         case 7: x2='7';break;
  185.         case 8: x2='8';break;
  186.         case 9: x2='9';
  187.     }
  188.     else
  189.     { 
  190.         cout<<"play again";
  191.         continue;
  192.         goto lb;
  193.     }
  194. break;
  195.  
  196.  
  197. case 3:
  198.     if (x3==' ')
  199.     switch (w)
  200.     {
  201.         case 1: x3='1';break;
  202.         case 2: x3='2';break;
  203.         case 3: x3='3';break;
  204.         case 4: x3='4';break;
  205.         case 5: x3='5';break;
  206.         case 6: x3='6';break;
  207.         case 7: x3='7';break;
  208.         case 8: x3='8';break;
  209.         case 9: x3='9';
  210.     }
  211.     else
  212.     {
  213.         cout<<"play again";
  214.         continue;
  215.         goto lb;
  216.     }
  217. break;
  218.  
  219.  
  220. case 4:
  221.     if (y1==' ')
  222.     switch (w)
  223.     {
  224.         case 1: y1='1';break;
  225.         case 2: y1='2';break;
  226.         case 3: y1='3';break;
  227.         case 4: y1='4';break;
  228.         case 5: y1='5';break;
  229.         case 6: y1='6';break;
  230.         case 7: y1='7';break;
  231.         case 8: y1='8';break;
  232.         case 9: y1='9';
  233.     }
  234.     else
  235.     {
  236.         cout<<"play again";
  237.         continue;
  238.         goto lb;
  239.     } 
  240.     break;
  241.  
  242. case 5:
  243. if (y2==' ')
  244.     switch (w)
  245.     {
  246.         case 1: y2='1';break;
  247.         case 2: y2='2';break;
  248.         case 3: y2='3';break;
  249.         case 4: y2='4';break;
  250.         case 5: y2='5';break;
  251.         case 6: y2='6';break;
  252.         case 7: y2='7';break;
  253.         case 8: y2='8';break;
  254.         case 9: y2='9';
  255.     }
  256.     else
  257.     {
  258.     cout<<"play again";
  259.     continue;
  260.     goto lb;
  261.     } 
  262.     break;
  263.  
  264.  
  265. case 6:
  266. if (y3==' ')
  267.     switch (w)
  268.     {
  269.         case 1: y3='1';break;
  270.         case 2: y3='2';break;
  271.         case 3: y3='3';break;
  272.         case 4: y3='4';break;
  273.         case 5: y3='5';break;
  274.         case 6: y3='6';break;
  275.         case 7: y3='7';break;
  276.         case 8: y3='8';break;
  277.         case 9: y3='9';
  278.     }
  279.     else
  280.     {
  281.         cout<<"play again";
  282.         continue;
  283.         goto lb;
  284.     } 
  285. break;
  286.  
  287.  
  288. case 7:
  289. if (z1==' ')
  290.     switch (w)
  291.     {
  292.         case 1: z1='1';break;
  293.         case 2: z1='2';break;
  294.         case 3: z1='3';break;    
  295.         case 4: z1='4';break;
  296.         case 5: z1='5';break;
  297.         case 6: z1='6';break;
  298.         case 7: z1='7';break;
  299.         case 8: z1='8';break;
  300.         case 9: z1='9';
  301.     }
  302.     else
  303.     {
  304.         cout<<"play again";
  305.         continue;
  306.         goto lb;
  307.     } 
  308. break;
  309.  
  310. case 8:
  311. if (z2==' ')
  312. switch (w)
  313.     {
  314.         case 1: z2='1';break;
  315.         case 2: z2='2';break;
  316.         case 3: z2='3';break;
  317.         case 4: z2='4';break;
  318.         case 5: z2='5';break;
  319.         case 6: z2='6';break;
  320.         case 7: z2='7';break;
  321.         case 8: z2='8';break;
  322.         case 9: z2='9';
  323.     }
  324.     else
  325.     {
  326.         cout<<"play again";
  327.         continue;
  328.         goto lb;
  329.     } 
  330. break;
  331.  
  332. case 9:
  333. if (z3==' ')
  334. switch (w)
  335.     {
  336.         case 1: z3='1';break;
  337.         case 2: z3='2';break;
  338.         case 3: z3='3';break;
  339.         case 4: z3='4';break;
  340.         case 5: z3='5';break;
  341.         case 6: z3='6';break;
  342.         case 7: z3='7';break;
  343.         case 8: z3='8';break;
  344.         case 9: z3='9';
  345.     }
  346.     else
  347.     {
  348.         cout<<"play again";
  349.         continue;
  350.         goto lb;
  351.     } 
  352. break;
  353.  
  354. default:
  355.     continue;
  356.     goto lb;
  357. }
  358. system("cls");
  359. cout<<endl;
  360. cout<<"Cells Numbers"<<endl;
  361. cout<<1<<"_|_"<<2<<"_|_"<<3<<endl;
  362. cout<<4<<"_|_"<<5<<"_|_"<<6<<endl;
  363. cout<<7<<" | "<<8<<" | "<<9<<endl;
  364. cout<<"\n\n Grid Status";
  365. cout<<x1<<"_|_"<<x2<<"_|_"<<x3<<endl;
  366. cout<<y1<<"_|_"<<y2<<"_|_"<<y3<<endl;
  367. cout<<z1<<" | "<<z2<<" | "<<z3<<endl;
  368. cout<<endl;
  369.  
  370. }
  371.  
  372.  
  373. if((x1=='1')&&(x2=='2')&&(x3=='3')
  374. ||(x1=='2')&&(x2=='1')&&(x3=='3')
  375. ||(x1=='2')&&(x2=='3')&&(x3=='1')
  376. ||(x1=='1')&&(x2=='3')&&(x3=='2')
  377. ||(x1=='3')&&(x2=='2')&&(x3=='1')
  378. ||(x1=='3')&&(x2=='1')&&(x3=='2')
  379.  
  380. ||(y1=='1')&&(y2=='2')&&(y3=='3')
  381. ||(y1=='2')&&(y2=='1')&&(y3=='3')
  382. ||(y1=='2')&&(y2=='3')&&(y3=='1')
  383. ||(y1=='1')&&(y2=='3')&&(y3=='2')
  384. ||(y1=='3')&&(y2=='2')&&(y3=='1')
  385. ||(y1=='3')&&(y2=='1')&&(y3=='2')
  386.  
  387. ||(z1=='1')&&(z2=='2')&&(z3=='3')
  388. ||(z1=='2')&&(z2=='1')&&(z3=='3')
  389. ||(z1=='2')&&(z2=='3')&&(z3=='1')
  390. ||(z1=='1')&&(z2=='3')&&(z3=='2')
  391. ||(z1=='3')&&(z2=='2')&&(z3=='1')
  392. ||(z1=='3')&&(z2=='1')&&(z3=='2'))
  393.  
  394. /********************************/
  395.  
  396. if((x1=='1')&&(x2=='2')&&(x3=='3')
  397. ||(x1=='2')&&(x2=='1')&&(x3=='3')
  398. ||(x1=='2')&&(x2=='3')&&(x3=='1')
  399. ||(x1=='1')&&(x2=='3')&&(x3=='2')
  400. ||(x1=='3')&&(x2=='2')&&(x3=='1')
  401. ||(x1=='3')&&(x2=='1')&&(x3=='2')
  402.  
  403. ||(y1=='1')&&(y2=='2')&&(y3=='3')
  404. ||(y1=='2')&&(y2=='1')&&(y3=='3')
  405. ||(y1=='2')&&(y2=='3')&&(y3=='1')
  406. ||(y1=='1')&&(y2=='3')&&(y3=='2')
  407. ||(y1=='3')&&(y2=='2')&&(y3=='1')
  408. ||(y1=='3')&&(y2=='1')&&(y3=='2')
  409.  
  410. ||(z1=='1')&&(z2=='2')&&(z3=='3')
  411. ||(z1=='2')&&(z2=='1')&&(z3=='3')
  412. ||(z1=='2')&&(z2=='3')&&(z3=='1')
  413. ||(z1=='1')&&(z2=='3')&&(z3=='2')
  414. ||(z1=='3')&&(z2=='2')&&(z3=='1')
  415. ||(z1=='3')&&(z2=='1')&&(z3=='2')) 
  416. {
  417.  
  418.     if ((x1=='1')&&(y1=='2')&&(z1=='3')
  419.       ||(x1=='2')&&(y1=='1')&&(z1=='3')
  420.       ||(x1=='2')&&(y1=='3')&&(z1=='1')
  421.       ||(x1=='1')&&(y1=='3')&&(z1=='2')
  422.       ||(x1=='3')&&(y1=='2')&&(z1=='1')
  423.       ||(x1=='3')&&(y1=='1')&&(z1=='2')
  424.  
  425.       ||(x2=='1')&&(y2=='2')&&(z2=='3')
  426.       ||(x2=='2')&&(y2=='1')&&(z2=='3')
  427.       ||(x2=='2')&&(y2=='3')&&(z2=='1')
  428.       ||(x2=='1')&&(y2=='3')&&(z2=='2')
  429.       ||(x2=='3')&&(y2=='2')&&(z2=='1')
  430.       ||(x2=='3')&&(y2=='1')&&(z2=='2')
  431.  
  432.       ||(x3=='1')&&(y3=='2')&&(z3=='3')
  433.       ||(x3=='2')&&(y3=='1')&&(z3=='3')
  434.       ||(x3=='2')&&(y3=='3')&&(z3=='1')
  435.       ||(x3=='1')&&(y3=='3')&&(z3=='2')
  436.       ||(x3=='3')&&(y3=='2')&&(z3=='1')
  437.       ||(x3=='3')&&(y3=='1')&&(z3=='2'))
  438.     {
  439.         cout<<"you win"<<endl;
  440.     }
  441.     else if((x1!=' ')&&(x2!=' ')&&(x3!=' ')
  442.             ||(y1!=' ')&&(y2!=' ')&&(y3!=' ')
  443.             ||(z1!=' ')&&(z2!=' ')&&(z3!=' '))
  444.     {
  445.         cout<<"you lose"<<endl;
  446.     }
  447.  
  448.  
  449. }
  450. return 0;
  451. }

anyone can help? thank you.
Jan 11 '08 #1
1 1834
Wow! My goodness, that is quite a mess. I assume you've had your Q answered somewhere else, but eh =)...

Your problem lies in your hundreds of conditions, which read:

Expand|Select|Wrap|Line Numbers
  1. ||(x1=='2')&&(y1=='1')&&(z1=='3')
  2.  
These should read:
Expand|Select|Wrap|Line Numbers
  1. ||((x1=='2')&&(y1=='1')&&(z1=='3'))
  2.  
Note the extra set of brackets. These mean that ALL the conditions in the brackets must hold (that is, the entire row must be correct), not just the single x1=='2' being correct. Your program was returning true all the time because of this.

Hehe... I also appreciate your humour in "type -1 to exist" xP
Feb 26 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
tpgames
by: tpgames | last post by:
I have not been able to find source code for a kids (2x2) sudoku game, and was wondering if anyone had the time to volunteer to make a JavaScript version for me that uses images. I only ask, because...
11
tpgames
by: tpgames | last post by:
I've struck zero in finding a Link to a sudoku game that actually uses images for numbers, even images OF numbers. Yes, I'd prefer the game in JavaScript. Every site I've been too, (hundreds) uses...
2
tpgames
by: tpgames | last post by:
In a 3x3 sudoku JavaScript with images, what is 48? and 57? I'm trying to convert a 3x3 sudoku to a 2x2. I don't understand why 48 can't be say, 46 or 45; why 57 can't be 54, etc. Its the numbers I...
0
by: swint002 | last post by:
Here's another game written in T-SQL. Sudoku: http://www.geocities.com/swint002/Sudoku.txt Let me know what you think.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.