First working version with ncurse an inception = 2

This commit is contained in:
Yorick Barbanneau 2021-10-21 02:10:16 +02:00
parent dafe6f73ee
commit 46a55f31d1
3 changed files with 149 additions and 47 deletions

View file

@ -16,7 +16,9 @@ typedef struct view * p_view;
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);
//e_location display_ttt( WINDOW * w, s_tictactoe * ttt);
e_location coord_to_elocation( int x, int y);
int elocation_to_coord(e_location l);
/*!
* This function retrieves a valid move proposal from the user using the corresponding view.
*

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);
}

View file

@ -26,8 +26,9 @@ p_view create_view(s_utictactoe * u)
init_pair(2, COLOR_BLACK,COLOR_RED);
curs_set( 0 );
mvwprintw(stdscr,0,0,"Inception level %d", u->inception_level);
// Create the outer win
WINDOW * w_outer = subwin(stdscr, 9, 11, p_left, p_top);
WINDOW * w_outer = subwin(stdscr, 9, 11, p_top, p_left);
box(w_outer, ACS_VLINE, ACS_HLINE);
draw_ttt(w_outer, u->outer_tictactoe);
v->w_uttt = w_outer;
@ -35,8 +36,8 @@ p_view create_view(s_utictactoe * u)
// 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);
getmaxyx(w_outer, r, c);
WINDOW * w_inner = subwin(stdscr, 9, 11, p_top, p_left + c);
box(w_inner, ACS_VLINE, ACS_HLINE);
//draw_ttt(w_innera);
//wrefresh(w_inner);
@ -48,7 +49,6 @@ p_view create_view(s_utictactoe * u)
return v;
}
void draw_ttt(WINDOW * w, s_tictactoe * u){
int x,y;
int i = 0;
@ -68,21 +68,61 @@ void draw_ttt(WINDOW * w, s_tictactoe * u){
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);
int in_x = 3, out_x = 3;
int out_y = 2, in_y = 2;
bool outer_selected, played = false;
int ch=-1;
int p_x, p_y, p_yx = 0;
if ( v->p_uttt->inception_level == 2 && m->outer_position != FREE ) {
p_yx = elocation_to_coord(m->outer_position);
p_y = p_yx/10;
p_x = p_yx - ( p_y * 10);
}
while(played == false){
// debug print
mvwprintw(stdscr, 20,0, " position out: y:%d x:%d, in: y:%d x:%d\n", out_y, out_x, in_y, in_x);
mvwprintw(stdscr,1,0,s, m->player);
draw_ttt(v->w_uttt, v->p_uttt->outer_tictactoe);
// Need to draw the second tictac toe if we have an inecption level
if ( v->p_uttt->inception_level == 2 ) {
draw_ttt(v->w_ttt, v->p_uttt->inner_tictactoes[coord_to_elocation(out_y, out_x)]);
// and mark the current active inner ttt with red cursor
if ( m->outer_position != FREE && v->p_uttt->inception_level == 2) {
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)]
mvwaddch(v->w_uttt ,p_y, p_x,
v->p_uttt->outer_tictactoe->content[m->outer_position]
);
wattroff(v->w_uttt, COLOR_PAIR(2));
}
}
// inner_cursor cursor
if ( outer_selected == true ) {
wattron(v->w_ttt, COLOR_PAIR(1));
mvwaddch(v->w_ttt ,in_y, in_x,
v->p_uttt->inner_tictactoes[coord_to_elocation(out_y, out_x)]->content[coord_to_elocation(in_y, in_x)]
);
wattroff(v->w_ttt, COLOR_PAIR(1));
}
// outer_cursor cursor
wattron(v->w_uttt, COLOR_PAIR(1));
mvwaddch(v->w_uttt ,out_y, out_x,
v->p_uttt->outer_tictactoe->content[coord_to_elocation(out_y, out_x)]
);
wattroff(v->w_uttt, COLOR_PAIR(1));
// refresh screen
wrefresh(v->w_uttt);
if (v->p_uttt->inception_level == 2) wrefresh(v->w_ttt);
wrefresh(stdscr);
ch = wgetch(stdscr);
switch(ch)
{
case KEY_BACKSPACE:
@ -91,35 +131,61 @@ void set_next_player_move(s_move * m,p_view v)
exit(1);
break;
case KEY_RIGHT:
if(pos_x+1<8){
pos_x=pos_x+2;
if ( outer_selected ) {
if(in_x < 7) { in_x+=2; }
}
else {
if(out_x < 7) { out_x+=2; }
}
break;
case KEY_LEFT:
if(pos_x>3){
pos_x=pos_x-2;
if ( outer_selected ) {
if( in_x > 3 ) { in_x-=2; }
}
else {
if ( out_x > 3 ) { out_x-=2; }
}
break;
case KEY_UP:
if(pos_y>2){
pos_y=pos_y-2;
if ( outer_selected ) {
if ( in_y > 2 ) { in_y-=2; }
}
else {
if ( out_y > 2 ) { out_y-=2; }
}
break;
case KEY_DOWN:
if(pos_y<6){
pos_y=pos_y+2;
if ( outer_selected ) {
if ( in_y < 6 ) { in_y+=2; }
}
else {
if ( out_y < 6 ) { out_y+=2; }
}
break;
case ' ':
if ( v->p_uttt->inception_level == 1 ) {
m->outer_position = coord_to_elocation( out_y, out_x);
return;
}
else {
if ( outer_selected == true ) {
m->outer_position = coord_to_elocation( out_y, out_x);
m->inner_position = coord_to_elocation( in_y, in_x);
return;
}
else {
if ( coord_to_elocation( out_y, out_x ) == m->outer_position || m->outer_position == FREE){
outer_selected = true;
}
}
}
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) {
e_location coord_to_elocation( 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; }
@ -132,6 +198,39 @@ e_location get_ttt_position( int y, int x) {
return NONE;
}
int elocation_to_coord(e_location l){
switch(l){
case TOPLEFT:
return 23;
break;
case TOPCENTER:
return 25;
break;
case TOPRIGHT:
return 27;
break;
case MIDLEFT:
return 43;
break;
case MIDCENTER:
return 45;
break;
case MIDRIGHT:
return 47;
break;
case BOTTOMLEFT:
return 63;
break;
case BOTTOMCENTER:
return 65;
break;
case BOTTOMRIGHT:
return 67;
break;
default:
return NONE;
}
}
void free_view(p_view v){
if(v->p_uttt->inception_level == 2){
@ -143,3 +242,4 @@ void free_view(p_view v){
v->w_uttt = NULL;
endwin();
}