bin node search in map - c

I try to search in a map I created. Each cell have 3 branches. When A is the closest, B after and C is the farthest. I'm trying to find the shortest route.
It is possible, for instance, that b->c->c->a is closer than a->a->a->a->a.
What should I do?
gag *pathfinder(gag *root, int choice)
{
if (root == NULL)
{
printf_s("the map didnt exits.");
getchar();
exit(1);
}
if (choice == root->index)
{
printf_s("(%d,%d)", root->x, root->y);
return root;
}
if (choice != root->index)
if (root->A != NULL)
{
if (root->A->index != NULL && root->A->index == choice)
;
else
{
pathfinder(root->A, choice);
}
}
else if (choice != root->index)
if (root->B != NULL)
{
if (root->B->index != NULL && root->B->index == choice)
; // printf_s("(%d,%d)->", root->B->x, root->B->y);
else
{
pathfinder(root->B, choice);
}
}
else if (choice != root->index)
if (root->C != NULL)
{
if (root->C->index != NULL && root->C->index == choice)
; //
printf_s("(%d,%d)->", root->C->x, root->C->y);
else
{
pathfinder(root->C, choice);
printf_s("(%d,%d)->", root->C->x, root->C->y);
}
}
}
gag* path(gag *map, gag* tree, int len) {
char temp = map->index;
gag* hold;
tree->index = temp;
static int c = -1;
int *close;
c++;
(map)->index = 'v'; // Mark as visited.
(map)->entries++;
//go A
if (!map->A || map->A->index == 'v' || map->A->entries == 3)
tree->A = NULL;
else {
hold = malloc(sizeof(gag)*len);
d_p_s(map, hold);
tree->A = path(map->A, hold, len);
}
//go B
if (!map->B || map->B->index == 'v' || map->B->entries == 3)
tree->B = NULL;
else {
hold = malloc(sizeof(gag)*len);
d_p_s(map, hold);
tree->B = path(map->B, hold, len);
}
//go C
if (!map->C || map->C->index == 'v' || map->C->entries == 3)
tree->C = NULL;
else {
hold = malloc(sizeof(gag*)*len);
d_p_s(map, hold);
tree->C = path(map->C, hold, len);
}
map->index = temp;
return tree;
}

Related

How to skip NULL file

My code traverses through command line arguments. if the command line contains "alice.txt" index zero of an empty array is filled with "alice.txt" This also applies to "anh.txt" if it is present, then index 1 is filled with "anh.txt". When i type in the command line ./wc alice.txt anh.txt it will print out words line and characters of both files. however if I just do /wc anh.txt i will get "NULL FILE" I suspect this is because of index zero being NULL, I wrote it so it would print "NULL FILE" and then exit the program. How can I modify my code so it doesn't exit, rather skips to the next index? This is what I have for code:
int main(int argc, char **argv)
{
int argArray[argc];
char *nameArray[argc];
for (int i = 1; i < argc; i ++)
{
if (strcmp(argv[i], "-l") == 0)
{
argArray[0] = 1;
}
if (strcmp(argv[i], "-w") == 0)
{
argArray[1] = 1;
}
if (strcmp(argv[i], "-c") == 0)
{
argArray[2] = 1;
}
if (strcmp(argv[i], "alice.txt") == 0)
{
nameArray[0] = "alice.txt";
}
if (strcmp(argv[i], "anh.txt") == 0)
{
nameArray[1] = "anh.txt";
}
if ((strcmp(argv[i], "-l") != 0) && (strcmp(argv[i], "-c") != 0 ) && (strcmp(argv[i], "-w") != 0 ) && (strcmp(argv[i], "-w") != 0) && (strcmp(argv[i], "alice.txt") != 0) && (strcmp(argv[i], "anh.txt") != 0))
{
printf("%s is an invalid argument or file\n", argv[i]);
}
}
if ((argArray[0] != 1) && (argArray[1] != 1) && (argArray[2] != 1))
{
argArray[0] = 1;
argArray[1] = 1;
argArray[2] = 1;
}
int *myArrayAlice = get_counts2(nameArray[0]);
print_counts(argArray, myArrayAlice, *nameArray);
printf(" alice.txt\n");
int *myArrayAnh = get_counts(nameArray[1]);
print_counts(argArray,myArrayAnh, *nameArray);
printf(" anh.txt\n");
int *myArrayTotal2 = get_counts2(nameArray[1]);
print_counts(argArray, myArrayTotal2, *nameArray);
printf(" total\n");
return 0;
}
int *get_counts(char *filename)
{
FILE *file = fopen(filename, "r");
if (file == NULL)
{
printf("NULL FILE");
//exit(1);
}
int c;
bool whitespace = true;
int *arr = calloc(3,sizeof(int));
for(;;)
{
c = fgetc(file);
if (c == EOF)
{
break;
}
else if (c == '\n')
{
arr[0]++;
}
else if (whitespace && !isspace(c))
{
arr[1]++;
whitespace = false;
}
else if (!whitespace && isspace(c))
{
whitespace = true;
}
arr[2]++;
}
fclose(file);
return arr;
}
int *get_counts2(char *filename)
{
FILE *file = fopen(filename, "r");
if (file == NULL)
{
printf("NULL FILE");
//exit(1);
}
int c;
bool whitespace = true;
static int arr[3] = {0,0,0};
for(;;)
{
c = fgetc(file);
if (c == EOF)
{
break;
}
else if (c == '\n')
{
arr[0]++;
}
else if (whitespace && !isspace(c))
{
arr[1]++;
whitespace = false;
}
else if (!whitespace && isspace(c))
{
whitespace = true;
}
arr[2]++;
}
fclose(file);
return arr;
}

