Ncurse interface seems to work...

This commit is contained in:
Yorick Barbanneau 2021-10-18 21:39:00 +02:00
parent 8656f37eab
commit dafe6f73ee
4 changed files with 199 additions and 18 deletions

36
include/view.h Normal file
View file

@ -0,0 +1,36 @@
#ifndef VIEW_H
#define VIEW_H
#include <ncurses.h>
#include "common.h"
#include "model.h"
typedef struct view * p_view;
/*!
* This function allocates dynamically a struct view in order to handle the corresponding u_tictactoe.
*
* \param p_uttt a pointer on a s_utictactoe.
* \return a reference to the s_utictactoe memory space allocated, NULL in case
* of allocation problem.
*/
p_view create_view(s_utictactoe * p_uttt);
void draw_ttt(WINDOW * w, s_tictactoe * t);
e_location get_ttt_position( int x, int y);
/*!
* This function retrieves a valid move proposal from the user using the corresponding view.
*
* \param p_move a pointer on a s_move to be modified.
* \param v a pointer on the view to use.
*/
void set_next_player_move(s_move * p_move, p_view v);
/*!
* This function free all the memory used by a given view which
* reference is given.
*
* \param v a pointer on a view to be freed.
*/
void free_view(p_view v);
#endif /* VIEW_H */

View file

@ -289,9 +289,9 @@ e_status play_move(s_utictactoe *p_uttt, s_move *p_move) {
//check if we have a winner in inner_ttt then set player to outer
p_uttt->outer_tictactoe->content[p_move->outer_position] = p_uttt->inner_tictactoes[p_move->outer_position]->winner;
}
//else {
// p_uttt->outer_tictactoe->content[p_move->outer_position] = p_move->player;
//}
else {
p_uttt->outer_tictactoe->content[p_move->outer_position] = p_move->player;
}
set_tictactoe_winner(p_uttt->outer_tictactoe);
return YES;
}

View file

@ -157,19 +157,19 @@ int main(int argc, char* argv[]) {
free_move(m);
free_utictactoe(s);
s = create_empty_utictactoe(2);
m = create_empty_move();
v = create_view(s);
while (s->outer_tictactoe->winner == NOBODY) {
m->player = get_next_player_to_play(s);
m->outer_position=get_next_outer_position(s);
set_next_player_move(m,v);
play_move(s, m);
m->outer_position = m->inner_position;
}
free_view(v);
draw_utictactoe_history(s);
printf("The winner is : %c\n", s->outer_tictactoe->winner);
free_move(m);
free_utictactoe(s);
// s = create_empty_utictactoe(2);
// m = create_empty_move();
// v = create_view(s);
// while (s->outer_tictactoe->winner == NOBODY) {
// m->player = get_next_player_to_play(s);
// m->outer_position=get_next_outer_position(s);
// set_next_player_move(m,v);
// play_move(s, m);
// m->outer_position = m->inner_position;
// }
// free_view(v);
// draw_utictactoe_history(s);
// printf("The winner is : %c\n", s->outer_tictactoe->winner);
// free_move(m);
// free_utictactoe(s);
}

145
src/view.c Normal file
View file

@ -0,0 +1,145 @@
#include "common.h"
#include "model.h"
#include "view.h"
struct view{
s_utictactoe * p_uttt;
WINDOW* w_uttt;
WINDOW* w_ttt;
};
p_view create_view(s_utictactoe * u)
{
p_view v = (p_view) malloc(sizeof(p_view));
v->p_uttt = u;
int p_left = 4;
int p_top = 2;
initscr();
raw();
noecho();
start_color();
keypad(stdscr, TRUE);
init_pair(1, COLOR_BLACK,COLOR_WHITE);
init_pair(2, COLOR_BLACK,COLOR_RED);
curs_set( 0 );
// Create the outer win
WINDOW * w_outer = subwin(stdscr, 9, 11, p_left, p_top);
box(w_outer, ACS_VLINE, ACS_HLINE);
draw_ttt(w_outer, u->outer_tictactoe);
v->w_uttt = w_outer;
// If needed, create the inner ttt
if ( u->inception_level == 2 ){
int r,c;
getmaxyx(w_outer, c, r);
WINDOW * w_inner = subwin(stdscr, 9, 11, c + p_left, p_top);
box(w_inner, ACS_VLINE, ACS_HLINE);
//draw_ttt(w_innera);
//wrefresh(w_inner);
v->w_ttt = w_inner;
}
wrefresh(w_outer);
wrefresh(stdscr);
return v;
}
void draw_ttt(WINDOW * w, s_tictactoe * u){
int x,y;
int i = 0;
for( y = 2; y < 7; y++){
if ( y % 2 == 0){
for(x = 3; x < 8 ;x++){
if (x % 2 != 0) {
mvwprintw(w,y,x,"%c", (u->content[i] == NOBODY)?'.':u->content[i]);
i++;
}
else { mvwprintw(w,y,x,"|"); }
}
}
else { mvwprintw(w,y,3,"-----"); }
}
}
void set_next_player_move(s_move * m,p_view v)
{
int ch=-1;
char * s="Player to play:%c";
int pos_x=3;
int pos_y=2;
while(ch!=' '){
draw_ttt(v->w_uttt, v->p_uttt->outer_tictactoe);
mvwprintw(stdscr,1,0,s, m->player);
wattron(v->w_uttt, COLOR_PAIR(2));
mvwaddch(v->w_uttt,pos_y, pos_x,
v->p_uttt->outer_tictactoe->content[get_ttt_position(pos_y, pos_x)]
);
wattroff(v->w_uttt, COLOR_PAIR(2));
wrefresh(v->w_uttt);
wrefresh(stdscr);
ch = wgetch(stdscr);
switch(ch)
{
case KEY_BACKSPACE:
case 127:
case '\b':
exit(1);
break;
case KEY_RIGHT:
if(pos_x+1<8){
pos_x=pos_x+2;
}
break;
case KEY_LEFT:
if(pos_x>3){
pos_x=pos_x-2;
}
break;
case KEY_UP:
if(pos_y>2){
pos_y=pos_y-2;
}
break;
case KEY_DOWN:
if(pos_y<6){
pos_y=pos_y+2;
}
break;
}
}
// m->inner_position=(e_location)(TOPLEFT + rand() % TICTACTOE_SIZE);
// m->outer_position=(e_location)(TOPLEFT + rand() % TICTACTOE_SIZE);
m->outer_position = get_ttt_position( pos_y, pos_x);
}
e_location get_ttt_position( int y, int x) {
if ( y == 2 && x == 3 ){ return TOPLEFT; }
if ( y == 2 && x == 5 ){ return TOPCENTER; }
if ( y == 2 && x == 7 ){ return TOPRIGHT; }
if ( y == 4 && x == 3 ){ return MIDLEFT; }
if ( y == 4 && x == 5 ){ return MIDCENTER; }
if ( y == 4 && x == 7 ){ return MIDRIGHT; }
if ( y == 6 && x == 3 ){ return BOTTOMLEFT; }
if ( y == 6 && x == 5 ){ return BOTTOMCENTER; }
if ( y == 6 && x == 7 ){ return BOTTOMRIGHT; }
return NONE;
}
void free_view(p_view v){
if(v->p_uttt->inception_level == 2){
delwin(v->w_uttt);
v->w_uttt = NULL;
}
delwin(v->w_ttt);
v->w_ttt = NULL;
v->w_uttt = NULL;
endwin();
}