Witam serdecznie, mam problem z kodem gry w wisielca.
Działa ten program tak:
- Jest to gra dla dwoch osob
- Pierwsza osoba wybiera opcje 1 z menu i wpisuje slowo które potem bedzie zgadywane
- Druga osoba potem wybiera opcje 2 i podaje pierwsza literke
- Osoba znowu wybiera opcje 2 i podaje nastepna literke
I tak w kółko aż zgadnie. Problem polega na tym że czasami ten program działa, a czasami po podaniu drugiej literki wpada w jakąś pętle i nic się nie da zrobić, trzeba zamknąć okno.
Byłbym niezmiernie wdzięczny jeśli ktoś mógłby rzucić okiem na ten kod i powiedzieć czemu tak się dzieje:
// Skeleton Program code for the AQA COMP1 Summer 2009 examination
// this code should be used in conjunction with the Preliminary Materials
// written by the AQA COMP1 Programmer Team
// developed using Microsoft Visual C++ 2008 Express Edition
// the DisplayMenu procedure has deliberately omitted a menu choice 3 and 4
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <iostream>
using namespace std;
char NewPhrase[22]; //since C terminates strings with characters /0
int Choice;
int PhraseHasBeenSet; // C has no Boolean type - code 'True' as 1 / 'False' as o
int PhraseGuessed; // C has no Boolean type
char GuessStatusArray[22];
char LettersGuessedArray[27];
char NextGuessedLetter;
int Index;
void DisplayMenu();
char* GetNewPhrase();
void SetUpGuessStatusArray(char NewPhrase[22], char GuessStatusArray[20]);
void DisplayCurrentStatus(int PhraseLength, char GuessStatusArray[20]);
char GetNextLetterGuess();
int AllLettersGuessedCorrectly(char GuessArrayStatus[22],char NewPhrase[22]);
void main(void){
PhraseHasBeenSet = 0;
do {
DisplayMenu();
cout << "Choice? ";
_flushall();
cin >> Choice;
if(Choice ==1 ){
strcpy(NewPhrase, GetNewPhrase()); // cannot copy or move strings directly ...
SetUpGuessStatusArray(NewPhrase,GuessStatusArray);
PhraseHasBeenSet = 1;
}
if(Choice == 2) {
if(PhraseHasBeenSet ) { // no need for PhraseHasBeenSet == 1
DisplayCurrentStatus(strlen(NewPhrase),GuessStatusArray);
NextGuessedLetter = GetNextLetterGuess();
for(Index=0; Index<strlen(NewPhrase); Index++){
if(NextGuessedLetter == NewPhrase[Index]){
GuessStatusArray[Index] = NextGuessedLetter;
}
}
DisplayCurrentStatus(strlen(NewPhrase),GuessStatusArray);
PhraseGuessed = AllLettersGuessedCorrectly(GuessStatusArray,NewPhrase);
if(PhraseGuessed){
cout << "You have guessed correctly" << endl;
}
}
else {
cout << "The setter has not specified the word/phrase .." << endl;
}
}
if((Choice == 5)&&(PhraseGuessed == 0)){
cout << "You have not completed this word/phrase...Press Return to exit" << endl;
_getch();
}
}while (Choice !=5);
_getch();
}
void DisplayMenu(){
cout << "_________________________________";
cout << endl;
cout << "1. SETTER - Makes new word/phrase";
cout << endl;
cout << "2. USER - Next letter guess";
cout << endl;
cout << "5. End" ;
cout << endl;
}
char* GetNewPhrase(){
int PhraseOK;
char ThisNewPhrase[53];
do {
cout<<"Key in new word ...(letters and any Spaces)";
_flushall();
gets_s(ThisNewPhrase);
if(strlen(ThisNewPhrase)<10) {
PhraseOK = 0;
cout<<"Not enough letters ... " << endl;
// possible further validation ckeck(s)
}
else {
PhraseOK = 1;
}
}while(PhraseOK ==0);
return ThisNewPhrase;
}
void SetUpGuessStatusArray(char NewPhrase[22], char GuessStatusArray[22]){
int Position;
for(Position=0; Position<strlen(NewPhrase); Position++){
if(NewPhrase[Position] == ' '){
GuessStatusArray[Position]= ' ';
}
else {
GuessStatusArray[Position]= '*';
}
}
}
void DisplayCurrentStatus(int PhraseLength, char GuessStatusArray[22]){
int Position;
for(Position = 0; Position <PhraseLength; Position++){
cout << GuessStatusArray[Position];
}
cout << endl;
}
char GetNextLetterGuess(){
char GuessedLetter;
cout << "Next guess? ";
_flushall();
cin >> GuessedLetter;
return GuessedLetter;
}
int AllLettersGuessedCorrectly(char GuessArrayStatus[22],char NewPhrase[22]){
int Position;
int MissingLetter; // C has no Boolean type !
MissingLetter = 0;
Position = 0;
do{
if(GuessArrayStatus[Position] != NewPhrase[Position]){
MissingLetter = 1;
}
else {
Position = Position++;
}
}while((MissingLetter == 0) && (Position < strlen(NewPhrase)));
return !MissingLetter; // the ! reverses logic of MissingLetter to
// return a valid result
}
Pozdrawiam serdecznie,
Moa.