[Please answer ASAP, and in C] I\'ve been getting a segmentation error every tim
ID: 3880333 • Letter: #
Question
[Please answer ASAP, and in C] I've been getting a segmentation error every time I try to run program 4. Please help fix it with better code so that the program runs as intended, you can change my programs accordingly.
My makefile:
My main tictactoe.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, starting from the lowest place value
The loop condition is "i<9" because after i is 0, i will wrap around to 255*/
for (i = 8; i < 9; i--)
{
/*convert 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;
}
}
/*read and make use of the ipow function*/
u8 b3atpos(u16 us, u8 pos)
{
/*directly returns the b3 value at a place value*/
return (us / (u8)ipow(3,pos)) % 3;
}
void boardtob3(char b3[10], char board[60])
{
u8 i;
/*set the end of the array to a null character*/
b3[9] = '';
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'));
}
}
/*wrapper for boardtob3 to automatically convert back to us*/
u16 boardtous(char board[60])
{
char b3[10];
boardtob3(b3, board);
return b3tous(b3);
}
void boardfromb3(char board[60], char b3[10])
{
u8 i;
/*Fill the board with an empty grid. The board is 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.
Sets the piece character based on the number character.
'2'->'X' '1'->'O' '0'->' '*/
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);
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};
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. A value of 0b01 is 'O'
and a value of 0b10 is 'X'. 0b11 is possible, but the board will never be reached
*/
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*/
}
/*OR all the values together and store in the first array value, so that the
array can be tested for non-zero values easily*/
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' : ' '));
}
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;
}
/*wrapper for get_move to allow using 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 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);
}
void get_record(FILE *fileStrat, unsigned short us, struct strategy_struct*r){
fseek(fileStrat, us, SEEK_SET);
fread(r, sizeof(struct strategy_struct), 1, fileStrat);
}
=====================
.h file:
#include
#include
#include
#include
#define NUMBOARD 19683 /*equal to pow(3,9)*/
#define TICTACTOE_H
/*These types are used constantly; easier to read and type shortforms*/
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);
void get_record(FILE *fileStrat, unsigned short us, struct strategy_struct*r);
====================================
program 4 file:
#include "tictactoe.h"
/**
Name: Danielle Reyes
Date: January 28, 2018
Class: CIS 2500
Student Number: 1007924
**/
int main(int argc, char **argv)
{
/*Initilize variables*/
u16 us, i;
char b3[10];
char board[60];
/*Declare variable for the strategy file as a struct*/
struct strategy_struct strategyBuf;
/*Open the strategy file as a binary*/
FILE *fileLocation = fopen("strategyfile.bin", "rb");
/*Checks if an argument has been supplied*/
if (argc < 2)
{
printf("Error: there is no argument supplied ");
return 1;
}
/*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);**/
get_record (fileLocation, us, &strategyBuf);
/*Prints the best move and the winner*/
printf("best_move= %c, winner= %c ", strategyBuf.best_move, strategyBuf.winner);
fclose(fileLocation);
/*print out all the possible moves*/
for (i = 0; i < 9; i++)
{
printf("%d -> %d ", i, next(us, i));
}
return 0;
}
==============
Please fix any part of the code and let me know which parts you've added/changed so I can understand it. Thank you again!
Program 4 Review the program that you created in alp1.c and create a new program called alp4.c that also displays the winner and best_move values from the strategyfile for each board as follows: Board number 18324 Board b3: 221010200 Board pic: Move:5 Turn: 1 Winner best move-3, winner 0 -0 3 -> 18567 5 18351 6 ->0 -18327 8-18325Explanation / Answer
#include "TicTacToe.h"
int main(int argc, char **argv)
{
/*Initilize Vars*/
u16 us, i;
char b3[10];
char board[60];
/*Ensure that an argument has been supplied*/
if (argc < 2)
{
printf("Error:: no argument supplied ");
return 1;
}
/*Calculate information about the board*/
us = atoi(argv[1]);
b3fromus(b3, us);
boardfromb3(board, b3);
/*print board information*/
printf("Board number: %d ", us);
printf("Board b3: %s ", b3);
printf("Board pic: %s ", board); /*Using printBoardb3 would require an extra print statement for new lines*/
printf("Move: %d ", get_move(b3));
printf("Turn: %c ", get_turn(b3));
printf("Winner: %c ", winner(us));
/*print next possible moves*/
for (i = 0; i < 9; i++)
{
printf("%d -> %d ", i, next(us, i));
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.