SlideShare a Scribd company logo
1 of 18
Download to read offline
#include <stdio.h>

#include <iostream>

#include <time.h>

#include <stdlib.h>

#include <fstream>

#include <pthread.h>

#include <unistd.h>

#include <signal.h>



using namespace std;



#define CLEAR_ROW 1

#define CLEAR_COLUMN 2

#define CLEAR_FIRST_NEIGHBOURS 3

#define DUAL_COLOUR 4

#define NORMAL 5

#define PLAYER_1 1

#define PLAYER_2 2



/* global decalarations */

int turn = 0;/* this number will record whose turn is it */

int moves1=0,moves2=0;/* for tracking the number of moves pklayed yet by participants*/

int
spmove11=0,spmove21=0,spmove31=0,spmove41=0,spmove12=0,spmove22=0,spmove32=0,spmov
e42=0;/* flags to keep track the special moves of players */

int move_type=0,move=0;

int board[7][6] = {0};
/* functions used */

int legal_move();

int add_disk();

int check_win(int team);

int right_up(int x,int y);

int left_up(int x,int y);

int vertical(int x,int y);

int horizontal(int x,int y);

int write_board();

int draw_board();

int first_turn();

int board_full();

int call_code();

int move_counter();



int main()

{

    first_turn();/* decides first turn */

    write_board();/* writes board to txt file */

    draw_board();/* call graphics function to draw the board */

    int match_over = 0;/* flag telling match is over or not */

    while(1)/*starting the infinite loop*/

    {

        write_board();

        match_over = board_full();/*checking if board is full*/

        if(match_over==1)

        break;
/*calling the participant code*/

match_over = call_code();

if(match_over == 1)

break;



else/* match_over == 0 *//* match is not over */

{

    move_counter();

    turn++;

    draw_board();

    int check1 = check_win(1);/* checking winner */

    int check2 = check_win(2);

    if(check1 == 1 && check2 == 1)

    {

        printf("its a drawn");

        break;

    }

    else if(check1 == 1)

    {

        printf("player 1 winsn");

        break;

    }

    else if(check2 == 1)

    {

        printf("player 2 winsn");

        break;

    }
}

    }

    return 0;

}



int legal_move()

{

    if(move > 7 || move < 1)/*invalid column */

    return 0;

    else if(board[move-1][0] != 0)/* top most position of column is not empty*/

    return 0;

    if(turn%2 == 0)/* for player 2 */

    {

        if(move_type == CLEAR_ROW && spmove12 == 1)

        return 0;

        else if(move_type == CLEAR_COLUMN && spmove22 == 1)

        return 0;

        else if(move_type == CLEAR_FIRST_NEIGHBOURS && spmove32 == 1)

        return 0;

        else if(move_type == DUAL_COLOUR && spmove42 == 1)

        return 0;

    else if(move_type != DUAL_COLOUR && moves2 == 9 && spmove42 == 0)/* move type 4 means
dual color block */

        return 0;

    }

    else

    {

        if(move_type == CLEAR_ROW && spmove11 == 1)
return 0;

        else if(move_type == CLEAR_COLUMN && spmove21 == 1)

        return 0;

        else if(move_type == CLEAR_FIRST_NEIGHBOURS && spmove31 == 1)

        return 0;

        else if(move_type == DUAL_COLOUR && spmove41 == 1)

        return 0;

        else if(move_type != DUAL_COLOUR && moves1 == 9 && spmove41 == 0)

        return 0;

    }

    return 1;

}



int add_disk()

{

    for(int y = 0;y<6;y++)

    {

        if(board[move-1][y+1] != 0 || y+1>5)/* bottommost position in that column */

        {

            if(move_type == CLEAR_ROW)/* 1 means detroying complete row */

            {

                for(int x=0;x<7;x++)

                {

                    for(int temp = y;temp>=0;temp--)

                    board[x][temp] = board[x][temp-1];

                    board[x][0] = 0;

                }
}

      else if(move_type == CLEAR_COLUMN)/* destroying complete column */

      {

          for(int temp = 0;temp<7;temp++)

          board[move-1][temp] = 0;

      }

       else if(move_type == CLEAR_FIRST_NEIGHBOURS)/* destroying neighbour block in every
direction */

      {

          for(int temp = move-2;temp <= move;temp++)

          {

              if(temp<0 || temp>6)

              continue;

              if(y+1<6)

              board[temp][y+1]=0;

              board[temp][y]=0;

              if(y-1>=0)

              board[temp][y-1]=0;

              /* now need to shift zeroes */

              for(int count=0;count < 3;count++)

              {

                  for(int change = 5;change > 0;change--)

                  {

                      if(board[temp][change]==0)

                      {

                          board[temp][change] = board[temp][change-1];

                          board[temp][change-1] = 0;

                      }
}

                    }

                }

            }

            else if(move_type == DUAL_COLOUR)/* dual color block */

            {

            board[move-1][y] = 12;

            }

            else/* normal block */

            {

                if(turn % 2 == 1)

                board[move-1][y] = 1;

                else

                board[move-1][y] = 2;

            }

            break;

        }

    }

    return 0;

}



