-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
190 lines (168 loc) · 7.82 KB
/
main.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
// "**********************************************************\n";
// " Programmed by: Hanran Yang\n";
// " CS2 : MW: 10:45a - 12:10p, TTH: 10:45a - 12:50p\n";
// " Final Project - Battleship \n";
// "**********************************************************\n";
#include "header.h"
using namespace std;
/******************************************************************************
*
* Final Project - Battleship
* ____________________________________________________________________________
* This program is a guessing game for two players
*
* It is played on ruled grids on which each player's fleet of ships are
* marked. The locations of the fleets are concealed from the other player.
* Players alternate turns calling "shots" at the other player's ships,
* and the objective of the game is to destroy the opposing player's fleet.
*
* RULE:
* Before play begins, each player secretly arranges their ships on their
* primary grid. Each ship occupies a number of consecutive squares on the
* grid, arranged either horizontally or vertically. The number of squares
* for each ship is determined by the type of the ship. The ships cannot
* overlap ( only one ship can occupy any given square in the grid).
* The types and numbers of ships allowed are the same for each player.
* (Wikipedia)
* ____________________________________________________________________________
* OUTPUT:
* The game, including two board
*
* INPUT:
* game_mod //Input- game mod, 1(play), 0(end)
*
* ARRAY:
* board //Array- board(0 is you, 1 is enemy)
* hit_board_palyer1 //Array- Palyer1's attack record
* hit_board_palyer2 //Array- Palyer2's attack record
*
* Processing:
* game_status //Processing- current game status
* round //Processing- players round
******************************************************************************/
int main()
{
PlayerBoard board[2]; //Array- board(0 is you, 1 is enemy)
char hit_board_palyer1[10][10]; //Array- Palyer1's attack record
char hit_board_palyer2[10][10]; //Array- Palyer2's attack record
int game_status; //Processing- current game status
char game_mod; //Input- game mod
int round; //Processing- players round
game_status = 1; //Processing- 1(play), 0(end)
round = 0; //0 is player1, 1 is player2
//initialize All cell to space
clear_board(board[0]);
clear_board(board[1]);
clear_hit_board(hit_board_palyer1);
clear_hit_board(hit_board_palyer2);
//initialize name, size and hitcount.
initFleet(board[0]);
initFleet(board[1]);
do{
cout << "play against another player(P) or against the computer(C)?";
cin >> game_mod;
//Game mod, P is PVP, C is PVE
if(game_mod == 'P')
{
cout << "\nYou will against another player(PVP)\n";
//display Boards, let 2 user set ships
initBoard(board[0] ,board[1]);
//display a empty board
displayBoards(board[0].a_board,board[1].a_board);
//draw all ships back to the board
draw_board_with_ship(board[0]);
draw_board_with_ship(board[1]);
//0 is player1, 1 is player2
do{
if(round == 0)
{
cout << "Player 1:\n";
//attack_ship_info- get shot location from player
attack_ship_info(board[1], hit_board_palyer1);
//True when all ships hit point is zero
if(all_ships_sink(board[1]))
{
cout << "\nYou sunk the fleet!!! You win!!!\n";
game_status = 0;
cout << "\n Game result: \n";
cout << "\nPlayer2's board[left], Player2's hit board[right] ";
displayBoards(board[1].a_board,hit_board_palyer2);
cout << "\nPlayer1(Winer)'s board[left], Player1(Winer)'s hit board[right] ";
}
displayBoards(board[0].a_board,hit_board_palyer1);
round = 1;
}
else
{
cout << "Player 2:\n";
//attack_ship_info- get shot location from player
attack_ship_info(board[0], hit_board_palyer2);
//True when all ships hit point is zero
if(all_ships_sink(board[0]))
{
cout << "\nYou sunk the fleet!!! You win!!!\n";
game_status = 0;
cout << "\n Game result: \n";
cout << "\nPlayer1's board[left], Player1's hit board[right] ";
displayBoards(board[0].a_board,hit_board_palyer1);
cout << "\nPlayer2(Winer)'s board[left], Player2(Winer)'s hit board[right] ";
}
displayBoards(board[1].a_board,hit_board_palyer2);
round = 0;
}
}while(game_status == 1);
}
else if(game_mod == 'C')
{
cout << "\nYou will against the computer(PVE)\n";
//display Boards, let user set ships, AI will automate set ship
PVE_initBoard(board[0] ,board[1]);
//draw all ships back to the board
draw_board_with_ship(board[0]);
draw_board_with_ship(board[1]);
do{
if(round == 0)
{
cout << "\nPlayer:\n";
//attack_ship_info- get shot location from player
attack_ship_info(board[1], hit_board_palyer1);
//True when all ships hit point is zero
if(all_ships_sink(board[1]))
{
cout << "\nPlayer sunk the fleet!!! You win!!!\n";
game_status = 0;
cout << "\n Game result: \n";
cout << "\nComputer's board[left], Computer's hit board[right] ";
displayBoards(board[1].a_board,hit_board_palyer2);
cout << "\nPlayer(Winer)'s board[left], Player(Winer)'s hit board[right] ";
}
displayBoards(board[0].a_board,hit_board_palyer1);
round = 1;
}
else
{
cout << "Computer:\n";
//AI_attack_ship_info- get shot location from AI
AI_attack_ship_info(board[0], hit_board_palyer2);
//True when all ships hit point is zero
if(all_ships_sink(board[0]))
{
cout << "\nComputer sunk the fleet!!! Computer win!!!\n";
game_status = 0;
cout << "\n Game result: \n";
cout << "\nPlayer's board[left], Player's hit board[right] ";
displayBoards(board[0].a_board,hit_board_palyer1);
cout << "\nComputer(Winer)'s board[left], Computer(Winer)'s hit board[right] ";
}
displayBoards(board[1].a_board,hit_board_palyer2);
round = 0;
}
}while(game_status == 1);
}
else
{
cout << "\nError: invalid game mod";
}
}while(game_mod != 'P' && game_mod != 'C');
return 0;
}