Some Errors in my X and O game ( C language)

What I'm trying to do here is a tie toe game, but when my code enters the do - while part, it ends the process by itself. Since I could not solve this part, I did not have a chance to try whether there are other problems with the code, unfortunately, I would be glad if you could help with this issue.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
char box_area[] = { '0','1','2','3','4','5','6','7','8','9' };
struct Player {
bool turn;
char mark;
int ID;
};
int main()
{
int return_result;
int player_number;
struct Player player1;
struct Player player2;
struct Player users[2];
users[0] = player1;
users[1] = player2;
(player1.mark = 'X') && (player2.mark = 'O');
(player1.turn = true) && (player2.turn = false);
(player1.ID = 1) && (player2.ID = 2);
do
{
box_creat();
for (int i = 0; i < 2; i++) {
if (users[i].turn == true)
{
make_move(users[i].ID);
box_creat();
users[i].turn = false;
users[i + 1].turn = true; \\ I made the logic of this section wrong
\\I will try to fix it, I realized after I sent the question
}
}
return_result = check_the_winner();
} while (return_result == 1 || return_result == -1);
return 0;
}
void box_creat(void) {
printf("| %c | %c | %c |", box_area[0], box_area[1], box_area[2]);
printf("\n\n");
printf("| %c | %c | %c |", box_area[3], box_area[4], box_area[5]);
printf("\n\n");
printf("| %c | %c | %c |", box_area[6], box_area[7], box_area[8]);
}
void make_move(int Player_ID)
{
int choice;
printf("Please select a area between 0-9 ");
scanf("%d", choice);
if (choice == '0' && box_area[0] == '0')
{
if (Player_ID == 1) {
box_area[0] = 'X';
}
else {
box_area[0] = 'O';
}
}
else if (choice == '1' && box_area[1] == '1')
{
if (Player_ID == 1) {
box_area[1] = 'X';
}
else {
box_area[1] = 'O';
}
}
else if (choice == '2' && box_area[2] == '2')
{
if (Player_ID == 1) {
box_area[2] = 'X';
}
else {
box_area[2] = 'O';
}
}
else if (choice == '3' && box_area[3] == '0')
{
if (Player_ID == 1) {
box_area[3] = 'X';
}
else {
box_area[3] = 'O';
}
}
else if (choice == '4' && box_area[4] == '0')
{
if (Player_ID == 1) {
box_area[4] = 'X';
}
else {
box_area[4] = 'O';
}
}
else if (choice == '5' && box_area[5] == '0')
{
if (Player_ID == 1) {
box_area[5] = 'X';
}
else {
box_area[5] = 'O';
}
}
else if (choice == '6' && box_area[6] == '0')
{
if (Player_ID == 1) {
box_area[6] = 'X';
}
else {
box_area[6] = 'O';
}
}
else if (choice == '7' && box_area[7] == '0')
{
if (Player_ID == 1) {
box_area[7] = 'X';
}
else {
box_area[7] = 'O';
}
}
else if (choice == '8' && box_area[8] == '0')
{
if (Player_ID == 1) {
box_area[8] = 'X';
}
else {
box_area[8] = 'O';
}
}
}
int check_the_winner(void)
{
if (box_area[0] && box_area[1] && box_area[2] == 'X' || 'O') {
return 1;
}
else if(box_area[3] && box_area[4] && box_area[5] == 'X' || 'O') {
return 1;
}
else if (box_area[6] && box_area[7] && box_area[8] == 'X' || 'O') {
return 1;
}
else if (box_area[2] && box_area[4] && box_area[6] == 'X' || 'O') {
return 1;
}
else if (box_area[0] && box_area[3] && box_area[6] == 'X' || 'O') {
return 1;
}
else if (box_area[2] && box_area[8] && box_area[5] == 'X' || 'O') {
return 1;
}
else if (box_area[0] && box_area[4] && box_area[8] == 'X' || 'O') {
return 1;
}
else if (box_area[1] && box_area[4] && box_area[7] == 'X' || 'O') {
return 1;
}
else {
return -1;
}
}
I tried out your program and found a few glitches that needed revision as well as adding in some additional bits of code just to neaten things up a bit. Following is your code with those revisions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
char box_area[] = { '0','1','2','3','4','5','6','7','8','9' };
struct Player
{
bool turn;
char mark;
int ID;
};
void box_creat(void)
{
printf("| %c | %c | %c |", box_area[0], box_area[1], box_area[2]);
printf("\n\n");
printf("| %c | %c | %c |", box_area[3], box_area[4], box_area[5]);
printf("\n\n");
printf("| %c | %c | %c |", box_area[6], box_area[7], box_area[8]);
printf("\n"); /* Added for aesthetics */
}
void make_move(int Player_ID)
{
int choice;
printf("Player %d - please select a area between 0-9 ", Player_ID);
scanf("%d", &choice); /* Corrected missing ampersand */
printf("Player: %d makes a move\n", Player_ID);
if (choice == 0 && box_area[0] == '0') /* FYI - '0' is equivalent to integer value 48 */
{
if (Player_ID == 1)
{
box_area[0] = 'X';
}
else
{
box_area[0] = 'O';
}
}
else if (choice == 1 && box_area[1] == '1')
{
if (Player_ID == 1)
{
box_area[1] = 'X';
}
else
{
box_area[1] = 'O';
}
}
else if (choice == 2 && box_area[2] == '2')
{
if (Player_ID == 1)
{
box_area[2] = 'X';
}
else
{
box_area[2] = 'O';
}
}
else if (choice == 3 && box_area[3] == '3')
{
if (Player_ID == 1)
{
box_area[3] = 'X';
}
else
{
box_area[3] = 'O';
}
}
else if (choice == 4 && box_area[4] == '4')
{
if (Player_ID == 1)
{
box_area[4] = 'X';
}
else
{
box_area[4] = 'O';
}
}
else if (choice == 5 && box_area[5] == '5')
{
if (Player_ID == 1)
{
box_area[5] = 'X';
}
else
{
box_area[5] = 'O';
}
}
else if (choice == 6 && box_area[6] == '6')
{
if (Player_ID == 1)
{
box_area[6] = 'X';
}
else
{
box_area[6] = 'O';
}
}
else if (choice == 7 && box_area[7] == '7')
{
if (Player_ID == 1)
{
box_area[7] = 'X';
}
else
{
box_area[7] = 'O';
}
}
else if (choice == 8 && box_area[8] == '8')
{
if (Player_ID == 1)
{
box_area[8] = 'X';
}
else
{
box_area[8] = 'O';
}
}
}
int check_the_winner(void)
{
if ((box_area[0] == box_area[1]) && (box_area[1] == box_area[2]) && (box_area[0] != '0')) /* Corrected the testing for proper "and" conditioning */
{
return 1;
}
else if ((box_area[3] == box_area[4]) && (box_area[4] == box_area[5]) && (box_area[3] != '3'))
{
return 1;
}
else if ((box_area[6] == box_area[7]) && (box_area[7] == box_area[8]) && (box_area[6] != '6'))
{
return 1;
}
else if ((box_area[2] == box_area[4]) && (box_area[4] == box_area[6]) && (box_area[2] != '2'))
{
return 1;
}
else if ((box_area[0] == box_area[3]) && (box_area[3] == box_area[6]) && (box_area[0] != '0'))
{
return 1;
}
else if ((box_area[2] == box_area[5]) && (box_area[5] == box_area[8]) && (box_area[2] != '2'))
{
return 1;
}
else if ((box_area[0] == box_area[4]) && (box_area[4] == box_area[8]) && (box_area[4] != '4'))
{
return 1;
}
else if ((box_area[1] == box_area[4]) && (box_area[4] == box_area[7]) && (box_area[1] != '1'))
{
return 1;
}
else
{
return -1;
}
}
int main()
{
int return_result;
//int player_number; /* Compiler said that this wasn't being used */
int swap = 1;
int i;
struct Player player1;
struct Player player2;
struct Player *users[2]; /* Used these as address pointers to player "1" and player "2" */
users[0] = &player1; /* Before making these pointers, the users array just contained copies of the player structures */
users[1] = &player2;
(player1.mark = 'X') && (player2.mark = 'O');
(player1.turn = true) && (player2.turn = false);
(player1.ID = 1) && (player2.ID = 2);
do
{
box_creat();
swap += 1;
i = swap % 2;
if (i == 1)
{
make_move(users[1]->ID);
//box_creat(); /* Being performed at the top of the loop */
users[0]->turn = false;
users[1]->turn = true;
}
else
{
make_move(users[0]->ID);
//box_creat();
users[1]->turn = false;
users[0]->turn = true;
}
return_result = check_the_winner();
if (check_the_winner() == 1) /* Added when a winner has been sensed */
{
box_creat();
if (i == 1)
{
printf("Player 2 won!\n");
}
else
{
printf("Player 1 won!\n");
}
}
}
while (return_result != 1);
return 0;
}
I added comments to most of the places I had tweaked, but here are the highlights:
I shuffled some of the functions around so that the "main" function followed all of the helper functions (the compiler was giving warnings about the original sequence of the function positions).
I corrected the "choice" tests as "choice" is an integer, so the test of "choice" needed to be compared to an integer value (e.g. zero) as opposed to character '0' (which actually has a value of 48).
The tests for three letters in a row for either player do not work in that manner. Either a test needed to be made for each box for a certain row needed to tested for an "X" or an "O", and then if that test was true the test needed to be continued for the next box in the row, and then again for the third box in the row. In lieu of that route, the test was revised to basically say "if the character in the first box in the row is the same as the character in the second box in the row, and the character in the second box in the row is the same as the character in the third box in the row, and the first box does not contain a digit character (which means is contains an "X" or an "O"), then there is a winner.
It appears that an attempt was made to mirror the contents of "Player1" and "Player2" into an array of player structures. Although initially, structure "users[0]" contained the same data as "Player1" and structure "users[1]" contained the same data as "Player2", once the data in "Player1" and "Player2" were updated, those changes did to propagate over to the "users" array. So to utilize "users" in the spirit of this program, they were defined as pointers to their respective player structures.
Those were the most significant bits. The other bits were added for a logical conclusion of the game. Note that no logic was added for a draw.
Give that a test and see if that clarifies things and follows the spirit of the project.

