Church Building Grants To Assist With New Construction, Additions, And Restor...
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)
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();
}
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();
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)