First working version with ncurse an inception = 2
This commit is contained in:
parent
dafe6f73ee
commit
46a55f31d1
3 changed files with 149 additions and 47 deletions
|
@ -16,7 +16,9 @@ typedef struct view * p_view;
|
||||||
p_view create_view(s_utictactoe * p_uttt);
|
p_view create_view(s_utictactoe * p_uttt);
|
||||||
|
|
||||||
void draw_ttt(WINDOW * w, s_tictactoe * t);
|
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.
|
* This function retrieves a valid move proposal from the user using the corresponding view.
|
||||||
*
|
*
|
||||||
|
|
|
@ -157,19 +157,19 @@ int main(int argc, char* argv[]) {
|
||||||
free_move(m);
|
free_move(m);
|
||||||
free_utictactoe(s);
|
free_utictactoe(s);
|
||||||
|
|
||||||
// s = create_empty_utictactoe(2);
|
s = create_empty_utictactoe(2);
|
||||||
// m = create_empty_move();
|
m = create_empty_move();
|
||||||
// v = create_view(s);
|
v = create_view(s);
|
||||||
// while (s->outer_tictactoe->winner == NOBODY) {
|
while (s->outer_tictactoe->winner == NOBODY) {
|
||||||
// m->player = get_next_player_to_play(s);
|
m->player = get_next_player_to_play(s);
|
||||||
// m->outer_position=get_next_outer_position(s);
|
m->outer_position=get_next_outer_position(s);
|
||||||
// set_next_player_move(m,v);
|
set_next_player_move(m,v);
|
||||||
// play_move(s, m);
|
play_move(s, m);
|
||||||
// m->outer_position = m->inner_position;
|
m->outer_position = m->inner_position;
|
||||||
// }
|
}
|
||||||
// free_view(v);
|
free_view(v);
|
||||||
// draw_utictactoe_history(s);
|
draw_utictactoe_history(s);
|
||||||
// printf("The winner is : %c\n", s->outer_tictactoe->winner);
|
printf("The winner is : %c\n", s->outer_tictactoe->winner);
|
||||||
// free_move(m);
|
free_move(m);
|
||||||
// free_utictactoe(s);
|
free_utictactoe(s);
|
||||||
}
|
}
|
||||||
|
|
148
src/view.c
148
src/view.c
|
@ -26,8 +26,9 @@ p_view create_view(s_utictactoe * u)
|
||||||
init_pair(2, COLOR_BLACK,COLOR_RED);
|
init_pair(2, COLOR_BLACK,COLOR_RED);
|
||||||
curs_set( 0 );
|
curs_set( 0 );
|
||||||
|
|
||||||
|
mvwprintw(stdscr,0,0,"Inception level %d", u->inception_level);
|
||||||
// Create the outer win
|
// 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);
|
box(w_outer, ACS_VLINE, ACS_HLINE);
|
||||||
draw_ttt(w_outer, u->outer_tictactoe);
|
draw_ttt(w_outer, u->outer_tictactoe);
|
||||||
v->w_uttt = w_outer;
|
v->w_uttt = w_outer;
|
||||||
|
@ -35,8 +36,8 @@ p_view create_view(s_utictactoe * u)
|
||||||
// If needed, create the inner ttt
|
// If needed, create the inner ttt
|
||||||
if ( u->inception_level == 2 ){
|
if ( u->inception_level == 2 ){
|
||||||
int r,c;
|
int r,c;
|
||||||
getmaxyx(w_outer, c, r);
|
getmaxyx(w_outer, r, c);
|
||||||
WINDOW * w_inner = subwin(stdscr, 9, 11, c + p_left, p_top);
|
WINDOW * w_inner = subwin(stdscr, 9, 11, p_top, p_left + c);
|
||||||
box(w_inner, ACS_VLINE, ACS_HLINE);
|
box(w_inner, ACS_VLINE, ACS_HLINE);
|
||||||
//draw_ttt(w_innera);
|
//draw_ttt(w_innera);
|
||||||
//wrefresh(w_inner);
|
//wrefresh(w_inner);
|
||||||
|
@ -48,7 +49,6 @@ p_view create_view(s_utictactoe * u)
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void draw_ttt(WINDOW * w, s_tictactoe * u){
|
void draw_ttt(WINDOW * w, s_tictactoe * u){
|
||||||
int x,y;
|
int x,y;
|
||||||
int i = 0;
|
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)
|
void set_next_player_move(s_move * m,p_view v)
|
||||||
{
|
{
|
||||||
int ch=-1;
|
|
||||||
char * s="Player to play:%c";
|
char * s="Player to play:%c";
|
||||||
int pos_x=3;
|
int in_x = 3, out_x = 3;
|
||||||
int pos_y=2;
|
int out_y = 2, in_y = 2;
|
||||||
while(ch!=' '){
|
bool outer_selected, played = false;
|
||||||
draw_ttt(v->w_uttt, v->p_uttt->outer_tictactoe);
|
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);
|
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));
|
wattron(v->w_uttt, COLOR_PAIR(2));
|
||||||
mvwaddch(v->w_uttt,pos_y, pos_x,
|
mvwaddch(v->w_uttt ,p_y, p_x,
|
||||||
v->p_uttt->outer_tictactoe->content[get_ttt_position(pos_y, pos_x)]
|
v->p_uttt->outer_tictactoe->content[m->outer_position]
|
||||||
);
|
);
|
||||||
wattroff(v->w_uttt, COLOR_PAIR(2));
|
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);
|
wrefresh(v->w_uttt);
|
||||||
|
if (v->p_uttt->inception_level == 2) wrefresh(v->w_ttt);
|
||||||
wrefresh(stdscr);
|
wrefresh(stdscr);
|
||||||
|
|
||||||
ch = wgetch(stdscr);
|
ch = wgetch(stdscr);
|
||||||
|
|
||||||
switch(ch)
|
switch(ch)
|
||||||
{
|
{
|
||||||
case KEY_BACKSPACE:
|
case KEY_BACKSPACE:
|
||||||
|
@ -91,35 +131,61 @@ void set_next_player_move(s_move * m,p_view v)
|
||||||
exit(1);
|
exit(1);
|
||||||
break;
|
break;
|
||||||
case KEY_RIGHT:
|
case KEY_RIGHT:
|
||||||
if(pos_x+1<8){
|
if ( outer_selected ) {
|
||||||
pos_x=pos_x+2;
|
if(in_x < 7) { in_x+=2; }
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if(out_x < 7) { out_x+=2; }
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case KEY_LEFT:
|
case KEY_LEFT:
|
||||||
if(pos_x>3){
|
if ( outer_selected ) {
|
||||||
pos_x=pos_x-2;
|
if( in_x > 3 ) { in_x-=2; }
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if ( out_x > 3 ) { out_x-=2; }
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case KEY_UP:
|
case KEY_UP:
|
||||||
if(pos_y>2){
|
if ( outer_selected ) {
|
||||||
pos_y=pos_y-2;
|
if ( in_y > 2 ) { in_y-=2; }
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if ( out_y > 2 ) { out_y-=2; }
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case KEY_DOWN:
|
case KEY_DOWN:
|
||||||
if(pos_y<6){
|
if ( outer_selected ) {
|
||||||
pos_y=pos_y+2;
|
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;
|
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 == 3 ){ return TOPLEFT; }
|
||||||
if ( y == 2 && x == 5 ){ return TOPCENTER; }
|
if ( y == 2 && x == 5 ){ return TOPCENTER; }
|
||||||
if ( y == 2 && x == 7 ){ return TOPRIGHT; }
|
if ( y == 2 && x == 7 ){ return TOPRIGHT; }
|
||||||
|
@ -132,6 +198,39 @@ e_location get_ttt_position( int y, int x) {
|
||||||
return NONE;
|
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){
|
void free_view(p_view v){
|
||||||
if(v->p_uttt->inception_level == 2){
|
if(v->p_uttt->inception_level == 2){
|
||||||
|
@ -143,3 +242,4 @@ void free_view(p_view v){
|
||||||
v->w_uttt = NULL;
|
v->w_uttt = NULL;
|
||||||
endwin();
|
endwin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue