首页 > 解决方案 > 使用 ncurses 将光标卡在板上(编程 SOS 游戏)

问题描述

我正在编写 SOS 游戏。我printf用来打印带有斜线或/和连字符的板和字母。当我被告知必须printf根据播放器(播放器 1=红色和播放器 2=黄色)使用彩色打印之前的符号时,我遇到了问题。printf你不能在同一行以不同的颜色打印符号(你可以,但代码会很大)。

我开始将 ncurses 与该mvprintw功能一起使用,然后一切都变糟了。我将光标卡在了板子的最后一列和最后一行,这扰乱了程序的良好行为。

在此处输入图像描述

上图是我输入“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

标签: ccursorncurses

解决方案


基本问题是您将诅咒与 stdio 混合在一起——它们实际上并没有结合在一起。尝试更改scanfscanw,看看会发生什么。


推荐阅读