Noam_15

ארבע בשורה צבעוני שני שחקנים

Jul 2nd, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. using namespace std;
  4.  
  5. //מדריך צבעים https://www.youtube.com/watch?v=IXtm7cI21vM
  6. void setcolor(unsigned short color){
  7.     HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
  8.     SetConsoleTextAttribute(hcon, color);
  9. }
  10.  
  11.  
  12. enum status{empty, white, black};
  13. enum status main_board [7][6]; ///  i,j     // הוא 0 j השורה הכי עליונה היא השורה בה
  14.  
  15. void reset_board(enum status board [7][6]){
  16.     for(int i=0; i<7; i++){
  17.         for(int j=0; j<6; j++){
  18.             board[i][j] = empty;
  19.         }
  20.     }
  21. }
  22.  
  23. void show(enum status board [7][6]){
  24.     cout<<"\n\n1234567\n_______\n";
  25.     for(int j=0; j<6; j++){
  26.         for(int i=0; i<7; i++){
  27.             if(board[i][j]== empty) cout<<"@";
  28.             else if (board[i][j]== white) {
  29.                 setcolor(11);
  30.                 cout<<"w";
  31.                 setcolor(7);
  32.             }
  33.             else {
  34.                 setcolor(12);
  35.                 cout<<"b";
  36.                 setcolor(7);
  37.             }
  38.         }
  39.         cout<<"\n";
  40.     }
  41.     cout<<"\n";
  42. }
  43.  
  44. int get_from_player(enum status stat){
  45.     show(main_board);
  46.     if (stat==white) cout<<"-White move.\n";
  47.     else cout<<"-Black move.\n";
  48.  
  49.     int location;
  50.     cout <<"Enter location:  ";
  51.     cin>> location;
  52.     while(location<1 || location>7 ||  main_board[location-1][0] != empty){
  53.         cout<<"Please enter a legal location:  ";
  54.         cin >> location;
  55.     }
  56.     cout<<"\n";
  57.     return location-1;
  58. }
  59.  
  60. void Put_in_a_move(enum status board [7][6], int location, enum status stat){
  61.     for(int j=5; j>=0; j--){
  62.         if (board[location][j] == empty){
  63.             board[location][j] = stat;
  64.             break;
  65.         }
  66.     }
  67. }
  68.  
  69. enum status Victory_check (enum status board [7][6]){
  70.  
  71.     for(int j=5; j>=0; j--){
  72.         for(int i=0; i<4; i++){
  73.             if (board[i][j]==empty) continue;
  74.             for(int b=i+1, q=0; q<3; q++, b++){
  75.                 if (board[i][j]!=board[b][j]) break;
  76.                 if(q==2) return board[i][j];
  77.             }
  78.         }
  79.     }
  80.     // עד כאן בדיקת ניצחונות אופקיים
  81.  
  82.  
  83.     for(int i=0; i<7; i++){
  84.         for(int j=5; j>2; j--){
  85.             if (board[i][j]==empty) continue;
  86.  
  87.             for(int b=j-1, q=0; q<3; q++, b--){
  88.                 if (board[i][j]!=board[i][b]) break;
  89.                 if(q==2) return board[i][j];
  90.             }
  91.         }
  92.     }
  93.     // עד כאן בדיקת ניצחונות ישרים
  94.  
  95.  
  96.  
  97.     for(int j=5; j>2; j--){
  98.         for(int i=0; i<4; i++){
  99.             if (board[i][j]==empty) continue;
  100.             for(int q=1; q<4; q++){
  101.                 if (board[i][j]!=board[i+q][j-q]) break;
  102.                 if(q==3) return board[i][j];
  103.             }
  104.         }
  105.     }
  106.     // עד כאן אלכסון משמאל נמוך לימין גבוה
  107.  
  108.  
  109.     for(int j=5; j>2; j--){
  110.         for(int i=3; i<7; i++){
  111.             if (board[i][j]==empty) continue;
  112.             for(int q=1; q<4; q++){
  113.                 if (board[i][j]!=board[i-q][j-q]) break;
  114.                 if(q==3) return board[i][j];
  115.             }
  116.         }
  117.     }
  118.     // עד כאן אלכסון מימין נמוך לשמאל גבוה
  119.    
  120.     return empty;
  121. }
  122.  
  123.  
  124. void main(){
  125.     enum status VictoryFlag = empty;
  126.     reset_board(main_board);
  127.     //show(main_board);
  128.  
  129.  
  130.     int Count_of_actions = 0, loc;
  131.     do{
  132.         loc = get_from_player(white);
  133.         Put_in_a_move(main_board, loc, white);
  134.         VictoryFlag = Victory_check(main_board);
  135.         if (VictoryFlag != empty) break;
  136.         Count_of_actions++;
  137.         if(Count_of_actions==42) break;
  138.  
  139.         loc = get_from_player(black);
  140.         Put_in_a_move(main_board, loc, black);
  141.         VictoryFlag = Victory_check(main_board);
  142.         if (VictoryFlag != empty) break;
  143.         Count_of_actions++;
  144.  
  145.     }while(Count_of_actions<42);
  146.  
  147.     show(main_board);
  148.     if (VictoryFlag==white) {setcolor(11); cout<<"White won!!!!!\n\n\n\n"; setcolor(7);}
  149.     else if (VictoryFlag==black) {setcolor(12); cout<<"Black won!!!!!\n\n\n\n"; setcolor(7);}
  150.     else {setcolor(15); cout<<"End! No win.\n\n\n\n"; setcolor(7);}
  151. }
Advertisement
Add Comment
Please, Sign In to add comment