im doing a battleship game program for my project.
here's my code so far
Code:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
int main_menu(char board1[10][10],char board2[10][10]){
int choice;
printf(" \n_____________\n");
printf("|--Main Menu--|\n");
printf("| |\n");
printf("|[1]Game Start|\n"); //print main menu
printf("|[2]Logout |\n");
printf(" ~~~~~~~~~~~~~\n\n");
do{
printf("Enter Choice: ");
scanf("%d",&choice);
if (choice==2) {
return 0;
}
else if (choice!=1){
printf("Choice invalid!\n");
}
}while(choice!=2 && choice!=1);
do{
printf("\n[1]Single Player\n");
printf("[2]Multiplayer\n");
printf("[3]Go back\n");
scanf("%d", &choice);
switch(choice){
case 1:
setBoard1(board1,board2);
printBoard1(board1,board2);
setShip(board1,board2);
break;
case 2:
return 0;
break;
case 3:
main_menu(board1,board2);
break;
default:printf("Choice invalid!");
break;
}
}while(choice!=3);
}
int setBoard1(char board1[10][10], char board2[10][10]){
int row,col;
for (row=0;row<10;row++){
for(col=0;col<10;col++){
board1[row][col]=(char)247;
board2[row][col]=(char)247;
}
}
}
int printBoard1(char board1[10][10],char board2[10][10]){
int row,col;
printf(" Player 1 Player 2\n\n");
for(row=0;row<10;row++){
printf("%c",(char)row+65);
for(col=0;col<10;col++){
printf("%3c",board1[row][col]);
}
printf(" |||");
for(col=0;col<10;col++){
printf("%3c",board2[row][col]);
}
printf(" %c",(char)row+65);
printf("\n\n");
}
printf(" ");
for(col=1;col<=10;col++){
printf("%3d",col);
}
printf(" ");
for(col=1;col<=10;col++){
printf("%3d",col);
}
putchar('\n');
}
int setShip(char board1[10][10],char board2[10][10]){
int i,j,choice;
char row, direction[5];
int col;
printf(" # ship size");
printf("\n[1] x1 Aircraft Carrier 5\n");
printf("[2] x1 Battleship 4\n");
printf("[3] x1 Cruiser 3\n");
printf("[4] x2 Destroyer 2\n");
printf("[5] x2 Cruiser 1\n\n");
printf("Choose a ship:");
scanf("%d",&choice);
switch (choice){
case 1:
printf("-- Aircraft Carrier --\n");
printf("Place Head(row,col): ");
printf("row: ");
scanf("%c",&row);
getchar();
printf("col: ");
scanf("%d",&col);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
default:
break;
}
printf("From head (up,down,left,right): ");
scanf("%s", direction);
printf("%s", direction);
if(strcmp(direction,"up")==0){
if (choice==1){
for(i=0;i<5;i++){
board1[(int)row-65][col-1]= 'A';
row--;
}
}
printBoard1(board1,board2);
}
}
main(){
char board1[10][10];
char board2[10][10];
printf("Hello! Welcome to Battleship C!\n");
printf("What would you like to do?\n\n");
if (main_menu(board1,board2)==0){
return 0;
}
}
my problem is in this function below
Code:
int setShip(char board1[10][10],char board2[10][10]){ int i,j,choice;
char row, direction[5];
int col;
printf(" # ship size");
printf("\n[1] x1 Aircraft Carrier 5\n");
printf("[2] x1 Battleship 4\n");
printf("[3] x1 Cruiser 3\n");
printf("[4] x2 Destroyer 2\n");
printf("[5] x2 Cruiser 1\n\n");
printf("Choose a ship:");
scanf("%d",&choice);
switch (choice){
case 1:
printf("-- Aircraft Carrier --\n");
printf("Place Head(row,col): ");
printf("row: ");
scanf("%c",&row);
getchar();
printf("col: ");
scanf("%d",&col);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
default:
break;
}
printf("From head (up,down,left,right): ");
scanf("%s", direction);
printf("%s", direction);
if(strcmp(direction,"up")==0){
if (choice==1){
for(i=0;i<5;i++){
board1[(int)row-65][col-1]= 'A';
row--;
}
}
printBoard1(board1,board2);
}
}
then when i print it the values doesnt change to 'A'... why?
any help is appreciated
i cannot set the values to 'A' and print it..