-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattleship.c
184 lines (156 loc) · 4.63 KB
/
battleship.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N_SHIPS 5
typedef struct{
int[][] m;
int size;
}BattleField;
typedef struct{
int symbol; // 0->mar ou 1->navio ou -1->territorio inimigo
int row;
int column;
}Cell;
typedef struct{
int status=1; //0->morto ou 1->vivo;
int size;
int x1,x2;
int y1,y2;
ShipType x;
}Ship;
typedef struct{
int x;
int y;
}Shot;
typedef struct{
int carrier = 5;
int battleship = 4;
int cruiser = 3;
int submarine = 3;
int destroyer = 2;
}ShipType;
Ship* makeship(){
Ship.status=1;
Ship.x1
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void InsertShipManual(int m [l][c] ,Ship s, int nShip){
int linhas, colunas;
printf("Qual o número de linhas do tabuleiro?\n");
scanf("%d",&linhas);
printf("Qual o número de colunas do tabuleiro?\n");
scanf("%d",&colunas);
makeboard(linhas,colunas);
printboard(linhas,colunas);
makeship(); //introduzir navio manualmente (manual selection)
if(s.x1==s.x2)
//printf("Qual o navio que colocar? ", );
//scanf("%s\n");
}
//rand() % 1 + 0
/////////////////////////////////////////////////////////////////////////////////////////////////////
void InsertShipRandom(int[][] m,Ship s){//introduzir navio randomly(randomize setup)
int linhas,colunas;
printf("Qual o número de linhas do tabuleiro?\n");
scanf("%d",&linhas);
printf("Qual o número de colunas do tabuleiro?\n");
scanf("%d",&colunas);
makeboard(linhas,colunas);
printboard(linhas,colunas);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void giveShot(int shot[2]){
printf("Line: ");
scanf("%d",&shot[0]);
shot[0]--;
printf("Column: ");
scanf("%d",&shot[1]);
shot[1]--;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
int hitship(int shot[2], int ships[][2]){
int ship;
for(ship=0 ; ship < 5 ; ship++){
if( shot[0]==ships[ship][0] && shot[1]==ships[ship][1]){
printf("You hit a ship with the shot (%d,%d)\n",shot[0]+1,shot[1]+1);
return 1;
}
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void makeboard(int linhas,int colunas){
for(int i=0; i<linhas ; i++ )
for(int j=0 ; j<colunas ; j++ )
cell[i][j]=0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void printboard(int linhas,int colunas){
for(int i=1;i<=linhas;i++){
printf("%d\n",i);
}
for(int i=1;i<=colunas;i++){
printf("%d ",i );
}
printf("%d",i+1);
for(column=0 ; column < 5 ; column++ ){
if(cell.status=-1){
printf("\t~");
}
else if(cell.status==0){
printf("\t*");
}
else if(cell.status==1){
printf("\tX");
}
}
printf("\n");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
int main(){
int linhas,colunas;
char modojogo[10],playagain[4];
printf ("\n\n\n\n");
printf ("SSSSS SSSS SSSSSS SSSSSS SS SSSSSS SSSSS SS SS SS SSSS\n");
printf ("SS SS SS SS SS SS SS SS SS SS SS SS SS SS\n");
printf ("SSSSS SS SS SS SS SS SSSS SSSS SSSSSS SS SSSS\n");
printf ("SS SS SSSSSS SS SS SS SS SS SS SS SS SS\n");
printf ("SSSSS SS SS SS SS SSSSSS SSSSSS SSSSS SS SS SS SS\n");
printf ("\n\n\n\n");
printf("Qual o número de linhas do tabuleiro?\n");
scanf("%d",&linhas);
printf("Qual o número de colunas do tabuleiro?\n");
scanf("%d",&colunas);
printf("Qual a forma do jogo? Random ou Manual?\n");
scanf("%s",modojogo);
//makeboard(linhas,colunas);
//printboard(linhas,colunas);
while( strcmp(modojogo,"Random")!=0&& strcmp(modojogo,"random")!=0 && strcmp(modojogo,"Manual")!=0 && strcmp(modojogo,"manual")!=0){
printf("Modo de jogo inexistente.\nTente novamente.\nAs opcões existentes são Random e Manual.\n ");
scanf("%s",modojogo);
}
if(strcmp(modojogo,"Random")==0 || strcmp(modojogo,"random")==0){
InsertShipRandom();
}
else if(strcmp(modojogo,"Manual")==0 || strcmp(modojogo,"manual")==0){
InsertShipManual();
}
jogo();
printf("You want to play again? Yes or no?");
scanf("%s",playagain);
if(strcmp(playagain,"Yes")==0 || strcmp(playagain,"yes")==0){
jogo();
}
return 0;
}
void jogo(){
int mortes=0;
do{
giveShot(shot);
if(hitship(shot,ships)){
//retirar "vida" ao navio acertado
}
while(mortes!=5)
printf(" CONGRATULATIONS!\nYou sink all the ships and the win is yours\n\n\n\n\n\n\n");
}
}