Re-added the option to use vt100 escape codes
This commit is contained in:
parent
a2142a48b5
commit
2b27aef099
7 changed files with 244 additions and 26 deletions
169
ui_vt100.c
Normal file
169
ui_vt100.c
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
/*
|
||||
* AnnaConnect is a silly little text-based connect 4 game c:
|
||||
* Copyright (C) 2025 Anna Snoeijs. anna@snoeijs.tech
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "ui.h"
|
||||
#include <stdio.h>
|
||||
#include "macros.h"
|
||||
|
||||
#define BOARD_WIDTH_CHARS ( 3 * BOARD_WIDTH + 6 )
|
||||
#define SCOREBOARD_OFFSET 5
|
||||
#define SCOREBOARD_WIDTH 27
|
||||
#define SCOREBOARD_HEIGTH 9
|
||||
|
||||
void initBoard( void ){
|
||||
for( int i = SCOREBOARD_HEIGTH - ( BOARD_HEIGTH + 2 ); i > 0; i-- )
|
||||
putchar( '\n' );
|
||||
printf( "\n " );
|
||||
for( int column = 0; column < BOARD_WIDTH; column++ )
|
||||
printf( "%2d ", column + FIRST_NUMBER );
|
||||
putchar( '\n' );
|
||||
for( int row = BOARD_HEIGTH - 1; row > -1; row-- ){
|
||||
// begin row
|
||||
printf( "%2d " , row + FIRST_NUMBER );
|
||||
for( int column = 0; column < BOARD_WIDTH; column++ )
|
||||
printf( "[ ]" );
|
||||
// end of row
|
||||
printf( "%2d" , row + FIRST_NUMBER );
|
||||
switch( row ){
|
||||
}
|
||||
putchar( '\n' );
|
||||
}
|
||||
// end of board
|
||||
printf( " " );
|
||||
for( int column = 0; column < BOARD_WIDTH; column++ )
|
||||
printf( "%2d ", column + FIRST_NUMBER );
|
||||
#define SCOREBOARD_LINE_END "\033[B\033["XSTR(SCOREBOARD_WIDTH)"D"
|
||||
printf("\033["XSTR(SCOREBOARD_OFFSET)"C\033["XSTR(SCOREBOARD_HEIGTH)"A"
|
||||
"┌───────────────┬────┬────┐"SCOREBOARD_LINE_END
|
||||
"│ player │ 0 │ 1 │"SCOREBOARD_LINE_END
|
||||
"├───────────────┼────┼────┤"SCOREBOARD_LINE_END
|
||||
"│ vertical │ │ │"SCOREBOARD_LINE_END
|
||||
"│ horizontal │ │ │"SCOREBOARD_LINE_END
|
||||
"│ diagonal up │ │ │"SCOREBOARD_LINE_END
|
||||
"│ diagonal down │ │ │"SCOREBOARD_LINE_END
|
||||
"├───────────────┼────┼────┤"SCOREBOARD_LINE_END
|
||||
"│ total │ │ │"SCOREBOARD_LINE_END
|
||||
"└───────────────┴────┴────┘"SCOREBOARD_LINE_END
|
||||
);
|
||||
#undef SCOREBOARD_LINE_END
|
||||
putchar( '\n' );
|
||||
putchar( '\n' );
|
||||
}
|
||||
|
||||
void updateBoard(
|
||||
const wins_t wins,
|
||||
const board_t board,
|
||||
const int column
|
||||
){
|
||||
int heigth = board.heigth [ column ];
|
||||
#ifndef ONLYPUT
|
||||
switch( move ){
|
||||
case PUT: // Only print the put one
|
||||
#endif /* ! ONLYPUT */
|
||||
printf(
|
||||
"\033[%dA\033[%dC%c\033[%dB",
|
||||
heigth + 3,
|
||||
column * 3 + 4,
|
||||
'0' + !!( board.player ),
|
||||
heigth + 1
|
||||
);
|
||||
#ifndef ONLYPUT
|
||||
break;
|
||||
default: // Print all the pieces in the column and the air above
|
||||
printf( "\033[%dA", heigth + 4 );
|
||||
if( heigth < BOARD_HEIGTH ) printf( "\033[%dC ", column * 3 + 4 );
|
||||
else printf( "\033[%dC", column * 3 + 5 );
|
||||
for( int row = heigth; row --> 0; )
|
||||
printf(
|
||||
"\033[B\033[D%c",
|
||||
'0' + !!( board.column [ column ] & ( 1 << row ) )
|
||||
);
|
||||
printf( "\033[2B" );
|
||||
}
|
||||
#endif /* ! ONLYPUT */
|
||||
printf(
|
||||
"\r\033[7A\033[%dC"
|
||||
"%3d │%3d\033[B\033[8D"
|
||||
"%3d │%3d\033[B\033[8D"
|
||||
"%3d │%3d\033[B\033[8D"
|
||||
"%3d │%3d\033[2B\033[8D"
|
||||
"%3d │%3d\033[2B"
|
||||
,BOARD_WIDTH_CHARS + SCOREBOARD_WIDTH - 8
|
||||
,wins.count0.vertical, wins.count1.vertical
|
||||
,wins.count0.horizontal, wins.count1.horizontal
|
||||
,wins.count0.diagonalUp, wins.count1.diagonalUp
|
||||
,wins.count0.diagonalDown, wins.count1.diagonalDown
|
||||
,wins.count0.total, wins.count1.total
|
||||
);
|
||||
printf( "\033[2K\n" );
|
||||
}
|
||||
|
||||
int askColumn(
|
||||
const board_t board
|
||||
#ifndef ONLYPUT
|
||||
,const move_t move
|
||||
#endif /* ONLYPUT */
|
||||
){
|
||||
int column;
|
||||
for(;;){
|
||||
#ifndef ONLYPUT
|
||||
switch( move ){
|
||||
case PUT: printf( "Wher player %d put? ", board.player );
|
||||
break;
|
||||
case POP: printf( "Wher player %d pop? ", board.player );
|
||||
}
|
||||
#else /* ONLYPUT */
|
||||
printf( "Wher player %d put? ", board.player );
|
||||
#endif /* ONLYPUT */
|
||||
printf( "\033[K" ); // clear everything after cursor
|
||||
scanf( "%d", &column );
|
||||
column -= FIRST_NUMBER;
|
||||
if( column >= 0 && column < BOARD_WIDTH )
|
||||
#ifndef ONLYPUT
|
||||
switch( move ){
|
||||
case PUT:
|
||||
#endif /* ! ONLYPUT */
|
||||
if( board.heigth [ column ] < BOARD_HEIGTH )
|
||||
return column;
|
||||
else printf( "\033[2Apls enter a column that ain't full" );
|
||||
#ifndef ONLYPUT
|
||||
break;
|
||||
case POP:
|
||||
if( board.heigth [ column ] != 0 )
|
||||
return column;
|
||||
else printf( "\033[2Apls enter a column that ain't empty" );
|
||||
}
|
||||
#endif /* ! ONLYPUT */
|
||||
else
|
||||
printf( "\033[2Apls enter valid column" );
|
||||
printf( "\033[K\n" );
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef ONLYPUT
|
||||
move_t askMove( void ){
|
||||
move_t move = PUT;
|
||||
// TODO: actually ask what move
|
||||
return move;
|
||||
}
|
||||
#endif /* ! ONLYPUT */
|
||||
|
||||
#undef BOARD_WIDTH_CHARS
|
||||
#undef SCOREBOARD_OFFSET
|
||||
#undef SCOREBOARD_WIDTH
|
||||
#undef SCOREBOARD_HEIGTH
|
||||
Loading…
Add table
Add a link
Reference in a new issue