catch me if you can  0.1
gridmanagement.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include "gridmanagement.h"
4 
5 #include "type.h" //nos types
6 
7 
8 using namespace std;
9 
10 
11 
13 {
14  cout << "\033[H\033[2J";
15 }// ClearScreen ()
16 
17 void Color (const string & Col)
18 {
19  cout << "\033[" << Col.c_str () <<"m";
20 } // Color ()
21 
22 
23 void DisplayGrid (const CMat & Mat)
24 {
25  const unsigned KNbLine = Mat.size ();
26  const unsigned KNbCol = Mat[0].size ();
27  cout << string (KNbCol + 2 , '-') << endl;
28  for (unsigned i (0); i < KNbLine; ++i)
29  {
30  cout << '|';
31  for (char c : Mat[i])
32  {
33 
34  switch (c)
35  {
36  case KEmpty:
37  cout << c;
38  break;
39  case 'X':
40  Color (KColor.find("KBlue")->second);
41  cout << c;
42  Color (KColor.find("KReset")->second);
43  break;
44  case 'O':
45  Color (KColor.find("KBlue")->second);
46  cout << c;
47  Color (KColor.find("KReset")->second);
48  break;
49 
50  }
51  }
52  cout << '|' << endl;
53  }
54  cout << string (KNbCol + 2 , '-') << endl;
55 }// ShowMatrix ()
56 
57 
58 void InitGrid (CMat & Mat, unsigned NbLine, unsigned NbColumn, CPosition & PosPlayer1, CPosition & PosPlayer2)
59 {
60  Mat.resize (NbLine);
61  const CVLine KLine (NbColumn, KEmpty);
62  for (CVLine &ALine : Mat)
63  ALine = KLine;
64 
65  PosPlayer1.first = 0;
66  PosPlayer1.second = NbColumn - 1;
67  Mat [PosPlayer1.first][PosPlayer1.second] = 'X';
68  PosPlayer2.first = NbLine - 1;
69  PosPlayer2.second =0;
70  Mat [PosPlayer2.first][PosPlayer2.second] = 'O';
71 }//InitMat ()
const char KEmpty
KEmpty : character for an empty cell.
Definition: type.h:71
void Color(const string &Col)
std::vector< char > CVLine
CVLine : alias to a line of the matrix.
Definition: type.h:19
void ClearScreen()
Clear the current terminal.
std::pair< unsigned, unsigned > CPosition
CPosition : a pair gathering the coordinates in the grid.
Definition: type.h:30
std::vector< CVLine > CMat
CMat : alias to a game grid type.
Definition: type.h:25
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)
Set of usefull functions for the grid management.
void InitGrid(CMat &Mat, unsigned NbLine, unsigned NbColumn, CPosition &PosPlayer1, CPosition &PosPlayer2)
Definition of usefull types or aliases for the project.