Question
#include #include char ** createboard(); void printboard(char** Board); int isdraw(char** Board); char winningmove(char** Board, int i, int j); // Prints the board void printboard( char**
#include
#include
char ** createboard();
void printboard(char** Board);
int isdraw(char** Board);
char winningmove(char** Board, int i, int j);
// Prints the board
void printboard( char** Board )
{
printf(" |1|2|3| ");
for(int i = 0; i
{
printf("%c|", 'a' + i);
for( int j = 0; j
{
printf("%c|", Board[i][j]);
}
printf(" ");
}
}
// Creates 3x3 tic tac toe board
char** createboard()
{
char** B = calloc( 3, sizeof(char*) );
// space for board
for(int i = 0; i
{
B[i] = calloc(3, sizeof(char));
}
for(int j=0; j
{
for(int k=0; k
{
B[j][k] = ' ';
}
}
return B;
}
int main()
{
char** Board = createboard();
char winner = '\0';
char row;
char col;
char turn = 'X';
// standard game loop
while( !winner && !isdraw(Board) )
{
printboard(Board);
// Get Move
printf( "Player's %c turn ", turn );
printf( "(%c) Enter Move (row column) Example: a 1 ---> ", turn );
fflush(stdout);
scanf(" %c %c", &row, &col);
// Make move if square is free
int rowind = row - 'a';
int colind = col - '1';
if (Board[rowind][colind] == ' ')
{
printf("Move is %c %c (%d, %d) ", row, col, rowind, colind);
Board[rowind][colind] = turn;
if (turn == 'X')
{
turn = 'O';
}
else
{
turn = 'X';
}
winner = winningmove( Board, rowind, colind );
}
else
{
printf("Square is occupied; try again. ");
}
}// while
// Game over - print board & declare finish
printboard(Board);
if (winner == 'X' || winner == 'O')
{
printf("Congratulations %c! ", winner);
}
else
{
printf("Game ends in a draw. ");
}
return 0;
}
// Returns true if the game is a draw
int isdraw(char** Board) {
for(int i = 0; i
for(int j = 0; j
if (Board[i][j] == ' ') {
// empty square, so game ain't over yet
return 0;
}
}
}
// no empty squares, so it's a draw
return 1;
}
// Returns 'X' if (i,j) was a winning move for X
// Returns 'Y' if (i,j) was a winning move for Y
// Returns ASCII value 0 otherwise
char winningmove( char** Board, int i, int j )
{
if (Board[i][j] == Board[i][(j+1)%3]
&& Board[i][j] == Board[i][(j+2)%3])
{
// check column
return Board[i][j];
}
else if (Board[i][j] == Board[(i+1)%3][j]
&& Board[i][j] == Board[(i+2)%3][j])
// check row
{
return Board[i][j];
}
else if (i == j && Board[i][j] == Board[(i+1)%3][(j+1)%3]
&& Board[i][j] == Board[(i+2)%3][(j+2)%3])
// check forward diagonal
{
return Board[i][j];
}
else if (i+j == 2 && Board[i][j] == Board[(i+2)%3][(j+1)%3]
&& Board[i][j] == Board[(i+1)%3][(j+2)%3])
{
// check reverse diagonal
return Board[i][j];
}
else {
// got nothing
return 0;
}
}
code on the top is tictactoe.c
i need to get an explain of how to get command line argument -s
and -i to indicate that the computer picks its position randomly, using getopt().
it'll be great if i can get the separate .c file containing parsing the command line.
and i need
New Functions (and/or Modifications)
int main() - clean up main() so that it only processes input parameters, and seeds the random generator, i.e. srand(time(0));
int play_tictactoe(int board_sz, int computer_enabled )
- game loop
int getMove( char ** Board, int board_sz, char *x, char *y )
- Returns a valid move (the integer is a 2 digit integer, and the move the row is the first digit, and the column is stored in the second digit. Valid - an empty square.
- The mechanics of this function is a suggestion. You are free to implement a different mechanics. Minimum: You do need a method to get a move.
please share the code and also result and explain. thank you.
Starting the Game: "X"/Player Move - Player selects move, game gives a suggested move which is remaining 'empty square'. Suggested move is selected randomly from all empty squares. a Computer 'o' Moves are 'enabled' c-Player's x turn (qq to quit) "---> suggestion: (b1) (X) Enter Nove (row column) "O"/Computer Move - Computer picks its position randomly, or interactively. This mode is set on the command line with the -i parameter. Below random selection mode is 'enabled': 1. Starting the game - it should have to two optional arguments, 5 to set the dimensions of the board, and -i to indicate that the computer picks its position randomly: Syntax: tictactoe [-s board_size] [-i ] Example : tictactoe 510-i Default is 33 and the computer does not picks its positions randomly, similar to the provided starter code, 2. Screen is cleared before the board is redrawn at top of terminal (only exception is the last move, then the completed board is drawn below exiting output, 3. No Global Variables. 4. Defined constants are OK (e.g., \#define MAX_DIM) 5. Avoid code duplication (modularize the code with functions0) to avoid repeating code segments. 6. Elements in the Board array should be the same as started code, i.. , ' X ' for player ' X ' move, ' O ' for the computer, and ' ' (blank) for an empty space, 7. The Board columns index should begin at ' 0 ' 8. The Board row index should begin at 'a' 9. When the player enters ' q ' ' q ' the game should terminate 10. When the computer picks a random move the player should hit a key to continue the game. 11. When the computer is picking its moves randomly it should pick randomly from all currently empty squares. 12. The game should suggest a random empty square to the user. Function Requirements You need to modify the existing methods by renaming, and adding parameters, the below represents the result of that (self-explanatory). Existing Functions (renamed with and with added parameter: board_sz ). New Functions (and/or Modifications) - int main() clean up main( ) so that it only processes input parameters, and seeds the random generator, i.e. arand (time(0)); - int play_tictactoe(int board_sz, int computer_enabled ) - game loop - int getmove ( char Board, int board_sz, char x, char y ) - Returns a valid move (the integer is a 2 digit integer, and the move the row is the first digit, and the column is stored in the second digit. - Valid - an empty square. - The mechanics of this function is a suggestion. You are free to implement a different mechanics. - Minimum: You do need a method to get a move. - void extract_digits (unsigned int num, char * x, char y, int turn, int computer_enabled ) - extracts (row, column) from int num, and stores them in the reference of x,y,i,e,x and " y This is a suggested function, not a requirementStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started