Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I got a program in C that creates a tictactoe board. I have the header and main c file. Can you please explain what each

I got a program in C that creates a tictactoe board. I have the header and main c file. Can you please explain what each of the functions do (like for example, what is u16 and why is it used in this case, or b3atpos) in tictactoe.c and what it does as the comments are still kind of unclear? Please be detailed in your explanation as I want to understand it clearly. Thank you.

Header File:

#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);

Main C file:

#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 < e; i++, result*=b);

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 < 9; i--) { /*Converts b3 to a number value, then multiply it by it's place value*/ value += (b3[i] - '0') * placeValue; placeValue *= 3; }

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 < 9; i--) { /*Calculate the b3 value for each place value in order from lowest to highest*/ b3[i] = (us % 3) + '0'; us /= 3; } }

/*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 < 9; i++) { /*This converts the index from range [0,8] to the indices of each piece on the grid. The first part of the calculation handles accessing the columns, and the second skips over the rows where there are no pieces.*/ char piece = board[i*4 + 1 + 12*(i/3)]; /*Set the number character in b3 based on the piece character 'X'->'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 < 9; i++) { /* This converts i from range [0,8] to the indices of each piece on the grid. The first part of the calculation handles accessing the columns, and the second skips over the rows where there are no pieces. */ board[i*4 + 1 + 12*(i/3)] = (b3[i] == '2' ? 'X' : (b3[i] == '1' ? 'O' : ' ')); } }

/*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 < 9; i++) { /*Convert each b3 value to a number value (0->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 < 8; i++) { win[0] |= win[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 < 9; i++) { count += !!(b3[i] - '0'); /* The "!!" coverts the value to either 1 or 0*/ } return count; }

/*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); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

More Books

Students also viewed these Databases questions

Question

The assessor will make a determination of the tax due.

Answered: 1 week ago