我一直在用C为一个类编写战列舰程序,我的print_node函数从我的head节点(currentState、ship_type、charInput等(返回值时遇到了问题。每次运行它时,它都会编译,但它总是输出"0"。我希望第二双眼睛能帮我解决这个问题。谢谢你,并原谅我的混乱代码。
#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 endn");
int size = scanf(" %c%d", c, i);
if(size != 2){
printf("INVALID INPUTn");
continue;
}
*c = toupper(*c);
if(*c == 'Z' && *i == 0)
break;
if (*c < 65 || *c > 74)
printf("INVALID INPUTn");
else if (*i <0 || *i >9)
printf("INVALID INPUTn");
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("%cn", h->charInput);
printf("%sn", 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 9n");
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
试试这个
void print_node(struct Node *h) {
if(h != null){
printf("%cn", h->charInput);
printf("%sn", h->ship_type);
print_node(h->next);
}
}
我注意到您在update_state中调用了打印函数,这意味着您每次移动都要存储到文件中。
数字1,您需要追加到文件中,而不是写入更改:
fPTR = fopen("log.txt", "w");
至:
fPTR = fopen("log.txt", "a");
并将您的写入功能更改为:
//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 EMPTYn");
}
else{
printf("nThis move was:n");
printf("%cn", h->charInput);
printf("%sn", h->ship_type);
printf("%dn", h->intInput);
printf("%sn", h->currentState);
fprintf(fPTR, "Fired at %c%d. %s - %s. n", h->charInput, h->intInput, h->currentState, h->ship_type);
fclose(fPTR);
}
}
这是假设你的链接列表头是正确创建的
编辑:此代码将播放最后一个动作并将其添加到文件中,无论何时开始新游戏,您都必须删除该文件。