int check_win(int team)

{

    int result=0,temp=0;

    for(int y=5;y>=0;y--)

    for(int x=0;x<7;x++)

    {
if(board[x][y] == 0)/* block is empty */

        continue;

        if(board[x][y] == team || board[x][y] == 12)

        {

            result = right_up(x,y);

            if(result!=1)

            result = left_up(x,y);

            if(result!=1)

            result = vertical(x,y);

            if(result!=1)

            result = horizontal(x,y);

        }

        if(result == 1)

        return 1;

    }

    return 0;

}



int right_up(int x,int y)

{

    if(y<3 || x>3)/* not possible */

    return 0;

    else if(x-1>=0 && y+1<6 && (board[x-1][y+1] == board[x][y] || board[x-1][y+1] == 12))

    return 0;/* condition was checked at x-1 and y+1 already */

    else

    {

        int count=1;
for(int temp = 1;temp<4;temp++)

        {

            if(board[x+temp][y-temp] == board[x][y] || board[x+temp][y-temp] == 12)

            count++;

        }

        if(count == 4)

        return 1;

    }

    return 0;

}



int left_up(int x,int y)

{

    if(y<3 || x<3)/* not possible */

    return 0;

    else if(x+1<7 && y+1<6 && (board[x+1][y+1] == board[x][y] || board[x+1][y+1] == 12))

    return 0;/* condition was checked at x+1 and y+1 already */

    else

    {

        int count=1;

        for(int temp = 1;temp<4;temp++)

        {

            if(board[x-temp][y-temp] == board[x][y])

            count++;

        }

        if(count == 4)

        return 1;
}

    return 0;

}



int vertical(int x,int y)

{

    if(y<3)

    return 0;

    else if( y+1 < 6 && (board[x][y+1] == board[x][y] || board[x][y+1] == 12))

    return 0;

    int count = 1;

    for(int temp=1;temp<4;temp++)

    {

        if(board[x][y-temp] == board[x][y] || board[x][y-temp] == 12 )

        count++;

    }

    if(count == 4)

    return 1;

    return 0;

}



int horizontal(int x,int y)

{

    if(x>3)

    return 0;

    else if( x-1>=0 && (board[x-1][y] == board[x][y] || board[x-1][y]==12))

    return 0;
int count=1;

    for(int temp=1;temp<4;temp++)

    {

        if(board[x+temp][y] == board[x][y] || board[x+temp][y] == 12 )

        count++;

    }

    if(count == 4)

    return 1;

    return 0;

}



int write_board()

{

    ofstream pt ("board.txt");

    for(int y = 5 ; y>=0 ; y--)

    {

        for(int x = 0 ; x < 7 ;x++)

        {

            pt<<board[x][y]<<" ";

        }

    }

    pt.close();

    return 0;

}



int draw_board()

{
printf("n");

    for(int y = 0 ; y<6;y++)

    {

        for(int x = 0 ; x < 7 ; x++)



        printf("%d ",board[x][y]);

        printf("n");

    }

    printf("nn");

    return 0;

}



int first_turn()

{

    srand ( time(NULL) );

    /* initialization to generate a random number*/

    /* deciding the first player whose turn is it */

    ifstream pt ("turn.txt");

    /* opening the file in which the player who got the first turn is written */

    if(pt == NULL)/* no such file exists*/

    {

        turn = rand()%2 + 1;

        /* generating a random number to decide the first player to play */

        ofstream ptr ("turn.txt");

        ptr<<turn;

        ptr.close();

    }
else/* file exists */

    {

        pt>>turn;

        pt.close();

        if(turn%2==0)

                turn = 1;

                else

                turn = 2;

        ofstream file ("turn.txt");

        file<<turn;

        file.close();

    }

}



int board_full()

{

    for(int y=0;y<6;y++)

        {

            int empty=0;/* flag for board empty */

            for(int x=0;x<7;x++)

            {

            if(board[x][y]==0)

                {

                empty = 1;

                break;

                }

            }
if(empty==1)

          return 0;

          else/*board is full*/

             {

             printf("this match is a drawn");

             return 1;

             }

      }

}

int call_code()

