我正在编写S-O-S游戏。我使用printf打印黑板和带有斜杠或/和连字符的字母。当我被告知必须使用printf根据播放器(播放器1=red和播放器2=yellow)打印前面的符号时,我遇到了一个问题。在printf中,你不能用不同的颜色打印同一行的符号(你可以这样做,但是代码会很大)。
我开始在mvprintw函数中使用ncurses,然后一切都变坏了。我把光标卡在了黑板的最后一列和最后一行,这扰乱了程序的良好行为。
上图是当我输入"f7“和字母"S”时(列h不起作用,所有内容都向左移动)。
这是使用main函数打印线路板的程序:
void printboard(sos *jg){
//my_win = create_newwin(25, 50, 0, 0);
int row,col; /* to store the number of rows and */
int num=1;
int let=97;
/*the number of colums of the screen */
//while (getch()!= '\n'){
/* start the curses mode */
start_color();
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_YELLOW, COLOR_BLACK);
curs_set(2);
for(row=1;row<35;row++){
mvprintw(1,1,"+",0);
if((row-1)%4==0) mvprintw(row,1,"+",0);
for(col=2; col<66;col+=8){
mvprintw(0,col," %c ",let-8);
if(row<34){
if((row-1)%4==0) mvprintw(row,col," - - - +",0);
if((row-1)%4==1) {
mvprintw(row,col-1,"|",0);
mvprintw(row,65,"|",0);
}
if((row-1)%4==2) {
mvprintw(row,col-1,"|",0);
mvprintw(row,65,"|",0);
mvprintw(row,0,"%d",num/8);
mvprintw(row,66,"%d",num/8);
num++;
}
if((row-1)%4==3) {
mvprintw(row,col-1,"|",0);
mvprintw(row,65,"|",0);
}
}
mvprintw(34,col," %c ",let-8);
let++;
}
}
for(row=1;row<35;row++){
for(col=2; col<73;col+=8){
if(row<34){
if((row-1)%4==1) {
if(jg->L[row/4][col/8] & NW_MASK){
if(jg->L[row/4][col/8]==jg->C[row/4][col/8]){
attron(COLOR_PAIR(1));
mvprintw(row/4,col/8,"\\",0);
attroff(COLOR_PAIR(1));
}
else {attron(COLOR_PAIR(2)); mvprintw(row/4,col/8,"\\",0);attroff(COLOR_PAIR(2));}
}
if( jg->L[row/4][col/8] & N_MASK){
if(jg->L[row/4][col/8]==jg->C[row/4][col/8]){
attron(COLOR_PAIR(1));
mvprintw(row/4,col/8,"|",0);
attroff(COLOR_PAIR(1));
}
else {attron(COLOR_PAIR(2)); mvprintw(row/4,col/8,"\\",0);attroff(COLOR_PAIR(2));}
}
if( jg->L[row/4][col/8] & NE_MASK) {
if(jg->L[row/4][col/8]==jg->C[row/4][col/8]){
attron(COLOR_PAIR(1));
mvprintw(row/4,col/8,"/",0);
attroff(COLOR_PAIR(1));
}
else {attron(COLOR_PAIR(2)); mvprintw(row/4,col/8,"\\",0);attroff(COLOR_PAIR(2));}
}
}
if((row-1)%4==2) {
mvprintw(row,col-5,"%c",jg->V[row/4][col/8]);
move(200,100);
//printf("%c\n",jg->V[row/4][col/8]);
}
if((row-1)%4==3) {
}
}
}
}
//printf("\n");
//}
//move(100,100);
//getch();
refresh;
}
int main(){
sos jg, *pjg;
pjg=&jg;
int action=0,move=0, count=0,check=0;
num_player player=JOGADOR1;
InitGame(pjg);
initscr();
noecho();
while(count<64){
printboard(&jg);
while(action!=1){
move=GetPlayerMove(player);
action=CheckAndSetMove(&jg, move, player, action); // return 0 for no , 1 for yes
}
check=CheckSequence(&jg, move, player);
if(check==0) player++;
printboard(&jg);
action=0;
count++;
check=0;
}
endwin();
return 0;
}
提前感谢您对我们的帮助!
编辑:这里有一个指向完整代码https://github.com/Zaregtyp/SOS-game的链接
发布于 2019-11-25 07:54:08
最基本的问题是你把诅咒和stdio混在一起了--它们真的不能在一起。尝试将scanf更改为scanw,看看会发生什么情况。
https://stackoverflow.com/questions/59002204
复制相似问题