/** (c) 2013 Michał (Foxbond) Chraniuk */
#include "mm.h"
mm::mm (){
windowSize = 80;
gamefieldSize = 10;
cheats = true;
}
void mm::init (void){
cls();
srand((int)time(NULL));
SetConsoleTitle(L"Mastermind Challenge");
changeFontSize(12, 16);
setWindowSize(80,30);
cursorOff();
}
void mm::drawLogo (void){
short leftMargin = (int)((windowSize-24)/2);
cout << spc(leftMargin) << box_topFrame(22) << "\n";
cout << spc(leftMargin) << box_vl();
changeColor(GREEN);
cout << " Mastermind Challenge ";
changeColor(LIGHTGRAY);
cout << box_vl() << "\n";
cout << spc(leftMargin) << box_bottomFrame(22);
}
void mm::run (void){
short lm = (int)((windowSize-24)/2);
cls();
drawLogo();
cout << "\n\n\n\n" << spc(lm) << "Nacisnij dowolny klawisz";
_getch();
menu();
}
void mm::menu (void){
short lm = (int)((windowSize-21)/2);
cls();
drawLogo();
cout << "\n\n\n";
cout << spc(lm) << box_topFrame(19) << "\n";
cout << spc(lm) << box_vl();
changeColor(GREEN);
cout << " 1. Rozpocznij gre ";
changeColor(LIGHTGRAY);
cout << box_vl() << "\n" << spc(lm) << box_vl() << spc(19) << box_vl() << "\n" << spc(lm) << box_vl();
cout << " 2. Pomoc ";
cout << box_vl() << "\n" << spc(lm) << box_vl() << spc(19) << box_vl() << "\n" << spc(lm) << box_vl();
cout << " 3. Autor ";
cout << box_vl() << "\n" << spc(lm) << box_vl() << spc(19) << box_vl() << "\n" << spc(lm) << box_vl();
changeColor(RED);
cout << " 4. Wyjscie ";
changeColor(LIGHTGRAY);
cout << box_vl() << "\n";
cout << spc(lm) << box_bottomFrame(19);
char opt = 0;
do {
opt = _getch();
}while(opt != '1' && opt != '2' && opt != '3' && opt !='4');
if (opt == '1'){
startGame();
}else if (opt == '2'){
pomoc();
}else if (opt == '3'){
autor();
}else {
halt();
}
}
void mm::pomoc (void){
short tm = (int)((windowSize-24)/2);
cls();
drawLogo();
cout << "\n\n\n\n" << spc(tm) << "Nacisnij dowolny klawisz";
_getch();
menu();
}
void mm::autor (void){
short lm = (int)((windowSize-28)/2);
short tm = (int)((windowSize-24)/2);
cls();
drawLogo();
cout << "\n\n\n";
cout << spc(lm) << box_topFrame(26) << "\n";
cout << spc(lm) << box_vl() << spc(26) << box_vl() << "\n";
cout << spc(lm) << box_vl() << " (c) 2013 Michal Chraniuk " << box_vl() << "\n";
cout << spc(lm) << box_vl() << spc(26) << box_vl() << "\n";
cout << spc(lm) << box_vl() << " http://foxbond.info " << box_vl() << "\n";
cout << spc(lm) << box_vl() << spc(26) << box_vl() << "\n";
cout << spc(lm) << box_bottomFrame(26);
cout << "\n\n\n\n" << spc(tm) << "Nacisnij dowolny klawisz";
_getch();
menu();
}
void mm::halt (void){
changeFontSize(8, 12);
setWindowSize(80,25);
cursorOn();
exit(0);
}
void mm::startGame (void){
char pAction;
prepareGamedata();
while(true){
cls();
drawLogo();
drawGamefield();
while (true){
pAction = _getch();
if (pAction == 72){
//up
changeSelectedColor(1);
break;
}else if (pAction == 80){
//down
changeSelectedColor(-1);
break;
}else if (pAction == 77){
//right
changeSelectedField(1);
break;
}else if (pAction == 75){
//left
changeSelectedField(-1);
break;
}else if (pAction == 13){
//enter
checkCode();
break;
}else if (pAction == 27){
//esc
lose();
}else {
//nothing
}
}//inputLoop
}//gameLoop
}
void mm::prepareGamedata (void){
for (short i=0;i<10;i++){
gamefieldCheck[i][0] = 0;
gamefieldCheck[i][1] = 0;
for (short ii=0;ii<4;ii++){
gamefield[i][ii] = 0;
}
}
currentMove = 0;
currSelectingField = 0;
showCode = false;
encode();
}
void mm::drawGamefield (void){
short lm = (int)((windowSize-18)/2);
char b = 219;
char dot = 254;
char upArrow = 24;
char downArrow = 25;
short _empty;
if (cheats || showCode){
cout << "\n" << spc((lm+5+2));
for (short chi=0;chi<4;chi++){
changeColor(idToColor(code[chi]));
cout << code[chi];
changeColor(LIGHTGRAY);
}
cout << "\n";
}else{
cout << "\n\n";
}
cout << spc((lm+5)) <<b<<b<<b<<b<<b<<b<<b<<b<< "\n";
for (short i=0;i<10;i++){
if (i == currentMove){
cout << spc((lm+5)) <<b<< " ";
for (short seli=0;seli<4;seli++){
if (seli == currSelectingField){
cout << downArrow;
}else{
cout << " ";
}
}
cout << " " <<b<< "\n";
}else if (i == currentMove+1){
cout << spc((lm+5)) <<b<< " ";
for (short seli=0;seli<4;seli++){
if (seli == currSelectingField){
cout << upArrow;
}else{
cout << " ";
}
}
cout << " " <<b<< "\n";
}else{
cout << spc((lm+5)) <<b<< spc(6) <<b<<"\n";
}
cout << spc(lm);
_empty = 4-gamefieldCheck[i][0];
for (short _emptyi=0;_emptyi<_empty;_emptyi++){
cout << " ";
}
cout << "<";
changeColor(WHITE);
for (short halfi=0;halfi<gamefieldCheck[i][0];halfi++){
cout << halfi+1;
}
changeColor(LIGHTGRAY);
cout << b << " ";
for (short tryi=0;tryi<4;tryi++){
if (i == currentMove){
if (gamefield[i][tryi] == 0){
changeColor(DARKGRAY);
cout << "?";
changeColor(LIGHTGRAY);
}else{
changeColor(idToColor(gamefield[i][tryi]));
cout << "?";
changeColor(LIGHTGRAY);
}
}else{
if (gamefield[i][tryi] == 0){
cout << " ";
}else{
changeColor(idToColor(gamefield[i][tryi]));
cout << b;
changeColor(LIGHTGRAY);
}
}
}
cout << " " << b;
changeColor(RED);
for (short fulli=0;fulli<gamefieldCheck[i][1];fulli++){
cout << fulli+1;
}
changeColor(LIGHTGRAY);
cout << ">";
_empty = 4-gamefieldCheck[i][1];
for (short _emptyi=0;_emptyi<_empty;_emptyi++){
cout << " ";
}
cout << "\n";
if (i==9){
if (currentMove == i){
cout << spc((lm+5)) <<b<< " ";
for (short seli=0;seli<4;seli++){
if (seli == currSelectingField){
cout << upArrow;
}else{
cout << " ";
}
}
cout << " " <<b<< "\n";
}else{
cout << spc((lm+5)) <<b<< spc(6) <<b<<"\n";
}
}
}
cout << spc((lm+5)) <<b<<b<<b<<b<<b<<b<<b<<b<< "\n";
}
short mm::idToColor (short id){
if (id == 1){
return MAGENTA;
}else if (id == 2){
return LIGHTRED;
}else if (id == 3){
return GREEN;
}else if (id == 4){
return RED;
}else if (id == 5){
return BROWN;
}else if (id == 6){
return CYAN;
}else if (id == 7){
return YELLOW;
}else if (id == 8){
return BLUE;
}else {
cout << "error";
exit(0);
}
}
void mm::changeSelectedColor (short d){
gamefield[currentMove][currSelectingField] += d;
if (gamefield[currentMove][currSelectingField] < 1){
gamefield[currentMove][currSelectingField] = 8;
}
if (gamefield[currentMove][currSelectingField] > 8){
gamefield[currentMove][currSelectingField] = 1;
}
}
void mm::changeSelectedField (short d){
currSelectingField += d;
if (currSelectingField < 0){
currSelectingField = 3;
}
if (currSelectingField > 3){
currSelectingField = 0;
}
}
void mm::encode (void){
short chcki = 0;
for (short i=0;i<4;i++){
code[i] = (rand()%8)+1;
for (chcki = (i-1);chcki >= 0;chcki--){
if (code[chcki] == code[i]){
code[i] = (rand()%8)+1;
chcki = i-1;
}
}
}
}
void mm::checkCode (void){
for (short i=0;i<4;i++){
if (gamefield[currentMove][i] == 0){
return;
}
}
for (short i=0;i<4;i++){
if (code[i] == gamefield[currentMove][i]){
gamefieldCheck[currentMove][1]++;
continue;
}
for (short hi=0;hi<4;hi++){
if (hi == i){
continue;
}
if (code[i] == gamefield[currentMove][hi]){
gamefieldCheck[currentMove][0]++;
continue;
}
}
}
/** old
for (short i=0;i<4;i++){
if (gamefield[currentMove][i] == code[i]){
gamefieldCheck[currentMove][1]++;
}else{
for (short hi=0;hi<4;hi++){
if (hi == i){
continue;
}
if (gamefield[currentMove][i] == code[hi]){
gamefieldCheck[currentMove][0]++;
continue;
}
}
}
}*/
if (gamefieldCheck[currentMove][1] == 4){
//win
win();
}
if (currentMove == 9){
//lose
lose();
}
currentMove++;
}
void mm::win (void){
showCode = true;
short lm = (int)((windowSize-9)/2);
cls();
drawLogo();
drawGamefield();
changeColor(GREEN);
cout << spc(lm) << "Wygrales!";
changeColor(LIGHTGRAY);
_getch();
menu();
}
void mm::lose (void){
showCode = true;
short lm = (int)((windowSize-11)/2);
cls();
drawLogo();
drawGamefield();
changeColor(RED);
cout << spc(lm) << "Przegrales!";
changeColor(LIGHTGRAY);
_getch();
menu();
}
> Back