-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattleship_game.cpp
212 lines (211 loc) · 5.79 KB
/
battleship_game.cpp
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <iostream>
#include <utility>
#include <string>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/wait.h>
using namespace std;
typedef pair<int,int> Point;
class Data{
public:
Point point;
int turnNum;
bool childPrint=false;
bool parentPrint=false;
int winBomb;
int childPid;
};
pair<Point,Point> setGunboat(int randSeed){
srand(randSeed);
int x=rand()%4,y=rand()%4,randNum=rand()%4;//得到第一個隨機位置xy和隨機變數
//總共4種情況會出界
int move[4][2]={{1,0},{0,1},{-1,0},{0,-1}},dx,dy;
if((x>2&&randNum==0)||(y>2&&randNum==1)||(x<1&&randNum==2)||(y<1&&randNum==3)){
randNum=(randNum+2)%4;
}
dx=x+move[randNum][0],dy=y+move[randNum][1];//第二個位置
Point p1(x,y),p2(dx,dy);
return pair<Point,Point>(p1,p2);
}
void printGunboat(int pid,pair<Point,Point> p){
if(pid==0){
cout<<"["<<getpid()<<" Child]:The gunboat: ";
}
else{
cout<<"["<<getpid()<<" Parent]:The gunboat: ";
}
cout<<"("<<p.first.first<<","<<p.first.second<<")("<<p.second.first<<","<<p.second.second<<")\n";
}
Point setBomb(int& randSeed){
srand(randSeed);
int x=rand()%4,y=rand()%4;//隨機炸
randSeed++;
return Point(x,y);
}
void printBomb(int pid,Point p){
if(pid==0){
cout<<"["<<getpid()<<" Child]:bombing: ";
}
else{
cout<<"["<<getpid()<<" Parent]:bombing: ";
}
cout<<"("<<p.first<<","<<p.second<<")\n";
}
int main(void){
pid_t pid;//process id;
int i=100;
int parentSeed,childSeed;
cout<<"請輸入Parent和Child的亂數種子和0(基本功能)\n";
cin>>parentSeed>>childSeed;//輸入亂數種子
int mode;
cin>>mode;
pid = fork();
int sharedSpace=shm_open("space",O_CREAT|O_TRUNC|O_RDWR,0666);//設置shared memory
size_t regionSize=sizeof(Data);
int r=ftruncate(sharedSpace,regionSize);
if(r==-1){
perror("size error");
exit(1);
}
Data* position=(Data*)mmap(NULL,sizeof(class Data),PROT_READ|PROT_WRITE,MAP_SHARED,sharedSpace,0);//在shared memory裡放Data
position->turnNum=0;
int childBombNum=0;
int parentBombNum=0;
bool isParentWin=true;
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
cout<<"["<<getpid()<<" Child]:Random Seed "<<childSeed<<endl;
srand(childSeed);
i = (rand() % 10) +1;
pair<bool,bool> HP(true,true);
Point bombPosition;
pair<Point,Point> gunboatPosition;
position->childPid=getpid();
while(true){
if(position->turnNum==1&&!position->childPrint){
gunboatPosition=setGunboat(childSeed);//設砲艇位置
printGunboat(pid,gunboatPosition);//印砲艇位置
position->turnNum--;
position->childPrint=true;
break;
}
}
bool change=false;
while(true){
if(position->turnNum==1){
if(change){
change=false;
if(gunboatPosition.first==bombPosition){
HP.first=false;//第一個位置被擊中
if(!HP.first&&!HP.second){//兩個都被擊中
cout<<"["<<getpid()<<" Child]: "<<"hit and sinking\n";
isParentWin=true;
position->winBomb=childBombNum;
break;
}
else{
cout<<"["<<getpid()<<" Child]: "<<"hit\n";
}
}
else if(gunboatPosition.second==bombPosition){
HP.second=false;//第二個位置被擊中
if(!HP.first&&!HP.second){//兩個都被擊中
cout<<"["<<getpid()<<" Child]: "<<"hit and sinking\n";
isParentWin=true;
position->winBomb=childBombNum;
break;
}
else{
cout<<"["<<getpid()<<" Child]: "<<"hit\n";
}
}
else{
cout<<"["<<getpid()<<" Child]: "<<"missed\n";
}
bombPosition=setBomb(childSeed);//設炸彈
printBomb(pid,bombPosition);//印炸彈
position->point=bombPosition;//傳炸彈位置進shared memory
childBombNum++;
}
position->turnNum--;//換對方
change=true;
}
}
}
else { /* parent process */
/* parent will wait for the child to complete */
cout<<"["<<getpid()<<" Parent]:Random Seed "<<parentSeed<<endl;
srand(parentSeed);
i = (rand() % 10) +1;
pair<bool,bool> HP(true,true);
Point bombPosition;
pair<Point,Point> gunboatPosition;
while(true){
if(position->turnNum==0&&!position->parentPrint){
gunboatPosition=setGunboat(parentSeed);//設砲艇位置
printGunboat(pid,gunboatPosition);//印砲艇位置
position->turnNum++;
position->parentPrint=true;
break;
}
}
bool change=false;
while(true){
if(position->turnNum==0){
if(change){
change=false;
if(gunboatPosition.first==bombPosition){
HP.first=false;//第一個位置被擊中
if(!HP.first&&!HP.second){//兩個都被擊中
cout<<"["<<getpid()<<" Parent]: "<<"hit and sinking\n";
isParentWin=false;
position->winBomb=parentBombNum;
break;
}
else{//只擊中一個
cout<<"["<<getpid()<<" Parent]: "<<"hit\n";
}
}
else if(gunboatPosition.second==bombPosition){
HP.second=false;//第二個位置被擊中
if(!HP.first&&!HP.second){//兩個都被擊中
cout<<"["<<getpid()<<" Parent]: "<<"hit and sinking\n";
isParentWin=false;
position->winBomb=parentBombNum;
break;
}
else{//只擊中一個
cout<<"["<<getpid()<<" Parent]: "<<"hit\n";
}
}
else{
if(parentBombNum>0){
cout<<"["<<getpid()<<" Parent]: "<<"missed\n";
}
}
bombPosition=setBomb(parentSeed);//設炸彈
printBomb(pid,bombPosition);//印炸彈
position->point=bombPosition;//傳炸彈位置進shared memory
parentBombNum++;
}
position->turnNum++;//換對方
change=true;
}
}
cout<<"["<<getpid()<<" Parent]: ";
if(isParentWin){
cout<<getpid()<<" wins with "<<(int)position->winBomb<<" bombs";
}
else{
cout<<position->childPid<<" wins with "<<(int)position->winBomb<<" bombs";
}
exit(0);
}
}