我正在开发一个游戏,用户必须更改单词中的字母才能使用光标将其变为目标单词。我的代码正在运行,但是当用户输入时,光标不会移动,字母也不会更改。在“游戏状态”之前,在循环的下一次迭代之前打印符号。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "WordGame.h"
int main() {
char current_letters[] = {'b', 'e', 'l', 'l'};
char target_letters[] = {'c', 'o', 'a', 't'};
int position_of_cursor = 2;
const int NUMBER_OF_LETTERS = 4;
bool game_finished = false;
int number_of_letter_changes = 0;
int max = 3;
while (number_of_letter_changes < 8) {
display_game_state (target_letters, current_letters, NUMBER_OF_LETTERS, position_of_cursor, number_of_letter_changes);
printf("Enter a command: ");
char letter = get_user_char();
if( letter == '>') {
move_cursor_right(position_of_cursor, max);
}
else if ( letter == '<') {
move_cursor_left(position_of_cursor, max);
}
move_cursor_right (&position_of_cursor, max);
printf("%d", position_of_cursor);
move_cursor_right (&position_of_cursor, max);
printf("%d", position_of_cursor);
number_of_letter_changes++;
}
return 0;
}
WordGame.c代码
#include <stdio.h>
#include <stdbool.h>
char get_user_char() {
//1. We create the output variable with the value the user has input by keyboard
char res = getchar();
//2. We discard any extra character the user has input by keyboard
bool finish = false;
char dummy_char = ' ';
while (finish == false) {
dummy_char = getchar();
if (dummy_char == '\n')
finish = true;
}
//3. We return the output variable
return res;
}
void display_game_state(char* pTarget_letters, char* pCurrent_letters,const int NUMBER_OF_LETTERS,
const int cursor_position, const int number_of_letter_changes)
{
printf("---Game State ---\n");
// printf("%p\n", pCurrent_letters);
int c;
printf("Target: ");
for(c = 0; c <= 3; ++c) {
printf("%c", *pTarget_letters);
pTarget_letters = pTarget_letters + 1;
}
printf("\n-----------------\n");
for(c = 0; c <= 3; ++c) {
printf ("%c", *pCurrent_letters);
pCurrent_letters = pCurrent_letters + 1;
}
int i;
const char DASH = '-';
const char SPACE = ' ';
for(i = 0; i < cursor_position ; ++i) {
printf("%c", SPACE);
}
printf("\n%c", DASH);
printf("\nChanges: %d\n", number_of_letter_changes);
char letter;
}
void move_cursor_right(int* pPosition_of_cursor, const int max)
{
printf ("%c", *pPosition_of_cursor);
pPosition_of_cursor = pPosition_of_cursor + 1;
}
void move_cursor_left(int* pPosition_of_cursor, const int max)
{
printf ("%c", *pPosition_of_cursor);
pPosition_of_cursor = pPosition_of_cursor - 1;
}
void change_letters(char* pLetters, int position, char new_letter, int max)
{
char* plocation_of_letter_to_change = pLetters + position;
}
void start_game(char * pCurrent, char* pTarget, const int size_of_word)
{
int position_of_cursor = 0;
bool game_finished = false;
int number_of_letter_changes = 0;
}