ahmed0saber

Sudoku 3*3 in C++

Nov 7th, 2020 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     string play[3][3]={{"1","b2","2"},{"d4","e5","f6"},{"3","h8","i9"}};
  6.     char solved[3][3]={{'1','3','2'},{'2','1','3'},{'3','2','1'}} , choice;
  7.     int loca , n=3 , sol[3][3]={{1,0,1},{0,0,0},{1,0,0}} , l;
  8.     board:
  9.     cout<<"\n\n\n\t\t\t\t----------\n";
  10.     cout<<"\t\t\t\t|"<<play[0][0]<<"|"<<play[0][1]<<"|"<<play[0][2]<<"|\n";
  11.     cout<<"\t\t\t\t----------\n";
  12.     cout<<"\t\t\t\t|"<<play[1][0]<<"|"<<play[1][1]<<"|"<<play[1][2]<<"|\n";
  13.     cout<<"\t\t\t\t----------\n";
  14.     cout<<"\t\t\t\t|"<<play[2][0]<<"|"<<play[2][1]<<"|"<<play[2][2]<<"|\n";
  15.     cout<<"\t\t\t\t----------\n";
  16.     if(n==9)
  17.     {
  18.         cout<<"YOU WIN";
  19.         goto done;
  20.     }
  21.     cout<<"Choose the location (1 to 9) : ";
  22.     cin>>loca;
  23.     if(loca>0&&loca<10)
  24.     {
  25.         cout<<"Enter the value you want to place here (1 to 3) : ";
  26.         cin>>choice;
  27.         if(choice=='1'||choice=='2'||choice=='3')
  28.         {
  29.             if(loca>0&&loca<4)
  30.             {
  31.                 l=0;
  32.             }
  33.             else if(loca>3&&loca<7)
  34.             {
  35.                 l=1;
  36.             }
  37.             else if(loca>6&&loca<10)
  38.             {
  39.                 l=2;
  40.             }
  41.             if(choice==solved[l][loca-1-(l*3)])
  42.             {
  43.                 if(sol[l][loca-1-(l*3)]==0)
  44.                 {
  45.                     play[l][loca-1-(l*3)]=solved[l][loca-1-(l*3)];
  46.                     sol[l][loca-1-(l*3)]=1;
  47.                     n=n+1;
  48.                 }
  49.                 else
  50.                 {
  51.                     cout<<"ERROR , Repeated move\n";
  52.                     goto board;
  53.                 }
  54.             }
  55.             else
  56.             {
  57.                     cout<<"WRONG move , Try again\n";
  58.                     goto board;
  59.             }
  60.         }
  61.         else
  62.         {
  63.             cout<<"ERROR , Enter 1 , 2 or 3\n";
  64.             goto board;
  65.         }
  66.     }
  67.     else
  68.     {
  69.         cout<<"ERROR , Enter a character from 1 to 9\n";
  70.         goto board;
  71.     }
  72.     goto board;
  73.     done:
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment