我是C编程的新手,需要一些帮助,试图在用C编写的Mastermind游戏中准确删除重复的白钉,有3种游戏模式,这是允许重复颜色的中等。如果用户输入'RRGG'和随机生成的值是'ROYG',我试图找出检查第一个'R'(黑色是一个挂钩)的最佳方法,所以我不会得到一个白色挂钩第二个。正如您将在下面看到的,我正在尝试介绍检查阵列的概念,但不确定如何去做(尽管我已尝试过)。任何其他一般编码建议将受到欢迎和赞赏。
#include "masterMindProject.h"
// Declaration of Function Variables & Arrays
int i, j;
int num = 0;
int blackPegCounter = 0;
int whitePegCounter = 0;
int attempts = 15;
int completedAttempts = 0;
int invalidCharFlag = 0;
int checkedBlackArray[] = {0, 0, 0, 0, '\0'};
int checkedWhiteArray[] = {0, 0, 0, 0, '\0'};
char name[MAX_LENGTH + 1];
char colour[NUMCOLS + 1];
char emptyArray[FOURARRAY + 1];
char randomEmptyArray[FOURARRAY + 1];
FILE *fptr;
// Initialise Character Array
colour[0] = 'R';
colour[1] = 'O';
colour[2] = 'Y';
colour[3] = 'G';
colour[4] = 'B';
colour[5] = 'I';
colour[6] = 'V';
// initialize random seed:
srand ( time(NULL) );
system("cls");
// Medium Mode Header
printf("M E D I U M M O D E\n");
printf("_________________\n");
// Randomly Shuffle the 'colour[]' array and Store the result in the randomEmptyArray[]
for(i=0; i<FOURARRAY; i++)
{
randomEmptyArray[i] = colour[randFunc()];
// printf("%c", randomEmptyArray[i]);
} // End for loop
START: printf("\nHere are the Colours, Please Enter Capital letters only, no spaces: \nR O Y G B I V\n");
do
{
blackPegCounter = 0;
// User enters Values
printf("\nEnter any 4 from the Above, you have %d Attempts Remaining\n", attempts);
fflush(stdin);
scanf("%s", emptyArray);
printf("\n", emptyArray);
// Validation
if ( (emptyArray[0] == 'R' || emptyArray[0] == 'O' || emptyArray[0] == 'Y' || emptyArray[0] == 'G' || emptyArray[0] == 'B'|| emptyArray[0] == 'I' || emptyArray[0] == 'V') ||
(emptyArray[1] == 'R' || emptyArray[1] == 'O' || emptyArray[1] == 'Y' || emptyArray[1] == 'G' || emptyArray[1] == 'B'|| emptyArray[1] == 'I' || emptyArray[1] == 'V') ||
(emptyArray[2] == 'R' || emptyArray[2] == 'O' || emptyArray[2] == 'Y' || emptyArray[2] == 'G' || emptyArray[2] == 'B'|| emptyArray[2] == 'I' || emptyArray[2] == 'V') ||
(emptyArray[3] == 'R' || emptyArray[3] == 'O' || emptyArray[3] == 'Y' || emptyArray[3] == 'G' || emptyArray[3] == 'B'|| emptyArray[3] == 'I' || emptyArray[3] == 'V') )
{
printf("\nYou Have chosen correct Colours...");
}
else
{
printf("Please Enter Valid Characters from the List R O Y G B I V\n");
goto START;
} // End of Validation if/else
printf("\nYou Entered: %s\n", emptyArray);
// Loop to Check Black Pegs / White Pegs
for (i=0; i<FOURARRAY; i++)
{
if ( emptyArray[i] == randomEmptyArray[i] )
{
printf("Black Peg | ");
blackPegCounter++;
checkedBlackArray[i]++;
}
else if (
(
( emptyArray[0] == randomEmptyArray[1] ) ||
( emptyArray[0] == randomEmptyArray[2] ) ||
( emptyArray[0] == randomEmptyArray[3] )
)
||
(
( emptyArray[1] == randomEmptyArray[0] ) ||
( emptyArray[1] == randomEmptyArray[2] ) ||
( emptyArray[1] == randomEmptyArray[3] )
)
||
(
( emptyArray[2] == randomEmptyArray[0] ) ||
( emptyArray[2] == randomEmptyArray[1] ) ||
( emptyArray[2] == randomEmptyArray[3] )
)
||
(
( emptyArray[3] == randomEmptyArray[0] ) ||
( emptyArray[3] == randomEmptyArray[1] ) ||
( emptyArray[3] == randomEmptyArray[2] )
)
)
{
checkedWhiteArray[i]++;
if ( checkedWhiteArray[i] == checkedBlackArray[i] )
{
printf("White Peg | ");
whitePegCounter++;
}
else
{
printf("----------| ");
}
}
} // End Loop to Check Black Pegs / White Pegs
attempts--;
completedAttempts++;
// Game over if No More Attaempts or Win the Game if 4 Black Pegs
// If Win, record details and Write to File
if( attempts == 0)
{
printf("\nG A M E O V E R");
printf("\nYou have no more Chances");
break;
}
else if( blackPegCounter == 4 )
{
printf("\n\nYou Guessed the Sequence and won the Game in %d Attempts", completedAttempts);
printf("\n\nPlease Enter your Intials: ");
scanf("%s", &name);
fptr = fopen (FILENAME, "a");
if( fptr == NULL )
{
printf("\n Could not open the file %s.", FILENAME);
system("Pause");
exit(0);
} // End of if ( fptr == NULL )
fscanf(fptr, "%s %d %d", &name, &completedAttempts, &whitePegCounter);
fprintf(fptr, "\nNAME: %s: Completed in %d Attempt(s) and with %d Whitepegs.", name, completedAttempts, whitePegCounter);
fclose(fptr);
break;
} // End if / else if (Lose, Win Write To File)
}
while( blackPegCounter != 4 );
// End do While
printf("\n\n\n");
system("Pause");
答案 0 :(得分:1)
你可以使用一个hashmap并将pegs颜色作为键和数字作为值,这应该会让你更容易。
答案 1 :(得分:1)
我是C的新手,我正在设计策划者。我有一个不同的分配白钉的解决方案。
我有2个char
数组,一个存储颜色代码,另一个存储颜色代码
存储猜测的颜色。然后我使用2个for
循环和if
语句
检查数字。
// initilising the 2 arrays
char code[4] = {'r', 'b', 'y', 'o', '\0'};
char guessed[4] = {'b', 'r', 'i', 'v', '\0'};
int i, j, white, black, newwhite;
// in the main
// first for will go through every letter in the color code array
for(i=0; i<4; i++){
// second for will go through every letter in the guessed color array
for(j=0; j<4; j++){
// if there is a match white + 1
if(code[i] == guessed[j]){
white++;
// this will change the found character to z
// z is a char that doesnt appear in any array
// you can use any char that is not in the array
code[j]='z';
}
}
}
要整理黑色钉子,只需使用if,然后使用白色 - 黑色来获得newwhite
计数。
我希望这可以帮助你,即使它是一种不同的方式。