{

    for(int temp=0;temp<=3;temp++)/* at max 3 times */

     {

                 remove("output.txt");/* deleting the output.txt file */

          ofstream ptr ("moves.txt");/* writing moves played yet and powers used by player*/

          ifstream abc ("turn.txt");

                 int record;

                 abc>>record;

                 if(record%2==1)

          ptr<<(turn-1)/2<<" ";

                 else

                 ptr<<turn/2-1<<" ";

          if(turn%2==0)

          ptr<<spmove12<<" "<<spmove22<<" "<<spmove32<<" "<<spmove42;

          else

          ptr<<spmove11<<" "<<spmove21<<" "<<spmove31<<" "<<spmove41;

          ptr.close();
ofstream file ("team_no.txt");/* creating the team_no.txt file */



    if(turn%2==0)

    file<<"2";

    else

    file<<"1";

    file.close();



    ofstream pt ("opponent_move.txt");



    if(turn%2==0)

    pt<<spmove11<<" "<<spmove21<<" "<<spmove31<<" "<<spmove41;

    else

    pt<<spmove12<<" "<<spmove22<<" "<<spmove32<<" "<<spmove42;

    pt.close();



int legal = 1;

if(temp == 3)

{

    printf("game overn");

    if(turn%2 == 1)

    printf("player 2 winsn");

    else

    printf("player 1 winsn");

    return 1;

}
pid_t pid;

              pid = fork();/* to start a child process*/

              if(pid == 0)/* child process */

              {

                     // team codes will be called from here

                     // respective commands according to the language of the code will be used

                 // for eg to run c++ program named "abc.cpp" first copy the code in the same
directory as this code then write

                     // system("g++ abc.cpp -o abc");

                     // system("./abc");

          if(turn%2 == 1)

          {

                        /*call first player code */

                     // write command (system function) here to call code

          }

          else

          {

              /* call second participant code*/

                     // write command (system function) here to call code

          }

                  exit(0);/* exit the child process */

      }

              else

              {/* main program sleeps for 2s*/

              sleep(2);/* execution time limit */

              kill(pid,SIGKILL);

              }

      ifstream out ("output.txt");/*reading output*/
if(out == NULL)

        continue;

        else

        {

            out>>move_type;

            out>>move;

            out.close();

        }

        int check = legal_move();/* check if move is legal */

        if(check == 0)/* move is illegal */

        continue;

        if(legal == 1)/* this means code reaches succesfully i.e. move is legal */

        break;

    }

    return 0;

}



int move_counter()

{

        add_disk();

        if(turn%2 == 0)

        moves2++;

        else

        moves1++;

        if(move_type==1)

        {

            if(turn%2==0)
spmove12++;

        else

        spmove11++;

    }

    else if(move_type==2)

    {

        if(turn%2==0)

        spmove22++;

        else

        spmove21++;

    }

    else if(move_type==3)

    {

        if(turn%2==0)

        spmove32++;

        else

        spmove31++;

    }

    else if(move_type==4)

    {

        if(turn%2==0)

        spmove42++;

        else

        spmove41++;

    }

}

More Related Content

What's hot

The Ring programming language version 1.10 book - Part 38 of 212
The Ring programming language version 1.10 book - Part 38 of 212The Ring programming language version 1.10 book - Part 38 of 212
The Ring programming language version 1.10 book - Part 38 of 212Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 79 of 184
The Ring programming language version 1.5.3 book - Part 79 of 184The Ring programming language version 1.5.3 book - Part 79 of 184
The Ring programming language version 1.5.3 book - Part 79 of 184Mahmoud Samir Fayed
 
The Ring programming language version 1.5 book - Part 5 of 31
The Ring programming language version 1.5 book - Part 5 of 31The Ring programming language version 1.5 book - Part 5 of 31
The Ring programming language version 1.5 book - Part 5 of 31Mahmoud Samir Fayed
 
[WELC] 21. I’m Changing the Same Code All Over the Place
[WELC] 21. I’m Changing the Same Code All Over the Place[WELC] 21. I’m Changing the Same Code All Over the Place
[WELC] 21. I’m Changing the Same Code All Over the Place종빈 오
 
統計的学習の基礎 4章 前半
統計的学習の基礎 4章 前半統計的学習の基礎 4章 前半
統計的学習の基礎 4章 前半Ken'ichi Matsui
 
The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189Mahmoud Samir Fayed
 
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 180Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212Mahmoud Samir Fayed
 
AST: threats and opportunities
AST: threats and opportunitiesAST: threats and opportunities
AST: threats and opportunitiesAlexander Lifanov
 
The Ring programming language version 1.4 book - Part 7 of 30
The Ring programming language version 1.4 book - Part 7 of 30The Ring programming language version 1.4 book - Part 7 of 30
The Ring programming language version 1.4 book - Part 7 of 30Mahmoud Samir Fayed
 
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. MartinLalit Kale
 
