Soldato
- Joined
- 15 Feb 2011
- Posts
- 10,234
- Location
- Slough
*edit*
problem is all fixed now. feel free to delete this mods
first, i really want to make it clear how much of a noob to programming i am. i only started programming in C in October, and moved on to C++ in February. right now the limit of complexity for me is classes and objects.
what i need the program to do is play a simple text based version of the classic game mastermind. when i had the program as one giant main function with no classes or functions, everything was fine. however, now i have put most of the functionality into classes i have the following two problems:
- the variable that stores the number of pegs in of the correct colour, but in the wrong place is stuck at zero, no matter how many pegs of the right colour are in the wrong place.
*edit*
fixed the overwriting problem. i made the 2D array's that store the guesses and the results too small
(insert blushing smiley here)
if anyone can explain in morons terms how to fix these problems that would be fantastic. ive attached the code at the bottom. please dont laugh, i know its pretty rubbish, but its all i know so far:
(using visual C++ 2010 express)
header file: mastermind class.h
class functions file: mastermind class.cpp
main file: mastermind.cpp
problem is all fixed now. feel free to delete this mods
first, i really want to make it clear how much of a noob to programming i am. i only started programming in C in October, and moved on to C++ in February. right now the limit of complexity for me is classes and objects.
what i need the program to do is play a simple text based version of the classic game mastermind. when i had the program as one giant main function with no classes or functions, everything was fine. however, now i have put most of the functionality into classes i have the following two problems:
- the variable that stores the number of pegs in of the correct colour, but in the wrong place is stuck at zero, no matter how many pegs of the right colour are in the wrong place.
*edit*
fixed the overwriting problem. i made the 2D array's that store the guesses and the results too small
(insert blushing smiley here)
if anyone can explain in morons terms how to fix these problems that would be fantastic. ive attached the code at the bottom. please dont laugh, i know its pretty rubbish, but its all i know so far:
(using visual C++ 2010 express)
header file: mastermind class.h
Code:
#include <iostream>
using namespace std;
class mastermind
{
public:
//all functions
void set_random_answer_code();
void set_custom_answer_code();
void reset_variables(int guess_number);
void print_previous_results(int guess_number);
void enter_current_guess(int guess_number);
void calculate_current_result(int guess_number);
int game_won_check(int guess_number);
void print_answer();
private:
//all variables
//counters
int c1;
int c2;
int c3;
//sentinals for checking right colour, right place, and right colour, wrong place
int sentinal_g1;
int sentinal_g2;
int sentinal_g3;
int sentinal_g4;
int sentinal_a1;
int sentinal_a2;
int sentinal_a3;
int sentinal_a4;
int result_array_all[15][2];
char guess_array_all[15][4];
//guess arrays
char answer_array[4];
//temporary variables
int temp1;
protected:
};
class functions file: mastermind class.cpp
Code:
#include <iostream>
#include <ctime>
#include "mastermind class.h"
using namespace std;
void mastermind::set_random_answer_code()
{
for (c1 = 0; c1 < 4; c1++)
{
//seed random number
int t;
t = time (NULL);
srand (t);
temp1 = (rand() %6); //remainder when divided by 6
switch (temp1)
{
case 0: answer_array[c1] = 'R'; break;
case 1: answer_array[c1] = 'G'; break;
case 2: answer_array[c1] = 'B'; break;
case 3: answer_array[c1] = 'Y'; break;
case 4: answer_array[c1] = 'O'; break;
case 5: answer_array[c1] = 'P'; break;
default: answer_array[c1] = 'R'; break;
}
}
}
void mastermind::set_custom_answer_code()
{
cout << "\nenter the colours of the four pegs your opponent will be guessing\n R = Red\n G = Green\n B = Blue\n Y = Yellow\n O = Orange\n P = Purple\n" <<endl;
//need error checking!!!
cin >> answer_array[0] >> answer_array[1] >> answer_array[2] >> answer_array[3];
}
void mastermind::reset_variables(int guess_number)
{
//sentinals used in checking your current guess
int sentinal_g1 = 0;
int sentinal_g2 = 0;
int sentinal_g3 = 0;
int sentinal_g4 = 0;
int sentinal_a1 = 0;
int sentinal_a2 = 0;
int sentinal_a3 = 0;
int sentinal_a4 = 0;
result_array_all[guess_number][0] = 0;
result_array_all[guess_number][1] = 0;
}
void mastermind::print_previous_results(int guess_number)
{
for(c3 = 0; c3 < guess_number; c3++)
{
cout << (c3 + 1) << ": " << guess_array_all[c3][0] << ", " << guess_array_all[c3][1] << ", ";
cout << guess_array_all[c3][2] << ", " << guess_array_all[c3][3] << ", ";
cout << result_array_all[c3][0] << ", " << result_array_all[c3][1] << endl;
}
}
void mastermind::enter_current_guess(int guess_number)
{
cout << "enter guess" << endl;
cin >> guess_array_all[guess_number][0] >> guess_array_all[guess_number][1] >> guess_array_all[guess_number][2] >> guess_array_all[guess_number][3];
}
void mastermind::calculate_current_result(int guess_number)
{
reset_variables(guess_number);
//calculate correct colour and correct place
//result_array_all[x][0] == correct colour and correct place variable
if(guess_array_all[guess_number][0] == answer_array[0])
{
result_array_all[guess_number][0] += 1;
sentinal_g1 ++;
sentinal_a1 ++;
}
if(guess_array_all[guess_number][1] == answer_array[1])
{
result_array_all[guess_number][0] += 1;
sentinal_g2 ++;
sentinal_a2 ++;
}
if(guess_array_all[guess_number][2] == answer_array[2])
{
result_array_all[guess_number][0] += 1;
sentinal_g3 ++;
sentinal_a3 ++;
}
if(guess_array_all[guess_number][3] == answer_array[3])
{
result_array_all[guess_number][0] += 1;
sentinal_g4 ++;
sentinal_a4 ++;
}
//calculate correct colour wrong place
//result_array_all[x][1] == correct colour but wrong place variable
//character 1
if(guess_array_all[guess_number][0] == answer_array[1] && sentinal_g1 == 0 && sentinal_a2 == 0)
{
result_array_all[guess_number][1] += 1;
sentinal_g1 ++;
sentinal_a2 ++;
}
if(guess_array_all[guess_number][0] == answer_array[2] && sentinal_g1 == 0 && sentinal_a3 == 0)
{
result_array_all[guess_number][1] += 1;
sentinal_g1 ++;
sentinal_a3 ++;
}
if(guess_array_all[guess_number][0] == answer_array[3] && sentinal_g1 == 0 && sentinal_a4 == 0)
{
result_array_all[guess_number][1] += 1;
sentinal_g1 ++;
sentinal_a4 ++;
}
//character 2
if(guess_array_all[guess_number][1] == answer_array[0] && sentinal_g2 == 0 && sentinal_a1 == 0)
{
result_array_all[guess_number][1] += 1;
sentinal_g2 ++;
sentinal_a1 ++;
}
if(guess_array_all[guess_number][1] == answer_array[2] && sentinal_g2 == 0 && sentinal_a3 == 0)
{
result_array_all[guess_number][1] += 1;
sentinal_g2 ++;
sentinal_a3 ++;
}
if(guess_array_all[guess_number][1] == answer_array[3] && sentinal_g2 == 0 && sentinal_a4 == 0)
{
result_array_all[guess_number][1] += 1;
sentinal_g2 ++;
sentinal_a4 ++;
}
//character 3
if(guess_array_all[guess_number][2] == answer_array[0] && sentinal_g3 == 0 && sentinal_a1 == 0)
{
result_array_all[guess_number][1] += 1;
sentinal_g3 ++;
sentinal_a1 ++;
}
if(guess_array_all[guess_number][2] == answer_array[1] && sentinal_g3 == 0 && sentinal_a2 == 0)
{
result_array_all[guess_number][1] += 1;
sentinal_g3 ++;
sentinal_a2 ++;
}
if(guess_array_all[guess_number][2] == answer_array[3] && sentinal_g3 == 0 && sentinal_a4 == 0)
{
result_array_all[guess_number][1] += 1;
sentinal_g3 ++;
sentinal_a4 ++;
}
//character 4
if(guess_array_all[guess_number][3] == answer_array[0] && sentinal_g4 == 0 && sentinal_a1 == 0)
{
result_array_all[guess_number][1] += 1;
sentinal_g4 ++;
sentinal_a1 ++;
}
if(guess_array_all[guess_number][3] == answer_array[1] && sentinal_g4 == 0 && sentinal_a2 == 0)
{
result_array_all[guess_number][1] += 1;
sentinal_g4 ++;
sentinal_a2 ++;
}
if(guess_array_all[guess_number][3] == answer_array[2] && sentinal_g4 == 0 && sentinal_a3 == 0)
{
result_array_all[guess_number][1] += 1;
sentinal_g4 ++;
sentinal_a3 ++;
}
}
int mastermind::game_won_check(int guess_number)
{
if (result_array_all[guess_number][0] == 4)
{
return 1;
}else
{
return 0;
}
}
void mastermind::print_answer()
{
for (c2 = 0; c2 < 4; c2++)
{
cout << answer_array[c2] << ", ";
}
}
main file: mastermind.cpp
Code:
#include <iostream>
#include <ctime>
#include <stdlib.h>
#include "mastermind class.h"
using namespace std;
int main ()
{
int game_won_sentinal = 0;
int guess_number;
int noofplayers;
int lives;
mastermind game;
cout << "MASTERMIND" << endl;
//choose number of players
cout << "for single player, enter 1 \nfor two player, enter 2" << endl;
cin >> noofplayers;
cout << endl;
//colours: R = Red; G = Green; B = Blue; Y = Yellow; O = Orange; P = Purple
if (noofplayers == 1)
{
game.set_random_answer_code();
} else
if (noofplayers == 2)
{
game.set_custom_answer_code();
}
guess_number = -1;
lives = 15;
for (lives = 15; lives >= 0; lives --)
{
//clear screen
system("cls");
//print introduction and instructions
guess_number ++;
//reset variables
game.reset_variables(guess_number);
//game description
cout << "MASTERMIND\n" << endl;
cout << "A combination of four colours has been chosen from the colours below." << endl;
cout << "You must guess the combination your opponent has chosen within 15 tries" << endl;
cout << "The number under 'pl' is the number of pegs that are the\ncorrect colour and in the correct place" << endl;
cout << "The number under 'cl' is the number of pegs that are the\ncorrect colour but are in the wrong place" << endl;
cout << "\nThe colours available are:\n R = Red\n G = Green\n B = Blue\n Y = Yellow\n O = Orange\n P = Purple\n" <<endl;
//print format of how results are displayed
cout << "g# c1 c2 c3 c4 pl cl" << endl;
//print all previous results (with all array)
game.print_previous_results(guess_number);
//enter current guess
game.enter_current_guess(guess_number);
game.calculate_current_result(guess_number);
//game won check
game_won_sentinal = game.game_won_check(guess_number);
if (game_won_sentinal == 1)
{
lives = 0;
}
}// end of loop for playing the game
//game won
if (game_won_sentinal == 1)
{
cout << "congratulations, you won" << endl;
cout << "the correct answer was: " << endl;
game.print_answer();
}
//game lost
if (game_won_sentinal != 1)
{
cout << "sorry, you lost" << endl;
cout << "the correct answer was: ";
game.print_answer();
}
return 0;
}
Last edited: