Noam_15

איקס עיגול אדם נגד מחשב

May 12th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime> // Needed for the true randomization
  3. #include <windows.h> // בשביל ההשהיה
  4. using namespace std;
  5. enum Status {empty, ix, igul};
  6.  
  7.  
  8. // הצהרות //
  9.  
  10. Status victory(Status board[3][3]);
  11.  
  12. /// // /// /
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. class location{
  20. public:
  21.     int x, y;
  22. };
  23.  
  24. location get_From_user(Status stat, Status board[3][3]){
  25.     location l;
  26.     do{
  27.         if (stat == ix) cout<<"Ix turn. Enter x, y:  ";
  28.         else cout<<"Igul turn. Enter x, y:  ";
  29.         cin >>l.x >>l.y;
  30.  
  31.     } while(board[l.x][l.y] !=empty || l.x>2 || l.y>2 || l.x<0 || l.y<0);
  32.  
  33.     return l;
  34. }
  35.  
  36.  
  37.  
  38. location random_empty_corner(Status board[3][3]){
  39.     int randC;
  40.     location l;
  41.     /*
  42.     if (board[0][0]!=empty && board[2][0]!=empty && board[2][2]!=empty && board[0][2]!=empty){ // בדיקה שאם אין פינה ריקה שלא ימשיך לנצח
  43.         l.x=-1;
  44.         l.y=-1;
  45.         return l;
  46.     }
  47.     */
  48.  
  49.     do{
  50.         randC = rand() % 4;
  51.         switch(randC){
  52.         case 0: {l.x=0; l.y=0;    break;}
  53.         case 1: {l.x=2; l.y=0;    break;}
  54.         case 2: {l.x=2; l.y=2;    break;}
  55.         case 3: {l.x=0; l.y=2;    break;}
  56.         }
  57.     }while(board[l.x][l.y]!=empty);
  58.  
  59.         return l;
  60.     }
  61.  
  62. location random_empty(Status board[3][3],Status stat){
  63.     // ראשית מחפשרנדומלי עםפוטנציאל לניצחון, אם אין אז מביא רנדומלי כלשהו
  64.     location rand_location;
  65.  
  66.     Status DemoBoard[3][3];
  67.     for(int y=0; y<3; y++){
  68.         for(int x=0; x<3; x++){
  69.             DemoBoard[x][y] = board[x][y];
  70.         }
  71.     }
  72.  
  73.     // בדיקת מקום שיש לו פוטנציאל לניצחון - אם נשים איפה שהוא ואח"כ יש מקום שאם נשים בו ננצח
  74.     bool find=false;
  75.     for(int y=0; y<3; y++){
  76.         for(int x=0; x<3; x++){
  77.             if(DemoBoard[x][y]!=empty) continue;
  78.             DemoBoard[x][y]=stat;
  79.  
  80.             for(int y1=0; y1<3; y1++){
  81.                 for(int x1=0; x1<3; x1++){
  82.                     if(DemoBoard[x1][y1]!=empty) continue;
  83.                     DemoBoard[x1][y1]=stat;
  84.                     if(victory(DemoBoard)==stat){
  85.                         rand_location.x=x;
  86.                         rand_location.y=y;
  87.                         return rand_location;
  88.                     }
  89.                     DemoBoard[x1][y1]=empty;
  90.                 }
  91.             }
  92.             DemoBoard[x][y]=empty;
  93.         }
  94.     }
  95.  
  96.  
  97.     do{
  98.         rand_location.x = rand() % 3;
  99.         rand_location.y= rand() % 3;
  100.     }while(board[rand_location.x][rand_location.y]!=empty);
  101.     return rand_location;
  102. }
  103.  
  104. location if_there_is_a_key_location(Status board[3][3], Status stat){ // מיקום מפתח זה מיקום שאם צד מסוים ישים בו יהיו לו שתי מקומות לנצח בהם
  105.     // הנחה: אין ניצחון מיידי אפשרי לאף צד
  106.     // אם אין מקום מפתח - יחזיר -1 -1      - הכוונה למינוס
  107.  
  108.  
  109.     location loc;
  110.     bool find=false;
  111.     for(loc.y=0; loc.y<3; loc.y++){
  112.         for(loc.x=0; loc.x<3; loc.x++){ // נבדוק עבור כל מיקום האם הוא מיקום מפתח
  113.             if(board[loc.x][loc.y]!=empty) continue;
  114.             /// יש שלוש בדיקות- בדיקת אלכסונים, שורות ועמודות
  115.  
  116.             if(loc.x==0 && loc.y==0){ //00
  117.                 if(((board[1][0]==stat&&board[2][0]==empty) || (board[2][0]==stat&&board[1][0]==empty))   &&
  118.                     ((board[0][1]==stat&&board[0][2]==empty) || (board[0][2]==stat&&board[0][1]==empty))){
  119.                     find = true;
  120.                     break;
  121.                 }
  122.                 continue;
  123.             }
  124.             if(loc.x==1 && loc.y==0){ //10
  125.                 if(((board[0][0]==stat&&board[2][0]==empty) || (board[2][0]==stat&&board[0][0]==empty)) &&
  126.                     ((board[1][1]==stat&&board[1][2]==empty) || (board[1][2]==stat&&board[1][1]==empty))){
  127.                     find = true;
  128.                     break;
  129.                 }
  130.                 continue;
  131.             }
  132.             if(loc.x==2 && loc.y==0){ //20
  133.                 if(((board[0][0]==stat&&board[1][0]==empty) || (board[1][0]==stat&&board[0][0]==empty)) &&
  134.                     ((board[2][1]==stat&&board[2][2]==empty) || (board[2][2]==stat&&board[2][1]==empty))){
  135.                     find = true;
  136.                     break;
  137.                 }
  138.                 continue;
  139.             }
  140.             if(loc.x==0 && loc.y==1){  //01
  141.                 if(((board[1][1]==stat&&board[2][1]==empty) || (board[2][1]==stat&&board[1][1]==empty)) &&
  142.                     ((board[0][0]==stat&&board[0][2]==empty) || (board[0][2]==stat&&board[0][0]==empty))){
  143.                     find = true;
  144.                     break;
  145.                 }
  146.                 continue;
  147.             }
  148.             if(loc.x==1 && loc.y==1){  //11
  149.                 if(((board[0][1]==stat&&board[2][1]==empty) || (board[2][1]==stat&&board[0][1]==empty)) &&
  150.                     ((board[1][0]==stat&&board[1][2]==empty) || (board[1][2]==stat&&board[1][0]==empty))){
  151.                     find = true;
  152.                     break;
  153.                 }
  154.                 continue;
  155.             }
  156.             if(loc.x==2 && loc.y==1){  //21
  157.                 if(((board[0][1]==stat&&board[1][1]==empty) || (board[1][1]==stat&&board[0][1]==empty)) &&
  158.                     ((board[2][0]==stat&&board[2][2]==empty) || (board[2][2]==stat&&board[2][0]==empty))){
  159.                     find = true;
  160.                     break;
  161.                 }
  162.                 continue;
  163.             }
  164.             if(loc.x==0 && loc.y==2){ //02
  165.                 if(((board[1][2]==stat&&board[2][2]==empty) || (board[2][2]==stat&&board[1][2]==empty)) &&
  166.                     ((board[0][0]==stat&&board[0][1]==empty) || (board[0][1]==stat&&board[0][0]==empty))){
  167.                     find = true;
  168.                     break;
  169.                 }
  170.                 continue;
  171.             }
  172.             if(loc.x==1 && loc.y==2){  //12
  173.                 if(((board[0][2]==stat&&board[2][2]==empty) || (board[2][2]==stat&&board[0][2]==empty)) &&
  174.                     ((board[1][0]==stat&&board[1][1]==empty) || (board[1][1]==stat&&board[1][0]==empty))){
  175.                     find = true;
  176.                     break;
  177.                 }
  178.                 continue;
  179.             }
  180.             if(loc.x==2 && loc.y==2){  //22
  181.                 if(((board[0][2]==stat&&board[1][2]==empty) || (board[1][2]==stat&&board[0][2]==empty)) &&
  182.                     ((board[2][0]==stat&&board[2][1]==empty) || (board[2][1]==stat&&board[2][0]==empty))){
  183.                        
  184.                     find = true;
  185.                     break;
  186.                 }
  187.                 continue;
  188.             }
  189.  
  190.  
  191.  
  192.  
  193.  
  194.         }
  195.         if (find==true) break;
  196.     }
  197.     if(find==false){
  198.         loc.x=-1;
  199.         loc.y=-1;
  200.     }
  201.     return loc;
  202. }
  203.  
  204.  
  205. location win_location_for_stat(Status board[3][3], Status stat){
  206.     // אם הצבע האמור לא יכול לנצח, יחזיר  -1 -1
  207.     location l;
  208.     Status DemoBoard[3][3];
  209.     for(int y=0; y<3; y++){
  210.         for(int x=0; x<3; x++){
  211.             DemoBoard[x][y] = board[x][y];
  212.         }
  213.     }
  214.  
  215.     bool stop=false;
  216.     for(int y=0; y<3; y++){
  217.         for(int x=0; x<3; x++){
  218.             if(DemoBoard[x][y]!=empty) continue;
  219.             DemoBoard[x][y]=stat;
  220.             if(victory(DemoBoard)==stat){
  221.                 stop=true;
  222.                 l.x=x; l.y=y;
  223.                 // DemoBoard[x][y]=empty;  // להוסיף השורה הזאת אם רוצים לוותר על הצורך במערך העזרולעבוד ישר על המערך הישיר
  224.                 break;
  225.             }
  226.             DemoBoard[x][y]=empty;
  227.         }
  228.         if (stop==true) break;
  229.     }
  230.     if (stop==false){
  231.         l.x=-1;
  232.         l.y=-1;
  233.     }
  234.     return l;
  235. }
  236.  
  237.  
  238. location get_From_AI(int turn, Status board[3][3], Status stat){ // מקבל את התור מ1-9
  239.     if (stat == ix) cout<<"Ix turn. AI...\n";
  240.     else cout<<"Igul turn. AI...\n";
  241.     Sleep(1000*1.1); // השהייה של שנייה וקצת
  242.     location l;
  243.     location a_check;
  244.     int randC;
  245.     Status enemy_stat = (stat==ix)? igul : ix;
  246.     Status DemoBoard[3][3];
  247.     for(int y=0; y<3; y++){
  248.         for(int x=0; x<3; x++){
  249.             DemoBoard[x][y] = board[x][y];
  250.         }
  251.     }
  252.  
  253.  
  254.  
  255.     switch (turn){
  256.     case 1:{ l= random_empty_corner(board); break;}
  257.     case 2:{if (board[1][1]==empty){l.x=1; l.y=1; break;}
  258.            else { l= random_empty_corner(board); break;}
  259.            }
  260.     case 3:{
  261.         if(board[0][0] == stat){
  262.             if (board[2][2]==empty) {l.x=2; l.y=2; break;}
  263.             else{
  264.                 randC = rand() % 2;
  265.                 if (randC==0){l.x=0; l.y=2; break;}
  266.                 else {l.x=2; l.y=0; break;}
  267.             }
  268.         }
  269.         else if(board[2][0] == stat){
  270.             if (board[0][2]==empty) {l.x=0; l.y=2; break;}
  271.             else{
  272.                 randC = rand() % 2;
  273.                 if (randC==0){l.x=2; l.y=2; break;}
  274.                 else {l.x=0; l.y=0; break;}
  275.             }
  276.         }
  277.         else if(board[2][2] == stat){
  278.             if (board[0][0]==empty) {l.x=0; l.y=0; break;}
  279.             else{
  280.                 randC = rand() % 2;
  281.                 if (randC==0){l.x=0; l.y=2; break;}
  282.                 else {l.x=2; l.y=0; break;}
  283.             }
  284.         }
  285.         else if(board[0][2] == stat){
  286.             if (board[2][0]==empty) {l.x=2; l.y=0; break;}
  287.             else{
  288.                 randC = rand() % 2;
  289.                 if (randC==0){l.x=2; l.y=2; break;}
  290.                 else {l.x=0; l.y=0; break;}
  291.             }
  292.         }
  293.         break;
  294.            }
  295.     case 4:{if((board[0][0]==enemy_stat&& board[2][2]==enemy_stat) ||
  296.                (board[0][2]==enemy_stat&& board[2][0]==enemy_stat) ){
  297.                    randC = rand() % 4;
  298.                    // אם שתי פינות מנוגדות תפוסות על ידי היריב - כמובן שהאמצע תפוס כבר על ידינו - נשים באחד מ4 ההמקומות שהם לא פינות
  299.                    switch (randC){
  300.                    case 0: { l.x=1; l.y=0; break; }
  301.                    case 1: { l.x=2; l.y=1; break; }
  302.                    case 2: { l.x=1; l.y=2; break; }
  303.                    case 3: { l.x=0; l.y=1; break; }
  304.                    }
  305.                    break;
  306.            }
  307.  
  308.            // מכיוון שזה התור הרביעי אז וודאי שאנחנו עדיין לא יכולים לעשות ניצחון
  309.  
  310.            //עכשיו בדיקה אם היריב יכול לעשות ניצחון בתור הבא שלו:
  311.            a_check = win_location_for_stat(board, enemy_stat);
  312.            if(a_check.x != -1){
  313.                l=a_check;
  314.                break;
  315.            }
  316.  
  317.  
  318.            // לא נעשה בדיקהת מקום מפתח לצד שלנו כי זה רק תור רביעי
  319.            // עכשיו אם סימן של היריב הוא משהו כמו 01 ו 20  אז צריך לחסום אותו ב 00.     משמעות: בדיקת מקום מפתח.
  320.            a_check = if_there_is_a_key_location(board,enemy_stat);
  321.            if(a_check.x != -1){
  322.                l=a_check;
  323.                break;
  324.            }
  325.  
  326.            l=random_empty(board, stat);
  327.            break;
  328.            }
  329.     case 5: case 6: case 7: case 8:case 9:{
  330.         a_check = win_location_for_stat(board,stat);
  331.         if(a_check.x != -1){
  332.             l=a_check;
  333.             break;
  334.         }
  335.         a_check = win_location_for_stat(board, enemy_stat);
  336.         if(a_check.x != -1){
  337.             l=a_check;
  338.             break;
  339.         }
  340.  
  341.  
  342.        
  343.         // עכשיו אם סימן של היריב הוא משהו כמו 01 ו 20  אז צריך לחסום אותו ב 00.     משמעות: בדיקת מקום מפתח.
  344.         a_check = if_there_is_a_key_location(board,stat);
  345.         if(a_check.x != -1){
  346.             l=a_check;
  347.             break;
  348.         }
  349.         a_check = if_there_is_a_key_location(board,enemy_stat);
  350.         if(a_check.x != -1){
  351.             l=a_check;
  352.             break;
  353.         }
  354.  
  355.         l=random_empty(board, stat);
  356.         break;
  357.             }
  358.     }
  359.  
  360.     return l;
  361. }
  362.  
  363.  
  364.  
  365. void print(Status board[3][3]){
  366.     int x, y;
  367.     cout<<"\n   012\n\n";
  368.     for (y=0; y<3; y++){
  369.         cout<<y<<"  ";
  370.         for(x=0; x<3; x++){
  371.             if (board[x][y] == empty) cout<<(char)177;
  372.             else if (board[x][y] == ix) cout<<"x";
  373.             else if (board[x][y] == igul) cout<<"O";
  374.         }
  375.         cout<<"\n";
  376.     }
  377.     cout<<"\n";
  378. }
  379.  
  380. Status victory(Status board[3][3]){
  381.     if(board[0][0] != empty &&board[0][0]==board[1][0] && board[0][0]==board[2][0])return board[0][0];
  382.     if(board[0][1] != empty &&board[0][1]==board[1][1] && board[0][1]==board[2][1])return board[0][1];
  383.     if(board[0][2] != empty &&board[0][2]==board[1][2] && board[0][2]==board[2][2])return board[0][2];
  384.     // 3 שורות
  385.  
  386.     if(board[0][0] != empty &&board[0][0]==board[0][1] && board[0][0]==board[0][2])return board[0][0];
  387.     if(board[1][0] != empty &&board[1][0]==board[1][1] && board[1][0]==board[1][2])return board[1][0];
  388.     if(board[2][0] != empty &&board[2][0]==board[2][1] && board[2][0]==board[2][2])return board[2][0];
  389.     // 3 עמודות
  390.  
  391.     if(board[0][0] != empty &&board[0][0]==board[1][1] && board[0][0]==board[2][2])return board[0][0];
  392.     if(board[2][0] != empty &&board[2][0]==board[1][1] && board[2][0]==board[0][2])return board[2][0];
  393.     // אלכסונים
  394.  
  395.  
  396.     return empty; // אין עדיין ניצחון
  397. }
  398.  
  399. void main(){
  400.     srand( time(0));
  401.     cout<<"**Wellcome to IX-Igul game by Noam and Daniel\n\n";
  402.     Status board[3][3];
  403.     Status vic;
  404.     location l;
  405.     bool user_first=true;
  406.     int i=0, x, y;
  407.  
  408.     for (y=0; y<3; y++){ // איפוס המערך
  409.         for (x=0; x<3; x++){
  410.             board[x][y]=empty;
  411.         }
  412.     }
  413.  
  414.  
  415.     print(board);
  416.     char str[6];
  417.     do{
  418.         flushall(); // עבור קלט שיש בו רווחים
  419.         cout<<"Are you first or second? (F/S):  ";
  420.         cin>>str;
  421.     }while(str[0]!='f' && str[0]!='F' && str[0]!='s' && str[0]!='S');
  422.  
  423.  
  424.     while(i<9){
  425.         if(str[0]=='f' || str[0]=='F') l = get_From_user(ix, board);
  426.         else l = get_From_AI(i+1, board, ix);
  427.         // l = (str[0]=='f' || str[0]=='F')? get_From_user(ix, board)  : get_From_AI(i+1, board, ix);  //  אני לא יודע למה .sלא עובד ב
  428.         board[l.x][l.y] = ix;
  429.         print(board);
  430.         vic = victory(board);
  431.         if (vic!=empty) break;
  432.         i++;
  433.         if (i>8) break;
  434.  
  435.  
  436.         if(str[0]=='f' || str[0]=='F') l = get_From_AI(i+1, board, igul);
  437.         else l = get_From_user(igul, board);
  438.         //  l = (str[0]=='s' || str[0]!='S')?  get_From_user(igul , board)  :  get_From_AI(i+1, board, igul);  // אני לא יודע למה .sלא עובד ב
  439.         board[l.x][l.y] = igul;
  440.         print(board);
  441.         vic = victory(board);
  442.         if (vic!=empty) break;
  443.         i++;
  444.  
  445.     }
  446.     if (i==9) cout<<"\nA tie!!\n\n";
  447.     else{
  448.         if(vic==ix) cout<<"\nIx win!!\n\n";
  449.         else cout<<"\nIgul win!!\n\n";
  450.     }
  451. }
Advertisement
Add Comment
Please, Sign In to add comment