This is for c programming. I need help creating the make file for the following
ID: 3740484 • Letter: T
Question
This is for c programming.
I need help creating the make file for the following assignment.
This is the SAMPLE OF A MAKEFILE:
gofish: gofish.o player.o board.o ship.o scanner.o main.c
gcc -Wall main.c gofish.o board.o player.o ship.o scanner.o -o gofish
gofish.o: scanner.c scanner.h player.c player.h ship.c ship.h board.c board.h
gcc -Wall -c gofish.c
scanner.o: scanner.c scanner.h
gcc -Wall -c scanner.c
player.o: player.c player.h ship.c ship.h board.c board.h
gcc -Wall -c player.c
ship.o: ship.c ship.h
gcc -Wall -c ship.c
board.o: board.c board.h
gcc -Wall -c board.c
clean:
rm *.salvos *.responses *.o gofish
///////THIS IS MY CURRENT MAIN FILE/////
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
//////////////////FUNCTIONS //////////////////
//void MergeSort(Card** headRef);
//Card* SortedMerge(Card* a, Card* b);
//void FrontBackSplit(Card* source, Card** frontRef, Card** backRef);
//Deck* createEmptyDeck();
//Deck* createFullDeck() ;
//Card* mergeCards(Card *card1, Card *card2);
//void sort(Deck *deck) ;
//Card* draw(Deck *deck) ;
//void insertIntoHand(Deck *hand, Card *newCard);
//void dealCards(Deck *startingDeck, Deck *hand, int numberOfCards);
//void freeDeck(Deck *deck);
//Card* splitCards(Card *cards);
//void shuffle(Deck *deck, int times);
//void printSortedDeck(Deck *deck);
//void printCard(Card *card);
//void checkBook(Player *player);
//////// ENUMS //////////////
enum Suit
{
HEARTS = 1,
SPADES = 2,
CLUBS = 3,
DIAMONDS = 4
};
enum Rank
{
Two = 0,
Three = 1,
Four = 2,
Five = 3,
Six = 4,
Seven = 5,
Eight = 6,
Nine = 7,
Ten = 8,
Jack = 9,
Queen = 10,
King = 11,
Ace = 12
};
char *ranks[] = {"two","three","four","five","six","seven","eight","nine","ten","jack","queen","king","ace"};
char *suits[] = {"","hearts","spades","clubs","diamonds"};
////////////////// CARD///////////////
typedef struct Card
{
int suit;
int rank;
struct Card *next;
} Card;
/////////////////DECK/////////////
typedef struct Deck
{
Card *cards;
} Deck;
////////////////// PLAYER/////////////////
typedef struct Player
{
Deck *hand;
Deck *books;
int numberOfBooks;
} Player;
void MergeSort(Card** headRef);
Card* SortedMerge(Card* a, Card* b);
void FrontBackSplit(Card* source, Card** frontRef, Card** backRef)
{
Card* fast;
Card* slow;
slow = source;
fast = source->next;
while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}
*frontRef = source;
*backRef = slow->next;
slow->next = NULL;
}
void MergeSort(Card** headRef)
{
Card* head = *headRef;
Card* a;
Card* b;
if ((head == NULL) || (head->next == NULL))
{
return;
}
FrontBackSplit(head, &a, &b);
MergeSort(&a);
MergeSort(&b);
*headRef = SortedMerge(a, b);
}
Card* SortedMerge(Card* a, Card* b)
{
Card* result = NULL;
if (a == NULL)
return(b);
else if (b==NULL)
return(a);
if (a->rank <= b->rank)
{
result = a;
result->next = SortedMerge(a->next, b);
}
else
{
result = b;
result->next = SortedMerge(a, b->next);
}
return(result);
}
Deck* createEmptyDeck()
{
return (Deck*)malloc(sizeof(Deck));
}
Deck* createFullDeck()
{
Deck *deck = createEmptyDeck();
Card *firstCard = (Card*)malloc(sizeof(Card));
firstCard->suit = 1;
firstCard->rank = 0;
deck->cards = firstCard;
Card *currentCard = firstCard;
int i;
for (i = 1; i <= 12; i++) {
Card *card = (Card*)malloc(sizeof(Card));
card->suit = 1;
card->rank = i;
currentCard->next = card;
currentCard = card;
}
int j;
for (j = 2; j <= 4; j++) {
for (i = 0; i<= 12 ; i++) {
Card *card = (Card*)malloc(sizeof(Card));
card->suit = j;
card->rank = i;
currentCard->next = card;
currentCard = card;
}
}
return deck;
}
Card* mergeCards(Card *card1, Card *card2)
{
Card *curr1 = card1, *curr2 = card2;
Card *next1, *next2;
while (curr1 != NULL && curr2 != NULL)
{
next1 = curr1->next;
next2 = curr2->next;
curr2->next = curr1;
curr1->next = next2;
curr1 = next1;
curr2 = next2;
}
return card2;
}
void sort(Deck *deck)
{
MergeSort(&(deck->cards));
}
Card* draw(Deck *deck)
{
Card *card = deck->cards;
deck->cards = card->next;
return card;
}
void insertIntoHand(Deck *hand, Card *newCard)
{
Card *currCard = hand->cards;
Card *prevCard = NULL;
while (currCard != NULL && currCard->rank < newCard->rank) {
prevCard = currCard;
currCard = currCard->next;
}
if (prevCard == NULL) {
newCard->next = hand->cards;
hand->cards = newCard;
return;
} else {
prevCard->next = newCard;
newCard->next = currCard;
return;
}
}
void dealCards(Deck *startingDeck, Deck *hand, int numberOfCards)
{
int i;
if (numberOfCards == 0) {
return;
}
hand->cards = startingDeck->cards;
Card *currCard = hand->cards;
for (i = 1; i < numberOfCards; i++) {
currCard = currCard->next;
}
startingDeck->cards = currCard->next;
currCard->next = NULL;
}
void freeDeck(Deck *deck)
{
deck->cards = NULL;
}
Card* splitCards(Card *cards)
{
Card *fast, *curr, *prev;
fast = cards;
curr = cards;
while (fast != NULL && fast->next != NULL) {
prev = curr;
curr = curr->next;
fast = fast->next->next;
}
prev->next = NULL;
return curr;
}
void shuffle(Deck *deck, int times)
{
Card *cards = deck->cards;
while (times > 0) {
cards = mergeCards(cards, splitCards(cards));
times--;
}
deck->cards = cards;
}
void printSortedDeck(Deck *deck)
{
Card *card = deck->cards;
int currRank = -1;
while(card != NULL)
{
if (currRank == card->rank)
{
printf(", ");
} else {
printf(" ");
}
printf("%s %s", ranks[card->rank], suits[card->suit]);
currRank = card->rank;
card = card->next;
}
}
void printCard(Card *card)
{
printf("[%s %s]", ranks[card->rank], suits[card->suit]);
}
void checkBook(Player *player)
{
Card *card = player->hand->cards;
Card *rankCardStart = card;
int currRank = card->rank;
int rankCount = 1;
Card *prevCard = card;
card = card->next;
Card *prevRankCard = NULL;
while (card != NULL) {
if (card->rank == currRank) {
rankCount++;
} else {
currRank = card->rank;
rankCardStart = card;
prevRankCard = prevCard;
rankCount = 1;
}
prevCard = card;
card = card->next;
if (rankCount == 4) {
if (prevRankCard == NULL) {
player->hand->cards = card;
}
else {
prevRankCard->next = card;
}
int i;
for (i=0;i<4;i++) {
Card *newCard = rankCardStart;
rankCardStart = rankCardStart->next;
newCard->next = NULL;
insertIntoHand(player->books,newCard);
player->numberOfBooks++;
}
prevCard = prevRankCard;
}
}
prevCard = NULL;
return;
}
int main ()
{
Deck *deck1 = createEmptyDeck();
deck1 = createFullDeck();
shuffle(deck1,5);
Player *player1 = (Player*)malloc(sizeof(Player));
player1->hand = createEmptyDeck();
player1->books = createEmptyDeck();
dealCards(deck1,player1->hand,47);
sort(player1->hand);
printSortedDeck(player1->hand);
//checkBook(player1);
//printSortedDeck(player1->hand);
//printSortedDeck(player1->books);
}
Explanation / Answer
Answer:
#include <iostream.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void displaygamerwindow();
void shiplocation();
void systemlocation();
void systemoption(int capacity1, int leftsidedirection, int upwards, int word_taken);
void systemwindow();
void locationselect(int capacity, int left, int up, int word_input);
char shiplabel[10], systemlabel[10];
int x,locatevertical, locatehorizontal,locateupwards,locateleftwards,computevertical, computehorizontal,computeupwards,computeleftwards;
char gamerboard [10][10]=
{
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
};
char hiddensystem [10][10]=
{
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
};
char displayedsystem [10][10]=
{
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
};
main()
{
HANDLE result;
result = GetStdHandle(STD_OUTPUT_HANDLE);
cout<<"Loading Please Wait : "<<endl;
Sleep(1000);
system("CLS");
cout<<"Loading Please Wait :"<<endl;
Sleep(1000);
system("CLS");
cout<<"Loading Please Wait :"<<endl;
Sleep(1000);
system("CLS");
cout<<"Loading Please Wait :"<<endl;
Sleep(1000);
system("CLS");
SetConsoleTextAttribute(result, FOREGROUND_BLUE|BACKGROUND_CYAN);
cout<<"Lets Play BattleShip Game :"<<endl;
SetConsoleTextAttribute(result, FOREGROUND_CYAN|FOREGROUND_GREEN|FOREGROUND_BLUE);
systemlocation();
systemwindow();
return 0;
}
void systemwindow()
{
computehorizontal=0;
computevertical=0;
cout << "" << endl;
cout << " System Board " << endl;
cout << "........................................................." << endl;
cout << " |0|1|2|3|4|5|6|7|8|9|" ;
cout<<' ';
cout << computehorizontal << "|";
do
{
if(computehorizontal == 9 && computevertical == 10)
{break;}
if(computevertical > 9 && computehorizontal < 9)
{
cout << "|"<< endl;
computehorizontal = computehorizontal + 1;
cout << (computehorizontal) << "|";
computevertical = 0;
}
if(computehorizontal < 10)
{
if(computevertical < 9)
{
cout << hiddensystem[computehorizontal] [computevertical] << " ";
}
if(computevertical > 8)
{
cout << hiddensystem[computehorizontal] [computevertical];
}
}
computevertical = computevertical + 1;
}
while(computehorizontal < 10);
computehorizontal = 0;
computevertical = 0;
cout << "|" << endl;
cout << "................................................................" << endl;
cout << "" << endl;
}
void systemlocation()
{
srand(time(NULL));
for (x=5;x>=2;x--)
{
switch(x)
{
case 5:
strcpy(systemlabel,"Carrier");
break;
case 4:
strcpy(systemlabel,"Battleship");
break;
case 3:
strcpy(systemlabel,"Kruiser");
break;
case 2:
strcpy(systemlabel,"Destroyer");
break;
}
do
{
computeleftwards=rand()%(10);
computeupwards=rand()%(10);
}
while((hiddensystem[computeleftwards][computeupwards]!=00));
hiddensystem[computeleftwards][computeupwards]=int(systemlabel[0]);
systemoption(x,computeleftwards,computeupwards, (int(systemlabel[0])));
}
do
{
computeleftwards=rand()%(10);
computeupwards=rand()%(10);
}
while((hiddensystem[computeleftwards][computeupwards]!=00));
hiddensystem[computeleftwards][computeupwards]=00;
systemoption(3,computeleftwards,computeupwards, 00);
}
void systemoption(int capacity1, int leftsidedirection, int upwards, int word_taken)
{
srand(time(NULL));
int select,circle;
select=1+rand()%(4);
switch (select)
{
case 1:
for(circle=1;circle<capacity1;circle++)
{
if(hiddensystem[leftsidedirection-circle][upwards]!=00 || int(leftsidedirection-(capacity1-1))<(0))
{
hiddensystem[leftsidedirection][upwards]=00;
x=x+1;
break;
}
if(int(leftsidedirection-(capacity1-1)) >= (0) && x==capacity1)
{
for(circle=1;circle<capacity1;circle++)
{hiddensystem[leftsidedirection-circle][upwards]=word_taken;}
systemwindow();
}
}
break;
case 2:
for(circle=1;circle<capacity1;circle++)
{
if(hiddensystem[leftsidedirection+circle][upwards]!=00 || int(leftsidedirection-(capacity1-1))>(9))
{
hiddensystem[leftsidedirection][upwards]=00;
x=x+1;
break;
}
if(int(leftsidedirection-(capacity1-1)) <= (9) && x==capacity1)
{
for(circle=1;circle<capacity1;circle++)
{hiddensystem[leftsidedirection+circle][upwards]=word_taken;}
systemwindow();
}
}
break;
case 3:
for(circle=1;circle<capacity1;circle++)
{
if(hiddensystem[leftsidedirection-circle][upwards]!=00 || int(leftsidedirection-(capacity1-1))>(9))
{
hiddensystem[leftsidedirection][upwards]=00;
x=x+1;
break;
}
if(int(upwards+(capacity1-1)) <= (9) && x==capacity1)
{
for(circle=1;circle<capacity1;circle++)
{hiddensystem[leftsidedirection][upwards+ circle]=word_taken;}
systemwindow();
}
}
break;
case 4:
for(circle=1;circle<capacity1;circle++)
{
if(hiddensystem[leftsidedirection-circle][upwards]!=00 || int(upwards-(capacity1-1))<(0))
{
hiddensystem[leftsidedirection][upwards]=00;
x=x+1;
break;
}
if(int(upwards-(capacity1-1)) >= (0) && x==capacity1)
{
for(circle=1;circle<capacity1;circle++)
{hiddensystem[leftsidedirection][upwards-circle]=word_taken;}
systemwindow();
}
}
break;
}
}
void displaygamerwindow()
{
locatehorizontal=0;
locatevertical=0;
cout << "" << endl;
cout << " Gamer 's Board" << endl;
cout << "................................................................." << endl;
cout << " |0|1|2|3|4|5|6|7|8|9|" ;
cout<<' ';
cout << locatehorizontal << "|";
do
{
if(locatehorizontal == 9 && locatevertical == 10)
{break;}
if(locatevertical > 9 && locatehorizontal < 9)
{
cout << "|"<< endl;
locatehorizontal = locatehorizontal + 1;
cout << (locatehorizontal) << "|";
locatevertical = 0;
}
if(locatehorizontal < 10)
{
if(locatevertical < 9)
{
cout << gamerboard[locatehorizontal] [locatevertical] << " ";
}
if(locatevertical > 8)
{
cout << gamerboard[locatehorizontal] [locatevertical]<<flush;
}
}
locatevertical = locatevertical + 1;
}
while(locatehorizontal < 10);
locatehorizontal = 0;
locatevertical = 0;
cout << "|" << endl;
cout << "................................................................................" << endl;
cout << "" << endl;
}
void locationselect(int capacity,int left,int up,int word_input)
{
int select,circle;
cout<<"Do you need any ship to face: "<< ' ' << "1. UP 2.DOWN 3.RIGHT 4.LEFT"<<' ';
cin>>select;
switch (select)
{
case 1:
for(circle=1;circle<capacity;circle++)
{
if(gamerboard[left-circle][up]!=00 || int(left-(capacity-1))<(0))
{
gamerboard[left][up]=00;
cout<<"Don't place ships illegally..."<<endl;
x=x+1;
break;
}
if(int(left-(capacity-1)) >= (0) && x==capacity)
{
for(circle=1;circle<capacity;circle++)
{gamerboard[left-circle][up]=word_input;}
displaygamerwindow();
}
}
break;
case 2:
for(circle=1;circle<capacity;circle++)
{
if(gamerboard[left+circle][up]!=00 || int(left-(capacity-1))>(9))
{
gamerboard[left][up]=00;
cout<<"Please check ships place correctly."<<endl;
x=x+1;
break;
}
if(int(left-(capacity-1)) <= (9) && x==capacity)
{
for(circle=1;circle<capacity;circle++)
{gamerboard[left+circle][up]=word_input;}
displaygamerwindow();
}
}
break;
case 3:
for(circle=1;circle<capacity;circle++)
{
if(gamerboard[left-circle][up]!=00 || int(left-(capacity-1))>(9))
{
gamerboard[left][up]=00;
cout<<"Please check ships place correctly"<<endl;
x=x+1;
break;
}
if(int(up+(capacity-1)) <= (9) && x==capacity)
{
for(circle=1;circle<capacity;circle++)
{gamerboard[left][up+ circle]=word_input;}
displaygamerwindow();
}
}
break;
case 4:
for(circle=1;circle<capacity;circle++)
{
if(gamerboard[left-circle][up]!=00 || int(up-(capacity-1))<(0))
{
gamerboard[left][up]=00;
cout<<"Please check ships place correctly"<<endl;
x=x+1;
break;
}
if(int(up-(capacity-1)) >= (0) && x==capacity)
{
for(circle=1;circle<capacity;circle++)
{gamerboard[left][up-circle]=word_input;}
displaygamerwindow();
}
}
break;
}
}
void shiplocation()
{
for (x=5;x>=2;x--)
{
switch(x)
{
case 5:
strcpy(shiplabel,"Carrier");
break;
case 4:
strcpy(shiplabel,"Battleship");
break;
case 3:
strcpy(shiplabel,"Kruiser");
break;
case 2:
strcpy(shiplabel,"Destroyer");
break;
}
do
{
cout<<"Locating one end of the "<<shiplabel<<"."<<' ';
cout<<"Place left side coordinate followed by top :"<< ' ';
cin>>locateleftwards;
cin>>locateupwards;
}
while(locateleftwards<0||locateleftwards>9||locateupwards<0||locateupwards>9||(gamerboard[locateleftwards][locateupwards]!=00));
gamerboard[locateleftwards][locateupwards]=int(shiplabel[0]);
displaygamerwindow();
locationselect(x,locateleftwards,locateupwards, (int(shiplabel[0])));
}
do
{
cout<<"Locate one end of the Submarine."<<' ';
cout<<"Place left side coordinate followed by top "<< ' ';
cin>>locateleftwards;
cin>>locateupwards;
}
while(locateleftwards<0||locateleftwards>9||locateupwards<0||locateupwards>9||(gamerboard[locateleftwards][locateupwards]!=00));
gamerboard[locateleftwards][locateupwards]=00;
displaygamerwindow();
locationselect(3,locateleftwards,locateupwards, 00);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.