Creating a binary search tree using a while loop, doesn't store all the elements nor traverses the tree properly

This is my code:
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
typedef struct node
{
int data;
struct node *left, *right;
} node;
node *create(int value)
{
node* temp = malloc(sizeof(node));
temp -> data = value;
temp -> left = NULL;
temp -> right = NULL;
return temp;
}
node *insert(node *root, int value)
{
if (root == NULL)
{
return create(value);
}
else
{
if (value < root -> data)
{
root -> left = insert(root -> left, value);
return root;
}
else if (value > root -> data)
{
root -> right = insert(root -> right, value);
return root;
}
}
return root;
}
void traverse(node *root)
{
if (root == NULL)
{
printf("Empty List!\n");
}
if (root -> left != NULL)
{
traverse(root -> left);
}
printf("%i ", root -> data);
if (root -> right != NULL)
{
traverse(root -> right);
}
}
bool search(node* root, int value)
{
node *temp = root;
while (temp != NULL)
{
if (temp -> data == value)
{
return true;
}
else if (value < temp -> data)
{
temp = temp -> left;
}
else
{
temp = temp -> right;
}
}
return false;
}
node* deleteNode(node* root, int value)
{
if (root == NULL)
{
printf("Empty Tree!\n");
return root;
}
if (value < root -> data)
{
root -> left = deleteNode(root -> left, value);
}
else if (value > root -> data)
{
root -> right = deleteNode(root -> right, value);
}
else
{
if (root -> left == NULL)
{
node *temp = root -> right;
free(root);
return temp;
}
else if (root -> right == NULL)
{
node *temp = root -> left;
free(root);
return temp;
}
}
return root;
}
int main(void)
{
node* root = NULL;
node* nodes[] = {NULL};
bool process;
int i = 0;
do
{
char input;
printf("Enter C to create a tree, I to insert an element, S to search an element, D to delete an element and T to traverse the tree - ");
scanf("%c", &input);
if (input == 'I' || input == 'i')
{
int value;
printf("Enter the data - ");
scanf("%i", &value);
fgetc(stdin);
nodes[i] = malloc(sizeof(node));
nodes[i] = insert(root, value);
char p;
printf("Enter 'y' to continue, 'n' to quit - ");
scanf("%c", &p);
fgetc(stdin);
if (p == 'y' || p == 'Y')
{
process = true;
}
else if (p == 'n' || p == 'N')
{
process = false;
}
else
{
printf("invalid input, exiting\n");
process = false;
}
i++;
}
else if (input == 'C' || input == 'c')
{
int value;
printf("Enter the data - ");
scanf("%i", &value);
fgetc(stdin);
root = insert(root, value);
char p;
printf("Enter 'y' to continue, 'n' to quit - ");
scanf("%c", &p);
fgetc(stdin);
if (p == 'y' || p == 'Y')
{
process = true;
}
else if (p == 'n' || p == 'N')
{
process = false;
}
else
{
printf("invalid input, exiting\n");
process = false;
}
}
else if (input == 'D' || input == 'd')
{
int value;
printf("Enter the data - ");
scanf("%i", &value);
fgetc(stdin);
deleteNode(root, value);
char p;
printf("Enter 'y' to continue, 'n' to quit - ");
scanf("%c", &p);
fgetc(stdin);
if (p == 'y' || p == 'Y')
{
process = true;
}
else if (p == 'n' || p == 'N')
{
process = false;
}
else
{
printf("invalid input, exiting\n");
process = false;
}
}
else if (input == 'S' || input == 's')
{
int value;
printf("Enter value - ");
scanf("%i", &value);
fgetc(stdin);
if (search(root, value))
{
printf("Found\n");
}
else
{
printf("Not Found\n");
}
char p;
printf("Enter 'y' to continue, 'n' to quit - ");
scanf("%c", &p);
fgetc(stdin);
if (p == 'y' || p == 'Y')
{
process = true;
}
else if (p == 'n' || p == 'N')
{
process = false;
}
else
{
printf("invalid input, exiting\n");
process = false;
}
}
else if (input == 'T' || input == 't')
{
traverse(root);
char p;
printf("Enter 'y' to continue, 'n' to quit - ");
scanf("%c", &p);
fgetc(stdin);
if (p == 'y' || p == 'Y')
{
process = true;
}
else if (p == 'n' || p == 'N')
{
process = false;
}
else
{
printf("invalid input, exiting\n");
process = false;
}
}
else
{
printf("Invalid Input!\n");
}
}
while (process);
}
I want to create a binary search tree which takes input from the user, so i put the a while loop inside the main function which runs as long as the bool variable "process" is true.
it compiles ok, no function gives any seg fault or any error of other kind. But the search function works only on the last two elements i inserted, the traverse function only prints out the last two elements along with 0. I don't know what's worng with my code...

C Battleship Game Linked List print_node function

I've been working on a battleship program in C for a class and I am having trouble with my print_node function returning the values from my head node (currentState, ship_type, charInput etc.). Each time I run it, it compiles however it always outputs "0". I'm hoping a second set of eyes could help me figure this out. Thank you, and forgive me for the messy code.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#define SIZE 10
typedef struct Node{
char currentState;
char ship_type[20];
char charInput;
int intInput;
struct Node* next;
}Node;
char** initialization(){
int i, j, k, row, col;
char **board = (char**)malloc(sizeof(char*)*SIZE);
for (i = 0; i < SIZE; i++){
board[i] = (char*)malloc(sizeof(char)*SIZE);
}
for(i = 0; i < SIZE; i++){
for(j = 0; j < SIZE; j++){
board[i][j] = '-';
}
}
//place the first ship
//the direction of placement is hard coded
//you can change it if you want
bool placed1 = false;
while(placed1 == false){
row = rand()%10;
col = rand()%10;
if(row+1 < 10 && row-1 >= 0){
if((board[row][col] = '-') &&
(board[row+1][col] = '-')){
board[row][col] = 'D';
board[row+1][col] = 'D';
placed1 = true;
}
else if((board[row][col] == '-') &&
(board[row-1][col] == '-')){
board[row][col] = 'D';
board[row-1][col] = 'D';
placed1 = true;
}
}
}
//place the second ship
bool placed2 = false;
while (placed2 == false){
row = rand()%10;
col = rand()%10;
if((row+1 < 10 && row+2 < 10) &&
(row-1 >= 0 && row-2 >= 0) &&
(col+1 < 10 && col+2 < 10) &&
(col-1 >= 0 && col-2 >= 0)){
if((board[row][col] == '-') &&
(board[row][col+1] == '-') &&
(board[row][col+2] == '-')){
board[row][col] = 'C';
board[row][col+1] = 'C';
board[row][col+2] = 'C';
placed2 = true;
}
else if((board[row][col] == '-') &&
(board[row][col-1] == '-') &&
(board[row][col-2] == '-')){
board[row][col] = 'C';
board[row][col-1] = 'C';
board[row][col-2] = 'C';
placed2 = true;
}
else if((board[row][col] == '-') &&
(board[row+1][col] == '-') &&
(board[row+2][col] == '-')){
board[row][col] = 'C';
board[row+1][col] = 'C';
board[row+2][col] = 'C';
placed2 = true;
}
else if((board[row][col] == '-') &&
(board[row-1][col] == '-') &&
(board[row-2][col] == '-')){
board[row][col] = 'C';
board[row-1][col] = 'C';
board[row-2][col] = 'C';
placed2 = true;
}
}
}
//place the third ship
bool placed3 = false;
while (placed3 == false){
row = rand()%10;
col = rand()%10;
if((row+1 < 10 && row+2 < 10) &&
(row-1 >= 0 && row-2 >= 0) &&
(col+1 < 10 && col+2 < 10) &&
(col-1 >= 0 && col-2 >= 0)){
if((board[row][col] == '-') &&
(board[row][col+1] == '-') &&
(board[row][col+2] == '-')){
board[row][col] = 'S';
board[row][col+1] = 'S';
board[row][col+2] = 'S';
placed3 = true;
}
else if((board[row][col] == '-') &&
(board[row][col-1] == '-') &&
(board[row][col-2] == '-')){
board[row][col] = 'S';
board[row][col-1] = 'S';
board[row][col-2] = 'S';
placed3 = true;
}
else if((board[row][col] =='-') &&
(board[row+1][col] == '-') &&
(board[row+2][col] == '-')){
board[row][col] = 'S';
board[row+1][col] = 'S';
board[row+2][col] = 'S';
placed3 = true;
}
else if((board[row][col] == '-') &&
(board[row-1][col] == '-') &&
(board[row-2][col] == '-')){
board[row][col] = 'S';
board[row-1][col] = 'S';
board[row-2][col] = 'S';
placed3 = true;
}
}
}
bool placed4 = false;
while (placed4 == false){
row = rand()%10;
col = rand()%10;
if((row+1 < 10 && row+2 < 10 && row+3 < 10) &&
(row-1 >= 0 && row-2 >= 0 && row-3 >= 0) &&
(col+1 < 10 && col+2 < 10 && col+3 < 10) &&
(col-1 >= 0 && col-2 >= 0 && col-3 >= 0)){
if((board[row][col] == '-') &&
(board[row+1][col] == '-') &&
(board[row+2][col] == '-') &&
(board[row+3][col] == '-')){
board[row][col] = 'B';
board[row+1][col] = 'B';
board[row+2][col] = 'B';
board[row+3][col] = 'B';
placed4 = true;
}
else if((board[row][col] == '-') &&
(board[row-1][col] == '-') &&
(board[row-2][col] == '-') &&
(board[row-3][col] == '-')){
board[row][col] = 'B';
board[row-1][col] = 'B';
board[row-2][col] = 'B';
board[row-3][col] = 'B';
placed4 = true;
}
else if((board[row][col] == '-') &&
(board[row][col+1] == '-') &&
(board[row][col+2] == '-') &&
(board[row][col+3] == '-')){
board[row][col] = 'B';
board[row][col+1] = 'B';
board[row][col+2] = 'B';
board[row][col+3] = 'B';
placed4 = true;
}
else if((board[row][col] == '-') &&
(board[row][col-1] == '-') &&
(board[row][col-2] == '-') &&
(board[row][col-3] == '-')){
board[row][col] = 'B';
board[row][col-1] = 'B';
board[row][col-2] = 'B';
board[row][col-3] = 'B';
placed4 = true;
}
}
}
bool placed5 = false;
while (placed5 == false){
row = rand()%10;
col = rand()%10;
if((row+1 < 10 && row+2 < 10 && row+3 < 10 && row+4 < 10) &&
(row-1 >= 0 && row-2 >= 0 && row-3 >= 0 && row-4 >= 0) &&
(col+1 < 10 && col+2 < 10 && col+3 < 10 && col+4 < 10) &&
(col-1 >= 0 && col-2 >= 0 && col-3 >= 0 && col-4 >= 0)){
if((board[row][col] == '-') &&
(board[row+1][col] == '-') &&
(board[row+2][col] == '-') &&
(board[row+3][col] == '-') &&
(board[row+4][col] == '-')){
board[row][col] = 'R';
board[row+1][col] = 'R';
board[row+2][col] = 'R';
board[row+3][col] = 'R';
board[row+4][col] = 'R';
placed5 = true;
}
else if((board[row][col] == '-') &&
(board[row-1][col] == '-') &&
(board[row-2][col] == '-') &&
(board[row-3][col] == '-') &&
(board[row-4][col] == '-')){
board[row][col] = 'R';
board[row-1][col] = 'R';
board[row-2][col] = 'R';
board[row-3][col] = 'R';
board[row-4][col] = 'R';
placed5 = true;
}
else if((board[row][col] == '-') &&
(board[row][col+1] == '-') &&
(board[row][col+2] == '-') &&
(board[row][col+3] == '-') &&
(board[row][col+4] == '-')){
board[row][col] = 'R';
board[row][col+1] = 'R';
board[row][col+2] = 'R';
board[row][col+3] = 'R';
board[row][col+4] = 'R';
placed5 = true;
}
else if((board[row][col] == '-') &&
(board[row][col-1] == '-') &&
(board[row][col-2] == '-') &&
(board[row][col-3] == '-') &&
(board[row][col-4] == '-')){
board[row][col] = 'R';
board[row][col-1] = 'R';
board[row][col-2] = 'R';
board[row][col-3] = 'R';
board[row][col-4] = 'R';
placed5 = true;
}
}
}
return board;
}
void update_state(char* state, char ** board, char character, int col){
int row;
char shipType[20];
row = character % 65;
if(board[row][col] == '-'){
strcpy(state, "MISS");
}
else{
strcpy(state, "HIT!");
/* add code to change the board to indicate
* that shot hit , for example you could change
* the corresponding letter back to '-'.
* but before this, you need to record the letter
* and corresponding ship type.
*/ //COMPLETED
char shipChar = board[row][col];
if(shipChar == 'R'){
strcpy(shipType,"Carrier");
printf("%s", shipType);
}
else if(shipChar == 'B'){
strcpy(shipType,"Battleship");
printf("%s", shipType);
}
else if(shipChar == 'S'){
strcpy(shipType,"Submarine");
printf("%s", shipType);
}
else if(shipChar == 'C'){
strcpy(shipType,"Cruiser");
printf("%s", shipType);
}
else if(shipChar == 'D'){
strcpy(shipType,"Destroyer");
printf("%s", shipType);
}
printf("%c", shipChar);
board[row][col] = '-'; //set target coordinates to '-'
}
/* add code to update temp node's attributes (i.e
* hit or miss, ship type, then insert the temp node
* the node into the linked list.
* You may need to write the insert node function
* (you can refer to the insert node function in lab5
* handout )
*/
struct Node *head, *tail;
head = tail = NULL;
insert_node(&head, &tail, col, row, *state, shipType);
print_node(head);
//check if game is over //completed
int m = 0;
int k, l;
for(k = 0; k < SIZE; k++){
for(l = 0; l < SIZE; l++){
if(board[k][l] == '-'){
m++;
if(m >= 100){
strcpy(state, "GAME OVER!");
}
}
}
}
}
int accept_input(char * c, int * i){
bool flag = true;
do{
printf("Enter a letter A-J and number 0-9 ex. B4 - enter Z0 to end\n");
int size = scanf(" %c%d", c, i);
if(size != 2){
printf("INVALID INPUT\n");
continue;
}
*c = toupper(*c);
if(*c == 'Z' && *i == 0)
break;
if (*c < 65 || *c > 74)
printf("INVALID INPUT\n");
else if (*i <0 || *i >9)
printf("INVALID INPUT\n");
else
flag = false;
}while(flag);
}
/*
char currentState;
char ship_type;
*/
void insert_node(struct Node **h, struct Node **t, int x, char y, char* state, char shipTyp){
//create new node with value given by int x, y
struct Node *temp;
if ((temp = (struct Node *)malloc(sizeof(struct Node))) == NULL){
printf("Node Allocation Failed \n");
exit(1);
}
//space for node obtained, copy values into node
temp->charInput = y; //store user character input
temp->intInput = x; //store user number input
temp->currentState = state; //store state
temp->ship_type[20] = shipTyp; //store shipType
temp->next = NULL;
if (*h == NULL){
//list is empty if so
*h = *t = temp;
}
else{ //list isnt empty, use *t to add node at the end
(*t)->next = temp;
*t = (*t)->next;
}
}
//will be converted to a write to file function
void print_node(struct Node *h){
if(h == NULL){
printf("The list is empty.\n");
}
else{
printf("Values in the list are: \n");
while(h != NULL) {
printf("%c\n", h->charInput);
printf("%s\n", h->ship_type);
h = h->next;
}
}
}
void display_state(char* state, char** board){
int i, j;
printf("\n**** %s ****\n", state);
printf(" 0 1 2 3 4 5 6 7 8 9\n");
for (i = 0; i < SIZE; i++){
printf("%c ", 65+i);
for (j = 0; j < SIZE; j++){
printf("%c ", board[i][j]);
}
printf("\n");
}
}
int teardown(char ** board){
int i;
for(i = 0; i < SIZE; i++)
free(board[i]);
free(board);
/* add code below to traverse the linkded list
* you should create a log file name "log.txt"
* traverse each node in the linked list, and
* write the information in each node into "log.txt"
* Each line should follow this format:
* Fired at A1. Hit - Carrier.
* Fired at C2. Miss.
* You may refer to the print_list function in lab5
* handout.
* In addition, remember to free the nodes of the
* linked list.
*/
return 0;
}
int main(void){
//
void print_node(struct Node*);
void insert_node(struct Node**, struct Node**, int, char, char*, char);
srand(time(NULL));
char** board;
char state[] = "GAME START";
char flag[] = "GAME OVER!";
char character;
int integer;
/* declare a linked list below*/ //COMPLETED
//struct Node* head = NULL;
//struct Node* second = NULL;
//head = (struct Node*)malloc(sizeof(struct Node));
//second = (struct Node*)malloc(sizeof(struct Node));
board = initialization();
do{
display_state(state, board);
if(display_state)
/*modify the accept_input function
* accept input function should return
* a temp node, which stores the current valid
* input (i.e. character and letter)
*/
accept_input(&character, &integer);
if(character == 'Z' && integer == 0)
break;
/*modify the update_state function
* update_state function should accept
* the head node of linked list and the
* temp node
*/
update_state(state, board, character, integer);
} while((character != 'Z' || integer != 0) && strcmp(state, flag) );
/*modify the teardown function
* tear_down function should accept
* a head node of linked list
*/
teardown(board);
return 0;
}
//modularize insertNode, etc. inito own functions
Try this
void print_node(struct Node *h) {
if(h != null){
printf("%c\n", h->charInput);
printf("%s\n", h->ship_type);
print_node(h->next);
}
}
I noticed you call your print function in update_state, which means you are storing to the file every move.
number 1, you need to append to the file and not write
change:
fPTR = fopen("log.txt", "w");
to:
fPTR = fopen("log.txt", "a");
And change your write function to:
//will be converted to a write to file function
void print_node(struct Node *h, FILE *fPTR){
printf("got this far");
if (h == NULL) {
printf("THE LIST IS EMPTY\n");
}
else{
printf("\nThis move was:\n");
printf("%c\n", h->charInput);
printf("%s\n", h->ship_type);
printf("%d\n", h->intInput);
printf("%s\n", h->currentState);
fprintf(fPTR, "Fired at %c%d. %s - %s. \n", h->charInput, h->intInput, h->currentState, h->ship_type);
fclose(fPTR);
}
}
This is assuming your linkedist head is created correctly
EDIT: This code gets the last move played and adds it to the file, you will have to delete the file whenever you start a new game.

