Noam_15

ארבע בשורה אדם נגד מחשב

Jul 3rd, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.21 KB | None | 0 0
  1. // V0.05
  2. #include <iostream>
  3. #include <windows.h>
  4. #include <ctime> // עבור מספר רנדומלי
  5. using namespace std;
  6.  
  7. //מדריך צבעים https://www.youtube.com/watch?v=IXtm7cI21vM
  8. void setcolor(unsigned short color){
  9.     HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
  10.     SetConsoleTextAttribute(hcon, color);
  11. }
  12.  
  13.  
  14. enum status{empty, white, black};
  15. enum status main_board [7][6]; ///  i,j     // הוא 0 j השורה הכי עליונה היא השורה בה
  16.  
  17. void reset_board(enum status board [7][6]){
  18.     for(int i=0; i<7; i++){
  19.         for(int j=0; j<6; j++){
  20.             board[i][j] = empty;
  21.         }
  22.     }
  23. }
  24.  
  25. void show(enum status board [7][6]){
  26.     cout<<"\n\n1234567\n_______\n";
  27.     for(int j=0; j<6; j++){
  28.         for(int i=0; i<7; i++){
  29.             if(board[i][j]== empty) cout<<"@";
  30.             else if (board[i][j]== white) {
  31.                 setcolor(11);
  32.                 cout<<"w";
  33.                 setcolor(7);
  34.             }
  35.             else {
  36.                 setcolor(12);
  37.                 cout<<"b";
  38.                 setcolor(7);
  39.             }
  40.         }
  41.         cout<<"\n";
  42.     }
  43.     cout<<"\n";
  44. }
  45.  
  46. int get_from_player(enum status stat){
  47.     show(main_board);
  48.     if (stat==white) cout<<"-White move.\n";
  49.     else cout<<"-Black move.\n";
  50.  
  51.     int location;
  52.     cout <<"Enter location:  ";
  53.     cin>> location;
  54.     while(location<1 || location>7 ||  main_board[location-1][0] != empty){
  55.         cout<<"Please enter a legal location:  ";
  56.         cin >> location;
  57.     }
  58.     cout<<"\n";
  59.     return location-1;
  60. }
  61.  
  62. void Put_in_a_move(enum status board [7][6], int location, enum status stat){
  63.     for(int j=5; j>=0; j--){
  64.         if (board[location][j] == empty){
  65.             board[location][j] = stat;
  66.             break;
  67.         }
  68.     }
  69. }
  70.  
  71. enum status Victory_check (enum status board [7][6]){
  72.  
  73.     for(int j=5; j>=0; j--){
  74.         for(int i=0; i<4; i++){
  75.             if (board[i][j]==empty) continue;
  76.             for(int b=i+1, q=0; q<3; q++, b++){
  77.                 if (board[i][j]!=board[b][j]) break;
  78.                 if(q==2) return board[i][j];
  79.             }
  80.         }
  81.     }
  82.     // עד כאן בדיקת ניצחונות אופקיים
  83.  
  84.  
  85.     for(int i=0; i<7; i++){
  86.         for(int j=5; j>2; j--){
  87.             if (board[i][j]==empty) continue;
  88.  
  89.             for(int b=j-1, q=0; q<3; q++, b--){
  90.                 if (board[i][j]!=board[i][b]) break;
  91.                 if(q==2) return board[i][j];
  92.             }
  93.         }
  94.     }
  95.     // עד כאן בדיקת ניצחונות ישרים
  96.  
  97.  
  98.  
  99.     for(int j=5; j>2; j--){
  100.         for(int i=0; i<4; i++){
  101.             if (board[i][j]==empty) continue;
  102.             for(int q=1; q<4; q++){
  103.                 if (board[i][j]!=board[i+q][j-q]) break;
  104.                 if(q==3) return board[i][j];
  105.             }
  106.         }
  107.     }
  108.     // עד כאן אלכסון משמאל נמוך לימין גבוה
  109.  
  110.  
  111.     for(int j=5; j>2; j--){
  112.         for(int i=3; i<7; i++){
  113.             if (board[i][j]==empty) continue;
  114.             for(int q=1; q<4; q++){
  115.                 if (board[i][j]!=board[i-q][j-q]) break;
  116.                 if(q==3) return board[i][j];
  117.             }
  118.         }
  119.     }
  120.     // עד כאן אלכסון מימין נמוך לשמאל גבוה
  121.    
  122.     return empty;
  123. }
  124.  
  125.  
  126.  
  127.  
  128.     /* מכאן פונקציות לאדם נגד מחשב */
  129.  
  130.  
  131. void clear_location(enum status board [7][6], int loc){
  132.         for(int j=0; j<6; j++){
  133.         if (board[loc][j] != empty){
  134.             board[loc][j] = empty;
  135.             break;
  136.         }
  137.     }
  138. }
  139.  
  140. int get_random_location(enum status board [7][6]){
  141.     int x;
  142.     while(1){
  143.         x = rand() % 11;
  144.         if(x==10)x=3;
  145.         else if(x>6) x-=5;
  146.         if(board[x][0]!=empty) continue;
  147.         return x;
  148.     }
  149. }
  150.  
  151. int check_for_winning_location(enum status board [7][6], enum status stat){
  152.     enum status VictoryFlag = empty;
  153.     for(int i=0; i<7; i++){
  154.         if(board[i][0] != empty) continue;
  155.         Put_in_a_move(board, i, stat);
  156.         VictoryFlag = Victory_check(board);
  157.         clear_location(board, i); // מחזירים את הלוח לאיך שהוא היה לפני השינוי
  158.         if(VictoryFlag == stat) return i;
  159.     }
  160.     return -1; // המשמעות היא שאין מיקום שיניב ניצחון לצבע הזה
  161. }
  162.  
  163. int get_from_AI(enum status board [7][6], enum status stat, int Count_of_actions){
  164.     int loc;
  165.     enum status enemy_color = (stat==white)? black : white;
  166.  
  167.     if (Count_of_actions == 0 || Count_of_actions == 1) return 3; // תור ראשון ושני לשים באמצע
  168.    
  169.     loc = check_for_winning_location(board, stat); // נצח אם אפשר
  170.     if(loc!=-1) return loc;
  171.     loc = check_for_winning_location(board, enemy_color); // חסום ניצחון מיידי של היריב
  172.     if(loc!=-1) return loc;
  173.  
  174.  
  175.  
  176.  
  177.     return get_random_location(board);// עד שנכתוב קוד חכם נשאיר את המיקום האקראי
  178.  
  179.  
  180. }
  181.  
  182. int get_from_AI_with_show(enum status board [7][6], enum status stat, int Count_of_actions){
  183.     show(main_board);
  184.     if (stat==white) cout<<"-White AI move...\n";
  185.     else cout<<"-Black AI move...\n";
  186.     Sleep(1100);
  187.  
  188.     int location = get_from_AI(board, stat, Count_of_actions);
  189.     cout <<"location (AI):  "<<location<<"\n\n";
  190.     return location;
  191. }
  192.  
  193.  
  194. void main(){
  195.     srand(time(0)); // עבור מספר רנדומלי
  196.     enum status VictoryFlag = empty;
  197.     char strMODE[10];
  198.     reset_board(main_board);
  199.  
  200.     int Count_of_actions = 0, loc;
  201.     do{
  202.     cout<<"AI or Friend mode (A/F): ";
  203.     cin>>strMODE;
  204.     }while(strMODE[0] != 'a' && strMODE[0] != 'A' && strMODE[0] != 'f' && strMODE[0] != 'F');
  205.  
  206.  
  207.     if(strMODE[0] == 'f' || strMODE[0] == 'F'){ // אם זה אדם נגד אדם
  208.     do{
  209.         loc = get_from_player(white);
  210.         Put_in_a_move(main_board, loc, white);
  211.         VictoryFlag = Victory_check(main_board);
  212.         if (VictoryFlag != empty) break;
  213.         Count_of_actions++;
  214.         if(Count_of_actions==42) break;
  215.  
  216.         loc = get_from_player(black);
  217.         Put_in_a_move(main_board, loc, black);
  218.         VictoryFlag = Victory_check(main_board);
  219.         if (VictoryFlag != empty) break;
  220.         Count_of_actions++;
  221.  
  222.     }while(Count_of_actions<42);
  223.     }
  224.  
  225.     else{ // אם זה אדם נגד מחשב
  226.         do{
  227.         loc = get_from_player(white);
  228.         Put_in_a_move(main_board, loc, white);
  229.         VictoryFlag = Victory_check(main_board);
  230.         if (VictoryFlag != empty) break;
  231.         Count_of_actions++;
  232.         if(Count_of_actions==42) break;
  233.  
  234.         loc = get_from_AI_with_show(main_board, black, Count_of_actions);
  235.         Put_in_a_move(main_board, loc, black);
  236.         VictoryFlag = Victory_check(main_board);
  237.         if (VictoryFlag != empty) break;
  238.         Count_of_actions++;
  239.  
  240.     }while(Count_of_actions<42);
  241.  
  242.     }
  243.  
  244.     show(main_board);
  245.     if (VictoryFlag==white) {setcolor(11); cout<<"White won!!!!!\n\n\n\n"; setcolor(7);}
  246.     else if (VictoryFlag==black) {setcolor(12); cout<<"Black won!!!!!\n\n\n\n"; setcolor(7);}
  247.     else {setcolor(15); cout<<"End! No win.\n\n\n\n"; setcolor(7);}
  248. }
Advertisement
Add Comment
Please, Sign In to add comment