SlideShare a Scribd company logo
1 of 9
Download to read offline
#include<iostream>
#include<string>
#include<cstdio>
#include<map>
#include<algorithm>
#include<cmath>
#include<vector>
#include<sstream>
#include<stack>
#include<queue>
#include<cstring>
#include<fstream>
#include<climits>

#define pb push_back
#define LL long long
#define OUTPUT_TO_FILE 1
#define s(n)                                         scanf("%d",&n)
#define sl(n)                                        scanf("%lld",&n)
#define sf(n)                                        scanf("%lf",&n)
#define ss(n)                                        scanf("%s",n)
#define MAX_LEVEL 5

using namespace std;

class node
{
   public:
   int board[6][7];
};

typedef struct
{
int val,move;
}ret;
int team_no;
void print(node start){
   int i,j;
   for(i=0;i<6;i++){
            for(j=0;j<7;j++)
                      cout<<start.board[i][j]<<" ";
            cout<<endl;
   }
}
int weight(vector<int> v){
            if(v.size()<=3)
                      return 0;
            int i,j;
            int Weights[] = { 1, 5, 100, 10000,2, 6, 200, 15000 };
            int score = 0;
            for(i=0;i<v.size()-3;i++){
int c = 0;
         int p = 0;
         int dual = 0;
         for (j = 0; j < 4; j++){
             if (v[i + j] == team_no) c++; //TODO Make sure that it looks at it's own identity
            else if (v[i + j] ==3 - team_no) p++;
            else if (v[i + j] == 12) dual++;
         }
         if ((c > 0) && (p == 0))
         {
            c += dual;
            //Computer opportunity
             if (c == 4) return Weights[3]; //Win
            score += ((c/3)*Weights[2]) + ((c/2)*Weights[1]) + Weights[0];
         }
         else if ((c == 0) && (p > 0))
         {
             p += dual;
            //Player opportunity
             if (p == 4) return -1*Weights[7]; //Win
            score -= ((p / 3) * Weights[6]) + ((p / 2) * Weights[5]) + Weights[4];
         }
    }
    return score;

}


int evaluate(node start){
         int val = 0;
         int i,j,k;

        //rows
        vector<int> v;
        for(i=5;i>=0;i--){
                 v.clear();
                 for(j=0;j<=6;j++)
                           v.pb(start.board[i][j]);
                 val += weight(v);
        }
        //cout<<val<<" after rows"<<endl;
        //columns
        for(j=0;j<=6;j++){
                 v.clear();
                 for(i=0;i<=5;i++){
                           v.pb(start.board[i][j]);
                 }
                 val += weight(v);
        }
        //cout<<val<<" after columns"<<endl;
//daigonals
       for(k=0;k<=5;k++){
               i = k;
               j = 0;
               v.clear();
               while(i>=0){
                        v.pb(start.board[i][j]);
                        i--;j++;
               }
               val += weight(v);
       }

       for(k=1;k<=6;k++){
               i = 5;j = k;
               v.clear();
               while(j<=6){
                         v.pb(start.board[i][j]);
                         j++;i--;
               }
               val += weight(v);
       }
       //cout<<val<<" after diagonals"<<endl;

       //2nd set of diagonals
       for(k=5;k>=0;k--){
               i = k;
               j = 0;
               v.clear();
               while(i<=5){
                        v.pb(start.board[i][j]);
                        i++;j++;
               }
               val += weight(v);
       }
       for(k=1;k<=6;k++){
               j = k;
               i = 0;
               v.clear();
               while(j<=6){
                        v.pb(start.board[i][j]);
                        i++;j++;
               }
               val += weight(v);
       }

       return val;
}