Bus error 10 in Radix Tree C program

I'm writing a program which does operations on a Radix trie and I'm stuck at add() function, getting the Bus error 10.
void addRec(struct tNode *p, char *w) {
int matches = prefixMatch(p->word,w);
bool insert = true;
if ((p == root) ||
((matches > 0) && (matches < strlen(w)) && (matches == strlen(p->word)))) {
char *updatedWord = &w[matches];
printf("%s\n", updatedWord);
struct tNode *tmp = p->child;
while (tmp != NULL) {
if (tmp->word[0] == updatedWord[0]) {
insert = false;
addRec(tmp, updatedWord);
}
tmp = tmp->brother;
}
if (insert) {
addChild(p,updatedWord);
}
} else if ((matches > 0) && (matches == strlen(w)) && (matches == strlen(p->word))) {
if (p->count < 0) p->count = ++globalCount;
else printf("ignored");
} else if ((matches > 0) && (matches < strlen(w)) && (matches < strlen(p->word))) {
struct tNode *tmp = malloc(sizeof(struct tNode));
tmp->word = &p->word[matches];
p->word[matches+1] = '\0';
tmp->child = p->child;
tmp->count = p->count;
tmp->brother = NULL;
p->child = tmp;
p->count = -1;
}
}
The error appears to be somewhere inside the while loop but I can't figure out where and what's wrong exactly. Please help.

Resources