Teacher gave us a project that I just cannot understand. Our task is to write a program for the game Mastermind and in the game normally there are just four spots that you have to fill in. (example: Black, Green, Blue, Yellow would be a guess). However, my teacher wants us to make the number of slots dynamic so the player can choose how many slots for colors there should be. On top of that she also wants us to produce an AI to guess a player's number intelligently (using the player's feedback of how many are right and now many are right but in the wrong spot to make the next guess)
Here is what I have so far and it works for a set number of slots (customary 4) but Idk how to make that dynamic and have absolutely NO idea of how to make the AI. Please help...
//
// main.cpp
// Mastermind
//
// Created by Gregory Forbes on 1/12/14.
// Copyright (c) 2014 Gregory Forbes. All rights reserved.
//
#include <iostream>
#include <vector>
#include <sstream>
#include <stack>
#include <stdlib.h>
#include <stdio.h>
usingnamespace std;
int main(){
int choose;
int choose2;
int c1, c2, c3, c4;
int g1, g2, g3, g4;
//int D;
//bool menu = true;
MenuOne:
cout << "Welcome to MASTERMIND!" << endl;
cout << "[1] Play" << endl;
cout << "[2] Instructions" << endl;
cout << "[3] Exit" << endl; //idk if I'll use this
cin >> choose;
switch(choose){
case 1:
cout << "Choose your game mode:\n [1] AI is the Master.\n [2] You are the Master\n";
cin >> choose2;
if (choose2==1){
//cout << "How many colors?";
//cin >> D;
c1=rand()&4;
c2=rand()&4;
c3=rand()&4;
c4=rand()&4;
Guessing:
int total = 0;
int totalwrong = 0;
int end;
cout << "Enter your guess: ";
cin >> g1;
cout << "Enter your guess: ";
cin >> g2;
cout << "Enter your guess: ";
cin >> g3;
cout << "Enter your guess: ";
cin >> g4;
if(g1==c1){
total=total+1;
}
elseif(g1==c2||g1==c3||g1==c4){
totalwrong=totalwrong+1;
}
if(g2==c2){
total=total+1;
}
elseif(g2==c1||g2==c3||g2==c4){
totalwrong=totalwrong+1;
}
if(g3==c3){
total=total+1;
}
elseif(g3==c1||g3==c2||g3==c4){
totalwrong=totalwrong+1;
}
if(g4==c4){
total=total+1;
}
elseif(g4==c1||g4==c2||g4==c3){
totalwrong=totalwrong+1;
}
cout << "Number in the right place: "<< total <<" and Number right but in the wrong place: \n"<< totalwrong << ".";
if(total!=4){
goto Guessing;
} else {
cout << "You win!";
cout << "Press any key to continue...";
cin >> end;
if(end==1){
}
}
} elseif(choose2==2){
char correct;
int numberRight, numberWrong;
//to be added
cout << "choose your colors!";
cout << endl << "Type '1' to continue.";
cin >> c1;
if(c1==1){
compguess: g1=rand()%4;
g2=rand()%4;
g3=rand()%4;
g4=rand()%4;
cout << "Computer's guess is " << g1 << " " << g2 << " " << g3 << " " << g4 << "." << endl;
cout << "Is this correct? Y/N ";
cin >> correct;
if(correct=='Y'){
cout << "comp wins!";
} elseif(correct=='N'){
cout << "How many are right and in the right place? ";
cin >> numberRight;
cout << "How many are right but in the wrong place? ";
cin >> numberWrong;
goto compguess;
}
}
else {
goto compguess;
}
}
case 2:
cout << "How to Play\n";
cout << "MASTERMIND is a game about colors and concentration.\n";
cout << "A number of Colors will be chosen at random and put in a random order.\n";
cout << "The number of colors will be told to you and it will be your job to guess the order.\n";
cout << "The colors are BLACK[K] RED[R] GREEN[G] and ORANGE[O].\n";
// cin >> " ";
goto MenuOne;
case'3':
cout << "Goodbye.\n";
return 0;
}
}
For the dynamic bit I wasn't sure if I should use a stack or a vector. My teacher said something about arrays but idk... please help.
// Copyright (c) 2014 Gregory Forbes. All rights reserved.
And yet here you are publishing it in public domain. Thanks for the laugh OP.
Anyway, Vectors or any of the other ready to use dynamic arrays from the STL are a good idea to simplify the code if you can get away with it. Some instructors are jerks and will want you to use some specific method instead so it might be worth the time to write an email and ask about this.
@Nathan: I will do that
@Jaybob: Was thinking more vectors but I think changing the names would help...
@mutexe: I was thinking about using vectors but I have no idea how to implement them (They've confused me and I'm reading up on them now)
@Compgeek: I just copied everything and pasted it. Don't really care about that since I'm sure no one will copy-paste this horrid code.
If you want the computer to create a color arrangement for you to guess. You don't need AI, just random draws will work. However if you want the computer to try and logically solve your color picks then you need to choose a stratedgy for the computer to follow. I suggest you think about how you try to slove the game and recreate your own thought process for the computer to follow.
I once made this game in javascript and was recently considering making it in C++ myself. But I had never considered making it dynamic before. That is interesting. At least your teacher is going easy on you guys. :P