Bowling Game Kata in C# Adapted
Bowling Game Kata in C# AdaptedBowling Game Kata in C# Adapted
Bowling Game Kata in C# AdaptedMike Clement
 
Bowling Game Kata C#
Bowling Game Kata C#Bowling Game Kata C#
Bowling Game Kata C#Dan Stewart
 
The Ring programming language version 1.9 book - Part 37 of 210
The Ring programming language version 1.9 book - Part 37 of 210The Ring programming language version 1.9 book - Part 37 of 210
The Ring programming language version 1.9 book - Part 37 of 210Mahmoud Samir Fayed
 
Games, AI, and Research - Part 2 Training (FightingICE AI Programming)
Games, AI, and Research - Part 2 Training (FightingICE AI Programming)Games, AI, and Research - Part 2 Training (FightingICE AI Programming)
Games, AI, and Research - Part 2 Training (FightingICE AI Programming)Pujana Paliyawan
 
「ベータ分布の謎に迫る」第6回 プログラマのための数学勉強会 LT資料
「ベータ分布の謎に迫る」第6回 プログラマのための数学勉強会 LT資料「ベータ分布の謎に迫る」第6回 プログラマのための数学勉強会 LT資料
「ベータ分布の謎に迫る」第6回 プログラマのための数学勉強会 LT資料Ken'ichi Matsui
 

What's hot (20)

The Ring programming language version 1.10 book - Part 38 of 212
The Ring programming language version 1.10 book - Part 38 of 212The Ring programming language version 1.10 book - Part 38 of 212
The Ring programming language version 1.10 book - Part 38 of 212
 
The Ring programming language version 1.5.3 book - Part 79 of 184
The Ring programming language version 1.5.3 book - Part 79 of 184The Ring programming language version 1.5.3 book - Part 79 of 184
The Ring programming language version 1.5.3 book - Part 79 of 184
 
The Ring programming language version 1.5 book - Part 5 of 31
The Ring programming language version 1.5 book - Part 5 of 31The Ring programming language version 1.5 book - Part 5 of 31
The Ring programming language version 1.5 book - Part 5 of 31
 
[WELC] 21. I’m Changing the Same Code All Over the Place
[WELC] 21. I’m Changing the Same Code All Over the Place[WELC] 21. I’m Changing the Same Code All Over the Place
[WELC] 21. I’m Changing the Same Code All Over the Place
 
統計的学習の基礎 4章 前半
統計的学習の基礎 4章 前半統計的学習の基礎 4章 前半
統計的学習の基礎 4章 前半
 
The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212
 
ES6, WTF?
ES6, WTF?ES6, WTF?
ES6, WTF?
 
The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189
 
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
 
The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212
 
AST: threats and opportunities
AST: threats and opportunitiesAST: threats and opportunities
AST: threats and opportunities
 
The Ring programming language version 1.4 book - Part 7 of 30
The Ring programming language version 1.4 book - Part 7 of 30The Ring programming language version 1.4 book - Part 7 of 30
The Ring programming language version 1.4 book - Part 7 of 30
 
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
 
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#
 
The Ring programming language version 1.9 book - Part 37 of 210
The Ring programming language version 1.9 book - Part 37 of 210The Ring programming language version 1.9 book - Part 37 of 210
The Ring programming language version 1.9 book - Part 37 of 210
 
Games, AI, and Research - Part 2 Training (FightingICE AI Programming)
Games, AI, and Research - Part 2 Training (FightingICE AI Programming)Games, AI, and Research - Part 2 Training (FightingICE AI Programming)
Games, AI, and Research - Part 2 Training (FightingICE AI Programming)
 
Pemrograman visual
Pemrograman visualPemrograman visual
Pemrograman visual
 
Lập trình Python cơ bản
Lập trình Python cơ bảnLập trình Python cơ bản
Lập trình Python cơ bản
 
「ベータ分布の謎に迫る」第6回 プログラマのための数学勉強会 LT資料
「ベータ分布の謎に迫る」第6回 プログラマのための数学勉強会 LT資料「ベータ分布の謎に迫る」第6回 プログラマのための数学勉強会 LT資料
「ベータ分布の謎に迫る」第6回 プログラマのための数学勉強会 LT資料
 

Similar to AI CHALLENGE ADMIN

In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfanjandavid
 
#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdfsinghanubhav1234
 