bool endgame(node start){
       int temp;
       temp = evaluate(start);
//cout<<temp<<endl;
        if(temp>=7000||temp<=-7000)
                return true;
        else
                return false;
}
bool possible(node start,int j){
       int i;
       for(i = 0;i <= 5;i++){
                 if(start.board[i][j]==0)
                           return true;
       }
       return false;
}
node makemove(node start,int j,int team,int disc){
       int i = 0,k;
       while(start.board[i][j]==0&&i<=5)
                 i++;
       i--;
       if(disc==5)
                 start.board[i][j] = team;
       else if(disc==4)
                 start.board[i][j] = 12;
       else if(disc==1){
                 for(j=0;j<=6;j++){
                           for(k=i-1;k>=0;k--){
                                    start.board[k+1][j] = start.board[k][j];
                           }
                           start.board[0][j] = 0;
                 }
       }
       else if(disc==2){
                 for(i=0;i<=5;i++)
                           start.board[i][j] = 0;
       }
       else if(disc==3){
                 start.board[i][j] = 0;
                 //clear all the neighbours
                 int t1[] = {0,0,-1,-1,-1,0,1,1,1};
                 int t2[] = {0,-1,-1,0,1,1,1,0,-1};
                 for(k=0;k<=8;k++){
                           int u1,v1;
                           u1 = t1[k] + i;
                           v1 = t2[k] + j;
                           if(u1>=0&&u1<=5&&v1>=0&&v1<=6){
                                    start.board[u1][v1] = 0;
                           }
                 }
                 vector<int> a;
                 if(j-1>=0){
                           k = i-2;
while(k>=0){
                                    if(start.board[k][j-1]!=0)
                                              a.pb(start.board[k][j-1]);
                                    start.board[k][j-1]=0;
                                    k--;
                          }
                          k = i;
                          if(i+1<=5)
                                    k = k+1;
                          int l = 0;
                          while(l<a.size()){
                                    start.board[k][j-1] = a[l];
                                    l++;k--;
                          }
                 }
                 a.clear();
                 if(j+1<=6){
                          k = i-2;
                          while(k>=0){
                                    if(start.board[k][j+1]!=0)
                                              a.pb(start.board[k][j+1]);
                                    start.board[k][j+1]=0;
                                    k--;
                          }
                          k = i;
                          if(i+1<=5)
                                    k = k+1;
                          int l = 0;
                          while(l<a.size()){
                                    start.board[k][j+1] = a[l];
                                    l++;k--;
                          }

                }
        }
        return start;
}
ret Min(node start,int level,int alpha,int beta,int disc);
ret Max(node start,int level,int alpha,int beta,int disc){
        //cout<<"Level ="<<level<<endl;
        //print(start);
        //system("pause");
        if(level == MAX_LEVEL||endgame(start)){
                  //cout<<"ho";
                  ret leaf;
                  leaf.val = evaluate(start);
                  leaf.move = -1;
                  //cout<<"Value = "<<leaf.val<<endl;
                  //system("pause");
                  return leaf;
        }
ret best;
        best.val = INT_MIN;
        for(int i = 0;i<7;i++){
                  if(possible(start,i)){
                           node newboard;
                           ret temp;
                           newboard = makemove(start,i,team_no,disc);
                           temp = Min(newboard,level+1,alpha,beta,5);
                           if(temp.val > best.val){
                                    best.val = temp.val;
                                    best.move = i;
                           }
                           alpha = max(alpha,temp.val);
                           if(alpha > beta)
                                    break;
                  }
        }
        return best;
}

ret Min(node start,int level,int alpha,int beta,int disc){
        //cout<<"Level ="<<level<<endl;
        //print(start);
        //system("pause");
        ret best;
        best.val = INT_MAX;
        for(int i = 0;i<7;i++){
                  if(possible(start,i)){
                           node newboard;
                           ret temp;
                           newboard = makemove(start,i,3 - team_no,5);
                           temp = Max(newboard,level+1,alpha,beta,5);
                           if(temp.val < best.val){
                                    best.val = temp.val;
                                    best.move = i;
                           }
                           beta = min(beta,temp.val);
                           if(alpha > beta)
                                    break;
                  }

        }
        return best;
}

int main()
{
  int i,j;

  ///////////////////////////////////////////////////READING
INPUT////////////////////////////////////////////////////////////////////////////
/*SECTION I
  Reading Board Number and Team no from the file*/
  ifstream fin;
  ofstream fout;

  fin.open("team_no.txt");
  fin>>team_no;
  fin.close();

  fin.open("board.txt");
  node start;
  for(i=5;i>=0;i--){
     for(j=0;j<7;j++){
       int token;
       fin>>token;
       start.board[i][j] = token;
     }
  }
  fin.close();
  /*end*/

//////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////


 //////////////////////////////////////////////////////MAKING THE DUAL COLOR
MOVE//////////////////////////////////////////////////////////
 int moves,column,disc;          //This will hold the column and disc of my move
 int r,c,n,d;
 fin.open("moves.txt");
 fin>>moves>>r>>c>>n>>d;
 if(moves==0)
        disc = 4;
 else
        disc = 5;
 /*if(moves==5)
        disc = 4;
 else if(moves==6&&d!=1)
        disc = 4;
 else if(moves==7&&d!=1)
        disc = 4;
 else if(moves==8&&d!=1)
        disc = 4;
 else
        disc = 5;*/
 fin.close();

  ret ans = Max(start,1,INT_MIN,INT_MAX,disc);

  if(disc==4&&ans.val <= -10000&&moves!=8){
               disc = 5;
ans = Max(start,1,INT_MIN,INT_MAX,disc);
 }
 else if(!(disc==4&&moves==8))
 {
 ////////////////////////////////////////////////////MAKING THE CLEAR ROW,COL,NEIGHBOUR
MOVES/////////////////////////////////////////////
                 //now the dual disc is used
                 if(ans.val <= -10000){
                          ret temp;
                          if(r!=1){
                          temp = Max(start,1,INT_MIN,INT_MAX,1);
                          if(temp.val > ans.val){
                                    disc = 1;
                                    ans.val = temp.val;
                                    ans.move = temp.move;
                          }
                          }

                      if(c!=1){
                      temp = Max(start,1,INT_MIN,INT_MAX,2);
                      if(temp.val > ans.val){
                                disc = 2;
                                ans.val = temp.val;
                                ans.move = temp.move;
                      }
                      }

                      if(n!=1){
                      temp = Max(start,1,INT_MIN,INT_MAX,3);
                      if(temp.val > ans.val){
                               disc = 3;
                               ans.val = temp.val;
                               ans.move = temp.move;
                      }
                      }
              }
              else
              {
                      ret temp;
                      if(r!=1){
                      temp = Max(start,1,INT_MIN,INT_MAX,1);
                      if(temp.val > ans.val && temp.val > 8000){
                                disc = 1;
                                ans.val = temp.val;
                                ans.move = temp.move;
                      }
                      }

                      if(c!=1){
                      temp = Max(start,1,INT_MIN,INT_MAX,2);
                      if(temp.val > ans.val && temp.val > 8000){
disc = 2;
                                ans.val = temp.val;
                                ans.move = temp.move;
                        }
                        }

                        if(n!=1){
                        temp = Max(start,1,INT_MIN,INT_MAX,3);
                        if(temp.val > ans.val && temp.val > 8000){
                                 disc = 3;
                                 ans.val = temp.val;
                                 ans.move = temp.move;
                        }
                        }
                }
  }

  ////////////////////////////////////MAKING THE FINAL
MOVE////////////////////////////////////////////////////////////////////////////////
  column = ans.move + 1;
  cout<<ans.move<<endl;
  fout.open("output.txt");
  cout<<disc<<" "<<column<<" "<<ans.val<<endl;
  fout<<disc<<" "<<column;
}

More Related Content

What's hot

Dzanan_Bajgoric_C2CUDA_MscThesis_Present
Dzanan_Bajgoric_C2CUDA_MscThesis_PresentDzanan_Bajgoric_C2CUDA_MscThesis_Present
Dzanan_Bajgoric_C2CUDA_MscThesis_Present
Džanan Bajgorić
 
Bowling Game Kata in C# Adapted
Bowling Game Kata in C# AdaptedBowling Game Kata in C# Adapted
Bowling Game Kata in C# Adapted
Mike Clement
 
Bowling Game Kata
Bowling Game KataBowling Game Kata
Bowling Game Kata
guest958d7
 
Java Puzzle
Java PuzzleJava Puzzle
Java Puzzle
SFilipp
 

What's hot (19)

The Ring programming language version 1.4.1 book - Part 3 of 31
The Ring programming language version 1.4.1 book - Part 3 of 31The Ring programming language version 1.4.1 book - Part 3 of 31
The Ring programming language version 1.4.1 book - Part 3 of 31
 
Reiterating JavaScript & Node.js iterators
Reiterating JavaScript & Node.js iteratorsReiterating JavaScript & Node.js iterators
Reiterating JavaScript & Node.js iterators
 
The Ring programming language version 1.10 book - Part 46 of 212
The Ring programming language version 1.10 book - Part 46 of 212The Ring programming language version 1.10 book - Part 46 of 212
The Ring programming language version 1.10 book - Part 46 of 212
 
Racing To Win: Using Race Conditions to Build Correct and Concurrent Software
Racing To Win: Using Race Conditions to Build Correct and Concurrent SoftwareRacing To Win: Using Race Conditions to Build Correct and Concurrent Software
Racing To Win: Using Race Conditions to Build Correct and Concurrent Software
 
Dzanan_Bajgoric_C2CUDA_MscThesis_Present
Dzanan_Bajgoric_C2CUDA_MscThesis_PresentDzanan_Bajgoric_C2CUDA_MscThesis_Present
Dzanan_Bajgoric_C2CUDA_MscThesis_Present
 
Introduction to CNN with Application to Object Recognition
Introduction to CNN with Application to Object RecognitionIntroduction to CNN with Application to Object Recognition
Introduction to CNN with Application to Object Recognition
 
Legacy projects: how to win the race
Legacy projects: how to win the raceLegacy projects: how to win the race
Legacy projects: how to win the race
 
Bowling Game Kata by Robert C. Martin
Bowling Game Kata by Robert C. MartinBowling Game Kata by Robert C. Martin
Bowling Game Kata by Robert C. Martin
 
The Ring programming language version 1.5.1 book - Part 51 of 180
The Ring programming language version 1.5.1 book - Part 51 of 180The Ring programming language version 1.5.1 book - Part 51 of 180
The Ring programming language version 1.5.1 book - Part 51 of 180
 
Bowling Game Kata in C# Adapted
Bowling Game Kata in C# AdaptedBowling Game Kata in C# Adapted
Bowling Game Kata in C# Adapted
 
Bowling Game Kata C#
Bowling Game Kata C#Bowling Game Kata C#
Bowling Game Kata C#
 
Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012
 
The Ring programming language version 1.4.1 book - Part 17 of 31
The Ring programming language version 1.4.1 book - Part 17 of 31The Ring programming language version 1.4.1 book - Part 17 of 31
The Ring programming language version 1.4.1 book - Part 17 of 31
 
Aa
AaAa
Aa
 
The Ring programming language version 1.8 book - Part 59 of 202
The Ring programming language version 1.8 book - Part 59 of 202The Ring programming language version 1.8 book - Part 59 of 202
The Ring programming language version 1.8 book - Part 59 of 202
 
Bowling Game Kata
Bowling Game KataBowling Game Kata
Bowling Game Kata
 
