C++ Mastermind Help

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...

._. code below


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  //
//  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>
using namespace 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;
                }
                else if(g1==c2||g1==c3||g1==c4){
                    totalwrong=totalwrong+1;
                }
                if(g2==c2){
                    total=total+1;
                }
                else if(g2==c1||g2==c3||g2==c4){
                    totalwrong=totalwrong+1;
                }
                if(g3==c3){
                    total=total+1;
                }
                else if(g3==c1||g3==c2||g3==c4){
                    totalwrong=totalwrong+1;
                }
                if(g4==c4){
                    total=total+1;
                }
                else if(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){
                        
                    }
                }
                
            } else if(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!";
                    } else if(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.
closed account (iAk3T05o)
Turn the menu into a function, remove the goto's and loop the whole program
to make it dynamic you are going to have to replace these

1
2
int c1, c2, c3, c4;
int g1, g2, g3, g4;


with something like this:

1
2
int c[max_slots];
int g[max_slots];


and update your checks accordingly.

in my opinion you should also call them "color" and "guess" or some other longer meaningful name.
Last edited on
replace them with vectors, not horrible c arrays.

And like nathan said, get rid of the goto's.

And i would completely forget about implementing an AI for now. Concentrate on the dynamic slot stuff first.
Last edited on
// 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.
Last edited on
@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.
Oh, I thought you were making a joke about how companies put stuff like this in contexts where it clearly doesn't belong.

Jaybob asked you to change the variable names for our benefit as much as yours. It makes the code easier to read.
Last edited on
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
Topic archived. No new replies allowed.