19012011102_Nayan Oza_Practical-7_AI.pdf
19012011102_Nayan Oza_Practical-7_AI.pdf19012011102_Nayan Oza_Practical-7_AI.pdf
19012011102_Nayan Oza_Practical-7_AI.pdfNayanOza
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfasif1401
 
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdf
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdfNeed to make a ReversiOthello Board game in JAVAThe board size ca.pdf
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdfflashfashioncasualwe
 
package Assignment;import java.util.;public class assignment .pdf
package Assignment;import java.util.;public class assignment .pdfpackage Assignment;import java.util.;public class assignment .pdf
package Assignment;import java.util.;public class assignment .pdfnoelbuddy
 
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.pdffonecomp
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語ikdysfm
 
#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).docxPiersRCoThomsonw
 
write the TODO part of the program.docx
write the TODO part of the program.docxwrite the TODO part of the program.docx
write the TODO part of the program.docxannetnash8266
 
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 .pdfpoblettesedanoree498
 
ARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lectureARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lectureanishgoel
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsKandarp Tiwari
 
Sorting programs
Sorting programsSorting programs
Sorting programsVarun Garg
 
#includeiostream #includefstreamusing namespace std; glo.pdf
#includeiostream #includefstreamusing namespace std; glo.pdf#includeiostream #includefstreamusing namespace std; glo.pdf
#includeiostream #includefstreamusing namespace std; glo.pdfkrram1989
 
Create a C program that implements The Game of Life cellular auto.pdf
Create a C program that implements The Game of Life cellular auto.pdfCreate a C program that implements The Game of Life cellular auto.pdf
Create a C program that implements The Game of Life cellular auto.pdfarihantsherwani
 
Here is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdfHere is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdfanithareadymade
 
import tio.;class TicTacToe {static final int EMPTY = 0;stati.pdf
import tio.;class TicTacToe {static final int EMPTY = 0;stati.pdfimport tio.;class TicTacToe {static final int EMPTY = 0;stati.pdf
import tio.;class TicTacToe {static final int EMPTY = 0;stati.pdfpreetajain
 

Similar to AI CHALLENGE ADMIN (20)

Include
IncludeInclude
Include
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
 
#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf
 
19012011102_Nayan Oza_Practical-7_AI.pdf
19012011102_Nayan Oza_Practical-7_AI.pdf19012011102_Nayan Oza_Practical-7_AI.pdf
19012011102_Nayan Oza_Practical-7_AI.pdf
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
 
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdf
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdfNeed to make a ReversiOthello Board game in JAVAThe board size ca.pdf
Need to make a ReversiOthello Board game in JAVAThe board size ca.pdf
 
tetris
tetristetris
tetris
 
package Assignment;import java.util.;public class assignment .pdf
package Assignment;import java.util.;public class assignment .pdfpackage Assignment;import java.util.;public class assignment .pdf
package Assignment;import java.util.;public class assignment .pdf
 
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
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
#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
 
write the TODO part of the program.docx
write the TODO part of the program.docxwrite the TODO part of the program.docx
write the TODO part of the program.docx
 
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
 
ARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lectureARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lecture
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
#includeiostream #includefstreamusing namespace std; glo.pdf
#includeiostream #includefstreamusing namespace std; glo.pdf#includeiostream #includefstreamusing namespace std; glo.pdf
#includeiostream #includefstreamusing namespace std; glo.pdf
 
Create a C program that implements The Game of Life cellular auto.pdf
Create a C program that implements The Game of Life cellular auto.pdfCreate a C program that implements The Game of Life cellular auto.pdf
Create a C program that implements The Game of Life cellular auto.pdf
 
Here is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdfHere is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdf
 
import tio.;class TicTacToe {static final int EMPTY = 0;stati.pdf
import tio.;class TicTacToe {static final int EMPTY = 0;stati.pdfimport tio.;class TicTacToe {static final int EMPTY = 0;stati.pdf
import tio.;class TicTacToe {static final int EMPTY = 0;stati.pdf
 

Recently uploaded

Digital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdfDigital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdfJos Voskuil
 
Traction part 2 - EOS Model JAX Bridges.
Traction part 2 - EOS Model JAX Bridges.Traction part 2 - EOS Model JAX Bridges.
Traction part 2 - EOS Model JAX Bridges.Anamaria Contreras
 
Market Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 EditionMarket Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 EditionMintel Group
 
PSCC - Capability Statement Presentation
PSCC - Capability Statement PresentationPSCC - Capability Statement Presentation
PSCC - Capability Statement PresentationAnamaria Contreras
 
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607dollysharma2066
 
8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCR8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCRashishs7044
 
APRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdfAPRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdfRbc Rbcua
 
Organizational Structure Running A Successful Business
Organizational Structure Running A Successful BusinessOrganizational Structure Running A Successful Business
Organizational Structure Running A Successful BusinessSeta Wicaksana
 
MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?Olivia Kresic
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchirictsugar
 
Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737Riya Pathan
 
Financial-Statement-Analysis-of-Coca-cola-Company.pptx
Financial-Statement-Analysis-of-Coca-cola-Company.pptxFinancial-Statement-Analysis-of-Coca-cola-Company.pptx
Financial-Statement-Analysis-of-Coca-cola-Company.pptxsaniyaimamuddin
 
Call Us 📲8800102216📞 Call Girls In DLF City Gurgaon
Call Us 📲8800102216📞 Call Girls In DLF City GurgaonCall Us 📲8800102216📞 Call Girls In DLF City Gurgaon
Call Us 📲8800102216📞 Call Girls In DLF City Gurgaoncallgirls2057
 
Kenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith PereraKenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith Pereraictsugar
 
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCRashishs7044
 
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...ictsugar
 
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCRashishs7044
 
Investment in The Coconut Industry by Nancy Cheruiyot
Investment in The Coconut Industry by Nancy CheruiyotInvestment in The Coconut Industry by Nancy Cheruiyot
Investment in The Coconut Industry by Nancy Cheruiyotictsugar
 

Recently uploaded (20)

Digital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdfDigital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdf
 
Traction part 2 - EOS Model JAX Bridges.
Traction part 2 - EOS Model JAX Bridges.Traction part 2 - EOS Model JAX Bridges.
Traction part 2 - EOS Model JAX Bridges.
 
Market Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 EditionMarket Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 Edition
 
PSCC - Capability Statement Presentation
PSCC - Capability Statement PresentationPSCC - Capability Statement Presentation
PSCC - Capability Statement Presentation
 
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
 
8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCR8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCR
 
APRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdfAPRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdf
 
Organizational Structure Running A Successful Business
Organizational Structure Running A Successful BusinessOrganizational Structure Running A Successful Business
Organizational Structure Running A Successful Business
 
MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?
 
Call Us ➥9319373153▻Call Girls In North Goa
Call Us ➥9319373153▻Call Girls In North GoaCall Us ➥9319373153▻Call Girls In North Goa
Call Us ➥9319373153▻Call Girls In North Goa
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchir
 
Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737
 
Financial-Statement-Analysis-of-Coca-cola-Company.pptx
Financial-Statement-Analysis-of-Coca-cola-Company.pptxFinancial-Statement-Analysis-of-Coca-cola-Company.pptx
Financial-Statement-Analysis-of-Coca-cola-Company.pptx
 
Call Us 📲8800102216📞 Call Girls In DLF City Gurgaon
Call Us 📲8800102216📞 Call Girls In DLF City GurgaonCall Us 📲8800102216📞 Call Girls In DLF City Gurgaon
Call Us 📲8800102216📞 Call Girls In DLF City Gurgaon
 
Kenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith PereraKenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith Perera
 
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
 
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
 
Corporate Profile 47Billion Information Technology
Corporate Profile 47Billion Information TechnologyCorporate Profile 47Billion Information Technology
Corporate Profile 47Billion Information Technology
 
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
 
Investment in The Coconut Industry by Nancy Cheruiyot
Investment in The Coconut Industry by Nancy CheruiyotInvestment in The Coconut Industry by Nancy Cheruiyot
Investment in The Coconut Industry by Nancy Cheruiyot
 

AI CHALLENGE ADMIN

  • 1. #include <stdio.h> #include <iostream> #include <time.h> #include <stdlib.h> #include <fstream> #include <pthread.h> #include <unistd.h> #include <signal.h> using namespace std; #define CLEAR_ROW 1 #define CLEAR_COLUMN 2 #define CLEAR_FIRST_NEIGHBOURS 3 #define DUAL_COLOUR 4 #define NORMAL 5 #define PLAYER_1 1 #define PLAYER_2 2 /* global decalarations */ int turn = 0;/* this number will record whose turn is it */ int moves1=0,moves2=0;/* for tracking the number of moves pklayed yet by participants*/ int spmove11=0,spmove21=0,spmove31=0,spmove41=0,spmove12=0,spmove22=0,spmove32=0,spmov e42=0;/* flags to keep track the special moves of players */ int move_type=0,move=0; int board[7][6] = {0};
  • 2. /* functions used */ int legal_move(); int add_disk(); int check_win(int team); int right_up(int x,int y); int left_up(int x,int y); int vertical(int x,int y); int horizontal(int x,int y); int write_board(); int draw_board(); int first_turn(); int board_full(); int call_code(); int move_counter(); int main() { first_turn();/* decides first turn */ write_board();/* writes board to txt file */ draw_board();/* call graphics function to draw the board */ int match_over = 0;/* flag telling match is over or not */ while(1)/*starting the infinite loop*/ { write_board(); match_over = board_full();/*checking if board is full*/ if(match_over==1) break;
  • 3. /*calling the participant code*/ match_over = call_code(); if(match_over == 1) break; else/* match_over == 0 *//* match is not over */ { move_counter(); turn++; draw_board(); int check1 = check_win(1);/* checking winner */ int check2 = check_win(2); if(check1 == 1 && check2 == 1) { printf("its a drawn"); break; } else if(check1 == 1) { printf("player 1 winsn"); break; } else if(check2 == 1) { printf("player 2 winsn"); break; }
  • 4. } } return 0; } int legal_move() { if(move > 7 || move < 1)/*invalid column */ return 0; else if(board[move-1][0] != 0)/* top most position of column is not empty*/ return 0; if(turn%2 == 0)/* for player 2 */ { if(move_type == CLEAR_ROW && spmove12 == 1) return 0; else if(move_type == CLEAR_COLUMN && spmove22 == 1) return 0; else if(move_type == CLEAR_FIRST_NEIGHBOURS && spmove32 == 1) return 0; else if(move_type == DUAL_COLOUR && spmove42 == 1) return 0; else if(move_type != DUAL_COLOUR && moves2 == 9 && spmove42 == 0)/* move type 4 means dual color block */ return 0; } else { if(move_type == CLEAR_ROW && spmove11 == 1)
  • 5. return 0; else if(move_type == CLEAR_COLUMN && spmove21 == 1) return 0; else if(move_type == CLEAR_FIRST_NEIGHBOURS && spmove31 == 1) return 0; else if(move_type == DUAL_COLOUR && spmove41 == 1) return 0; else if(move_type != DUAL_COLOUR && moves1 == 9 && spmove41 == 0) return 0; } return 1; } int add_disk() { for(int y = 0;y<6;y++) { if(board[move-1][y+1] != 0 || y+1>5)/* bottommost position in that column */ { if(move_type == CLEAR_ROW)/* 1 means detroying complete row */ { for(int x=0;x<7;x++) { for(int temp = y;temp>=0;temp--) board[x][temp] = board[x][temp-1]; board[x][0] = 0; }
  • 6. } else if(move_type == CLEAR_COLUMN)/* destroying complete column */ { for(int temp = 0;temp<7;temp++) board[move-1][temp] = 0; } else if(move_type == CLEAR_FIRST_NEIGHBOURS)/* destroying neighbour block in every direction */ { for(int temp = move-2;temp <= move;temp++) { if(temp<0 || temp>6) continue; if(y+1<6) board[temp][y+1]=0; board[temp][y]=0; if(y-1>=0) board[temp][y-1]=0; /* now need to shift zeroes */ for(int count=0;count < 3;count++) { for(int change = 5;change > 0;change--) { if(board[temp][change]==0) { board[temp][change] = board[temp][change-1]; board[temp][change-1] = 0; }
  • 7. } } } } else if(move_type == DUAL_COLOUR)/* dual color block */ { board[move-1][y] = 12; } else/* normal block */ { if(turn % 2 == 1) board[move-1][y] = 1; else board[move-1][y] = 2; } break; } } return 0; } int check_win(int team) { int result=0,temp=0; for(int y=5;y>=0;y--) for(int x=0;x<7;x++) {
  • 8. if(board[x][y] == 0)/* block is empty */ continue; if(board[x][y] == team || board[x][y] == 12) { result = right_up(x,y); if(result!=1) result = left_up(x,y); if(result!=1) result = vertical(x,y); if(result!=1) result = horizontal(x,y); } if(result == 1) return 1; } return 0; } int right_up(int x,int y) { if(y<3 || x>3)/* not possible */ return 0; else if(x-1>=0 && y+1<6 && (board[x-1][y+1] == board[x][y] || board[x-1][y+1] == 12)) return 0;/* condition was checked at x-1 and y+1 already */ else { int count=1;
  • 9. for(int temp = 1;temp<4;temp++) { if(board[x+temp][y-temp] == board[x][y] || board[x+temp][y-temp] == 12) count++; } if(count == 4) return 1; } return 0; } int left_up(int x,int y) { if(y<3 || x<3)/* not possible */ return 0; else if(x+1<7 && y+1<6 && (board[x+1][y+1] == board[x][y] || board[x+1][y+1] == 12)) return 0;/* condition was checked at x+1 and y+1 already */ else { int count=1; for(int temp = 1;temp<4;temp++) { if(board[x-temp][y-temp] == board[x][y]) count++; } if(count == 4) return 1;
  • 10. } return 0; } int vertical(int x,int y) { if(y<3) return 0; else if( y+1 < 6 && (board[x][y+1] == board[x][y] || board[x][y+1] == 12)) return 0; int count = 1; for(int temp=1;temp<4;temp++) { if(board[x][y-temp] == board[x][y] || board[x][y-temp] == 12 ) count++; } if(count == 4) return 1; return 0; } int horizontal(int x,int y) { if(x>3) return 0; else if( x-1>=0 && (board[x-1][y] == board[x][y] || board[x-1][y]==12)) return 0;
  • 11. int count=1; for(int temp=1;temp<4;temp++) { if(board[x+temp][y] == board[x][y] || board[x+temp][y] == 12 ) count++; } if(count == 4) return 1; return 0; } int write_board() { ofstream pt ("board.txt"); for(int y = 5 ; y>=0 ; y--) { for(int x = 0 ; x < 7 ;x++) { pt<<board[x][y]<<" "; } } pt.close(); return 0; } int draw_board() {
  • 12. printf("n"); for(int y = 0 ; y<6;y++) { for(int x = 0 ; x < 7 ; x++) printf("%d ",board[x][y]); printf("n"); } printf("nn"); return 0; } int first_turn() { srand ( time(NULL) ); /* initialization to generate a random number*/ /* deciding the first player whose turn is it */ ifstream pt ("turn.txt"); /* opening the file in which the player who got the first turn is written */ if(pt == NULL)/* no such file exists*/ { turn = rand()%2 + 1; /* generating a random number to decide the first player to play */ ofstream ptr ("turn.txt"); ptr<<turn; ptr.close(); }
  • 13. else/* file exists */ { pt>>turn; pt.close(); if(turn%2==0) turn = 1; else turn = 2; ofstream file ("turn.txt"); file<<turn; file.close(); } } int board_full() { for(int y=0;y<6;y++) { int empty=0;/* flag for board empty */ for(int x=0;x<7;x++) { if(board[x][y]==0) { empty = 1; break; } }
  • 14. if(empty==1) return 0; else/*board is full*/ { printf("this match is a drawn"); return 1; } } } int call_code() { for(int temp=0;temp<=3;temp++)/* at max 3 times */ { remove("output.txt");/* deleting the output.txt file */ ofstream ptr ("moves.txt");/* writing moves played yet and powers used by player*/ ifstream abc ("turn.txt"); int record; abc>>record; if(record%2==1) ptr<<(turn-1)/2<<" "; else ptr<<turn/2-1<<" "; if(turn%2==0) ptr<<spmove12<<" "<<spmove22<<" "<<spmove32<<" "<<spmove42; else ptr<<spmove11<<" "<<spmove21<<" "<<spmove31<<" "<<spmove41; ptr.close();
  • 15. ofstream file ("team_no.txt");/* creating the team_no.txt file */ if(turn%2==0) file<<"2"; else file<<"1"; file.close(); ofstream pt ("opponent_move.txt"); if(turn%2==0) pt<<spmove11<<" "<<spmove21<<" "<<spmove31<<" "<<spmove41; else pt<<spmove12<<" "<<spmove22<<" "<<spmove32<<" "<<spmove42; pt.close(); int legal = 1; if(temp == 3) { printf("game overn"); if(turn%2 == 1) printf("player 2 winsn"); else printf("player 1 winsn"); return 1; }
  • 16. pid_t pid; pid = fork();/* to start a child process*/ if(pid == 0)/* child process */ { // team codes will be called from here // respective commands according to the language of the code will be used // for eg to run c++ program named "abc.cpp" first copy the code in the same directory as this code then write // system("g++ abc.cpp -o abc"); // system("./abc"); if(turn%2 == 1) { /*call first player code */ // write command (system function) here to call code } else { /* call second participant code*/ // write command (system function) here to call code } exit(0);/* exit the child process */ } else {/* main program sleeps for 2s*/ sleep(2);/* execution time limit */ kill(pid,SIGKILL); } ifstream out ("output.txt");/*reading output*/
  • 17. if(out == NULL) continue; else { out>>move_type; out>>move; out.close(); } int check = legal_move();/* check if move is legal */ if(check == 0)/* move is illegal */ continue; if(legal == 1)/* this means code reaches succesfully i.e. move is legal */ break; } return 0; } int move_counter() { add_disk(); if(turn%2 == 0) moves2++; else moves1++; if(move_type==1) { if(turn%2==0)
  • 18. spmove12++; else spmove11++; } else if(move_type==2) { if(turn%2==0) spmove22++; else spmove21++; } else if(move_type==3) { if(turn%2==0) spmove32++; else spmove31++; } else if(move_type==4) { if(turn%2==0) spmove42++; else spmove41++; } }