Skip to content

Instantly share code, notes, and snippets.

@wolfposd
Last active January 2, 2016 14:49
Show Gist options
  • Save wolfposd/8319046 to your computer and use it in GitHub Desktop.
Save wolfposd/8319046 to your computer and use it in GitHub Desktop.
C++ Console Memory-Game
// Copyright (c) 2014, Wolf Posdorfer
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <iostream>
#include <vector>
#include <gpa/input>
#include <algorithm>
const int FIELDSIZE = 6;
int aktuellerspieler = 0;
struct spielertyp {
int nummer;
std::string name;
int punkte;
};
bool operator<(const spielertyp& a, const spielertyp& b)
{
return a.punkte < b.punkte;
}
std::vector<spielertyp> spieler;
std::vector<std::vector<int>> memoryfeld(FIELDSIZE,std::vector<int>(FIELDSIZE));
void spielerabfrage()
{
std::string name = gpa::read_string("Spielername: ");
int id = 0;
while(name != "")
{
spielertyp t = {id,name,0};
spieler.push_back(t); //emplace_back(id,name);
id++;
name = gpa::read_string("Spielername: ");
}
std::cout << "Folgende Spieler sind dabei:\n";
for(spielertyp t : spieler)
{
std::cout << t.name << "\n";
}
}
void initfeld()
{
std::vector<int> werte;
for(int i = 0; i < FIELDSIZE*FIELDSIZE/2; i++)
{
werte.push_back('A'+i);
werte.push_back('A'+i);
}
std::random_shuffle(werte.begin(), werte.end());
for(int i =0 ; i < FIELDSIZE; i++)
{
for(int j = 0; j < FIELDSIZE; j++)
{
memoryfeld[i][j] = werte.back();
werte.pop_back();
}
}
}
void printFeld(int x1, int y1, int x2, int y2)
{
std::cout << " ";
for(int i = 0; i < FIELDSIZE ; i++)
{
std::cout << i << " ";
}
std::cout << "\n";
for(int i =0 ; i < FIELDSIZE; i++)
{
std::cout << i << " ";
for(int j = 0; j < FIELDSIZE; j++)
{
if((i == x1 && j == y1 )|| (i == x2 && j == y2) || memoryfeld[i][j] == ' ')
{
std::cout << (char)memoryfeld[i][j] << " ";
}
else
{
std::cout << "∏" << " ";
}
}
std::cout << "\n";
}
}
void printFeld()
{
printFeld(-1,-1,-1,-1);
}
void wechselSpieler()
{
aktuellerspieler = (aktuellerspieler + 1) % spieler.size();
}
void frageNachPosition()
{
spielertyp& current = spieler[aktuellerspieler];
std::cout<< "Spieler: " << current.name << "(" << current.punkte << ")"<< " ist dran\n";
int x1 = gpa::read_int("X: ");
int y1 = gpa::read_int("Y: ");
int x2 = gpa::read_int("Nächster Stein\nX: ");
int y2 = gpa::read_int("Y: ");
printFeld(x1, y1, x2, y2);
if(memoryfeld[x1][y1] == memoryfeld[x2][y2] && memoryfeld[x1][y1] != ' ')
{
current.punkte += 1;
memoryfeld[x1][y1] = ' ';
memoryfeld[x2][y2] = ' ';
std::cout<< "Du bist nochmal dran\n";
}
else
{
wechselSpieler();
}
}
bool hat_noch_karten()
{
for(int i = 0 ; i < FIELDSIZE; i++)
{
for(int j = 0; j < FIELDSIZE; j++)
{
if(memoryfeld[i][j] != ' ')
return true;
}
}
return false;
}
void print_platzierung()
{
std::sort(spieler.begin(), spieler.end());
std::cout << "Gewinner ist " << spieler[0].name << " mit " << spieler[0].punkte << " Punkten!" << std::endl;
for (auto it = spieler.begin()+1; it < spieler.end(); ++it)
{
std::cout << (it - spieler.begin())-1 << ". Platz: " << it->name << " mit " << it->punkte << " Punkten." << std::endl;
}
}
int main(int argc, const char * argv[])
{
spielerabfrage();
initfeld();
printFeld();
while(hat_noch_karten())
{
frageNachPosition();
}
print_platzierung();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment