Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ncurses.h>
- #include <stdbool.h>
- #include <stdarg.h>
- #include <unistd.h>
- #include <time.h>
- #include <stdlib.h>
- #define xMax 80
- #define height_m 10
- #define width_m 20
- #define start_y_m 3
- #define start_x_m ((xMax - width_m) / 2 - 1)
- #define height_g 16
- #define width_g 50
- #define start_y_g 3
- #define start_x_g ((xMax - width_g) / 2 - 1)
- struct matrix1010
- {
- char m[10][10];
- };
- struct nava
- {
- int dim, id;//dim - dimensiune - de la 1 la 4
- //id - identificatorul barcii - de la 1 la 10
- int dir; //directia: 1 vertical | 0 orizontal | -1 nu are directie
- int intacte;//cate parti intacte are nava. 0 daca nava este distrusa
- char status[4];//maxim 4 patratele. 1 - intact, 0 - lovit
- };
- /*
- TODO: ADD FUNCTION DECLARATION
- */
- //TODO: ADD comp_ppov TO LOGS, READ comp_ppov FROM LOGS
- void start_ncurses(void)
- {
- initscr();
- cbreak();
- noecho();
- start_color();
- curs_set(0);
- srand(time(0)); //nu face parte din ncurses, dar tot e initializare
- refresh();
- }
- WINDOW *start_win(const int h, const int w, const int y, const int x,
- const char lr, const char tb, const char cor)
- {
- WINDOW *win = newwin(h, w, y, x);
- wborder(win, lr, lr, tb, tb, cor, cor, cor, cor);
- //wborder(win, left, right, top, bottom, tlc, trc, blc, brc)
- keypad(win, true);
- return win;
- }
- void clrwin(WINDOW *win)
- {
- win = newwin(0, 0, 0, 0);
- wrefresh(win);
- }
- bool open_files(int nr_files, FILE **fin, char *arg[], struct matrix1010 map[])
- {
- //1 - eroare
- int k = 0;
- for(k = 0; k < nr_files; ++k) {
- fin[k] = fopen(arg[k + 1], "r");
- if(fin[k] == NULL)
- {
- fprintf(stderr, "[Eroare]: Fisierul %s nu poate fi deschis.\n",
- arg[k]);
- return 1;
- }
- int i = 0, j = 0;
- for(i = 0; i < 10; ++i)
- {
- for(j = 0; j < 10; ++j)
- {
- fscanf(fin[k], "|%c", &map[k].m[i][j]);
- }
- fscanf(fin[k], "|\n");
- }
- }
- return 0;
- }
- void close_files(FILE *logs, FILE *scores, int nr_files, FILE **fin)
- {
- fclose(logs);
- fclose(scores);
- int i;
- for(i = 0; i < nr_files; ++i)
- fclose(fin[i]);
- //free(fin);
- }
- void read_logs(FILE *logs, struct nava ship[2][10],
- int *nave_pl, int *nave_cmp,
- struct matrix1010 *player, struct matrix1010 *computer,
- struct matrix1010 *comp_ppov)
- {
- fclose(logs);
- logs = fopen("logs", "r");
- fscanf(logs, "1\n");//citim valoarea de resume, pe care o stim ca este 1
- fscanf(logs, "%d %d\n", &(*nave_pl), &(*nave_cmp));
- int i, j, k, value;
- for(i = 0; i < 2; ++i)
- for(j = 0; j < 10; ++j)
- {
- ship[i][j].id = j + 1;
- fscanf(logs, "%d %d %d ", &ship[i][j].dim, &ship[i][j].intacte,
- &ship[i][j].dir);
- for(k = 0; k < ship[i][j].dim; ++k)
- {
- fscanf(logs, "%d ", &value);
- ship[i][j].status[k] = value;
- //printw("val: %d | stat[%d]: %d\n", value, k, ship[i][j].status[k]);
- }
- fscanf(logs, "\n");
- }
- for(i = 0; i < 10; ++i)
- {
- for(j = 0; j < 10; ++j)
- {
- fscanf(logs, "%d ", &value);
- (*player).m[i][j] = value;
- }
- fscanf(logs, "\n");
- }
- for(i = 0; i < 10; ++i)
- {
- for(j = 0; j < 10; ++j)
- {
- fscanf(logs, "%d ", &value);
- (*computer).m[i][j] = value;
- }
- fscanf(logs, "\n");
- }
- for(i = 0; i < 10; ++i)
- {
- for(j = 0; j < 10; ++j)
- {
- fscanf(logs, "%d ", &value);
- (*comp_ppov).m[i][j] = value;
- }
- fscanf(logs, "\n");
- }
- }
- void write_logs(FILE *logs, int resume, struct nava ship[2][10],
- int nave_pl, int nave_cmp,
- struct matrix1010 player, struct matrix1010 computer,
- struct matrix1010 comp_ppov)
- {
- fclose(logs);
- logs = fopen("logs", "w");
- fprintf(logs, "%d\n", resume);
- fprintf(logs, "%d %d\n", nave_pl, nave_cmp);
- int i, j, k;
- for(i = 0; i < 2; ++i)
- for(j = 0; j < 10; ++j)
- {
- fprintf(logs, "%d %d %d ", ship[i][j].dim, ship[i][j].intacte,
- ship[i][j].dir);
- for(k = 0; k < ship[i][j].dim; ++k)
- fprintf(logs, "%d ", ship[i][j].status[k]);
- fprintf(logs, "\n");
- }
- for(i = 0; i < 10; ++i)
- {
- for(j = 0; j < 10; ++j)
- fprintf(logs, "%d ", player.m[i][j]);
- fprintf(logs, "\n");
- }
- for(i = 0; i < 10; ++i)
- {
- for(j = 0; j < 10; ++j)
- if(computer.m[i][j] != -1)
- fprintf(logs, "%d ", computer.m[i][j]);
- else
- fprintf(logs, "0 ");
- fprintf(logs, "\n");
- }
- for(i = 0; i < 10; ++i)
- {
- for(j = 0; j < 10; ++j)
- fprintf(logs, "%d ", comp_ppov.m[i][j]);
- fprintf(logs, "\n");
- }
- return;
- }
- void read_scores(FILE *scores)
- {
- fclose(scores);
- scores = fopen("scores", "r");
- int Pships, Cships, Pwins = 0, Cwins = 0, Twins, TPships = 0, TCships = 0;
- while(fscanf(scores, "%d - %d\n", &Pships, &Cships) != EOF)
- if(Pships == 10)
- ++Pwins;
- else
- ++Cwins;
- Twins = Pwins + Cwins;
- fclose(scores);
- scores = fopen("scores", "r");
- WINDOW *menuwin = start_win(10, 28, start_y_g, start_x_g - 12,
- '|', '~', '+');
- mvwprintw(menuwin, 1, 1, " P - C");
- mvwprintw(menuwin, 2, 1, "~~~~~~~~~~~~~~~~~~~~~~~~~~");
- mvwprintw(menuwin, 3, 1, "%3d - %3d wins", Pwins, Twins);
- int i;
- for(i = 0; i < Twins; ++i)
- {
- fscanf(scores, "%d - %d\n", &Pships, &Cships);
- //mvwprintw(menuwin, i + 3, 1, "%2d - %2d", Pships, Cships);
- TPships += Pships;
- TCships += Cships;
- }
- mvwprintw(menuwin, 4, 1, "%3d - %3d total destroyed", TPships, TCships);
- mvwprintw(menuwin, 5, 1, "~~~~~~~~~~~~~~~~~~~~~~~~~~");
- wrefresh(menuwin);
- }
- void write_scores(FILE *scores, bool whowin, int nave_distruse)
- {
- fclose(scores);
- scores = fopen("scores", "a");
- if(whowin == 1) //player win
- fprintf(scores, "\n10 - %d", nave_distruse);
- else //computer win
- fprintf(scores, "\n%d - 10", nave_distruse);
- }
- void setplayership(struct nava *ship, const int dim,
- const int index, const int dir)
- {
- (*ship).dim = dim;
- (*ship).intacte = dim;
- (*ship).dir = dir;
- (*ship).id = index;
- int i;
- for(i = 0; i < dim; ++i)
- (*ship).status[i] = 1;
- }
- void visual_to_value(struct matrix1010 *A, struct nava ship[])
- {
- int i, j, index = 0;
- for(i = 0; i < 10; ++i)
- for(j = 0; j < 10; ++j)
- {
- if((*A).m[i][j] == ' ')
- (*A).m[i][j] = 0;
- else if((*A).m[i][j] == 'X')
- {
- ++index;
- (*A).m[i][j] = index;
- int k;
- k = j + 1;
- while(k < 10 && (*A).m[i][k] == 'X')//nava este pe orizontal
- {
- (*A).m[i][k] = index;
- ++k;
- }
- if(k - 1 != j)
- setplayership(&ship[index-1], k - j, index, 0);
- else
- {
- k = i + 1;
- while(k < 10 && (*A).m[k][j] == 'X')//nava este pe vertical
- {
- (*A).m[k][j] = index;
- ++k;
- }
- if(k - 1 != i)
- setplayership(&ship[index-1], k - i, index, 1);
- else//nava are dimensiunea 1
- setplayership(&ship[index-1], 1, index, -1);
- }
- }
- }
- }
- void init_matrix(struct matrix1010 *A)
- {
- int i, j;
- for(i = 0; i < 10; ++i)
- for(j = 0; j < 10; ++j)
- (*A).m[i][j] = 0;
- }
- void init_ships(struct nava ship[])
- {
- int i, j;
- int dimensions[] = {4, 3, 3, 2, 2, 2, 1, 1, 1, 1};
- for(i = 0; i < 10; ++i)
- {
- ship[i].id = i + 1;
- ship[i].dim = dimensions[i];
- ship[i].intacte = ship[i].dim;
- for(j = 0; j < ship[i].dim; ++j)
- ship[i].status[j] = 1;
- }
- }
- bool check_vertically(int rline, int rcolumn, int dim, struct matrix1010 A)
- {
- //verificare peste tot pe teritoriul navei si in jurul ei
- int i;
- int rline1 = rline + dim;
- if(rline > 0)
- {
- if(rcolumn > 0 && A.m[rline-1][rcolumn-1] > 0)
- return 0;
- if(rcolumn < 9 && A.m[rline-1][rcolumn+1] > 0)
- return 0;
- if(A.m[rline-1][rcolumn] > 0)
- return 0;
- }
- if(rline1 < 9)
- {
- if(rcolumn > 0 && A.m[rline1][rcolumn-1] > 0)
- return 0;
- if(rcolumn < 9 && A.m[rline1][rcolumn+1] > 0)
- return 0;
- if(A.m[rline1][rcolumn] > 0)
- return 0;
- }
- if(rcolumn > 0)
- for(i = rline; i < rline1; ++i)
- if(A.m[i][rcolumn-1] > 0)
- return 0;
- if(rcolumn < 9)
- for(i = rline; i < rline1; ++i)
- if(A.m[i][rcolumn+1] > 0)
- return 0;
- for(i = rline; i < rline1; ++i)
- if(A.m[i][rcolumn])
- return 0;
- return 1;
- }
- bool check_horizontally(int rline, int rcolumn, int dim, struct matrix1010 A)
- {
- int i;
- int rcolumn1 = rcolumn + dim ;
- if(rcolumn > 0)
- {
- if(rline > 0 && A.m[rline-1][rcolumn-1] > 0)
- return 0;
- if(rline < 9 && A.m[rline+1][rcolumn-1] > 0)
- return 0;
- if(A.m[rline][rcolumn-1] > 0)
- return 0;
- }
- if(rcolumn1 < 9)
- {
- if(rline > 0 && A.m[rline-1][rcolumn1] > 0)
- return 0;
- if(rline < 9 && A.m[rline+1][rcolumn1] > 0)
- return 0;
- if(A.m[rline][rcolumn1])
- return 0;
- }
- if(rline > 0)
- for(i = rcolumn; i < rcolumn1; ++i)
- if(A.m[rline-1][i] > 0)
- return 0;
- if(rline < 9)
- for(i = rcolumn; i < rcolumn1; ++i)
- if(A.m[rline+1][i] > 0)
- return 0;
- for(i = rcolumn; i < rcolumn1; ++i)
- if(A.m[rline][i])
- return 0;
- return 1;
- }
- void place_vertically(const int rline, const int rcolumn, const int dim,
- const int ship_id, struct matrix1010 *A)
- {
- //se cunoaste faptul ca se poate plasa
- int i;
- int rline1 = rline + dim;
- for(i = rline; i < rline1; ++i)
- (*A).m[i][rcolumn] = ship_id;
- if(rline > 0)
- {
- if(rcolumn > 0)
- (*A).m[rline-1][rcolumn-1] = -1;
- if(rcolumn < 9)
- (*A).m[rline-1][rcolumn+1] = -1;
- (*A).m[rline-1][rcolumn] = -1;
- }
- if(rline1 < 9)
- {
- if(rcolumn > 0)
- (*A).m[rline1][rcolumn-1] = -1;
- if(rcolumn < 9)
- (*A).m[rline1][rcolumn+1] = -1;
- (*A).m[rline1][rcolumn] = -1;
- }
- if(rcolumn > 0)
- for(i = rline; i < rline1; ++i)
- (*A).m[i][rcolumn-1] = -1;
- if(rcolumn < 9)
- for(i = rline; i < rline1; ++i)
- (*A).m[i][rcolumn+1] = -1;
- }
- void place_horizontally(const int rline, const int rcolumn, const int dim,
- const int ship_id, struct matrix1010 *A)
- {
- //se cunoaste faptul ca se poate plasa
- int i;
- int rcolumn1 = rcolumn + dim;
- for(i = rcolumn; i < rcolumn1; ++i)
- (*A).m[rline][i] = ship_id;
- if(rcolumn > 0)
- {
- if(rline > 0)
- (*A).m[rline-1][rcolumn-1] = -1;
- if(rline < 9)
- (*A).m[rline+1][rcolumn-1] = -1;
- (*A).m[rline][rcolumn-1] = -1;
- }
- if(rcolumn1 < 9)
- {
- if(rline > 0)
- (*A).m[rline-1][rcolumn1] = -1;
- if(rcolumn < 9)
- (*A).m[rline+1][rcolumn1] = -1;
- (*A).m[rline+1][rcolumn] = -1;
- }
- if(rline > 0)
- for(i = rcolumn; i < rcolumn1; ++i)
- (*A).m[rline-1][i] = -1;
- if(rline < 9)
- for(i = rcolumn; i < rcolumn1; ++i)
- (*A).m[rline+1][i] = -1;
- }
- bool random_place(struct nava *ship, struct matrix1010 *A)
- {
- //return 1 - eroare
- //return 0 - OK
- int rline, rcolumn, rdirection, tries = 0;
- bool placed = false;
- do
- {
- ++tries;
- if(tries == 100)
- {
- tries = 0;
- return 1;
- }
- rline = rand() % 10;
- rcolumn = rand() % 10;
- rdirection = rand() % 2;
- // 1 vertical in jos, 0 orizontal in dreapta
- if(rdirection && rline + (*ship).dim < 10)
- if(check_vertically(rline, rcolumn, (*ship).dim, *A))
- {
- //verificam adiacenta pe verticala
- place_vertically(rline, rcolumn, (*ship).dim, (*ship).id, A);
- placed = true;
- }
- if(!rdirection && rcolumn + (*ship).dim < 10)
- if(check_horizontally(rline, rcolumn, (*ship).dim, *A))
- {
- //verificam adiacenta pe orizontala
- place_horizontally(rline, rcolumn, (*ship).dim, (*ship).id, A);
- placed = true;
- }
- }while(!placed);
- (*ship).dir = rdirection;
- return 0;
- }
- struct matrix1010 random_map(struct nava ship[])
- {
- /*
- ship[0-0] - dimensiune 4
- ship[1-2] - dimensiune 3
- ship[3-5] - dimensiune 2
- ship[6-9] - dimensiune 1
- */
- struct matrix1010 map;
- init_matrix(&map);
- init_ships(ship);
- int i;
- for(i = 0; i < 10; ++i)
- if(random_place(&ship[i], &map))
- {
- init_matrix(&map);
- i = -1;
- }
- return map;
- }
- int highlighter(WINDOW *win, const int nr_choices, char *choices[])
- {
- int choice;
- int highlight = 0;
- while(true)
- {
- int i;
- for(i = 0; i < nr_choices; ++i)
- {
- if(i == highlight)
- wattron(win, A_REVERSE);
- mvwprintw(win, i + 1, 1, choices[i]);
- wattroff(win, A_REVERSE);
- }
- choice = wgetch(win);
- switch(choice)
- {
- case KEY_UP:
- --highlight;
- if(highlight == -1)
- highlight = 0;
- break;
- case KEY_DOWN:
- ++highlight;
- if(highlight == nr_choices)
- highlight = nr_choices - 1;
- break;
- default:
- break;
- }
- if(choice == 'q' || choice == 'Q')
- return -1;
- if(choice == 10)//ENTER
- break;
- }
- win = newwin(0, 0, 0, 0);
- clrwin(win);
- return highlight;
- }
- int menu(const int resume)
- {
- WINDOW *menuwin = start_win(height_m, width_m, start_y_m, start_x_m,
- '|', '~', '+');
- mvwprintw(menuwin, 0, 5, "Battleship");
- int highlight = 0;
- /*
- RETURN CODES:
- 0 - Quit
- 1 - New Game
- 2 - Resume Game
- 3 - Score
- */
- if(!resume)
- {
- char *choices[] = {
- "New Game",
- "Score",
- "Quit"
- };
- highlight = highlighter(menuwin, 3, choices);
- if(highlight == 1)
- return 3;
- return (highlight + 1) % 3; //se verifica codurile de mai sus
- }
- else
- {
- char *choices[] = {
- "New Game",
- "Resume Game",
- "Score",
- "Quit"
- };
- highlight = highlighter(menuwin, 4, choices);
- return (highlight + 1) % 4; //se verifica codurile de mai sus
- }
- }
- int choosemap_menu()
- {
- WINDOW *menuwin = start_win(height_m, width_m, start_y_m, start_x_m,
- '|', '~', '+');
- mvwprintw(menuwin, 0, 5, "Battleship");
- char *choices[] = {
- "Alege harta",
- "Harta aleatorie"
- };
- clrwin(menuwin);
- return highlighter(menuwin, 2, choices);
- }
- int choose_map(FILE **fin, const int nr_maps, struct matrix1010 map[])
- {
- int hl = choosemap_menu();
- if(hl == 1)//alegere aleatorie a hartii
- return (rand() % nr_maps);
- if(hl == -1)//ne intoarcem in meniul principal
- return -1;
- //else -> jucatorul isi alege harta
- WINDOW *game = start_win(height_g, width_g, start_y_g, start_x_g,
- '|', '~', '+');
- keypad(game, true);
- mvwprintw(game, 0, 20, "Battleship");
- mvwprintw(game, 1, 6, "Alege configuratia predefinita de nave");
- mvwprintw(game, 2, 1, "================================================");
- mvwprintw(game, 14, 9, "Apasa sageata stanga sau dreapta");
- wrefresh(game);
- int selected = 0;
- while(true)
- {
- int i, j;
- for(i = 3; i <= 12; ++i)
- {
- for(j = 1; j <= 10; ++j)
- {
- char ch = map[selected].m[i - 3][j - 1];
- if(ch != ' ' && ch != 0)
- mvwprintw(game, i, 13 + 2 * j - 1, "|X");
- else
- mvwprintw(game, i, 13 + 2 * j - 1, "| ");
- }
- mvwprintw(game, i, 13 + 21, "|");
- }
- int choice = wgetch(game);
- switch(choice)
- {
- case KEY_LEFT:
- --selected;
- if(selected == -1)
- selected = 0;
- break;
- case KEY_RIGHT:
- ++selected;
- if(selected == nr_maps)
- selected = nr_maps - 1;
- break;
- case 'Q':
- clrwin(game);
- return -1;
- break;
- case 'q':
- clrwin(game);
- return -1;
- break;
- default:
- break;
- wrefresh(game);
- }
- if(choice == 10)//ENTER
- break;
- }
- clrwin(game);
- return selected;
- }
- void print_player_map(WINDOW *game, struct matrix1010 player)
- {
- int i, j;
- for(i = 3; i <= 12; ++i)
- {
- for(j = 1; j <= 10; ++j)
- {
- char ch = player.m[i - 3][j - 1];
- //printw("player.m[%d][%d]: %d\n", i-2, j, ch);
- switch(ch)
- {
- case 0:
- mvwprintw(game, i, 2 * j - 1, "| ");
- break;
- case -2:
- mvwprintw(game, i, 2 * j - 1, "|#");
- break;
- case -3:
- mvwprintw(game, i, 2 * j - 1, "|-");
- break;
- default:
- //printw("afisat X la: %d\n", ch);
- mvwprintw(game, i, 2 * j - 1, "|X");
- break;
- }
- }
- mvwprintw(game, i, 21, "|");
- }
- }
- void print_computer_map(WINDOW *game, struct matrix1010 computer,
- const int hl_i, const int hl_j)
- {
- int i, j;
- for(i = 3; i <= 12; ++i)
- {
- for(j = 1; j <= 10; ++j)
- {
- char ch = computer.m[i-3][j-1];
- if(i - 3 == hl_i && j - 1 == hl_j)
- mvwprintw(game, i, 27 + 2 * j - 1, "|O");
- else
- switch(ch)
- {
- case -1:
- mvwprintw(game, i, 27 + 2 * j - 1, "| ");
- break;
- case 0:
- mvwprintw(game, i, 27 + 2 * j - 1, "| ");
- break;
- case -2:
- mvwprintw(game, i, 27 + 2 * j - 1, "|#");
- break;
- case -3:
- mvwprintw(game, i, 27 + 2 * j - 1, "|-");
- break;
- default:
- mvwprintw(game, i, 27 + 2 * j - 1, "|X");
- break;
- }
- }
- mvwprintw(game, i, 27 + 21, "|");
- }
- wrefresh(game);
- }
- int player_turn(WINDOW *game, struct nava ship[], int *nave_cmp,
- struct matrix1010 *computer, struct matrix1010 *comp_ppov,
- int *highlight_i, int *highlight_j)
- {
- int choice = 0;
- int hl_i = *highlight_i;
- int hl_j = *highlight_j;
- while(true)
- {
- print_computer_map(game, *comp_ppov, hl_i, hl_j);
- choice = wgetch(game);
- switch(choice)
- {
- case KEY_UP:
- --hl_i;
- if(hl_i == -1)
- hl_i = 0;
- break;
- case KEY_RIGHT:
- ++hl_j;
- if(hl_j == 10)
- hl_j = 9;
- break;
- case KEY_DOWN:
- ++hl_i;
- if(hl_i == 10)
- hl_i = 9;
- break;
- case KEY_LEFT:
- --hl_j;
- if(hl_j == -1)
- hl_j = 0;
- break;
- case 'q':
- return -1;
- case 'Q':
- return -1;
- default:
- break;
- }
- if(choice == 10)//ENTER
- break;
- }
- *highlight_i = hl_i;
- *highlight_j = hl_j;
- int value = (*computer).m[hl_i][hl_j];
- if(value > 0)
- {
- //zona intacta
- //TODO: animatie lovit
- //TODO: modify status
- --ship[value-1].intacte;
- if(!ship[value-1].intacte)
- if(!(--(*nave_cmp)))
- return 2;
- (*computer).m[hl_i][hl_j] = -2;
- (*comp_ppov).m[hl_i][hl_j] = -2;
- return 1;
- }
- if(value == 0 || value == -1)
- {
- //zona apa
- (*comp_ppov).m[hl_i][hl_j] = -3;
- }
- //cazul in care jucatorul alege o zona cu o barca lovita
- //nu este tratat pentru ca nu se intampla absolut nimic
- return 0;
- }
- int computer_turn(struct nava ship[], int *nave_pl, struct matrix1010 *player)
- {
- int ri, rj, value;
- do
- {
- ri = rand() % 10;
- rj = rand() % 10;
- }while((*player).m[ri][rj] <= -2); //verificare zona lovit/incercat
- value = (*player).m[ri][rj];
- //TODO: modify status
- //sleep(3);
- if(value > 0)
- {
- --ship[value-1].intacte;
- if(!ship[value-1].intacte)
- if(!(--(*nave_pl)))
- return 2;
- (*player).m[ri][rj] = -2;
- return 1;
- }
- if(value == 0)
- (*player).m[ri][rj] = -3;
- return 0;
- }
- void player_win(struct nava ship[])
- {
- WINDOW *game = start_win(height_g, width_g, start_y_g, start_x_g,
- '|', '~', '+');
- keypad(game, true);
- mvwprintw(game, 0, 20, "Battleship");
- int i, nave_distruse = 0;
- for(i = 0; i < 10; ++i)
- if(ship[i].intacte == 0)
- ++nave_distruse;
- mvwprintw(game, 1, 1, "Ai castigat! :) Ti-au fost distruse %d nave",
- nave_distruse);
- mvwprintw(game, 2, 1, "Apasa Q pentru a reveni la meniu");
- while(true)
- {
- int key = wgetch(game);
- if(key == 'q' || key == 'Q')
- return;
- }
- }
- void computer_win(struct nava ship[])
- {
- WINDOW *game = start_win(height_g, width_g, start_y_g, start_x_g,
- '|', '~', '+');
- keypad(game, true);
- mvwprintw(game, 0, 20, "Battleship");
- int i, nave_distruse = 0;
- for(i = 0; i < 10; ++i)
- if(ship[i].intacte == 0)
- ++nave_distruse;
- mvwprintw(game, 1, 1, "Ai pierdut! :( Ai distrus %d nave",
- nave_distruse);
- mvwprintw(game, 2, 1, "Apasa Q pentru a reveni la meniu");
- while(true)
- {
- int key = wgetch(game);
- if(key == 'q' || key == 'Q')
- return;
- }
- }
- void game(FILE *logs, FILE *scores,
- struct nava ship[2][10], int *nave_pl, int *nave_cmp,
- struct matrix1010 *player, struct matrix1010 *computer,
- struct matrix1010 *comp_ppov)
- {
- WINDOW *game = start_win(height_g, width_g, start_y_g, start_x_g,
- '|', '~', '+');
- keypad(game, true);
- mvwprintw(game, 0, 20, "Battleship");
- mvwprintw(game, 1, 8, "Alege unde ataci folosind sagetile");
- mvwprintw(game, 2, 1, "================================================");
- int hl_i = 0, hl_j = 0;
- bool turn = 1; //1 - player | 0 - computer
- int resume = 0;
- while(true)
- {
- print_player_map(game, *player);
- if(turn)
- {
- int value = player_turn(game, ship[1], nave_cmp,
- computer, comp_ppov,
- &hl_i, &hl_j);
- if (value == 2)//jucatorul a castigat
- {
- player_win(ship[0]);
- write_scores(scores, 1, 10 - *nave_pl);
- clrwin(game);
- break;
- }
- if(value == 1)//jucatorul a lovit o barca
- continue;
- else if(value == 0)//jucatorul nu a lovit
- turn = 0;
- else //jucatorul doreste a se reintoarce in meniu
- {
- resume = 1;
- break;
- }
- }
- if(!turn)
- {
- int value = computer_turn(ship[0], nave_pl, player);
- if(value == 0)
- turn = 1;
- else if(value == 2)
- {
- computer_win(ship[1]);
- write_scores(scores, 0, 10 - *nave_cmp);
- clrwin(game);
- break;
- }
- }
- }
- write_logs(logs, resume, ship, *nave_pl, *nave_cmp,
- *player, *computer, *comp_ppov);
- //scriem in logs date pentru a putea continua in caz ca cineva
- // doreste sa continue jocul mai tarziu prin functia resume
- // game din meniu.
- }
- int main(int argc, char *argv[])
- {
- //verificare daca exista argumente
- if(argc == 1)
- {
- fprintf(stderr, "[Eroare]: Nu s-au dat argumente de comanda.\n");
- return 1;
- }
- //verificare daca toate fisierele date ca argumente s-au deschis
- FILE **fin = (FILE**)malloc(argc);
- struct matrix1010 *map = malloc(argc * sizeof(struct matrix1010));
- struct matrix1010 comp_ppov, cmap;
- struct matrix1010 pmap;
- //comp_ppov = tabla computerului, asa cum o vede jucatorul
- //cmap = tabla computerului, in realitate
- struct nava ship[2][10];//prima linie este pentru player
- //cealalta este pentru computer
- int map_index;
- int resume = 0;
- //initializare fisiere aditionale
- FILE *logs, *scores;
- logs = fopen("logs", "r");
- scores = fopen("scores", "r");
- if(open_files(argc-1, fin, argv, map))
- return 1;
- //initializare module pentru ncurses
- start_ncurses();
- //citirea fisierului logs
- fscanf(logs, "%d\n", &resume);
- int nave_pl, nave_cmp;
- while(true)
- {
- refresh();
- int choice = menu(resume);
- switch(choice)
- {
- case 0://Quit
- close_files(logs, scores, argc - 1, fin);
- endwin();
- //free(map);
- return 0;
- case 1://New Game
- if(open_files(argc-1, fin, argv, map))
- return 1;
- map_index = choose_map(fin, argc - 1, map);
- if(map_index < 0)
- break;
- visual_to_value(&map[map_index], ship[0]);
- //initializarea matricei si navelor jucatorului pentru a putea
- // fi folosite in program
- nave_pl = nave_cmp = 10;
- //numarul de nave ale jucatorului, respectiv ale computerului
- init_ships(ship[1]);
- //initializare standard pentru navele computerului
- cmap = random_map(ship[1]);
- init_matrix(&comp_ppov);
- //harta computerului este generata aleator
- game(logs, scores, ship, &nave_pl, &nave_cmp, &map[map_index],
- &cmap, &comp_ppov);
- close_files(logs, scores, argc - 1, fin);
- break;
- case 2://Resume Game
- read_logs(logs, ship, &nave_pl, &nave_cmp,
- &pmap, &cmap, &comp_ppov);
- game(logs, scores, ship, &nave_pl, &nave_cmp, &pmap,
- &cmap, &comp_ppov);
- close_files(logs, scores, argc - 1, fin);
- break;
- case 3://Score
- read_scores(scores);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement