catch me if you can  0.1
game.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "game.h"
4 #include "params.h"
5 #include "gridmanagement.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 }// ShowMap ()
14 
15 void MoveToken (CMat & Mat, const char & Move, CPosition & Pos)
16 {
17  char car = Mat [Pos.first][Pos.second];
18  Mat [Pos.first][Pos.second] = KEmpty;
19  switch (Move)
20  {
21  case 'A':
22  -- Pos.first;
23  -- Pos.second;
24  break;
25  case 'Z':
26  --Pos.first;
27  break;
28  case 'E':
29  --Pos.first;
30  ++Pos.second;
31  break;
32  case 'Q':
33  --Pos.second;
34  break;
35  case 'D':
36  ++Pos.second;
37  break;
38  case 'W':
39  ++Pos.first;
40  --Pos.second;
41  break;
42  case 'X':
43  ++Pos.first;
44  break;
45  case 'C':
46  ++Pos.first;
47  ++Pos.second;
48  break;
49  }
50  Mat [Pos.first][Pos.second] = car;
51 } //MoveToken ()
52 
53 
54 int ppal (void)
55 {
56 
57  const unsigned KSize (10);
58  unsigned PartyNum (1);
59  const unsigned KMaxPartyNum (KSize * KSize);
60  CMat Mat;
61 
62  bool Player1Turn (true);
63  bool Victory (false);
64 
65  CPosition PosPlayer1, PosPlayer2;
66 
67 
68  InitGrid(Mat, 10, 10, PosPlayer1, PosPlayer2);
69 
70  DisplayGrid (Mat);
71 
72  while (PartyNum <= KMaxPartyNum && ! Victory)
73  {
74 
75  cout << "tour numero : " << PartyNum << ", Joueur"
76  << (Player1Turn ? '1' : '2') << ", entrez un déplacement : ";
77 
78  char Move;
79  cin >> Move;
80 
81  Move = toupper (Move);
82  MoveToken (Mat, Move, (Player1Turn ? PosPlayer1: PosPlayer2));
83  ClearScreen();
84  DisplayGrid (Mat);
85 
86  //Victiry test
87  if (PosPlayer1 == PosPlayer2) Victory = true;
88 
89  //Increase party's number
90  ++PartyNum;
91 
92  //Player changing
93  Player1Turn = !Player1Turn;
94  }//while (no victory)
95 
96  if (!Victory)
97  {
98  Color (KColor.find("KMAgenta")->second);
99  cout << "Aucun vainqueur" << endl;
100  return 1;
101  }
102 
103  Color (KColor.find("KGreen")->second);
104  cout << "Félicitations Joueur" << (Player1Turn ? '2' : '1')
105  << " vous avez gagné :)" << endl;
106  Color (KColor.find("KReset")->second);
107  return 0;
108 } //ppal ()
const char KEmpty
KEmpty : character for an empty cell.
Definition: type.h:71
void Color(const string &Col)
void ClearScreen()
Clear the current terminal.
Set of usefull functions for the game.
std::pair< unsigned, unsigned > CPosition
CPosition : a pair gathering the coordinates in the grid.
Definition: type.h:30
Paramters&#39; definition and associated functions.
std::vector< CVLine > CMat
CMat : alias to a game grid type.
Definition: type.h:25
int ppal(void)
new main
Definition: game.cpp:54
const std::map< std::string, std::string > KColor
KColor : map between the "human" color and its correspondence for the Unix terminal.
Definition: type.h:56
void DisplayGrid(const CMat &Mat)
void ShowMap(const map< T, U > &AMap)
Definition: game.cpp:10
Set of usefull functions for the grid management.
void InitGrid(CMat &Mat, unsigned NbLine, unsigned NbColumn, CPosition &PosPlayer1, CPosition &PosPlayer2)
void MoveToken(CMat &Mat, const char &Move, CPosition &Pos)
Move the current token according to the character in the 2nd parameter.
Definition: game.cpp:15