Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5.  
  6. bool validateInput(int x){
  7.     return (x>0 && x<8)?true:false;
  8. }// to check whether input is between board dimensions
  9.  
  10. void dropPiece(int (&droppedPieces)[7],int column,int (&board)[6][7],int player){
  11.         column--;
  12.         int row=6-droppedPieces[column]-1;
  13.         board[row][column]=player;
  14.         droppedPieces[column]=droppedPieces[column]+1;
  15.    
  16. }
  17.  
  18. void showBoard(int (&board)[6][7]){
  19.     for(int i=0;i<6;i++){
  20.         for(int j=0;j<7;j++){
  21.             cout<<board[i][j]<<" ";
  22.         }
  23.         cout<<endl;
  24.     }
  25. }
  26.  
  27. /*
  28. int check(int (&board)[6][7],int player,int row){
  29.     for(int i=0;i<6;i++){
  30.         for(int j=0;j<7;j++){
  31.             if(board[i][j]==player){
  32.                
  33.             }
  34.         }
  35.     }
  36. }
  37.  
  38. */
  39.  
  40. int checkHorizontal(int row,int column,int (&board)[6][7],int player,int (&droppedPieces)[7]){
  41.     int count=0;
  42.     int middle;
  43.     bool flag=false;
  44.     bool has=false;
  45.     for(int i=0;i<7;i++){
  46.         if(board[row][i]==player){
  47.             count++;
  48.         }
  49.         else{
  50.             count=0;
  51.         }
  52.        
  53.         if(count==3){
  54.             middle=i-1;
  55.             has=true;
  56.         }
  57.         if(i==6 && count<3){
  58.             flag=true;
  59.         }
  60.     }
  61.     if(has==false && flag==true){
  62.         return -1;
  63.     }
  64.    
  65.    
  66.     int left=middle-2;
  67.     int right=middle+2;
  68.     bool leftSolution=true,rightSolution=true;
  69.     if(left<0){
  70.         leftSolution=false;
  71.     }
  72.     if(right>=7){
  73.         rightSolution=false;
  74.     }
  75.    
  76.     if(leftSolution || rightSolution){
  77.         if(!rightSolution){
  78.             if(row=6-droppedPieces[left]-1){
  79.                 return left;
  80.             }
  81.         }
  82.         if(!leftSolution){
  83.             if(row=6-droppedPieces[right]-1){
  84.                 return right;
  85.             }
  86.         }
  87.     }
  88.    
  89.    
  90.    
  91.    
  92.    
  93.     return -1;
  94. }
  95.  
  96.  
  97.  
  98. /*
  99. checkVertical(int col,int (&board)[6][7],int player,int (&droppedPieces)[7]){
  100.     int count=0;
  101.     int oppo=(player==1)?2:1;
  102.     bool available=true;
  103.     for(int i=0;i<6;i++){
  104.         if(board[i][col]==1){
  105.             count++;
  106.         }
  107.         else{
  108.             if(board[i][col]==2){
  109.                 oppo=i;
  110.             }
  111.         }
  112.        
  113.         if(droppedPieces[col]-(6-oppo)<3){
  114.             available=false;
  115.             return -1;
  116.         }
  117.        
  118.         if(count==3 && available==true){
  119.             return i;
  120.         }
  121.        
  122.     }
  123.  
  124.    
  125.    
  126.    
  127.     return -1;
  128. }
  129. */
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. int main(){
  138.     int board[6][7]={{0,0,0,0,0,0,0},
  139.                     {0,0,0,0,0,0,0},
  140.                     {0,0,0,0,0,0,0},
  141.                     {0,0,0,0,0,0,0},
  142.                     {0,0,0,0,0,0,0},
  143.                     {0,0,0,0,0,0,0}};//defaut :D
  144.     int n=-1;//number of user input
  145.     while(n<1 || n>10){
  146.         cin>>n;
  147.     }
  148.     int firstMoves[n];//array to hold the input
  149.     //now will drop the pieces of user
  150.     //we will need an array of data to keep track of the thrown pieces
  151.     int droppedPieces[7];  //0 dropped pieces in each column
  152.     memset(droppedPieces,0,sizeof(droppedPieces));
  153.  
  154.  
  155.     for(int i=0;i<n;i++){ //getting the input
  156.         cin>>firstMoves[i];
  157.         if(!validateInput(firstMoves[i])){
  158.             i--;//if the input is not between board dimension,
  159.                     //lower 'i', in order to repeat the current input
  160.         }
  161.        
  162.     }
  163.    
  164.     int player=1;
  165.    
  166.     //dropping first moves
  167.     for(int i=0;i<n;i++){
  168.         int temp=firstMoves[i];
  169.         dropPiece(droppedPieces,temp,board,player);
  170.         if(player==1){
  171.             player=2;
  172.         }
  173.         else{
  174.             player=1;
  175.         }
  176.     }
  177.     /*
  178.     bool finish=false;
  179.     while(!finish){
  180.        
  181.        
  182.        
  183.        
  184.     }
  185.    
  186.     */
  187.  
  188.     showBoard(board);
  189.     cout<<endl<<endl<<endl<<"----------------------------------------\n";
  190.     cout<<"Put row, colum,player\n";
  191.     int a,b,c;
  192.     cin>>a>>b>>c;
  193.     cout<<checkHorizontal(a,b,board,c,droppedPieces);
  194.  
  195.  
  196.    
  197.    
  198.  
  199.    
  200.    
  201.    
  202.    
  203.    
  204.    
  205.    
  206.    
  207.    
  208.    
  209.    
  210.    
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement