Salut à tous, étant débutant dans ce langage, je souhaiterai avoir des avis sur mon code(améliorations du code, organisation etc.).
Je vous laisse mon code source afin de voir ce qu'il en est:
Main.cpp
Game.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 #include <iostream> #include "Game.h" using namespace std; int main(){ Game g; g.play(); return 0; }
Game.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 #ifndef GAME_H #define GAME_H #include <array> enum PlayerChar { Empty, // Empty cell Round, // Round piece / player1 Cross // Cross piece / player2 }; class Game { public: Game(); void play(); // Lance le jeu(appel des différentes méthodes) private: void resetBoard(); // Efface le plateau void printBoard() const; // Affiche le plateau void switchPlayer(); // Change de joueur après avoir jouer char getPlayer(PlayerChar& p); // Retourne le joueur courant void putPiece(); // Place piece du joueur bool isSlotEmpty(int slot) const; // Retourne TRUE si le slot est vide bool isBoardFull() const; // Retourne TRUE si le plateau est plein bool checkLine() const; // Retourne TRUE si ligne gagnante bool checkColumn() const; // Retourne TRUE si colonne gagnante bool checkDiagonal() const; // Retourne TRUE si diagonale gagnante bool checkWon() const; // Retourne TRUE si le jeu est gagné std::array<PlayerChar, 9> mBoard; // Plateau de jeu PlayerChar mPlayer; // Joueur courrant int mTurn; // Nombre de tours joués }; #endif // GAME_H
Merci et bonne journée,
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 #include "Game.h" #include <iostream> Game::Game(): mPlayer(Round), mTurn(1){ resetBoard(); } void Game::resetBoard() { mBoard.fill(Empty); } void Game::printBoard() const { std::cout << std::endl << std::endl; if(checkWon()) std::cout << " VICTOIRE" << std::endl; else std::cout << " Tour n°" << mTurn << std::endl; std::cout << "+---+---+---+" << std::endl; for(size_t i = 0; i < mBoard.max_size(); i++) { std::cout << "| "; switch(mBoard[i]) { case Round: std::cout << " O "; break; case Cross: std::cout << " X "; break; case Empty: std::cout << " " << i+1 << " "; break; default: break; } std::cout << " "; if ((i+1) % 3 == 0 && i != 0) { std::cout << "|" << std::endl; std::cout << "+---+---+---+" << std::endl; } } } void Game::play() { while(!isBoardFull() && !checkWon()) { printBoard(); putPiece(); switchPlayer(); mTurn++; } switchPlayer(); printBoard(); std::cout << std::endl; if(isBoardFull() && !checkWon()) std::cout << "Match nul !"; if(checkWon()) { std::cout << "Joueur ["; std::cout << getPlayer(mPlayer); std::cout << "]"; std::cout << " a gagné la partie en "; std::cout << mTurn - 1 << " coups !"; } std::cout << std::endl; } void Game::switchPlayer() { if(mPlayer == Cross) mPlayer = Round; else mPlayer = Cross; } char Game::getPlayer(PlayerChar& p) { return( (p == Cross) ? 'X' : 'O'); } void Game::putPiece() { std::cout << "Joueur ["; std::cout << getPlayer(mPlayer); std::cout << "]" << std::endl; int slot(-1); while(slot < 1 || slot > 9 || !isSlotEmpty(slot - 1)) { std::cout << "Case: "; std::cin >> slot; } mBoard[slot - 1] = mPlayer; } bool Game::isSlotEmpty(int slot) const { return(mBoard[slot] == Empty); } bool Game::checkLine() const { bool res = false; for(size_t i = 0; i < mBoard.max_size(); i += 3) { if(!isSlotEmpty(i)) { if(mBoard[i] == mBoard[i+1] && mBoard[i] == mBoard[i+2]) { res = true; } } } return res; } bool Game::checkColumn() const { bool res = false; for(size_t i = 0; i < 3; i++) { if(!isSlotEmpty(i)) { if(mBoard[i] == mBoard[i+3] && mBoard[i] == mBoard[i+6]) { res = true; } } } return res; } bool Game::checkDiagonal() const { bool res = false; for(size_t i = 0; i < mBoard.max_size(); i++) { if(!isSlotEmpty(0)) { if(mBoard[0] == mBoard[4] && mBoard[0] == mBoard[8]) { res = true; } } if(!isSlotEmpty(2)) { if(mBoard[2] == mBoard[4] && mBoard[2] == mBoard[6]) { res = true; } } } return res; } bool Game::isBoardFull() const { bool res = true; for(size_t i = 0; i < mBoard.max_size(); i++) { if(mBoard[i] == Empty) { res = false; } } return res; } bool Game::checkWon() const { if(checkLine() || checkDiagonal() || checkColumn()) { return true; } else { return false; } }
Partager