Beispiel #1
0
//---------- UnCover ----------
void Mines::UnCover(tile *T) {
#ifdef DEBUG
	cout << "-- DEBUG: ENTER: Mines::UnCover(unsigned int posX, unsigned int posY, bool check) with (" << T->GetXPos() << ", " << T->GetYPos() << " --\n";
#endif
	if (T->GetState() == MARKED) {	//If field is marked as a bomb do nothing
		return;
	}

	if (T->GetState() == COVERED) {
		covered_tiles--;
		T->SetState(UNCOVERED);
	}
	if (!T->GetBomb()) {	//if the current field is not a mine
		if (T->GetValue() == 0) {	//If the current field has no value, uncover those around as well
			if (T->GetXPos() > 0 && T->GetUpper()->GetState() == COVERED) {
				//Uncovers the field above the current
				UnCover(T->GetUpper());
			}
			if (T->GetXPos() < height - 1 && T->GetLower()->GetState() == COVERED) {
				//Uncovers the field below the current
				UnCover(T->GetLower());
			}
			if (T->GetYPos() > 0 && T->GetLeft()->GetState() == COVERED) {
				//Uncovers the field left from the current
				UnCover(T->GetLeft());
			}
			if (T->GetYPos() < width - 1 && T->GetRight()->GetState() == COVERED) {
				//Uncovers the field right from the current
				UnCover(T->GetRight());
			}
			if (T->GetXPos() > 0 && T->GetYPos() > 0 && T->GetUpperLeft()->GetState() == COVERED) {
				//Uncovers the upperleft field
				UnCover(T->GetUpperLeft());
			}
			if (T->GetXPos() > 0 && T->GetYPos() < width - 1 && T->GetUpperRight()->GetState() == COVERED) {
				//Uncovers the upperright field
				UnCover(T->GetUpperRight());
			}
			if (T->GetXPos() < height - 1 && T->GetYPos() > 0 && T->GetLowerLeft()->GetState() == COVERED) {
				//Uncovers the lowerleft field
				UnCover(T->GetLowerLeft());
			}
			if (T->GetXPos() < height - 1 && T->GetYPos() < width - 1 && T->GetLowerRight()->GetState() == COVERED) {
				//UnCovers the lowerright field
				UnCover(T->GetLowerRight());
			}
		}
// 		else {
// 			T->SetState(UNCOVERED);
// 		}
	} else {
		SetGameState(FAIL);
	}
// #ifdef DEBUG
// 	else cout << "-- DEBUG: Board[" << posX << "][" << posY << "] has Value " << board[posX][posY].GetValue() << " --\n";
// #endif
#ifdef DEBUG
	cout << "-- DEBUG: EXIT:  Mines::UnCover(unsigned int posX, unsigned int posY, bool check) --\n";
#endif
}
void Search(int k) {
	/*if(GlobalProgressUpdate < k) {
	printf("== Search(%d)\n", k);
	PrintSolution();
	GlobalProgressUpdate = k;
	}*/
	if ((RootNode->Left == RootNode && RootNode->Right == RootNode) || k == (81 - MaxK)) {
		//Valid solution!
		//printf("----------- SOLUTION FOUND -----------\n");
		PrintSolution();
		Finished = 1;
		return;
	}
	struct str_node *Column = ChooseColumn();
	Cover(Column);

	struct str_node *RowNode;
	struct str_node *RightNode;
	for (RowNode = Column->Down; RowNode != Column && !Finished; RowNode = RowNode->Down) {
		// Try this row node on!
		Result[nResult++] = RowNode->IDNum;

		for (RightNode = RowNode->Right; RightNode != RowNode; RightNode = RightNode->Right)
			Cover(RightNode->Header);

		Search(k + 1);

		// Ok, that node didn't quite work
		for (RightNode = RowNode->Right; RightNode != RowNode; RightNode = RightNode->Right)
			UnCover(RightNode->Header);

		Result[--nResult] = 0;
	}

	UnCover(Column);
}
Beispiel #3
0
//---------- GameCore ----------
int Mines::GameCore() {
// 	int game_end = 0;
// 	string InPut = "";
// 	unsigned int xpos = 0, ypos = 0;
	getch InPut;
	int key = 0;
	if (!initiated) Init(STD_HEIGHT, STD_WIDTH, 0);
	while (MinesMenu()) {
		Create();
		SetGameState(CONTINUE);
		while (game_state == CONTINUE) {
			Draw();
			key = InPut.GetKey();
			if (key == UP_KEY) {
				focus.MoveUp();
			} else if (key == DOWN_KEY) {
				focus.MoveDown();
			} else if (key == RIGHT_KEY) {
				focus.MoveRight();
			} else if (key == LEFT_KEY) {
				focus.MoveLeft();
			} else if (key == OPEN_FIELD) {
				if (first_time) {
					focus.GetField()->SetState(PROTECTED);
					SetMines();
					focus.GetField()->SetState(COVERED);
					first_time = false;
				}
				UnCover(focus.GetField());
			} else if (key == MARK_FIELD) {
				MarkBomb(focus.GetField());
			} else if (key == QUIT_KEY) {
				SetGameState(MANUAL_END);
			} else {
// 				E.Error("There's no such option");
#ifdef DEBUG
				D.Debug("There's no such option");
#endif //DEBUG
			}
			CheckGameState();
		}
		if (game_state == FAIL) {
			RevealMines();
		}
		Draw();
		if (game_state == FAIL) {
			losses++;
			cout << "You've lost\n";
			Enter();
		} else if (game_state == WIN) {
			wins++;
			cout << "You've won!\n";
			Enter();
		} else if (game_state == MANUAL_END);
		else {
			cout << "Why the f*** has the game ended??\n";
			Enter();
		}
	}
	#ifdef __UNIX__
	system("clear");
	#endif //__UNIX__
	#ifdef __WINDOWS__
	system("cls");
	#endif //__WINDOWS__
	return 0;
}