Skip to content
Snippets Groups Projects
Commit e268667f authored by Jake Feddersen's avatar Jake Feddersen
Browse files

Pretty things up

parent 8187ec58
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -51,19 +51,44 @@ int main(int argc, char *argv[]) {
player_board.draw();
opponent_board.draw();
while(!player_board.has_lost() && !opponent_board.has_lost()) {
while(true) {
clear_message();
display_message_1("Select Target Position");
display_message_2("Press Space To Fire");
std::string move = opponent_board.get_move();
clear_message();
display_message_1("Waiting for Opponent");
refresh();
std::string opponent_move = c->exchange_message(move);
clear_message();
player_board.make_move(opponent_move);
opponent_board.make_move(move);
player_board.draw();
opponent_board.draw();
if (player_board.has_lost() || opponent_board.has_lost()) break;
usleep(1000000);
}
clear_message();
if (player_board.has_lost() && !opponent_board.has_lost()) {
display_message_1("You Lose.");
display_message_2("Better luck next time.");
} else if (!player_board.has_lost() && opponent_board.has_lost()) {
display_message_1("You Win!");
display_message_2("Congratulations!");
} else {
display_message_1("It's a tie!");
display_message_2("What are the odds?!?");
}
player_board.draw_game_over();
opponent_board.draw_game_over();
getch();
destroy_screen();
......
......@@ -115,8 +115,34 @@ void board::make_move(std::string position) {
int y = position[0] - 'A';
int x = position[1] - '1';
bool resultHit = false;
for (int i = 0; i < (int)ships.size(); i++) {
if (ships[i]->contains(x, y)) ships[i]->hit();
if (ships[i]->contains(x, y)) {
resultHit = true;
ships[i]->hit();
}
}
int x_offset = 0;
if (side == PLAYER) {
x_offset = 57;
}
attron(COLOR_PAIR(TEXT_WHITE));
mvprintw(14, x_offset, " ");
mvprintw(14, x_offset, "Last Move: %c%d", 'A' + y, x + 1);
attroff(COLOR_PAIR(TEXT_WHITE));
if (resultHit) {
attron(COLOR_PAIR(TEXT_RED));
mvprintw(14, x_offset + 20, "HIT");
attroff(COLOR_PAIR(TEXT_RED));
} else {
attron(COLOR_PAIR(TEXT_WHITE));
mvprintw(14, x_offset + 19, "MISS");
attroff(COLOR_PAIR(TEXT_WHITE));
}
guesses[y][x] = true;
......@@ -192,7 +218,52 @@ void board::draw() {
}
for (int i = 0; i < 5; i++) {
ships[i]->print_ship(x_offset, 14+i);
ships[i]->print_ship(x_offset, 16+i);
}
refresh();
}
void board::draw_game_over() {
int x_offset = 0;
if (side == PLAYER) {
x_offset = 57;
}
attron(COLOR_PAIR(TEXT_WHITE));
mvprintw(0, 4, "Opponent's Ships");
mvprintw(0, 64, "Your Ships");
mvprintw(1, x_offset, " 1 2 3 4 5 6 7 8 9 10 ");
for (int i = 0; i < 10; i++) {
mvaddch(3 + i, x_offset, 'A' + i);
mvaddch(3 + i, x_offset+1, '|');
mvaddch(3 + i, x_offset+22, '|');
}
for (int i = 0; i < 22; i++) {
mvaddch(2, x_offset+i+1, '-');
mvaddch(13, x_offset+i+1, '-');
}
attroff(COLOR_PAIR(TEXT_WHITE));
// Nuke the side variable since now we want to see the ships on both sides
side = PLAYER;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
int color = get_position_color(j, i);
attron(COLOR_PAIR(color));
mvprintw(i+3, x_offset + 2 + (j*2), " ");
attroff(COLOR_PAIR(color));
}
}
for (int i = 0; i < 5; i++) {
ships[i]->print_ship(x_offset, 16+i);
}
refresh();
......@@ -205,8 +276,8 @@ std::string board::get_move() {
while(true) {
int key = getch();
if (key == 'f') {
break;
if (key == ' ') {
if (!guesses[cursor_y][cursor_x]) break;
}
else if (key == KEY_UP) cursor_y--;
......
......@@ -26,7 +26,7 @@ private:
bool show_cursor;
int cursor_x;
int cursor_y;
public:
board(int side);
board(int side, std::string &board_description);
......@@ -39,6 +39,7 @@ public:
bool has_lost();
void draw();
void draw_game_over();
};
#endif
\ No newline at end of file
......@@ -70,6 +70,19 @@ std::string getstring() {
return input;
}
void clear_message() {
mvprintw(6, 23, " ");
mvprintw(7, 23, " ");
}
void display_message_1(std::string message) {
mvprintw(6, 23+17-(message.length() / 2), message.c_str());
}
void display_message_2(std::string message) {
mvprintw(7, 23+17-(message.length() / 2), message.c_str());
}
int randrange(int min, int max) {
int range = max - min + 1;
return (rand() % range) + min;
......
......@@ -15,4 +15,8 @@ std::string getstring();
int randrange(int min, int max);
int sign(int x);
void clear_message();
void display_message_1(std::string message);
void display_message_2(std::string message);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment