Your task in this assignment is to implement a simplified guessing
1. Your task in this assignment is to implement a simplified
guessing game, hinted by a well-known plastic board game
called Battleship. The computer arranges ships on the grid
structure in which each ship occupies a number of consecutive
positions on the grid. Those ships are arranged either
horizontally or vertically. The number of positions for each ship
is determined by the type of the ship. The ship cannot overlap
and only one ship can occupy any given position in the grid.
You can play a real version on the web such as this to get an
idea. The version to realize in this assignment is simpler and a
one-man based game in which you are bombing those ships
generated by your computer. The next sample session will
illustrate the entire scheme:
There are 6 ships. Must bomb 18 places to destroy all...
<<Pacific Ocean>>
A B C D E F G H I J
1 . . . . . . . . . .
2 . . . . . . . . . .
3 . . . . . . . . . .
4 . . . . . . . . . .
5 . . . . . . . . . .
6 . . . . . . . . . .
7 . . . . . . . . . .
8 . . . . . . . . . .
9 . . . . . . . . . .
10 . . . . . . . . . .
Enter position, "show", "init" or "quit": 4 G
You missed :(
<<Pacific Ocean>>
A B C D E F G H I J
1 . . . . . . . . . .
2 . . . . . . . . . .
3 . . . . . . . . . .
3. Enter position, "show", "init" or "quit": 8 B
You bomed same place... You missed :(
<<Pacific Ocean>>
A B C D E F G H I J
1 . . . . . . . . . .
2 . . . . . . . . . .
3 . . . . . . . . . .
4 . . . . . . - . . .
5 . . . . . . . . . .
6 . . . . . . . . . .
7 . . . . . . . . . .
8 . @ . . . . . . . .
9 . . . . . . . . . .
10 . . . . . . . . . .
Enter position, "show", "init" or "quit": init
There are 6 ships. Must bomb 18 places to destroy all...
<<Pacific Ocean>>
A B C D E F G H I J
1 . . . . . . . . . .
2 . . . . . . . . . .
3 . . . . . . . . . .
4 . . . . . . . . . .
5 . . . . . . . . . .
6 . . . . . . . . . .
7 . . . . . . . . . .
8 . . . . . . . . . .
9 . . . . . . . . . .
10 . . . . . . . . . .
Enter position, "show", "init" or "quit": show
<<Pacific Ocean>>
A B C D E F G H I J
1 . . . . . . . . . .
2 * . . . . . . . . .
4. 3 * . . . . * . . . .
4 * . . . . * . . . .
5 * . . . . * . . . .
6 . * . . . . * * * *
7 . * . . . . . . . .
8 . * . . * * . . . .
9 . . . * . . . . . .
10 . . . * . . . . . .
Enter position, "show", "init" or "quit": quit
Bye...
The default board size is 10 x 10 and the computer arranges 6
ships of sizes between 2 and 4. The user can type in the position
to aim and hit by a space-delimited pair of a row position
(number) and a column position (alphabet) of the grid. The
result is hit or miss and is shown with '@' or '-' respectively at
the chosen position of the board. The dot '.' indicates
a position for choice. A command "show" discloses the
computer's arrangement of ships using '*' (as well as '@' at a
damaged position). The command "init" is to reset the game by
rearranging ships. The game is over when all the ships are
destroyed. The ship arrangement is random. Generation either
horizontal or vertical layout is also chosen at random.
The novel coronavirus pandemic is currently affecting every
single part of this world, and we are all hoping that the medical
researchers can come up with a vaccine as soon as possible so
that we can resume our normal day to day activities. Personally,
this pandemic has affected me in a lot of ways from my daily
routine to college and work at the same time. Having to take
classes online has been a challenge to me and I haven’t able to
grasp things quite well. My nature as a person was always
5. indoors and the crisis hasn’t affected my indoor activities, but I
miss having a cup of coffee while walking in the streets
especially on a warm day. Indoor games like chess and ludo
with friends online has been a healthy dose of workout.
Growing up just knowing how to cook the basic meals, using
YouTube videos, I have now advanced my cooking skills, and
once I move out of my parent's house after school, for sure, I
will be able to cook tasty meals for myself.
Talking about social effects, In a way, I think the current
condition has brought an idea of fairness into the limelight.
This crisis didn’t choose class, race or gender, or any other
discrimination in society. It, however, affected elderly people
the most, but that is due to their level of physical well-being. If
we see the flip side of the coin, the discrimination between race
and ethnicity was on the rise during the beginning of the crisis.
We read articles about Chinese students being beaten by white
Americans. I just hope we don’t see that happening after the
crisis is over.
Food security is a huge necessity during this time when more
than 24 million people are out of a job here in the US just
within five weeks, ("Impacts of the COVID-19 crisis on
employment: (potential) solutions for informal/ self-employed
workers," n.d.). The fact that one can afford to get meals every
single day is something to appreciate and should not be taken
for granted. Some parts of the world people are dying of hunger
during this period owing to the lockdowns, and people cannot
work, and they cannot afford to buy food.
To me, social media has proven to be a very useful tool during
this period. I have been using Facebook, Instagram, and other
platforms to connect with friends and families, find ideas for
my projects, and keep up with news at all times. This period has
also shown the need to evaluate the information given because
some information can cause unnecessary fear and some
information can be so useful if taken seriously and acted upon
early enough e.g., when the first patient in Wuhan was detected,
if WHO had alerted people much earlier the world would not be
6. at this position but China chose to assume things ("Caribbean
COVID-19 food security & livelihoods impact survey | World
food program," n.d.).
With the current condition of lockdown around the US and other
countries, learning has shifted to online to ensure that students
are safe. The idea is brilliant, but its a challenge for classes that
require students to have one on one interaction with the
equipment. It might benefit students who take part in theory
studies on the larger part. Online learning does not also
incorporate social equity. Research by PEW research center
shows by the end of 2019, 29% of Americans could not afford
smartphones (Pew Research Center, 2019). What happens to the
future of children from these families? They need to read to
uplift their families.
Education should not just be in theory like online learning is
portraying. Students need to come into contact with real-world
studies to understand what the job market needs. Therefore we
should all stay at home and hope for the crisis to end as soon as
possible for the economy to resuscitate and physical learning to
continue.
prog5.cpp
1 /* One-man Battleship Game Akira's Version implementation
template.
2 Use -std=c++11 to utilize regex input check. (C) Akira
Kawaguchi */
3 #include <iostream>
4 #include <iomanip>
5 #include <vector>
6 #include <cassert>
7. 7 #include <sstream>
8 #include <algorithm>
9 #include <regex>
10 using namespace std;
11
12 const int DefaultBoardSize = 10; // default board size
is 10 x 10.
13 const int DefaultMinShipSize = 2; // default ship size
is [2, 4].
14 const int DefaultMaxShipSize = 4; //
15 const int DefaultShipCount = 6; // defulat ship count
in a map is 6.
16
17 enum class Command { INIT, SHOW, QUIT, BOMB };
18
19 class Cell {
20 enum mark { HIT, MISS, NONE };
21 mark m_status; // indicating bombed, missed, or not
selected.
22 bool m_ship; // indicating presence of ship.
23 public:
24 Cell() : m_status(NONE), m_ship(false) {}
25
26 friend ostream& operator<<(ostream& o, const Cell& c) {
27 switch (c.m_status) {
28 case HIT: o << '@'; break;
29 case MISS: o << '-'; break;
30 case NONE: o << '.'; break;
31 default: assert(false);
32 }
33 return o << ' ';
34 }
35 };
36
37 class Board {
38 string m_name; // map name.
8. 39 int m_size; // n x n seamap size.
40 vector<vector<Cell> > m_map; // 2D sea map.
41 public:
42 Board(const string& n ="my map", const int s
=DefaultBoardSize) :
43 m_name(n), m_size(s) {
44 vector<Cell> a_row(m_size);
45 for (int i = 0; i < m_size; ++i) m_map.push_back(a_row);
46 init();
47 }
48 void init(const int n =DefaultShipCount) { // randomly set
ships in the map.
49 // not implemented.
50 }
51 bool bomb(const int row, const int col) { // bomb the ship!
52 // not implemented.
53 return true;
54 }
55 bool isover() const {
56 // not implemented.
57 return false;
58 }
59 friend ostream& operator<<(ostream& o, const Board& m)
{
60 o << " <<" << m.m_name << ">>" << endl << " ";
61 for (int row = 0; row < m.m_size; ++row) o << (char)('A'
+ row) << ' ';
62 o << endl;
Page 1
prog5.cpp
63 for (int row = 0; row < m.m_size; ++row) {
9. 64 for (int col = 0; col < m.m_size; ++col) {
65 if (!col) o << setw(2) << right << 1 + row << ' ';
66 o << m.m_map[row][col];
67 }
68 o << endl;
69 }
70 return o;
71 }
72 };
73
74 /* Input parsing logic for this application. This one uses so-
called "regular
75 expressions" to prune out inconsistent input (i.e., syntactic
check) at
76 first and then apply more application-related inspections
(semantic check).
77 Note that there are three return values - a return value and
two non-const
78 references in the parameter list. */
79 Command parse(const int size, int& row, int& col) {
80 cout << "nEnter position, "show", "init" or "quit": ";
81 string line;
82 regex pattern1("show|init|quit"); // one word input.
83 regex pattern2("[0-9]+[[:blank:]]+[a-z]"); // two words with
space(s) between.
84
85 while (getline(cin, line)) { // read a whole line and convert
to lower cases.
86 transform(line.begin(), line.end(), line.begin(), ::tolower);
87 if (regex_match(line, pattern1)) { // match one word
input?
88 if (line.find("init") != string::npos) return
Command::INIT;
89 if (line.find("show") != string::npos) return
Command::SHOW;
90 if (line.find("quit") != string::npos) return
10. Command::QUIT;
91 } // you can use enum if you want above instead of a
number return.
92 if (regex_match(line, pattern2)) { // match two word
input?
93 string c, r;
94 istringstream iss(line); // then read int two
words.
95 iss >> c >> r; // stringstream is the one
to use!
96 assert(!iss.fail()); // this will not happen
(why?)
97 row = atoi(c.c_str()) - 1; // apply semantic check
for col,
98 col = r[0] - 'a'; // and row values.
99 if (row >= 0 && row < size && col >= 0 && col < size)
100 return Command::BOMB;
101 }
102 cout << "Input error, try again: ";
103 }
104 assert(false); // to make sure not to
reach here.
105 }
106
107 int main() {
108 const int size = DefaultBoardSize; // instantiate a
board object with
109 Board b("Pacific Ocean", size); // game (war)
name and board size.
110
111 while (true) {
112 cout << b;
113
114 int row, col;
115 switch (parse(size, row, col)) {
116 case Command::QUIT: goto END; // "quit" is to
11. end the game.
117 case Command::SHOW: break; // "show" is to
show the answer.
118 case Command::INIT: b.init(); continue; // "init" is to
restart the game.
119 case Command::BOMB: break; // other correct
input.
120 default: abort(); // should not reach.
121 }
122 bool r = b.bomb(row, col); // otherwise do the
game...
123 cout << "You " << (r? "ahit :)" : "missed :(") << endl;
124 if (b.isover()) {
Page 2
prog5.cpp
125 cout << "You bombed all ships, congratulations!" <<
endl;
126 break;
127 }
128 }
129 END:
130 cout << "Bye..." << endl;
131 return 0;
132 }
133
Page 3