/* chris born, 2017, bord met controle 4op1rij */ /* speelbaar voor 2 mensen, die zelf netjes bijhouden wie wat gedaan heeft */ /* het programmaatje eindigt met een replay-lijst met de uitgevoerde zetten */ #include static char bord[8][12]; /* making it static invite to use the 'board' as storage for ALL variables */ char pl, pl0 ; int connect4, cnct, ta, s ; int a[7] , lm , show_tb, show ; int ax[18] , axb[18] , atemp ; void bordprnt(void) { /* print the actual 'board' */ int i, h ; char brd , *bcol ; printf("\e[38;5;33m 1 2 3 4 5 6 7 \e[39m\n"); /* foreground / ansi /blue */ for ( i = 1 ; i <= 11 ; i++ ) { for ( h = 1 ; h <= 7 ; h++ ) { bcol="\e[39m" ; /* foreground / ansi / default colors */ brd=bord[h][i] ; if ( brd == 0 ) brd ='+' ; if ( brd == 'O' ) bcol="\e[38;5;196m" ; /* foreground / ansi /red */ if ( brd == 'X' ) bcol="\e[38;5;226m" ; /* foreground / ansi /yellow */ if ( i == 7 ) { brd='_'; bcol="\e[38;5;33m" ; } if ( i == 8 && pl=='O' ) bcol="\e[38;5;196m" ; if ( i == 8 && pl=='X' ) bcol="\e[38;5;226m" ; if ( i == 10 && pl0=='O') bcol="\e[38;5;196m" ; if ( i == 10 && pl0=='X') bcol="\e[38;5;226m" ; if (i<= 6 || show == 1) printf ("%s %c \e[39m", bcol, brd ) ; } printf("\n"); } } /*+++++++++++++++++++++++++++++++++++*/ int main() { const char *text ; char replay[41] ; int i, h, key, move, player ; /* key = pressed key*/ system ( "clear [all]" ); /* clear the screen */ /* printf("\f"); wont work??? */ /* clear the screen */ text="choose a column :" ; player=1 ; printf("Hello, welcome to this board init \n its just the playboard for 4connect \n so you play against (another) human \n and not against a computer \n"); printf("\nYet the computer will aid you if wanted\n Choose with key 's' to show the status per move\n press 'd' to show varmapping\n"); printf("\n\n to play the game choose a column between 1 and 7 to drop the 'stone' \n if a column is already filled you need to choose another one\n you can quit with key '0'\n the board is empty so, \n"); scanf( "%c", &key ); if ((char)key == 'd') show_tb = 1; if ((char)key == 's' || show_tb == 1) show = 1; if (show_tb == 1) printf("key:%c\nkey:%d\nshow:%d\nshowtb:%d\n",key,key,show,show_tb); for ( h = 1 ; h <= 7 ; h++ ) { bord[h][0]= 7; bord[h][8]= '_'; bord[h][9]= '_'; bord[h][10]= '_'; bord[h][11]= '_'; for ( i = 1 ; i <= 6 ; i++ ) bord[h][i]= 0 ; } for ( move = 0 ; move < 42; move++) { /* 7x6 = 42 stones */ bordprnt() ; switch (player) { case 1: pl='O' ;pl0='X' ; break; case 2: pl='X' ;pl0='O' ; break; } printf("\bplayer %d, %s", player, text ); key =-1; while(key <0 || key > 7 || (key >=1 && key <=7 && bord[key][0] <= 1 ) ) { scanf( "%d", &key ); if (key == 0 ) { printf("\n leaving game, used keys: %s \n", replay); return ; } scanf("%*[^\n]%*c"); /* should clear the inputbuffer ...https://cboard.cprogramming.com/c-programming/138930-c-programming-scanf-function-help-urgent.html */ } system ( "clear [all]" ); /* clear the (terminal) screen */ printf("\n \n last move %d from player %d in column %d with an \"%c\"\n \n \n", move+1, player, key, pl); lm = key ; for ( i = 6; i > 0 ; i-- ) { if (bord[key][i] == 0 ) { bord[key][0]= i ; bord[key][i]= pl; replay[move] = key+48 ; /* make ascii value */ replay[move+1] = '\0'; /* null terminator for string */ break; } } checking4connect(); /* checking 4connect jumps here to routine*/ if (cnct == 4) { bordprnt() ; printf ("\nPlayer %d has won this time\nwith stone \"%c\" in column %d\n4 stones are connected \n\n used keys: %s \n\n\n",player, pl, lm, replay); system ( "setterm --foreground default" ) ; return; } /* end of game already ??? */ printf("\nPlayer %d reached a %d stone connection\n",player,connect4); /* next player plz ,change player 1,2 by NOT */ /* player++ = !player; notworking */ --player; /* player = player -1 */ player = !player; ++player ; } /* end from for(move=1 */ bordprnt() ; printf("Its a tie,\n remise,\n equal,\n fair play,\n no winners vs no losers,\n %s \n used keys: ", replay); } /* program has ended here, will the last one leaving turn off the lights plz */ /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ /* check 4 axes for 4 times a stone */ /* o _ _ _ _ _ o */ /* _ o _ _ _ o _ */ /* _ _ o _ o _ _ */ /* 1 2 3 X 5 6 7 */ /* _ _ o o o _ _ */ /* _ o _ o _ o _ */ /* o _ _ o _ _ o */ checking4connect(){ int i, h, s, tm, atemp, atm, ta ; for ( h = 1; h <= 7 ; h++ ) { for (s=0 ; s<=17; s++){ ax[s] =0 ; axb[s] =0 ; } i = bord[h][0] ; /* if ( i == 7 || i==0 ) { continue; } */ /* should continue with NEXT h++ */ if (i>6) i=6 ; /* fetch hight of column , set to lowest if not yet used*/ for (s=1; s<8; s++) a[s]=0; a[4] = bord[h][i] ; /* centerpoint 'X' */ for (s=1; s<4; s++){ if (h-s >= 1) {a[4-s]=bord[h-s][i] ;} else {a[4-s]='-' ;} } for (s=1; s<4; s++){ if (h+s <= 7) {a[4+s]=bord[h+s][i] ;} else {a[4+s]='-' ;} } for (s=0; s<4; s++) { tm=a[4+s]+a[3+s]+a[2+s]+a[1+s]; atemp=tm/pl; atm= atemp*pl; ta=tm-atm; if (ta == 0) ax[1+s]=atemp; /* count only stone THIS player */ if (show_tb == 1) printf("\e[38;5;202m%d\e[39m ",ax[1+s]); atemp=tm/pl0; atm= atemp*pl0; ta=tm-atm; if (ta == 0) axb[1+s]=atemp; /* count only stone THAT player */ if (show_tb == 1) printf("\e[38;5;202m%d\e[39m ",axb[1+s]); } if (show_tb == 1) printf( " h:%d i:%d\ta1:%c\ta2:%c\ta3:%c\ta4:%c\ta5:%c\ta6:%c\ta7:%c\ta[1:%d a[2:%d a[3:%d a[4:%d axb[1:%d axb[2:%d axb[3:%d a4b[:%d\n", h, i, a[1], a[2], a[3], a[4], a[5], a[6], a[7],ax[1],ax[2],ax[3],ax[4],axb[1],axb[2],axb[3],axb[4] ); for (s=1; s<8; s++) a[s]=0; a[4] = bord[h][i] ; for (s=1; s<4; s++){ if (h-s >= 1 && i+s <= 6) { a[4-s]=bord[h-s][i+s] ;} else {a[4-s]='-' ;} } for (s=1; s<4; s++){ if (h+s <= 7 && i-s >= 1 ) {a[4+s]=bord[h+s][i-s] ;} else {a[4+s]='-' ;} } for (s=0; s<4; s++) { tm=a[4+s]+a[3+s]+a[2+s]+a[1+s]; atemp=tm/pl; atm= atemp*pl; ta=tm-atm; if (ta == 0) ax[5+s]=atemp; /* count only stone THIS player */ if (show_tb == 1) printf("\e[38;5;212m%d\e[39m ",ax[5+s]); atemp=tm/pl0; atm= atemp*pl0; ta=tm-atm; if (ta == 0) axb[5+s]=atemp; /* count only stone THAT player */ if (show_tb == 1) printf("\e[38;5;212m%d\e[39m ",axb[5+s]); } if (show_tb == 1) printf( " h:%d i:%d\ta1:%c\ta2:%c\ta3:%c\ta4:%c\ta5:%c\ta6:%c\ta7:%c\tb1:%d b2:%d b3:%d b4:%d b1b:%d b2b:%d b3b:%d b4b:%d\n", h, i, a[1], a[2], a[3], a[4], a[5], a[6], a[7],ax[5],ax[6],ax[7],ax[8],axb[5],axb[6],axb[7],axb[8] ); for (s=1; s<8; s++) a[s]=0; a[4] = bord[h][i] ; for (s=1; s<4; s++){ if (h-s >= 1 && i-s <= 6 ) {a[4-s]=bord[h-s][i-s] ;} else {a[4-s]='-' ;} } for (s=1; s<4; s++){ if (h+s <= 7 && i+s <= 6 ) {a[4+s]=bord[h+s][i+s] ;} else {a[4+s]='-' ;} } for (s=0; s<4; s++) { tm=a[4+s]+a[3+s]+a[2+s]+a[1+s]; atemp=tm/pl; atm= atemp*pl; ta=tm-atm; if (ta == 0) ax[9+s]=atemp; /* count only stone THIS player */ if (show_tb == 1) printf("\e[38;5;222m%d\e[39m ",ax[9+s]); atemp=tm/pl0; atm= atemp*pl0; ta=tm-atm; if (ta == 0) axb[9+s]=atemp; /* count only stone THAT player */ if (show_tb == 1) printf("\e[38;5;222m%d\e[39m ",axb[9+s]); } if (show_tb == 1) printf( " h:%d i:%d\ta1:%c\ta2:%c\ta3:%c\ta4:%c\ta5:%c\ta6:%c\ta7:%c\tc1:%d c2:%d c3:%d c4:%d c1b:%d c2b:%d c3b:%d c4b:%d\n", h, i, a[1], a[2], a[3], a[4], a[5], a[6], a[7],ax[9],ax[10],ax[11],ax[12],axb[9],axb[10],axb[11],axb[12] ); for (s=1; s<8; s++) a[s]=0; a[4] = bord[h][i] ; for (s=1; s<4; s++){ if (i-s >= 1) {a[4-s]=bord[h][i-s] ;} else {a[4-s]='-' ;} } for (s=1; s<4; s++){ if (i+s <= 6) {a[4+s]=bord[h][i+s] ;} else {a[4+s]='-' ;} } for (s=0; s<4; s++) { tm=a[4+s]+a[3+s]+a[2+s]+a[1+s]; atemp=tm/pl; atm= atemp*pl; ta=tm-atm; /* INT result from FLOAT calc */ if (ta == 0) ax[13+s]=atemp; /* count only stone THIS player */ if (show_tb == 1) printf("\e[38;5;231m%d\e[39m ",ax[13+s]); atemp=tm/pl0; atm= atemp*pl0; ta=tm-atm; if (ta == 0) axb[13+s]=atemp; /* count only stone THAT player */ if (show_tb == 1) printf("\e[38;5;231m%d\e[39m ",axb[13+s]); } if (show_tb == 1) printf( " h:%d i:%d\ta1:%c\ta2:%c\ta3:%c\ta4:%c\ta5:%c\ta6:%c\ta7:%c\td1:%d d2:%d d3:%d d4:%d d1b:%d d2b:%d d3b:%d d4b:%d\n", h, i, a[1], a[2], a[3], a[4], a[5], a[6], a[7],ax[13],ax[14],ax[15],ax[16],axb[13],axb[14],axb[15],axb[16] ); for (s=0; s<17; s++) { if ( ax[s] > ax[0] ) ax[0] = ax[s] ; if ( axb[s] > axb[0] ) axb[0] = axb[s] ; } for (s=1; s<17; s++) if (ax[0]==ax[s]) ax[17]++; for (s=1; s<17; s++) if (axb[0]==axb[s]) axb[17]++; bord[h][8]=ax[0]+48; bord[h][9]=ax[17]+48; bord[h][10]=axb[0]+48; bord[h][11]=axb[17]+48; if ( ax[0]==1 && axb[0]==0 ) connect4=1; if ( ax[0]==2 && axb[0]==0 ) connect4=2; if ( ax[0]==3 && axb[0]==0 ) connect4=3; if ( ax[0]==4 ) connect4=4; if (cnct < connect4) cnct = connect4 ; if (show_tb == 1) printf( " ax:%d axb:%d C:%d\n", ax[0], axb[0],connect4); } /* end of for_(h=1... */ } /* end checking4connect(;) */ void flot(void){ int tm, atemp, atm; if (show_tb == 1) printf("pl:%d s:%d tm:%d atemp:%d atm:%d ta:%d\n",pl,s,tm,atemp,atm,ta); tm = a[4+s]+a[3+s]+a[2+s]+a[1+s] ; atemp = tm/pl ; /* this dividing with INT will strip the result from any "floating point result" */ atm = atemp*pl ; /* the multiply WONT use the value behind the dot */ ta = tm-atm ; /* the FLOATING behind the dot result from INT calc */ /* if TA is NOT zero there was a "float" /* if TA == 0 then only 1 stone type is found */ if (show_tb == 1) printf("pl:%d s:%d tm:%d atemp:%d atm:%d ta:%d\n",pl,s,tm,atemp,atm,ta); }