使用ncurses(编程SOS游戏)将光标固定在板上的光标

问题描述 投票:1回答:1

我正在编写S-O-S游戏。并且我使用printf来打印木板和带有斜线或/和连字符的字母。当我被告知我必须使用printf来打印先前的符号时,我遇到了问题,具体取决于播放器(播放器1 =红色,播放器2 =黄色)。使用printf,您不能在同一行中以不同的颜色打印符号(可以,但是代码会很大)。

我开始将ncurses与mvprintw函数一起使用,然后一切都变糟了。我的光标停留在板子的最后一列和最后一行,这干扰了程序的良好行为。

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的链接

c cursor ncurses
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.