Model Based Fault Detection, Identification and Accommodation in Antilock Bra...
Model Based Fault Detection, Identification and Accommodation in Antilock Bra...Model Based Fault Detection, Identification and Accommodation in Antilock Bra...
Model Based Fault Detection, Identification and Accommodation in Antilock Bra...
 
Monadologie
MonadologieMonadologie
Monadologie
 
Java Puzzle
Java PuzzleJava Puzzle
Java Puzzle
 

Similar to Connect 4

The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
fonecomp
 
i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
poblettesedanoree498
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdf
calderoncasto9163
 
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
PiersRCoThomsonw
 
include ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfinclude ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdf
contact32
 
Project_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_endsProject_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_ends
? ?
 
Artificial intelligence
Artificial intelligenceArtificial intelligence
Artificial intelligence
Aditya Sharma
 
Psimd open64 workshop_2012-new
Psimd open64 workshop_2012-newPsimd open64 workshop_2012-new
Psimd open64 workshop_2012-new
dibyendu_das0708
 

Similar to Connect 4 (20)

Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Include
IncludeInclude
Include
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
 
i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
 
week-17x
week-17xweek-17x
week-17x
 
week-18x
week-18xweek-18x
week-18x
 
Proga 0622
Proga 0622Proga 0622
Proga 0622
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdf
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscation
 
C# Assignmet Help
C# Assignmet HelpC# Assignmet Help
C# Assignmet Help
 
Snake report ROHIT MALAV
Snake report ROHIT MALAVSnake report ROHIT MALAV
Snake report ROHIT MALAV
 
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
 
include ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfinclude ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdf
 
Project_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_endsProject_Euler_No_104_Pandigital_Fibonacci_ends
Project_Euler_No_104_Pandigital_Fibonacci_ends
 
Artificial intelligence
Artificial intelligenceArtificial intelligence
Artificial intelligence
 
Please implement in Java. comments would be appreciated 5.pdf
Please implement in Java. comments would be appreciated 5.pdfPlease implement in Java. comments would be appreciated 5.pdf
Please implement in Java. comments would be appreciated 5.pdf
 
Psimd open64 workshop_2012-new
Psimd open64 workshop_2012-newPsimd open64 workshop_2012-new
Psimd open64 workshop_2012-new
 
PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~
PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~
PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

Connect 4

  • 1. #include<iostream> #include<string> #include<cstdio> #include<map> #include<algorithm> #include<cmath> #include<vector> #include<sstream> #include<stack> #include<queue> #include<cstring> #include<fstream> #include<climits> #define pb push_back #define LL long long #define OUTPUT_TO_FILE 1 #define s(n) scanf("%d",&n) #define sl(n) scanf("%lld",&n) #define sf(n) scanf("%lf",&n) #define ss(n) scanf("%s",n) #define MAX_LEVEL 5 using namespace std; class node { public: int board[6][7]; }; typedef struct { int val,move; }ret; int team_no; void print(node start){ int i,j; for(i=0;i<6;i++){ for(j=0;j<7;j++) cout<<start.board[i][j]<<" "; cout<<endl; } } int weight(vector<int> v){ if(v.size()<=3) return 0; int i,j; int Weights[] = { 1, 5, 100, 10000,2, 6, 200, 15000 }; int score = 0; for(i=0;i<v.size()-3;i++){
  • 2. int c = 0; int p = 0; int dual = 0; for (j = 0; j < 4; j++){ if (v[i + j] == team_no) c++; //TODO Make sure that it looks at it's own identity else if (v[i + j] ==3 - team_no) p++; else if (v[i + j] == 12) dual++; } if ((c > 0) && (p == 0)) { c += dual; //Computer opportunity if (c == 4) return Weights[3]; //Win score += ((c/3)*Weights[2]) + ((c/2)*Weights[1]) + Weights[0]; } else if ((c == 0) && (p > 0)) { p += dual; //Player opportunity if (p == 4) return -1*Weights[7]; //Win score -= ((p / 3) * Weights[6]) + ((p / 2) * Weights[5]) + Weights[4]; } } return score; } int evaluate(node start){ int val = 0; int i,j,k; //rows vector<int> v; for(i=5;i>=0;i--){ v.clear(); for(j=0;j<=6;j++) v.pb(start.board[i][j]); val += weight(v); } //cout<<val<<" after rows"<<endl; //columns for(j=0;j<=6;j++){ v.clear(); for(i=0;i<=5;i++){ v.pb(start.board[i][j]); } val += weight(v); } //cout<<val<<" after columns"<<endl;
  • 3. //daigonals for(k=0;k<=5;k++){ i = k; j = 0; v.clear(); while(i>=0){ v.pb(start.board[i][j]); i--;j++; } val += weight(v); } for(k=1;k<=6;k++){ i = 5;j = k; v.clear(); while(j<=6){ v.pb(start.board[i][j]); j++;i--; } val += weight(v); } //cout<<val<<" after diagonals"<<endl; //2nd set of diagonals for(k=5;k>=0;k--){ i = k; j = 0; v.clear(); while(i<=5){ v.pb(start.board[i][j]); i++;j++; } val += weight(v); } for(k=1;k<=6;k++){ j = k; i = 0; v.clear(); while(j<=6){ v.pb(start.board[i][j]); i++;j++; } val += weight(v); } return val; } bool endgame(node start){ int temp; temp = evaluate(start);
  • 4. //cout<<temp<<endl; if(temp>=7000||temp<=-7000) return true; else return false; } bool possible(node start,int j){ int i; for(i = 0;i <= 5;i++){ if(start.board[i][j]==0) return true; } return false; } node makemove(node start,int j,int team,int disc){ int i = 0,k; while(start.board[i][j]==0&&i<=5) i++; i--; if(disc==5) start.board[i][j] = team; else if(disc==4) start.board[i][j] = 12; else if(disc==1){ for(j=0;j<=6;j++){ for(k=i-1;k>=0;k--){ start.board[k+1][j] = start.board[k][j]; } start.board[0][j] = 0; } } else if(disc==2){ for(i=0;i<=5;i++) start.board[i][j] = 0; } else if(disc==3){ start.board[i][j] = 0; //clear all the neighbours int t1[] = {0,0,-1,-1,-1,0,1,1,1}; int t2[] = {0,-1,-1,0,1,1,1,0,-1}; for(k=0;k<=8;k++){ int u1,v1; u1 = t1[k] + i; v1 = t2[k] + j; if(u1>=0&&u1<=5&&v1>=0&&v1<=6){ start.board[u1][v1] = 0; } } vector<int> a; if(j-1>=0){ k = i-2;
  • 5. while(k>=0){ if(start.board[k][j-1]!=0) a.pb(start.board[k][j-1]); start.board[k][j-1]=0; k--; } k = i; if(i+1<=5) k = k+1; int l = 0; while(l<a.size()){ start.board[k][j-1] = a[l]; l++;k--; } } a.clear(); if(j+1<=6){ k = i-2; while(k>=0){ if(start.board[k][j+1]!=0) a.pb(start.board[k][j+1]); start.board[k][j+1]=0; k--; } k = i; if(i+1<=5) k = k+1; int l = 0; while(l<a.size()){ start.board[k][j+1] = a[l]; l++;k--; } } } return start; } ret Min(node start,int level,int alpha,int beta,int disc); ret Max(node start,int level,int alpha,int beta,int disc){ //cout<<"Level ="<<level<<endl; //print(start); //system("pause"); if(level == MAX_LEVEL||endgame(start)){ //cout<<"ho"; ret leaf; leaf.val = evaluate(start); leaf.move = -1; //cout<<"Value = "<<leaf.val<<endl; //system("pause"); return leaf; }
  • 6. ret best; best.val = INT_MIN; for(int i = 0;i<7;i++){ if(possible(start,i)){ node newboard; ret temp; newboard = makemove(start,i,team_no,disc); temp = Min(newboard,level+1,alpha,beta,5); if(temp.val > best.val){ best.val = temp.val; best.move = i; } alpha = max(alpha,temp.val); if(alpha > beta) break; } } return best; } ret Min(node start,int level,int alpha,int beta,int disc){ //cout<<"Level ="<<level<<endl; //print(start); //system("pause"); ret best; best.val = INT_MAX; for(int i = 0;i<7;i++){ if(possible(start,i)){ node newboard; ret temp; newboard = makemove(start,i,3 - team_no,5); temp = Max(newboard,level+1,alpha,beta,5); if(temp.val < best.val){ best.val = temp.val; best.move = i; } beta = min(beta,temp.val); if(alpha > beta) break; } } return best; } int main() { int i,j; ///////////////////////////////////////////////////READING INPUT////////////////////////////////////////////////////////////////////////////
  • 7. /*SECTION I Reading Board Number and Team no from the file*/ ifstream fin; ofstream fout; fin.open("team_no.txt"); fin>>team_no; fin.close(); fin.open("board.txt"); node start; for(i=5;i>=0;i--){ for(j=0;j<7;j++){ int token; fin>>token; start.board[i][j] = token; } } fin.close(); /*end*/ ////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////// //////////////////////////////////////////////////////MAKING THE DUAL COLOR MOVE////////////////////////////////////////////////////////// int moves,column,disc; //This will hold the column and disc of my move int r,c,n,d; fin.open("moves.txt"); fin>>moves>>r>>c>>n>>d; if(moves==0) disc = 4; else disc = 5; /*if(moves==5) disc = 4; else if(moves==6&&d!=1) disc = 4; else if(moves==7&&d!=1) disc = 4; else if(moves==8&&d!=1) disc = 4; else disc = 5;*/ fin.close(); ret ans = Max(start,1,INT_MIN,INT_MAX,disc); if(disc==4&&ans.val <= -10000&&moves!=8){ disc = 5;
  • 8. ans = Max(start,1,INT_MIN,INT_MAX,disc); } else if(!(disc==4&&moves==8)) { ////////////////////////////////////////////////////MAKING THE CLEAR ROW,COL,NEIGHBOUR MOVES///////////////////////////////////////////// //now the dual disc is used if(ans.val <= -10000){ ret temp; if(r!=1){ temp = Max(start,1,INT_MIN,INT_MAX,1); if(temp.val > ans.val){ disc = 1; ans.val = temp.val; ans.move = temp.move; } } if(c!=1){ temp = Max(start,1,INT_MIN,INT_MAX,2); if(temp.val > ans.val){ disc = 2; ans.val = temp.val; ans.move = temp.move; } } if(n!=1){ temp = Max(start,1,INT_MIN,INT_MAX,3); if(temp.val > ans.val){ disc = 3; ans.val = temp.val; ans.move = temp.move; } } } else { ret temp; if(r!=1){ temp = Max(start,1,INT_MIN,INT_MAX,1); if(temp.val > ans.val && temp.val > 8000){ disc = 1; ans.val = temp.val; ans.move = temp.move; } } if(c!=1){ temp = Max(start,1,INT_MIN,INT_MAX,2); if(temp.val > ans.val && temp.val > 8000){
  • 9. disc = 2; ans.val = temp.val; ans.move = temp.move; } } if(n!=1){ temp = Max(start,1,INT_MIN,INT_MAX,3); if(temp.val > ans.val && temp.val > 8000){ disc = 3; ans.val = temp.val; ans.move = temp.move; } } } } ////////////////////////////////////MAKING THE FINAL MOVE//////////////////////////////////////////////////////////////////////////////// column = ans.move + 1; cout<<ans.move<<endl; fout.open("output.txt"); cout<<disc<<" "<<column<<" "<<ans.val<<endl; fout<<disc<<" "<<column; }