【问题标题】:Cursor stuck in a board using ncurses (programming SOS game)使用 ncurses 将光标卡在板上(编程 SOS 游戏)
【发布时间】:2019-11-25 07:54:08
【问题描述】:

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

我开始使用带有mvprintw 函数的ncurses,然后一切都变糟了。 我把光标卡在了板子的最后一列和最后一行,这扰乱了程序的良好行为。

enter image description here

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

【问题讨论】:

  • 底部的refresh; 不能为你工作,它应该被称为refresh();
  • 抱歉,我在这里手动添加了它,因为它在操作复制/粘贴时会自行擦除。但是在我的程序里写的很好。
  • 从你的描述中看不太清楚是什么问题,而且这段代码还不够完整,无法测试。
  • 我尝试使用该工具为它放置代码,但它不起作用。在这里,您可以访问整个代码:github.com/Zaregtyp/SOS-game

标签: c cursor ncurses


【解决方案1】:

基本问题是您将诅咒与 stdio 混合在一起——它们确实不能一起使用。尝试将scanf 更改为scanw,看看会发生什么。

【讨论】:

  • 我已经从头开始我的功能 PrintBoard 并且它现在可以工作,但我仍然在为彩色打印而苦苦挣扎。如果玩家 1 提出观点,则链接必须以他的颜色打印,并且与玩家 2 相同。我不知道我的函数 UpdateDirInfo 是否适用于 Bit Wise 类型的代码。 (或者如果 PrintBoard 再次表现不佳)。再次一切都在这里:github.com/Zaregtyp/SOS-game
猜你喜欢
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 2017-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多