Ruch komputera - pętla while działa w nieskończoność

0

Witam
Mam problem z pętlą. Pętla wszystko fajnie wykonuje, jednak problem pojawia się gdy brakuje już punktów. Kręci pętlę w nieskończoność (w przypadku jeśli napiszę cout dla sprawdzenia) w innym wypadku nic nie robi. Pętla nie chce wyjść.

void PC(){
srand(time(NULL));
bool czyZnalazlesPunkt = false;
    while(czyZnalazlesPunkt != true){
        int i = rand()%8;
        cout << i << endl;
        if(((tablica[i]=='1') | (tablica[i]=='2') | (tablica[i]=='3')| (tablica[i]=='4') | (tablica[i]=='5') | (tablica[i]=='6') | (tablica[i]=='7') | (tablica[i]=='8') | (tablica[i]=='9'))){
            tablica[i] = znakPC;
            czyZnalazlesPunkt = true;
    }
        }
}
0

A co innego ma zrobić, jeśli tablica[i]>='1' && tablica[i]<='9'?

0

Nie kumam troszkę. Gdy nie ma punktu do szukania on nadal go szuka

0

Bo dokładnie tak to zaprogramowałeś.

0

Zatem brakuje mi odpowiedniego warunku w pętli ?

0

Pewnie tak. Powiedziałbym także, że możliwe wykonywanie kodu w nieskończoność to nie jest najlepsza opcja z możliwych.

0

Próbowałem pisać bez nieskończoności, ale nie wychodziło... zawsze problem był jak natykał na siebie albo mój znak i pomijał ruch

0
#include <iostream>
#include <string>
#include <random>
#include <vector>
#include <cctype>

template<typename RNG>
std::size_t random_digit_index(RNG &rng, std::string const &src) {
	std::vector<std::size_t> idxs;
	for(auto i = 0u; i < src.size(); ++i) {
		if(isdigit(src[i])) {
			idxs.emplace_back(i);
		}
	}
	
	if(idxs.empty()) {
		return std::string::npos;
	}
	
	return idxs[std::uniform_int_distribution<>{0, idxs.size()-1}(rng)];
}

int main() {
	std::default_random_engine gen;
	std::string string = "1234567890";
	
	auto idx = 0u;
	while((idx = random_digit_index(gen, string)) != std::string::npos){
		string[idx] = 'x';
		std::cout << "idx: " << idx << ", str: " << string << std::endl;
	}
	return 0;
}

http://ideone.com/u24IIW

idx: 0, str: x234567890
idx: 2, str: x2x4567890
idx: 8, str: x2x45678x0
idx: 5, str: x2x45x78x0
idx: 6, str: x2x45xx8x0
idx: 3, str: x2xx5xx8x0
idx: 1, str: xxxx5xx8x0
idx: 9, str: xxxx5xx8xx
idx: 7, str: xxxx5xxxxx
idx: 4, str: xxxxxxxxxx

1 użytkowników online, w tym zalogowanych: 0, gości: 1