Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Main Menu #3

Merged
merged 4 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion source/main.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
#include <stdio.h>
#include "board.h"
#include "menu.h"

int main(int argc, char const *argv[])
{
int mode;
board b_own;
board_init(&b_own, 10);
board b_enemy;

mode = handle_menu();
if (mode == 0) // Quit
return 0;

board_init(&b_own, 10);
board_init(&b_enemy, 10);
board_display(&b_own, &b_enemy);

clear_screen();
if (mode == 1) { // Computer game mode
printf("computer mode");
ArkadySK marked this conversation as resolved.
Show resolved Hide resolved
//TODO Adam: implement computer mode
}
if (mode == 2) { // Human vs Human (network) mode
printf("Please wait, connecting...\n");
// TODO Adam: bind somehow with client.c :D
}

board_destroy(&b_own);
board_destroy(&b_enemy);
return 0;
Expand Down
49 changes: 49 additions & 0 deletions source/menu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include "menu.h"
#include "utils.h"

char display_menu(void) {
clear_screen();

printf("\n");
printf("*********************************\n");
printf("* BATTLESHIP *\n");
printf("*********************************\n");
printf("\n");
printf("Welcome! Choose an activity:\n");
printf("\n");
printf("[1] Play against Computer\n");
printf("[2] Play against Human\n");
printf("[q] Quit\n");
printf("\n");
printf("Your choice: ");

// Read single character
char choice = getchar();
while (getchar() != '\n');

return choice;
}

int handle_menu(void) {
char choice;
int running = 1;

while (running) {
choice = display_menu();
switch (choice) {
case '1':
return 1;
case '2':
return 2;
case 'q':
case 'Q':
return 0;
default:
ArkadySK marked this conversation as resolved.
Show resolved Hide resolved
getchar();
break;
}
}
return 0;
}
7 changes: 7 additions & 0 deletions source/menu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef BATTLESHIP_MENU_H
#define BATTLESHIP_MENU_H

char display_menu(void);
int handle_menu(void);

#endif
ArkadySK marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 8 additions & 1 deletion source/utils.c
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
//TODO Adam: is it needed?
#include <stdio.h>

// Clears the screen using ANSI escape sequence
void clear_screen() {
// \033 - represents the ASCII escape character
// \033[H - Moves the cursor to the "home" position (top-left corner of the terminal)
printf("\033[2J\033[H");
ArkadySK marked this conversation as resolved.
Show resolved Hide resolved
}