Question: Introduction In this assignment you will be writing an agent to play the game of Nine - Board Tic - Tac - Toe. This game

Introduction
In this assignment you will be writing an agent to play the game of Nine-Board Tic-Tac-Toe. This game is played on a 3 x 3 array of 3 x 3 Tic-Tac-Toe boards. The first move is made by placing an X in a randomly chosen cell of a randomly chosen board. After that, the two players take turns placing an O or X alternately into an empty cell of the board corresponding to the cell of the previous move. (For example, if the previous move was into the upper right corner of a board, the next move must be made into the upper right board.)
The game is won by getting three-in-a row either horizontally, vertically or diagonally in one of the nine boards. If a player is unable to make their move (because the relevant board is already full) the game ends in a draw.
Getting Started
Copy the archive src.zip into your own filespace and unzip it. Then type
cd src
make all
./servt -x -o
You should then see something like this:
...|...|...
...|...|...
...|...|...
------+-------+------
...|...|...
...|...|...
...|.. x |...
------+-------+------
...|...|...
...|...|...
...|...|...
next move for O ?
You can now play Nine-Board Tic-Tac-Toe against yourself, by typing a number for each move. The cells in each board are numbered 1,2,3,4,5,6,7,8,9 as follows:
+-----+
|123|
|456|
|789|
+-----+
To play against a computer player, you need to open another terminal window (and cd to the src directory).
Type this into the first window:
./servt -p 12345-x
This tells the server to use port 12345 for communication, and that the moves for X will be chosen by you, the human, typing at the keyboard. (If port 12345 is busy, choose another 5-digit number.)
You should then type this into the second window (using the same port number):
./randt -p 12345
The program randt simply chooses each move randomly among the available legal moves. The Python program agent.py behaves in exactly the same way. You can play against it by typing this into the second window:
python3 agent.py -p 12345
You can play against a somewhat more sophisticated player by typing this into the second window:
./lookt -p 12345
(If you are using a Mac, type ./lookt.mac instead of ./lookt)
Writing a Player
Your task is to write a program to play the game of nine-board tic-tac-toe as well as you can. Your program will receive commands from the server (init, start(), second_move(), third_move(), last_move(), win(), loss(), draw(), end()) and must send back a single digit specifying the chosen move.
(the parameters for these commands are explained in the comments of agent.py)
Communication between the server and the player(s) is illustrated in this brief example:
Player X Server Player O
init
init ->
start(x)
start(o)->
second_move(6,1)->
6
third_move(6,1,6)
9->
next_move(9)->
6
next_move(6)
5->
last_move(5)->
win(triple)
loss(triple)->
end
end ->
can you do it in C
This is the starter code
#include
#include
#include
#include
#include "common.h"
#include "agent.h"
#include "game.h"
#define MAX_MOVE 81
int board[10][10];
int move[MAX_MOVE+1];
int player;
int m;
/*********************************************************
Print usage information and exit
*/
void usage( char argv0[])
{
printf("Usage: %s
",argv0);
printf("[-p port]
"); // tcp port
printf("[-h host]
"); // tcp host
exit(1);
}
/*********************************************************
Parse command-line arguments
*/
void agent_parse_args( int argc, char *argv[])
{
int i=1;
while( i < argc ){
if( strcmp( argv[i],"-p")==0){
if( i+1>= argc ){
usage( argv[0]);
}
port = atoi(argv[i+1]);
i +=2;
}
else if( strcmp( argv[i],"-h")==0){
if( i+1>= argc ){
usage( argv[0]);
}
host = argv[i+1];
i +=2;
}
else {
usage( argv[0]);
}
}
}
/*********************************************************
Called at the beginning of a series of games
*/
void agent_init()
{
struct timeval tp;
// generate a new random seed each time
gettimeofday( &tp, NULL );
srandom(( unsigned int )( tp.tv_usec ));
}
/*********************************************************
Called at the beginning of each game
*/
void agent_start( int this_player )
{
reset_board( board );
m =0;
move[m]=0;
player = this_player;
}
/*********************************************************
Choose second move and return it
*/
int agent_second_move( int board_num, int prev_move )
{
int this_move;
move[0]= board_num;
move[1]= prev_move;
board[board_num][prev_move]=!player;
m =2;
do {
t

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!