Number Crush  1.0
..
game.cpp
1 #include <iostream>
2 
3 #include "game.h"
4 #include "Correc_prof/params.h"
6 
7 #include <map>
8 using namespace std;
9 template <class T, class U>
10 void ShowMap (const map<T,U> & AMap) {
11  for (const pair <T,U> & Val : AMap) cout << "Cle : " << Val.first << " " << "Valeur : " << Val.second << endl;
12  cout << endl;
13 }
14 
15 
16 using namespace std;
17 void Menu (CPosition & Pos, char & Direction, const CMyParam & Params)
18 {
19  ShowMap (Params.MapParamChar);
20  cout << "Saisir un indice de ligne, un indice de column,\n"
21  << " et un sens de Deplacements : \n"
22  << Params.MapParamChar.find("KeyLeft")->second << " a gauche,\n "
23  << Params.MapParamChar.find("KeyDown")->second << " en bas,\n"
24  << Params.MapParamChar.find("KeyRight")->second << " a droite,\n "
25  << Params.MapParamChar.find("KeyUp")->second << " en haut\n"
26  << endl;
27  cin >> Pos.first >> Pos.second >> Direction;
28 
29 } // Menu()
30 
31 void MakeAMove (CMat & Grid, const CPosition & Pos, const char & Direction, const CMyParam & Params)
32 {
33  unsigned NewLineNumber (Pos.first), NewColumnNumber (Pos.second);
34  if (Direction == Params.MapParamChar.find("KeyLeft")->second )
35  --NewColumnNumber;
36  else if (Direction == Params.MapParamChar.find("KeyDown")->second)
37  ++NewLineNumber;
38  else if (Direction == Params.MapParamChar.find("KeyRight")->second)
39  ++NewColumnNumber;
40  else if (Direction == Params.MapParamChar.find("KeyUp")->second)
41  --NewLineNumber;
42  else
43  exit (EXIT_FAILURE);
44  swap (Grid [Pos.first][Pos.second], Grid [NewLineNumber][NewColumnNumber]);
45 } // MakeAMove ()
46 
47 bool AtLeastThreeInARow (const CMat & Mat, CPosition & Pos, unsigned & HowMany)
48 {
49  for (unsigned LineNumber = 0; LineNumber < Mat.size (); ++LineNumber)
50  {
51  for (unsigned ColNmber = 0; ColNmber < Mat.size () -2; ++ColNmber)
52  {
53  if (KImpossible == Mat[LineNumber][ColNmber]) continue;
54  for (HowMany = 1; ColNmber + HowMany < Mat.size() && Mat [LineNumber][ColNmber] == Mat[LineNumber][ColNmber + HowMany]; ++HowMany);
55  if (HowMany > 2)
56  {
57  Pos = make_pair (LineNumber, ColNmber);
58  return true;
59  }
60  }
61  }
62  return false;
63 } // AtLeastThreeInARow ()
64 
65 bool AtLeastThreeInAColumn (const CMat & Mat, CPosition & Pos, unsigned & HowMany)
66 {
67  for (unsigned ColNmber = 0; ColNmber < Mat.size (); ++ColNmber)
68  {
69  for (unsigned LineNumber = 0; LineNumber < Mat.size () -2; ++LineNumber)
70  {
71  if (KImpossible == Mat[LineNumber][ColNmber]) continue;
72  for (HowMany = 1; LineNumber + HowMany < Mat.size() && Mat [LineNumber][ColNmber] == Mat[LineNumber + HowMany][ColNmber]; ++HowMany);
73 
74  if (HowMany > 2)
75  {
76  Pos = make_pair (LineNumber, ColNmber);
77  return true;
78  }
79  }
80  }
81  return false;
82 } // AtLeastThreeInAColumn ()
83 
84 void RemovalInColumn (CMat & Mat, const CPosition & Pos, const unsigned & HowMany)
85 {
86  unsigned Nb (0);
87  for (; Pos.first + Nb + HowMany < Mat.size (); ++Nb)
88  {
89  Mat [Pos.first + Nb][Pos.second] = Mat [Pos.first + Nb + HowMany][Pos.second];
90  Mat [Pos.first + Nb + HowMany][Pos.second] = KImpossible;
91  }
92  for (; Pos.first + Nb < Mat.size (); ++Nb)
93  Mat [Pos.first + Nb][Pos.second] = KImpossible;
94 } // ReplaceColumn()
95 
96 void RemovalInRow (CMat & Mat, const CPosition & Pos, const unsigned & HowMany)
97 {
98  CPosition PosToRemove (Pos);
99  for (unsigned i (0); i < HowMany; ++i)
100  {
101  RemovalInColumn (Mat, PosToRemove, 1);
102  ++PosToRemove.second;
103  }
104 } // ReplaceRow ()
105 
106 unsigned ComputeScore (const unsigned & HowMany)
107 {
108  return HowMany * (HowMany + 1) / 2;
109 }
110 
111 int ppal ()
112 {
113  srand (time (NULL));
114  cout << "Welcome to number crush!" << endl;
115 
116  CMyParam Params;
117  InitParams(Params);
118  LoadParams (Params);
119 
120  CMat Grid;
121  InitGrid (Grid, 10);
122  CPosition Pos;
123  unsigned HowMany, Score (0);
124  char Dirrection;
125 
126  for (unsigned Count (0); Count < KMaxTimes; ++Count)
127  {
128  DisplayGrid (Grid, Params);
129  Menu (Pos, Dirrection, Params);
130  if ('q' == Dirrection) break;
131  MakeAMove (Grid, Pos, Dirrection, Params);
132 
133  while (AtLeastThreeInAColumn (Grid, Pos, HowMany))
134  {
135  RemovalInColumn (Grid, Pos, HowMany);
136  Score += ComputeScore (HowMany);
137  }
138  while (AtLeastThreeInARow (Grid, Pos, HowMany))
139  {
140  RemovalInRow (Grid, Pos, HowMany);
141  Score += ComputeScore (HowMany);
142  }
143  }
144  cout << "Your score is : " << Score << endl << "That's not so bad for a dummy ;)" << endl;
145  return 0;
146 }
bool AtLeastThreeInAColumn(const CMat &Mat, CPosition &Pos, unsigned &HowMany)
Chek if there is at least 3 token in the same column.
Definition: game.cpp:65
std::map< std::string, char > MapParamChar
Definition: type.h:37
void LoadParams(CMyParam &Param)
Load the set of parameters from a YAML file.
Definition: params.cpp:30
Set of functions usefull for the game.
bool AtLeastThreeInARow(const CMat &Mat, CPosition &Pos, unsigned &HowMany)
Chek if there is at least 3 token in the same row.
Definition: game.cpp:47
const unsigned KImpossible
KImpossible :
Definition: params.h:28
void RemovalInColumn(CMat &Mat, const CPosition &Pos, const unsigned &HowMany)
Remove "Howmany" same tokens starting from "Pos".
Definition: game.cpp:84
unsigned ComputeScore(const unsigned &HowMany)
Computer the score.
Definition: game.cpp:106
std::pair< unsigned, unsigned > CPosition
CPosition : a pair gathering the coordinates in the grid.
Definition: type.h:30
void DisplayGrid(const CMat &Mat, const CMyParam &Params, bool ShowLineNumber=true, bool ShowColor=true)
Display the grid according to the parameters.
Struct containing all the game&#39;s parameters.
Definition: type.h:35
int ppal()
new main
Definition: game.cpp:111
void RemovalInRow(CMat &Mat, const CPosition &Pos, const unsigned &HowMany)
Remove "Howmany" same tokens starting from "Pos".
Definition: game.cpp:96
Paramters&#39; definition and associated functions.
std::vector< CVLine > CMat
CMat : alias to a game grid type.
Definition: type.h:25
void InitGrid(CMat &Grid, const unsigned &Size)
Initialize the grid according to the size.
void Menu(CPosition &Pos, char &Direction, const CMyParam &Params)
Display the menu according to the set of parameters.
Definition: game.cpp:17
void MakeAMove(CMat &Grid, const CPosition &Pos, const char &Direction, const CMyParam &Params)
Swap the token from its inital place to its final destination.
Definition: game.cpp:31
const unsigned KMaxTimes
KMaxTimes : number of maximum allowed turns to win.
Definition: params.h:23
Set of usefull functions for the grid management.
void InitParams(CMyParam &Param)
Initialize the set of parameters from scratch.
Definition: params.cpp:11