Question: Hello, I have questions about a program. I just need a thorough explanation on programs that were already written for the program below: Now, I

Hello, I have questions about a program. I just need a thorough explanation on programs that were already written for the program below:

Hello, I have questions about a program. I just need a thorough

explanation on programs that were already written for the program below: Now,

I already have the programs, but I just do not understand thoroughly

what they exactly do. I am not asking for new code, my

question is just to please explain in text and in detail what

each function is doing and why it's doing it so I have

a clearer understanding. There are some comments, but I need more explanation,

especially for the calculations, as to what they are doing as I

don't grasp it entirely. I need a thorough explanation for tictactoe.c mostly

Now, I already have the programs, but I just do not understand thoroughly what they exactly do. I am not asking for new code, my question is just to please explain in text and in detail what each function is doing and why it's doing it so I have a clearer understanding. There are some comments, but I need more explanation, especially for the calculations, as to what they are doing as I don't grasp it entirely. I need a thorough explanation for tictactoe.c mostly which is:

TICTACTOE.C

#include "tictactoe.h"

/*Used in the next and b3atpos function, where it is only called once; therefore not too wasteful*/ int ipow(int b, u16 e) { int result = 1; u16 i;

for (i = 0; i

return result; }

u16 b3tous(char b3[10]) { u16 value = 0;

u16 placeValue = 1; u8 i = 0; /*Go through every space on the board.*/ for (i = 8; i

return value; }

void b3fromus(char b3[10], u16 us) { u8 i; /*Start at lowest place value and itterate towards the highest place value*/ for (i = 8; i

/*Reads and makes use of the ipow function*/ u8 b3atpos(u16 us, u8 pos) { /*This directly returns the b3 value at a place value*/ return (us / (u8)ipow(3,pos)) % 3; }

void boardtob3(char b3[10], char board[60]) { /*unsigned value as the index*/ u8 i;

/*Sets the end of the array to a null character*/ b3[9] = '\0';

for (i = 0; i '2' 'O'->'1' ' '->'0'*/ b3[i] = (piece == 'X' ? '2' : (piece == 'O' ? '1' : '0')); }

}

/*Allows boardtob3 to automatically convert back to us*/ u16 boardtous(char board[60]) { char b3[10]; boardtob3(b3, board); /*return value*/ return b3tous(b3); }

void boardfromb3(char board[60], char b3[10]) { u8 i;

/*Fill the board with an empty grid. The board becomes populated in the next step*/ strcpy(board, " | | ---+---+--- | | ---+---+--- | | ");

/*Goes through every position on the board*/ for (i = 0; i

/*Allows boardfromus to use us as a parameter*/ void boardfromus(char board[60], u16 us) { char b3[10]; b3fromus(b3, us); boardfromb3(board, b3); }

/*Prints out the board status*/ void printBoardb3(char b3[10]) { char board[60]; boardfromb3(board, b3); /*prints the board*/ printf("%s ", board); }

/*Allows printBoardb3 to use us as a parameter*/ void printBoardus(u16 us) { char b3[10]; b3fromus(b3, us); printBoardb3(b3); }

char winner(u16 us) { char b3[10];

/*Binary 0b11*/ u8 win[8] = {3,3,3,3,3,3,3,3}; /*unsigned value as index*/ u8 i;

b3fromus(b3, us);

for (i = 0; i 2), from a char. This also means that the value becomes one of 0b00, 0b01, 0b10.*/ b3[i] = b3[i] - '0';

/* Calculates if everything in each row, column, or diagonal are all the same by using the '&' operator. If there is a line of 3 of the same piece, then the corresponding value in the array will be non-zero. If there is any non-zero values in the array, then there is a winner */

win[i / 3] &= b3[i]; /*Calculate rows*/ win[(i % 3) + 3] &= b3[i]; /*Calculate columns*/ win[6] &= (i % 4 == 0 ? b3[i] : 3); /*Calculate the first diagonal*/ win[7] &= (i == 2 || i == 4 || i == 6 ? b3[i] : 3); /*Calculate the second diagonal*/

}

for (i = 1; i

/*If win[0] has 0b10, then 'X' won, if win[0] has 0b01, then 'O' won. else nobody one. 0b11 is not going to occur in regular use. Returns '2'for this case*/ return (win[0] & 2 ? '2' : (win[0] & 1 ? '1' : ' ')); }

/*this function has the algorithm for board values*/ u16 next(u16 us, char pos) { char b3[10];

b3fromus(b3, us);

/*Checks if there is no piece number at the location pos*/ if (b3[(int)pos] == '0') { /*Adds the place value of the move to us. It works the same as the get_turn function. The piece number is multiplied by the place value.*/ return us + (2-(get_move(b3)%2)) * ipow(3,8-pos); }

return 0; }

char get_move(char b3[10]) { u8 i; char count = 0; /*The counter counts non-zero values in b3*/ for (i = 0; i

/*Allows get_move to use us as a parameter*/ char get_move_us(u16 us) { char b3[10]; b3fromus(b3, us); return get_move(b3); }

char get_turn(char b3[10]) { /*get_move decides the pieces.*/ /* This is calulated by checking the value of the move number, assuming X plays first.*/ /* If the move number is divisible by 2, the move is '2'*/ return (get_move(b3) % 2 == 0 ? '2' : '1'); }

/*This allows get_turn to use us as a parameter*/ char get_turn_us(u16 us) { char b3[10]; b3fromus(b3, us); /*return value*/ return get_turn(b3); }

char get_next_turn(char b3[10]) { /*This takes get_turn and inverts the solution*/ return (get_turn(b3) == '1' ? '2' : '1'); }

/*This is to allow get_next_turn to use us as a parameter*/ char get_next_turn_us(u16 us) { char b3[10]; b3fromus(b3, us); return get_next_turn(b3); }

/*Opens strategy file and stores it into buf*/ void readStrategyFile(void *buf) { FILE *fp = fopen( "strategyfile", "rb" ); /*Binary Format*/

fread(buf, sizeof(strategy), NUMBOARD, fp); /*Reads NUMBOARD strategy structs*/

fclose(fp); }

/*Opens strategy file and writes buf to the file*/ void writeStrategyFile(void *buf) { FILE *fp = fopen( "strategyfile", "wb" ); /*Binary Format*/

fwrite(buf, sizeof(strategy), NUMBOARD, fp); /*Writes NUMBOARD strategy structs*/

fclose(fp); }

TICTACTOE.H

#include #include #include #include

#define NUMBOARD 19683 /*same as pow(3,9)*/ #define TICTACTOE_H

/*Declare types that are going to be reoccuring*/ typedef unsigned char u8; typedef unsigned short u16; typedef unsigned int u32;

typedef struct strategy_struct{ char best_move; char winner; } strategy;

int ipow(int b, u16 e);

u16 b3tous(char b3[10]); void b3fromus(char b3[10], u16 us); u8 b3atpos(u16 us, u8 pos);

void boardtob3(char b3[10], char board[60]); u16 boardtous(char board[60]); void boardfromb3(char board[60], char b3[10]); void boardfromus(char board[60], u16 us);

void printBoardb3(char b3[10]); void printBoardus(u16 us);

char winner(u16 us); u16 next(u16 us, char pos); char get_move(char b3[10]); char get_move_us(u16 us); char get_turn(char b3[10]); char get_turn_us(u16 us); char get_next_turn(char b3[10]); char get_next_turn_us(u16 us);

void readStrategyFile(void *buf); void writeStrategyFile(void *buf);

A1P3.C

#include "tictactoe.h"

int main(int argc, char **argv) { /*unsigned values as variables*/ u8 move; u16 us;

/*assign NUMBOARD to the stategy variable*/ strategy strategyBuf[NUMBOARD];

/*if statement to ensure that an argument has been supplied*/ if (argc

move = atoi(argv[1]);

/*Open the current Strategy File All previous entries are kept This allows fast and easy access to its contents*/ readStrategyFile(strategyBuf);

for (us = 0; us

/*Checks if a player has won*/ if (playerWin != ' ') { /*No next move if the winner is player that won*/ strategyBuf[us].winner = playerWin; strategyBuf[us].best_move = -1; } else /*if there is no winner on current move*/ { u8 i = 0; /*9 is for 9 possible moves. 3 is used for the 3 strategy cases*/ for (i = 0; i 8 3 times)*/ u8 move = i%9; u16 nextBoard = next(us, move);

/*Next move that leads to a winner*/ if (i/9 == 0 && strategyBuf[nextBoard].winner == get_turn_us(us)) { /*Winner is the current player while best move is the current move being examined*/ strategyBuf[us].winner = get_turn_us(us); strategyBuf[us].best_move = move + '0'; i = 9*3;/*Exits back to theus loop and stops evaluating the strategy for current board*/ }

/*Checks if the next move leads to a tie*/ if (i/9 == 1 && (strategyBuf[nextBoard].winner == '0' || get_move_us(nextBoard) == 9)) { /*No one is the Winner and so the best move is the current move being examined*/ strategyBuf[us].winner = '0'; strategyBuf[us].best_move = move + '0'; i = 9*3;/*Exits back to the us loop and stops evaluating the stratagy for current board*/ }

/*no winning strategy; next move is highest us value; select the first valid move*/ if (i/9 == 2 && nextBoard != 0) { /*Winner is other player, best move is current move being examined*/ strategyBuf[us].winner = get_next_turn_us(us); strategyBuf[us].best_move = move + '0'; i = 9*3; /*This exits back to the us loop and stops evaluating the strategy for the current board*/ } } } } }/*End of us loop*/

writeStrategyFile(strategyBuf); /*Saves the newly calculated Strategy File*/

return 0; }

A1P4.C

#include "tictactoe.h"

int main(int argc, char **argv) { /*Initilize variables*/ u16 us, i; char b3[10]; char board[60]; strategy strategyBuf[NUMBOARD];

/*Checks if an argument has been supplied*/ if (argc

/*Calculate information about the board*/ us = atoi(argv[1]); b3fromus(b3, us); boardfromb3(board, b3);

/*prints out the board details*/ printf("Board number: %d ", us); printf("Board b3: %s ", b3); printf("Board pic: %s ", board); printf("Move: %d ", get_move(b3)); printf("Turn: %c ", get_turn(b3)); printf("Winner: %c ", winner(us));

/*Opens the strategy file to read the required information*/ readStrategyFile(strategyBuf); /*Prints the best move and the winner*/ printf("best_move= %c, winner= %c ", strategyBuf[us].best_move, strategyBuf[us].winner);

/*print out all the possible moves*/ for (i = 0; i %d ", i, next(us, i)); }

return 0; }

A1P5.C

#include "tictactoe.h"

int main() { /* Format exactly like this:

SHALL WE PLAY A GAME? PLEASE ENTER YOUR NAME: PROFESSOR FALKEN GREETINGS PROFESSOR FALKEN Which side would you like to play (X/O)? O Ok, you will be O; I will be X.

*/

char playerName[21]; char playerPiece; u8 turn = 0, play = 1; u16 board = 0; strategy strategyBuf[NUMBOARD];

readStrategyFile(strategyBuf);

/*get user input for the following prompts*/ printf("SHALL WE PLAY A GAME? PLEASE ENTER YOUR NAME: "); fgets(playerName, 20, stdin); /*user stdin to get a string from the user*/ printf(" GREETINGS %s ", playerName); printf("Which side would you like to play (X/O)? "); scanf("%c", &playerPiece); /*promtps user to pic a char between X or O*/ printf("Ok, you will be %c; I will be %c. ", playerPiece, playerPiece == 'O' ? 'X' : 'O');

/*Determines which player goes first by checking who the player who has X*/ turn = (playerPiece == 'X' ? 1 : 0);

printBoardus(board);

while (play) { /*Do each player's turn*/ if (turn == 0) { /*Get move from strategy*/ char move = strategyBuf[board].best_move; printf("My turn; my move is %c: ", move); board = next(board, move - '0'); /*Proceed with next move on the board*/ } else if (turn == 1) { /*Gets next move*/ int move = 0; printf("Your turn; chose a move [0-8]: "); scanf("%d", &move); board = next(board, (char)move); /*Proceed with next move on the board*/ }

/*Displays the board*/ printf(" "); printBoardus(board); printf(" ");

/*This checks for end game conditions*/ /*Also checks if the winner is the piece other than the player, like if the computer won*/ if (winner(board) == (playerPiece == 'X' ? '1' : '2')) { printf("Player one won! "); play = 0; }/*check for player two win*/ else if (winner(board) == playerPiece) { printf("Player two won! "); play = 0; }/*check for tie*/ else if (get_move_us(board) == 9) { printf("It is a tie. "); play = 0; }

/*Switches players*/ turn = 1 - turn; }

printf(" A STRANGE GAME. THE ONLY WINNING MOVE IS NOT TO PLAY. ");

return 0; }

Again, I'm not asking for new code, just through comments and explanations as to what each function is doing and its calculations, particulary for the main c file. Thank you if you are able to explain it.

For this assignment, you will be working with binary files that represent the game of Tic-Tac-Toe (a.k.a. noughts and crosses, Xs and Os) Base-3 Consider a base-3 numbering system consisting of the digits: 0, 1, 2. And 9 columns as shown below: Multiplier Column 243s Value 81s 0 0 0 0 0 6561s 2187s 729s 27s 0 0 0 Digits 0 1 19,681 2 19,68:2 This number system can represent integers starting from zero (0) all the way up to 19,682. Write a function that converts strings containing base-3 numbers to their integer equivalent as an unsigned short integer unsigned short b3tous char b3[10]) Write a function that works the other way, converting unsigned short integers to null terminated strings of the characters '0', '1, and '2'. void b3fromus( char b3 [10], unsigned short us

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 Databases Questions!