/* 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 /* #include */ /* only for the 'clear' command , big overload ??*/ static char bord[8][12]; /* making it static invite to use the 'board' as storage for ALL variables */ char pl, pl0 ; int connect4, cnct, s ; int a[7] , lm ; int ax[2][18] ; 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;033m" ; } 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 || bord[0][1] >= 1) printf ("%s %c \e[39m", bcol, brd ) ; } printf("\n"); } } /*+++++++++++++++++++++++++++++++++++*/ int main() { const char *text ; char replay[41] ; int i, h, key, move ; /* key = pressed key*/ text="choose a column :" ; /*int*/ startscreen() ; /* set neccesary environment, like player etc ...*/ /* for ( h=0 ; h<19; h++ ) { */ /* ax[0][h]=0; */ /* ax[1][h]=0; */ /* } */ 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 (bord[0][0]) { case 1: pl='O' ;pl0='X' ; break; case 2: pl='X' ;pl0='O' ; break; } printf("\bplayer %d, %s", bord[0][0], 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 0; } 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, bord[0][0], 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; } } /*int*/ 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",bord[0][0], pl, lm, replay); system ( "setterm --foreground default" ) ; return 0; } /* end of game already ??? */ printf("\nPlayer %d reached a %d stone connection\n",bord[0][0],connect4); /* next player plz ,change player 1,2 by NOT */ /* player++ = !player; notworking */ /* --player = !player++; notworking */ /* --player = !(player)++; */ --bord[0][0]; /* player = player -1 */ bord[0][0] = !bord[0][0]; ++bord[0][0] ; } /* end from for(move=1 */ bordprnt() ; printf("Its a tie,\n remise,\n equal,\n fair play,\n no winners vs no losers, \n used keys are: \n %s\n\n", replay); return 0 ; } /* 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 */ int checking4connect(){ int i, h, s, r ; for ( h = 1; h <= 7 ; h++ ) { for (s=0 ; s<=17; s++){ ax[0][s] =0 ; ax[1][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]='-' ;} } r = 1 ; for (s=0; s<4; s++) flot(pl,pl0,s,r); if (bord[0][1] == 2) printf( "\n 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 ax[1][1:%d ax[1][2:%d ax[1][3:%d a4b[:%d\n", h, i, a[1], a[2], a[3], a[4], a[5], a[6], a[7],ax[0][1],ax[0][2],ax[0][3],ax[0][4],ax[1][1],ax[1][2],ax[1][3],ax[1][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]='-' ;} } r = 5 ; for (s=0; s<4; s++) flot(pl,pl0,s,r); if (bord[0][1] == 2) printf( "\n 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[0][5],ax[0][6],ax[0][7],ax[0][8],ax[1][5],ax[1][6],ax[1][7],ax[1][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]='-' ;} } r = 9 ; for (s=0; s<4; s++) flot(pl,pl0,s,r); if (bord[0][1] == 2) printf( "\n 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[0][9],ax[0][10],ax[0][11],ax[0][12],ax[1][9],ax[1][10],ax[1][11],ax[1][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] ;} /* vertical above centerpoint, */ 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]='-' ;} } r = 13 ; for (s=0; s<4; s++) flot(pl,pl0,s,r); if (bord[0][1] == 2) printf( "\n 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[0][13],ax[0][14],ax[0][15],ax[0][16],ax[1][13],ax[1][14],ax[1][15],ax[1][16] ); for (s=0; s<17; s++) { if ( ax[0][s] > ax[0][0] ) ax[0][0] = ax[0][s] ; if ( ax[1][s] > ax[1][0] ) ax[1][0] = ax[1][s] ; } for (s=1; s<17; s++) if (ax[0][0]==ax[0][s]) ax[0][17]++; for (s=1; s<17; s++) if (ax[1][0]==ax[1][s]) ax[1][17]++; bord[h][8]=ax[0][0]+48; bord[h][9]=ax[0][17]+48; bord[h][10]=ax[1][0]+48; bord[h][11]=ax[1][17]+48; if ( ax[0][0]==1 && ax[1][0]==0 ) connect4=1; if ( ax[0][0]==2 && ax[1][0]==0 ) connect4=2; if ( ax[0][0]==3 && ax[1][0]==0 ) connect4=3; if ( ax[0][0]==4 ) connect4=4; if (cnct < connect4) cnct = connect4 ; if (bord[0][1] == 2) printf( " ax:%d axb:%d C:%d\n", ax[0][0], ax[1][0],connect4); } /* end of for_(h=1... */ return 0 ; } /* end checking4connect() */ /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ int flot(int pl, int pl0, int s, int r){ int tm, atemp, atm, ta, p, q ; tm=a[4+s]+a[3+s]+a[2+s]+a[1+s]; for (q=0 ; q<2 ; q++){ p=(pl && !q)+(pl0 && q); ax[q][r+s]=0; atemp=tm/pl; atm= atemp*pl; ta=tm-atm; if (ta == 0) ax[q][1+s]=atemp; /* count only stone THIS player */ if (bord[0][1] == 2) printf("\e[38;5;%dm%d,%d,%d,%d,%d,s:%d|\e[39m ",202+p*20,tm,atemp,atm,ta,ax[q][1+s],s); } return 0 ; } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ int startscreen(){ int key ; system ( "clear [all]" ); /* clear the screen */ /* printf("\f"); wont work??? */ /* clear the screen */ bord[0][0] = 1 ; /* set player*/ bord[0][1] = 0 ; /* show normal game only*/ 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"); while (key != 'd' && key != 's' && key!=13 /*&& key !=10*/ ) { scanf( "%c", &key ); if(key== 's') bord[0][1] = 1 ; /* small showing */ if(key== 'd') bord[0][1] = 2 ; /* full develope show */ scanf("%*[^\n]%*c"); /* should clear the inputbuffer ...https://cboard.cprogramming.com/c-programming/138930-c-programming-scanf-function-help-urgent.html */ } if (bord[0][1] >= 1 ) printf("key:%c\nkey:%d\nshow:%d\n",key,key,bord[0][1]); scanf("%*[^\n]%*c"); return 0 ; } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */