Skip to content

Commit

Permalink
Merge pull request #3 from ArkadySK/main-menu
Browse files Browse the repository at this point in the history
Added Main Menu
  • Loading branch information
WaffleMaker9000 authored Jan 8, 2025
2 parents a9713fe + fb14c86 commit 42e82eb
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
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");
//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
46 changes: 46 additions & 0 deletions source/menu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#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;
}
}
return 0;
}
2 changes: 2 additions & 0 deletions source/menu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
char display_menu(void);
int handle_menu(void);
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");
}
2 changes: 2 additions & 0 deletions source/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Clears the screen using ANSI escape sequence
void clear_screen(void);

0 comments on commit 42e82eb

Please sign in to comment.